Advertisement
Guest User

FastCustomMiniNdexedForEachList<T> C#

a guest
Feb 21st, 2016
172
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 12.44 KB | None | 0 0
  1.         public class ListWithCounter<T> : IList<T>, IList
  2.         {
  3.             IEnumerable<T> Collection;
  4.             private T[] _items;
  5.             static readonly T[] _emptyArray = new T[0];
  6.             private int _size;
  7.  
  8.             bool System.Collections.ICollection.IsSynchronized
  9.             {
  10.                 get { return false; }
  11.             }
  12.            
  13.             public ListWithCounter(int Capcity = 0)
  14.                 : base()
  15.             {
  16.  
  17.                 if (Capcity == 0)
  18.                     _items = _emptyArray;
  19.                 else
  20.                     _items = new T[Capcity];
  21.  
  22.             }
  23.             public int Count
  24.             {
  25.                 get
  26.                 {
  27.  
  28.                     return _size;
  29.                 }
  30.             }
  31.             public int Capacity
  32.             {
  33.                 get
  34.                 {
  35.                     return _items.Length;
  36.                 }
  37.                 set
  38.                 {
  39.                     if (value < _size)
  40.                     {
  41.                         //ThrowHelper.ThrowArgumentOutOfRangeException(ExceptionArgument.value, ExceptionResource.ArgumentOutOfRange_SmallCapacity);
  42.                     }
  43.                     if (value != _items.Length)
  44.                     {
  45.                         if (value > 0)
  46.                         {
  47.                             T[] newItems = new T[value];
  48.                             if (_size > 0)
  49.                             {
  50.                                 Array.Copy(_items, 0, newItems, 0, _size);
  51.                             }
  52.                             _items = newItems;
  53.                         }
  54.                         else
  55.                         {
  56.                             _items = _emptyArray;
  57.                         }
  58.                     }
  59.                 }
  60.             }
  61.             Object System.Collections.IList.this[int index]
  62.             {
  63.                 get
  64.                 {
  65.                     return this[index];
  66.                 }
  67.                 set
  68.                 {
  69.  
  70.                     try
  71.                     {
  72.                         this[index] = (T)value;
  73.                     }
  74.                     catch (InvalidCastException)
  75.                     {
  76.                     }
  77.                 }
  78.             }
  79.             private const int _defaultCapacity = 10;
  80.             internal const int MaxArrayLength = 0X7FEFFFFF;
  81.             public void Add(T item)
  82.             {
  83.                 if (_size == _items.Length && _items.Length < _size + 1)
  84.                 {
  85.                     int newCapacity = _items.Length == 0 ? _defaultCapacity : _items.Length * 4;
  86.                     // Allow the list to grow to maximum possible capacity (~2G elements) before encountering overflow.
  87.                     // Note that this check works even when _items.Length overflowed thanks to the (uint) cast
  88.                     if ((uint)newCapacity > MaxArrayLength) newCapacity = MaxArrayLength;
  89.                     if (newCapacity < _size + 1) newCapacity = _size + 1;
  90.                     Capacity = newCapacity;
  91.                 }
  92.                 _items[_size++] = item;
  93.             }
  94.             [NonSerialized]
  95.             private Object _syncRoot;
  96.             Object System.Collections.ICollection.SyncRoot
  97.             {
  98.                 get
  99.                 {
  100.                     if (_syncRoot == null)
  101.                     {
  102.                         System.Threading.Interlocked.CompareExchange<Object>(ref _syncRoot, new Object(), null);
  103.                     }
  104.                     return _syncRoot;
  105.                 }
  106.             }
  107.             public ListWithCounter(IEnumerable<T> collection)
  108.             {
  109.                 if (collection == null) return;
  110.  
  111.  
  112.                 ICollection<T> c = collection as ICollection<T>;
  113.                 if (c != null)
  114.                 {
  115.                     int count = c.Count;
  116.                     if (count == 0)
  117.                     {
  118.                         _items = _emptyArray;
  119.                     }
  120.                     else
  121.                     {
  122.                         _items = new T[count];
  123.                         c.CopyTo(_items, 0);
  124.                         _size = count;
  125.                     }
  126.                 }
  127.                 else
  128.                 {
  129.                     _size = 0;
  130.                     _items = _emptyArray;
  131.                     // This enumerable could be empty.  Let Add allocate a new array, if needed.
  132.                     // Note it will also go to _defaultCapacity first, not 1, then 2, etc.
  133.  
  134.                     using (IEnumerator<T> en = collection.GetEnumerator())
  135.                     {
  136.                         while (en.MoveNext())
  137.                         {
  138.                             Add(en.Current);
  139.                         }
  140.                     }
  141.                 }
  142.             }
  143.             public int CurId;
  144.  
  145.  
  146.             //public virtual void ForEach(Action<T> itm, ListWithCounter<T> l =l.CurId)
  147.             //{
  148.  
  149.             //    for (int i = 0; i < _size; i++)
  150.             //    {
  151.             //        CurId = i;
  152.             //        itm(_items[i]); l(this.CurId++);
  153.  
  154.             //    }
  155.  
  156.             //}
  157.  
  158.             public int IndexOf(T item)
  159.             {
  160.                 throw new NotImplementedException();
  161.             }
  162.  
  163.             public void Insert(int index, T item)
  164.             {
  165.                 throw new NotImplementedException();
  166.             }
  167.  
  168.             public void RemoveAt(int index)
  169.             {
  170.                 throw new NotImplementedException();
  171.             }
  172.  
  173.             public T this[int index]
  174.             {
  175.                 get
  176.                 {
  177.                     return _items[index];
  178.                 }
  179.                 set
  180.                 {
  181.                     _items[index] = value;
  182.                 }
  183.             }
  184.  
  185.  
  186.             public void Clear()
  187.             {
  188.                 throw new NotImplementedException();
  189.             }
  190.  
  191.             public bool Contains(T item)
  192.             {
  193.                 throw new NotImplementedException();
  194.             }
  195.  
  196.             public void CopyTo(T[] array, int arrayIndex)
  197.             {
  198.                 throw new NotImplementedException();
  199.             }
  200.  
  201.  
  202.             public bool IsReadOnly
  203.             {
  204.                 get { throw new NotImplementedException(); }
  205.             }
  206.  
  207.             public bool Remove(T item)
  208.             {
  209.                 throw new NotImplementedException();
  210.             }
  211.  
  212.             public Enumerator GetEnumerator()
  213.             {
  214.                 return new Enumerator(this);
  215.             }
  216.             IEnumerator<T> IEnumerable<T>.GetEnumerator()
  217.             {
  218.                 return new Enumerator(this);
  219.             }
  220.             System.Collections.IEnumerator System.Collections.IEnumerable.GetEnumerator()
  221.             {
  222.                 return new Enumerator(this);
  223.             }
  224.             [Serializable]
  225.             public struct Enumerator : IEnumerator<T>, System.Collections.IEnumerator
  226.             {
  227.                 private ListWithCounter<T> list;
  228.                 private int index;
  229.                 private T current;
  230.  
  231.                 internal Enumerator(ListWithCounter<T> list)
  232.                 {
  233.                     this.list = list;
  234.                     index = 0;
  235.                     current = default(T);
  236.                 }
  237.  
  238.                 public void Dispose()
  239.                 {
  240.                 }
  241.  
  242.                 public bool MoveNext()
  243.                 {
  244.  
  245.                     ListWithCounter<T> localList = list;
  246.  
  247.                     current = localList._items[index];
  248.                     index++;
  249.                     return true;
  250.                     //}
  251.                     return MoveNextRare();
  252.                 }
  253.                 private bool MoveNextRare()
  254.                 {
  255.  
  256.                     index = list._size + 1;
  257.                     current = default(T);
  258.                     return false;
  259.                 }
  260.                 //public int Add(object value)
  261.                 //{
  262.                 //    throw new NotImplementedException();
  263.                 //}
  264.  
  265.                 public bool Contains(object value)
  266.                 {
  267.                     throw new NotImplementedException();
  268.                 }
  269.  
  270.                 public int IndexOf(object value)
  271.                 {
  272.                     throw new NotImplementedException();
  273.                 }
  274.  
  275.                 public void Insert(int index, object value)
  276.                 {
  277.                     throw new NotImplementedException();
  278.                 }
  279.  
  280.                 public bool IsFixedSize
  281.                 {
  282.                     get { throw new NotImplementedException(); }
  283.                 }
  284.  
  285.                 public void Remove(object value)
  286.                 {
  287.                     throw new NotImplementedException();
  288.                 }
  289.  
  290.                 public void CopyTo(Array array, int index)
  291.                 {
  292.                     throw new NotImplementedException();
  293.                 }
  294.  
  295.                 public bool IsSynchronized
  296.                 {
  297.                     get { throw new NotImplementedException(); }
  298.                 }
  299.  
  300.                 public T Current
  301.                 {
  302.                     get
  303.                     {
  304.                         return current;
  305.                     }
  306.                 }
  307.  
  308.                 object IEnumerator.Current
  309.                 {
  310.                     get
  311.                     {
  312.                     //    if (index == 0 || index == list._size + 1)
  313.                     //    {
  314.                     //        //ThrowHelper.ThrowInvalidOperationException(ExceptionResource.InvalidOperation_EnumOpCantHappen);
  315.                     //    }
  316.                         return Current;
  317.                     }
  318.                 }
  319.  
  320.                 public void Reset()
  321.                 {
  322.                     index = 0;
  323.                     current = default(T);
  324.                 }
  325.             }
  326.  
  327.             public int Add(object value)
  328.             {
  329.                 throw new NotImplementedException();
  330.             }
  331.  
  332.             public bool Contains(object value)
  333.             {
  334.                 throw new NotImplementedException();
  335.             }
  336.  
  337.             public int IndexOf(object value)
  338.             {
  339.                 throw new NotImplementedException();
  340.             }
  341.  
  342.             public void Insert(int index, object value)
  343.             {
  344.                 throw new NotImplementedException();
  345.             }
  346.  
  347.             public bool IsFixedSize
  348.             {
  349.                 get { throw new NotImplementedException(); }
  350.             }
  351.  
  352.             public void Remove(object value)
  353.             {
  354.                 throw new NotImplementedException();
  355.             }
  356.  
  357.             public void CopyTo(Array array, int index)
  358.             {
  359.                 throw new NotImplementedException();
  360.             }
  361.  
  362.             int IList<T>.IndexOf(T item)
  363.             {
  364.                 throw new NotImplementedException();
  365.             }
  366.  
  367.             void IList<T>.Insert(int index, T item)
  368.             {
  369.                 throw new NotImplementedException();
  370.             }
  371.  
  372.             void IList<T>.RemoveAt(int index)
  373.             {
  374.                 throw new NotImplementedException();
  375.             }
  376.  
  377.             T IList<T>.this[int index]
  378.             {
  379.                 get
  380.                 {
  381.                     throw new NotImplementedException();
  382.                 }
  383.                 set
  384.                 {
  385.                     throw new NotImplementedException();
  386.                 }
  387.             }
  388.  
  389.             void ICollection<T>.Add(T item)
  390.             {
  391.                 throw new NotImplementedException();
  392.             }
  393.  
  394.             void ICollection<T>.Clear()
  395.             {
  396.                 throw new NotImplementedException();
  397.             }
  398.  
  399.             bool ICollection<T>.Contains(T item)
  400.             {
  401.                 throw new NotImplementedException();
  402.             }
  403.  
  404.             void ICollection<T>.CopyTo(T[] array, int arrayIndex)
  405.             {
  406.                 throw new NotImplementedException();
  407.             }
  408.  
  409.             int ICollection<T>.Count
  410.             {
  411.                 get { throw new NotImplementedException(); }
  412.             }
  413.  
  414.             bool ICollection<T>.IsReadOnly
  415.             {
  416.                 get { throw new NotImplementedException(); }
  417.             }
  418.  
  419.             bool ICollection<T>.Remove(T item)
  420.             {
  421.                 throw new NotImplementedException();
  422.             }
  423.         }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement