feat: Add SliceTable/Stream

This commit is contained in:
John Colvin committed 2025-11-19 14:40:50 -05:00
1 parent a181bde099
commit ce455ac52c
5 files changed
+383

No files matched your search

@@ -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<IDataPacket>(Subscribe).AsViewStream());
IDisposable Subscribe(IObserver<IDataPacket> o) {
var rows = new SliceList<IBlueRow>(new List<IBlueRow>(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);
}
}
}
}
@@ -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<IBlueRow>.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<IBlueRow> 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<object?> 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;
}
}
@@ -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<IBlueRow> rows;
readonly Lazy<Dictionary<int, (int Index, IBlueRow Row)>> rowIndex;
public BlueTableListWrapper(IBlueTableSchema schema, IList<IBlueRow> 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<IBlueRow> 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;
}
@@ -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<T> : IMutableListFacade<T> {
readonly IList<T> source;
readonly IList<(int Index, T Value)> sortedInserts;
readonly IList<(int Index, T Value)> sourceValueIndexTuples;
readonly IList<(int Index, T Value)> mergedValueIndexTuples;
readonly IList<T> mergedValues;
readonly IList<int> mergedIndices;
// readonly IList<(int Index, T Value)> valueIndexTupleOutputs;
readonly IList<int> sortedInsertIndices;
readonly IList<T> sortedInsertValues;
public IndexInjectionList(IList<T> 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<T> 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<T> source;
public SourceValueIndexTupleList(IList<T> 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<T>.Default.Equals(source[item.Index], item.Value);
IEnumerator IEnumerable.GetEnumerator() =>
GetEnumerator();
}
}
@@ -0,0 +1,77 @@
namespace Spark.Core.Collections;
using bts.collections;
using System;
using System.Collections.Generic;
using System.Collections.Immutable;
public class IndexMaskList<T> : IMutableListFacade<T> {
readonly IList<T> source;
readonly IList<int> sortedRemoves;
public IndexMaskList(IList<T> source, IList<int> 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<T> 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;
}
}