From 65fbea141d2833cb12ca5e8f30ab7b60f346a700 Mon Sep 17 00:00:00 2001 From: John Colvin Date: Wed, 20 Nov 2024 07:34:36 +0000 Subject: [PATCH] Add BlueColumnTable for pandas integration This will make it easier/more natural to support pandas column expressions --- .../BlueData/BlueColumnTable.cs | 92 +++++++++++++++++++ 1 file changed, 92 insertions(+) create mode 100644 src/DataStreamSuite/DataStreamCore/BlueData/BlueColumnTable.cs diff --git a/src/DataStreamSuite/DataStreamCore/BlueData/BlueColumnTable.cs b/src/DataStreamSuite/DataStreamCore/BlueData/BlueColumnTable.cs new file mode 100644 index 000000000..f37198dbf --- /dev/null +++ b/src/DataStreamSuite/DataStreamCore/BlueData/BlueColumnTable.cs @@ -0,0 +1,92 @@ +namespace Bts.DataStreamCore; + +using BCG; + +using bts.utils; + +using Spark.Core; + +using System; + +using System.Collections; +using System.Collections.Generic; +using System.Linq; + +public class BlueColumnTable : IBlueRowCollection { + readonly Variant[][] columns; + readonly List rowIds; + readonly int idColIndex; + int count; + + public BlueColumnTable(IBlueTableSchema schema) { + columns = new Variant[schema.columnNames.Length][]; + if (columns.Length == 0) { + throw new ArgumentException("Schema contains no columns", nameof(schema)); + } + if (!schema.columnInfo.Select(c => c.colName).TryFindIndex("id", out idColIndex)) { + throw new ArgumentException("Schema contains no 'id' column", nameof(schema)); + } + } + + public int Count => count; + public IBlueTableSchema schema { get; } + + public IBlueRow this[int id] => new MyRow(this, id); + + public void Add(Variant[] values) { + var id = values[idColIndex].VInt32; + var idx = rowIds.BinarySearch(id); + if (idx < 0) { + rowIds.Insert(~idx, id); + } else { + throw new ArgumentException($"Row with id {id} already exists.", nameof(values)); + } + int len; + for (int i = 0; i < columns.Length; i++) { + len = count; + ArrayHelper.Insert(ref columns[i], idx, values[i], ref len); + } + count++; + } + + public void AddOrUpdate(Variant[] values) { + var id = values[idColIndex].VInt32; + var idx = rowIds.BinarySearch(id); + if (idx >= 0) { + rowIds[idx] = id; + for (int i = 0; i < columns.Length; i++) { + columns[idx][i] = values[i]; + } + return; + } + rowIds.Insert(~idx, id); + int len; + for (int i = 0; i < columns.Length; i++) { + len = count; + ArrayHelper.Insert(ref columns[i], idx, values[i], ref len); + } + count++; + } + + public bool containsID(int id) => + rowIds.BinarySearch(id) >= 0; + + public IEnumerator GetEnumerator() { + for (int i = 0; i < count; i++) { + yield return new MyRow(this, i); + } + } + + class MyRow : IBlueRow { + readonly BlueColumnTable table; + readonly int rowIdx; + + public MyRow(BlueColumnTable table, int rowIdx) { + this.table = table; + this.rowIdx = rowIdx; + } + + public object this[int colNo] => table.columns[rowIdx][colNo].GetValue(); + public IBlueTableSchema schema => table.schema; + } +}