Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using DictionaryWorkshop.Implementation.Contracts;
- using System;
- using System.Collections;
- using System.Collections.Generic;
- using System.Linq;
- using System.Text;
- namespace DictionaryWorkshop.Implementation
- {
- public class MyDictionary<TKey, TValue> : IMyDictionary<TKey, TValue>, IEnumerable<MyKeyValuePair<TKey, TValue>>
- {
- private const int initialCap = 16;
- private const float loadFactor = 0.75f;
- private MyLinkedList<TKey, TValue>[] buckets;
- public MyDictionary()
- {
- this.buckets = new MyLinkedList<TKey, TValue>[initialCap];
- }
- public MyDictionary(int capacity)
- {
- this.buckets = new MyLinkedList<TKey, TValue>[capacity];
- this.Count = 0;
- }
- public int Count { get; private set; }
- public int Capacity { get => this.buckets.Length; }
- public TValue this[TKey key]
- {
- get
- {
- return this.Get(key);
- }
- set
- {
- if (value != null)
- {
- AddOrReplace(key, value);
- }
- }
- }
- public TValue Get (TKey key)
- {
- var item = Find(key);
- if (item==null)
- {
- throw new KeyNotFoundException();
- }
- return item.Value;
- }
- public bool Add(TKey key, TValue value)
- {
- if ((float)(this.Count + 1) / this.Capacity > loadFactor)
- {
- this.Resize();
- }
- var index = FindHashKey(key);
- if (this.buckets[index] == null)
- {
- this.buckets[index] = new MyLinkedList<TKey, TValue>(key, value);
- this.Count++;
- return true;
- }
- var ifKeyExist = this.buckets[index].Any(x => x.Key.Equals(key));
- if (ifKeyExist)
- {
- throw new InvalidOperationException("Key already exists!");
- }
- this.buckets[index].Add(key,value);
- this.Count++;
- return true;
- }
- private void Resize()
- {
- var newDictionary = new MyDictionary<TKey, TValue>(2*this.Capacity);
- foreach (var item in this)
- {
- newDictionary.Add(item.Key, item.Value);
- }
- this.buckets = newDictionary.buckets;
- this.Count = newDictionary.Count;
- }
- public bool ContainsKey(TKey key)
- {
- var item = Find(key);
- return item != null;
- }
- private int FindHashKey(TKey key)
- {
- return Math.Abs(key.GetHashCode() % this.buckets.Length);
- }
- public MyKeyValuePair<TKey, TValue> Find (TKey key)
- {
- int index = FindHashKey(key);
- var items = this.buckets[index];
- if (items !=null)
- {
- foreach (var item in items)
- {
- if (item.Key.Equals(key))
- {
- return item;
- }
- }
- }
- return null;
- }
- public bool AddOrReplace(TKey key, TValue value)
- {
- if ((float)(this.Count + 1) / this.Capacity > loadFactor)
- {
- this.Resize();
- }
- int slotNumber = this.FindHashKey(key);
- if (this.buckets[slotNumber] == null)
- {
- this.buckets[slotNumber] = new MyLinkedList<TKey, TValue>(key, value);
- }
- foreach (var item in this.buckets[slotNumber])
- {
- if (item.Key.Equals(key))
- {
- item.Value = value;
- return false;
- }
- }
- this.buckets[slotNumber].Add(key, value);
- this.Count++;
- return true;
- }
- public IEnumerable<TKey> GetKeys()
- {
- return this.buckets.Where(l => l != null)
- .SelectMany(x => x.Select(y => y.Key));
- }
- public IEnumerable<TValue> GetValues()
- {
- return this.buckets.Where(v => v != null)
- .SelectMany(x => x.Select(y => y.Value));
- }
- public bool Remove(TKey key)
- {
- var kvpItem = Find(key);
- if (kvpItem !=null)
- {
- int index = this.FindHashKey(key);
- buckets[index].Remove(key);
- this.Count--;
- return true;
- }
- return false;
- }
- public bool TryGetValue(TKey key, out TValue value)
- {
- var item = this.Find(key);
- if (item!= null)
- {
- value = item.Value;
- return true;
- }
- value = default(TValue);
- return false;
- }
- public IEnumerator<MyKeyValuePair<TKey, TValue>> GetEnumerator()
- {
- foreach (var items in buckets)
- {
- if (items !=null)
- {
- foreach (var item in items)
- {
- yield return item;
- }
- }
- }
- }
- IEnumerator IEnumerable.GetEnumerator()
- {
- return this.GetEnumerator();
- }
- public override string ToString()
- {
- var sb = new StringBuilder();
- foreach (var item in buckets)
- {
- if (item !=null)
- {
- sb.AppendLine(item.ToString());
- }
- }
- return sb.ToString().Trim();
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement