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); }
public void Clear() { Updates++; Backend.Clear(); First = null; Last = null; }
public LinkedDictionaryEnumerator(AbstractLinkedDictionary <TKey, TValue> parent, bool forward) { _parent = parent; _forward = forward; _current = null; _updates = parent.Updates; }
protected override void SetItem(TKey key, TValue value) { LinkedKeyValuePair l = GetItem(key); if (l != null) { RemoveItem(key); } AddItem(key, value); }
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); }
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); }
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); }
public void Reset() { _current = null; }
public LinkedKeyValuePair(TKey key, TValue value, LinkedKeyValuePair prev, LinkedKeyValuePair next) { KeyValuePair = new KeyValuePair <TKey, TValue>(key, value); Previous = prev; Next = next; }