From 39b3490f5a2fc66920ab739ab080ff324e7fd377 Mon Sep 17 00:00:00 2001 From: John Colvin Date: Tue, 22 Jul 2025 13:25:50 -0400 Subject: [PATCH] chore: Add OffsetList class This supports mutations (unlike the SubRangeWrapper class). --- .../spark_core/Collections/OffsetList.cs | 112 ++++++++++++++++++ .../spark_test/Collections/OffsetListTest.cs | 65 ++++++++++ 2 files changed, 177 insertions(+) create mode 100644 src/client/spark_core/Collections/OffsetList.cs create mode 100644 src/client/spark_test/Collections/OffsetListTest.cs diff --git a/src/client/spark_core/Collections/OffsetList.cs b/src/client/spark_core/Collections/OffsetList.cs new file mode 100644 index 000000000..38d02a86b --- /dev/null +++ b/src/client/spark_core/Collections/OffsetList.cs @@ -0,0 +1,112 @@ +#nullable enable +namespace Spark.Core.Collections; + +using mbts.utils; + +using System.Collections; +using System.Collections.Generic; + +public class OffsetList : IList, IReadOnlyList, IReadOnlyCollection { + readonly IList source; + int origin; + + public OffsetList(IList source, int origin) { + this.source = source; + this.origin = origin; + } + + public T this[int index] { + get => source[getOffsetIndex(index)]; + set => source[getOffsetIndex(index)] = value; + } + + public int Count => source.Count; + + public bool IsReadOnly => source.IsReadOnly; + + public void Add(T item) { + if (origin == 0) { + source.Add(item); + } else { + source.Insert(origin++, item); + } + } + + public void Clear() { + source.Clear(); + origin = 0; + } + + public bool Contains(T item) => + source.Contains(item); + + public void CopyTo(T[] array, int arrayIndex) { + var count = source.Count; + int i; + for (i = origin; i < count - origin; i++) { + array[arrayIndex++] = source[i]; + } + for (i = 0; i < origin; i++) { + array[arrayIndex++] = source[i]; + } + } + + public IEnumerator GetEnumerator() { + int i; + for (i = origin; i < source.Count; i++) { + yield return source[i]; + } + for (i = 0; i < origin; i++) { + yield return source[i]; + } + } + + public int IndexOf(T item) { + var idx = source.IndexOf(item); + if (idx == -1) { + return -1; + } + idx -= origin; + if (idx < 0) { + idx += source.Count; + } + return idx; + } + + public void Insert(int index, T item) { + index += origin; + var count = source.Count; + if (index > count) { + index -= count; + origin++; + } + source.Insert(index, item); + } + + public bool Remove(T item) => + source.Remove(item); + + public void RemoveAt(int index) { + var count = source.Count; + ExceptionAssert.Index.IsValid(index, count); + index += origin; + if (index >= count) { + index -= count; + origin--; + } + source.RemoveAt(index); + } + + IEnumerator IEnumerable.GetEnumerator() => + GetEnumerator(); + + int getOffsetIndex(int index, bool canInsert = false) { + var count = source.Count; + ExceptionAssert.Index.IsValid(index, count, canInsert); + index += origin; + if (index > count || (index == count && !canInsert)) { + index -= count; + } + return index; + } +} diff --git a/src/client/spark_test/Collections/OffsetListTest.cs b/src/client/spark_test/Collections/OffsetListTest.cs new file mode 100644 index 000000000..97fa368e2 --- /dev/null +++ b/src/client/spark_test/Collections/OffsetListTest.cs @@ -0,0 +1,65 @@ +#nullable enable +namespace Spark.Test; + +using bts.test; + +using Spark.Core.Collections; + +using System.Linq; + +[TestGroup] +public class OffsetListTest { + [Test] + public void TestEnumerate() { + int length = 10; + var nums = Enumerable.Range(0, length).ToList(); + for (int i = 0; i < length; i++) { + var off = new OffsetList(nums, i); + Assert.That(off.Take(length - i).SequenceEqual(Enumerable.Range(i, length - i))); + Assert.That(off.Skip(length - i).SequenceEqual(Enumerable.Range(0, i))); + Assert.That(off.SequenceEqual(Enumerable.Range(i, length - i).Concat(Enumerable.Range(0, i)))); + } + } + + [Test] + public void TestAdd() { + int length = 10; + for (int i = 0; i < length; i++) { + var nums = Enumerable.Range(0, length).ToList(); + var offNums = new OffsetList(nums, i); + // Add item to offNums (this mutates the source list, nums). + offNums.Add(length); + var expectedOffNums = Enumerable.Range(i, length - i).Concat(Enumerable.Range(0, i)).Append(length); + var expectedNums = i == 0 + ? Enumerable.Range(0, length + 1) + : Enumerable.Range(0, i).Append(length).Concat(Enumerable.Range(i, length - i)); + Assert.That(offNums.SequenceEqual(expectedOffNums)); + Assert.That(nums.SequenceEqual(expectedNums)); + } + } + + [Test] + public void TestRemove() { + int length = 10; + for (int i = 0; i < length; i++) { + for (int j = 0; j < length; j++) { + var nums = Enumerable.Range(0, length).ToList(); + var offNums = new OffsetList(nums, i); + offNums.RemoveAt(j); + Assert.That(offNums.Take(j).SequenceEqual(Enumerable.Range(0, j))); + Assert.That(offNums.Skip(j).SequenceEqual(Enumerable.Range(j + 1, length - j - 1))); + } + } + } + + [Test] + public void TestClear() { + int length = 10; + for (int i = 0; i < length; i++) { + var nums = Enumerable.Range(0, length).ToList(); + var offNums = new OffsetList(nums, i); + offNums.Clear(); + Assert.That(offNums.Count == 0 && nums.Count == 0); + } + } +}