123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265 |
- using System;
- using System.Collections;
- using System.Collections.Generic;
- namespace Wayne.Lib
- {
-
-
-
-
-
- public class RefinedList<BaseType, RefinedType> : IList<RefinedType> where RefinedType : BaseType
- {
- #region RefinedEnumerator
- private class RefinedEnumerator : IEnumerator<RefinedType>
- {
- private IList<BaseType> baseList;
- private int index;
- private bool invalidated;
- public event EventHandler OnDisposed;
- public RefinedEnumerator(IList<BaseType> baseList)
- {
- this.baseList = baseList;
- index = -1;
- }
- public RefinedType Current { get; private set; }
- public void Dispose()
- {
- if (OnDisposed != null)
- OnDisposed(this, EventArgs.Empty);
- }
- object IEnumerator.Current
- {
- get { return Current; }
- }
- public bool MoveNext()
- {
- if (invalidated)
- throw new InvalidOperationException("The collection was modified after the enumerator was created.");
- index++;
- if (index < baseList.Count)
- Current = (RefinedType)baseList[index];
- return (index < baseList.Count);
- }
- public void Reset()
- {
- if (invalidated)
- throw new InvalidOperationException("The collection was modified after the enumerator was created.");
- index = -1;
- }
- public void Invalidate()
- {
- invalidated = true;
- }
- }
- #endregion
- #region Fields
- private IList<BaseType> baseList;
- private List<RefinedEnumerator> refinedEnumerators = new List<RefinedEnumerator>();
- private object refinedEnumeratorSyncObj = new object();
- #endregion
- #region Construction
-
-
-
-
- public RefinedList(IList<BaseType> baseList)
- {
- this.baseList = baseList;
- }
- #endregion
- #region Properties
-
-
-
- public int Count
- {
- get { return baseList.Count; }
- }
-
-
-
- public bool IsReadOnly
- {
- get { return baseList.IsReadOnly; }
- }
-
-
-
-
-
- public RefinedType this[int index]
- {
- get { return (RefinedType)baseList[index]; }
- set { baseList[index] = value; }
- }
- #endregion
- #region List Methods
-
-
-
-
-
- public int IndexOf(RefinedType item)
- {
- return baseList.IndexOf(item);
- }
-
-
-
-
-
- public void Insert(int index, RefinedType item)
- {
- baseList.Insert(index, item);
- InvalidateEnumerators();
- }
-
-
-
-
- public void RemoveAt(int index)
- {
- baseList.RemoveAt(index);
- InvalidateEnumerators();
- }
-
-
-
-
- public void Add(RefinedType item)
- {
- baseList.Add(item);
- InvalidateEnumerators();
- }
-
-
-
- public void Clear()
- {
- if (baseList.Count > 0)
- InvalidateEnumerators();
- baseList.Clear();
- }
-
-
-
-
-
- public bool Contains(RefinedType item)
- {
- return baseList.Contains(item);
- }
-
-
-
-
-
- public void CopyTo(RefinedType[] array, int arrayIndex)
- {
- for (int i = 0; i < baseList.Count; i++)
- array[i + arrayIndex] = (RefinedType)baseList[i];
- }
-
-
-
-
-
- public bool Remove(RefinedType item)
- {
- if (baseList.Remove(item))
- {
- InvalidateEnumerators();
- return true;
- }
- return false;
- }
-
-
-
-
- public void AddRange(IEnumerable<RefinedType> collection)
- {
- foreach (RefinedType item in collection)
- baseList.Add(item);
- InvalidateEnumerators();
- }
- #endregion
- #region Enumerator Methods
-
-
-
-
- IEnumerator IEnumerable.GetEnumerator()
- {
- return GetEnumerator();
- }
-
-
-
-
- public IEnumerator<RefinedType> GetEnumerator()
- {
- lock (refinedEnumeratorSyncObj)
- {
- RefinedEnumerator refinedEnumerator = new RefinedEnumerator(baseList);
- refinedEnumerator.OnDisposed += refinedEnumerator_OnDisposed;
- refinedEnumerators.Add(refinedEnumerator);
- return refinedEnumerator;
- }
- }
- private void refinedEnumerator_OnDisposed(object sender, EventArgs e)
- {
- lock (refinedEnumeratorSyncObj)
- {
- refinedEnumerators.Remove((RefinedEnumerator)sender);
- }
- }
- private void InvalidateEnumerators()
- {
- lock (refinedEnumeratorSyncObj)
- {
- foreach (RefinedEnumerator refinedEnumerator in refinedEnumerators)
- refinedEnumerator.Invalidate();
- }
- }
- #endregion
- }
- }
|