mscorlib(4.0.0.0) API with additions
ReadOnlyCollectionBuilder.cs
1 using System.Collections;
4 using System.Dynamic.Utils;
6 using System.Threading;
7 
9 {
12  [Serializable]
14  {
15  [Serializable]
16  private class Enumerator : IEnumerator<T>, IDisposable, IEnumerator
17  {
18  private readonly ReadOnlyCollectionBuilder<T> _builder;
19 
20  private readonly int _version;
21 
22  private int _index;
23 
24  private T _current;
25 
26  public T Current => _current;
27 
28  object IEnumerator.Current
29  {
30  get
31  {
32  if (_index == 0 || _index > _builder._size)
33  {
34  throw Error.EnumerationIsDone();
35  }
36  return _current;
37  }
38  }
39 
40  internal Enumerator(ReadOnlyCollectionBuilder<T> builder)
41  {
42  _builder = builder;
43  _version = builder._version;
44  _index = 0;
45  _current = default(T);
46  }
47 
48  public void Dispose()
49  {
50  GC.SuppressFinalize(this);
51  }
52 
53  public bool MoveNext()
54  {
55  if (_version == _builder._version)
56  {
57  if (_index < _builder._size)
58  {
59  _current = _builder._items[_index++];
60  return true;
61  }
62  _index = _builder._size + 1;
63  _current = default(T);
64  return false;
65  }
66  throw Error.CollectionModifiedWhileEnumerating();
67  }
68 
69  void IEnumerator.Reset()
70  {
71  if (_version != _builder._version)
72  {
73  throw Error.CollectionModifiedWhileEnumerating();
74  }
75  _index = 0;
76  _current = default(T);
77  }
78  }
79 
80  private const int DefaultCapacity = 4;
81 
82  private T[] _items;
83 
84  private int _size;
85 
86  private int _version;
87 
88  [NonSerialized]
89  private object _syncRoot;
90 
91  private static readonly T[] _emptyArray = new T[0];
92 
95  public int Capacity
96  {
97  get
98  {
99  return _items.Length;
100  }
101  set
102  {
103  ContractUtils.Requires(value >= _size, "value");
104  if (value == _items.Length)
105  {
106  return;
107  }
108  if (value > 0)
109  {
110  T[] array = new T[value];
111  if (_size > 0)
112  {
113  Array.Copy(_items, 0, array, 0, _size);
114  }
115  _items = array;
116  }
117  else
118  {
119  _items = _emptyArray;
120  }
121  }
122  }
123 
126  public int Count => _size;
127 
131  public T this[int index]
132  {
133  get
134  {
135  ContractUtils.Requires(index < _size, "index");
136  return _items[index];
137  }
138  set
139  {
140  ContractUtils.Requires(index < _size, "index");
141  _items[index] = value;
142  _version++;
143  }
144  }
145 
149  bool ICollection<T>.IsReadOnly
150  {
151  get
152  {
153  return false;
154  }
155  }
156 
160  bool IList.IsReadOnly
161  {
162  get
163  {
164  return false;
165  }
166  }
167 
171  bool IList.IsFixedSize
172  {
173  get
174  {
175  return false;
176  }
177  }
178 
182  object IList.this[int index]
183  {
184  get
185  {
186  return this[index];
187  }
188  set
189  {
190  ValidateNullValue(value, "value");
191  try
192  {
193  this[index] = (T)value;
194  }
195  catch (InvalidCastException)
196  {
197  ThrowInvalidTypeException(value, "value");
198  }
199  }
200  }
201 
206  {
207  get
208  {
209  return false;
210  }
211  }
212 
215  object ICollection.SyncRoot
216  {
217  get
218  {
219  if (_syncRoot == null)
220  {
221  Interlocked.CompareExchange<object>(ref _syncRoot, new object(), (object)null);
222  }
223  return _syncRoot;
224  }
225  }
226 
229  {
230  _items = _emptyArray;
231  }
232 
235  public ReadOnlyCollectionBuilder(int capacity)
236  {
237  ContractUtils.Requires(capacity >= 0, "capacity");
238  _items = new T[capacity];
239  }
240 
244  {
245  ContractUtils.Requires(collection != null, "collection");
246  ICollection<T> collection2 = collection as ICollection<T>;
247  if (collection2 != null)
248  {
249  int count = collection2.Count;
250  _items = new T[count];
251  collection2.CopyTo(_items, 0);
252  _size = count;
253  }
254  else
255  {
256  _size = 0;
257  _items = new T[4];
258  foreach (T item in collection)
259  {
260  Add(item);
261  }
262  }
263  }
264 
268  public int IndexOf(T item)
269  {
270  return Array.IndexOf(_items, item, 0, _size);
271  }
272 
276  public void Insert(int index, T item)
277  {
278  ContractUtils.Requires(index <= _size, "index");
279  if (_size == _items.Length)
280  {
281  EnsureCapacity(_size + 1);
282  }
283  if (index < _size)
284  {
285  Array.Copy(_items, index, _items, index + 1, _size - index);
286  }
287  _items[index] = item;
288  _size++;
289  _version++;
290  }
291 
294  public void RemoveAt(int index)
295  {
296  ContractUtils.Requires(index >= 0 && index < _size, "index");
297  _size--;
298  if (index < _size)
299  {
300  Array.Copy(_items, index + 1, _items, index, _size - index);
301  }
302  _items[_size] = default(T);
303  _version++;
304  }
305 
308  public void Add(T item)
309  {
310  if (_size == _items.Length)
311  {
312  EnsureCapacity(_size + 1);
313  }
314  _items[_size++] = item;
315  _version++;
316  }
317 
319  public void Clear()
320  {
321  if (_size > 0)
322  {
323  Array.Clear(_items, 0, _size);
324  _size = 0;
325  }
326  _version++;
327  }
328 
332  public bool Contains(T item)
333  {
334  if (item == null)
335  {
336  for (int i = 0; i < _size; i++)
337  {
338  if (_items[i] == null)
339  {
340  return true;
341  }
342  }
343  return false;
344  }
346  for (int j = 0; j < _size; j++)
347  {
348  if (@default.Equals(_items[j], item))
349  {
350  return true;
351  }
352  }
353  return false;
354  }
355 
359  public void CopyTo(T[] array, int arrayIndex)
360  {
361  Array.Copy(_items, 0, array, arrayIndex, _size);
362  }
363 
367  public bool Remove(T item)
368  {
369  int num = IndexOf(item);
370  if (num >= 0)
371  {
372  RemoveAt(num);
373  return true;
374  }
375  return false;
376  }
377 
381  {
382  return new Enumerator(this);
383  }
384 
388  {
389  return GetEnumerator();
390  }
391 
395  int IList.Add(object value)
396  {
397  ValidateNullValue(value, "value");
398  try
399  {
400  Add((T)value);
401  }
402  catch (InvalidCastException)
403  {
404  ThrowInvalidTypeException(value, "value");
405  }
406  return Count - 1;
407  }
408 
413  bool IList.Contains(object value)
414  {
415  if (IsCompatibleObject(value))
416  {
417  return Contains((T)value);
418  }
419  return false;
420  }
421 
425  int IList.IndexOf(object value)
426  {
427  if (IsCompatibleObject(value))
428  {
429  return IndexOf((T)value);
430  }
431  return -1;
432  }
433 
437  void IList.Insert(int index, object value)
438  {
439  ValidateNullValue(value, "value");
440  try
441  {
442  Insert(index, (T)value);
443  }
444  catch (InvalidCastException)
445  {
446  ThrowInvalidTypeException(value, "value");
447  }
448  }
449 
452  void IList.Remove(object value)
453  {
454  if (IsCompatibleObject(value))
455  {
456  Remove((T)value);
457  }
458  }
459 
463  void ICollection.CopyTo(Array array, int index)
464  {
465  ContractUtils.RequiresNotNull(array, "array");
466  ContractUtils.Requires(array.Rank == 1, "array");
467  Array.Copy(_items, 0, array, index, _size);
468  }
469 
471  public void Reverse()
472  {
473  Reverse(0, Count);
474  }
475 
479  public void Reverse(int index, int count)
480  {
481  ContractUtils.Requires(index >= 0, "index");
482  ContractUtils.Requires(count >= 0, "count");
483  Array.Reverse(_items, index, count);
484  _version++;
485  }
486 
489  public T[] ToArray()
490  {
491  T[] array = new T[_size];
492  Array.Copy(_items, 0, array, 0, _size);
493  return array;
494  }
495 
499  {
500  T[] list = (_size != _items.Length) ? ToArray() : _items;
501  _items = _emptyArray;
502  _size = 0;
503  _version++;
504  return new TrueReadOnlyCollection<T>(list);
505  }
506 
507  private void EnsureCapacity(int min)
508  {
509  if (_items.Length < min)
510  {
511  int num = 4;
512  if (_items.Length != 0)
513  {
514  num = _items.Length * 2;
515  }
516  if (num < min)
517  {
518  num = min;
519  }
520  Capacity = num;
521  }
522  }
523 
524  private static bool IsCompatibleObject(object value)
525  {
526  if (!(value is T))
527  {
528  if (value == null)
529  {
530  return default(T) == null;
531  }
532  return false;
533  }
534  return true;
535  }
536 
537  private static void ValidateNullValue(object value, string argument)
538  {
539  if (value == null && default(T) != null)
540  {
541  throw new ArgumentException(Strings.InvalidNullValue(typeof(T)), argument);
542  }
543  }
544 
545  private static void ThrowInvalidTypeException(object value, string argument)
546  {
547  throw new ArgumentException(Strings.InvalidObjectType((value != null) ? ((object)value.GetType()) : ((object)"null"), typeof(T)), argument);
548  }
549  }
550 }
void Clear()
Removes all items from the T:System.Runtime.CompilerServices.ReadOnlyCollectionBuilder`1.
object SyncRoot
Gets an object that can be used to synchronize access to the T:System.Collections....
Definition: ICollection.cs:23
Provides a base class for implementations of the T:System.Collections.Generic.IEqualityComparer`1 gen...
int Count
Returns number of elements in the ReadOnlyCollectionBuilder.
void Reset()
Sets the enumerator to its initial position, which is before the first element in the collection.
int Capacity
Gets and sets the capacity of this ReadOnlyCollectionBuilder.
Provides the base class for a generic read-only collection.
static void SuppressFinalize(object obj)
Requests that the common language runtime not call the finalizer for the specified object.
Definition: GC.cs:308
void RemoveAt(int index)
Removes the T:System.Runtime.CompilerServices.ReadOnlyCollectionBuilder`1 item at the specified index...
static void Clear(Array array, int index, int length)
Sets a range of elements in an array to the default value of each element type.
bool Contains(T item)
Determines whether the T:System.Runtime.CompilerServices.ReadOnlyCollectionBuilder`1 contains a speci...
Represents a non-generic collection of objects that can be individually accessed by index.
Definition: IList.cs:8
Provides a mechanism for releasing unmanaged resources.To browse the .NET Framework source code for t...
Definition: IDisposable.cs:8
IEnumerator< T > GetEnumerator()
Returns an enumerator that iterates through the collection.
void Insert(int index, object value)
Inserts an item to the T:System.Collections.IList at the specified index.
static void Reverse(Array array)
Reverses the sequence of the elements in the entire one-dimensional T:System.Array.
Definition: Array.cs:3171
Definition: __Canon.cs:3
The exception that is thrown for invalid casting or explicit conversion.
void Reverse()
Reverses the order of the elements in the entire T:System.Runtime.CompilerServices....
bool IsReadOnly
Gets a value indicating whether the T:System.Collections.IList is read-only.
Definition: IList.cs:30
void CopyTo(T[] array, int arrayIndex)
Copies the elements of the T:System.Runtime.CompilerServices.ReadOnlyCollectionBuilder`1 to an T:Syst...
int IndexOf(object value)
Determines the index of a specific item in the T:System.Collections.IList.
void Insert(int index, T item)
Inserts an item to the T:System.Runtime.CompilerServices.ReadOnlyCollectionBuilder`1 at the specified...
static EqualityComparer< T > Default
Returns a default equality comparer for the type specified by the generic argument.
void Reverse(int index, int count)
Reverses the order of the elements in the specified range.
bool IsFixedSize
Gets a value indicating whether the T:System.Collections.IList has a fixed size.
Definition: IList.cs:40
ReadOnlyCollectionBuilder(int capacity)
Constructs a ReadOnlyCollectionBuilder with a given initial capacity. The contents are empty but buil...
Exposes an enumerator, which supports a simple iteration over a non-generic collection....
Definition: IEnumerable.cs:9
static int CompareExchange(ref int location1, int value, int comparand)
Compares two 32-bit signed integers for equality and, if they are equal, replaces the first value.
object Current
Gets the element in the collection at the current position of the enumerator.
Definition: IEnumerator.cs:15
Provides methods for creating, manipulating, searching, and sorting arrays, thereby serving as the ba...
Definition: Array.cs:17
bool IsSynchronized
Gets a value indicating whether access to the T:System.Collections.ICollection is synchronized (threa...
Definition: ICollection.cs:33
ReadOnlyCollectionBuilder()
Constructs a ReadOnlyCollectionBuilder.
int Add(object value)
Adds an item to the T:System.Collections.IList.
bool Remove(T item)
Removes the first occurrence of a specific object from the T:System.Runtime.CompilerServices....
IEnumerator GetEnumerator()
Returns an enumerator that iterates through a collection.
void Add(T item)
Adds an item to the T:System.Runtime.CompilerServices.ReadOnlyCollectionBuilder`1.
Controls the system garbage collector, a service that automatically reclaims unused memory.
Definition: GC.cs:11
ReadOnlyCollection< T > ToReadOnlyCollection()
Creates a T:System.Collections.ObjectModel.ReadOnlyCollection`1 containing all of the elements of the...
static void Copy(Array sourceArray, Array destinationArray, int length)
Copies a range of elements from an T:System.Array starting at the first element and pastes them into ...
Definition: Array.cs:1275
T [] ToArray()
Copies the elements of the T:System.Runtime.CompilerServices.ReadOnlyCollectionBuilder`1 to a new arr...
Specifies that the class can be serialized.
void Remove(object value)
Removes the first occurrence of a specific object from the T:System.Collections.IList.
bool Contains(object value)
Determines whether the T:System.Collections.IList contains a specific value.
int Count
Gets the number of elements contained in the T:System.Collections.ICollection.
Definition: ICollection.cs:14
Defines size, enumerators, and synchronization methods for all nongeneric collections.
Definition: ICollection.cs:8
void CopyTo(Array array, int index)
Copies the elements of the T:System.Collections.ICollection to an T:System.Array, starting at a parti...
ReadOnlyCollectionBuilder(IEnumerable< T > collection)
Constructs a ReadOnlyCollectionBuilder, copying contents of the given collection.
Provides atomic operations for variables that are shared by multiple threads.
Definition: Interlocked.cs:10
Supports a simple iteration over a non-generic collection.
Definition: IEnumerator.cs:9
int IndexOf(T item)
Returns the index of the first occurrence of a given value in the builder.