Files
spark/src/DataStreamSuite/DataStream/BlueOrb/OrderedTable.cs
T
2025-11-15 12:45:08 -05:00

50 lines
1.4 KiB
C#

namespace bts.BlueOrb;
using BCG;
using Bts.DataStreamCore;
using System.Collections.Generic;
public class OrderedTable : IBlueRowCollection {
readonly IBlueTableSchema schema;
readonly List<IBlueRow> rows;
readonly Dictionary<int, int> idToIndex;
public OrderedTable(IBlueTableSchema schema, List<IBlueRow> rows, Dictionary<int, int> idToIndex) {
this.schema = schema;
this.rows = rows;
this.idToIndex = idToIndex;
}
public static OrderedTable FromRowCollection(IBlueRowCollection rowCollection) {
var rowList = new List<IBlueRow>(rowCollection.Count);
var idToIndex = new Dictionary<int, int>();
foreach (var r in rowCollection) {
idToIndex[r.Id()] = rowList.Count;
rowList.Add(r);
}
return new(rowCollection.schema, rowList, idToIndex);
}
public IBlueTableSchema Schema => schema;
public int Count => rows.Count;
public IBlueRow this[int id] => idToIndex.TryGetValue(id, out var idx) ? rows[idx] : throw new KeyNotFoundException();
IBlueTableSchema IBlueRowCollection.schema => schema;
public bool containsID(int id) =>
idToIndex.ContainsKey(id);
public IEnumerator<IBlueRow> GetEnumerator() =>
rows.GetEnumerator();
public IBlueRow GetRow(int index) =>
rows[index];
public int IndexOf(IBlueRow item) =>
idToIndex.TryGetValue(item.Id(), out var idx) ? idx : -1;
}