chore: Add various collection classes

These will be useful in stream operators, row indexing, etc.

* AggregateList
* DeltaList
* DictionarySetWrapper
* ImmutableOrderedDictionary
* IMutableListFacade
  - inherits IReadOnlyList<T> and IList<T>
  - throws NotSupportedException in default impls for mutation ops
* IMutableCollectionFacade
  - inherits IReadOnlyCollection<T> and ICollection<T>
  - throws NotSupportedException in default impls for mutation ops
* MergedSortedList
* OrderedDictionary
* ReverseArrayList
* SliceList
* StepList
This commit is contained in:
John Colvin committed 2025-11-19 14:03:46 -05:00
1 parent 85babef37b
commit d6158bc9a0
29 files changed
+3452 -2

No files matched your search

@@ -0,0 +1,49 @@
#nullable enable
namespace Spark.Core.Collections;
using bts.collections;
using System;
using System.Collections.Generic;
using System.Linq;
public static class AggregateList {
public static AggregateList<T> Create<T>(params IList<T>[] lists) =>
new(lists);
public static AggregateList<T> Create<T>(IEnumerable<IList<T>> lists) =>
new(lists);
}
public class AggregateList<T> : IMutableListFacade<T> {
static readonly IComparer<(IList<T> Items, int Index)> indexedListComparer =
TupleComparer.Comparer<IList<T>, int>(item2Ordinal: 0);
readonly (IList<T> Items, int Index)[] indexedLists;
public AggregateList(IEnumerable<IList<T>> lists) =>
indexedLists = lists.Where(l => l.Count > 0)
.AggregateEach((Items: (IList<T>)null!, Index: 0), (last, next) => (next, last.Items?.Count ?? 0))
.ToArray();
public T this[int index] {
get {
if (index < 0 || index >= Count) {
throw new IndexOutOfRangeException();
}
var indexedListIdx = indexedLists.BinarySearch((null!, index), indexedListComparer);
if (indexedListIdx < 0) {
indexedListIdx = ~indexedListIdx;
}
var (items, origin) = indexedLists[indexedListIdx];
return items[index - origin];
}
}
public int Count => indexedLists.Length == 0
? 0
: indexedLists[^1].Index + indexedLists[^1].Items.Count;
public IEnumerator<T> GetEnumerator() =>
indexedLists.SelectMany(l => l.Items).GetEnumerator();
}
@@ -0,0 +1,28 @@
#nullable enable
namespace Spark.Core.Collections;
using System.Collections.Immutable;
/// <summary>
/// Represents a unit of change that can be applied to a list.
/// </summary>
public readonly struct BlockTransaction<T> {
public readonly int Index;
public readonly int NumRemoved;
public readonly ImmutableList<T> InsertedValues;
public readonly int InitialOffset;
public BlockTransaction(int index, int initialOffset, int numRemoved = 0, ImmutableList<T>? insertedValues = null) {
Index = index;
NumRemoved = numRemoved;
InsertedValues = insertedValues ?? ImmutableList<T>.Empty;
InitialOffset = initialOffset;
}
public int Offset => InsertedValues.Count - NumRemoved;
public int CummulativeOffset => InitialOffset + Offset;
public int DestIndex => Index + InitialOffset;
public override string ToString() =>
$"{{ \"src\": {Index}, \"dst\": {DestIndex}, \"ins\": {InsertedValues.Count}, \"rem\": {NumRemoved} \"initOff\": {InitialOffset}, \"cumOff\": {CummulativeOffset} }}";
}
@@ -0,0 +1,279 @@
#nullable enable
namespace Spark.Core.Collections;
using bts;
using bts.collections;
using System;
using System.Collections;
using System.Collections.Generic;
using System.Collections.Immutable;
using System.Linq;
public static class BlockTransactionEnumerable {
public static BlockTransactionEnumerable<T> Create<T>(IList<T> source, IList<int> sortedDeletes, IList<(int Index, T Value)> sortedAdds) =>
new(source, sortedDeletes, sortedAdds);
public static IEnumerable<BlockTransaction<T>> ToSliceBlocks<T>(this IEnumerable<BlockTransaction<T>> blocks, int sourceStartIndex, int destStartIndex) {
BlockTransaction<T>? tx = null;
foreach (var block in blocks) {
var numRemoved = block.NumRemoved;
var numInserted = block.InsertedValues.Count;
var gap = destStartIndex - block.DestIndex;
if (gap > 0) {
// Subtract inserts/removes that cancel eachother out before the slice.
var numChangedInGap = Math.Min(gap, Math.Min(numRemoved, numInserted));
numRemoved -= numChangedInGap;
numInserted -= numChangedInGap;
}
if (numRemoved == 0 && numInserted == 0) {
continue;
}
if (!tx.HasValue) {
tx = block;
continue;
}
if (block.Index <= sourceStartIndex && numRemoved > 0) {
tx = CombineDeletes(tx.Value, numRemoved);
numRemoved = 0;
}
if (block.DestIndex + tx.Value.InsertedValues.Count + block.InsertedValues.Count <= destStartIndex) {
tx = Combine(tx.Value, numRemoved, block.InsertedValues.Slice(-numInserted));
} else {
yield return tx.Value;
tx = new(block.Index, tx.Value.CummulativeOffset, numRemoved, block.InsertedValues.Slice(-numInserted).ToImmutableList());
}
}
if (tx.HasValue) {
yield return tx.Value;
}
static BlockTransaction<T> CombineDeletes(BlockTransaction<T> tx, int numRemoved) =>
new(tx.Index, tx.InitialOffset, tx.NumRemoved + numRemoved, tx.InsertedValues);
static BlockTransaction<T> Combine(BlockTransaction<T> tx, int numRemoved, IEnumerable<T> insertedValues) =>
new(tx.Index, tx.InitialOffset, tx.NumRemoved + numRemoved, tx.InsertedValues.AddRange(insertedValues));
}
public static IEnumerable<BlockTransactionMetadata> ToReducedMetadata<T>(this IEnumerable<BlockTransaction<T>> blocks, int sourceStartIndex = 0, int destStartIndex = 0) {
BlockTransactionMetadata? tx = null;
foreach (var block in blocks) {
var numRemoved = block.NumRemoved;
var numInserted = block.InsertedValues.Count;
var gap = destStartIndex - block.DestIndex;
if (gap > 0) {
// Subtract inserts/removes that cancel eachother out before the slice.
var numChangedInGap = Math.Min(gap, Math.Min(numRemoved, numInserted));
numRemoved -= numChangedInGap;
numInserted -= numChangedInGap;
}
if (numRemoved == 0 && numInserted == 0) {
continue;
}
if (!tx.HasValue) {
tx = new(block.Index, block.DestIndex, numInserted, numRemoved, numInserted - numRemoved);
continue;
}
if (block.Index <= sourceStartIndex && numRemoved > 0) {
tx = CombineDeletes(tx.Value, numRemoved);
numRemoved = 0;
}
if (block.DestIndex + tx.Value.NumInserted + block.InsertedValues.Count <= destStartIndex) {
tx = Combine(tx.Value, numRemoved, numInserted);
} else {
yield return tx.Value;
tx = new(block.Index, block.DestIndex, numInserted, numRemoved, tx.Value.CummulativeOffset + numInserted - numRemoved);
}
}
if (tx.HasValue) {
yield return tx.Value;
}
static BlockTransactionMetadata CombineDeletes(BlockTransactionMetadata tx, int numRemoved) =>
new(tx.SourceIndex, tx.DestIndex, tx.NumInserted, tx.NumRemoved + numRemoved, tx.CummulativeOffset - numRemoved);
static BlockTransactionMetadata Combine(BlockTransactionMetadata tx, int numRemoved, int numInserted) =>
new(tx.SourceIndex, tx.DestIndex, tx.NumInserted + numInserted, tx.NumRemoved + numRemoved, tx.CummulativeOffset + numInserted - numRemoved);
}
public static void SliceDiff<T>(this IList<T> source, IList<T> newValues, IList<int> removes, IList<(int Index, T Value)> inserts, ref List<T> removedValues, ref List<T> insertedValues, int? start, int? stop) =>
Create(source, removes, inserts)
.SliceDiff(source, newValues, ref removedValues, ref insertedValues, start, stop);
public static (List<T> Removes, List<T> Inserts) SliceDiff<T>(this IEnumerable<BlockTransaction<T>> blocks, IList<T> source, IList<T> newValues, int? start, int? stop) {
var removes = new List<T>();
var inserts = new List<T>();
blocks.SliceDiff(source, newValues, ref removes, ref inserts, start, stop);
return (removes, inserts);
}
public static void SliceDiff<T>(this IEnumerable<BlockTransaction<T>> blocks, IList<T> source, IList<T> newValues, ref List<T> removes, ref List<T> inserts, int? start, int? stop) {
int srcCount = source.Count;
int srcStartIdx = SliceList.GetStartIndex(start, srcCount);
int srcStopIdx = SliceList.GetStopIndex(stop, srcCount, true);
int dstCount = newValues.Count;
int dstStartIdx = SliceList.GetStartIndex(start, dstCount);
int dstStopIdx = SliceList.GetStopIndex(stop, dstCount, true);
blocks.ToReducedMetadata(srcStartIdx, dstStartIdx)
.TakeWhile(b => b.DestIndex < dstStopIdx)
.SliceDiff(source, newValues, ref removes, ref inserts, srcStartIdx, srcStopIdx, dstStartIdx, dstStopIdx);
}
public static void SliceDiff<T>(this IEnumerable<BlockTransactionMetadata> blocks, IList<T> source, IList<T> newValues, ref List<T> sliceRemoves, ref List<T> sliceInserts, int? start, int? stop) {
int srcCount = source.Count;
int srcStartIdx = SliceList.GetStartIndex(start, srcCount);
int srcStopIdx = SliceList.GetStopIndex(stop, srcCount, true);
int dstCount = newValues.Count;
int dstStartIdx = SliceList.GetStartIndex(start, dstCount);
int dstStopIdx = SliceList.GetStopIndex(stop, dstCount, true);
blocks.SliceDiff(source, newValues, ref sliceRemoves, ref sliceInserts, srcStartIdx, srcStopIdx, dstStartIdx, dstStopIdx);
}
public static void SliceDiff<T>(this IEnumerable<BlockTransactionMetadata> blocks, IList<T> source, IList<T> newValues, ref List<T> sliceRemoves, ref List<T> sliceInserts, int srcStartIdx, int srcStopIdx, int dstStartIdx, int dstStopIdx) {
int srcCount = source.Count;
bool srcSlicePopulated = srcCount > srcStartIdx && srcStopIdx > srcStartIdx;
int dstCount = newValues.Count;
bool dstSlicePopulated = dstCount > dstStartIdx && dstStopIdx > dstStartIdx;
if (!srcSlicePopulated) {
if (dstSlicePopulated) {
sliceInserts.AddRange(newValues.WrapSubRange(dstStartIdx, dstStopIdx - dstStartIdx));
}
return;
}
if (!dstSlicePopulated) {
sliceRemoves.AddRange(source.WrapSubRange(srcStartIdx, srcStopIdx - srcStartIdx));
return;
}
Log.assert(srcStartIdx >= 0);
Log.assert(dstStartIdx >= 0);
var sourceSliceLength = srcStopIdx - srcStartIdx;
var destSliceLength = dstStopIdx - dstStartIdx;
var lengthChange = destSliceLength - sourceSliceLength;
var inserts = new List<int>();
var removes = new List<int>();
using var en = blocks.GetEnumerator();
if (!en.MoveNext() || en.Current.DestIndex >= dstStopIdx) {
Log.assert(destSliceLength == sourceSliceLength);
for (int i = 0; i < -lengthChange; i++) {
removes.Add(srcStopIdx + lengthChange + i);
}
sliceRemoves.AddRange(removes.WrapSelector(i => source[i], null));
return;
}
int nEarlyInserts = 0;
BlockTransactionMetadata tx = new();
do {
tx = en.Current;
if (tx.DestIndex < dstStartIdx) {
tx.DestIndex = dstStartIdx + nEarlyInserts;
nEarlyInserts += tx.NumInserted;
}
if (tx.SourceIndex < srcStartIdx) {
tx.SourceIndex = srcStartIdx;
}
removes.AddRange(Enumerable.Range(tx.SourceIndex, tx.NumRemoved));
inserts.AddRange(Enumerable.Range(tx.DestIndex, tx.NumInserted));
} while (en.MoveNext() && en.Current.DestIndex < dstStopIdx);
var remOffset = tx.CummulativeOffset;
if (remOffset > 0) {
// Remove items that were pushed out of the back by earlier inserts
for (int i = remOffset - lengthChange; i > 0; i--) {
var idx = srcStopIdx - i;
if (idx >= srcStartIdx && idx < srcStopIdx) {
removes.Add(idx);
}
}
} else if (remOffset < 0) {
// Add items that were pulled into the back by earlier removes
for (int i = -remOffset - lengthChange; i > 0; i--) {
var idx = dstStopIdx - i;
if (idx >= dstStartIdx && idx < dstStopIdx) {
inserts.Add(idx);
}
}
}
sliceRemoves.AddRange(removes.WrapSelector(i => source[i], null));
sliceInserts.AddRange(inserts.WrapSelector(i => newValues[i], null));
}
}
public class BlockTransactionEnumerable<T> : IEnumerable<BlockTransaction<T>> {
readonly IList<T> source;
// readonly IList<T> newValues;
readonly IList<int> sortedDeletes;
readonly IList<(int Index, T Value)> sortedAdds;
public BlockTransactionEnumerable(IList<T> source, IList<int> sortedDeletes, IList<(int Index, T Value)> sortedAdds) {
this.source = source;
// this.newValues = newValues;
this.sortedDeletes = sortedDeletes;
this.sortedAdds = sortedAdds;
}
public IEnumerator<BlockTransaction<T>> GetEnumerator() {
// Log.assert(newValues.Count == source.Count + sortedAdds.Count - sortedDeletes.Count);
int delCursor = 0;
int addCursor = 0;
BlockTransaction<T>? block = null;
// lastDel/Cursor and lastAddIndex/Cursor are only used to assert that items are sorted.
int lastDel = -1;
int lastDelCursor = -1;
int lastAddIndex = -1;
int lastAddCursor = -1;
while (true) {
int? del = delCursor < sortedDeletes.Count ? sortedDeletes[delCursor] : null;
if (del.HasValue && delCursor > lastDelCursor) {
Log.assert(del.Value > lastDel);
lastDel = del.Value;
lastDelCursor = delCursor;
}
(int Index, T Value)? add = addCursor < sortedAdds.Count ? sortedAdds[addCursor] : null;
if (add.HasValue && addCursor > lastAddCursor) {
Log.assert(add.Value.Index >= lastAddIndex);
lastAddIndex = add.Value.Index;
lastAddCursor = addCursor;
}
if (del.HasValue && add.HasValue && del.Value == add.Value.Index) {
if (!block.HasValue) {
block = new(del.Value, 0, 1, ImmutableList<T>.Empty.Add(add.Value.Value));
} else if (block.Value.InsertedValues.Count == 0 && (block.Value.Index + block.Value.NumRemoved) == del.Value) {
block = new(block.Value.Index, block.Value.InitialOffset, block.Value.NumRemoved + 1, block.Value.InsertedValues.Add(add.Value.Value));
} else {
yield return block.Value;
block = new(del.Value, block.Value.InitialOffset + block.Value.Offset, 1, ImmutableList<T>.Empty.Add(add.Value.Value));
}
delCursor++;
addCursor++;
} else if (del.HasValue && (!add.HasValue || del.Value < add.Value.Index)) {
if (!block.HasValue) {
block = new(del.Value, 0, 1, ImmutableList<T>.Empty);
} else if (block.Value.Index + block.Value.NumRemoved == del.Value) {
block = new(block.Value.Index, block.Value.InitialOffset, block.Value.NumRemoved + 1, block.Value.InsertedValues);
} else {
yield return block.Value;
block = new(del.Value, block.Value.InitialOffset + block.Value.Offset, 1, ImmutableList<T>.Empty);
}
delCursor++;
} else if (add.HasValue) {
if (!block.HasValue) {
block = new(add.Value.Index, 0, 0, ImmutableList<T>.Empty.Add(add.Value.Value));
} else if (block.Value.Index == add.Value.Index) {
block = new(block.Value.Index, block.Value.InitialOffset, block.Value.NumRemoved, block.Value.InsertedValues.Add(add.Value.Value));
} else {
yield return block.Value;
block = new(add.Value.Index, block.Value.InitialOffset + block.Value.Offset, 0, ImmutableList<T>.Empty.Add(add.Value.Value));
}
addCursor++;
} else {
if (block.HasValue) {
yield return block.Value;
}
break;
}
}
}
IEnumerator IEnumerable.GetEnumerator() =>
GetEnumerator();
}
@@ -0,0 +1,25 @@
#nullable enable
namespace Spark.Core.Collections;
/// <summary>
/// Represents a unit of change that can be applied to a list. The only difference
/// between this struct and <see cref="BlockTrasaction{T}"/>, is that this has the
/// number of values to insert, while the latter has the actual list of values.
/// </summary>
public struct BlockTransactionMetadata {
public int SourceIndex;
public int DestIndex;
public int NumInserted;
public int NumRemoved;
public int CummulativeOffset;
public BlockTransactionMetadata(int sourceIndex, int destIndex, int numInserted, int numRemoved, int cummulativeOffset) {
SourceIndex = sourceIndex;
DestIndex = destIndex;
NumInserted = numInserted;
NumRemoved = numRemoved;
CummulativeOffset = cummulativeOffset;
}
public readonly int Offset => NumInserted - NumRemoved;
}
@@ -0,0 +1,42 @@
namespace Spark.Core.Collections;
using System;
using System.Collections;
using System.Collections.Generic;
public static class DeltaList {
public static DeltaList<long, long> Create(IList<long> source) =>
new(source, (lhs, rhs) => lhs - rhs);
public static DeltaList<T, TDelta> Create<T, TDelta>(IList<T> source, Func<T, T, TDelta> getDelta) =>
new(source, getDelta);
}
public class DeltaList<T, TDelta> : IMutableListFacade<TDelta> {
readonly IList<T> source;
readonly Func<T, T, TDelta> getDelta;
public DeltaList(IList<T> source, Func<T, T, TDelta> getDelta) {
this.source = source;
this.getDelta = getDelta;
}
public TDelta this[int index] =>
getDelta(source[index + 1], source[index]);
public int Count => Math.Max(0, source.Count - 1);
public IEnumerator<TDelta> GetEnumerator() {
using var en = source.GetEnumerator();
if (en.MoveNext()) {
var prev = en.Current;
while (en.MoveNext()) {
yield return getDelta(en.Current, prev);
prev = en.Current;
}
}
}
IEnumerator IEnumerable.GetEnumerator() =>
GetEnumerator();
}
@@ -0,0 +1,118 @@
#nullable enable
namespace Spark.Core.Collections;
using System.Collections;
using System.Collections.Generic;
using System.Collections.Immutable;
using System.Linq;
public class DictionarySetWrapper<TKey, TValue> : IReadOnlySet<TKey> {
readonly IDictionary<TKey, TValue> source;
readonly IEqualityComparer<TKey> comparer;
public DictionarySetWrapper(IDictionary<TKey, TValue> source, IEqualityComparer<TKey>? comparer = null) {
this.source = source;
this.comparer = comparer ?? ImplicitComparer(source) ?? EqualityComparer<TKey>.Default;
static IEqualityComparer<TKey>? ImplicitComparer(IDictionary<TKey, TValue> source) =>
source switch {
#nullable disable
Dictionary<TKey, TValue> dct => dct.Comparer,
ImmutableDictionary<TKey, TValue> idct => idct.KeyComparer,
#nullable enable
_ => null,
};
}
public int Count => source.Count;
public bool Contains(TKey item) =>
source.ContainsKey(item);
public bool IsProperSubsetOf(IEnumerable<TKey> other) =>
isSubsetOf(other, true);
public bool IsProperSupersetOf(IEnumerable<TKey> other) {
int count = source.Count;
if (count == 0) {
return false;
}
var knownKeys = new HashSet<TKey>(comparer);
foreach (var k in other) {
if (!source.ContainsKey(k) || (knownKeys.Add(k) && knownKeys.Count == count)) {
return false;
}
}
return true;
}
/// <summary>Determines whether a <see cref="HashSet{T}"/> object is a subset of the specified collection.</summary>
/// <param name="other">The collection to compare to the current <see cref="HashSet{T}"/> object.</param>
/// <returns>true if the <see cref="HashSet{T}"/> object is a subset of <paramref name="other"/>; otherwise, false.</returns>
public bool IsSubsetOf(IEnumerable<TKey> other) =>
isSubsetOf(other);
public bool IsSupersetOf(IEnumerable<TKey> other) =>
other.All(source.ContainsKey);
public bool Overlaps(IEnumerable<TKey> other) =>
source.Count > 0 && other.Any(source.ContainsKey);
public bool SetEquals(IEnumerable<TKey> other) {
var otherSet = new HashSet<TKey>(comparer);
int count = source.Count;
foreach (var k in other) {
if (!source.ContainsKey(k) || (otherSet.Add(k) && otherSet.Count > count)) {
return false;
}
}
return true;
}
public IEnumerable<TKey> Intersection(IEnumerable<TKey> other) {
int count = Count;
var knownKeys = new HashSet<TKey>(0, comparer);
using var en = other.GetEnumerator();
while (en.MoveNext() && knownKeys.Count < count) {
var k = en.Current;
if (source.ContainsKey(k) && knownKeys.Add(k)) {
yield return k;
}
}
}
public IEnumerator<TKey> GetEnumerator() =>
source.Keys.GetEnumerator();
IEnumerator IEnumerable.GetEnumerator() =>
GetEnumerator();
bool isSubsetOf(IEnumerable<TKey> other, bool proper = false) {
var otherSet = new HashSet<TKey>(comparer);
using var en = other.GetEnumerator();
foreach (var k in source.Keys) {
if (otherSet.Contains(k)) {
continue;
}
var hash = comparer.GetHashCode(k!);
while (true) {
if (!en.MoveNext()) {
return false;
}
var k2 = en.Current!;
// Compare hash codes before objects, because the hash fn is likely cheap, and collisions rare.
if (otherSet.Add(k2) && comparer.GetHashCode(k2) == hash && comparer.Equals(k2, k)) {
break;
}
}
}
int count = source.Count;
if (proper) {
count++;
}
while (otherSet.Count < count && en.MoveNext()) {
otherSet.Add(en.Current);
}
return otherSet.Count >= count;
}
}
@@ -1,4 +1,6 @@
namespace bts.collections {
using Spark.Core.Collections;
using System;
using System.Collections.Generic;
using System.Diagnostics.CodeAnalysis;
@@ -13,6 +15,9 @@
} else return array[index];
}
public static IList<T> Reverse<T>(this T[] array) =>
new ReverseArrayList<T>(array);
public static TResult[] SelectArray<T, TResult>(this T[] array, Func<T, TResult> selector) =>
Array.ConvertAll(array, new Converter<T, TResult>(selector));
@@ -18,6 +18,13 @@ namespace System.Collections.Generic {
using System.Runtime.Versioning;
public static class EnumerableExtensions {
public static IEnumerable<TAccumulate> AggregateEach<T, TAccumulate>(this IEnumerable<T> source, TAccumulate seed, Func<TAccumulate, T, TAccumulate> accumulate) {
foreach (var v in source) {
seed = accumulate(seed, v);
yield return seed;
}
}
/// <summary>
/// Determines whether all elements of a sequence are true.
/// </summary>
@@ -369,6 +376,12 @@ namespace System.Collections.Generic {
this IEnumerable<T> source, Func<T, TResult> selector, IEqualityComparer<TResult> comparer) =>
source.Select(selector).ToHashSet(comparer);
public static OrderedDictionary<TKey, TValue> ToOrderedDictionary<T, TKey, TValue>(this IEnumerable<T> source, Func<T, TKey> keySelector, Func<T, TValue> valueSelector) where TKey : notnull =>
new(source.Select(x => new KeyValuePair<TKey, TValue>(keySelector(x), valueSelector(x))));
public static OrderedDictionary<TKey, TValue> ToOrderedDictionary<TKey, TValue>(this IEnumerable<KeyValuePair<TKey, TValue>> source) where TKey : notnull =>
new(source);
public static bool TryGetFirst<T>(this IEnumerable<T> source, [MaybeNullWhen(false)]out T result) {
if (source == null) throw new ArgumentNullException(nameof(source));
using (var en = source.GetEnumerator()) {
@@ -168,6 +168,15 @@ namespace bts.collections {
return true;
}
public static bool RemoveSorted<T>(this List<T> list, T item, IComparer<T> comparer) {
int i = list.BinarySearch(item, comparer);
if (i < 0) {
return false;
}
list.RemoveAt(i);
return true;
}
public static void RemoveSorted<T>(this IList<T> list, IEnumerable<T> items) where T : IComparable<T> =>
RemoveSorted(list, items, Comparer<T>.Default);
@@ -439,6 +448,9 @@ namespace bts.collections {
public static IReadOnlyList<T> WrapReverseRO<T>(this IList<T> list) =>
new ReverseWrapper<T>(list);
public static IList<T> Slice<T>(this IList<T> source, int? start = null, int? stop = null) =>
new SliceList<T>(source, start, stop);
public static IList<T> WrapSubRange<T>(this IList<T> list, int start) =>
new SubRangeWrapper<T>(list, start, list.Count - start);
@@ -0,0 +1,18 @@
#nullable enable
namespace Spark.Core.Collections;
using System;
using System.Collections;
using System.Collections.Generic;
public interface IDualComparer<T> : IComparer<T>, IComparer {
int IComparer.Compare(object? x, object? y) {
if (x is not T xt && !ConvertUtils.try_convert(x, out xt)) {
throw new ArgumentException($"Unable to convert to type {typeof(T)}", nameof(x));
}
if (y is not T yt && !ConvertUtils.try_convert(y, out yt)) {
throw new ArgumentException($"Unable to convert to type {typeof(T)}", nameof(y));
}
return Compare(xt, yt);
}
}
@@ -0,0 +1,33 @@
#nullable enable
namespace Spark.Core.Collections;
using System;
using System.Collections;
using System.Collections.Generic;
public interface IMutableCollectionFacade<T> : ICollection<T> {
bool ICollection<T>.IsReadOnly => true;
void ICollection<T>.Add(T item) => throw new NotSupportedException();
bool ICollection<T>.Remove(T item) => throw new NotSupportedException();
void ICollection<T>.Clear() => throw new NotSupportedException();
bool ICollection<T>.Contains(T item) {
var comp = EqualityComparer<T>.Default;
foreach (var v in this) {
if (comp.Equals(v, item)) {
return true;
}
}
return false;
}
void ICollection<T>.CopyTo(T[] array, int arrayIndex) {
foreach (var v in this) {
array[arrayIndex++] = v;
}
}
IEnumerator IEnumerable.GetEnumerator() =>
GetEnumerator();
}
@@ -0,0 +1,25 @@
#nullable enable
namespace Spark.Core.Collections;
using System;
using System.Collections.Generic;
public interface IMutableListFacade<T> : IReadOnlyList<T>, IList<T>, IMutableCollectionFacade<T> {
T IList<T>.this[int index] {
get => ((IReadOnlyList<T>)this)[index];
set => throw new NotSupportedException();
}
void IList<T>.Insert(int index, T item) => throw new NotSupportedException();
void IList<T>.RemoveAt(int index) => throw new NotImplementedException();
int IList<T>.IndexOf(T item) {
var comp = EqualityComparer<T>.Default;
int idx = 0;
foreach (var v in this) {
if (comp.Equals(v, item)) {
return idx;
}
idx++;
}
return -1;
}
}
@@ -0,0 +1,643 @@
#nullable enable
namespace Spark.Core.Collections;
using bts;
using bts.collections;
using System;
using System.Collections;
using System.Collections.Generic;
using System.Collections.Immutable;
using System.Diagnostics.CodeAnalysis;
using System.Linq;
public static class ImmutableOrderedDictionary {
public static ImmutableOrderedDictionary<TKey, TValue> Create<TKey, TValue>(IEqualityComparer<TKey>? comparer = null) where TKey : notnull =>
new(comparer: comparer);
public static ImmutableOrderedDictionary<TKey, TValue> CreateRange<TKey, TValue>(IEnumerable<KeyValuePair<TKey, TValue>> items) where TKey : notnull =>
new ImmutableOrderedDictionary<TKey, TValue>().AddRange(items);
public static ImmutableOrderedDictionary<TKey, TValue> CreateRange<TKey, TValue>(IEqualityComparer<TKey>? comparer, IEnumerable<KeyValuePair<TKey, TValue>> items) where TKey : notnull =>
new ImmutableOrderedDictionary<TKey, TValue>(comparer).AddRange(items);
}
public class ImmutableOrderedDictionary<TKey, TValue> : IDictionary<TKey, TValue>, IList<KeyValuePair<TKey, TValue>> where TKey : notnull {
public static readonly ImmutableOrderedDictionary<TKey, TValue> Empty = new();
// These represent the min/max gap size between consecutive ordinals when the collection is
// balanced. Gaps (greater than 1) are there so that inserts can be performed without needing to
// update the ordinals of every item after the target index. The collection will gradually become
// unbalanced as mutations cause the gaps between ordinals to shrink. It is rebalanced when:
// A) an insert is attempted between 2 neighbors with a gap of 1, or
// B) an add is attempted when the last ordinal is ulong.MaxValue.
//
// Thus, the min/max ordinal steps have been tuned (crudely) to minimize rebalances. Here's the
// basic school of thought:
// A value of 1 is most optimial for adds, as it maximizes the number of elements that can be
// appended to the end of the list before the key ordinals must be rebalanced. However, a value
// of 1 is also the LEAST optimal for inserts, as it leaves no "ordinal space" between items,
// meaning the key ordinals would need to be rebalanced on each insert.
const ulong MAX_ORDINAL_STEP = 1 << 16;
const ulong ORDINAL_SEED = 1 << 12;
const int MAX_NUM_ITEMS = int.MaxValue;
readonly ImmutableDictionary<TKey, (TValue Value, ulong Ordinal)> data;
readonly ImmutableSortedSet<(ulong Ordinal, TKey Key)> ordinalKeys;
readonly IList<TKey> orderedKeys;
readonly SelectorList<TKey, TValue> orderedValues;
public ImmutableOrderedDictionary(IEqualityComparer<TKey>? comparer = null)
: this(ImmutableDictionary.Create<TKey, (TValue Value, ulong Ordinal)>(comparer), ImmutableSortedSet.Create<(ulong, TKey)>(TupleComparer.Comparer<ulong, TKey>(0, null))) { }
ImmutableOrderedDictionary(ImmutableDictionary<TKey, (TValue Value, ulong Ordinal)> data, ImmutableSortedSet<(ulong Ordinal, TKey Key)> ordinalKeys) {
this.data = data;
this.ordinalKeys = ordinalKeys;
orderedKeys = ordinalKeys.WrapSelector(p => p.Key, null);
orderedValues = new SelectorList<TKey, TValue>(orderedKeys, k => data[k].Value, null);
OrdinalGaps = DeltaList.Create(ordinalKeys.WrapSelector(p => p.Ordinal, null), (x, y) => checked(x - y));
}
public IEqualityComparer<TKey> Comparer => data.KeyComparer;
public TValue this[TKey key] => data[key].Value;
public IList<TKey> Keys => orderedKeys;
public IList<TValue> Values => orderedValues;
public int Count => ordinalKeys.Count;
public bool IsReadOnly => false;
public IList<ulong> OrdinalGaps { get; }
ICollection<TKey> IDictionary<TKey, TValue>.Keys => orderedKeys;
ICollection<TValue> IDictionary<TKey, TValue>.Values => orderedValues;
ulong? firstOrdinal =>
ordinalKeys.Count == 0 ? null : ordinalKeys[0].Ordinal;
ulong? lastOrdinal =>
ordinalKeys.Count == 0 ? null : ordinalKeys[^1].Ordinal;
TValue IDictionary<TKey, TValue>.this[TKey key] {
get => this[key];
set => throw new NotSupportedException();
}
KeyValuePair<TKey, TValue> IList<KeyValuePair<TKey, TValue>>.this[int index] {
get {
var k = ordinalKeys[index].Key;
return new(k, data[k].Value);
}
set => throw new NotSupportedException();
}
public IList<KeyValuePair<TKey, TValue>> AsList() =>
this;
public ImmutableOrderedDictionary<TKey, TValue> Add(TKey key, TValue value) {
if (Count == 0) {
return new(data.Add(key, (value, ORDINAL_SEED)), ordinalKeys.Add((ORDINAL_SEED, key)));
}
if (data.ContainsKey(key)) {
return this;
}
checkCanInsert();
var lastOrd = ordinalKeys[^1].Ordinal;
var step = getStep(ulong.MaxValue - lastOrd);
if (step == 0) {
return reset(AggregateList.Create(this, new KeyValuePair<TKey, TValue>[] { new(key, value) }));
} else {
return new(data.Add(key, (value, lastOrd + step)), ordinalKeys.Add((lastOrd + step, key)));
}
}
public ImmutableOrderedDictionary<TKey, TValue> Add(KeyValuePair<TKey, TValue> item) =>
Add(item.Key, item.Value);
public ImmutableOrderedDictionary<TKey, TValue> AddRange(IEnumerable<KeyValuePair<TKey, TValue>> items) {
if (items is null) {
return this;
}
var itemList = items.Where(kv => !data.ContainsKey(kv.Key))
.Distinct(KeyValuePairEqualityComparer.KeyComparer<TKey, TValue>(Comparer))
.ToList();
var itemCountUL = (ulong)itemList.Count;
if (itemCountUL == 0) {
return this;
}
if (Count == 0) {
return reset(itemList);
}
var lastOrd = ordinalKeys[^1].Ordinal;
var step = getStep(ulong.MaxValue - lastOrd, itemCountUL + 1);
if (step == 0) {
return reset(AggregateList.Create(this, itemList));
} else {
return new(
data.AddRange(IndexDependentSelectorList.Create(itemList, GetDatum)),
ordinalKeys.AddRange(IndexDependentSelectorList.Create(itemList, GetOrdinalKey)));
ulong GetOrdinal(int i) =>
lastOrd + (ulong)(i + 1) * step;
KeyValuePair<TKey, (TValue Value, ulong Ordinal)> GetDatum(KeyValuePair<TKey, TValue> kv, int i) =>
new(kv.Key, (kv.Value, GetOrdinal(i)));
(ulong Ordinal, TKey Key) GetOrdinalKey(KeyValuePair<TKey, TValue> kv, int i) =>
(GetOrdinal(i), kv.Key);
}
}
public ImmutableOrderedDictionary<TKey, TValue> InsertRange(int index, IEnumerable<KeyValuePair<TKey, TValue>> items) {
if (index == Count) {
return AddRange(items);
}
if (index < 0 || index > Count) {
throw new IndexOutOfRangeException();
}
var itemList = items.Where(kv => !data.ContainsKey(kv.Key))
.Distinct(KeyValuePairEqualityComparer.KeyComparer<TKey, TValue>(Comparer))
.ToList();
int itemCount = itemList.Count;
ulong itemCountUL = (ulong)itemCount;
if (itemCountUL == 0) {
return this;
}
var upperOrd = ordinalKeys[index].Ordinal;
ulong firstOrd;
ulong step;
if (index == 0) {
step = getStep(upperOrd, itemCountUL);
if (step >= (1 << 4)) {
step = getStep(upperOrd, itemCountUL + 1);
firstOrd = step;
} else {
firstOrd = 0;
}
} else {
var lowerOrd = ordinalKeys[index - 1].Ordinal;
// Compute step size for 'itemCountUL + 1' to account for the item at lowerOrd.
step = getStep(upperOrd - lowerOrd, itemCountUL + 1);
firstOrd = lowerOrd + step;
}
if (step == 0) {
return reset(
AggregateList.Create(
this.WrapSubRange(0, index),
itemList,
this.WrapSubRange(index, Count - index)));
}
var dataBd = data.ToBuilder();
var ordinalKeysBd = ordinalKeys.ToBuilder();
for (int i = 0; i < itemCount; i++) {
var (k, v) = itemList[i];
var itemOrd = firstOrd + step * (ulong)i;
ordinalKeysBd.Add((itemOrd, k));
dataBd.Add(k, (v, itemOrd));
}
return new(dataBd.ToImmutable(), ordinalKeysBd.ToImmutable());
}
public ImmutableOrderedDictionary<TKey, TValue> Clear() =>
new(data.Clear(), ordinalKeys.Clear());
public bool Contains(KeyValuePair<TKey, TValue> item) =>
data.TryGetValue(item.Key, out var v) && EqualityComparer<TValue>.Default.Equals(v.Value, item.Value);
public bool ContainsKey(TKey key) =>
data.ContainsKey(key);
public void CopyTo(KeyValuePair<TKey, TValue>[] array, int arrayIndex) {
foreach (var (ord, k) in ordinalKeys) {
array[arrayIndex++] = new(k, data[k].Value);
}
}
public int IndexOf(KeyValuePair<TKey, TValue> item) =>
data.TryGetValue(item.Key, out var v) && EqualityComparer<TValue>.Default.Equals(v.Value, item.Value)
? ordinalKeys.IndexOf((v.Ordinal, item.Key))
: -1;
public ImmutableOrderedDictionary<TKey, TValue> Insert(int index, KeyValuePair<TKey, TValue> item) =>
InsertRange(index, new[] { item });
public ImmutableOrderedDictionary<TKey, TValue> Remove(TKey key) =>
data.TryGetValue(key, out var v)
? new(data.Remove(key), ordinalKeys.Remove((v.Ordinal, key)))
: this;
public ImmutableOrderedDictionary<TKey, TValue> Remove(KeyValuePair<TKey, TValue> item) =>
data.TryGetValue(item.Key, out var v) && EqualityComparer<TValue>.Default.Equals(v.Value, item.Value)
? new(data.Remove(item.Key), ordinalKeys.Remove((v.Ordinal, item.Key)))
: this;
public ImmutableOrderedDictionary<TKey, TValue> RemoveAt(int index) {
var (ord, key) = ordinalKeys[index];
return new(data.Remove(key), ordinalKeys.Remove((ord, key)));
}
public ImmutableOrderedDictionary<TKey, TValue> SetItem(TKey key, TValue value) {
if (Count == 0) {
return new(data.Add(key, (value, MAX_ORDINAL_STEP)), ordinalKeys.Add((MAX_ORDINAL_STEP, key)));
} else if (data.TryGetValue(key, out var curr)) {
return new(data.SetItem(key, (value, curr.Ordinal)), ordinalKeys);
} else {
var itemOrd = getNextOrdinal(ordinalKeys[^1].Ordinal);
if (!itemOrd.HasValue) {
return Empty.AddRange(AggregateList.Create(AsList(), new KeyValuePair<TKey, TValue>[] { new(key, value) }));
}
return new(data.Add(key, (value, itemOrd.Value)), ordinalKeys.Add((itemOrd.Value, key)));
}
}
public ImmutableOrderedDictionary<TKey, TValue> SetItem(int index, KeyValuePair<TKey, TValue> item) {
var data = this.data;
var ordinalKeys = this.ordinalKeys;
var keyComp = data.KeyComparer;
var (currOrdinalAtIndex, currKeyAtIndex) = ordinalKeys[index];
if (data.ContainsKey(item.Key)) {
// If the key is present, it must already be at the specified index to avoid introducing
// a duplicate key.
if (!keyComp.Equals(item.Key, currKeyAtIndex)) {
throw new ArgumentException("The same key is already present at another index", nameof(item));
}
} else {
data = data.Remove(currKeyAtIndex);
ordinalKeys = ordinalKeys.Remove((currOrdinalAtIndex, currKeyAtIndex));
}
data = data.SetItem(item.Key, (item.Value, currOrdinalAtIndex));
return new(data, ordinalKeys);
}
public Builder ToBuilder() =>
new(data, ordinalKeys);
public bool TryGetValue(TKey key, [MaybeNullWhen(false)] out TValue value) {
if (data.TryGetValue(key, out var v)) {
value = v.Value;
return true;
}
value = default;
return false;
}
public IEnumerator<KeyValuePair<TKey, TValue>> GetEnumerator() {
for (int i = 0; i < orderedKeys.Count; i++) {
yield return new(orderedKeys[i], data[orderedKeys[i]].Value);
}
}
static ulong? getNextOrdinal(ulong lastOrd) {
if (lastOrd == ulong.MaxValue) {
return null;
}
ulong step = getStep(ulong.MaxValue - lastOrd);
return lastOrd + step;
}
static ulong getStep(ulong ordinalSpan) {
ulong step = ordinalSpan switch {
>= (1UL << 32) => 1 << 16,
>= (1UL << 30) => 1 << 15,
>= (1UL << 28) => 1 << 14,
>= (1UL << 26) => 1 << 13,
>= (1UL << 24) => 1 << 12,
>= (1UL << 22) => 1 << 11,
>= (1UL << 20) => 1 << 10,
>= (1UL << 18) => 1 << 9,
>= (1UL << 16) => 1 << 8,
>= (1UL << 14) => 1 << 7,
>= (1UL << 12) => 1 << 6,
>= (1UL << 10) => 1 << 5,
>= (1UL << 8) => 1 << 4,
>= (1UL << 6) => 1 << 3,
>= (1UL << 4) => 1 << 2,
>= (1UL << 2) => 1 << 1,
>= 1UL => 1,
0UL => 0,
};
return step;
}
static ulong getStep(ulong ordinalSpan, ulong numItems) {
if (numItems >= ordinalSpan) {
return 0;
}
ulong step = ordinalSpan / numItems;
step = Math.Min(step, MAX_ORDINAL_STEP);
return step;
}
IEnumerator IEnumerable.GetEnumerator() =>
GetEnumerator();
void IDictionary<TKey, TValue>.Add(TKey key, TValue value) =>
throw new NotSupportedException();
void ICollection<KeyValuePair<TKey, TValue>>.Add(KeyValuePair<TKey, TValue> item) =>
throw new NotSupportedException();
void ICollection<KeyValuePair<TKey, TValue>>.Clear() =>
throw new NotSupportedException();
void IList<KeyValuePair<TKey, TValue>>.Insert(int index, KeyValuePair<TKey, TValue> item) =>
throw new NotSupportedException();
bool ICollection<KeyValuePair<TKey, TValue>>.Remove(KeyValuePair<TKey, TValue> item) =>
throw new NotSupportedException();
bool IDictionary<TKey, TValue>.Remove(TKey key) =>
throw new NotSupportedException();
void IList<KeyValuePair<TKey, TValue>>.RemoveAt(int index) =>
throw new NotSupportedException();
void checkCanInsert() {
if (Count >= MAX_NUM_ITEMS) {
throw new InvalidOperationException("Add/Insert would exceed the maximum number of items");
}
}
/// WARNING: <param name="items" /> should already be unique (this fn does NOT check)
ImmutableOrderedDictionary<TKey, TValue> reset(IEnumerable<KeyValuePair<TKey, TValue>>? items) {
if (items is null) {
return Clear();
}
var itemList = items as IList<KeyValuePair<TKey, TValue>> ?? items.ToList();
var itemCountUL = (ulong)itemList.Count;
if (itemList.Count == 0) {
return Clear();
}
var step = getStep(ulong.MaxValue, itemCountUL + 1UL << 8);
Log.assert(step > 0);
var dataBd = data.Clear().ToBuilder();
var ordinalKeysBd = ordinalKeys.Clear().ToBuilder();
var ord = step * (1UL << 7);
foreach (var (k, v) in itemList) {
if (dataBd.ContainsKey(k)) {
continue;
}
dataBd[k] = (v, ord);
ordinalKeysBd.Add((ord, k));
ord += step;
}
return new(dataBd.ToImmutable(), ordinalKeysBd.ToImmutable());
}
public class Builder : IDictionary<TKey, TValue>, IList<KeyValuePair<TKey, TValue>> {
readonly ImmutableDictionary<TKey, (TValue Value, ulong Ordinal)>.Builder data;
readonly ImmutableSortedSet<(ulong Ordinal, TKey Key)>.Builder ordinalKeys;
readonly IList<TKey> orderedKeys;
readonly SelectorList<TKey, TValue> orderedValues;
public Builder(ImmutableDictionary<TKey, (TValue Value, ulong Ordinal)> data, ImmutableSortedSet<(ulong Ordinal, TKey Key)> ordinalKeys) {
this.data = data.ToBuilder();
this.ordinalKeys = ordinalKeys.ToBuilder();
orderedKeys = ordinalKeys.WrapSelector(p => p.Key, null);
orderedValues = new SelectorList<TKey, TValue>(orderedKeys, k => data[k].Value, null);
OrdinalGaps = DeltaList.Create(ordinalKeys.WrapSelector(p => p.Ordinal, null), (x, y) => checked(x - y));
}
public int Count => ordinalKeys.Count;
public bool IsReadOnly => false;
public TValue this[TKey key] => data[key].Value;
public IList<TKey> Keys => orderedKeys;
public IList<TValue> Values => orderedValues;
public IList<ulong> OrdinalGaps { get; }
ICollection<TKey> IDictionary<TKey, TValue>.Keys => orderedKeys;
ICollection<TValue> IDictionary<TKey, TValue>.Values => orderedValues;
ulong? firstOrdinal =>
ordinalKeys.Count == 0 ? null : ordinalKeys[0].Ordinal;
ulong? lastOrdinal =>
ordinalKeys.Count == 0 ? null : ordinalKeys[^1].Ordinal;
TValue IDictionary<TKey, TValue>.this[TKey key] {
get => this[key];
set => throw new NotSupportedException();
}
KeyValuePair<TKey, TValue> IList<KeyValuePair<TKey, TValue>>.this[int index] {
get {
var k = ordinalKeys[index].Key;
return new(k, data[k].Value);
}
set => throw new NotSupportedException();
}
public void Add(TKey key, TValue value) {
if (Count == 0) {
var ord = ORDINAL_SEED;
data.Add(key, (value, ord));
ordinalKeys.Add((ord, key));
return;
}
checkCanInsert();
var lastOrd = ordinalKeys[^1].Ordinal;
var step = getStep(ulong.MaxValue - lastOrd);
if (step == 0) {
Reset(AggregateList.Create(this, new KeyValuePair<TKey, TValue>[] { new(key, value) }));
} else {
data.Add(key, (value, lastOrd + step));
ordinalKeys.Add((lastOrd + step, key));
}
}
public void Add(KeyValuePair<TKey, TValue> item) =>
Add(item.Key, item.Value);
public void AddRange(IEnumerable<KeyValuePair<TKey, TValue>> items) {
if (items is null) {
return;
}
var itemList = items as IList<KeyValuePair<TKey, TValue>> ?? items.ToList();
var itemCountUL = (ulong)itemList.Count;
if (itemCountUL == 0) {
return;
}
if (Count == 0) {
Reset(itemList);
return;
}
var lastOrd = ordinalKeys[^1].Ordinal;
var step = getStep(ulong.MaxValue - lastOrd, itemCountUL + 1);
if (step == 0) {
Reset(AggregateList.Create(this, itemList));
return;
} else {
data.AddRange(IndexDependentSelectorList.Create(itemList, GetDatum));
ordinalKeys.AddRange(IndexDependentSelectorList.Create(itemList, GetOrdinalKey));
ulong GetOrdinal(int i) =>
lastOrd + (ulong)(i + 1) * step;
KeyValuePair<TKey, (TValue Value, ulong Ordinal)> GetDatum(KeyValuePair<TKey, TValue> kv, int i) =>
new(kv.Key, (kv.Value, GetOrdinal(i)));
(ulong Ordinal, TKey Key) GetOrdinalKey(KeyValuePair<TKey, TValue> kv, int i) =>
(GetOrdinal(i), kv.Key);
}
}
public void InsertRange(int index, IEnumerable<KeyValuePair<TKey, TValue>> items) {
if (index == Count) {
AddRange(items);
return;
}
if (index < 0 || index > Count) {
throw new IndexOutOfRangeException();
}
var itemList = items as IList<KeyValuePair<TKey, TValue>> ?? items.ToList();
int itemCount = itemList.Count;
ulong itemCountUL = (ulong)itemCount;
if (itemCountUL == 0) {
return;
}
var upperOrd = ordinalKeys[index].Ordinal;
ulong firstOrd;
ulong step;
if (index == 0) {
step = getStep(upperOrd, itemCountUL);
if (step >= (1 << 4)) {
step = getStep(upperOrd, itemCountUL + 1);
firstOrd = step;
} else {
firstOrd = 0;
}
} else {
var lowerOrd = ordinalKeys[index - 1].Ordinal;
// Compute step size for 'itemCountUL + 1' to account for the item at lowerOrd.
step = getStep(upperOrd - lowerOrd, itemCountUL + 1);
firstOrd = lowerOrd + step;
}
if (step == 0) {
Reset(AggregateList.Create(
this.WrapSubRange(0, index),
itemList,
this.WrapSubRange(index, Count - index)));
return;
}
for (int i = 0; i < itemCount; i++) {
var (k, v) = itemList[i];
var itemOrd = firstOrd + step * (ulong)i;
ordinalKeys.Add((itemOrd, k));
data.Add(k, (v, itemOrd));
}
}
public void Clear() {
data.Clear();
ordinalKeys.Clear();
}
public bool Contains(KeyValuePair<TKey, TValue> item) =>
data.TryGetValue(item.Key, out var v) && EqualityComparer<TValue>.Default.Equals(v.Value, item.Value);
public bool ContainsKey(TKey key) =>
data.ContainsKey(key);
public void CopyTo(KeyValuePair<TKey, TValue>[] array, int arrayIndex) {
foreach (var (ord, k) in ordinalKeys) {
array[arrayIndex++] = new(k, data[k].Value);
}
}
public int IndexOf(KeyValuePair<TKey, TValue> item) =>
data.TryGetValue(item.Key, out var v) && EqualityComparer<TValue>.Default.Equals(v.Value, item.Value)
? ordinalKeys.IndexOf((v.Ordinal, item.Key))
: -1;
public void Insert(int index, KeyValuePair<TKey, TValue> item) {
InsertRange(index, new[] { item });
}
public bool Remove(TKey key) {
if (data.TryGetValue(key, out var v)) {
if (!data.Remove(key)) {
throw new InvalidOperationException("Failed to remove key from data");
}
if (!ordinalKeys.Remove((v.Ordinal, key))) {
throw new InvalidOperationException("Failed to remove key from ordinalKeys");
}
return true;
}
return false;
}
public bool Remove(KeyValuePair<TKey, TValue> item) {
if (data.TryGetValue(item.Key, out var v) && EqualityComparer<TValue>.Default.Equals(v.Value, item.Value)) {
if (!data.Remove(item.Key)) {
throw new InvalidOperationException("Remove failed");
}
if (!ordinalKeys.Remove((v.Ordinal, item.Key))) {
throw new InvalidOperationException("Failed to remove key from ordinalKeys");
}
return true;
}
return false;
}
public void RemoveAt(int index) {
var (ord, key) = ordinalKeys[index];
data.Remove(key);
ordinalKeys.Remove((ord, key));
}
public void Reset(IEnumerable<KeyValuePair<TKey, TValue>>? items) {
if (items is null) {
Clear();
return;
}
var itemList = items as IList<KeyValuePair<TKey, TValue>> ?? items.ToList();
var itemCountUL = (ulong)itemList.Count;
if (itemList.Count == 0) {
Clear();
return;
}
var step = getStep(ulong.MaxValue, itemCountUL + 1UL << 8);
Log.assert(step > 0);
data.Clear();
ordinalKeys.Clear();
var ord = step * 1UL << 7;
foreach (var (k, v) in itemList) {
data[k] = (v, ord);
ordinalKeys.Add((ord, k));
ord += step;
}
}
public ImmutableOrderedDictionary<TKey, TValue> ToImmutable() =>
new(data.ToImmutable(), ordinalKeys.ToImmutable());
public bool TryGetValue(TKey key, [MaybeNullWhen(false)] out TValue value) {
if (data.TryGetValue(key, out var v)) {
value = v.Value;
return true;
}
value = default;
return false;
}
public IEnumerator<KeyValuePair<TKey, TValue>> GetEnumerator() {
foreach (var (_, k) in ordinalKeys) {
yield return new(k, data[k].Value);
}
}
IEnumerator IEnumerable.GetEnumerator() =>
GetEnumerator();
void checkCanInsert() {
if (Count >= MAX_NUM_ITEMS) {
throw new InvalidOperationException("Add/Insert would exceed the maximum number of items");
}
}
}
}
@@ -0,0 +1,647 @@
#nullable enable
namespace Spark.Core.Collections;
using bts;
using bts.collections;
using System;
using System.Collections;
using System.Collections.Generic;
using System.Collections.Immutable;
using System.Diagnostics.CodeAnalysis;
using System.Linq;
public static class ImmutableOrderedSet {
public static ImmutableOrderedSet<T> Create<T>(IEqualityComparer<T>? comparer = null) where T : notnull =>
new(comparer: comparer);
public static ImmutableOrderedSet<T> CreateRange<T>(IEnumerable<T> items) where T : notnull =>
new ImmutableOrderedSet<T>().AddRange(items);
public static ImmutableOrderedSet<T> CreateRange<T>(IEqualityComparer<T>? comparer, IEnumerable<T> items) where T : notnull =>
new ImmutableOrderedSet<T>(comparer).AddRange(items);
}
public class ImmutableOrderedSet<T> : IReadOnlySet<T>, IList<T> where T : notnull {
public static readonly ImmutableOrderedSet<T> Empty = new();
// These represent the min/max gap size between consecutive ordinals when the collection is
// balanced. Gaps (greater than 1) are there so that inserts can be performed without needing to
// update the ordinals of every item after the target index. The collection will gradually become
// unbalanced as mutations cause the gaps between ordinals to shrink. It is rebalanced when:
// A) an insert is attempted between 2 neighbors with a gap of 1, or
// B) an add is attempted when the last ordinal is ulong.MaxValue.
//
// Thus, the min/max ordinal steps have been tuned (crudely) to minimize rebalances. Here's the
// basic school of thought:
// A value of 1 is most optimial for adds, as it maximizes the number of elements that can be
// appended to the end of the list before the key ordinals must be rebalanced. However, a value
// of 1 is also the LEAST optimal for inserts, as it leaves no "ordinal space" between items,
// meaning the key ordinals would need to be rebalanced on each insert.
const ulong MAX_ORDINAL_STEP = 1 << 16;
const ulong ORDINAL_SEED = 1 << 12;
const int MAX_NUM_ITEMS = int.MaxValue;
readonly ImmutableDictionary<T, ulong> data;
readonly ImmutableSortedSet<(ulong Ordinal, T Key)> ordinalKeys;
readonly IList<T> orderedKeys;
readonly DictionarySetWrapper<T, ulong> keySet;
public ImmutableOrderedSet(IEqualityComparer<T>? comparer = null)
: this(ImmutableDictionary.Create<T, ulong>(comparer), ImmutableSortedSet.Create<(ulong, T)>(TupleComparer.Comparer<ulong, T>(0, null))) { }
ImmutableOrderedSet(ImmutableDictionary<T, ulong> data, ImmutableSortedSet<(ulong Ordinal, T Key)> ordinalKeys) {
this.data = data;
this.ordinalKeys = ordinalKeys;
orderedKeys = ordinalKeys.WrapSelector(p => p.Key, null);
keySet = new(data, data.KeyComparer);
OrdinalGaps = DeltaList.Create(ordinalKeys.WrapSelector(p => p.Ordinal, null), (x, y) => checked(x - y));
}
public IEqualityComparer<T> Comparer => data.KeyComparer;
public IList<T> Keys => orderedKeys;
public int Count => ordinalKeys.Count;
public bool IsReadOnly => false;
public IList<ulong> OrdinalGaps { get; }
ulong? firstOrdinal =>
ordinalKeys.Count == 0 ? null : ordinalKeys[0].Ordinal;
ulong? lastOrdinal =>
ordinalKeys.Count == 0 ? null : ordinalKeys[^1].Ordinal;
public T this[int index] {
get => ordinalKeys[index].Key;
set => throw new NotSupportedException();
}
public IList<T> AsList() =>
this;
public ImmutableOrderedSet<T> Add(T key) {
if (Count == 0) {
return new(data.Add(key, ORDINAL_SEED), ordinalKeys.Add((ORDINAL_SEED, key)));
}
checkCanInsert();
var lastOrd = ordinalKeys[^1].Ordinal;
var step = getStep(ulong.MaxValue - lastOrd);
if (step == 0) {
return reset(AggregateList.Create(this, new T[] { key }));
} else {
return new(data.Add(key, lastOrd + step), ordinalKeys.Add((lastOrd + step, key)));
}
}
public ImmutableOrderedSet<T> AddRange(IEnumerable<T> items) {
if (items is null) {
return this;
}
var itemList = items as IList<T> ?? items.ToList();
var itemCountUL = (ulong)itemList.Count;
if (itemCountUL == 0) {
return this;
}
if (Count == 0) {
return reset(itemList);
}
var lastOrd = ordinalKeys[^1].Ordinal;
var step = getStep(ulong.MaxValue - lastOrd, itemCountUL + 1);
if (step == 0) {
return reset(AggregateList.Create(this, itemList));
} else {
return new(
data.AddRange(IndexDependentSelectorList.Create(itemList, GetDatum)),
ordinalKeys.AddRange(IndexDependentSelectorList.Create(itemList, GetOrdinalKey)));
ulong GetOrdinal(int i) =>
lastOrd + (ulong)(i + 1) * step;
KeyValuePair<T, ulong> GetDatum(T item, int i) =>
new(item, GetOrdinal(i));
(ulong Ordinal, T Key) GetOrdinalKey(T item, int i) =>
(GetOrdinal(i), item);
}
}
public ImmutableOrderedSet<T> Clear() =>
new(data.Clear(), ordinalKeys.Clear());
public bool Contains(T item) =>
data.ContainsKey(item);
public void CopyTo(T[] array, int arrayIndex) {
foreach (var (_, k) in ordinalKeys) {
array[arrayIndex++] = k;
}
}
public int IndexOf(T item) =>
data.TryGetValue(item, out var ord)
? ordinalKeys.IndexOf((ord, item))
: -1;
public ImmutableOrderedSet<T> Insert(int index, T item) =>
InsertRange(index, new[] { item });
public ImmutableOrderedSet<T> InsertRange(int index, IEnumerable<T> items) {
if (index == Count) {
return AddRange(items);
}
if (index < 0 || index > Count) {
throw new IndexOutOfRangeException();
}
var itemList = items.WhereNot(data.ContainsKey)
.Distinct(Comparer)
.ToList();
int itemCount = itemList.Count;
ulong itemCountUL = (ulong)itemCount;
if (itemCountUL == 0) {
return this;
}
var upperOrd = ordinalKeys[index].Ordinal;
ulong firstOrd;
ulong step;
if (index == 0) {
step = getStep(upperOrd, itemCountUL);
if (step >= (1 << 4)) {
step = getStep(upperOrd, itemCountUL + 1);
firstOrd = step;
} else {
firstOrd = 0;
}
} else {
var lowerOrd = ordinalKeys[index - 1].Ordinal;
// Compute step size for 'itemCountUL + 1' to account for the item at lowerOrd.
step = getStep(upperOrd - lowerOrd, itemCountUL + 1);
firstOrd = lowerOrd + step;
}
if (step == 0) {
return reset(
AggregateList.Create(
this.WrapSubRange(0, index),
itemList,
this.WrapSubRange(index, Count - index)));
}
var dataBd = data.ToBuilder();
var ordinalKeysBd = ordinalKeys.ToBuilder();
for (int i = 0; i < itemCount; i++) {
var k = itemList[i];
var itemOrd = firstOrd + step * (ulong)i;
ordinalKeysBd.Add((itemOrd, k));
dataBd.Add(k, itemOrd);
}
return new(dataBd.ToImmutable(), ordinalKeysBd.ToImmutable());
}
public bool IsProperSubsetOf(IEnumerable<T> other) =>
keySet.IsProperSubsetOf(other);
public bool IsProperSupersetOf(IEnumerable<T> other) =>
keySet.IsProperSupersetOf(other);
/// <summary>Determines whether a <see cref="HashSet{T}"/> object is a subset of the specified collection.</summary>
/// <param name="other">The collection to compare to the current <see cref="HashSet{T}"/> object.</param>
/// <returns>true if the <see cref="HashSet{T}"/> object is a subset of <paramref name="other"/>; otherwise, false.</returns>
public bool IsSubsetOf(IEnumerable<T> other) =>
keySet.IsSubsetOf(other);
public bool IsSupersetOf(IEnumerable<T> other) =>
keySet.IsSupersetOf(other);
public bool Overlaps(IEnumerable<T> other) =>
keySet.Overlaps(other);
public ImmutableOrderedSet<T> Remove(T key) =>
data.TryGetValue(key, out var ord)
? new(data.Remove(key), ordinalKeys.Remove((ord, key)))
: this;
public ImmutableOrderedSet<T> RemoveAt(int index) {
var (ord, key) = ordinalKeys[index];
return new(data.Remove(key), ordinalKeys.Remove((ord, key)));
}
public bool SetEquals(IEnumerable<T> other) =>
keySet.SetEquals(other);
public ImmutableOrderedSet<T> SetItem(int index, T item) {
var data = this.data;
var ordinalKeys = this.ordinalKeys;
var (currOrdinalAtIndex, currKeyAtIndex) = ordinalKeys[index];
if (data.ContainsKey(item)) {
// If the key is present, it must already be at the specified index to avoid introducing
// a duplicate key.
if (!Comparer.Equals(item, currKeyAtIndex)) {
throw new ArgumentException("The same key is already present at another index", nameof(item));
}
} else {
data = data.Remove(currKeyAtIndex);
ordinalKeys = ordinalKeys.Remove((currOrdinalAtIndex, currKeyAtIndex));
}
data = data.SetItem(item, currOrdinalAtIndex);
return new(data, ordinalKeys);
}
public Builder ToBuilder() =>
new(data, ordinalKeys);
public bool TryGetValue(T equalValue, [MaybeNullWhen(false)] out T actualValue) {
if (data.TryGetValue(equalValue, out var ord)) {
if (!ordinalKeys.TryGetValue((ord, equalValue), out var res)) {
throw new InvalidOperationException("Lookup failed");
}
actualValue = res.Key;
return true;
}
actualValue = default;
return false;
}
public IEnumerator<T> GetEnumerator() =>
orderedKeys.GetEnumerator();
static ulong getStep(ulong ordinalSpan) {
ulong step = ordinalSpan switch {
>= (1UL << 32) => 1 << 16,
>= (1UL << 30) => 1 << 15,
>= (1UL << 28) => 1 << 14,
>= (1UL << 26) => 1 << 13,
>= (1UL << 24) => 1 << 12,
>= (1UL << 22) => 1 << 11,
>= (1UL << 20) => 1 << 10,
>= (1UL << 18) => 1 << 9,
>= (1UL << 16) => 1 << 8,
>= (1UL << 14) => 1 << 7,
>= (1UL << 12) => 1 << 6,
>= (1UL << 10) => 1 << 5,
>= (1UL << 8) => 1 << 4,
>= (1UL << 6) => 1 << 3,
>= (1UL << 4) => 1 << 2,
>= (1UL << 2) => 1 << 1,
>= 1UL => 1,
0UL => 0,
};
return step;
}
static ulong getStep(ulong ordinalSpan, ulong numItems) {
if (numItems >= ordinalSpan) {
return 0;
}
ulong step = ordinalSpan / numItems;
step = Math.Min(step, MAX_ORDINAL_STEP);
return step;
}
IEnumerator IEnumerable.GetEnumerator() =>
GetEnumerator();
void ICollection<T>.Add(T item) =>
throw new NotSupportedException();
void ICollection<T>.Clear() =>
throw new NotSupportedException();
void IList<T>.Insert(int index, T item) =>
throw new NotSupportedException();
bool ICollection<T>.Remove(T item) =>
throw new NotSupportedException();
void IList<T>.RemoveAt(int index) =>
throw new NotSupportedException();
void checkCanInsert() {
if (Count >= MAX_NUM_ITEMS) {
throw new InvalidOperationException("Add/Insert would exceed the maximum number of items");
}
}
/// WARNING: <param name="items" /> should already be distinct (this fn does NOT check)
ImmutableOrderedSet<T> reset(IEnumerable<T>? items) {
if (items is null) {
return Clear();
}
var itemList = items as IList<T> ?? items.ToList();
var itemCountUL = (ulong)itemList.Count;
if (itemList.Count == 0) {
return Clear();
}
var step = getStep(ulong.MaxValue, itemCountUL + 1UL << 8);
Log.assert(step > 0);
var dataBd = data.Clear().ToBuilder();
var ordinalKeysBd = ordinalKeys.Clear().ToBuilder();
var ord = step * 1UL << 7;
foreach (var k in itemList) {
if (dataBd.ContainsKey(k)) {
continue;
}
dataBd[k] = ord;
ordinalKeysBd.Add((ord, k));
ord += step;
}
return new(dataBd.ToImmutable(), ordinalKeysBd.ToImmutable());
}
public class Builder : ISet<T>, IReadOnlySet<T>, IList<T> {
readonly ImmutableDictionary<T, ulong>.Builder data;
readonly ImmutableSortedSet<(ulong Ordinal, T Key)>.Builder ordinalKeys;
readonly IList<T> orderedKeys;
readonly DictionarySetWrapper<T, ulong> keySet;
public Builder(ImmutableDictionary<T, ulong> data, ImmutableSortedSet<(ulong Ordinal, T Key)> ordinalKeys) {
this.data = data.ToBuilder();
this.ordinalKeys = ordinalKeys.ToBuilder();
orderedKeys = ordinalKeys.WrapSelector(p => p.Key, null);
keySet = new(data, data.KeyComparer);
OrdinalGaps = DeltaList.Create(ordinalKeys.WrapSelector(p => p.Ordinal, null), (x, y) => checked(x - y));
}
public IEqualityComparer<T> Comparer => data.KeyComparer;
public int Count => ordinalKeys.Count;
public bool IsReadOnly => false;
public IList<T> Keys => orderedKeys;
public IList<ulong> OrdinalGaps { get; }
ulong? firstOrdinal =>
ordinalKeys.Count == 0 ? null : ordinalKeys[0].Ordinal;
ulong? lastOrdinal =>
ordinalKeys.Count == 0 ? null : ordinalKeys[^1].Ordinal;
T IList<T>.this[int index] {
get => ordinalKeys[index].Key;
set => throw new NotSupportedException();
}
public bool Add(T key) {
if (data.ContainsKey(key)) {
return false;
}
if (Count == 0) {
var ord = ORDINAL_SEED;
data.Add(key, ord);
ordinalKeys.Add((ord, key));
return true;
}
checkCanInsert();
var lastOrd = ordinalKeys[^1].Ordinal;
var step = getStep(ulong.MaxValue - lastOrd);
if (step == 0) {
reset(AggregateList.Create(this, new T[] { key }));
} else {
data.Add(key, lastOrd + step);
ordinalKeys.Add((lastOrd + step, key));
}
return true;
}
public void AddRange(IEnumerable<T> items) {
if (items is null) {
return;
}
var itemList = items.WhereNot(data.ContainsKey)
.Distinct(Comparer)
.ToList();
var itemCountUL = (ulong)itemList.Count;
if (itemCountUL == 0) {
return;
}
if (Count == 0) {
reset(itemList);
return;
}
var lastOrd = ordinalKeys[^1].Ordinal;
var step = getStep(ulong.MaxValue - lastOrd, itemCountUL + 1);
if (step == 0) {
reset(AggregateList.Create(this, itemList));
return;
} else {
data.AddRange(IndexDependentSelectorList.Create(itemList, GetDatum));
ordinalKeys.AddRange(IndexDependentSelectorList.Create(itemList, GetOrdinalKey));
ulong GetOrdinal(int i) =>
lastOrd + (ulong)(i + 1) * step;
KeyValuePair<T, ulong> GetDatum(T item, int i) =>
new(item, GetOrdinal(i));
(ulong Ordinal, T Key) GetOrdinalKey(T item, int i) =>
(GetOrdinal(i), item);
}
}
public IList<T> AsList() =>
this;
public void InsertRange(int index, IEnumerable<T> items) {
if (index == Count) {
AddRange(items);
return;
}
if (index < 0 || index > Count) {
throw new IndexOutOfRangeException();
}
var itemList = items as IList<T> ?? items.ToList();
int itemCount = itemList.Count;
ulong itemCountUL = (ulong)itemCount;
if (itemCountUL == 0) {
return;
}
var upperOrd = ordinalKeys[index].Ordinal;
ulong firstOrd;
ulong step;
if (index == 0) {
step = getStep(upperOrd, itemCountUL);
if (step >= (1 << 4)) {
step = getStep(upperOrd, itemCountUL + 1);
firstOrd = step;
} else {
firstOrd = 0;
}
} else {
var lowerOrd = ordinalKeys[index - 1].Ordinal;
// Compute step size for 'itemCountUL + 1' to account for the item at lowerOrd.
step = getStep(upperOrd - lowerOrd, itemCountUL + 1);
firstOrd = lowerOrd + step;
}
if (step == 0) {
reset(AggregateList.Create(
this.WrapSubRange(0, index),
itemList,
this.WrapSubRange(index, Count - index)));
return;
}
for (int i = 0; i < itemCount; i++) {
var k = itemList[i];
var itemOrd = firstOrd + step * (ulong)i;
ordinalKeys.Add((itemOrd, k));
data.Add(k, itemOrd);
}
}
public void Clear() {
data.Clear();
ordinalKeys.Clear();
}
public bool Contains(T key) =>
data.ContainsKey(key);
public void CopyTo(T[] array, int arrayIndex) {
foreach (var (_, k) in ordinalKeys) {
array[arrayIndex++] = k;
}
}
public void ExceptWith(IEnumerable<T> other) {
foreach (var v in other) {
if (data.TryGetValue(v, out var ord)) {
if (!data.Remove(v)) {
throw new InvalidOperationException("Failed to remove item from data");
}
if (!ordinalKeys.Remove((ord, v))) {
throw new InvalidOperationException("Failed to remove item from ordinalKeys");
}
}
}
}
public int IndexOf(T item) {
if (data.TryGetValue(item, out var ord)) {
int idx = ordinalKeys.IndexOf((ord, item));
if (idx < 0 || idx > ordinalKeys.Count) {
throw new InvalidOperationException("IndexOf failed");
}
return idx;
}
return -1;
}
public void Insert(int index, T item) =>
InsertRange(index, new[] { item });
public void IntersectWith(IEnumerable<T> other) {
if (Count > 0) {
reset(other.Where(data.ContainsKey).Distinct(Comparer).ToList());
}
}
public bool IsProperSubsetOf(IEnumerable<T> other) =>
keySet.IsProperSubsetOf(other);
public bool IsProperSupersetOf(IEnumerable<T> other) =>
keySet.IsProperSupersetOf(other);
/// <summary>Determines whether a <see cref="HashSet{T}"/> object is a subset of the specified collection.</summary>
/// <param name="other">The collection to compare to the current <see cref="HashSet{T}"/> object.</param>
/// <returns>true if the <see cref="HashSet{T}"/> object is a subset of <paramref name="other"/>; otherwise, false.</returns>
public bool IsSubsetOf(IEnumerable<T> other) =>
keySet.IsSubsetOf(other);
public bool IsSupersetOf(IEnumerable<T> other) =>
keySet.IsSupersetOf(other);
public bool Overlaps(IEnumerable<T> other) =>
keySet.Overlaps(other);
public bool Remove(T item) {
if (!data.TryGetValue(item, out var v)) {
return false;
}
if (!data.Remove(item)) {
throw new InvalidOperationException("Failed to remove item from data");
}
if (!ordinalKeys.Remove((v, item))) {
throw new InvalidOperationException("Failed to remove item from ordinalKeys");
}
return true;
}
public void RemoveAt(int index) {
var (ord, key) = ordinalKeys[index];
data.Remove(key);
ordinalKeys.Remove((ord, key));
}
public bool SetEquals(IEnumerable<T> other) =>
keySet.SetEquals(other);
public void SymmetricExceptWith(IEnumerable<T> other) {
if (Count == 0) {
reset(other.Distinct(Comparer).ToList());
return;
}
foreach (var v in other) {
if (!Remove(v)) {
Add(v);
}
}
}
public ImmutableOrderedSet<T> ToImmutable() =>
new(data.ToImmutable(), ordinalKeys.ToImmutable());
public bool TryGetValue(T equalValue, [MaybeNullWhen(false)] out T actualValue) {
if (data.TryGetValue(equalValue, out var ord)) {
if (!ordinalKeys.TryGetValue((ord, equalValue), out var res)) {
throw new InvalidOperationException("Lookup failed");
}
actualValue = res.Key;
return true;
}
actualValue = default;
return false;
}
public void UnionWith(IEnumerable<T> other) =>
AddRange(other);
public IEnumerator<T> GetEnumerator() =>
orderedKeys.GetEnumerator();
void ICollection<T>.Add(T item) =>
Add(item);
IEnumerator IEnumerable.GetEnumerator() =>
GetEnumerator();
void checkCanInsert() {
if (Count >= MAX_NUM_ITEMS) {
throw new InvalidOperationException("Add/Insert would exceed the maximum number of items");
}
}
/// WARNING: <param name="items" /> should already be distinct (this fn does NOT check)
void reset(IEnumerable<T>? items) {
if (items is null) {
Clear();
return;
}
var itemList = items as IList<T> ?? items.ToList();
var itemCountUL = (ulong)itemList.Count;
if (itemList.Count == 0) {
Clear();
return;
}
var step = getStep(ulong.MaxValue, itemCountUL + 1UL << 8);
Log.assert(step > 0);
data.Clear();
ordinalKeys.Clear();
var ord = step * 1UL << 7;
foreach (var k in itemList) {
data[k] = ord;
ordinalKeys.Add((ord, k));
ord += step;
}
}
}
}
@@ -0,0 +1,32 @@
#nullable enable
namespace Spark.Core.Collections;
using System;
using System.Collections.Generic;
public static class IndexDependentSelectorList {
public static IndexDependentSelectorList<T, TResult> Create<T, TResult>(IList<T> source, Func<T, int, TResult> selector) =>
new(source, selector);
}
public class IndexDependentSelectorList<T, TResult> : IMutableListFacade<TResult> {
readonly IList<T> source;
readonly Func<T, int, TResult> selector;
public IndexDependentSelectorList(IList<T> source, Func<T, int, TResult> selector) {
this.source = source;
this.selector = selector;
}
public TResult this[int index] => selector(source[index], index);
public int Count => source.Count;
public IEnumerator<TResult> GetEnumerator() {
int i = 0;
foreach (var v in source) {
yield return selector(v, i);
i++;
}
}
}
@@ -0,0 +1,36 @@
namespace Spark.Core.Collections;
using System.Collections.Generic;
public static class KeyValuePairComparer {
public static IComparer<KeyValuePair<TKey, TValue>> KeyComparer<TKey, TValue>() =>
KeyComparerImpl<TKey, TValue>.Instance;
public static IComparer<KeyValuePair<TKey, TValue>> KeyComparer<TKey, TValue>(IComparer<TKey> comparer) =>
new KeyComparerImpl<TKey, TValue>(comparer);
public static IComparer<KeyValuePair<TKey, TValue>> ValueComparer<TKey, TValue>() =>
ValueComparerImpl<TKey, TValue>.Instance;
class KeyComparerImpl<TKey, TValue> : IComparer<KeyValuePair<TKey, TValue>> {
public static readonly IComparer<KeyValuePair<TKey, TValue>> Instance = new KeyComparerImpl<TKey, TValue>();
readonly IComparer<TKey> comparer;
public KeyComparerImpl()
: this(Comparer<TKey>.Default) { }
public KeyComparerImpl(IComparer<TKey> comparer) =>
this.comparer = comparer;
public int Compare(KeyValuePair<TKey, TValue> x, KeyValuePair<TKey, TValue> y) =>
comparer.Compare(x.Key, y.Key);
}
class ValueComparerImpl<TKey, TValue> : IComparer<KeyValuePair<TKey, TValue>> {
public static readonly IComparer<KeyValuePair<TKey, TValue>> Instance = new KeyComparerImpl<TKey, TValue>();
public int Compare(KeyValuePair<TKey, TValue> x, KeyValuePair<TKey, TValue> y) =>
Comparer<TValue>.Default.Compare(x.Value, y.Value);
}
}
@@ -0,0 +1,52 @@
#nullable enable
namespace Spark.Core.Collections;
using System.Collections.Generic;
using System.Diagnostics.CodeAnalysis;
public static class KeyValuePairEqualityComparer {
public static IEqualityComparer<KeyValuePair<TKey, TValue>> KeyComparer<TKey, TValue>() =>
KeyComparerImpl<TKey, TValue>.Instance;
public static IEqualityComparer<KeyValuePair<TKey, TValue>> KeyComparer<TKey, TValue>(IEqualityComparer<TKey>? comparer) =>
new KeyComparerImpl<TKey, TValue>(comparer);
public static IEqualityComparer<KeyValuePair<TKey, TValue>> ValueComparer<TKey, TValue>() =>
ValueComparerImpl<TKey, TValue>.Instance;
class KeyComparerImpl<TKey, TValue> : IEqualityComparer<KeyValuePair<TKey, TValue>> {
public static readonly IEqualityComparer<KeyValuePair<TKey, TValue>> Instance = new KeyComparerImpl<TKey, TValue>();
readonly IEqualityComparer<TKey> comparer;
public KeyComparerImpl()
: this(EqualityComparer<TKey>.Default) { }
public KeyComparerImpl(IEqualityComparer<TKey>? comparer) =>
this.comparer = comparer ?? EqualityComparer<TKey>.Default;
public bool Equals(KeyValuePair<TKey, TValue> x, KeyValuePair<TKey, TValue> y) =>
comparer.Equals(x.Key, y.Key);
public int GetHashCode([DisallowNull] KeyValuePair<TKey, TValue> obj) =>
comparer.GetHashCode(obj.Key!);
}
class ValueComparerImpl<TKey, TValue> : IEqualityComparer<KeyValuePair<TKey, TValue>> {
public static readonly IEqualityComparer<KeyValuePair<TKey, TValue>> Instance = new KeyComparerImpl<TKey, TValue>();
readonly IEqualityComparer<TValue> comparer;
public ValueComparerImpl()
: this(EqualityComparer<TValue>.Default) { }
public ValueComparerImpl(IEqualityComparer<TValue>? comparer) =>
this.comparer = comparer ?? EqualityComparer<TValue>.Default;
public bool Equals(KeyValuePair<TKey, TValue> x, KeyValuePair<TKey, TValue> y) =>
comparer.Equals(x.Value, y.Value);
public int GetHashCode([DisallowNull] KeyValuePair<TKey, TValue> obj) =>
comparer.GetHashCode(obj.Value!);
}
}
@@ -0,0 +1,72 @@
namespace Spark.Core.Collections;
using bts.utils;
using System;
using System.Collections;
using System.Collections.Generic;
public static class MergedSortedList {
public static MergedSortedList<T> Create<T>(IList<T> list1, IList<T> list2) =>
new(list1, list2, Comparer<T>.Default);
public static MergedSortedList<T> Create<T>(IList<T> list1, IList<T> list2, IComparer<T> comparer) =>
new(list1, list2, comparer);
}
public class MergedSortedList<T> : IMutableListFacade<T> {
readonly IList<T> list1;
readonly IList<T> list2;
readonly IList<T>[] lists;
readonly IComparer<T> comparer;
public MergedSortedList(IList<T> list1, IList<T> list2, IComparer<T> comparer) {
this.list1 = list1;
this.list2 = list2;
lists = new[] { list1, list2 };
this.comparer = comparer;
}
public T this[int index] {
get {
var (listNum, listIndex) = CollectionHelper.BinarySearchMergedIndex(list1, index, list2, comparer);
return lists[listNum][listIndex];
}
}
public int Count => list1.Count + list2.Count;
public IEnumerator<T> GetEnumerator() {
int i1 = 0;
int i2 = 0;
bool didYield = false;
while (i1 < list1.Count && i2 < list2.Count) {
while (i1 < list1.Count && comparer.Compare(list1[i1], list2[i2]) < 1) {
yield return list1[i1++];
didYield = true;
}
if (i1 >= list1.Count) {
break;
}
while (i2 < list2.Count && comparer.Compare(list2[i2], list1[i1]) < 0) {
yield return list2[i2++];
didYield = true;
}
if (i2 >= list2.Count) {
break;
}
if (!didYield) {
throw new InvalidOperationException("Infinite loop detected");
}
}
while (i1 < list1.Count) {
yield return list1[i1++];
}
while (i2 < list2.Count) {
yield return list2[i2++];
}
}
IEnumerator IEnumerable.GetEnumerator() =>
GetEnumerator();
}
@@ -0,0 +1,396 @@
#nullable enable
namespace Spark.Core.Collections;
using bts;
using bts.collections;
using System;
using System.Collections;
using System.Collections.Generic;
using System.Diagnostics.CodeAnalysis;
using System.Linq;
public class OrderedDictionary<TKey, TValue> : IDictionary<TKey, TValue>, IList<KeyValuePair<TKey, TValue>> where TKey : notnull {
// These represent the min/max gap size between consecutive ordinals when the collection is
// balanced. Gaps (greater than 1) are there so that inserts can be performed without needing to
// update the ordinals of every item after the target index. The collection will gradually become
// unbalanced as mutations cause the gaps between ordinals to shrink. It is rebalanced when:
// A) an insert is attempted between 2 neighbors with a gap of 1, or
// B) an add is attempted when the last ordinal is ulong.MaxValue.
//
// Thus, the min/max ordinal steps have been tuned (crudely) to minimize rebalances. Here's the
// basic school of thought:
// A value of 1 is most optimial for adds, as it maximizes the number of elements that can be
// appended to the end of the list before the key ordinals must be rebalanced. However, a value
// of 1 is also the LEAST optimal for inserts, as it leaves no "ordinal space" between items,
// meaning the key ordinals would need to be rebalanced on each insert.
const ulong MAX_ORDINAL_STEP = 1 << 16;
const ulong ORDINAL_SEED = 1 << 12;
const int MAX_NUM_ITEMS = int.MaxValue;
readonly Dictionary<TKey, (TValue Value, ulong Ordinal)> data;
readonly List<(ulong Ordinal, TKey Key)> ordinalKeys;
readonly IList<TKey> orderedKeys;
readonly SelectorList<TKey, TValue> orderedValues;
readonly IComparer<(ulong Ordinal, TKey Key)> ordinalKeyComparer = TupleComparer.Comparer<ulong, TKey>(item1Ordinal: 0).AsComparer();
public OrderedDictionary()
: this(0, null, null) { }
public OrderedDictionary(IEqualityComparer<TKey>? comparer)
: this(4, comparer, null) { }
public OrderedDictionary(IEqualityComparer<TKey>? comparer, IEnumerable<KeyValuePair<TKey, TValue>>? items)
: this(items?.SoftCount() ?? 4, comparer, items) { }
public OrderedDictionary(IEnumerable<KeyValuePair<TKey, TValue>>? items)
: this(items?.SoftCount() ?? 4, null, items) { }
public OrderedDictionary(int capacity, IEqualityComparer<TKey>? comparer, IEnumerable<KeyValuePair<TKey, TValue>>? items) {
data = new(capacity, comparer);
ordinalKeys = new();
orderedKeys = ordinalKeys.WrapSelector(tup => tup.Key, null);
orderedValues = new SelectorList<TKey, TValue>(Keys, k => data[k].Value, null);
if (items is not null) {
AddRange(items);
}
}
public IEqualityComparer<TKey> Comparer => data.Comparer;
public TValue this[TKey key] {
get => data[key].Value;
set => SetItem(key, value);
}
public IList<TKey> Keys => orderedKeys;
public IList<TValue> Values => orderedValues;
public int Count => ordinalKeys.Count;
public bool IsReadOnly => false;
ICollection<TKey> IDictionary<TKey, TValue>.Keys => orderedKeys;
ICollection<TValue> IDictionary<TKey, TValue>.Values => orderedValues;
KeyValuePair<TKey, TValue> IList<KeyValuePair<TKey, TValue>>.this[int index] {
get {
var k = ordinalKeys[index].Key;
return new(k, data[k].Value);
}
set {
var keyComp = data.Comparer;
var (currOrdinalAtIndex, currKeyAtIndex) = ordinalKeys[index];
if (data.ContainsKey(value.Key)) {
// If the key is present, it must already be at the specified index to avoid introducing
// a duplicate key.
if (!keyComp.Equals(value.Key, currKeyAtIndex)) {
throw new ArgumentException("The same key is already present at another index", nameof(value));
}
} else {
Log.assert(data.Remove(currKeyAtIndex));
ordinalKeys[index] = (currOrdinalAtIndex, value.Key);
}
data[value.Key] = (value.Value, currOrdinalAtIndex);
}
}
ulong? firstOrdinal =>
ordinalKeys.Count == 0 ? null : ordinalKeys[0].Ordinal;
ulong? lastOrdinal =>
ordinalKeys.Count == 0 ? null : ordinalKeys[^1].Ordinal;
public void Add(TKey key, TValue value) {
if (Count == 0) {
data.Add(key, (value, ORDINAL_SEED));
ordinalKeys.Add((ORDINAL_SEED, key));
return;
}
checkCanInsert();
var lastOrd = ordinalKeys[^1].Ordinal;
var step = getStep(ulong.MaxValue - lastOrd);
if (step == 0) {
balanceAndAddRange(new KeyValuePair<TKey, TValue>[] { new(key, value) });
} else {
data.Add(key, (value, lastOrd + step));
ordinalKeys.Add((lastOrd + step, key));
}
}
public void Add(KeyValuePair<TKey, TValue> item) =>
Add(item.Key, item.Value);
public void AddRange(IEnumerable<KeyValuePair<TKey, TValue>> items) {
if (items is null) {
return;
}
var itemList = items.Where(kv => !data.ContainsKey(kv.Key))
.Distinct(KeyValuePairEqualityComparer.KeyComparer<TKey, TValue>(Comparer))
.ToList();
var itemCountUL = (ulong)itemList.Count;
if (itemCountUL == 0) {
return;
}
if (Count == 0) {
balanceAndAddRange(itemList, checkDuplicates: false);
return;
}
var lastOrd = ordinalKeys[^1].Ordinal;
var step = getStep(ulong.MaxValue - lastOrd, itemCountUL + 1);
if (step == 0) {
balanceAndAddRange(itemList, checkDuplicates: false);
} else {
data.AddRange(IndexDependentSelectorList.Create(itemList, GetDatum));
ordinalKeys.AddRange(IndexDependentSelectorList.Create(itemList, GetOrdinalKey));
ulong GetOrdinal(int i) =>
lastOrd + (ulong)(i + 1) * step;
KeyValuePair<TKey, (TValue Value, ulong Ordinal)> GetDatum(KeyValuePair<TKey, TValue> kv, int i) =>
new(kv.Key, (kv.Value, GetOrdinal(i)));
(ulong Ordinal, TKey Key) GetOrdinalKey(KeyValuePair<TKey, TValue> kv, int i) =>
(GetOrdinal(i), kv.Key);
}
}
public IList<KeyValuePair<TKey, TValue>> AsList() =>
this;
public void Clear() {
data.Clear();
orderedKeys.Clear();
}
public bool Contains(KeyValuePair<TKey, TValue> item) =>
data.TryGetValue(item.Key, out var v) && EqualityComparer<TValue>.Default.Equals(v.Value, item.Value);
public bool ContainsKey(TKey key) =>
data.ContainsKey(key);
public void CopyTo(KeyValuePair<TKey, TValue>[] array, int arrayIndex) {
int count = ordinalKeys.Count;
for (int i = 0; i < count; i++) {
var k = ordinalKeys[i].Key;
array[arrayIndex++] = new(k, data[k].Value);
}
}
public int IndexOf(KeyValuePair<TKey, TValue> item) {
if (data.TryGetValue(item.Key, out var v) && EqualityComparer<TValue>.Default.Equals(v.Value, item.Value)) {
int idx = ordinalKeys.BinarySearch((v.Ordinal, item.Key), ordinalKeyComparer);
if (idx < 0) {
throw new InvalidOperationException("OrdinalKey BinarySearch failed");
}
return idx;
}
return -1;
}
public void Insert(int index, KeyValuePair<TKey, TValue> item) =>
InsertRange(index, new[] { item });
public void InsertRange(int index, IEnumerable<KeyValuePair<TKey, TValue>> items) {
if (index == Count) {
AddRange(items);
return;
}
if (index < 0 || index > Count) {
throw new IndexOutOfRangeException();
}
var itemList = items.Where(kv => !data.ContainsKey(kv.Key))
.Distinct(KeyValuePairEqualityComparer.KeyComparer<TKey, TValue>(Comparer))
.ToList();
int itemCount = itemList.Count;
ulong itemCountUL = (ulong)itemCount;
if (itemCountUL == 0) {
return;
}
var upperOrd = ordinalKeys[index].Ordinal;
ulong firstOrd;
ulong step;
if (index == 0) {
step = getStep(upperOrd, itemCountUL);
if (step >= (1 << 4)) {
step = getStep(upperOrd, itemCountUL + 1);
firstOrd = step;
} else {
firstOrd = 0;
}
} else {
var lowerOrd = ordinalKeys[index - 1].Ordinal;
// Compute step size for 'itemCountUL + 1' to account for the item at lowerOrd.
step = getStep(upperOrd - lowerOrd, itemCountUL + 1);
firstOrd = lowerOrd + step;
}
if (step == 0) {
balanceAndInsertRange(index, itemList, checkDuplicates: false);
return;
}
var insertedOrdinalKeyList = IndexDependentSelectorList.Create(itemList, (kv, i) => (firstOrd + step * (ulong)i, kv.Key));
// Batch insert the ordinalKeys since they're stored in continuous memory
ordinalKeys.InsertRange(index, insertedOrdinalKeyList);
for (int i = 0; i < itemCount; i++) {
var (k, v) = itemList[i];
var itemOrd = firstOrd + step * (ulong)i;
data.Add(k, (v, itemOrd));
}
}
public bool Remove(TKey key) {
if (data.TryGetValue(key, out var v)) {
Log.assert(data.Remove(key));
ordinalKeys.RemoveSorted((v.Ordinal, key), ordinalKeyComparer);
return true;
}
return false;
}
public bool Remove(KeyValuePair<TKey, TValue> item) {
if (data.TryGetValue(item.Key, out var v) && EqualityComparer<TValue>.Default.Equals(v.Value, item.Value)) {
Log.assert(data.Remove(item.Key));
ordinalKeys.RemoveSorted((v.Ordinal, item.Key), ordinalKeyComparer);
return true;
}
return false;
}
public void RemoveAt(int index) {
var (_, key) = ordinalKeys[index];
ordinalKeys.RemoveAt(index);
data.Remove(key);
}
public void SetItem(TKey key, TValue value) {
if (Count == 0) {
Add(key, value);
} else if (data.TryGetValue(key, out var curr)) {
data[key] = (value, curr.Ordinal);
} else {
var itemOrd = getNextOrdinal(ordinalKeys[^1].Ordinal);
if (itemOrd.HasValue) {
ordinalKeys.Add((itemOrd.Value, key));
data.Add(key, (value, itemOrd.Value));
} else {
balanceAndAddRange(new KeyValuePair<TKey, TValue>[] { new(key, value) });
}
}
}
public bool TryGetValue(TKey key, [MaybeNullWhen(false)] out TValue value) {
if (data.TryGetValue(key, out var v)) {
value = v.Value;
return true;
}
value = default;
return false;
}
public IEnumerator<KeyValuePair<TKey, TValue>> GetEnumerator() {
for (int i = 0; i < orderedKeys.Count; i++) {
yield return new(orderedKeys[i], data[orderedKeys[i]].Value);
}
}
static ulong? getNextOrdinal(ulong lastOrd) {
if (lastOrd == ulong.MaxValue) {
return null;
}
ulong step = getStep(ulong.MaxValue - lastOrd);
return lastOrd + step;
}
static ulong getStep(ulong ordinalSpan) {
ulong step = ordinalSpan switch {
>= (1UL << 32) => 1 << 16,
>= (1UL << 30) => 1 << 15,
>= (1UL << 28) => 1 << 14,
>= (1UL << 26) => 1 << 13,
>= (1UL << 24) => 1 << 12,
>= (1UL << 22) => 1 << 11,
>= (1UL << 20) => 1 << 10,
>= (1UL << 18) => 1 << 9,
>= (1UL << 16) => 1 << 8,
>= (1UL << 14) => 1 << 7,
>= (1UL << 12) => 1 << 6,
>= (1UL << 10) => 1 << 5,
>= (1UL << 8) => 1 << 4,
>= (1UL << 6) => 1 << 3,
>= (1UL << 4) => 1 << 2,
>= (1UL << 2) => 1 << 1,
>= 1UL => 1,
0UL => 0,
};
return step;
}
static ulong getStep(ulong ordinalSpan, ulong numItems) {
if (numItems >= ordinalSpan) {
return 0;
}
ulong step = ordinalSpan / numItems;
step = Math.Min(step, MAX_ORDINAL_STEP);
return step;
}
IEnumerator IEnumerable.GetEnumerator() =>
GetEnumerator();
void checkCanInsert() {
if (Count >= MAX_NUM_ITEMS) {
throw new InvalidOperationException("Add/Insert would exceed the maximum number of items");
}
}
void balanceAndAddRange(IEnumerable<KeyValuePair<TKey, TValue>>? concatenatedItems, bool checkDuplicates = true) =>
balanceAndInsertRange(Count, concatenatedItems, checkDuplicates);
void balanceAndInsertRange(int index, IEnumerable<KeyValuePair<TKey, TValue>>? insertedItems, bool checkDuplicates = true) {
if (index < 0 || index > Count) {
throw new IndexOutOfRangeException();
}
if (checkDuplicates) {
insertedItems = insertedItems
?.Where(kv => !data.ContainsKey(kv.Key))
?.Distinct(KeyValuePairEqualityComparer.KeyComparer<TKey, TValue>(Comparer));
}
var insertedItemList = insertedItems as IList<KeyValuePair<TKey, TValue>> ?? insertedItems?.ToList() ?? new(0);
int insertedItemCount = insertedItemList.Count;
int origCount = Count;
int newCount = origCount + insertedItemCount;
var newCountUL = (ulong)origCount + (ulong)insertedItemList.Count;
var step = getStep(ulong.MaxValue, newCountUL + 1UL << 8);
var ord = step * (1UL << 7);
int i;
// Balance ordinals before insertion index
for (i = 0; i < index; i++) {
var (_, k) = ordinalKeys[i];
ordinalKeys[i] = (ord, k);
var (v, _) = data[k];
data[k] = (v, ord);
ord += step;
}
// Batch insert the ordinalKeys since they're stored in continuous memory
ordinalKeys.InsertRange(index, insertedItemList.Select((kv, i) => (ord + (ulong)i * step, kv.Key)));
foreach (var (k, v) in insertedItemList) {
data[k] = (v, ord);
ord += step;
}
// Balance ordinals after insertion index
for (i = index + insertedItemCount; i < newCount; i++) {
var (_, k) = ordinalKeys[i];
ordinalKeys[i] = (ord, k);
var (v, _) = data[k];
data[k] = (v, ord);
ord += step;
}
Log.assert(Count == origCount + insertedItemCount);
}
/// WARNING: <param name="items" /> should already be unique (this fn does NOT check)
void reset(IEnumerable<KeyValuePair<TKey, TValue>>? items) {
Clear();
balanceAndAddRange(items);
}
}
@@ -0,0 +1,65 @@
namespace Spark.Core.Collections;
using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
public class ReverseArrayList<T> : IList<T> {
readonly T[] source;
public ReverseArrayList(T[] source) =>
this.source = source;
public T this[int index] {
get => source[source.Length - 1 - index];
set => source[source.Length - 1 - index] = value;
}
public int Count => source.Length;
public bool IsReadOnly => false;
public void Add(T item) =>
throw new NotSupportedException();
public void Clear() =>
throw new NotSupportedException();
public bool Contains(T item) =>
source.Contains(item);
public void CopyTo(T[] array, int arrayIndex) {
for (int i = source.Length - 1; i > -1; i--) {
array[arrayIndex++] = source[i];
}
}
public IEnumerator<T> GetEnumerator() {
for (int i = source.Length - 1; i > -1; i--) {
yield return source[i];
}
}
public int IndexOf(T item) {
var comp = EqualityComparer<T>.Default;
for (int i = 0; i < source.Length; i++) {
if (comp.Equals(source[i], item)) {
return source.Length - 1 - i;
}
}
return -1;
}
public void Insert(int index, T item) =>
throw new NotSupportedException();
public bool Remove(T item) =>
throw new NotSupportedException();
public void RemoveAt(int index) =>
throw new NotSupportedException();
IEnumerator IEnumerable.GetEnumerator() =>
GetEnumerator();
}
@@ -0,0 +1,98 @@
#nullable enable
namespace Spark.Core.Collections;
using bts;
using bts.collections;
using System;
using System.Collections.Generic;
public static class SliceList {
public static SliceList<T> Create<T>(IList<T> source, int? start, int? stop) =>
new(source, start, stop);
public static int GetSliceIndex(int i, int count, bool constrainToCount = false) {
int rv = i < 0 ? i + count : i;
if (constrainToCount) {
rv = Math.Max(0, Math.Min(rv, count));
}
return rv;
}
public static int GetStartIndex(int? i, int count, bool constrainToCount = false) =>
GetSliceIndex(i ?? 0, count, constrainToCount);
public static int GetStopIndex(int? i, int count, bool constrainToCount = false) =>
GetSliceIndex(i ?? count, count, constrainToCount);
}
/// <summary>
/// Represents a continuous subsection of a list.
/// </summary>
public class SliceList<T> : IMutableListFacade<T> {
readonly IList<T> source;
readonly int? start;
readonly int? stop;
public SliceList(IList<T> source, int? start, int? stop) {
this.source = source;
this.start = start;
this.stop = stop;
}
public int Count {
get {
if (startIndex >= stopIndex) {
return 0;
}
var srcCount = source.Count;
if (startIndex >= srcCount) {
return 0;
}
if (stopIndex > srcCount) {
return srcCount - startIndex;
}
return stopIndex - startIndex;
}
}
public T this[int index] =>
index >= 0 && index < Count
? source[startIndex + index]
: throw new IndexOutOfRangeException();
int startIndex => SliceList.GetStartIndex(start, source.Count);
int stopIndex => SliceList.GetStopIndex(stop, source.Count);
public SliceList<T> OnChange(IList<T> newItems, IList<T> removedItems, IList<T> addedItems, out List<T> sliceRemoves, out List<T> sliceInserts) {
var sortedRemoves = new List<int>();
var sortedInserts = new List<(int Index, T Row)>();
var indexRowTupleComp = Comparer<(int Index, T Row)>.Create((a, b) => a.Index.CompareTo(b.Index));
foreach (var r in removedItems) {
int idx = source.IndexOf(r);
Log.assert(idx >= 0);
sortedRemoves.InsertSorted(idx);
}
foreach (var r in addedItems) {
int idx = newItems.IndexOf(r);
Log.assert(idx >= 0);
sortedInserts.InsertSorted((idx, r), indexRowTupleComp);
}
sliceRemoves = new();
sliceInserts = new();
return OnChange(newItems, sortedRemoves, sortedInserts, ref sliceRemoves, ref sliceInserts);
}
public SliceList<T> OnChange(IList<T> newValues, IList<int> sortedRemoves, IList<(int Index, T Value)> sortedAdds, ref List<T> sliceRemoves, ref List<T> sliceInserts) {
BlockTransactionEnumerable.Create(source, sortedRemoves, sortedAdds)
.SliceDiff(source, newValues, ref sliceRemoves, ref sliceInserts, start, stop);
return new(newValues, start, stop);
}
public IEnumerator<T> GetEnumerator() {
var count = Count;
for (int i = 0; i < count; i++) {
yield return source[startIndex + i];
}
}
}
@@ -0,0 +1,36 @@
#nullable enable
namespace Spark.Core.Collections;
using System;
using System.Collections;
using System.Collections.Generic;
using System.Numerics;
public class StepList<T> : IMutableListFacade<T> where T : notnull, INumberBase<T> {
readonly T origin;
readonly T step;
readonly int count;
public StepList(T origin, T step, int count) {
this.origin = origin;
this.step = step;
this.count = count;
}
public T this[int index] =>
index < 0 || index >= count
? throw new IndexOutOfRangeException()
: origin + step * NumericCastConverter<int, T>.convert(index);
public int Count => count;
public IEnumerator<T> GetEnumerator() {
for (int i = 0; i < count; i++) {
yield return origin + step * NumericCastConverter<int, T>.convert(i);
}
}
IEnumerator IEnumerable.GetEnumerator() {
return GetEnumerator();
}
}
@@ -0,0 +1,89 @@
#nullable enable
namespace Spark.Core.Collections;
using bts.utils;
using mbts.utils;
using System.Collections.Generic;
public static class TupleComparer {
public static IDualComparer<(T1, T2)> Comparer<T1, T2>() =>
new ComparerBuilder<(T1, T2)>() {
t => t.Item1,
t => t.Item2,
}.ToDualComparer();
public static IDualComparer<(T1, T2)> Comparer<T1, T2>(int? item1Ordinal = null, int? item2Ordinal = null) {
var comps = new List<KeyValuePair<int, IComparer<(T1, T2)>>>();
if (item1Ordinal.HasValue) {
comps.Add(new(item1Ordinal.Value, CompareUtils.wrap<T1, (T1, T2)>(Comparer<T1>.Default, t => t.Item1)));
}
if (item2Ordinal.HasValue) {
comps.Add(new(item2Ordinal.Value, CompareUtils.wrap<T2, (T1, T2)>(Comparer<T2>.Default, t => t.Item2)));
}
comps.Sort(KeyValuePairComparer.KeyComparer<int, IComparer<(T1, T2)>>());
var bd = new ComparerBuilder<(T1, T2)>();
foreach (var (_, comp) in comps) {
bd.Add(comp);
}
return bd.ToDualComparer();
}
public static IDualComparer<(T1, T2)> Comparer<T1, T2>(int? item1Ordinal, int? item2Ordinal, IComparer<T1>? comparer1 = null, IComparer<T2>? comparer2 = null) {
var comps = new List<KeyValuePair<int, IComparer<(T1, T2)>>>();
if (item1Ordinal.HasValue) {
comps.Add(new(item1Ordinal.Value, CompareUtils.wrap<T1, (T1, T2)>(comparer1 ?? Comparer<T1>.Default, t => t.Item1)));
}
if (item2Ordinal.HasValue) {
comps.Add(new(item2Ordinal.Value, CompareUtils.wrap<T2, (T1, T2)>(comparer2 ?? Comparer<T2>.Default, t => t.Item2)));
}
comps.Sort(KeyValuePairComparer.KeyComparer<int, IComparer<(T1, T2)>>());
var bd = new ComparerBuilder<(T1, T2)>();
foreach (var (_, comp) in comps) {
bd.Add(comp);
}
return bd.ToDualComparer();
}
public static IDualComparer<(T1, T2, T3)> Comparer<T1, T2, T3>(int? item1Ordinal = null, int? item2Ordinal = null, int? item3Ordinal = null) {
var comps = new List<KeyValuePair<int, IComparer<(T1, T2, T3)>>>();
if (item1Ordinal.HasValue) {
comps.Add(new(item1Ordinal.Value, CompareUtils.wrap<T1, (T1, T2, T3)>(Comparer<T1>.Default, t => t.Item1)));
}
if (item2Ordinal.HasValue) {
comps.Add(new(item2Ordinal.Value, CompareUtils.wrap<T2, (T1, T2, T3)>(Comparer<T2>.Default, t => t.Item2)));
}
if (item3Ordinal.HasValue) {
comps.Add(new(item3Ordinal.Value, CompareUtils.wrap<T3, (T1, T2, T3)>(Comparer<T3>.Default, t => t.Item3)));
}
comps.Sort(KeyValuePairComparer.KeyComparer<int, IComparer<(T1, T2, T3)>>());
var bd = new ComparerBuilder<(T1, T2, T3)>();
foreach (var (_, comp) in comps) {
bd.Add(comp);
}
return bd.ToDualComparer();
}
public static IDualComparer<(T1, T2, T3, T4)> Comparer<T1, T2, T3, T4>(int? item1Ordinal = null, int? item2Ordinal = null, int? item3Ordinal = null, int? item4Ordinal = null) {
var comps = new List<KeyValuePair<int, IComparer<(T1, T2, T3, T4)>>>();
if (item1Ordinal.HasValue) {
comps.Add(new(item1Ordinal.Value, CompareUtils.wrap<T1, (T1, T2, T3, T4)>(Comparer<T1>.Default, t => t.Item1)));
}
if (item2Ordinal.HasValue) {
comps.Add(new(item2Ordinal.Value, CompareUtils.wrap<T2, (T1, T2, T3, T4)>(Comparer<T2>.Default, t => t.Item2)));
}
if (item3Ordinal.HasValue) {
comps.Add(new(item3Ordinal.Value, CompareUtils.wrap<T3, (T1, T2, T3, T4)>(Comparer<T3>.Default, t => t.Item3)));
}
if (item4Ordinal.HasValue) {
comps.Add(new(item4Ordinal.Value, CompareUtils.wrap<T4, (T1, T2, T3, T4)>(Comparer<T4>.Default, t => t.Item4)));
}
comps.Sort(KeyValuePairComparer.KeyComparer<int, IComparer<(T1, T2, T3, T4)>>());
var bd = new ComparerBuilder<(T1, T2, T3, T4)>();
foreach (var (_, comp) in comps) {
bd.Add(comp);
}
return bd.ToDualComparer();
}
}
@@ -466,5 +466,98 @@ namespace bts.utils {
}
return -1;
}
/// <summary>
/// Gets coordinates for an index in the imaginary sorted list that results from merging
/// <paramref name="list1"/> and <paramref name="list2"/>. The "X" coordinate is either 0
/// (list1) or 1 (list2), and the "Y" coordinate is the index in that list.
/// </summary>
/// <typeparam name="T">The type of elements in the list</typeparam>
/// <param name="list1">One of two lists making up the imaginary sorted merged list</param>
/// <param name="index">The index in the imaginary sorted merged list for which to get coordinates</param>
/// <param name="list2">One of two lists making up the imaginary sorted merged list</param>
/// <param name="comparer">The comparer by which the elements are sorted</param>
/// <returns></returns>
/// <exception cref="IndexOutOfRangeException"></exception>
/// <exception cref="InvalidOperationException"></exception>
public static (int X, int Y) BinarySearchMergedIndex<T>(IList<T> list1, int index, IList<T> list2, IComparer<T> comparer) {
if (index < 0 || index >= list1.Count + list2.Count) {
throw new IndexOutOfRangeException();
}
if (list1.Count == 0) {
return (1, index);
}
if (list2.Count == 0) {
return (0, index);
}
int list1Start = binarySearchPosition(list2, list1[0], comparer);
if (index < list1Start) {
return (1, index);
}
if (index == list1Start) {
return (0, 0);
}
if (list1.Count == 1) {
return (1, index - 1);
}
list2 = list2.WrapSubRange(list1Start, list2.Count - list1Start);
index -= list1Start;
var res = binarySearchMergedIndex(list1, index, list2, comparer);
if (res.X == 1) {
res = (res.X, res.Y + list1Start);
}
return res;
static (int X, int Y) binarySearchMergedIndex(IList<T> list1, int index, IList<T> list2, IComparer<T> comparer) {
if (index < 0 || index >= list1.Count + list2.Count) {
throw new IndexOutOfRangeException();
}
int list1Count = list1.Count;
if (list1Count == 0) {
return (1, index);
}
int list2Count = list2.Count;
if (list2Count == 0) {
return (0, index);
}
if (list1Count > index + 1) {
list1 = list1.WrapSubRange(0, index + 1);
}
if (list2Count > index + 1) {
list2 = list2.WrapSubRange(0, index + 1);
}
int mid1 = list1.Count / 2;
int mid1Pos2 = binarySearchPosition(list2, list1[mid1], comparer);
int shift1 = 0;
int shift2 = 0;
if ((mid1 + mid1Pos2) > index) {
list1 = list1.WrapSubRange(0, mid1);
list2 = list2.WrapSubRange(0, mid1Pos2);
} else if ((mid1 + mid1Pos2) < index) {
index -= mid1 + mid1Pos2 + 1;
if (index < 0) {
throw new InvalidOperationException("Bad state (index became negative)");
}
shift1 += mid1 + 1;
list1 = list1.WrapSubRange(mid1 + 1);
shift2 += mid1Pos2;
list2 = list2.WrapSubRange(mid1Pos2);
} else {
return (0, mid1);
}
var (x, y) = binarySearchMergedIndex(list1, index, list2, comparer);
return x == 0
? (0, y + shift1)
: (1, y + shift2);
}
}
static int binarySearchPosition<T>(IList<T> list, T value, IComparer<T> comparer) {
int rv = list.BinarySearch(value, comparer);
if (rv < 0) {
rv = ~rv;
}
return rv;
}
}
}
+11 -2
View File
@@ -1,7 +1,9 @@
#nullable enable
#nullable enable
namespace mbts.utils {
using bts.utils;
using Spark.Core.Collections;
using System;
using System.Collections;
using System.Collections.Generic;
@@ -78,6 +80,13 @@ namespace mbts.utils {
_comparers.RemoveAt(index);
public IComparer<T> to_comparer() =>
_comparers.Count switch {
0 => Comparer<T>.Default,
1 => _comparers[0],
_ => new _Comparer(_comparers.ToArray()),
};
public IDualComparer<T> ToDualComparer() =>
new _Comparer(_comparers.ToArray());
public ComparerBuilder<T> append<TInner>(Func<T, TInner> selector) =>
@@ -115,7 +124,7 @@ namespace mbts.utils {
IEnumerator IEnumerable.GetEnumerator() =>
_comparers.GetEnumerator();
class _Comparer : IComparer<T> {
class _Comparer : IDualComparer<T> {
readonly IComparer<T>[] _comparers;
public _Comparer(IComparer<T>[] comparers) =>
@@ -0,0 +1,180 @@
#nullable enable
namespace Spark.Test;
using bts.collections;
using bts.test;
using bts.tests;
using Spark.Core.Collections;
using System;
using System.Collections.Generic;
using System.Collections.Immutable;
using System.Diagnostics;
using System.Linq;
[TestGroup]
public class ImmutableOrderedDictionaryTests {
static IEnumerable<KeyValuePair<string, int>> basicItems1() {
yield return new("jane", 18);
yield return new("wayne", 19);
yield return new("foo", 8);
yield return new("bar", 9);
}
static IEnumerable<KeyValuePair<string, int>> basicItems2() {
yield return new("salt", 5);
yield return new("zoo", 100);
yield return new("pepper", 29);
yield return new("boo", 317);
}
static IEnumerable<KeyValuePair<string, int>> staggeredItems(IEnumerable<KeyValuePair<string, int>> items1, IEnumerable<KeyValuePair<string, int>> items2) {
foreach (var (item1, item2) in items1.Zip(items2)) {
yield return item1;
yield return item2;
}
}
static IEnumerable<KeyValuePair<string, int>> randomItems(int count) {
var rand = new Random(unchecked((int)DateTime.Now.Ticks));
while (count-- > 0) {
yield return new(Guid.NewGuid().ToString(), rand.Next());
}
}
[Test]
public void TestAdd() {
var items = basicItems1().ToArray();
var od = ImmutableOrderedDictionary.CreateRange(items);
Assert.SequenceEqual(od, items);
od = ImmutableOrderedDictionary<string, int>.Empty;
foreach (var (k, v) in items) {
od = od.Add(k, v);
}
Assert.SequenceEqual(od, items);
}
[Test]
public void TestAddMany() {
var items = randomItems(40_000).ToArray();
var od = ImmutableOrderedDictionary.CreateRange(items);
Assert.SequenceEqual(od, items);
od = ImmutableOrderedDictionary<string, int>.Empty;
foreach (var (k, v) in items) {
od = od.Add(k, v);
}
Assert.SequenceEqual(od, items);
}
[Test]
public void TestInsert() {
var items1 = basicItems1().ToArray();
var od = ImmutableOrderedDictionary<string, int>.Empty;
for (int i = 0; i < items1.Length; i++) {
od = od.Insert(i, items1[i]);
}
var items2 = basicItems2().ToArray();
if (items1.Length != items2.Length || items1.Length == 0) {
throw new InvalidOperationException("Invalid test items");
}
for (int i = 0; i < items2.Length; i++) {
// items1 and items2 are the same length. We insert at 2 * i to stagger the indices.
od = od.Insert(2 * i, items2[i]);
}
var expected = staggeredItems(items2, items1).ToList();
var matchLength = matchingSequenceLength(od, expected);
if (matchLength != od.Count || matchLength != expected.Count) {
}
Assert.SequenceEqual(od, expected);
}
[Test]
public void TestInsertMany() {
var items1 = basicItems1().ToArray();
var orig = ImmutableOrderedDictionary.CreateRange(items1);
// 8192
const int NUM_INSERTS = 1 << 12;
var inserts = randomItems(NUM_INSERTS).ToArray();
if (items1.Length > 10) {
throw new InvalidOperationException("Expected basicItems1 to be reasonably short (10 items or less)");
}
var sw = new Stopwatch();
for (int i = 0; i <= items1.Length; i++) {
var od = orig;
var elapsedBefore = sw.Elapsed;
int origCount = od.Count;
sw.Start();
foreach (var item in inserts) {
int countBefore = od.Count;
od = od.Insert(i, item);
Assert.That(od.Count == countBefore + 1);
}
sw.Stop();
var elapsed = sw.Elapsed - elapsedBefore;
Console.WriteLine($$"""[immutable_ordered_dict_test_ins_many.json]{ "orig_count": {{origCount}}, "ins_count": {{inserts.Length}}, "idx": {{i}}, "dur": "{{elapsed.TotalMilliseconds:0.000}}ms" }""");
var prefix = items1.Take(i);
var middle = inserts.Reverse();
var suffix = items1.Skip(i);
var expected = prefix.Concat(middle).Concat(suffix);
var matchLength = matchingSequenceLength(od, expected);
if (matchLength != od.Count) {
}
Assert.SequenceEqual(od, expected);
}
}
[Test]
public void TestInsertRange() {
var items1 = basicItems1().ToArray();
var orig = ImmutableOrderedDictionary.CreateRange(items1);
// 8192
const int NUM_INSERTS = 1 << 12;
var inserts = randomItems(NUM_INSERTS).ToArray();
if (items1.Length > 10) {
throw new InvalidOperationException("Expected basicItems1 to be reasonably short (10 items or less)");
}
var sw = new Stopwatch();
for (int i = 0; i <= items1.Length; i++) {
var od = orig;
var elapsedBefore = sw.Elapsed;
sw.Start();
od = od.InsertRange(i, inserts);
sw.Stop();
var elapsed = sw.Elapsed - elapsedBefore;
Console.WriteLine($$"""[immutable_ordered_dict_test_ins_range.json]{ "orig_count": {{orig.Count}}, "ins_count": {{inserts.Length}}, "idx": {{i}}, "dur": "{{elapsed.TotalMilliseconds:0.000}}ms" }""");
var prefix = items1.Take(i);
var middle = inserts;
var suffix = items1.Skip(i);
var expected = prefix.Concat(middle).Concat(suffix);
var matchLength = matchingSequenceLength(od, expected);
if (matchLength != od.Count) {
}
Assert.SequenceEqual(od, expected);
}
}
[Test]
public void TestRemove() {
var items = basicItems1().ToArray();
var od = ImmutableOrderedDictionary.CreateRange(items);
Assert.SequenceEqual(od, items);
for (int i = 0; i < items.Length; i++) {
od = od.Remove(items[i]);
Assert.That(od.Count == items.Length - (i + 1));
Assert.SequenceEqual(od, items[(i + 1)..]);
}
}
static int matchingSequenceLength<T>(IEnumerable<T> items1, IEnumerable<T> items2) {
var comp = EqualityComparer<T>.Default;
using var en1 = items1.GetEnumerator();
using var en2 = items2.GetEnumerator();
int n = 0;
while (en1.MoveNext() && en2.MoveNext() && comp.Equals(en1.Current, en2.Current)) {
n++;
}
return n;
}
}
@@ -0,0 +1,54 @@
namespace bts.tests;
using bts.test;
using Spark.Core.Collections;
using System.Collections.Generic;
using System.Linq;
[TestGroup]
class MergedSortedListTests {
[Test]
public void TestAll() {
var list1 = Enumerable.Range(0, 10).Select(i => (double)i).ToList();
var list2 = new List<double>() {
2,
2,
2.8,
3.3,
5,
6,
7.1,
9.1,
10.5,
};
assertSorted(list1, list2);
list1 = new();
assertSorted(list1, list2);
}
static void assertSorted(IList<double> list1, IList<double> list2) {
Impl(list1, list2);
Impl(list2, list1);
static void Impl(IList<double> list1, IList<double> list2) {
// list1 and list2 should already be sorted within themselves (MergedSortedList assumes this).
var expected = list1.Concat(list2).Ordered().ToList();
var mergedList = MergedSortedList.Create(list1, list2);
// Test IEnumerable impl
Assert.SequenceEqual(mergedList, expected);
var indexedVals = new List<double>();
for (int i = 0; i < expected.Count; i++) {
var val = mergedList[i];
if (val != expected[i]) {
val = mergedList[i];
}
indexedVals.Add(val);
}
var mergedIndexorList = Enumerable.Range(0, mergedList.Count).Select(i => mergedList[i]).ToList();
// Test indexor impl
Assert.SequenceEqual(mergedIndexorList, expected);
}
}
}
@@ -0,0 +1,96 @@
#nullable enable
namespace Spark.Test;
using bts.test;
using Spark.Core.Collections;
using System;
using System.Collections.Generic;
using System.Linq;
[TestGroup]
public class OrderedDictionaryTests {
static IEnumerable<KeyValuePair<string, int>> basicItems1() {
yield return new("jane", 18);
yield return new("wayne", 19);
yield return new("foo", 8);
yield return new("bar", 9);
}
static IEnumerable<KeyValuePair<string, int>> basicItems2() {
yield return new("salt", 5);
yield return new("zoo", 100);
yield return new("pepper", 29);
yield return new("boo", 317);
}
static IEnumerable<KeyValuePair<string, int>> staggeredItems(IEnumerable<KeyValuePair<string, int>> items1, IEnumerable<KeyValuePair<string, int>> items2) {
foreach (var (item1, item2) in items1.Zip(items2)) {
yield return item1;
yield return item2;
}
}
static IEnumerable<KeyValuePair<string, int>> randomItems(int count) {
var rand = new Random(unchecked((int)DateTime.Now.Ticks));
while (count-- > 0) {
yield return new(Guid.NewGuid().ToString(), rand.Next());
}
}
[Test]
public void TestAdd() {
var items = basicItems1().ToArray();
var od = new OrderedDictionary<string, int>(items);
Assert.SequenceEqual(od, items);
od = new OrderedDictionary<string, int>();
foreach (var (k, v) in items) {
od.Add(k, v);
}
Assert.SequenceEqual(od, items);
}
[Test]
public void TestAddMany() {
var items = randomItems(40_000).ToArray();
var od = new OrderedDictionary<string, int>(items);
Assert.SequenceEqual(od, items);
od = new OrderedDictionary<string, int>();
foreach (var (k, v) in items) {
od.Add(k, v);
}
Assert.SequenceEqual(od, items);
}
[Test]
public void TestInsert() {
var items1 = basicItems1().ToArray();
var od = new OrderedDictionary<string, int>();
for (int i = 0; i < items1.Length; i++) {
od.Insert(i, items1[i]);
}
var items2 = basicItems2().ToArray();
if (items1.Length != items2.Length || items1.Length == 0) {
throw new InvalidOperationException("Invalid test items");
}
for (int i = 0; i < items2.Length; i++) {
// items1 and items2 are the same length. We insert at 2 * i to stagger the indices.
od.Insert(2 * i, items2[i]);
}
var expected = staggeredItems(items2, items1).ToList();
Assert.SequenceEqual(od, expected);
}
[Test]
public void TestRemove() {
var items = basicItems1().ToArray();
var od = new OrderedDictionary<string, int>(items);
Assert.SequenceEqual(od, items);
for (int i = 0; i < items.Length; i++) {
Assert.That(od.Remove(items[i]));
Assert.That(od.Count == items.Length - (i + 1));
Assert.SequenceEqual(od, items[(i + 1)..]);
}
}
}
@@ -0,0 +1,205 @@
#nullable enable
namespace Spark.Test;
using bts.test;
using Spark.Core.Collections;
using System.Collections.Generic;
using System.Collections.Immutable;
using System.Linq;
[TestGroup]
public class SliceListTest {
[Test]
public void Test() {
// Test slice of indices 2-5 on a source list w/ values 0-10
// Values: [0.0, 1.0, 2.0, 3.0, 4.0, 5.0, 6.0, 7.0, 8.0, 9.0]
// Slice: [ 2.0, 3.0, 4.0 ]
GetTestData(0, 10, 2, 5)
.OnChange(arr(1.1, 1.2, 4.1), arr(0, 2, 5))
// Values: [1.0, 1.1, 1.2, 3.0, 4.0, 4.1, 6.0, 7.0, 8.0, 9.0]
// Slice: [ 1.2, 3.0, 4.0 ]
.Validate(arr(1.2), arr(2), arr(1.2, 3, 4))
.OnChange(null, arr(1.2))
// Values: [1.0, 1.1, 3.0, 4.0, 4.1, 6.0, 7.0, 8.0, 9.0]
// Slice: [ 3.0, 4.0, 4.1 ]
.Validate(arr(4.1), arr(1.2), arr(3, 4, 4.1))
.OnChange(null, arr(1.0))
// Values: [1.1, 3.0, 4.0, 4.1, 6.0, 7.0, 8.0, 9.0]
// Slice: [ 4.0, 4.1, 6.0 ]
.Validate(arr(6), arr(3), expectedSlice: arr(4, 4.1, 6))
.OnChange(null, arr(7.0))
// Values: [1.1, 3.0, 4.0, 4.1, 6.0, 8.0, 9.0]
// Slice: [ 4.0, 4.1, 6.0 ]
.Validate(null, null, arr(4, 4.1, 6))
.OnChange(arr(5.5), arr(3.0))
// Values: [1.1, 4.0, 4.1, 5.5, 6.0, 8.0, 9.0]
// Slice: [ 4.1, 5.5, 6.0 ]
.Validate(arr(5.5), arr(4), arr(4.1, 5.5, 6));
}
[Test]
public void TestSpecial() {
// Add value right after the slice, and remove value right before it.
// Verify this changes the last value in the slice.
new TestData(arr(1.1, 3, 4, 4.1, 6, 8, 9), 2, 5)
.OnChange(arr(5.5), arr(3.0))
// Values: [1.1, 4.0, 4.1, 5.5, 6.0, 8.0, 9.0]
// Slice: [ 4.1, 5.5, 6.0 ]
.Validate(expectedAdds: arr(5.5), expectedRemoves: arr(4), expectedSlice: arr(4.1, 5.5, 6));
}
[Test]
public void TestAdds() {
// Values: [0.0, 1.0, 2.0, 3.0, 4.0, 5.0, 6.0, 7.0, 8.0, 9.0]
// Slice: [ 2.0, 3.0, 4.0 ]
GetTestData(0, 10, 2, 5)
.OnChange(arr(1.1, 1.2, 4.1), arr(0, 2, 5))
// Values: [1.0, 1.1, 1.2, 3.0, 4.0, 4.1, 6.0, 7.0, 8.0, 9.0]
// Slice: [ 1.2, 3.0, 4.0 ]
.Validate(arr(1.2), arr(2), arr(1.2, 3, 4));
GetTestData(0, 10, 2, 5)
.OnChange(arr(1.1, 1.2, 4.1), arr(2))
.Validate(arr(1.1, 1.2), arr(2, 4), arr(1.1, 1.2, 3));
GetTestData(0, 10, 2, 5)
.OnChange(arr(), arr(2.0, 3.0))
.Validate(arr(5, 6), arr(2, 3), arr(4, 5, 6));
GetTestData(0, 10, 2, 5)
.OnChange(arr(), arr(0))
.Validate(arr(5), arr(2), arr(3, 4, 5));
GetTestData(0, 10, 2, 5)
.OnChange(arr(), Enumerable.Range(0, 10).Select(n => (double)n).ToArray())
.Validate(null, arr(2, 3, 4), null);
GetTestData(0, 10, 2, 5)
.OnChange(arr(), Enumerable.Range(2, 3).Select(n => (double)n).ToArray())
.Validate(expectedAdds: arr(5, 6, 7), expectedRemoves: arr(2, 3, 4), expectedSlice: arr(5, 6, 7));
GetTestData(0, 10, 2, 5)
.OnChange(arr(1.1), Enumerable.Range(2, 3).Select(n => (double)n).ToArray())
.Validate(expectedAdds: arr(1.1, 5, 6), expectedRemoves: arr(2, 3, 4), expectedSlice: arr(1.1, 5, 6));
GetTestData(0, 0, 2, 5)
.OnChange(arr(0), arr())
.Validate();
GetTestData(0, 0, 2, 5)
.OnChange(arr(0, 1), arr())
.Validate();
GetTestData(0, 0, 2, 5)
.OnChange(arr(0, 1, 2), arr())
.Validate(arr(2), null, arr(2));
GetTestData(0, 0, 0, 3)
.OnChange(arr(0), arr())
.Validate(arr(0), null, arr(0));
GetTestData(0, 0, 0, 3)
.OnChange(arr(0, 1), arr())
.Validate(arr(0, 1), null, arr(0, 1));
GetTestData(0, 0, 0, 3)
.OnChange(arr(0, 1, 2), arr())
.Validate(expectedAdds: arr(0, 1, 2), expectedSlice: arr(0, 1, 2));
GetTestData(0, 0, 0, 3)
.OnChange(arr(0, 1, 2, 3), arr())
.Validate(expectedAdds: arr(0, 1, 2), expectedSlice: arr(0, 1, 2));
GetTestData(0, 2, 0, 3)
.OnChange(arr(0.5, 1.5, 2.5), arr())
.Validate(expectedAdds: arr(0.5), expectedSlice: arr(0, 0.5, 1));
GetTestData(0, 3, 0, 3)
.OnChange(arr(0.5, 1.5, 2.5), arr())
.Validate(arr(0.5), arr(2), arr(0, 0.5, 1));
}
[Test]
public void TestLong() {
var td = GetTestData(0, 20, 7, 17);
td.OnChange(arr(0.5, 1.5, 2.5, 10.5, 13.5), arr(7, 11, 16));
td.Validate(
expectedAdds: arr(4, 5, 6, 10.5, 13.5),
expectedRemoves: arr(7, 11, 14, 15, 16),
expectedSlice: arr(4, 5, 6, 8, 9, 10, 10.5, 12, 13, 13.5));
}
static double[] arr(params double[] values) =>
values;
static TestData GetTestData(int start = 0, int count = 10, int sliceStart = 2, int sliceStop = 5) =>
new(Enumerable.Range(start, count).Select(i => (double)i), sliceStart, sliceStop);
struct TestData {
public List<double> Values;
public List<int>? Removes;
public List<(int Index, double Value)>? Adds;
public int Start;
public int Stop;
public SliceList<double> Slice;
public List<double> SliceAdds = new();
public List<double> SliceRemoves = new();
public TestData(IEnumerable<double> values, int start, int stop) {
Values = values.ToList();
Start = start;
Stop = stop;
Slice = new(Values, Start, Stop);
}
public TestData OnChange(IEnumerable<double>? adds, IEnumerable<double>? removes) {
SliceAdds.Clear();
SliceRemoves.Clear();
var newValues = Values.ToList();
var newValuesBd = ImmutableList.CreateRange(Values).ToBuilder();
var removedIndices = new List<int>();
removes ??= Enumerable.Empty<double>();
adds ??= Enumerable.Empty<double>();
int n = 0;
foreach (var v in removes.Ordered()) {
int i = Values.BinarySearch(v);
removedIndices.Add(i);
newValues.RemoveAt(i - n);
newValuesBd.RemoveAt(i - n);
n++;
}
var newValuesBd2 = ImmutableList.CreateRange(Values).ToBuilder();
var addsWithIndices = new List<(int Index, double Value)>();
n = 0;
foreach (var v in adds.Ordered()) {
int i = newValues.BinarySearch(v);
if (i < 0) {
i = ~i;
}
int i2 = newValuesBd2.BinarySearch(v);
if (i2 < 0) {
i2 = ~i2;
}
newValues.Insert(i, v);
newValuesBd2.Insert(i2, v);
addsWithIndices.Add((i2 - n, v));
n++;
}
Values = newValues;
Slice = Slice.OnChange(newValues, removedIndices, addsWithIndices, ref SliceRemoves, ref SliceAdds);
return this;
}
public TestData Validate(IEnumerable<double>? expectedAdds = null, IEnumerable<double>? expectedRemoves = null, IEnumerable<double>? expectedSlice = null) {
expectedAdds ??= Enumerable.Empty<double>();
expectedRemoves ??= Enumerable.Empty<double>();
expectedSlice ??= Enumerable.Empty<double>();
Assert.SequenceEqual(Slice, expectedSlice);
Assert.SequenceEqual(SliceAdds, expectedAdds);
Assert.SequenceEqual(SliceRemoves, expectedRemoves);
return this;
}
}
}