//-------------------------------------------// /// <summary> /// Initialize a new Belt. Optional loop signal causes the 'Loop' flag to be updated /// each loop cycle of the belt. /// </summary> public Belt() { Empty = true; Loop = true; Count = 0; _remove = new ArrayRig <T>(); }
/// <summary> /// Create a new array rig by cloning another. /// </summary> /// <param name="arrayRig">Array rig to clone.</param> public unsafe ArrayRig(ArrayRig <T> arrayRig) { Count = arrayRig.Count; Capacity = arrayRig.Array.Length; Array = new T[Capacity]; System.Array.Copy(arrayRig.Array, Array = new T[Capacity], Count); }
/// <summary> /// Initialize a new Belt. Optional loop signal will flag each loop completion and return false on loops. /// </summary> public Belt(bool loopSignal) { _loopSignal = loopSignal; Empty = true; Loop = true; Count = 0; _remove = new ArrayRig <T>(); }
//-------------------------------------------// public Capsule(IEqualityComparer <T> comparer) { _rig = new ArrayRig <T>(); _collection = new HashSet <T>(comparer); _toAdd = new HashSet <T>(comparer); _toRemove = new HashSet <T>(comparer); _collectionLock = new Lock(); _toRemoveLock = new Lock(); _toAddLock = new Lock(); }
public Capsule() { _rig = new ArrayRig <T>(); _collection = new HashSet <T>(); _toAdd = new HashSet <T>(); _toRemove = new HashSet <T>(); _collectionLock = new Lock(); _toRemoveLock = new Lock(); _toAddLock = new Lock(); }
/// <summary> /// Add a collection to the end of the queue. /// </summary> public void Enqueue(ArrayRig <T> collection) { Count += collection.Count; // should the rig be resized? if (_rig.Capacity < Count * 2) { _rig.SetCapacity(Count); } // iterate and add the items in the collection. foreach (T item in collection) { // has the loopback been set? if (_loopbackStarted) { // yes, has the loopback been filled? if (_loopbackFilled) { // yes, add the item to the end of the collection _rig.Add(item); } else { // no, set the item at the loopback index and increment _rig[_loopbackEnd] = item; ++_loopbackEnd; _loopbackFilled = _loopbackEnd == _loopbackStart; } } else { // is the queue at a starting position? if (_index == 0) { // yes, just add the item _rig.Add(item); // not empty Empty = false; } else { // no, start the loopback collection _loopbackStarted = true; _loopbackStart = _rig.Count; _rig[_loopbackEnd] = item; ++_loopbackEnd; _loopbackFilled = _loopbackEnd == _loopbackStart; } } } }
/// <summary> /// Add the specified items from the start index to the end index to the end of this rig. /// </summary> public void Add(ArrayRig <T> items, int start, int end) { if (Capacity < end - start + Count + 1) { Capacity = Count + end - start; System.Array.Resize <T>(ref Array, Capacity); } for (int i = start; i < end; ++i) { Array[Count] = items[i]; ++Count; } }
/// <summary> /// Add the specified items to the end of the array. /// </summary> public void AddItems(ArrayRig <T> items) { if (Capacity < items.Count + Count) { Capacity = Count + items.Count; System.Array.Resize <T>(ref Array, Capacity); } for (int i = 0; i < items.Count; ++i) { Array[Count] = items[i]; ++Count; } }
/// <summary> /// Map the current set of items to the new rig of the defined type. /// Optionally a predicate determines whether the item is mapped. /// </summary> public ArrayRig <V> Map <V>(Func <T, V> mapper, Func <T, bool> predicate = null) { ArrayRig <V> mapped = new ArrayRig <V>(); if (predicate == null) { foreach (T item in this) { mapped.Add(mapper(item)); } } else { foreach (T item in this) { if (predicate(item)) { mapped.Add(mapper(item)); } } } return(mapped); }
//-------------------------------------------// public Enumerator(ArrayRig <T> rig) { _rig = rig; Index = 0; }
//-------------------------------------------// public Enumerator(Capsule <T> _capsule) { capsule = _capsule; rig = _capsule._rig; index = -1; }
//-------------------------------------------// /// <summary> /// Construct a new empty array queue. /// </summary> public ArrayQueue() { _rig = new ArrayRig <T>(); Empty = true; }
/// <summary> /// Start with the content of an array. /// </summary> public ArrayQueue(T[] collection) { _rig = new ArrayRig <T>(collection); Empty = collection.Length == 0; }
/// <summary> /// Start with the content of an array rig. /// </summary> public ArrayQueue(ArrayRig <T> rig) { _rig = rig; Empty = _rig.Count == 0; }
/// <summary> /// Dispose of the array queue and the underlying array rig. /// </summary> public void Dispose() { _rig.Dispose(); _rig = null; Count = -1; }
public void Dispose() { _rig = null; }
//-------------------------------------------// //-------------------------------------------// public Layer(int _depth = 0) { depth = _depth; elements = new ArrayRig <T>(); }
/// <summary> /// Enqueue a collection. /// </summary> public void Enqueue(ArrayRig <T> collection) { _locks.B.Take(); _queues.B.Enqueue(collection); _locks.B.Release(); }
/// <summary> /// Gets indexes of the specified key. Returns an empty collection if /// the key isn't found. /// </summary> public ArrayRig <int> AllIndexesOf(IComparable <TKey> key) { // if no items - skip if (Count == 0) { return(new ArrayRig <int>(1)); } int max = Count; int min = 0; int next = min + (max - min >> 1); int median = -1; int compare; ArrayRig <int> collection = new ArrayRig <int>(); // while there are keys between the max and min while (median != next) { median = next; // compare the key at index with the target compare = key.CompareTo(Array[median].ArgA); // if the keys are equal if (compare == 0) { collection.Add(median); for (int i = median - 1; i >= 0; --i) { if (key.CompareTo(Array[i].ArgA) == 0) { collection.Add(i); } else { break; } } for (int i = median + 1; i < Count; ++i) { if (key.CompareTo(Array[i].ArgA) == 0) { collection.Add(i); } else { break; } } return(collection); } // is the key smaller than the current index? yes, move the max down if (compare < 0) { max = median + 1; } // no, move the min up else { min = median - 1; } // set the median of the max and min next = min + (max - min >> 1); } if (median > 0) { var average = _getAverage(Array[median].ArgA, Array[median - 1].ArgA); compare = key.CompareTo(average); if (compare < 0) { --median; } } if (key.CompareTo(Array[median].ArgA) != 0) { collection.Add(median); for (int i = median - 1; i >= 0; --i) { if (key.CompareTo(Array[i].ArgA) == 0) { collection.Add(i); } } for (int i = median + 1; i < Count; ++i) { if (key.CompareTo(Array[i].ArgA) == 0) { collection.Add(i); } } } // if the last median isn't the key, the key doesn't exist return(collection); }