M42.Collections by Daniel Pelsmaeker

<PackageReference Include="M42.Collections" Version="0.6.5056.5071" />

 Array<T>

An array.
using System; using System.Collections; using System.Collections.Generic; using System.Diagnostics; using System.Diagnostics.Contracts; using System.Runtime.Serialization; namespace M42.Collections { [DebuggerTypeProxy(typeof(EnumerableDebugVizualizer<>))] [DataContract] [DebuggerDisplay("Count = {Count}")] public sealed class Array<T> : IMutableArray<T>, IList<T>, ICollection<T>, IEnumerable<T>, IList, ICollection, IEnumerable, IFreezable { [DataMember] internal T[] innerArray; private bool safe; private static volatile Array<T> empty; private bool isFrozen; private readonly IEqualityComparer<T> comparer; public static Array<T> Empty { get { if (empty == null) { empty = new Array<T>(0); empty.Freeze(); } return empty; } } public int Count => innerArray.Length; public bool IsEmpty => Count == 0; public bool IsFrozen => isFrozen; public T this[int index] { get { if (__ContractsRuntime.insideContractEvaluation <= 1) try { __ContractsRuntime.insideContractEvaluation++; __ContractsRuntime.Requires<ArgumentOutOfRangeException>(index >= 0, (string)null, "index >= 0"); __ContractsRuntime.Requires<ArgumentOutOfRangeException>(index < ((ICollection)this).Count, (string)null, "index < Count"); } finally { __ContractsRuntime.insideContractEvaluation--; } return innerArray[index]; } set { if (__ContractsRuntime.insideContractEvaluation <= 4) try { __ContractsRuntime.insideContractEvaluation++; __ContractsRuntime.Requires<CollectionFrozenException>(!((ICollection)this).IsFrozen, (string)null, "!IsFrozen"); __ContractsRuntime.Requires<ArgumentOutOfRangeException>(index >= 0, (string)null, "index >= 0"); __ContractsRuntime.Requires<ArgumentOutOfRangeException>(index < ((ICollection)this).Count, (string)null, "index < Count"); __ContractsRuntime.Requires<ArgumentException>(((ICollection)this).IsValidMember((object)value), (string)null, "IsValidMember(value)"); } finally { __ContractsRuntime.insideContractEvaluation--; } innerArray[index] = value; } } T IList<T>.this[int index] { get { if (__ContractsRuntime.insideContractEvaluation <= 1) try { __ContractsRuntime.insideContractEvaluation++; __ContractsRuntime.Requires<ArgumentOutOfRangeException>(index >= 0, (string)null, "index >= 0"); __ContractsRuntime.Requires<ArgumentOutOfRangeException>(index < ((ICollection)this).Count, (string)null, "index < Count"); } finally { __ContractsRuntime.insideContractEvaluation--; } return innerArray[index]; } } object IList.this[int index] { get { if (__ContractsRuntime.insideContractEvaluation <= 1) try { __ContractsRuntime.insideContractEvaluation++; __ContractsRuntime.Requires<ArgumentOutOfRangeException>(index >= 0, (string)null, "index >= 0"); __ContractsRuntime.Requires<ArgumentOutOfRangeException>(index < ((ICollection)this).Count, (string)null, "index < Count"); } finally { __ContractsRuntime.insideContractEvaluation--; } return this[index]; } } public IEqualityComparer<T> Comparer => comparer; [Pure] private bool AreEqual(object expected, T value) { if (expected == null || expected is T) return Comparer.Equals((T)expected, value); return false; } public Array(int length) { __ContractsRuntime.Requires<ArgumentOutOfRangeException>(length >= 0, (string)null, "length >= 0"); this..ctor(length, (IEqualityComparer<T>)EqualityComparer<T>.Default); } public Array(int length, IEqualityComparer<T> comparer) { __ContractsRuntime.Requires<ArgumentOutOfRangeException>(length >= 0, (string)null, "length >= 0"); __ContractsRuntime.Requires<ArgumentNullException>(comparer != null, (string)null, "comparer != null"); this..ctor(checked((uint)length), comparer); } public Array(uint length) : this(length, (IEqualityComparer<T>)EqualityComparer<T>.Default) { } public Array(uint length, IEqualityComparer<T> comparer) { __ContractsRuntime.Requires<ArgumentNullException>(comparer != null, (string)null, "comparer != null"); base..ctor(); innerArray = new T[length]; this.comparer = comparer; safe = true; } public Array(params T[] array) { __ContractsRuntime.Requires<ArgumentNullException>(array != null, (string)null, "array != null"); base..ctor(); innerArray = array; comparer = EqualityComparer<T>.Default; safe = false; } public static explicit operator T[](Array<T> array) { if (__ContractsRuntime.insideContractEvaluation <= 4) try { __ContractsRuntime.insideContractEvaluation++; __ContractsRuntime.Requires<ArgumentException>(array == null || !array.IsFrozen, (string)null, "array == null || !array.IsFrozen"); } finally { __ContractsRuntime.insideContractEvaluation--; } if (array != null) { array.safe = false; return array.innerArray; } return null; } public static implicit operator Array<T>(T[] array) { __ContractsRuntime.Requires<ArgumentNullException>(array != null, (string)null, "array != null"); if (array != null) return new Array<T>(array); return null; } public void SetAll(T value) { if (__ContractsRuntime.insideContractEvaluation <= 4) try { __ContractsRuntime.insideContractEvaluation++; __ContractsRuntime.Requires<ArgumentException>(IsValidMember(value), (string)null, "IsValidMember(value)"); } finally { __ContractsRuntime.insideContractEvaluation--; } SetRange(0, Count, value); } public void SetAll(Func<int, T> valueProvider) { __ContractsRuntime.Requires<ArgumentNullException>(valueProvider != null, (string)null, "valueProvider != null"); SetRange(0, Count, valueProvider); } public void SetRange(int start, int count, T value) { if (__ContractsRuntime.insideContractEvaluation <= 4) try { __ContractsRuntime.insideContractEvaluation++; __ContractsRuntime.Requires<ArgumentOutOfRangeException>(start >= 0, (string)null, "start >= 0"); __ContractsRuntime.Requires<ArgumentOutOfRangeException>(start <= Count, (string)null, "start <= Count"); __ContractsRuntime.Requires<ArgumentOutOfRangeException>(count >= 0, (string)null, "count >= 0"); __ContractsRuntime.Requires<ArgumentOutOfRangeException>(start + count <= Count, (string)null, "start + count <= Count"); __ContractsRuntime.Requires<ArgumentException>(IsValidMember(value), (string)null, "IsValidMember(value)"); } finally { __ContractsRuntime.insideContractEvaluation--; } int num = start + count; for (int i = start; i < num; i++) { this[i] = value; } } public void SetRange(int start, int count, Func<int, T> valueProvider) { if (__ContractsRuntime.insideContractEvaluation <= 4) try { __ContractsRuntime.insideContractEvaluation++; __ContractsRuntime.Requires<ArgumentOutOfRangeException>(start >= 0, (string)null, "start >= 0"); __ContractsRuntime.Requires<ArgumentOutOfRangeException>(start <= Count, (string)null, "start <= Count"); __ContractsRuntime.Requires<ArgumentOutOfRangeException>(count >= 0, (string)null, "count >= 0"); __ContractsRuntime.Requires<ArgumentOutOfRangeException>(start + count <= Count, (string)null, "start + count <= Count"); __ContractsRuntime.Requires<ArgumentNullException>(valueProvider != null, (string)null, "valueProvider != null"); } finally { __ContractsRuntime.insideContractEvaluation--; } int num = start + count; for (int i = start; i < num; i++) { this[i] = valueProvider(i); } } public Array<T> Clone() { T[] array = (T[])innerArray.Clone(); Array<T> array2 = new Array<T>(array); array2.MakeSafe(); return array2; } public void ResetAll() { Array.Clear(innerArray, 0, innerArray.Length); } [Pure] public bool TryGetMember(object value, out T result) { result = ((ICollection<T>)this).GetMember(value, out bool isPresent); return isPresent; } T ICollection<T>.GetMember(object value, out bool isPresent) { int? nullable = ((IList)this).IndexOf(value); isPresent = nullable.HasValue; T result = default(T); if (!isPresent) return result; return this[nullable.Value]; } T ICollection<T>.GetMember(object value) { bool isPresent; return ((ICollection<T>)this).GetMember(value, out isPresent); } object ICollection.GetMember(object value, out bool isPresent) { return ((ICollection<T>)this).GetMember(value, out isPresent); } object ICollection.GetMember(object value) { return ((ICollection<T>)this).GetMember(value); } [Pure] public bool Contains(T value) { return ((ICollection)this).Contains((object)value); } bool ICollection.Contains(object value) { return ((IList)this).IndexOf(value).HasValue; } [Pure] public int? IndexOf(T value) { return ((IList)this).IndexOf((object)value); } int? IList.IndexOf(object value) { for (int i = 0; i < innerArray.Length; i++) { if (AreEqual(value, innerArray[i])) return i; } return null; } public bool IsValidMember(object value) { if (value != null) return value is T; return true; } public IEnumerator<T> GetEnumerator() { return ((IEnumerable<T>)innerArray).GetEnumerator(); } IEnumerator IEnumerable.GetEnumerator() { return GetEnumerator(); } public void Freeze() { if (__ContractsRuntime.insideContractEvaluation <= 4) try { __ContractsRuntime.insideContractEvaluation++; __ContractsRuntime.Requires<InvalidOperationException>(!((IFreezable)this).IsFrozen, (string)null, "!IsFrozen"); } finally { __ContractsRuntime.insideContractEvaluation--; } isFrozen = true; innerArray = GetFrozenInnerArray(); MakeSafe(); } private T[] GetFrozenInnerArray() { if (safe) return innerArray; return (T[])innerArray.Clone(); } internal void MakeSafe() { safe = true; } } }