Add BlueColumnTable for pandas integration
This will make it easier/more natural to support pandas column expressions
This commit is contained in:
1 parent
c6635005a1
commit
65fbea141d
1 file changed
+92
@@ -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<int> 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;
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user