diff --git a/src/client/spark_core/Collections/AggregateList.cs b/src/client/spark_core/Collections/AggregateList.cs new file mode 100644 index 000000000..4624c0e27 --- /dev/null +++ b/src/client/spark_core/Collections/AggregateList.cs @@ -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 Create(params IList[] lists) => + new(lists); + + public static AggregateList Create(IEnumerable> lists) => + new(lists); +} + +public class AggregateList : IMutableListFacade { + static readonly IComparer<(IList Items, int Index)> indexedListComparer = + TupleComparer.Comparer, int>(item2Ordinal: 0); + + readonly (IList Items, int Index)[] indexedLists; + + public AggregateList(IEnumerable> lists) => + indexedLists = lists.Where(l => l.Count > 0) + .AggregateEach((Items: (IList)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 GetEnumerator() => + indexedLists.SelectMany(l => l.Items).GetEnumerator(); +} diff --git a/src/client/spark_core/Collections/BlockTransaction.cs b/src/client/spark_core/Collections/BlockTransaction.cs new file mode 100644 index 000000000..3d8242c36 --- /dev/null +++ b/src/client/spark_core/Collections/BlockTransaction.cs @@ -0,0 +1,28 @@ +#nullable enable +namespace Spark.Core.Collections; + +using System.Collections.Immutable; + +/// +/// Represents a unit of change that can be applied to a list. +/// +public readonly struct BlockTransaction { + public readonly int Index; + public readonly int NumRemoved; + public readonly ImmutableList InsertedValues; + public readonly int InitialOffset; + + public BlockTransaction(int index, int initialOffset, int numRemoved = 0, ImmutableList? insertedValues = null) { + Index = index; + NumRemoved = numRemoved; + InsertedValues = insertedValues ?? ImmutableList.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} }}"; +} diff --git a/src/client/spark_core/Collections/BlockTransactionEnumerable.cs b/src/client/spark_core/Collections/BlockTransactionEnumerable.cs new file mode 100644 index 000000000..6e95e1f26 --- /dev/null +++ b/src/client/spark_core/Collections/BlockTransactionEnumerable.cs @@ -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 Create(IList source, IList sortedDeletes, IList<(int Index, T Value)> sortedAdds) => + new(source, sortedDeletes, sortedAdds); + + public static IEnumerable> ToSliceBlocks(this IEnumerable> blocks, int sourceStartIndex, int destStartIndex) { + BlockTransaction? 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 CombineDeletes(BlockTransaction tx, int numRemoved) => + new(tx.Index, tx.InitialOffset, tx.NumRemoved + numRemoved, tx.InsertedValues); + + static BlockTransaction Combine(BlockTransaction tx, int numRemoved, IEnumerable insertedValues) => + new(tx.Index, tx.InitialOffset, tx.NumRemoved + numRemoved, tx.InsertedValues.AddRange(insertedValues)); + } + + public static IEnumerable ToReducedMetadata(this IEnumerable> 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(this IList source, IList newValues, IList removes, IList<(int Index, T Value)> inserts, ref List removedValues, ref List insertedValues, int? start, int? stop) => + Create(source, removes, inserts) + .SliceDiff(source, newValues, ref removedValues, ref insertedValues, start, stop); + + public static (List Removes, List Inserts) SliceDiff(this IEnumerable> blocks, IList source, IList newValues, int? start, int? stop) { + var removes = new List(); + var inserts = new List(); + blocks.SliceDiff(source, newValues, ref removes, ref inserts, start, stop); + return (removes, inserts); + + } + public static void SliceDiff(this IEnumerable> blocks, IList source, IList newValues, ref List removes, ref List 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(this IEnumerable blocks, IList source, IList newValues, ref List sliceRemoves, ref List 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(this IEnumerable blocks, IList source, IList newValues, ref List sliceRemoves, ref List 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(); + var removes = new List(); + 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 : IEnumerable> { + readonly IList source; + // readonly IList newValues; + readonly IList sortedDeletes; + readonly IList<(int Index, T Value)> sortedAdds; + + public BlockTransactionEnumerable(IList source, IList sortedDeletes, IList<(int Index, T Value)> sortedAdds) { + this.source = source; + // this.newValues = newValues; + this.sortedDeletes = sortedDeletes; + this.sortedAdds = sortedAdds; + } + + public IEnumerator> GetEnumerator() { + // Log.assert(newValues.Count == source.Count + sortedAdds.Count - sortedDeletes.Count); + int delCursor = 0; + int addCursor = 0; + BlockTransaction? 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.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.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.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.Empty); + } + delCursor++; + } else if (add.HasValue) { + if (!block.HasValue) { + block = new(add.Value.Index, 0, 0, ImmutableList.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.Empty.Add(add.Value.Value)); + } + addCursor++; + } else { + if (block.HasValue) { + yield return block.Value; + } + break; + } + } + } + + IEnumerator IEnumerable.GetEnumerator() => + GetEnumerator(); +} diff --git a/src/client/spark_core/Collections/BlockTransactionMetadata.cs b/src/client/spark_core/Collections/BlockTransactionMetadata.cs new file mode 100644 index 000000000..1e10de67d --- /dev/null +++ b/src/client/spark_core/Collections/BlockTransactionMetadata.cs @@ -0,0 +1,25 @@ +#nullable enable +namespace Spark.Core.Collections; + +/// +/// Represents a unit of change that can be applied to a list. The only difference +/// between this struct and , is that this has the +/// number of values to insert, while the latter has the actual list of values. +/// +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; +} diff --git a/src/client/spark_core/Collections/DeltaList.cs b/src/client/spark_core/Collections/DeltaList.cs new file mode 100644 index 000000000..140e6afcd --- /dev/null +++ b/src/client/spark_core/Collections/DeltaList.cs @@ -0,0 +1,42 @@ +namespace Spark.Core.Collections; + +using System; +using System.Collections; +using System.Collections.Generic; + +public static class DeltaList { + public static DeltaList Create(IList source) => + new(source, (lhs, rhs) => lhs - rhs); + + public static DeltaList Create(IList source, Func getDelta) => + new(source, getDelta); +} + +public class DeltaList : IMutableListFacade { + readonly IList source; + readonly Func getDelta; + + public DeltaList(IList source, Func 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 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(); +} diff --git a/src/client/spark_core/Collections/DictionarySetWrapper.cs b/src/client/spark_core/Collections/DictionarySetWrapper.cs new file mode 100644 index 000000000..9da733eaa --- /dev/null +++ b/src/client/spark_core/Collections/DictionarySetWrapper.cs @@ -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 : IReadOnlySet { + readonly IDictionary source; + readonly IEqualityComparer comparer; + + public DictionarySetWrapper(IDictionary source, IEqualityComparer? comparer = null) { + this.source = source; + this.comparer = comparer ?? ImplicitComparer(source) ?? EqualityComparer.Default; + + static IEqualityComparer? ImplicitComparer(IDictionary source) => + source switch { +#nullable disable + Dictionary dct => dct.Comparer, + ImmutableDictionary idct => idct.KeyComparer, +#nullable enable + _ => null, + }; + } + + public int Count => source.Count; + + public bool Contains(TKey item) => + source.ContainsKey(item); + + public bool IsProperSubsetOf(IEnumerable other) => + isSubsetOf(other, true); + + public bool IsProperSupersetOf(IEnumerable other) { + int count = source.Count; + if (count == 0) { + return false; + } + var knownKeys = new HashSet(comparer); + foreach (var k in other) { + if (!source.ContainsKey(k) || (knownKeys.Add(k) && knownKeys.Count == count)) { + return false; + } + } + return true; + } + + /// Determines whether a object is a subset of the specified collection. + /// The collection to compare to the current object. + /// true if the object is a subset of ; otherwise, false. + public bool IsSubsetOf(IEnumerable other) => + isSubsetOf(other); + + public bool IsSupersetOf(IEnumerable other) => + other.All(source.ContainsKey); + + public bool Overlaps(IEnumerable other) => + source.Count > 0 && other.Any(source.ContainsKey); + + public bool SetEquals(IEnumerable other) { + var otherSet = new HashSet(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 Intersection(IEnumerable other) { + int count = Count; + var knownKeys = new HashSet(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 GetEnumerator() => + source.Keys.GetEnumerator(); + + IEnumerator IEnumerable.GetEnumerator() => + GetEnumerator(); + + bool isSubsetOf(IEnumerable other, bool proper = false) { + var otherSet = new HashSet(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; + } +} diff --git a/src/client/spark_core/Collections/Extensions/ArrayExtensions.cs b/src/client/spark_core/Collections/Extensions/ArrayExtensions.cs index 0e9d26d57..4d17dba1a 100644 --- a/src/client/spark_core/Collections/Extensions/ArrayExtensions.cs +++ b/src/client/spark_core/Collections/Extensions/ArrayExtensions.cs @@ -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 Reverse(this T[] array) => + new ReverseArrayList(array); + public static TResult[] SelectArray(this T[] array, Func selector) => Array.ConvertAll(array, new Converter(selector)); diff --git a/src/client/spark_core/Collections/Extensions/EnumerableExtensions.cs b/src/client/spark_core/Collections/Extensions/EnumerableExtensions.cs index 0b2ed3845..1db04d45d 100644 --- a/src/client/spark_core/Collections/Extensions/EnumerableExtensions.cs +++ b/src/client/spark_core/Collections/Extensions/EnumerableExtensions.cs @@ -18,6 +18,13 @@ namespace System.Collections.Generic { using System.Runtime.Versioning; public static class EnumerableExtensions { + public static IEnumerable AggregateEach(this IEnumerable source, TAccumulate seed, Func accumulate) { + foreach (var v in source) { + seed = accumulate(seed, v); + yield return seed; + } + } + /// /// Determines whether all elements of a sequence are true. /// @@ -369,6 +376,12 @@ namespace System.Collections.Generic { this IEnumerable source, Func selector, IEqualityComparer comparer) => source.Select(selector).ToHashSet(comparer); + public static OrderedDictionary ToOrderedDictionary(this IEnumerable source, Func keySelector, Func valueSelector) where TKey : notnull => + new(source.Select(x => new KeyValuePair(keySelector(x), valueSelector(x)))); + + public static OrderedDictionary ToOrderedDictionary(this IEnumerable> source) where TKey : notnull => + new(source); + public static bool TryGetFirst(this IEnumerable source, [MaybeNullWhen(false)]out T result) { if (source == null) throw new ArgumentNullException(nameof(source)); using (var en = source.GetEnumerator()) { diff --git a/src/client/spark_core/Collections/Extensions/ListExtensions.cs b/src/client/spark_core/Collections/Extensions/ListExtensions.cs index cac4d85fa..f05701528 100644 --- a/src/client/spark_core/Collections/Extensions/ListExtensions.cs +++ b/src/client/spark_core/Collections/Extensions/ListExtensions.cs @@ -168,6 +168,15 @@ namespace bts.collections { return true; } + public static bool RemoveSorted(this List list, T item, IComparer comparer) { + int i = list.BinarySearch(item, comparer); + if (i < 0) { + return false; + } + list.RemoveAt(i); + return true; + } + public static void RemoveSorted(this IList list, IEnumerable items) where T : IComparable => RemoveSorted(list, items, Comparer.Default); @@ -439,6 +448,9 @@ namespace bts.collections { public static IReadOnlyList WrapReverseRO(this IList list) => new ReverseWrapper(list); + public static IList Slice(this IList source, int? start = null, int? stop = null) => + new SliceList(source, start, stop); + public static IList WrapSubRange(this IList list, int start) => new SubRangeWrapper(list, start, list.Count - start); diff --git a/src/client/spark_core/Collections/IDualComparer.cs b/src/client/spark_core/Collections/IDualComparer.cs new file mode 100644 index 000000000..64ac6963d --- /dev/null +++ b/src/client/spark_core/Collections/IDualComparer.cs @@ -0,0 +1,18 @@ +#nullable enable +namespace Spark.Core.Collections; + +using System; +using System.Collections; +using System.Collections.Generic; + +public interface IDualComparer : IComparer, 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); + } +} diff --git a/src/client/spark_core/Collections/IMutableCollectionFacade.cs b/src/client/spark_core/Collections/IMutableCollectionFacade.cs new file mode 100644 index 000000000..ab05849c0 --- /dev/null +++ b/src/client/spark_core/Collections/IMutableCollectionFacade.cs @@ -0,0 +1,33 @@ +#nullable enable +namespace Spark.Core.Collections; + +using System; +using System.Collections; +using System.Collections.Generic; + +public interface IMutableCollectionFacade : ICollection { + bool ICollection.IsReadOnly => true; + + void ICollection.Add(T item) => throw new NotSupportedException(); + bool ICollection.Remove(T item) => throw new NotSupportedException(); + void ICollection.Clear() => throw new NotSupportedException(); + + bool ICollection.Contains(T item) { + var comp = EqualityComparer.Default; + foreach (var v in this) { + if (comp.Equals(v, item)) { + return true; + } + } + return false; + } + + void ICollection.CopyTo(T[] array, int arrayIndex) { + foreach (var v in this) { + array[arrayIndex++] = v; + } + } + + IEnumerator IEnumerable.GetEnumerator() => + GetEnumerator(); +} diff --git a/src/client/spark_core/Collections/IMutableListFacade.cs b/src/client/spark_core/Collections/IMutableListFacade.cs new file mode 100644 index 000000000..ff2aec599 --- /dev/null +++ b/src/client/spark_core/Collections/IMutableListFacade.cs @@ -0,0 +1,25 @@ +#nullable enable +namespace Spark.Core.Collections; + +using System; +using System.Collections.Generic; + +public interface IMutableListFacade : IReadOnlyList, IList, IMutableCollectionFacade { + T IList.this[int index] { + get => ((IReadOnlyList)this)[index]; + set => throw new NotSupportedException(); + } + void IList.Insert(int index, T item) => throw new NotSupportedException(); + void IList.RemoveAt(int index) => throw new NotImplementedException(); + int IList.IndexOf(T item) { + var comp = EqualityComparer.Default; + int idx = 0; + foreach (var v in this) { + if (comp.Equals(v, item)) { + return idx; + } + idx++; + } + return -1; + } +} diff --git a/src/client/spark_core/Collections/ImmutableOrderedDictionary.cs b/src/client/spark_core/Collections/ImmutableOrderedDictionary.cs new file mode 100644 index 000000000..302312edc --- /dev/null +++ b/src/client/spark_core/Collections/ImmutableOrderedDictionary.cs @@ -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 Create(IEqualityComparer? comparer = null) where TKey : notnull => + new(comparer: comparer); + + public static ImmutableOrderedDictionary CreateRange(IEnumerable> items) where TKey : notnull => + new ImmutableOrderedDictionary().AddRange(items); + + public static ImmutableOrderedDictionary CreateRange(IEqualityComparer? comparer, IEnumerable> items) where TKey : notnull => + new ImmutableOrderedDictionary(comparer).AddRange(items); +} + +public class ImmutableOrderedDictionary : IDictionary, IList> where TKey : notnull { + public static readonly ImmutableOrderedDictionary 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 data; + readonly ImmutableSortedSet<(ulong Ordinal, TKey Key)> ordinalKeys; + readonly IList orderedKeys; + readonly SelectorList orderedValues; + + public ImmutableOrderedDictionary(IEqualityComparer? comparer = null) + : this(ImmutableDictionary.Create(comparer), ImmutableSortedSet.Create<(ulong, TKey)>(TupleComparer.Comparer(0, null))) { } + + ImmutableOrderedDictionary(ImmutableDictionary data, ImmutableSortedSet<(ulong Ordinal, TKey Key)> ordinalKeys) { + this.data = data; + this.ordinalKeys = ordinalKeys; + orderedKeys = ordinalKeys.WrapSelector(p => p.Key, null); + orderedValues = new SelectorList(orderedKeys, k => data[k].Value, null); + OrdinalGaps = DeltaList.Create(ordinalKeys.WrapSelector(p => p.Ordinal, null), (x, y) => checked(x - y)); + } + + public IEqualityComparer Comparer => data.KeyComparer; + + public TValue this[TKey key] => data[key].Value; + + public IList Keys => orderedKeys; + public IList Values => orderedValues; + + public int Count => ordinalKeys.Count; + + public bool IsReadOnly => false; + + public IList OrdinalGaps { get; } + + ICollection IDictionary.Keys => orderedKeys; + ICollection IDictionary.Values => orderedValues; + + ulong? firstOrdinal => + ordinalKeys.Count == 0 ? null : ordinalKeys[0].Ordinal; + + ulong? lastOrdinal => + ordinalKeys.Count == 0 ? null : ordinalKeys[^1].Ordinal; + + TValue IDictionary.this[TKey key] { + get => this[key]; + set => throw new NotSupportedException(); + } + + KeyValuePair IList>.this[int index] { + get { + var k = ordinalKeys[index].Key; + return new(k, data[k].Value); + } + set => throw new NotSupportedException(); + } + + public IList> AsList() => + this; + + public ImmutableOrderedDictionary 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[] { new(key, value) })); + } else { + return new(data.Add(key, (value, lastOrd + step)), ordinalKeys.Add((lastOrd + step, key))); + } + } + + public ImmutableOrderedDictionary Add(KeyValuePair item) => + Add(item.Key, item.Value); + + public ImmutableOrderedDictionary AddRange(IEnumerable> items) { + if (items is null) { + return this; + } + var itemList = items.Where(kv => !data.ContainsKey(kv.Key)) + .Distinct(KeyValuePairEqualityComparer.KeyComparer(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 GetDatum(KeyValuePair kv, int i) => + new(kv.Key, (kv.Value, GetOrdinal(i))); + + (ulong Ordinal, TKey Key) GetOrdinalKey(KeyValuePair kv, int i) => + (GetOrdinal(i), kv.Key); + } + } + + public ImmutableOrderedDictionary InsertRange(int index, IEnumerable> 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(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 Clear() => + new(data.Clear(), ordinalKeys.Clear()); + + public bool Contains(KeyValuePair item) => + data.TryGetValue(item.Key, out var v) && EqualityComparer.Default.Equals(v.Value, item.Value); + + public bool ContainsKey(TKey key) => + data.ContainsKey(key); + + public void CopyTo(KeyValuePair[] array, int arrayIndex) { + foreach (var (ord, k) in ordinalKeys) { + array[arrayIndex++] = new(k, data[k].Value); + } + } + + public int IndexOf(KeyValuePair item) => + data.TryGetValue(item.Key, out var v) && EqualityComparer.Default.Equals(v.Value, item.Value) + ? ordinalKeys.IndexOf((v.Ordinal, item.Key)) + : -1; + + public ImmutableOrderedDictionary Insert(int index, KeyValuePair item) => + InsertRange(index, new[] { item }); + + public ImmutableOrderedDictionary Remove(TKey key) => + data.TryGetValue(key, out var v) + ? new(data.Remove(key), ordinalKeys.Remove((v.Ordinal, key))) + : this; + + public ImmutableOrderedDictionary Remove(KeyValuePair item) => + data.TryGetValue(item.Key, out var v) && EqualityComparer.Default.Equals(v.Value, item.Value) + ? new(data.Remove(item.Key), ordinalKeys.Remove((v.Ordinal, item.Key))) + : this; + + public ImmutableOrderedDictionary RemoveAt(int index) { + var (ord, key) = ordinalKeys[index]; + return new(data.Remove(key), ordinalKeys.Remove((ord, key))); + } + + public ImmutableOrderedDictionary 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[] { new(key, value) })); + } + return new(data.Add(key, (value, itemOrd.Value)), ordinalKeys.Add((itemOrd.Value, key))); + } + } + + public ImmutableOrderedDictionary SetItem(int index, KeyValuePair 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> 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.Add(TKey key, TValue value) => + throw new NotSupportedException(); + + void ICollection>.Add(KeyValuePair item) => + throw new NotSupportedException(); + + void ICollection>.Clear() => + throw new NotSupportedException(); + + void IList>.Insert(int index, KeyValuePair item) => + throw new NotSupportedException(); + + bool ICollection>.Remove(KeyValuePair item) => + throw new NotSupportedException(); + + bool IDictionary.Remove(TKey key) => + throw new NotSupportedException(); + + void IList>.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: should already be unique (this fn does NOT check) + ImmutableOrderedDictionary reset(IEnumerable>? items) { + if (items is null) { + return Clear(); + } + var itemList = items as IList> ?? 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, IList> { + readonly ImmutableDictionary.Builder data; + readonly ImmutableSortedSet<(ulong Ordinal, TKey Key)>.Builder ordinalKeys; + readonly IList orderedKeys; + readonly SelectorList orderedValues; + + public Builder(ImmutableDictionary 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(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 Keys => orderedKeys; + public IList Values => orderedValues; + + public IList OrdinalGaps { get; } + + ICollection IDictionary.Keys => orderedKeys; + ICollection IDictionary.Values => orderedValues; + + ulong? firstOrdinal => + ordinalKeys.Count == 0 ? null : ordinalKeys[0].Ordinal; + + ulong? lastOrdinal => + ordinalKeys.Count == 0 ? null : ordinalKeys[^1].Ordinal; + + TValue IDictionary.this[TKey key] { + get => this[key]; + set => throw new NotSupportedException(); + } + + KeyValuePair IList>.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[] { new(key, value) })); + } else { + data.Add(key, (value, lastOrd + step)); + ordinalKeys.Add((lastOrd + step, key)); + } + } + + public void Add(KeyValuePair item) => + Add(item.Key, item.Value); + + public void AddRange(IEnumerable> items) { + if (items is null) { + return; + } + var itemList = items as IList> ?? 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 GetDatum(KeyValuePair kv, int i) => + new(kv.Key, (kv.Value, GetOrdinal(i))); + + (ulong Ordinal, TKey Key) GetOrdinalKey(KeyValuePair kv, int i) => + (GetOrdinal(i), kv.Key); + } + } + + public void InsertRange(int index, IEnumerable> items) { + if (index == Count) { + AddRange(items); + return; + } + if (index < 0 || index > Count) { + throw new IndexOutOfRangeException(); + } + var itemList = items as IList> ?? 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 item) => + data.TryGetValue(item.Key, out var v) && EqualityComparer.Default.Equals(v.Value, item.Value); + + public bool ContainsKey(TKey key) => + data.ContainsKey(key); + + public void CopyTo(KeyValuePair[] array, int arrayIndex) { + foreach (var (ord, k) in ordinalKeys) { + array[arrayIndex++] = new(k, data[k].Value); + } + } + + public int IndexOf(KeyValuePair item) => + data.TryGetValue(item.Key, out var v) && EqualityComparer.Default.Equals(v.Value, item.Value) + ? ordinalKeys.IndexOf((v.Ordinal, item.Key)) + : -1; + + public void Insert(int index, KeyValuePair 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 item) { + if (data.TryGetValue(item.Key, out var v) && EqualityComparer.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>? items) { + if (items is null) { + Clear(); + return; + } + var itemList = items as IList> ?? 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 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> 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"); + } + } + } +} diff --git a/src/client/spark_core/Collections/ImmutableOrderedSet.cs b/src/client/spark_core/Collections/ImmutableOrderedSet.cs new file mode 100644 index 000000000..140856c90 --- /dev/null +++ b/src/client/spark_core/Collections/ImmutableOrderedSet.cs @@ -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 Create(IEqualityComparer? comparer = null) where T : notnull => + new(comparer: comparer); + + public static ImmutableOrderedSet CreateRange(IEnumerable items) where T : notnull => + new ImmutableOrderedSet().AddRange(items); + + public static ImmutableOrderedSet CreateRange(IEqualityComparer? comparer, IEnumerable items) where T : notnull => + new ImmutableOrderedSet(comparer).AddRange(items); +} + +public class ImmutableOrderedSet : IReadOnlySet, IList where T : notnull { + public static readonly ImmutableOrderedSet 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 data; + readonly ImmutableSortedSet<(ulong Ordinal, T Key)> ordinalKeys; + readonly IList orderedKeys; + readonly DictionarySetWrapper keySet; + + public ImmutableOrderedSet(IEqualityComparer? comparer = null) + : this(ImmutableDictionary.Create(comparer), ImmutableSortedSet.Create<(ulong, T)>(TupleComparer.Comparer(0, null))) { } + + ImmutableOrderedSet(ImmutableDictionary 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 Comparer => data.KeyComparer; + + public IList Keys => orderedKeys; + + public int Count => ordinalKeys.Count; + + public bool IsReadOnly => false; + + public IList 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 AsList() => + this; + + public ImmutableOrderedSet 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 AddRange(IEnumerable items) { + if (items is null) { + return this; + } + var itemList = items as IList ?? 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 GetDatum(T item, int i) => + new(item, GetOrdinal(i)); + + (ulong Ordinal, T Key) GetOrdinalKey(T item, int i) => + (GetOrdinal(i), item); + } + } + + public ImmutableOrderedSet 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 Insert(int index, T item) => + InsertRange(index, new[] { item }); + + public ImmutableOrderedSet InsertRange(int index, IEnumerable 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 other) => + keySet.IsProperSubsetOf(other); + + public bool IsProperSupersetOf(IEnumerable other) => + keySet.IsProperSupersetOf(other); + + /// Determines whether a object is a subset of the specified collection. + /// The collection to compare to the current object. + /// true if the object is a subset of ; otherwise, false. + public bool IsSubsetOf(IEnumerable other) => + keySet.IsSubsetOf(other); + + public bool IsSupersetOf(IEnumerable other) => + keySet.IsSupersetOf(other); + + public bool Overlaps(IEnumerable other) => + keySet.Overlaps(other); + + public ImmutableOrderedSet Remove(T key) => + data.TryGetValue(key, out var ord) + ? new(data.Remove(key), ordinalKeys.Remove((ord, key))) + : this; + + public ImmutableOrderedSet RemoveAt(int index) { + var (ord, key) = ordinalKeys[index]; + return new(data.Remove(key), ordinalKeys.Remove((ord, key))); + } + + public bool SetEquals(IEnumerable other) => + keySet.SetEquals(other); + + public ImmutableOrderedSet 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 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.Add(T item) => + throw new NotSupportedException(); + + void ICollection.Clear() => + throw new NotSupportedException(); + + void IList.Insert(int index, T item) => + throw new NotSupportedException(); + + bool ICollection.Remove(T item) => + throw new NotSupportedException(); + + void IList.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: should already be distinct (this fn does NOT check) + ImmutableOrderedSet reset(IEnumerable? items) { + if (items is null) { + return Clear(); + } + var itemList = items as IList ?? 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, IReadOnlySet, IList { + readonly ImmutableDictionary.Builder data; + readonly ImmutableSortedSet<(ulong Ordinal, T Key)>.Builder ordinalKeys; + readonly IList orderedKeys; + readonly DictionarySetWrapper keySet; + + public Builder(ImmutableDictionary 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 Comparer => data.KeyComparer; + + public int Count => ordinalKeys.Count; + + public bool IsReadOnly => false; + + public IList Keys => orderedKeys; + + public IList OrdinalGaps { get; } + + ulong? firstOrdinal => + ordinalKeys.Count == 0 ? null : ordinalKeys[0].Ordinal; + + ulong? lastOrdinal => + ordinalKeys.Count == 0 ? null : ordinalKeys[^1].Ordinal; + + T IList.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 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 GetDatum(T item, int i) => + new(item, GetOrdinal(i)); + + (ulong Ordinal, T Key) GetOrdinalKey(T item, int i) => + (GetOrdinal(i), item); + } + } + + public IList AsList() => + this; + + public void InsertRange(int index, IEnumerable items) { + if (index == Count) { + AddRange(items); + return; + } + if (index < 0 || index > Count) { + throw new IndexOutOfRangeException(); + } + var itemList = items as IList ?? 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 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 other) { + if (Count > 0) { + reset(other.Where(data.ContainsKey).Distinct(Comparer).ToList()); + } + } + + public bool IsProperSubsetOf(IEnumerable other) => + keySet.IsProperSubsetOf(other); + + public bool IsProperSupersetOf(IEnumerable other) => + keySet.IsProperSupersetOf(other); + + /// Determines whether a object is a subset of the specified collection. + /// The collection to compare to the current object. + /// true if the object is a subset of ; otherwise, false. + public bool IsSubsetOf(IEnumerable other) => + keySet.IsSubsetOf(other); + + public bool IsSupersetOf(IEnumerable other) => + keySet.IsSupersetOf(other); + + public bool Overlaps(IEnumerable 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 other) => + keySet.SetEquals(other); + + public void SymmetricExceptWith(IEnumerable other) { + if (Count == 0) { + reset(other.Distinct(Comparer).ToList()); + return; + } + foreach (var v in other) { + if (!Remove(v)) { + Add(v); + } + } + } + + public ImmutableOrderedSet 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 other) => + AddRange(other); + + public IEnumerator GetEnumerator() => + orderedKeys.GetEnumerator(); + + void ICollection.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: should already be distinct (this fn does NOT check) + void reset(IEnumerable? items) { + if (items is null) { + Clear(); + return; + } + var itemList = items as IList ?? 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; + } + } + } +} diff --git a/src/client/spark_core/Collections/IndexDependentSelectorList.cs b/src/client/spark_core/Collections/IndexDependentSelectorList.cs new file mode 100644 index 000000000..7fed40b5c --- /dev/null +++ b/src/client/spark_core/Collections/IndexDependentSelectorList.cs @@ -0,0 +1,32 @@ +#nullable enable +namespace Spark.Core.Collections; + +using System; +using System.Collections.Generic; + +public static class IndexDependentSelectorList { + public static IndexDependentSelectorList Create(IList source, Func selector) => + new(source, selector); +} + +public class IndexDependentSelectorList : IMutableListFacade { + readonly IList source; + readonly Func selector; + + public IndexDependentSelectorList(IList source, Func selector) { + this.source = source; + this.selector = selector; + } + + public TResult this[int index] => selector(source[index], index); + + public int Count => source.Count; + + public IEnumerator GetEnumerator() { + int i = 0; + foreach (var v in source) { + yield return selector(v, i); + i++; + } + } +} diff --git a/src/client/spark_core/Collections/KeyValuePairComparer.cs b/src/client/spark_core/Collections/KeyValuePairComparer.cs new file mode 100644 index 000000000..9ebef284b --- /dev/null +++ b/src/client/spark_core/Collections/KeyValuePairComparer.cs @@ -0,0 +1,36 @@ +namespace Spark.Core.Collections; + +using System.Collections.Generic; + +public static class KeyValuePairComparer { + public static IComparer> KeyComparer() => + KeyComparerImpl.Instance; + + public static IComparer> KeyComparer(IComparer comparer) => + new KeyComparerImpl(comparer); + + public static IComparer> ValueComparer() => + ValueComparerImpl.Instance; + + class KeyComparerImpl : IComparer> { + public static readonly IComparer> Instance = new KeyComparerImpl(); + + readonly IComparer comparer; + + public KeyComparerImpl() + : this(Comparer.Default) { } + + public KeyComparerImpl(IComparer comparer) => + this.comparer = comparer; + + public int Compare(KeyValuePair x, KeyValuePair y) => + comparer.Compare(x.Key, y.Key); + } + + class ValueComparerImpl : IComparer> { + public static readonly IComparer> Instance = new KeyComparerImpl(); + + public int Compare(KeyValuePair x, KeyValuePair y) => + Comparer.Default.Compare(x.Value, y.Value); + } +} diff --git a/src/client/spark_core/Collections/KeyValuePairEqualityComparer.cs b/src/client/spark_core/Collections/KeyValuePairEqualityComparer.cs new file mode 100644 index 000000000..bacba8105 --- /dev/null +++ b/src/client/spark_core/Collections/KeyValuePairEqualityComparer.cs @@ -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> KeyComparer() => + KeyComparerImpl.Instance; + + public static IEqualityComparer> KeyComparer(IEqualityComparer? comparer) => + new KeyComparerImpl(comparer); + + public static IEqualityComparer> ValueComparer() => + ValueComparerImpl.Instance; + + class KeyComparerImpl : IEqualityComparer> { + public static readonly IEqualityComparer> Instance = new KeyComparerImpl(); + + readonly IEqualityComparer comparer; + + public KeyComparerImpl() + : this(EqualityComparer.Default) { } + + public KeyComparerImpl(IEqualityComparer? comparer) => + this.comparer = comparer ?? EqualityComparer.Default; + + public bool Equals(KeyValuePair x, KeyValuePair y) => + comparer.Equals(x.Key, y.Key); + + public int GetHashCode([DisallowNull] KeyValuePair obj) => + comparer.GetHashCode(obj.Key!); + } + + class ValueComparerImpl : IEqualityComparer> { + public static readonly IEqualityComparer> Instance = new KeyComparerImpl(); + + readonly IEqualityComparer comparer; + + public ValueComparerImpl() + : this(EqualityComparer.Default) { } + + public ValueComparerImpl(IEqualityComparer? comparer) => + this.comparer = comparer ?? EqualityComparer.Default; + + public bool Equals(KeyValuePair x, KeyValuePair y) => + comparer.Equals(x.Value, y.Value); + + public int GetHashCode([DisallowNull] KeyValuePair obj) => + comparer.GetHashCode(obj.Value!); + } +} diff --git a/src/client/spark_core/Collections/MergedSortedList.cs b/src/client/spark_core/Collections/MergedSortedList.cs new file mode 100644 index 000000000..57508a110 --- /dev/null +++ b/src/client/spark_core/Collections/MergedSortedList.cs @@ -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 Create(IList list1, IList list2) => + new(list1, list2, Comparer.Default); + + public static MergedSortedList Create(IList list1, IList list2, IComparer comparer) => + new(list1, list2, comparer); +} + +public class MergedSortedList : IMutableListFacade { + readonly IList list1; + readonly IList list2; + readonly IList[] lists; + readonly IComparer comparer; + + public MergedSortedList(IList list1, IList list2, IComparer 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 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(); +} diff --git a/src/client/spark_core/Collections/OrderedDictionary.cs b/src/client/spark_core/Collections/OrderedDictionary.cs new file mode 100644 index 000000000..e2c992112 --- /dev/null +++ b/src/client/spark_core/Collections/OrderedDictionary.cs @@ -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 : IDictionary, IList> 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 data; + readonly List<(ulong Ordinal, TKey Key)> ordinalKeys; + readonly IList orderedKeys; + readonly SelectorList orderedValues; + readonly IComparer<(ulong Ordinal, TKey Key)> ordinalKeyComparer = TupleComparer.Comparer(item1Ordinal: 0).AsComparer(); + + public OrderedDictionary() + : this(0, null, null) { } + + public OrderedDictionary(IEqualityComparer? comparer) + : this(4, comparer, null) { } + + public OrderedDictionary(IEqualityComparer? comparer, IEnumerable>? items) + : this(items?.SoftCount() ?? 4, comparer, items) { } + + public OrderedDictionary(IEnumerable>? items) + : this(items?.SoftCount() ?? 4, null, items) { } + + public OrderedDictionary(int capacity, IEqualityComparer? comparer, IEnumerable>? items) { + data = new(capacity, comparer); + ordinalKeys = new(); + orderedKeys = ordinalKeys.WrapSelector(tup => tup.Key, null); + orderedValues = new SelectorList(Keys, k => data[k].Value, null); + if (items is not null) { + AddRange(items); + } + } + + public IEqualityComparer Comparer => data.Comparer; + + public TValue this[TKey key] { + get => data[key].Value; + set => SetItem(key, value); + } + + public IList Keys => orderedKeys; + public IList Values => orderedValues; + + public int Count => ordinalKeys.Count; + + public bool IsReadOnly => false; + + ICollection IDictionary.Keys => orderedKeys; + ICollection IDictionary.Values => orderedValues; + + KeyValuePair IList>.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[] { new(key, value) }); + } else { + data.Add(key, (value, lastOrd + step)); + ordinalKeys.Add((lastOrd + step, key)); + } + } + + public void Add(KeyValuePair item) => + Add(item.Key, item.Value); + + public void AddRange(IEnumerable> items) { + if (items is null) { + return; + } + var itemList = items.Where(kv => !data.ContainsKey(kv.Key)) + .Distinct(KeyValuePairEqualityComparer.KeyComparer(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 GetDatum(KeyValuePair kv, int i) => + new(kv.Key, (kv.Value, GetOrdinal(i))); + + (ulong Ordinal, TKey Key) GetOrdinalKey(KeyValuePair kv, int i) => + (GetOrdinal(i), kv.Key); + } + } + + public IList> AsList() => + this; + + public void Clear() { + data.Clear(); + orderedKeys.Clear(); + } + + public bool Contains(KeyValuePair item) => + data.TryGetValue(item.Key, out var v) && EqualityComparer.Default.Equals(v.Value, item.Value); + + public bool ContainsKey(TKey key) => + data.ContainsKey(key); + + public void CopyTo(KeyValuePair[] 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 item) { + if (data.TryGetValue(item.Key, out var v) && EqualityComparer.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 item) => + InsertRange(index, new[] { item }); + + public void InsertRange(int index, IEnumerable> 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(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 item) { + if (data.TryGetValue(item.Key, out var v) && EqualityComparer.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[] { 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> 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>? concatenatedItems, bool checkDuplicates = true) => + balanceAndInsertRange(Count, concatenatedItems, checkDuplicates); + + void balanceAndInsertRange(int index, IEnumerable>? 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(Comparer)); + } + var insertedItemList = insertedItems as IList> ?? 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: should already be unique (this fn does NOT check) + void reset(IEnumerable>? items) { + Clear(); + balanceAndAddRange(items); + } +} diff --git a/src/client/spark_core/Collections/ReverseArrayList.cs b/src/client/spark_core/Collections/ReverseArrayList.cs new file mode 100644 index 000000000..6369f659a --- /dev/null +++ b/src/client/spark_core/Collections/ReverseArrayList.cs @@ -0,0 +1,65 @@ +namespace Spark.Core.Collections; + +using System; +using System.Collections; +using System.Collections.Generic; +using System.Linq; + +public class ReverseArrayList : IList { + 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 GetEnumerator() { + for (int i = source.Length - 1; i > -1; i--) { + yield return source[i]; + } + } + + public int IndexOf(T item) { + var comp = EqualityComparer.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(); +} diff --git a/src/client/spark_core/Collections/SliceList.cs b/src/client/spark_core/Collections/SliceList.cs new file mode 100644 index 000000000..ce5b4480e --- /dev/null +++ b/src/client/spark_core/Collections/SliceList.cs @@ -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 Create(IList 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); +} + +/// +/// Represents a continuous subsection of a list. +/// +public class SliceList : IMutableListFacade { + readonly IList source; + readonly int? start; + readonly int? stop; + + public SliceList(IList 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 OnChange(IList newItems, IList removedItems, IList addedItems, out List sliceRemoves, out List sliceInserts) { + var sortedRemoves = new List(); + 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 OnChange(IList newValues, IList sortedRemoves, IList<(int Index, T Value)> sortedAdds, ref List sliceRemoves, ref List sliceInserts) { + BlockTransactionEnumerable.Create(source, sortedRemoves, sortedAdds) + .SliceDiff(source, newValues, ref sliceRemoves, ref sliceInserts, start, stop); + return new(newValues, start, stop); + } + + public IEnumerator GetEnumerator() { + var count = Count; + for (int i = 0; i < count; i++) { + yield return source[startIndex + i]; + } + } +} diff --git a/src/client/spark_core/Collections/StepList.cs b/src/client/spark_core/Collections/StepList.cs new file mode 100644 index 000000000..bcb62313d --- /dev/null +++ b/src/client/spark_core/Collections/StepList.cs @@ -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 : IMutableListFacade where T : notnull, INumberBase { + 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.convert(index); + + public int Count => count; + + public IEnumerator GetEnumerator() { + for (int i = 0; i < count; i++) { + yield return origin + step * NumericCastConverter.convert(i); + } + } + + IEnumerator IEnumerable.GetEnumerator() { + return GetEnumerator(); + } +} diff --git a/src/client/spark_core/Collections/TupleComparer.cs b/src/client/spark_core/Collections/TupleComparer.cs new file mode 100644 index 000000000..fad1a0d65 --- /dev/null +++ b/src/client/spark_core/Collections/TupleComparer.cs @@ -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() => + new ComparerBuilder<(T1, T2)>() { + t => t.Item1, + t => t.Item2, + }.ToDualComparer(); + + public static IDualComparer<(T1, T2)> Comparer(int? item1Ordinal = null, int? item2Ordinal = null) { + var comps = new List>>(); + if (item1Ordinal.HasValue) { + comps.Add(new(item1Ordinal.Value, CompareUtils.wrap(Comparer.Default, t => t.Item1))); + } + if (item2Ordinal.HasValue) { + comps.Add(new(item2Ordinal.Value, CompareUtils.wrap(Comparer.Default, t => t.Item2))); + } + comps.Sort(KeyValuePairComparer.KeyComparer>()); + var bd = new ComparerBuilder<(T1, T2)>(); + foreach (var (_, comp) in comps) { + bd.Add(comp); + } + return bd.ToDualComparer(); + } + + public static IDualComparer<(T1, T2)> Comparer(int? item1Ordinal, int? item2Ordinal, IComparer? comparer1 = null, IComparer? comparer2 = null) { + var comps = new List>>(); + if (item1Ordinal.HasValue) { + comps.Add(new(item1Ordinal.Value, CompareUtils.wrap(comparer1 ?? Comparer.Default, t => t.Item1))); + } + if (item2Ordinal.HasValue) { + comps.Add(new(item2Ordinal.Value, CompareUtils.wrap(comparer2 ?? Comparer.Default, t => t.Item2))); + } + comps.Sort(KeyValuePairComparer.KeyComparer>()); + var bd = new ComparerBuilder<(T1, T2)>(); + foreach (var (_, comp) in comps) { + bd.Add(comp); + } + return bd.ToDualComparer(); + } + + public static IDualComparer<(T1, T2, T3)> Comparer(int? item1Ordinal = null, int? item2Ordinal = null, int? item3Ordinal = null) { + var comps = new List>>(); + if (item1Ordinal.HasValue) { + comps.Add(new(item1Ordinal.Value, CompareUtils.wrap(Comparer.Default, t => t.Item1))); + } + if (item2Ordinal.HasValue) { + comps.Add(new(item2Ordinal.Value, CompareUtils.wrap(Comparer.Default, t => t.Item2))); + } + if (item3Ordinal.HasValue) { + comps.Add(new(item3Ordinal.Value, CompareUtils.wrap(Comparer.Default, t => t.Item3))); + } + comps.Sort(KeyValuePairComparer.KeyComparer>()); + 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(int? item1Ordinal = null, int? item2Ordinal = null, int? item3Ordinal = null, int? item4Ordinal = null) { + var comps = new List>>(); + if (item1Ordinal.HasValue) { + comps.Add(new(item1Ordinal.Value, CompareUtils.wrap(Comparer.Default, t => t.Item1))); + } + if (item2Ordinal.HasValue) { + comps.Add(new(item2Ordinal.Value, CompareUtils.wrap(Comparer.Default, t => t.Item2))); + } + if (item3Ordinal.HasValue) { + comps.Add(new(item3Ordinal.Value, CompareUtils.wrap(Comparer.Default, t => t.Item3))); + } + if (item4Ordinal.HasValue) { + comps.Add(new(item4Ordinal.Value, CompareUtils.wrap(Comparer.Default, t => t.Item4))); + } + comps.Sort(KeyValuePairComparer.KeyComparer>()); + var bd = new ComparerBuilder<(T1, T2, T3, T4)>(); + foreach (var (_, comp) in comps) { + bd.Add(comp); + } + return bd.ToDualComparer(); + } +} diff --git a/src/client/spark_core/utils/CollectionHelper.cs b/src/client/spark_core/utils/CollectionHelper.cs index f38594b94..758201c7e 100644 --- a/src/client/spark_core/utils/CollectionHelper.cs +++ b/src/client/spark_core/utils/CollectionHelper.cs @@ -466,5 +466,98 @@ namespace bts.utils { } return -1; } + + /// + /// Gets coordinates for an index in the imaginary sorted list that results from merging + /// and . The "X" coordinate is either 0 + /// (list1) or 1 (list2), and the "Y" coordinate is the index in that list. + /// + /// The type of elements in the list + /// One of two lists making up the imaginary sorted merged list + /// The index in the imaginary sorted merged list for which to get coordinates + /// One of two lists making up the imaginary sorted merged list + /// The comparer by which the elements are sorted + /// + /// + /// + public static (int X, int Y) BinarySearchMergedIndex(IList list1, int index, IList list2, IComparer 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 list1, int index, IList list2, IComparer 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(IList list, T value, IComparer comparer) { + int rv = list.BinarySearch(value, comparer); + if (rv < 0) { + rv = ~rv; + } + return rv; + } } } diff --git a/src/client/spark_core/utils/ComparerBuilder.cs b/src/client/spark_core/utils/ComparerBuilder.cs index 9b65ee632..de3dbc1ae 100644 --- a/src/client/spark_core/utils/ComparerBuilder.cs +++ b/src/client/spark_core/utils/ComparerBuilder.cs @@ -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 to_comparer() => + _comparers.Count switch { + 0 => Comparer.Default, + 1 => _comparers[0], + _ => new _Comparer(_comparers.ToArray()), + }; + + public IDualComparer ToDualComparer() => new _Comparer(_comparers.ToArray()); public ComparerBuilder append(Func selector) => @@ -115,7 +124,7 @@ namespace mbts.utils { IEnumerator IEnumerable.GetEnumerator() => _comparers.GetEnumerator(); - class _Comparer : IComparer { + class _Comparer : IDualComparer { readonly IComparer[] _comparers; public _Comparer(IComparer[] comparers) => diff --git a/src/client/spark_test/Collections/ImmutableOrderedDictionaryTests.cs b/src/client/spark_test/Collections/ImmutableOrderedDictionaryTests.cs new file mode 100644 index 000000000..b361aa317 --- /dev/null +++ b/src/client/spark_test/Collections/ImmutableOrderedDictionaryTests.cs @@ -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> basicItems1() { + yield return new("jane", 18); + yield return new("wayne", 19); + yield return new("foo", 8); + yield return new("bar", 9); + } + + static IEnumerable> basicItems2() { + yield return new("salt", 5); + yield return new("zoo", 100); + yield return new("pepper", 29); + yield return new("boo", 317); + } + + static IEnumerable> staggeredItems(IEnumerable> items1, IEnumerable> items2) { + foreach (var (item1, item2) in items1.Zip(items2)) { + yield return item1; + yield return item2; + } + } + + static IEnumerable> 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.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.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.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(IEnumerable items1, IEnumerable items2) { + var comp = EqualityComparer.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; + } +} diff --git a/src/client/spark_test/Collections/MergedSortedListTests.cs b/src/client/spark_test/Collections/MergedSortedListTests.cs new file mode 100644 index 000000000..0cfb05b73 --- /dev/null +++ b/src/client/spark_test/Collections/MergedSortedListTests.cs @@ -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() { + 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 list1, IList list2) { + Impl(list1, list2); + Impl(list2, list1); + + static void Impl(IList list1, IList 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(); + 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); + } + } +} diff --git a/src/client/spark_test/Collections/OrderedDictionaryTests.cs b/src/client/spark_test/Collections/OrderedDictionaryTests.cs new file mode 100644 index 000000000..90a479c58 --- /dev/null +++ b/src/client/spark_test/Collections/OrderedDictionaryTests.cs @@ -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> basicItems1() { + yield return new("jane", 18); + yield return new("wayne", 19); + yield return new("foo", 8); + yield return new("bar", 9); + } + + static IEnumerable> basicItems2() { + yield return new("salt", 5); + yield return new("zoo", 100); + yield return new("pepper", 29); + yield return new("boo", 317); + } + + static IEnumerable> staggeredItems(IEnumerable> items1, IEnumerable> items2) { + foreach (var (item1, item2) in items1.Zip(items2)) { + yield return item1; + yield return item2; + } + } + + static IEnumerable> 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(items); + Assert.SequenceEqual(od, items); + od = new OrderedDictionary(); + 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(items); + Assert.SequenceEqual(od, items); + od = new OrderedDictionary(); + 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(); + 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(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)..]); + } + } +} diff --git a/src/client/spark_test/Collections/SliceListTests.cs b/src/client/spark_test/Collections/SliceListTests.cs new file mode 100644 index 000000000..2db757b11 --- /dev/null +++ b/src/client/spark_test/Collections/SliceListTests.cs @@ -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 Values; + public List? Removes; + public List<(int Index, double Value)>? Adds; + public int Start; + public int Stop; + public SliceList Slice; + public List SliceAdds = new(); + public List SliceRemoves = new(); + + public TestData(IEnumerable values, int start, int stop) { + Values = values.ToList(); + Start = start; + Stop = stop; + Slice = new(Values, Start, Stop); + } + + public TestData OnChange(IEnumerable? adds, IEnumerable? removes) { + SliceAdds.Clear(); + SliceRemoves.Clear(); + var newValues = Values.ToList(); + var newValuesBd = ImmutableList.CreateRange(Values).ToBuilder(); + var removedIndices = new List(); + removes ??= Enumerable.Empty(); + adds ??= Enumerable.Empty(); + 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? expectedAdds = null, IEnumerable? expectedRemoves = null, IEnumerable? expectedSlice = null) { + expectedAdds ??= Enumerable.Empty(); + expectedRemoves ??= Enumerable.Empty(); + expectedSlice ??= Enumerable.Empty(); + Assert.SequenceEqual(Slice, expectedSlice); + Assert.SequenceEqual(SliceAdds, expectedAdds); + Assert.SequenceEqual(SliceRemoves, expectedRemoves); + return this; + } + } +}