128 lines
3.4 KiB
C#
128 lines
3.4 KiB
C#
namespace Bts.DataStreamCore;
|
|
|
|
using BCG;
|
|
|
|
using bts.utils;
|
|
|
|
using mbts.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;
|
|
|
|
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 => rowIds.Count;
|
|
public IBlueTableSchema schema { get; }
|
|
|
|
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) {
|
|
throw new ArgumentException($"Row with id {id} already exists.", nameof(values));
|
|
}
|
|
idx = ~idx;
|
|
for (int i = 0; i < columns.Length; i++) {
|
|
int len = rowIds.Count;
|
|
ArrayHelper.Insert(ref columns[i], idx, values[i], ref len);
|
|
}
|
|
rowIds.Insert(idx, id);
|
|
}
|
|
|
|
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;
|
|
}
|
|
idx = ~idx;
|
|
int len;
|
|
for (int i = 0; i < columns.Length; i++) {
|
|
len = rowIds.Count;
|
|
ArrayHelper.Insert(ref columns[i], idx, values[i], ref len);
|
|
}
|
|
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);
|
|
}
|
|
}
|
|
|
|
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[colNo][rowIdx].GetValue();
|
|
public IBlueTableSchema schema => table.schema;
|
|
}
|
|
}
|