new: Add GetColumn fn to IBlueRowCollection interface

This allows us to implement python/pandas functionality, much of which is based
on column operations.
This commit is contained in:
John Colvin committed 2025-11-15 12:38:50 -05:00
1 parent 2a4d62a1e9
commit 0d10c2ba5d
24 files changed
+495 -160

No files matched your search

@@ -4,8 +4,11 @@ namespace Bts.DataStreamCore;
using BCG;
using bts.api.core;
using bts.collections;
using Bts.DataStreamCore;
using mbts.utils;
using Spark.Core.Collections;
using System;
using System.Collections;
@@ -15,14 +18,14 @@ using System.Linq;
// I would have named this ImmutableBlueTable, but that was already taken.
public class BlueTableImmutable : IBlueRowCollection, IImmutableRowCollection {
readonly ImmutableDictionary<int, IBlueRow> rows;
readonly ImmutableSortedSet<KeyValuePair<int, IBlueRow>> rows;
public BlueTableImmutable(IBlueTableSchema schema)
: this(schema, ImmutableDictionary<int, IBlueRow>.Empty) { }
: this(schema, ImmutableSortedSet<KeyValuePair<int, IBlueRow>>.Empty) { }
public BlueTableImmutable(IBlueTableSchema schema, ImmutableDictionary<int, IBlueRow> rows) {
public BlueTableImmutable(IBlueTableSchema schema, ImmutableSortedSet<KeyValuePair<int, IBlueRow>> rows) {
this.schema = schema;
this.rows = rows;
this.rows = rows.WithComparer(new ComparerBuilder<KeyValuePair<int, IBlueRow>>() { kv => kv.Key });
}
public int Count => rows.Count;
@@ -30,30 +33,43 @@ public class BlueTableImmutable : IBlueRowCollection, IImmutableRowCollection {
public IBlueTableSchema schema { get; }
public object SyncRoot => this;
public IBlueRow this[int id] => rows[id];
public IBlueRow this[int id] =>
#nullable disable
rows.TryGetValue(new(id, default), out var kv)
#nullable enable
? kv.Value
: throw new KeyNotFoundException();
public bool containsID(int id) =>
rows.ContainsKey(id);
#nullable disable
rows.Contains(new(id, default));
#nullable enable
public void CopyTo(Array array, int index) {
foreach (var r in rows.Values) {
array.SetValue(r, index++);
foreach (var kv in rows) {
array.SetValue(kv.Value, index++);
}
}
public IEnumerator GetEnumerator() =>
rows.Values.GetEnumerator();
public IList<object?> GetColumn(int index) =>
rows.WrapSelector(kv => kv.Value[index], null);
public IEnumerator GetEnumerator() {
foreach (var kv in rows) {
yield return kv.Value;
}
}
public DataPacketBuilder ToDataPacketBuilder() =>
new(this);
public class DataPacketBuilder {
readonly IBlueTableSchema schema;
readonly ImmutableDictionary<int, IBlueRow>.Builder table;
readonly ImmutableDictionary<int, IBlueRow>.Builder added =
ImmutableDictionary<int, IBlueRow>.Empty.ToBuilder();
readonly ImmutableDictionary<int, IBlueRow>.Builder removed =
ImmutableDictionary<int, IBlueRow>.Empty.ToBuilder();
readonly ImmutableSortedSet<KeyValuePair<int, IBlueRow>>.Builder table;
readonly ImmutableSortedSet<KeyValuePair<int, IBlueRow>>.Builder added =
ImmutableSortedSet<KeyValuePair<int, IBlueRow>>.Empty.ToBuilder();
readonly ImmutableSortedSet<KeyValuePair<int, IBlueRow>>.Builder removed =
ImmutableSortedSet<KeyValuePair<int, IBlueRow>>.Empty.ToBuilder();
public DataPacketBuilder(BlueTableImmutable table) {
schema = table.schema;
@@ -62,30 +78,37 @@ public class BlueTableImmutable : IBlueRowCollection, IImmutableRowCollection {
public void AddOrUpdate(IBlueRow row, IEqualityComparer<IBlueRow>? comparer = null) {
var id = row.Id();
if (table.TryGetValue(id, out var orig)) {
if (comparer?.Equals(row, orig) ?? false) {
#nullable disable
if (table.TryGetValue(new(id, default), out var orig)) {
#nullable enable
if (comparer?.Equals(row, orig.Value) ?? false) {
// No op: This row was already in the table (according to 'comparer').
// Remove it from 'added' and 'removed'.
added.Remove(id);
removed.Remove(id);
added.Remove(new(id, row));
removed.Remove(new(id, orig.Value));
return;
}
// Change op: A row with this id was already in the table,
// but its values have changed (according to 'comparer').
removed[id] = orig;
removed.Remove(new(id, orig.Value));
removed.Add(new(id, orig.Value));
}
added[id] = row;
added.Remove(new(id, row));
added.Add(new(id, row));
}
public void Remove(int id) {
added.Remove(id);
if (table.TryGetValue(id, out var orig)) {
removed[id] = orig;
#nullable disable
added.Remove(new(id, default));
if (table.TryGetValue(new(id, default), out var orig)) {
#nullable enable
removed.Remove(new(id, orig.Value));
removed.Add(new(id, orig.Value));
}
}
public DataPacket ToDataPacket(bool reset = true) {
table.RemoveRange(removed.Keys);
table.RemoveRange(removed);
table.AddRange(added);
var newRows = new BlueTableImmutable(schema, table.ToImmutable());
var addedRows = new BlueTableImmutable(schema, added.ToImmutable());
@@ -103,12 +126,12 @@ public class BlueTableImmutable : IBlueRowCollection, IImmutableRowCollection {
}
public class BlueTableImmutable<T> : IBlueRowCollection<T> where T : IBlueRow {
readonly ImmutableDictionary<int, T> rows;
readonly ImmutableSortedSet<KeyValuePair<int, T>> rows;
public BlueTableImmutable(IBlueTableSchema schema)
: this(schema, ImmutableDictionary<int, T>.Empty) { }
: this(schema, ImmutableSortedSet<KeyValuePair<int, T>>.Empty) { }
public BlueTableImmutable(IBlueTableSchema schema, ImmutableDictionary<int, T> rows) {
public BlueTableImmutable(IBlueTableSchema schema, ImmutableSortedSet<KeyValuePair<int, T>> rows) {
this.schema = schema;
this.rows = rows;
}
@@ -118,19 +141,32 @@ public class BlueTableImmutable<T> : IBlueRowCollection<T> where T : IBlueRow {
public IBlueTableSchema schema { get; }
public object SyncRoot => this;
public T this[int id] => rows[id];
public T this[int id] =>
#nullable disable
rows.TryGetValue(new(id, default), out var kv)
#nullable enable
? kv.Value
: throw new KeyNotFoundException();
public bool containsID(int id) =>
rows.ContainsKey(id);
#nullable disable
rows.Contains(new(id, default));
#nullable enable
public IList<object?> GetColumn(int index) =>
rows.WrapSelector(kv => kv.Value[index], null);
public void CopyTo(Array array, int index) {
foreach (var r in rows.Values) {
array.SetValue(r, index++);
foreach (var (k, v) in rows) {
array.SetValue(v, index++);
}
}
public IEnumerator<T> GetEnumerator() =>
rows.Values.GetEnumerator();
public IEnumerator<T> GetEnumerator() {
foreach (var kv in rows) {
yield return kv.Value;
}
}
IEnumerator IEnumerable.GetEnumerator() =>
GetEnumerator();
@@ -140,11 +176,11 @@ public class BlueTableImmutable<T> : IBlueRowCollection<T> where T : IBlueRow {
public class DataPacketBuilder {
readonly IBlueTableSchema schema;
readonly ImmutableDictionary<int, T>.Builder table;
readonly ImmutableDictionary<int, T>.Builder added =
ImmutableDictionary<int, T>.Empty.ToBuilder();
readonly ImmutableDictionary<int, T>.Builder removed =
ImmutableDictionary<int, T>.Empty.ToBuilder();
readonly ImmutableSortedSet<KeyValuePair<int, T>>.Builder table;
readonly ImmutableSortedSet<KeyValuePair<int, T>>.Builder added =
ImmutableSortedSet<KeyValuePair<int, T>>.Empty.ToBuilder();
readonly ImmutableSortedSet<KeyValuePair<int, T>>.Builder removed =
ImmutableSortedSet<KeyValuePair<int, T>>.Empty.ToBuilder();
public DataPacketBuilder(BlueTableImmutable<T> table) {
schema = table.schema;
@@ -153,30 +189,37 @@ public class BlueTableImmutable<T> : IBlueRowCollection<T> where T : IBlueRow {
public void AddOrUpdate(T row, IEqualityComparer<IBlueRow>? comparer = null) {
var id = row.Id();
if (table.TryGetValue(id, out var orig)) {
if (comparer?.Equals(row, orig) ?? false) {
#nullable disable
if (table.TryGetValue(new(id, default), out var orig)) {
#nullable enable
if (comparer?.Equals(row, orig.Value) ?? false) {
// No op: This row was already in the table (according to 'comparer').
// Remove it from 'added' and 'removed'.
added.Remove(id);
removed.Remove(id);
added.Remove(new(id, row));
removed.Remove(new(id, orig.Value));
return;
}
// Change op: A row with this id was already in the table,
// but its values have changed (according to 'comparer').
removed[id] = orig;
removed.Remove(orig);
removed.Add(orig);
}
added[id] = row;
added.Remove(new(id, row));
added.Add(new(id, row));
}
public void Remove(int id) {
added.Remove(id);
if (table.TryGetValue(id, out var orig)) {
removed[id] = orig;
#nullable disable
added.Remove(new(id, default));
if (table.TryGetValue(new(id, default), out var orig)) {
#nullable enable
removed.Remove(orig);
removed.Remove(orig);
}
}
public DataPacket ToDataPacket(bool reset = true) {
table.RemoveRange(removed.Keys);
table.RemoveRange(removed);
table.AddRange(added);
var newRows = new BlueTableImmutable<T>(schema, table.ToImmutable());
var addedRows = new BlueTableImmutable<T>(schema, added.ToImmutable());
@@ -189,7 +232,7 @@ public class BlueTableImmutable<T> : IBlueRowCollection<T> where T : IBlueRow {
}
public IDataPacket<T> ToDataPacketT(bool reset = true) {
table.RemoveRange(removed.Keys);
table.RemoveRange(removed);
table.AddRange(added);
var newRows = new BlueTableImmutable<T>(schema, table.ToImmutable());
var addedRows = new BlueTableImmutable<T>(schema, added.ToImmutable());
@@ -207,7 +250,7 @@ public class BlueTableImmutable<T> : IBlueRowCollection<T> where T : IBlueRow {
out BlueTableImmutable<T> removedRows,
bool reset = true
) {
table.RemoveRange(removed.Keys);
table.RemoveRange(removed);
table.AddRange(added);
newRows = new BlueTableImmutable<T>(schema, table.ToImmutable());
addedRows = new BlueTableImmutable<T>(schema, added.ToImmutable());
@@ -226,12 +269,12 @@ public class BlueTableImmutable<T> : IBlueRowCollection<T> where T : IBlueRow {
public class BlueTableImmutableAdapter<T> : IBlueRowCollection<BlueRowAdapter<T>> {
readonly BlueTableSchemaAdapter<T> schemaAdapter;
readonly ImmutableDictionary<int, T> rows;
readonly ImmutableSortedSet<KeyValuePair<int, T>> rows;
public BlueTableImmutableAdapter(BlueTableSchemaAdapter<T> schemaAdapter)
: this(schemaAdapter, ImmutableDictionary<int, T>.Empty) { }
: this(schemaAdapter, ImmutableSortedSet<KeyValuePair<int, T>>.Empty) { }
public BlueTableImmutableAdapter(BlueTableSchemaAdapter<T> schemaAdapter, ImmutableDictionary<int, T> rows) {
public BlueTableImmutableAdapter(BlueTableSchemaAdapter<T> schemaAdapter, ImmutableSortedSet<KeyValuePair<int, T>> rows) {
this.schemaAdapter = schemaAdapter;
this.rows = rows;
}
@@ -241,20 +284,26 @@ public class BlueTableImmutableAdapter<T> : IBlueRowCollection<BlueRowAdapter<T>
public IBlueTableSchema schema => schemaAdapter.Schema;
public object SyncRoot => this;
public BlueRowAdapter<T> this[int id] => new(schemaAdapter, rows[id]);
public BlueRowAdapter<T> this[int id] =>
new(schemaAdapter, rows[id].Value);
public bool containsID(int id) =>
rows.ContainsKey(id);
#nullable disable
rows.Contains(new(id, default));
#nullable enable
public IList<object?> GetColumn(int index) =>
rows.WrapSelector(kv => schemaAdapter.GetBlueRow(kv.Value)[index], null);
public void CopyTo(Array array, int index) {
foreach (var r in rows.Values) {
array.SetValue(new BlueRowAdapter<T>(schemaAdapter, r), index++);
foreach (var kv in rows) {
array.SetValue(new BlueRowAdapter<T>(schemaAdapter, kv.Value), index++);
}
}
public IEnumerator<BlueRowAdapter<T>> GetEnumerator() {
foreach (var row in rows.Values) {
yield return new BlueRowAdapter<T>(schemaAdapter, row);
foreach (var kv in rows) {
yield return new BlueRowAdapter<T>(schemaAdapter, kv.Value);
}
}
@@ -263,11 +312,11 @@ public class BlueTableImmutableAdapter<T> : IBlueRowCollection<BlueRowAdapter<T>
public class DataPacketBuilder {
readonly BlueTableSchemaAdapter<T> schemaAdapter;
readonly ImmutableDictionary<int, T>.Builder table;
readonly ImmutableDictionary<int, T>.Builder added =
ImmutableDictionary<int, T>.Empty.ToBuilder();
readonly ImmutableDictionary<int, T>.Builder removed =
ImmutableDictionary<int, T>.Empty.ToBuilder();
readonly ImmutableSortedSet<KeyValuePair<int, T>>.Builder table;
readonly ImmutableSortedSet<KeyValuePair<int, T>>.Builder added =
ImmutableSortedSet<KeyValuePair<int, T>>.Empty.ToBuilder();
readonly ImmutableSortedSet<KeyValuePair<int, T>>.Builder removed =
ImmutableSortedSet<KeyValuePair<int, T>>.Empty.ToBuilder();
public DataPacketBuilder(BlueTableImmutableAdapter<T> table) {
schemaAdapter = table.schemaAdapter;
@@ -277,26 +326,33 @@ public class BlueTableImmutableAdapter<T> : IBlueRowCollection<BlueRowAdapter<T>
public void AddOrUpdate(T rowData, IEqualityComparer<IBlueRow>? comparer = null) {
var row = new BlueRowAdapter<T>(schemaAdapter, rowData);
var id = row.Id();
if (table.TryGetValue(id, out var origData)) {
var orig = new BlueRowAdapter<T>(schemaAdapter, origData);
#nullable disable
if (table.TryGetValue(new(id, default), out var origData)) {
#nullable enable
var orig = new BlueRowAdapter<T>(schemaAdapter, origData.Value);
if (comparer?.Equals(row, orig) ?? false) {
// No op: This row was already in the table (according to 'comparer').
// Remove it from 'added' and 'removed'.
added.Remove(id);
removed.Remove(id);
added.Remove(origData);
removed.Remove(origData);
return;
}
// Change op: A row with this id was already in the table,
// but its values have changed (according to 'comparer').
removed[id] = origData;
removed.Remove(origData);
removed.Add(origData);
}
added[id] = rowData;
removed.Remove(new(id, rowData));
removed.Add(new(id, rowData));
}
public void Remove(int id) {
added.Remove(id);
if (table.TryGetValue(id, out var orig)) {
removed[id] = orig;
#nullable disable
added.Remove(new(id, default));
if (table.TryGetValue(new(id, default), out var orig)) {
#nullable enable
removed.Remove(orig);
removed.Add(orig);
}
}
@@ -310,7 +366,7 @@ public class BlueTableImmutableAdapter<T> : IBlueRowCollection<BlueRowAdapter<T>
}
public IDataPacket<BlueRowAdapter<T>> ToDataPacket(bool reset = true) {
table.RemoveRange(removed.Keys);
table.RemoveRange(removed);
table.AddRange(added);
var newRows = new BlueTableImmutableAdapter<T>(schemaAdapter, table.ToImmutable());
var addedRows = new BlueTableImmutableAdapter<T>(schemaAdapter, added.ToImmutable());