refactor: Make IBlueRowCollection inherit IList<IBlueRow>

This commit is contained in:
John Colvin committed 2025-11-15 12:38:51 -05:00
1 parent 144b29c728
commit d2a1b67717
39 files changed
+1035 -439

No files matched your search

@@ -1,6 +1,7 @@
using System;
using System.Collections;
using System.Collections.Generic;
using System.Collections.Immutable;
using System.Data;
using System.Linq;
@@ -8,51 +9,83 @@ using BCG;
using bts.collections;
using Bts.DataStreamCore;
namespace bts {
public class CircularBufferTable : IBlueRowCollection {
private BlueTable _table;
private List<IBlueRow> _ordered_list = new List<IBlueRow>();
readonly ImmutableList<IBlueRow> orderedRows;
readonly ImmutableDictionary<int, IBlueRow> rowIndex;
private int _buffer_sz;
public CircularBufferTable(IBlueTableSchema schema, int buffer_sz) {
_table = new BlueTable(schema);
_buffer_sz = buffer_sz;
public CircularBufferTable(IBlueTableSchema schema, int buffer_sz)
: this(schema, buffer_sz, ImmutableList<IBlueRow>.Empty, ImmutableDictionary<int, IBlueRow>.Empty) { }
CircularBufferTable(IBlueTableSchema schema, int bufferSize, ImmutableList<IBlueRow> orderedRows, ImmutableDictionary<int, IBlueRow> rowIndex) {
this.schema = schema;
_buffer_sz = bufferSize;
this.orderedRows = orderedRows;
this.rowIndex = rowIndex;
}
public CircularBufferTable Add(IBlueRow row) =>
Add(row, out _);
// returns the row that might need to be deleted to keep
// the circular buffer intact
public IBlueRow add_row(IBlueRow row) {
_ordered_list.Add(row);
_table.addRow(row);
if (_ordered_list.Count > _buffer_sz) {
IBlueRow rv = _ordered_list.ElementAt(0);
_ordered_list.RemoveAt(0);
_table.deleteRow(rv);
return rv;
public CircularBufferTable Add(IBlueRow row, out IBlueRow oldRow) {
int id = row.Id();
var rowIndexBd = rowIndex.ToBuilder();
var orderedRowsBd = orderedRows.ToBuilder();
if (rowIndexBd.TryGetValue(id, out oldRow)) {
Log.assert(orderedRowsBd.RemoveFirst(r => r.Id() == id));
} else if (rowIndexBd.Count > _buffer_sz) {
oldRow = orderedRowsBd[0];
rowIndexBd.Remove(oldRow.Id());
orderedRowsBd.RemoveAt(0);
}
return null;
rowIndexBd[id] = row;
orderedRowsBd.Add(row);
return new(schema, _buffer_sz, orderedRowsBd.ToImmutable(), rowIndexBd.ToImmutable());
}
public IBlueRow this[int id] {
get {
return _table[id];
}
}
public IBlueTableSchema schema {
get {
return _table.schema;
return orderedRows[id];
}
}
public IBlueTableSchema schema { get; }
public bool containsID(int id) {
return _table.containsID(id);
return rowIndex.ContainsKey(id);
}
public void CopyTo(Array array, int index) { _table.CopyTo((IBlueRow[])array, index); }
public int Count { get { return _table.Count; } }
public void CopyTo(Array array, int index) {
foreach (var row in orderedRows) {
array.SetValue(row, index++);
}
}
public int Count => rowIndex.Count;
public bool IsSynchronized { get { return false; } }
public object SyncRoot { get { return this; } }
public System.Collections.IEnumerator GetEnumerator() { return _table.GetEnumerator(); }
public IEnumerator<IBlueRow> GetEnumerator() =>
orderedRows.GetEnumerator();
public IList<object> GetColumn(int index) =>
_ordered_list.WrapSelector(r => r[index], null);
orderedRows.WrapSelector(r => r[index], null);
public IBlueRow GetRow(int index) =>
orderedRows[index];
public int IndexOf(IBlueRow item) {
int id = item.Id();
if (rowIndex.TryGetValue(id, out var row) && BlueRowEqualityComparer.Equals(item, row)) {
int i = 0;
foreach (var r in orderedRows) {
if (r.Id() == id) {
return i;
}
i++;
}
}
return -1;
}
}
}