Example #1
0
        protected override bool RemoveItem(TKey key)
        {
            LinkedKeyValuePair l;

            if (Backend.TryGetValue(key, out l))
            {
                if (l != null)
                {
                    LinkedKeyValuePair pre = l.Previous;
                    LinkedKeyValuePair nxt = l.Next;

                    if (pre != null)
                    {
                        pre.Next = nxt;
                    }
                    else
                    {
                        First = nxt;
                    }
                    if (nxt != null)
                    {
                        nxt.Previous = pre;
                    }
                    else
                    {
                        Last = pre;
                    }
                }

                return(Backend.Remove(key));
            }

            return(false);
        }
Example #2
0
 public void Clear()
 {
     Updates++;
     Backend.Clear();
     First = null;
     Last  = null;
 }
Example #3
0
 public LinkedDictionaryEnumerator(AbstractLinkedDictionary <TKey, TValue> parent, bool forward)
 {
     _parent  = parent;
     _forward = forward;
     _current = null;
     _updates = parent.Updates;
 }
Example #4
0
        protected override void SetItem(TKey key, TValue value)
        {
            LinkedKeyValuePair l = GetItem(key);

            if (l != null)
            {
                RemoveItem(key);
            }

            AddItem(key, value);
        }
Example #5
0
 public bool MoveNext()
 {
     if (_parent.Updates != _updates)
     {
         throw new InvalidOperationException("Collection was modified after the enumerator was created");
     }
     if (_current == null)
     {
         _current = _forward ? _parent.First : _parent.Last;
     }
     else
     {
         _current = _forward ? _current.Next : _current.Previous;
     }
     return(_current != null);
 }
Example #6
0
        protected override LinkedKeyValuePair GetItem(TKey key)
        {
            LinkedKeyValuePair l;

            if (!Backend.TryGetValue(key, out l))
            {
                return(null);
            }

            if (l == null)
            {
                return(null);
            }

            LinkedKeyValuePair nxt = l.Next;

            if (nxt != null) // last => no-change
            {
                Updates++;   // looking is updating
                // note, atleast 2 items in chain now, since l != last
                LinkedKeyValuePair pre = l.Previous;
                if (pre == null)
                {
                    First = nxt;
                }
                else
                {
                    pre.Next = l.Next;
                }

                nxt.Previous = pre; // nxt != null since l != last
                Last.Next    = l;
                l.Next       = null;
                l.Previous   = Last;
                Last         = l;
            }

            return(l);
        }
Example #7
0
        protected override void AddItem(TKey key, TValue value)
        {
            if (Backend.ContainsKey(key))
            {
                throw new ArgumentException($"Key \"{key}\" already present in dictionary");
            }

            var l = new LinkedKeyValuePair(key, value, Last, null);

            if (Last != null)
            {
                Last.Next = l;
            }

            Last = l;
            if (First == null)
            {
                First = l;
            }

            Backend.Add(key, l);
        }
Example #8
0
 public void Reset()
 {
     _current = null;
 }
Example #9
0
 public LinkedKeyValuePair(TKey key, TValue value, LinkedKeyValuePair prev, LinkedKeyValuePair next)
 {
     KeyValuePair = new KeyValuePair <TKey, TValue>(key, value);
     Previous     = prev;
     Next         = next;
 }