Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #region ForwardingCollection<T>
- public class ForwardingCollection<T> : ICollection<T>
- {
- public ICollection<T> Items { get; private set; }
- public ForwardingCollection(ICollection<T> coll)
- {
- Items = coll;
- }
- public virtual void Add(T item)
- {
- Items.Add(item);
- }
- public virtual void Clear()
- {
- Items.Clear();
- }
- public virtual bool Contains(T item)
- {
- return Items.Contains(item);
- }
- public virtual void CopyTo(T[] array, int arrayIndex)
- {
- Items.CopyTo(array, arrayIndex);
- }
- public virtual int Count
- {
- get { return Items.Count; }
- }
- public virtual bool IsReadOnly
- {
- get { return Items.IsReadOnly; }
- }
- public virtual bool Remove(T item)
- {
- return Items.Remove(item);
- }
- public virtual IEnumerator<T> GetEnumerator()
- {
- return Items.GetEnumerator();
- }
- System.Collections.IEnumerator System.Collections.IEnumerable.GetEnumerator()
- {
- return GetEnumerator();
- }
- }
- #endregion
- #region ForwardingList<T>
- public class ForwardingList<T> : ForwardingCollection<T>, IList<T>
- {
- public ForwardingList(IList<T> list)
- : base(list)
- {
- }
- public new IList<T> Items
- {
- get { return (IList<T>)base.Items; }
- }
- public int IndexOf(T item)
- {
- return Items.IndexOf(item);
- }
- public void Insert(int index, T item)
- {
- Items.Insert(index, item);
- }
- public void RemoveAt(int index)
- {
- Items.RemoveAt(index);
- }
- public T this[int index]
- {
- get
- {
- return Items[index];
- }
- set
- {
- Items[index] = value;
- }
- }
- }
- #endregion
- internal class FlushingEnumerator<T> : IEnumerator<T>
- {
- private LoopingCollection<T> coll;
- private IEnumerator<T> enumerator;
- public FlushingEnumerator(LoopingCollection<T> coll, IEnumerator<T> enumerator)
- {
- this.coll = coll;
- this.enumerator = enumerator;
- coll.numLoops++;
- }
- public T Current
- {
- get { return enumerator.Current; }
- }
- public void Dispose()
- {
- enumerator.Dispose();
- }
- object System.Collections.IEnumerator.Current
- {
- get { return Current; }
- }
- public bool MoveNext()
- {
- bool hasNext = enumerator.MoveNext();
- if (!hasNext)
- {
- coll.numLoops--;
- if (coll.numLoops == 0)
- {
- coll.flush();
- }
- }
- return hasNext;
- }
- public void Reset()
- {
- enumerator.Reset();
- }
- }
- // Probably not a good name
- public class LoopingCollection<T> : ForwardingCollection<T>
- {
- // Keep track of how many loops are enumerating this collection
- // This is needed for nested loops
- internal int numLoops = 0;
- private List<T> addedItems = new List<T>();
- private List<T> removedItems = new List<T>();
- public LoopingCollection(ICollection<T> coll)
- : base(coll)
- {
- }
- public override void Add(T item)
- {
- if (numLoops == 0)
- {
- Items.Add(item);
- }
- else
- {
- addedItems.Add(item);
- // If there's a pending remove, cancel it
- removedItems.Remove(item);
- }
- }
- public override bool Remove(T item)
- {
- if (numLoops == 0)
- {
- return Items.Remove(item);
- }
- // If there's a pending add, cancel it
- if (addedItems.Remove(item))
- {
- return true;
- }
- if (Items.Contains(item))
- {
- removedItems.Add(item);
- return true;
- }
- return false;
- }
- public override IEnumerator<T> GetEnumerator()
- {
- return new FlushingEnumerator<T>(this, Items.GetEnumerator());
- }
- internal void flush()
- {
- foreach (var item in addedItems)
- Items.Add(item);
- addedItems.Clear();
- foreach (var item in removedItems)
- Items.Remove(item);
- removedItems.Clear();
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement