using System; using System.Collections; using System.Collections.Generic; namespace Substrate.Utility { public class IndexedLinkedList : ICollection, ICollection { private LinkedList _list; private Dictionary> _index; public T First { get { return _list.First.Value; } } public T Last { get { return _list.Last.Value; } } public IndexedLinkedList () { _list = new LinkedList(); _index = new Dictionary>(); } public void AddFirst (T value) { LinkedListNode node = _list.AddFirst(value); _index.Add(value, node); } public void AddLast (T value) { LinkedListNode node = _list.AddLast(value); _index.Add(value, node); } public void RemoveFirst () { _index.Remove(_list.First.Value); _list.RemoveFirst(); } public void RemoveLast () { _index.Remove(_list.First.Value); _list.RemoveLast(); } #region ICollection Members public void Add (T item) { AddLast(item); } public void Clear () { _index.Clear(); _list.Clear(); } public bool Contains (T item) { return _index.ContainsKey(item); } public void CopyTo (T[] array, int arrayIndex) { _list.CopyTo(array, arrayIndex); } public bool IsReadOnly { get { return false; } } public bool Remove (T value) { LinkedListNode node; if (_index.TryGetValue(value, out node)) { _index.Remove(value); _list.Remove(node); return true; } return false; } #endregion #region ICollection Members void ICollection.CopyTo (Array array, int index) { (_list as ICollection).CopyTo(array, index); } public int Count { get { return _list.Count; } } bool ICollection.IsSynchronized { get { return false; } } object ICollection.SyncRoot { get { return (_list as ICollection).SyncRoot; } } #endregion #region IEnumerable Members public IEnumerator GetEnumerator () { return _list.GetEnumerator(); } #endregion #region IEnumerable Members IEnumerator IEnumerable.GetEnumerator () { return _list.GetEnumerator(); } #endregion } }