diff --git a/src/DataStreamSuite/DataStream/BlueOrb/SliceStream.cs b/src/DataStreamSuite/DataStream/BlueOrb/SliceStream.cs new file mode 100644 index 000000000..53b866c9a --- /dev/null +++ b/src/DataStreamSuite/DataStream/BlueOrb/SliceStream.cs @@ -0,0 +1,33 @@ +namespace bts.BlueOrb; + +using BCG; + +using Bts.DataStream; +using Bts.DataStreamCore; + +using Spark.Core.Collections; + +using System; +using System.Collections.Generic; +using System.Reactive.Linq; + +[DataStreamUserRespresentation("slice")] +public class SliceStream : ViewStream { + public SliceStream(ViewStream inputStream, int? start, int? stop) { + mySetRealSource(Observable.Create(Subscribe).AsViewStream()); + + IDisposable Subscribe(IObserver o) { + var rows = new SliceList(new List(0), start, stop); + return inputStream.Subscribe(OnData); + + void OnData(IDataPacket dp) { + rows = rows.OnChange(dp.newRows, dp.deletedRows, dp.addedRows, out var sliceDeletes, out var sliceAdds); + var dpOut = new DataPacket( + new BlueTableListWrapper(dp.Schema, rows), + new BlueTableListWrapper(dp.Schema, sliceAdds), + new BlueTableListWrapper(dp.Schema, sliceDeletes)); + o.OnNext(dpOut); + } + } + } +} diff --git a/src/DataStreamSuite/DataStream/BlueOrb/SliceTable.cs b/src/DataStreamSuite/DataStream/BlueOrb/SliceTable.cs new file mode 100644 index 000000000..17a91a95a --- /dev/null +++ b/src/DataStreamSuite/DataStream/BlueOrb/SliceTable.cs @@ -0,0 +1,74 @@ +namespace bts.BlueOrb; + +using BCG; + +using bts.collections; + +using Spark.Core.Collections; + +using System; +using System.Collections.Generic; +using System.Linq; + +public class SliceTable : IBlueRowCollection { + readonly IBlueRowCollection inputRows; + readonly int? start; + readonly int? stop; + + public SliceTable(IBlueRowCollection inputRows, int? start, int? stop) { + this.inputRows = inputRows; + this.start = start; + this.stop = stop; + } + + public IBlueRow this[int id] { + get { + var r = inputRows[id]; + int idx = inputRows.IndexOf(r); + return idx >= startIndex && idx < stopIndex + ? r + : throw new KeyNotFoundException(); + } + } + + public IBlueTableSchema schema => inputRows.schema; + + public int Count => Math.Max(0, stopIndex - startIndex); + + IBlueRow IList.this[int index] { + get => GetRow(index); + set => throw new NotSupportedException(); + } + + int startIndex => SliceList.GetStartIndex(start, inputRows.Count); + int stopIndex => SliceList.GetStopIndex(stop, inputRows.Count); + + public bool containsID(int id) { + var r = inputRows[id]; + int idx = inputRows.IndexOf(r); + return idx >= startIndex && idx < stopIndex; + } + + public IEnumerator GetEnumerator() { + foreach (var r in inputRows.Skip(startIndex).Take(stopIndex - startIndex)) { + yield return r; + } + } + + public IBlueRow GetRow(int index) => + index >= 0 && index < Count + ? inputRows.GetRow(startIndex + index) + : throw new IndexOutOfRangeException(); + + public IList GetColumn(int index) { + var colWrapper = this.WrapSelector(r => r[index], null); + return colWrapper; + } + + public int IndexOf(IBlueRow item) { + int idx = inputRows.IndexOf(item); + return idx >= startIndex && idx < stopIndex + ? idx - startIndex + : -1; + } +} diff --git a/src/DataStreamSuite/DataStreamCore/BlueData/BlueTableListWrapper.cs b/src/DataStreamSuite/DataStreamCore/BlueData/BlueTableListWrapper.cs new file mode 100644 index 000000000..6d151c819 --- /dev/null +++ b/src/DataStreamSuite/DataStreamCore/BlueData/BlueTableListWrapper.cs @@ -0,0 +1,45 @@ +#nullable enable +namespace Bts.DataStreamCore; + +using BCG; + +using System; +using System.Collections.Generic; +using System.Linq; + +public class BlueTableListWrapper : IBlueRowCollection { + readonly IBlueTableSchema schema; + readonly IList rows; + readonly Lazy> rowIndex; + + public BlueTableListWrapper(IBlueTableSchema schema, IList rows) { + this.schema = schema; + this.rows = rows; + rowIndex = new(() => rows.Select((r, i) => (i, r)).ToDictionary(r => r.r.Id())); + } + + public IBlueRow this[int id] => + rowIndex.Value.TryGetValue(id, out var r) + ? r.Row + : throw new KeyNotFoundException(); + + public IBlueTableSchema Schema => schema; + + IBlueTableSchema IBlueRowCollection.schema => schema; + + public int Count => rows.Count; + + public bool containsID(int id) => + rowIndex.Value.ContainsKey(id); + + public IEnumerator GetEnumerator() => + rows.GetEnumerator(); + + public IBlueRow GetRow(int index) => + rows[index]; + + public int IndexOf(IBlueRow item) => + rowIndex.Value.TryGetValue(item.Id(), out var r) && BlueRowEqualityComparer.Equals(r.Row, item) + ? r.Index + : -1; +} diff --git a/src/client/spark_core/Collections/IndexInjectionList.cs b/src/client/spark_core/Collections/IndexInjectionList.cs new file mode 100644 index 000000000..eb2cbd5ec --- /dev/null +++ b/src/client/spark_core/Collections/IndexInjectionList.cs @@ -0,0 +1,154 @@ +#nullable enable +namespace Spark.Core.Collections; + +using bts; +using bts.collections; + +using System; +using System.Collections; +using System.Collections.Generic; +using System.Collections.Immutable; + +public class IndexInjectionList : IMutableListFacade { + readonly IList source; + readonly IList<(int Index, T Value)> sortedInserts; + readonly IList<(int Index, T Value)> sourceValueIndexTuples; + readonly IList<(int Index, T Value)> mergedValueIndexTuples; + readonly IList mergedValues; + readonly IList mergedIndices; + // readonly IList<(int Index, T Value)> valueIndexTupleOutputs; + readonly IList sortedInsertIndices; + readonly IList sortedInsertValues; + + public IndexInjectionList(IList source, IList<(int Index, T Value)> sortedInserts) { + this.source = source; + this.sortedInserts = sortedInserts; + sortedInsertIndices = sortedInserts.WrapSelector(t => t.Index, null); + sortedInsertValues = sortedInserts.WrapSelector(t => t.Value, null); + sourceValueIndexTuples = new SourceValueIndexTupleList(source); + mergedValueIndexTuples = MergedSortedList.Create(sourceValueIndexTuples, sortedInserts); + mergedValues = mergedValueIndexTuples.WrapSelector(t => t.Value, null); + mergedIndices = mergedValueIndexTuples.WrapSelector(t => t.Index, null); + } + + public T this[int index] { + get { + if (index < 0 || index >= Count) { + throw new IndexOutOfRangeException(); + } + var mergedIndex = mergedIndices.BinarySearch(index); + Log.assert(mergedIndex >= 0); + return mergedValues[mergedIndex]; + } + } + + public int Count => source.Count + sortedInserts.Count; + + public bool Contains(T item) => + source.Contains(item) || sortedInsertValues.Contains(item); + + public void CopyTo(T[] array, int arrayIndex) { + foreach (var v in this) { + array[arrayIndex++] = v; + } + } + + public IEnumerator GetEnumerator() { + var count = source.Count + sortedInserts.Count; + using var en = sortedInserts.GetEnumerator(); + bool canInsert = en.MoveNext(); + int nInserts = 0; + int i; + for (i = 0; i < count && canInsert; i++) { + if (i == en.Current.Index + nInserts) { + yield return en.Current.Value; + nInserts++; + canInsert = en.MoveNext(); + continue; + } + yield return source[i - nInserts]; + } + for (; i < count; i++) { + yield return source[i - nInserts]; + } + } + + public int IndexOf(T item) { + int idx = source.IndexOf(item); + var insIdx = sortedInsertValues.IndexOf(item); + if (idx != -1) { + int off = sortedInsertIndices.BinarySearch(idx); + if (off < 0) { + off = ~off; + } + int srcOccurrenceIdx = idx + off; + if (insIdx != -1) { + // Item was found in source list, and insert list. Return the lower index of the two. + int insOccurrenceIdx = sortedInserts[insIdx].Index + insIdx; + return Math.Min(srcOccurrenceIdx, insOccurrenceIdx); + } + return srcOccurrenceIdx; + } else if (insIdx != -1) { + int insOccurrenceIdx = sortedInserts[insIdx].Index + insIdx; + return insOccurrenceIdx; + } + return -1; + } + + class SortedInsertOutputList : IMutableListFacade<(int Index, T Value)> { + readonly IList<(int Index, T Value)> sortedInserts; + + public SortedInsertOutputList(IList<(int Index, T Value)> sortedInserts) { + this.sortedInserts = sortedInserts; + } + + public (int Index, T Value) this[int index] { + get { + var (i, v) = sortedInserts[index]; + return (i + index, v); + } + } + + public int Count => sortedInserts.Count; + + public IEnumerator<(int Index, T Value)> GetEnumerator() { + int n = 0; + foreach (var (i, v) in sortedInserts) { + yield return (i + n, v); + n++; + } + } + + IEnumerator IEnumerable.GetEnumerator() => + GetEnumerator(); + } + + class SourceValueIndexTupleList : IMutableListFacade<(int Index, T Value)> { + readonly IList source; + + public SourceValueIndexTupleList(IList source) => + this.source = source; + + public (int Index, T Value) this[int index] => + (index, source[index]); + + public int Count => source.Count; + + public IEnumerator<(int Index, T Value)> GetEnumerator() { + int n = 0; + foreach (var v in source) { + yield return (n, v); + n++; + } + } + + public int IndexOf((int Index, T Value) item) => + Contains(item) ? item.Index : -1; + + public bool Contains((int Index, T Value) item) => + item.Index >= 0 && item.Index < source.Count && EqualityComparer.Default.Equals(source[item.Index], item.Value); + + IEnumerator IEnumerable.GetEnumerator() => + GetEnumerator(); + } +} diff --git a/src/client/spark_core/Collections/IndexMaskList.cs b/src/client/spark_core/Collections/IndexMaskList.cs new file mode 100644 index 000000000..5b0f3f659 --- /dev/null +++ b/src/client/spark_core/Collections/IndexMaskList.cs @@ -0,0 +1,77 @@ +namespace Spark.Core.Collections; + +using bts.collections; + +using System; +using System.Collections.Generic; +using System.Collections.Immutable; + +public class IndexMaskList : IMutableListFacade { + readonly IList source; + readonly IList sortedRemoves; + + public IndexMaskList(IList source, IList sortedRemoves) { + this.source = source; + this.sortedRemoves = sortedRemoves; + } + + public T this[int index] { + get => source[convertIndex(index)]; + set => throw new NotSupportedException(); + } + + public int Count => source.Count - sortedRemoves.Count; + + public bool Contains(T item) { + int idx = source.IndexOf(item); + if (idx >= 0) { + int n = sortedRemoves.BinarySearch(idx); + return n < 0; + } + return false; + } + + public IEnumerator GetEnumerator() { + var count = Count; + using var en = sortedRemoves.GetEnumerator(); + if (!en.MoveNext()) { + foreach (var x in source) { + yield return x; + } + } + int n = 0; + for (int i = 0; i < count; i++) { + while (i > en.Current) { + n++; + if (!en.MoveNext()) { + break; + } + } + yield return source[i + n]; + } + } + + public int IndexOf(T item) { + int idx = source.IndexOf(item); + if (idx >= 0) { + int n = sortedRemoves.BinarySearch(idx); + if (n < 0) { + n = ~n; + idx -= n; + return idx; + } + } + return -1; + } + + int convertIndex(int i) { + if (i < 0 || i >= Count) { + throw new IndexOutOfRangeException(); + } + var n = sortedRemoves.BinarySearch(i); + if (n < 0) { + n = ~n; + } + return i + n; + } +}