chore: Add OffsetList class

This supports mutations (unlike the SubRangeWrapper class).
This commit is contained in:
John Colvin committed 2025-08-24 13:46:54 -04:00
1 parent a0ef2c3348
commit 39b3490f5a
2 files changed
+177

No files matched your search

@@ -0,0 +1,112 @@
#nullable enable
namespace Spark.Core.Collections;
using mbts.utils;
using System.Collections;
using System.Collections.Generic;
public class OffsetList<T> : IList<T>, IReadOnlyList<T>, IReadOnlyCollection<T> {
readonly IList<T> source;
int origin;
public OffsetList(IList<T> 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<T> 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;
}
}
@@ -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<int>(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<int>(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<int>(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<int>(nums, i);
offNums.Clear();
Assert.That(offNums.Count == 0 && nums.Count == 0);
}
}
}