fix Add BlueColumnTable for pandas integration
This commit is contained in:
1 parent
32fccbfb69
commit
ca522b7a23
2 files changed
+164
-15
No files matched your search
@@ -4,6 +4,8 @@ using BCG;
|
||||
|
||||
using bts.utils;
|
||||
|
||||
using mbts.utils;
|
||||
|
||||
using Spark.Core;
|
||||
|
||||
using System;
|
||||
@@ -16,37 +18,48 @@ 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][];
|
||||
public BlueColumnTable(IBlueTableSchema schema, int capacity = 4) {
|
||||
ExceptionAssert.Argument.GreaterThanEqualTo(capacity, 0);
|
||||
rowIds = new(capacity);
|
||||
columns = new Variant[schema.columnInfo.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));
|
||||
}
|
||||
this.schema = schema;
|
||||
for (int i = 0; i < columns.Length; i++) {
|
||||
columns[schema.columnInfo[i].colNo] = new Variant[capacity];
|
||||
}
|
||||
}
|
||||
|
||||
public int Count => count;
|
||||
public int Count => rowIds.Count;
|
||||
public IBlueTableSchema schema { get; }
|
||||
|
||||
public IBlueRow this[int id] => new MyRow(this, id);
|
||||
public IBlueRow this[int id] {
|
||||
get {
|
||||
var idx = rowIds.BinarySearch(id);
|
||||
if (idx < 0) {
|
||||
throw new KeyNotFoundException($"No row with id {id}");
|
||||
}
|
||||
return new MyRow(this, idx);
|
||||
}
|
||||
}
|
||||
|
||||
public void Add(Variant[] values) {
|
||||
var id = values[idColIndex].VInt32;
|
||||
var idx = rowIds.BinarySearch(id);
|
||||
if (idx < 0) {
|
||||
rowIds.Insert(~idx, id);
|
||||
} else {
|
||||
if (idx >= 0) {
|
||||
throw new ArgumentException($"Row with id {id} already exists.", nameof(values));
|
||||
}
|
||||
int len;
|
||||
idx = ~idx;
|
||||
for (int i = 0; i < columns.Length; i++) {
|
||||
len = count;
|
||||
int len = rowIds.Count;
|
||||
ArrayHelper.Insert(ref columns[i], idx, values[i], ref len);
|
||||
}
|
||||
count++;
|
||||
rowIds.Insert(idx, id);
|
||||
}
|
||||
|
||||
public void AddOrUpdate(Variant[] values) {
|
||||
@@ -59,19 +72,41 @@ public class BlueColumnTable : IBlueRowCollection {
|
||||
}
|
||||
return;
|
||||
}
|
||||
rowIds.Insert(~idx, id);
|
||||
idx = ~idx;
|
||||
int len;
|
||||
for (int i = 0; i < columns.Length; i++) {
|
||||
len = count;
|
||||
len = rowIds.Count;
|
||||
ArrayHelper.Insert(ref columns[i], idx, values[i], ref len);
|
||||
}
|
||||
count++;
|
||||
rowIds.Insert(idx, id);
|
||||
}
|
||||
|
||||
public void Clear() =>
|
||||
rowIds.Clear();
|
||||
|
||||
public bool Remove(int id) {
|
||||
var idx = rowIds.BinarySearch(id);
|
||||
if (idx < 0) {
|
||||
return false;
|
||||
}
|
||||
rowIds.RemoveAt(idx);
|
||||
for (int i = 0; i < columns.Length; i++) {
|
||||
ArrayHelper.remove_at(ref columns[i], idx);
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
public void Trim() {
|
||||
for (int i = 0; i < columns.Length; i++) {
|
||||
ArrayHelper.trim(ref columns[i], rowIds.Count);
|
||||
}
|
||||
}
|
||||
|
||||
public bool containsID(int id) =>
|
||||
rowIds.BinarySearch(id) >= 0;
|
||||
|
||||
public IEnumerator GetEnumerator() {
|
||||
var count = rowIds.Count;
|
||||
for (int i = 0; i < count; i++) {
|
||||
yield return new MyRow(this, i);
|
||||
}
|
||||
@@ -86,7 +121,7 @@ public class BlueColumnTable : IBlueRowCollection {
|
||||
this.rowIdx = rowIdx;
|
||||
}
|
||||
|
||||
public object this[int colNo] => table.columns[rowIdx][colNo].GetValue();
|
||||
public object this[int colNo] => table.columns[colNo][rowIdx].GetValue();
|
||||
public IBlueTableSchema schema => table.schema;
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user