diff --git a/src/client/spark_core/Collections/CircularBuffer.cs b/src/client/spark_core/Collections/CircularBuffer.cs new file mode 100644 index 000000000..cec8ddc86 --- /dev/null +++ b/src/client/spark_core/Collections/CircularBuffer.cs @@ -0,0 +1,316 @@ +#nullable enable + +namespace Spark.Core.Collections; + +using bts; +using bts.collections; +using bts.utils; + +using mbts.utils; + +using System; +using System.Collections; +using System.Collections.Generic; +using System.Diagnostics.CodeAnalysis; +using System.Linq; + +public class CircularBuffer : IList, IReadOnlyList { + int capacity; + T[] items; + int origin; + int count; + + public CircularBuffer(int capacity) : this(capacity, capacity) { } + + public CircularBuffer(int capacity, int initialCapacity) { + this.capacity = capacity; + ExceptionAssert.Argument.GreaterThanEqualTo(capacity, 0); + items = new T[initialCapacity.within_range(0, capacity)]; + } + + public T this[int index] { + get => items[getOffsetIndex(index)]; + set => items[getOffsetIndex(index)] = value; + } + + public int Capacity { + get => capacity; + set { + if (value < 0) { + throw new ArgumentException("Must be non-negative", nameof(value)); + } + if (value == capacity) { + return; + } + if (value < capacity) { + // Shrink buffer + var newItems = new T[value]; + int frontLength = count - origin; + if (newItems.Length <= frontLength) { + Array.Copy(items, origin, newItems, 0, newItems.Length); + } else { + Array.Copy(items, origin, newItems, 0, frontLength); + if (origin > 0) { + Array.Copy(items, 0, newItems, frontLength, newItems.Length - frontLength); + } + } + items = newItems; + origin = 0; + } else if (origin != 0) { + // Expand buffer and reset origin to 0 + int frontLength = count - origin; + var newItems = new T[value]; + Array.Copy(items, origin, newItems, 0, frontLength); + Array.Copy(items, 0, newItems, frontLength, origin); + } + capacity = value; + } + } + + public int Count => count; + + public bool IsReadOnly => false; + + public bool Push(T item, [MaybeNullWhen(false)] out T popped) { + if (capacity == 0) { + popped = default; + return false; + } + if (items.Length == capacity) { + popped = this[0]; + this[count - 1] = item; + return true; + } + Add(item); + popped = default; + return false; + } + + public void Add(T item) { + if (capacity == 0) { + return; + } + if (count == capacity) { + items[origin++] = item; + if (origin == items.Length) { + origin = 0; + } + return; + } + var idx = origin + count; + ensureCapacity(count + 1); + if (idx >= items.Length) { + idx -= count; + } + items[idx] = item; + count++; + } + + public void AddRange(IList itemList) { + int count = itemList.Count; + int nFree = capacity - this.count; + if (nFree > 0) { + Log.assert(origin == 0); + if (nFree > count) { + for (int i = 0; i < nFree; i++) { + items[i + this.count] = itemList[i]; + } + for (int i = 0; i < count - nFree; i++) { + items[i] = itemList[i + nFree]; + } + origin = count - nFree; + this.count = capacity; + } else { + for (int i = 0; i < count; i++) { + items[i + this.count] = itemList[i]; + } + this.count += count; + } + } else { + Log.assert(nFree == 0); + if (count < capacity) { + int start = origin - count; + if (start < 0) { + start += this.count; + int chunkSize = this.count - start; + for (int i = 0; i < chunkSize; i++) { + items[i + start] = itemList[i]; + } + for (int i = 0; i < count - chunkSize; i++) { + items[i] = itemList[i + chunkSize]; + } + } else { + for (int i = 0; i < count; i++) { + items[i + start] = itemList[i]; + } + origin += count; + } + } else { + int offset = count - capacity; + for (int i = 0; i < count; i++) { + items[i] = itemList[i + offset]; + } + origin = 0; + } + this.count = capacity; + } + } + + public void Clear() { + origin = 0; + count = 0; + Array.Clear(items); + } + + public bool Contains(T item) => + items.Contains(item); + + public void CopyTo(T[] array, int arrayIndex) { + Array.Copy(items, origin, array, arrayIndex, count - origin); + Array.Copy(items, 0, array, arrayIndex + count - origin, origin); + } + + public IEnumerator GetEnumerator() { + var ct = count; + int i; + for (i = origin; i < ct; i++) { + yield return items[i]; + } + for (i = 0; i < origin; i++) { + yield return items[i]; + } + } + + public int IndexOf(T item) { + var idx = Array.IndexOf(items, item, 0, count); + if (idx == -1) { + return -1; + } + idx -= origin; + if (idx < 0) { + idx += count; + } + return idx; + } + + public void Insert(int index, T item) { + if (index > count) { + throw new IndexOutOfRangeException("Index exceeds Count"); + } + if (index == 0 && count == capacity) { + // This is a FIFO buffer. When at capacity, inserting an element at the front is a no-op. + return; + } + if (items.Length == count) { + // Resize buffer (reset origin to 0 in the process) + var newLength = Math.Min(items.Length * 2, capacity); + var newItems = newLength == items.Length ? items : new T[newLength]; + if (index >= count - origin) { + // Copy items[origin:end] to newItems[0:] + Array.Copy(items, origin, newItems, 0, count - origin); + // Copy items[0:index-count-origin] to newItems[count-origin:] + Array.Copy(items, 0, newItems, count - origin, index - (count - origin)); + // Copy items[index-count-origin:origin] to newItems[index+1:] + Array.Copy(items, index - (count - origin), newItems, index + 1, count - index); + } else if (index == 0) { + // Copy items[origin:end] to newItems[1:] + Array.Copy(items, origin, newItems, 1, count - origin); + // Copy items[0:origin] to newItems[1 + count - origin:] + Array.Copy(items, 0, newItems, 1 + count - origin, origin); + } else { + // Copy items[origin:idx] to newItems[0:] + Array.Copy(items, origin + 1, newItems, 0, index); + // Copy items[idx:] to newItems[index+1:] + Array.Copy(items, origin + 1 + index, newItems, index + 1, count - origin - index - 1); + // Copy items[0:origin] to newItems[index+count-idx] + Array.Copy(items, 0, newItems, count - origin, origin); + } + newItems[index] = item; + items = newItems; + origin = 0; + } else { + var idx = origin + index; + if (idx >= count) { + idx -= count; + } + var lastIdx = origin - 1; + if (lastIdx < 0) { + lastIdx += count; + } + var lastItem = items[lastIdx]; + Array.Copy(items, idx, items, idx + 1, count - idx - origin - 1); + Array.Copy(items, 0, items, 0, idx + origin); + items[lastIdx] = lastItem; + } + } + + public bool Remove(T item) { + var idx = Array.IndexOf(items, item, 0, count); + if (idx == -1) { + return false; + } + if (idx < origin) { + origin--; + } + ArrayHelper.remove_at(ref items, ref count, idx); + return true; + } + + public void RemoveAt(int index) { + ExceptionAssert.Index.IsValid(index, count); + index += origin; + if (index >= count) { + index -= count; + origin--; + } + ArrayHelper.remove_at(ref items, ref count, index); + } + + public void RemoveRange(int index, int count) { + if (index < 0 || index >= Count) { + throw new IndexOutOfRangeException(); + } + if (count < 0 || count > Count - index) { + throw new ArgumentOutOfRangeException(nameof(count)); + } + int rem = this.count - origin; + if (index < rem) { + if (count < rem) { + ArrayHelper.RemoveRange(ref items, origin + index, count); + } else { + ArrayHelper.RemoveRange(ref items, origin + index, rem); + ArrayHelper.RemoveRange(ref items, 0, count - rem); + origin -= count - rem; + } + } else { + ArrayHelper.RemoveRange(ref items, index - rem, count); + } + this.count -= count; + } + + IEnumerator IEnumerable.GetEnumerator() => + GetEnumerator(); + + void ensureCapacity(int minLength, bool constrain = true) { + if (minLength <= items.Length) { + return; + } + int newSize = items.Length * 2; + if (constrain) { + newSize = Math.Min(newSize, capacity); + } + var newItems = new T[newSize]; + CopyTo(newItems, 0); + origin = 0; + items = newItems; + } + + int getOffsetIndex(int index, bool canInsert = false) { + ExceptionAssert.Index.IsValid(index, count, canInsert); + index += origin; + if (index > count || (index == count && !canInsert)) { + index -= count; + } + return index; + } +} diff --git a/src/client/spark_core/utils/ArrayUtils.cs b/src/client/spark_core/utils/ArrayUtils.cs index 0e3d9e366..c27bc88de 100644 --- a/src/client/spark_core/utils/ArrayUtils.cs +++ b/src/client/spark_core/utils/ArrayUtils.cs @@ -1,181 +1,207 @@ -using mbts.utils; -using System; -using System.Collections; -using System.Collections.Generic; - -namespace bts.utils { - public static class ArrayHelper { - public static void trim(ref T[] array, int size) { - if (array.Length > size) { - T[] trimmed = new T[size]; - Array.Copy(array, trimmed, size); - array = trimmed; - } - } - - public static void ensure_capacity(ref T[] array, int min) { - if (array.Length < min) { - int twice_cap = array.Length * 2; - expand(ref array, min > twice_cap ? min : twice_cap); - } - } - - public static void expand(ref T[] array) => expand(ref array, array.Length * 2); - - public static void expand(ref T[] array, int new_capacity) { - if (array.Length < new_capacity) { - T[] expanded = new T[new_capacity]; - Array.Copy(array, expanded, array.Length); - array = expanded; - } - } - - public static TResult[] safe_convert(T[] array, Func convert) => - array == null ? Array.Empty() : Array.ConvertAll(array, new Converter(convert)); - - public static TResult[] convert(ICollection source, Func convert) { - var array = new TResult[source.Count]; - int i = 0; - foreach (var v in source) - array[i++] = convert(v); - return array; - } - - public static T[] clone(T[] array) { - T[] rv = new T[array.Length]; - Array.Copy(array, rv, array.Length); - return rv; - } - - /// - /// Attemps to get an exact item count (to initialize the resultant array with an exact - /// length, thus avoiding unneccessary allocations) by checking if the specified source - /// implements , , or - /// . - /// - /// The type of elements. - /// The source that will be copied to the resultant array. - /// An array containing the elements from the specified source. - public static T[] clone_optimized(IEnumerable source) { - if (source == null) - return Array.Empty(); - T[] result; - if (source is ICollection collection) { - result = new T[collection.Count]; - collection.CopyTo(result, 0); - } else if (source is IReadOnlyCollection ro_collection) { - result = new T[ro_collection.Count]; - int i = 0; - foreach (var v in ro_collection) - result[i++] = v; - } else if (source is ICollection ng_collection) { - result = new T[ng_collection.Count]; - int i = 0; - foreach (var v in source) - result[i++] = v; - } else { - int i = 0; - using (var en = source.GetEnumerator()) { - if (en.MoveNext()) { - result = new[] { en.Current }; - i = 1; - while (en.MoveNext()) { - ensure_capacity(ref result, i + 1); - result[i++] = en.Current; - } - trim(ref result, i); - } else { - return Array.Empty(); - } - } - } - return result; - } - - public static void Insert(ref T[] array, int index, T value, ref int size) { - // validate size/index - if (size < 0) - throw ExceptionBuilder.ArgumentOutOfRange.lt_zero(nameof(size)); - if (size > array.Length) - throw ExceptionBuilder.ArgumentOutOfRange.gt_length(nameof(size)); - if (index < 0) - throw ExceptionBuilder.IndexOutOfRange.lt_zero(); - if (index > size) - throw ExceptionBuilder.IndexOutOfRange.gt_length(); - T[] old_array = array; - checked { - if (size == array.Length) { - array = new T[array.Length * 2]; - Array.Copy(old_array, 0, array, 0, index); - } - if (index < size) - Array.Copy(old_array, index, array, index + 1, size - index); - array[index] = value; - size++; - } - } - - public static void remove_at(ref T[] array, int index) { - int sz = array.Length; - remove_at(ref array, ref sz, index); - } - - public static void remove_at(ref T[] array, ref int size, int index) { - // validate size/index - if (size < 0) - throw ExceptionBuilder.ArgumentOutOfRange.lt_zero(nameof(size)); - if (size > array.Length) - throw ExceptionBuilder.ArgumentOutOfRange.gte_length(nameof(size)); - if (index < 0) - throw ExceptionBuilder.IndexOutOfRange.lt_zero(); - if (index >= size) - throw ExceptionBuilder.IndexOutOfRange.gte_length(); - // shift [any]items right of 'index' left 1 pos. - if (index < size - 1) { - Array.Copy(array, index + 1, array, index, size - index - 1); - } - // clear last item, and decrement size - Array.Clear(array, --size, 1); - } - - public static void remove_sorted(ref T[] array, ref int size, T value, IComparer comparer) => - remove_sorted(ref array, ref size, value, comparer); - - public static void remove_sorted(ref T[] array, ref int size, T value, IComparer comparer, out int? pos) { - int p = Array.BinarySearch(array, 0, size, value, comparer); - if (p < 0) { - pos = null; - } else { - pos = p; - size--; - Array.Copy(array, p + 1, array, p, size - p); - array[size] = default; - } - } - - public static T[] add(T[] array, T value) { - T[] clone = new T[array.Length + 1]; - Array.Copy(array, clone, array.Length); - clone[array.Length] = value; - return clone; - } - - public static void add_sorted(ref T[] array, ref int size, T value, IComparer comparer) => - add_sorted(ref array, ref size, value, comparer, out int pos); - - public static void add_sorted(ref T[] array, ref int size, T value, IComparer comparer, out int pos) { - pos = peek_sorted_index(array, size, value, comparer); - ensure_capacity(ref array, size + 1); - Array.Copy(array, pos, array, pos + 1, size - pos); - array[pos] = value; - size++; - } - - public static int peek_sorted_index(T[] array, int size, T value, IComparer comparer) { - int index = Array.BinarySearch(array, 0, size, value, comparer); - if (index < 0) - index = ~index; - return index; - } - } -} +using mbts.utils; +using System; +using System.Collections; +using System.Collections.Generic; + +namespace bts.utils { + public static class ArrayHelper { + public static void trim(ref T[] array, int size) { + if (array.Length > size) { + T[] trimmed = new T[size]; + Array.Copy(array, trimmed, size); + array = trimmed; + } + } + + public static void ensure_capacity(ref T[] array, int min) { + if (array.Length < min) { + int twice_cap = array.Length * 2; + expand(ref array, min > twice_cap ? min : twice_cap); + } + } + + public static void expand(ref T[] array) => expand(ref array, array.Length * 2); + + public static void expand(ref T[] array, int new_capacity) { + if (array.Length < new_capacity) { + T[] expanded = new T[new_capacity]; + Array.Copy(array, expanded, array.Length); + array = expanded; + } + } + + public static TResult[] safe_convert(T[] array, Func convert) => + array == null ? Array.Empty() : Array.ConvertAll(array, new Converter(convert)); + + public static TResult[] convert(ICollection source, Func convert) { + var array = new TResult[source.Count]; + int i = 0; + foreach (var v in source) + array[i++] = convert(v); + return array; + } + + public static T[] clone(T[] array) { + T[] rv = new T[array.Length]; + Array.Copy(array, rv, array.Length); + return rv; + } + + /// + /// Attemps to get an exact item count (to initialize the resultant array with an exact + /// length, thus avoiding unneccessary allocations) by checking if the specified source + /// implements , , or + /// . + /// + /// The type of elements. + /// The source that will be copied to the resultant array. + /// An array containing the elements from the specified source. + public static T[] clone_optimized(IEnumerable source) { + if (source == null) + return Array.Empty(); + T[] result; + if (source is ICollection collection) { + result = new T[collection.Count]; + collection.CopyTo(result, 0); + } else if (source is IReadOnlyCollection ro_collection) { + result = new T[ro_collection.Count]; + int i = 0; + foreach (var v in ro_collection) + result[i++] = v; + } else if (source is ICollection ng_collection) { + result = new T[ng_collection.Count]; + int i = 0; + foreach (var v in source) + result[i++] = v; + } else { + int i = 0; + using (var en = source.GetEnumerator()) { + if (en.MoveNext()) { + result = new[] { en.Current }; + i = 1; + while (en.MoveNext()) { + ensure_capacity(ref result, i + 1); + result[i++] = en.Current; + } + trim(ref result, i); + } else { + return Array.Empty(); + } + } + } + return result; + } + + public static void Insert(ref T[] array, int index, T value, ref int size) { + // validate size/index + if (size < 0) + throw ExceptionBuilder.ArgumentOutOfRange.lt_zero(nameof(size)); + if (size > array.Length) + throw ExceptionBuilder.ArgumentOutOfRange.gt_length(nameof(size)); + if (index < 0) + throw ExceptionBuilder.IndexOutOfRange.lt_zero(); + if (index > size) + throw ExceptionBuilder.IndexOutOfRange.gt_length(); + T[] old_array = array; + checked { + if (size == array.Length) { + array = new T[array.Length * 2]; + Array.Copy(old_array, 0, array, 0, index); + } + if (index < size) + Array.Copy(old_array, index, array, index + 1, size - index); + array[index] = value; + size++; + } + } + + public static void remove_at(ref T[] array, int index) { + int sz = array.Length; + remove_at(ref array, ref sz, index); + } + + public static void remove_at(ref T[] array, ref int size, int index) { + // validate size/index + if (size < 0) + throw ExceptionBuilder.ArgumentOutOfRange.lt_zero(nameof(size)); + if (size > array.Length) + throw ExceptionBuilder.ArgumentOutOfRange.gte_length(nameof(size)); + if (index < 0) + throw ExceptionBuilder.IndexOutOfRange.lt_zero(); + if (index >= size) + throw ExceptionBuilder.IndexOutOfRange.gte_length(); + // shift [any]items right of 'index' left 1 pos. + if (index < size - 1) { + Array.Copy(array, index + 1, array, index, size - index - 1); + } + // clear last item, and decrement size + Array.Clear(array, --size, 1); + } + + public static void RemoveRange(ref T[] array, int index, int count, ref int size, bool clearLeftovers = true) { + ArgumentOutOfRangeException.ThrowIfNegative(size); + ArgumentOutOfRangeException.ThrowIfGreaterThan(size, array.Length); + ArgumentOutOfRangeException.ThrowIfNegative(index); + ArgumentOutOfRangeException.ThrowIfNegative(count); + ArgumentOutOfRangeException.ThrowIfGreaterThan(count, size - index); + if (count == 0) { + return; + } + Array.Copy(array, index + count, array, index, size - (index + count)); + if (clearLeftovers) { + Array.Clear(array, size - count, count); + } + size -= count; + } + + public static void RemoveRange(ref T[] array, int index, int count, bool clearLeftovers = true) { + ArgumentOutOfRangeException.ThrowIfNegative(index); + ArgumentOutOfRangeException.ThrowIfNegative(count); + ArgumentOutOfRangeException.ThrowIfGreaterThan(count, array.Length - index); + if (count == 0) { + return; + } + Array.Copy(array, index + count, array, index, array.Length - (index + count)); + if (clearLeftovers) { + Array.Clear(array, array.Length - count, count); + } + } + + public static void remove_sorted(ref T[] array, ref int size, T value, IComparer comparer, out int? pos) { + int p = Array.BinarySearch(array, 0, size, value, comparer); + if (p < 0) { + pos = null; + } else { + pos = p; + size--; + Array.Copy(array, p + 1, array, p, size - p); + array[size] = default; + } + } + + public static T[] add(T[] array, T value) { + T[] clone = new T[array.Length + 1]; + Array.Copy(array, clone, array.Length); + clone[array.Length] = value; + return clone; + } + + public static void add_sorted(ref T[] array, ref int size, T value, IComparer comparer) => + add_sorted(ref array, ref size, value, comparer, out int pos); + + public static void add_sorted(ref T[] array, ref int size, T value, IComparer comparer, out int pos) { + pos = peek_sorted_index(array, size, value, comparer); + ensure_capacity(ref array, size + 1); + Array.Copy(array, pos, array, pos + 1, size - pos); + array[pos] = value; + size++; + } + + public static int peek_sorted_index(T[] array, int size, T value, IComparer comparer) { + int index = Array.BinarySearch(array, 0, size, value, comparer); + if (index < 0) + index = ~index; + return index; + } + } +} diff --git a/src/client/spark_test/Collections/CircularBufferTest.cs b/src/client/spark_test/Collections/CircularBufferTest.cs new file mode 100644 index 000000000..b16594c9e --- /dev/null +++ b/src/client/spark_test/Collections/CircularBufferTest.cs @@ -0,0 +1,88 @@ +#nullable enable +namespace Spark.Test; + +using bts.collections; +using bts.test; + +using Spark.Core.Collections; + +using System.Collections.Generic; +using System.Linq; + +[TestGroup] +public class CircularBufferTest { + [Test] + public void TestAdd() { + int length = 10; + var nums = Enumerable.Range(0, length).ToList(); + var buffer = new CircularBuffer(length); + buffer.AddRange(nums); + Assert.That(buffer.SequenceEqual(nums)); + for (int i = 0; i < length; i++) { + buffer.Add(length + i); + Assert.That(buffer.SequenceEqual(Enumerable.Range(i + 1, length))); + } + } + + [Test] + public void TestInsert() { + int len = 10; + var nums = Enumerable.Range(0, len).ToList(); + const int INS_VAL = -1; + for (int i = 1; i < len; i++) { + var buffer = new CircularBuffer(len); + buffer.AddRange(nums); + buffer.Insert(i, INS_VAL); + var expected = Enumerable.Range(1, i).Append(INS_VAL).Concat(Enumerable.Range(i + 1, len - i - 1)); + Assert.That(buffer.SequenceEqual(expected)); + } + } + + [Test] + public void TestRemove() { + const int LEN = 10; + const int N_EXTRA = LEN / 2; + var nums = Enumerable.Range(0, LEN).ToList(); + for (int i = 0; i < LEN; i++) { + var buffer = new CircularBuffer(LEN); + buffer.AddRange(nums); + buffer.RemoveAt(i); + var expected = Enumerable.Range(0, i).Concat(Enumerable.Range(i + 1, LEN - i - 1)); + Assert.That(buffer.SequenceEqual(expected)); + buffer.AddRange(Enumerable.Range(LEN, N_EXTRA)); + buffer.RemoveAt(i); + // We added N_EXTRA elements. The original range began at x=0, so now it begins at + // x=N_EXTRA (CircularBuffer is FIFO). Before that, we removed the element at i. If + // i < N_EXTRA, the resultant gap will have been overwritten by the new elements. + // Otherwise, the gap will still be in the buffer, and the range will be discontinuous. + if (i < N_EXTRA) { + expected = Enumerable.Range(N_EXTRA, i).Concat(Enumerable.Range(N_EXTRA + i + 1, LEN - i - 1)); + Assert.That(buffer.SequenceEqual(expected)); + } else { + expected = Enumerable.Range(N_EXTRA - 1, i - N_EXTRA + 1) + .Concat(Enumerable.Range(i + 1, N_EXTRA - 1)) + .Concat(Enumerable.Range(i + 1 + N_EXTRA, LEN - 1 - i)); + Assert.That(buffer.SequenceEqual(expected)); + } + } + } + + [Test] + public void TestIndexOf() { + int length = 10; + var nums = Enumerable.Range(0, length).ToList(); + var buffer = new CircularBuffer(length); + buffer.AddRange(nums); + for (int i = 0; i < length; i++) { + Assert.That(buffer.IndexOf(i) == i); + } + for (int i = 0; i < length; i++) { + buffer.Add(length + i); + // Assert IndexOf works for the first, last, and middle item (if it + // works for those, it's safe to assume it'll work for the rest). + Assert.That(buffer.IndexOf(i + 1) == 0); + Assert.That(buffer.IndexOf(i + length / 2) == length / 2 - 1); + Assert.That(buffer.IndexOf(i + length) == length - 1); + } + } +}