public void Add(KeyValuePair <TKey, TValue> data) { KeyValueNode <TKey, TValue> node = new KeyValueNode <TKey, TValue>(data); if (!Contains(data)) { if (head == null) { head = node; } else { tail.Next = node; } tail = node; Count++; Keys.Add(data.Key); Values.Add(data.Value); Notify?.Invoke($"Added key {data.Key}, value {data.Value}"); } else { throw new ArgumentException($"Key {data.Key} is already in the dictionary"); } }
public void Clear() { head = null; tail = null; Count = 0; Keys.Clear(); Values.Clear(); Notify?.Invoke($"Dictionary cleared"); }
IEnumerator IEnumerable.GetEnumerator() { KeyValueNode <TKey, TValue> current = head; while (current != null) { yield return(current.Data); current = current.Next; } }
public void CopyTo(KeyValuePair <TKey, TValue>[] array, int arrayIndex = 0) { KeyValueNode <TKey, TValue> current = head; int i = arrayIndex; while (current != null && i < Count + arrayIndex) { array[i] = current.Data; current = current.Next; i++; } }
public bool ContainsKey(TKey key) { KeyValueNode <TKey, TValue> current = head; while (current != null) { if (Comparer.Equals(current.Data.Key, key)) { return(true); } current = current.Next; } return(false); }
public bool TryGetValue(TKey key, out TValue value) { KeyValueNode <TKey, TValue> current = head; while (current != null) { if (Comparer.Equals(current.Data.Key, key)) { value = current.Data.Value; return(true); } current = current.Next; } value = default; return(false); }
public bool Remove(TKey key) { KeyValueNode <TKey, TValue> current = head; KeyValueNode <TKey, TValue> previous = null; while (current != null) { if (Comparer.Equals(current.Data.Key, key)) { Values.Remove(this[key]); Keys.Remove(key); Count--; if (previous != null) { previous.Next = current.Next; if (current.Next == null) { tail = previous; } } else { head = head.Next; if (head == null) { tail = null; } } Notify?.Invoke($"Removed key {key}"); return(true); } previous = current; current = current.Next; } return(false); }