virtual public TItem this[int index] { get { int relativeIndex; IMultiLinkedList <TItem> list = ListForIndex(index, out relativeIndex); if (list != null) { return(list[relativeIndex]); } return(default(TItem)); } set { int relativeIndex; IMultiLinkedList <TItem> list = ListForIndex(index, out relativeIndex); if (list != null) { // Remove the item at that index and replace it var item = list[relativeIndex]; list.RemoveAt(relativeIndex); list.Insert(relativeIndex, value); OnItemRemoved(item, index); OnItemAdded(item, index); } } }
virtual public void SortKeys(IComparer <TGroup> comparer = null) { var keys = _Dictionary.Keys.ToArray(); _Lists.Clear(); IMultiLinkedList <TItem> previous = null; foreach (var group in _Dictionary.Keys.OrderBy(k => k, comparer)) { var list = _Dictionary[group]; if (previous == null) { previous = list; previous.SetPrevious(null); } else { previous.SetNext(list); list.SetPrevious(previous); previous = list; } _Lists.Add(list); } }
public virtual void SortKeys(IComparer <TGroup> comparer = null) { _lists.Clear(); IMultiLinkedList <TItem> previous = null; foreach (var list in _dictionary.Keys .OrderBy(k => k, comparer) .Select(group => _dictionary[group])) { if (previous == null) { previous = list; previous.SetPrevious(null); } else { previous.SetNext(list); list.SetPrevious(previous); previous = list; } _lists.Add(list); } }
/// <summary> /// Ensures the list. /// </summary> /// <param name="group">The group.</param> /// <param name="createIfNecessary"> /// if set to <c>true</c> [create if necessary]. /// </param> /// <returns></returns> private IMultiLinkedList <TItem> EnsureList(TGroup group, bool createIfNecessary) { if (!_dictionary.ContainsKey(group)) { if (createIfNecessary) { var list = new MultiLinkedList <TItem>( ); _dictionary[group] = list; if (_lists.Count > 0) { // Attach the list to our list chain IMultiLinkedList <TItem> previous = _lists[_lists.Count - 1]; previous.SetNext(list); list.SetPrevious(previous); } _lists.Add(list); return(list); } } else { return(_dictionary[group]); } return(null); }
virtual public void Insert(int index, TItem item) { int relativeIndex; IMultiLinkedList <TItem> list = ListForIndex(index, out relativeIndex); if (list != null) { list.Insert(relativeIndex, item); OnItemAdded(item, index); } }
virtual public void RemoveAt(int index) { int relativeIndex; IMultiLinkedList <TItem> list = ListForIndex(index, out relativeIndex); if (list != null) { TItem item = list[relativeIndex]; list.RemoveAt(relativeIndex); OnItemRemoved(item, index); } }
/// <summary> /// Adds an item to the <see cref="T:System.Collections.Generic.ICollection`1" />. /// </summary> /// <param name="item"> /// The object to add to the <see cref="T:System.Collections.Generic.ICollection`1" />. /// </param> public virtual void Add(TItem item) { if (item != null) { // Get the "real" group for this item TGroup group = GroupModifier(item.Group); // Add a new list if necessary IMultiLinkedList <TItem> list = EnsureList(group, true); int index = list.Count; list.Add(SubscribeToKeyChanges(item)); OnItemAdded(item, list.StartIndex + index); } }
/// <summary> /// Removes the specified group. /// </summary> /// <param name="group">The group.</param> /// <returns></returns> public virtual bool Remove(TGroup group) { group = GroupModifier(group); if (_dictionary.ContainsKey(group)) { IMultiLinkedList <TItem> list = _dictionary[group]; for (int i = list.Count - 1; i >= 0; i--) { TItem obj = list[i]; list.RemoveAt(i); OnItemRemoved(UnsubscribeFromKeyChanges(obj), list.StartIndex + i); } return(true); } return(false); }
/// <summary> /// Removes the specified obj. /// </summary> /// <param name="obj">The obj.</param> /// <returns></returns> public virtual bool Remove(TItem obj) { TGroup group = GroupModifier(obj.Group); if (_dictionary.ContainsKey(group)) { IMultiLinkedList <TItem> items = _dictionary[group]; int index = items.IndexOf(obj); if (index >= 0) { items.RemoveAt(index); OnItemRemoved(UnsubscribeFromKeyChanges(obj), index); return(true); } } return(false); }
void item_GroupChanged(object sender, ObjectEventArgs <TGroup, TGroup> e) { TGroup oldValue = e.First; TGroup newValue = e.Second; TItem obj = sender as TItem; if (obj != null) { // Remove the object from the hash table // based on the old group. if (!object.Equals(oldValue, default(TGroup))) { // Find the specific item and remove it TGroup group = GroupModifier(oldValue); if (_Dictionary.ContainsKey(group)) { IMultiLinkedList <TItem> items = _Dictionary[group]; // Find the item's index within the list int index = items.IndexOf(obj); if (index >= 0) { // Get a reference to the object TItem item = items[index]; // Remove the object items.RemoveAt(index); // Notify that this item was removed, with the overall // index of the item in the keyed list. OnItemRemoved(UnsubscribeFromKeyChanges(item), items.StartIndex + index); } } } // If a new group exists, then re-add this item into the hash if (!object.Equals(newValue, default(TGroup))) { Add(obj); } } }
/// <summary> /// Determines the index of a specific item in the <see cref="T:System.Collections.Generic.IList`1" />. /// </summary> /// <param name="item"> /// The object to locate in the <see cref="T:System.Collections.Generic.IList`1" />. /// </param> /// <returns> /// The index of <paramref name="item" /> if found in the list; otherwise, -1. /// </returns> public virtual int IndexOf(TItem item) { // Get the "real" group TGroup group = GroupModifier(item.Group); if (_dictionary.ContainsKey(group)) { // Get the list associated with this object's group IMultiLinkedList <TItem> list = _dictionary[group]; // Find the object within the list. int index = list.IndexOf(item); // Return the index within the overall KeyedList if (index >= 0) { return(list.StartIndex + index); } } return(-1); }
virtual public void SetNext(IMultiLinkedList <TType> next) { _Next = next; }
virtual public void SetPrevious(IMultiLinkedList <TType> previous) { _Previous = previous; }