Advertisement
Guest User

Untitled

a guest
Mar 6th, 2015
199
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.07 KB | None | 0 0
  1. #region ForwardingCollection<T>
  2.  
  3. public class ForwardingCollection<T> : ICollection<T>
  4. {
  5. public ICollection<T> Items { get; private set; }
  6.  
  7. public ForwardingCollection(ICollection<T> coll)
  8. {
  9. Items = coll;
  10. }
  11.  
  12. public virtual void Add(T item)
  13. {
  14. Items.Add(item);
  15. }
  16.  
  17. public virtual void Clear()
  18. {
  19. Items.Clear();
  20. }
  21.  
  22. public virtual bool Contains(T item)
  23. {
  24. return Items.Contains(item);
  25. }
  26.  
  27. public virtual void CopyTo(T[] array, int arrayIndex)
  28. {
  29. Items.CopyTo(array, arrayIndex);
  30. }
  31.  
  32. public virtual int Count
  33. {
  34. get { return Items.Count; }
  35. }
  36.  
  37. public virtual bool IsReadOnly
  38. {
  39. get { return Items.IsReadOnly; }
  40. }
  41.  
  42. public virtual bool Remove(T item)
  43. {
  44. return Items.Remove(item);
  45. }
  46.  
  47. public virtual IEnumerator<T> GetEnumerator()
  48. {
  49. return Items.GetEnumerator();
  50. }
  51.  
  52. System.Collections.IEnumerator System.Collections.IEnumerable.GetEnumerator()
  53. {
  54. return GetEnumerator();
  55. }
  56. }
  57.  
  58. #endregion
  59.  
  60. #region ForwardingList<T>
  61.  
  62. public class ForwardingList<T> : ForwardingCollection<T>, IList<T>
  63. {
  64. public ForwardingList(IList<T> list)
  65. : base(list)
  66. {
  67.  
  68. }
  69.  
  70. public new IList<T> Items
  71. {
  72. get { return (IList<T>)base.Items; }
  73. }
  74.  
  75. public int IndexOf(T item)
  76. {
  77. return Items.IndexOf(item);
  78. }
  79.  
  80. public void Insert(int index, T item)
  81. {
  82. Items.Insert(index, item);
  83. }
  84.  
  85. public void RemoveAt(int index)
  86. {
  87. Items.RemoveAt(index);
  88. }
  89.  
  90. public T this[int index]
  91. {
  92. get
  93. {
  94. return Items[index];
  95. }
  96. set
  97. {
  98. Items[index] = value;
  99. }
  100. }
  101. }
  102.  
  103. #endregion
  104.  
  105. internal class FlushingEnumerator<T> : IEnumerator<T>
  106. {
  107. private LoopingCollection<T> coll;
  108. private IEnumerator<T> enumerator;
  109.  
  110. public FlushingEnumerator(LoopingCollection<T> coll, IEnumerator<T> enumerator)
  111. {
  112. this.coll = coll;
  113. this.enumerator = enumerator;
  114.  
  115. coll.numLoops++;
  116. }
  117.  
  118. public T Current
  119. {
  120. get { return enumerator.Current; }
  121. }
  122.  
  123. public void Dispose()
  124. {
  125. enumerator.Dispose();
  126. }
  127.  
  128. object System.Collections.IEnumerator.Current
  129. {
  130. get { return Current; }
  131. }
  132.  
  133. public bool MoveNext()
  134. {
  135. bool hasNext = enumerator.MoveNext();
  136. if (!hasNext)
  137. {
  138. coll.numLoops--;
  139. if (coll.numLoops == 0)
  140. {
  141. coll.flush();
  142. }
  143. }
  144. return hasNext;
  145. }
  146.  
  147. public void Reset()
  148. {
  149. enumerator.Reset();
  150. }
  151. }
  152.  
  153. // Probably not a good name
  154. public class LoopingCollection<T> : ForwardingCollection<T>
  155. {
  156. // Keep track of how many loops are enumerating this collection
  157. // This is needed for nested loops
  158. internal int numLoops = 0;
  159.  
  160. private List<T> addedItems = new List<T>();
  161. private List<T> removedItems = new List<T>();
  162.  
  163. public LoopingCollection(ICollection<T> coll)
  164. : base(coll)
  165. {
  166.  
  167. }
  168.  
  169. public override void Add(T item)
  170. {
  171. if (numLoops == 0)
  172. {
  173. Items.Add(item);
  174. }
  175. else
  176. {
  177. addedItems.Add(item);
  178.  
  179. // If there's a pending remove, cancel it
  180. removedItems.Remove(item);
  181. }
  182. }
  183.  
  184. public override bool Remove(T item)
  185. {
  186. if (numLoops == 0)
  187. {
  188. return Items.Remove(item);
  189. }
  190.  
  191. // If there's a pending add, cancel it
  192. if (addedItems.Remove(item))
  193. {
  194. return true;
  195. }
  196.  
  197. if (Items.Contains(item))
  198. {
  199. removedItems.Add(item);
  200. return true;
  201. }
  202.  
  203. return false;
  204. }
  205.  
  206. public override IEnumerator<T> GetEnumerator()
  207. {
  208. return new FlushingEnumerator<T>(this, Items.GetEnumerator());
  209. }
  210.  
  211. internal void flush()
  212. {
  213. foreach (var item in addedItems)
  214. Items.Add(item);
  215. addedItems.Clear();
  216.  
  217. foreach (var item in removedItems)
  218. Items.Remove(item);
  219. removedItems.Clear();
  220. }
  221. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement