public DrLinkedValue(TKey key, TValue value, DrLinkedValue previous, DrLinkedValue next) { this.Key = key; this.Value = value; this.Previous = previous; this.Next = next; }
/// <summary> /// rebuild links for the item which will be removed /// </summary> /// <param name="value">value which will be removed</param> private void rebuildLinkBeforeRemoveItem(DrLinkedValue value) { try { if ((value.IsFirst) && (value.IsLast)) { return; // dictionary has only one item } if (value.IsFirst) { value.Next.Previous = null; return; } // the first element will be removed if (value.IsLast) { value.Previous.Next = null; return; } // the last element will be removed value.Previous.Next = value.Next; value.Next.Previous = value.Previous; } catch (Exception e) { if (value == null) { throw new ApplicationException(Res.Msg.CANNOT_REBUILD_LINKS_VALUE_IS_NULL, e); } if (value.Key == null) { throw new ApplicationException(Res.Msg.CANNOT_REBUILD_LINKS_KEY_IS_NULL, e); } throw new ApplicationException(string.Format(Res.Msg.CANNOT_REBUILD_LINKS_FOR_ITEM, value.Key.ToString()), e); } }
/// <summary> /// /// </summary> /// <param name="afterKey">if value is null item will be inserted as last element</param> /// <param name="key"></param> /// <param name="value"></param> public void InsertAfter(TKey afterKey, TKey key, TValue value) { if (afterKey == null) { InsertAsLast(key, value); } else { DrLinkedValue afterValue; if (dic.TryGetValue(afterKey, out afterValue)) { var item = new DrLinkedValue(key, value, afterValue, afterValue.Next); dic.Add(key, item); if (afterValue.IsLast) { this.last = item; } else { afterValue.Next.Previous = item; } afterValue.Next = item; } else { throw new ApplicationException(string.Format(Res.Msg.CANNOT_INSERT_AFTER_KEY_NOT_FOUND, afterKey.ToString())); } } }
/// <summary> /// /// </summary> /// <param name="beforeKey">if value is null item will be inserted as first element</param> /// <param name="key"></param> /// <param name="value"></param> public void InsertBefore(TKey beforeKey, TKey key, TValue value) { if (beforeKey == null) { InsertAsFirst(key, value); } else { DrLinkedValue beforeValue; if (dic.TryGetValue(beforeKey, out beforeValue)) { var item = new DrLinkedValue(key, value, beforeValue.Previous, beforeValue); dic.Add(key, item); if (beforeValue.IsFirst) { this.first = item; } else { beforeValue.Previous.Next = item; } beforeValue.Previous = item; } else { throw new ApplicationException(string.Format(Res.Msg.CANNOT_INSERT_BEFORE_KEY_NOT_FOUND, beforeKey.ToString())); } } }
public void InsertAsFirst(TKey key, TValue value) { var item = new DrLinkedValue(key, value, null, first); // first is null if dictionary does not have any items dic.Add(key, item); if (first != null) { first.Previous = item; } this.first = item; // the following code will be executed only if the item was successfully added to the dictionary }
public LinkedEnumerator(DrLinkedDictonary <TKey, TValue> linkedDictionary, DrEnumerationRules eRules) { this.dic = linkedDictionary; this.eRules = (DrEnumerationRules)eRules.Clone(); if (this.eRules.IsFirstItemRelative) { this.startFrom = this.eRules.Direction == EDirection.FORWARD ? linkedDictionary.first : linkedDictionary.last; } else { this.startFrom = linkedDictionary.dic[this.eRules.StartFromKey]; } nextLinkedValue = this.startFrom; currentLinkedValue = default(DrLinkedValue); }
public bool MoveNext() { if (nextLinkedValue == null) { return(false); } currentLinkedValue = nextLinkedValue; if (this.eRules.Direction == EDirection.FORWARD) { nextLinkedValue = nextLinkedValue.Next; } else { nextLinkedValue = nextLinkedValue.Previous; } return(true); }
public void Add(TKey key, TValue value) { var item = new DrLinkedValue(key, value, last, null); dic.Add(key, item); {// the following code will be executed only if the item was successfully added to the dictionary if (last != null) { last.Next = item; } last = item; if (first == null) { first = item; } } }