Last active
March 20, 2024 21:58
-
-
Save GigaOrts/60542f9f685ad7c62afa588e9eb8c61f to your computer and use it in GitHub Desktop.
SLOT vs DICTIONARY
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Dictionary class-Key - как использовать ссылочный тип в словаре как ключ. | |
Dictionary struct-Key - то же что и первый вариант, но проще. | |
Slot vs Dictionary - сравнение вариантов хранения через класс Slot и через коллекцию Dirctionary | |
при том, что надо сохранить одинаковый функционал. | |
Вариант со Slot гибче, но громоздкий. Так что где достаточно Dictionary, используйте его. |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
namespace Storage | |
{ | |
internal class Program | |
{ | |
static void Main(string[] args) | |
{ | |
var product = new Product("A", 1); | |
var product2 = new Product("A", 1); | |
int amount = 5; | |
Storage storage = new Storage(); | |
storage.Add(product, amount); | |
storage.Show(); | |
Console.WriteLine("-------------------"); | |
storage.Add(product2, amount); | |
storage.Show(); | |
} | |
} | |
class Storage | |
{ | |
private readonly Dictionary<Product, int> _products = new(); | |
public void Add(Product product, int amount) | |
{ | |
if (_products.ContainsKey(product)) | |
{ | |
_products[product] += amount; | |
} | |
else | |
{ | |
_products[product] = amount; | |
} | |
} | |
public void Remove(Product product, int amount) | |
{ | |
if (_products.ContainsKey(product) == false) | |
throw new InvalidOperationException(); | |
if (_products[product] >= amount) | |
{ | |
_products[product] -= amount; | |
} | |
if (_products[product] == 0) | |
_products.Remove(product); | |
} | |
public void Show() | |
{ | |
foreach (var item in _products) | |
{ | |
Console.WriteLine($"{item.Key.Name} - {item.Value}"); | |
} | |
} | |
} | |
class Product | |
{ | |
public Product(string name, int price) | |
{ | |
Name = name; | |
Price = price; | |
} | |
public string Name { get; } | |
public int Price { get; } | |
public override bool Equals(object obj) | |
{ | |
return obj is Product product && | |
Name == product.Name && | |
Price == product.Price; | |
} | |
public override int GetHashCode() | |
{ | |
return HashCode.Combine(Name, Price); | |
} | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
namespace Storage | |
{ | |
internal class Program | |
{ | |
static void Main(string[] args) | |
{ | |
var product = new Product("A", 1); | |
var product2 = new Product("A", 1); | |
int amount = 5; | |
Storage storage = new Storage(); | |
storage.Add(product, amount); | |
storage.Show(); | |
Console.WriteLine("-------------------"); | |
storage.Add(product2, amount); | |
storage.Show(); | |
} | |
} | |
class Storage | |
{ | |
private readonly Dictionary<Product, int> _products = new(); | |
public void Add(Product product, int amount) | |
{ | |
if (_products.ContainsKey(product)) | |
{ | |
_products[product] += amount; | |
} | |
else | |
{ | |
_products[product] = amount; | |
} | |
} | |
public void Remove(Product product, int amount) | |
{ | |
if (_products.ContainsKey(product) == false) | |
throw new InvalidOperationException(); | |
if (_products[product] >= amount) | |
{ | |
_products[product] -= amount; | |
} | |
if (_products[product] == 0) | |
_products.Remove(product); | |
} | |
public void Show() | |
{ | |
foreach (var item in _products) | |
{ | |
Console.WriteLine($"{item.Key.Name} - {item.Value}"); | |
} | |
} | |
} | |
struct Product | |
{ | |
public Product(string name, int price) | |
{ | |
Name = name; | |
Price = price; | |
} | |
public string Name { get; } | |
public int Price { get; } | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
internal class Program | |
{ | |
static void Main(string[] args) | |
{ | |
SlotExample(); | |
Console.WriteLine("\n\n"); | |
DirctionaryExample(); | |
} | |
private static void SlotExample() | |
{ | |
var product = new Product("A", 1); | |
var product2 = new Product("A", 1); | |
int amount = 5; | |
var slot = new Slot(product, amount); | |
var slot2 = new Slot(product2, amount); | |
StorageSlot storage = new StorageSlot(); | |
Console.WriteLine("------------------- SlotExample -------------------"); | |
Console.WriteLine("--------- Add Slot +5 ---------"); | |
storage.Add(slot); | |
storage.Show(); | |
Console.WriteLine("--------- AddDuplicate Slot +5 ---------"); | |
storage.Add(slot2); | |
storage.Show(); | |
Console.WriteLine("--------- Remove Slot -5 ---------"); | |
storage.Remove(slot); | |
storage.Show(); | |
Console.WriteLine("--------- AddDuplicate Slot +5 (Add Overload(Product, amount) ---------"); | |
storage.Add(product, amount); | |
storage.Show(); | |
Console.WriteLine("--------- Remove Slot -5 (Remove Overload(Product, amount) ---------"); | |
storage.Remove(product, amount); | |
storage.Show(); | |
} | |
private static void DirctionaryExample() | |
{ | |
var product = new Product("A", 1); | |
var product2 = new Product("A", 1); | |
int amount = 5; | |
var storage = new StorageDictionary(); | |
Console.WriteLine("------------------- DirctionaryExample -------------------"); | |
Console.WriteLine("--------- Add Amount +5 ---------"); | |
storage.Add(product, amount); | |
storage.Show(); | |
Console.WriteLine("--------- AddDuplicate +5 ---------"); | |
storage.Add(product2, amount); | |
storage.Show(); | |
Console.WriteLine("--------- Remove Amount -3 ---------"); | |
amount = 3; | |
storage.Remove(product, amount); | |
storage.Show(); | |
} | |
} | |
class StorageSlot | |
{ | |
private readonly List<Slot> _slots = new(); | |
public void Add(Slot slot) | |
{ | |
if (_slots.Contains(slot) == false) | |
{ | |
_slots.Add(slot.Clone()); | |
} | |
else | |
{ | |
int index = _slots.IndexOf(slot); | |
_slots[index].Add(slot.Count); | |
} | |
} | |
public void Add(Product product, int amount) | |
{ | |
if (Contains(product, out Slot slot) == false) | |
{ | |
_slots.Add(new Slot(new Product(product.Name, product.Price), amount)); | |
} | |
else | |
{ | |
slot.Add(amount); | |
} | |
} | |
public void Remove(Slot slot) | |
{ | |
if (_slots.Contains(slot) == false) | |
throw new InvalidOperationException(); | |
int index = _slots.IndexOf(slot); | |
_slots[index].Remove(slot.Count); | |
if (_slots[index].Count == 0) | |
_slots.Remove(slot); | |
} | |
public void Remove(Product product, int amount) | |
{ | |
if (Contains(product, out Slot slot) == false) | |
{ | |
throw new InvalidOperationException(); | |
} | |
else | |
{ | |
slot.Remove(amount); | |
} | |
} | |
private bool Contains(Product product, out Slot slot) | |
{ | |
foreach (var item in _slots) | |
{ | |
if (item.Product.Equals(product)) | |
{ | |
slot = item; | |
return true; | |
} | |
} | |
slot = null; | |
return false; | |
} | |
public void Show() | |
{ | |
foreach (var item in _slots) | |
{ | |
Console.WriteLine(item); | |
} | |
} | |
} | |
class Slot | |
{ | |
public Slot(Product product, int count) | |
{ | |
Product = product; | |
Count = count; | |
} | |
public Product Product { get; } | |
public int Count { get; private set; } | |
public void Add(int amount) | |
{ | |
Count += amount; | |
} | |
public void Remove(int amount) | |
{ | |
if (Count < amount) | |
throw new InvalidOperationException(); | |
Count -= amount; | |
} | |
public override string ToString() | |
{ | |
return $"{Product} Count: {Count}"; | |
} | |
public Slot Clone() | |
{ | |
return new Slot(new Product(Product.Name, Product.Price), Count); | |
} | |
public override bool Equals(object obj) | |
{ | |
return obj is Slot slot && | |
slot.Product.Equals(Product); | |
} | |
public override int GetHashCode() | |
{ | |
return Product.GetHashCode(); | |
} | |
} | |
class StorageDictionary | |
{ | |
private readonly Dictionary<Product, int> _products = new(); | |
public void Add(Product product, int amount) | |
{ | |
if (_products.ContainsKey(product)) | |
{ | |
_products[product] += amount; | |
} | |
else | |
{ | |
_products[product] = amount; | |
} | |
} | |
public void Remove(Product product, int amount) | |
{ | |
if (_products.ContainsKey(product) == false) | |
throw new InvalidOperationException(); | |
if (_products[product] >= amount) | |
{ | |
_products[product] -= amount; | |
} | |
if (_products[product] == 0) | |
_products.Remove(product); | |
} | |
public void Show() | |
{ | |
foreach (var item in _products) | |
{ | |
Console.WriteLine($"{item.Key} Count: {item.Value}"); | |
} | |
} | |
} | |
class Product | |
{ | |
public Product(string name, int price) | |
{ | |
Name = name; | |
Price = price; | |
} | |
public string Name { get; } | |
public int Price { get; } | |
public override string ToString() | |
{ | |
return $"{Name} - ${Price}"; | |
} | |
public override bool Equals(object obj) | |
{ | |
return obj is Product product && | |
Name == product.Name && | |
Price == product.Price; | |
} | |
public override int GetHashCode() | |
{ | |
return HashCode.Combine(Name, Price); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment