refactor: Make IBlueRowCollection inherit IList<IBlueRow>

This commit is contained in:
John Colvin committed 2025-11-19 14:27:46 -05:00
1 parent 4fd2073b63
commit d3e31e4e44
38 files changed
+686 -266

No files matched your search

@@ -6,12 +6,9 @@ using BCG;
using bts.api.core;
using bts.collections;
using mbts.utils;
using Spark.Core.Collections;
using System;
using System.Collections;
using System.Collections.Generic;
using System.Collections.Immutable;
using System.Linq;
@@ -25,7 +22,7 @@ public class BlueTableImmutable : IBlueRowCollection, IImmutableRowCollection {
BlueTableImmutable(IBlueTableSchema schema, ImmutableSortedSet<KeyValuePair<int, IBlueRow>> rows) {
this.schema = schema;
this.rows = rows.WithComparer(new ComparerBuilder<KeyValuePair<int, IBlueRow>>() { kv => kv.Key });
this.rows = rows;
}
public int Count => rows.Count;
@@ -40,9 +37,31 @@ public class BlueTableImmutable : IBlueRowCollection, IImmutableRowCollection {
? kv.Value
: throw new KeyNotFoundException();
IBlueRow IList<IBlueRow>.this[int index] {
get => GetRow(index);
set => throw new NotSupportedException();
}
public static DataPacketBuilder CreateBuilder(IBlueTableSchema schema) =>
new(new(schema));
public static BlueTableImmutable CreateRange(IBlueTableSchema schema, IEnumerable<KeyValuePair<int, IBlueRow>> rows) =>
new(schema, ImmutableSortedSet.CreateRange(KeyValuePairComparer.KeyComparer<int, IBlueRow>(), rows));
public IBlueRow GetRow(int index) =>
rows[index].Value;
public int IndexOf(IBlueRow item) {
int id = item.Id();
if (rows.TryGetValue(new(id, item), out var rowKvp)) {
var vals = schema.columnInfo.Select(c => rowKvp.Value[c.colNo]);
var itemVals = item.schema.columnInfo.Select(c => item[c.colNo]);
if (vals.SequenceEqual(itemVals)) {
return rows.IndexOf(rowKvp);
}
}
return -1;
}
public bool containsID(int id) =>
#nullable disable
rows.Contains(new(id, default));
@@ -57,7 +76,7 @@ public class BlueTableImmutable : IBlueRowCollection, IImmutableRowCollection {
public IList<object?> GetColumn(int index) =>
rows.WrapSelector(kv => kv.Value[index], null);
public IEnumerator GetEnumerator() {
public IEnumerator<IBlueRow> GetEnumerator() {
foreach (var kv in rows) {
yield return kv.Value;
}
@@ -70,9 +89,9 @@ public class BlueTableImmutable : IBlueRowCollection, IImmutableRowCollection {
readonly IBlueTableSchema schema;
readonly ImmutableSortedSet<KeyValuePair<int, IBlueRow>>.Builder table;
readonly ImmutableSortedSet<KeyValuePair<int, IBlueRow>>.Builder added =
ImmutableSortedSet<KeyValuePair<int, IBlueRow>>.Empty.ToBuilder();
ImmutableSortedSet.CreateBuilder(KeyValuePairComparer.KeyComparer<int, IBlueRow>());
readonly ImmutableSortedSet<KeyValuePair<int, IBlueRow>>.Builder removed =
ImmutableSortedSet<KeyValuePair<int, IBlueRow>>.Empty.ToBuilder();
ImmutableSortedSet.CreateBuilder(KeyValuePairComparer.KeyComparer<int, IBlueRow>());
public DataPacketBuilder(BlueTableImmutable table) {
schema = table.schema;
@@ -264,7 +283,7 @@ public class BlueTableImmutable : IBlueRowCollection, IImmutableRowCollection {
// }
// }
public class BlueTableImmutableAdapter<T> : IBlueRowCollection<BlueRowAdapter<T>> {
public class BlueTableImmutableAdapter<T> : IBlueRowCollection {
readonly BlueTableSchemaAdapter<T> schemaAdapter;
readonly ImmutableSortedSet<KeyValuePair<int, T>> rows;
@@ -281,8 +300,8 @@ 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].Value);
public IBlueRow this[int id] =>
new BlueRowAdapter<T>(schemaAdapter, rows[id].Value);
public bool containsID(int id) =>
#nullable disable
@@ -292,13 +311,30 @@ public class BlueTableImmutableAdapter<T> : IBlueRowCollection<BlueRowAdapter<T>
public IList<object?> GetColumn(int index) =>
rows.WrapSelector(kv => schemaAdapter.GetBlueRow(kv.Value)[index], null);
public IBlueRow GetRow(int index) =>
new BlueRowAdapter<T>(schemaAdapter, rows[index].Value);
public void CopyTo(Array array, int index) {
foreach (var kv in rows) {
array.SetValue(new BlueRowAdapter<T>(schemaAdapter, kv.Value), index++);
}
}
public IEnumerator<BlueRowAdapter<T>> GetEnumerator() {
public int IndexOf(IBlueRow item) {
int id = item.Id();
#nullable disable
int index = rows.IndexOf(new(id, default));
if (index != -1) {
var r = this[index];
if (BlueRowEqualityComparer.Equals(r, item)) {
return index;
}
}
#nullable enable
return -1;
}
public IEnumerator<IBlueRow> GetEnumerator() {
foreach (var kv in rows) {
yield return new BlueRowAdapter<T>(schemaAdapter, kv.Value);
}
@@ -309,11 +345,12 @@ public class BlueTableImmutableAdapter<T> : IBlueRowCollection<BlueRowAdapter<T>
public class DataPacketBuilder {
readonly BlueTableSchemaAdapter<T> schemaAdapter;
readonly ImmutableSortedSet<KeyValuePair<int, T>>.Builder table;
readonly ImmutableSortedSet<KeyValuePair<int, T>>.Builder table =
ImmutableSortedSet.CreateBuilder(KeyValuePairComparer.KeyComparer<int, T>());
readonly ImmutableSortedSet<KeyValuePair<int, T>>.Builder added =
ImmutableSortedSet<KeyValuePair<int, T>>.Empty.ToBuilder();
ImmutableSortedSet.CreateBuilder(KeyValuePairComparer.KeyComparer<int, T>());
readonly ImmutableSortedSet<KeyValuePair<int, T>>.Builder removed =
ImmutableSortedSet<KeyValuePair<int, T>>.Empty.ToBuilder();
ImmutableSortedSet.CreateBuilder(KeyValuePairComparer.KeyComparer<int, T>());
public DataPacketBuilder(BlueTableImmutableAdapter<T> table) {
schemaAdapter = table.schemaAdapter;
@@ -334,13 +371,10 @@ public class BlueTableImmutableAdapter<T> : IBlueRowCollection<BlueRowAdapter<T>
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.Remove(origData);
removed.Add(origData);
}
removed.Remove(new(id, rowData));
removed.Add(new(id, rowData));
added.Remove(new(id, default!));
added.Add(new(id, rowData));
}
public void Remove(int id) {