Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- public class ListWithCounter<T> : IList<T>, IList
- {
- IEnumerable<T> Collection;
- private T[] _items;
- static readonly T[] _emptyArray = new T[0];
- private int _size;
- bool System.Collections.ICollection.IsSynchronized
- {
- get { return false; }
- }
- public ListWithCounter(int Capcity = 0)
- : base()
- {
- if (Capcity == 0)
- _items = _emptyArray;
- else
- _items = new T[Capcity];
- }
- public int Count
- {
- get
- {
- return _size;
- }
- }
- public int Capacity
- {
- get
- {
- return _items.Length;
- }
- set
- {
- if (value < _size)
- {
- //ThrowHelper.ThrowArgumentOutOfRangeException(ExceptionArgument.value, ExceptionResource.ArgumentOutOfRange_SmallCapacity);
- }
- if (value != _items.Length)
- {
- if (value > 0)
- {
- T[] newItems = new T[value];
- if (_size > 0)
- {
- Array.Copy(_items, 0, newItems, 0, _size);
- }
- _items = newItems;
- }
- else
- {
- _items = _emptyArray;
- }
- }
- }
- }
- Object System.Collections.IList.this[int index]
- {
- get
- {
- return this[index];
- }
- set
- {
- try
- {
- this[index] = (T)value;
- }
- catch (InvalidCastException)
- {
- }
- }
- }
- private const int _defaultCapacity = 10;
- internal const int MaxArrayLength = 0X7FEFFFFF;
- public void Add(T item)
- {
- if (_size == _items.Length && _items.Length < _size + 1)
- {
- int newCapacity = _items.Length == 0 ? _defaultCapacity : _items.Length * 4;
- // Allow the list to grow to maximum possible capacity (~2G elements) before encountering overflow.
- // Note that this check works even when _items.Length overflowed thanks to the (uint) cast
- if ((uint)newCapacity > MaxArrayLength) newCapacity = MaxArrayLength;
- if (newCapacity < _size + 1) newCapacity = _size + 1;
- Capacity = newCapacity;
- }
- _items[_size++] = item;
- }
- [NonSerialized]
- private Object _syncRoot;
- Object System.Collections.ICollection.SyncRoot
- {
- get
- {
- if (_syncRoot == null)
- {
- System.Threading.Interlocked.CompareExchange<Object>(ref _syncRoot, new Object(), null);
- }
- return _syncRoot;
- }
- }
- public ListWithCounter(IEnumerable<T> collection)
- {
- if (collection == null) return;
- ICollection<T> c = collection as ICollection<T>;
- if (c != null)
- {
- int count = c.Count;
- if (count == 0)
- {
- _items = _emptyArray;
- }
- else
- {
- _items = new T[count];
- c.CopyTo(_items, 0);
- _size = count;
- }
- }
- else
- {
- _size = 0;
- _items = _emptyArray;
- // This enumerable could be empty. Let Add allocate a new array, if needed.
- // Note it will also go to _defaultCapacity first, not 1, then 2, etc.
- using (IEnumerator<T> en = collection.GetEnumerator())
- {
- while (en.MoveNext())
- {
- Add(en.Current);
- }
- }
- }
- }
- public int CurId;
- //public virtual void ForEach(Action<T> itm, ListWithCounter<T> l =l.CurId)
- //{
- // for (int i = 0; i < _size; i++)
- // {
- // CurId = i;
- // itm(_items[i]); l(this.CurId++);
- // }
- //}
- public int IndexOf(T item)
- {
- throw new NotImplementedException();
- }
- public void Insert(int index, T item)
- {
- throw new NotImplementedException();
- }
- public void RemoveAt(int index)
- {
- throw new NotImplementedException();
- }
- public T this[int index]
- {
- get
- {
- return _items[index];
- }
- set
- {
- _items[index] = value;
- }
- }
- public void Clear()
- {
- throw new NotImplementedException();
- }
- public bool Contains(T item)
- {
- throw new NotImplementedException();
- }
- public void CopyTo(T[] array, int arrayIndex)
- {
- throw new NotImplementedException();
- }
- public bool IsReadOnly
- {
- get { throw new NotImplementedException(); }
- }
- public bool Remove(T item)
- {
- throw new NotImplementedException();
- }
- public Enumerator GetEnumerator()
- {
- return new Enumerator(this);
- }
- IEnumerator<T> IEnumerable<T>.GetEnumerator()
- {
- return new Enumerator(this);
- }
- System.Collections.IEnumerator System.Collections.IEnumerable.GetEnumerator()
- {
- return new Enumerator(this);
- }
- [Serializable]
- public struct Enumerator : IEnumerator<T>, System.Collections.IEnumerator
- {
- private ListWithCounter<T> list;
- private int index;
- private T current;
- internal Enumerator(ListWithCounter<T> list)
- {
- this.list = list;
- index = 0;
- current = default(T);
- }
- public void Dispose()
- {
- }
- public bool MoveNext()
- {
- ListWithCounter<T> localList = list;
- current = localList._items[index];
- index++;
- return true;
- //}
- return MoveNextRare();
- }
- private bool MoveNextRare()
- {
- index = list._size + 1;
- current = default(T);
- return false;
- }
- //public int Add(object value)
- //{
- // throw new NotImplementedException();
- //}
- public bool Contains(object value)
- {
- throw new NotImplementedException();
- }
- public int IndexOf(object value)
- {
- throw new NotImplementedException();
- }
- public void Insert(int index, object value)
- {
- throw new NotImplementedException();
- }
- public bool IsFixedSize
- {
- get { throw new NotImplementedException(); }
- }
- public void Remove(object value)
- {
- throw new NotImplementedException();
- }
- public void CopyTo(Array array, int index)
- {
- throw new NotImplementedException();
- }
- public bool IsSynchronized
- {
- get { throw new NotImplementedException(); }
- }
- public T Current
- {
- get
- {
- return current;
- }
- }
- object IEnumerator.Current
- {
- get
- {
- // if (index == 0 || index == list._size + 1)
- // {
- // //ThrowHelper.ThrowInvalidOperationException(ExceptionResource.InvalidOperation_EnumOpCantHappen);
- // }
- return Current;
- }
- }
- public void Reset()
- {
- index = 0;
- current = default(T);
- }
- }
- public int Add(object value)
- {
- throw new NotImplementedException();
- }
- public bool Contains(object value)
- {
- throw new NotImplementedException();
- }
- public int IndexOf(object value)
- {
- throw new NotImplementedException();
- }
- public void Insert(int index, object value)
- {
- throw new NotImplementedException();
- }
- public bool IsFixedSize
- {
- get { throw new NotImplementedException(); }
- }
- public void Remove(object value)
- {
- throw new NotImplementedException();
- }
- public void CopyTo(Array array, int index)
- {
- throw new NotImplementedException();
- }
- int IList<T>.IndexOf(T item)
- {
- throw new NotImplementedException();
- }
- void IList<T>.Insert(int index, T item)
- {
- throw new NotImplementedException();
- }
- void IList<T>.RemoveAt(int index)
- {
- throw new NotImplementedException();
- }
- T IList<T>.this[int index]
- {
- get
- {
- throw new NotImplementedException();
- }
- set
- {
- throw new NotImplementedException();
- }
- }
- void ICollection<T>.Add(T item)
- {
- throw new NotImplementedException();
- }
- void ICollection<T>.Clear()
- {
- throw new NotImplementedException();
- }
- bool ICollection<T>.Contains(T item)
- {
- throw new NotImplementedException();
- }
- void ICollection<T>.CopyTo(T[] array, int arrayIndex)
- {
- throw new NotImplementedException();
- }
- int ICollection<T>.Count
- {
- get { throw new NotImplementedException(); }
- }
- bool ICollection<T>.IsReadOnly
- {
- get { throw new NotImplementedException(); }
- }
- bool ICollection<T>.Remove(T item)
- {
- throw new NotImplementedException();
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement