/// <summary> /// Creates a new BitArray /// </summary> /// <param name="length">the length of the array</param> /// <param name="defaultValue">the set state for the bits</param> public ConcurrentBitArray(int length, bool defaultValue = false) { Contract.Requires(length >= 0, "length >= 0"); m_array = length == 0 ? CollectionUtilities.EmptyArray <int>() : new int[GetArrayLength(length)]; Length = length; if (defaultValue) { var last = m_array.Length - 1; for (int i = 0; i < m_array.Length; i++) { // Set all the bits // The last element only has the bits set for those which are actually used by the BitArray m_array[i] = unchecked ((int)(i != last ? AllSetInt32 : (AllSetInt32 >> (length % BitsPerInt32)))); } } }
/// <summary> /// Convert an array to a read only array by copying its contents. /// The provided array may safely change after this call. /// </summary> public static ReadOnlyArray <T> From(T[] array) { Contract.RequiresNotNull(array); T[] clone; if (array.Length == 0) { // normalize clone = CollectionUtilities.EmptyArray <T>(); } else { clone = new T[array.Length]; array.CopyTo(clone, 0); } return(new ReadOnlyArray <T>(clone)); }
/// <summary> /// Convert a hash set to a read only array. /// </summary> public static ReadOnlyArray <T> From(HashSet <T> set) { Contract.RequiresNotNull(set); if (set.Count == 0) { return(new ReadOnlyArray <T>(CollectionUtilities.EmptyArray <T>())); } var a = new T[set.Count]; int i = 0; foreach (var item in set) { a[i++] = item; } return(new ReadOnlyArray <T>(a)); }
/// <summary> /// Convert a range of an array to a read only array by copying its contents. /// The provided array may safely change after this call. /// </summary> public static ReadOnlyArray <T> From(T[] array, int start, int count) { Contract.RequiresNotNull(array); Contract.Requires(start >= 0); Contract.Requires(count >= 0); Contract.Requires((uint)start + (uint)count <= (uint)array.Length); T[] trimmed; if (count == 0) { // normalize trimmed = CollectionUtilities.EmptyArray <T>(); } else { trimmed = new T[count]; Array.Copy(array, start, trimmed, 0, count); } return(new ReadOnlyArray <T>(trimmed)); }
/// <summary> /// Class constructor /// </summary> protected SemaphoreSet() { SyncLock = new object(); SemaphoreLimits = new List <int>(); m_semaphoreUsages = CollectionUtilities.EmptyArray <int>(); }
/// <summary> /// Shallow copy constructor /// </summary> /// <param name="copy">the set whose structures will be shared with this instance</param> protected SemaphoreSet(SemaphoreSet copy) { SyncLock = copy.SyncLock; SemaphoreLimits = copy.SemaphoreLimits; m_semaphoreUsages = CollectionUtilities.EmptyArray <int>(); }