Beispiel #1
0
        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");
            }
        }
Beispiel #2
0
 public void Clear()
 {
     head  = null;
     tail  = null;
     Count = 0;
     Keys.Clear();
     Values.Clear();
     Notify?.Invoke($"Dictionary cleared");
 }
Beispiel #3
0
        IEnumerator IEnumerable.GetEnumerator()
        {
            KeyValueNode <TKey, TValue> current = head;

            while (current != null)
            {
                yield return(current.Data);

                current = current.Next;
            }
        }
Beispiel #4
0
        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++;
            }
        }
Beispiel #5
0
        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);
        }
Beispiel #6
0
        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);
        }
Beispiel #7
0
        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);
        }