new: Add GetColumn fn to IBlueRowCollection interface
This allows us to implement python/pandas functionality, much of which is based on column operations.
This commit is contained in:
1 parent
2a4d62a1e9
commit
0d10c2ba5d
24 files changed
+495
-160
No files matched your search
@@ -2,6 +2,7 @@ namespace Bts.DataStreamCore;
|
||||
|
||||
using BCG;
|
||||
|
||||
using bts.collections;
|
||||
using bts.utils;
|
||||
|
||||
using mbts.utils;
|
||||
@@ -139,6 +140,9 @@ public class BlueColumnTable : IBlueRowCollection {
|
||||
public bool containsID(int id) =>
|
||||
rowIds.BinarySearch(id) >= 0;
|
||||
|
||||
public IList<object> GetColumn(int index) =>
|
||||
columns[index].WrapSelector(v => v.GetValue(), null);
|
||||
|
||||
public IEnumerator GetEnumerator() {
|
||||
var count = rowIds.Count;
|
||||
for (int i = 0; i < count; i++) {
|
||||
|
||||
@@ -3,6 +3,8 @@ namespace Bts.DataStreamCore;
|
||||
|
||||
using BCG;
|
||||
|
||||
using bts.collections;
|
||||
|
||||
using System;
|
||||
using System.Collections;
|
||||
using System.Collections.Generic;
|
||||
@@ -12,6 +14,7 @@ public class BlueRowCollection : IBlueRowCollection {
|
||||
readonly ICollection source;
|
||||
readonly Func<int, IBlueRow> findRow;
|
||||
readonly Func<int, bool> containsKey;
|
||||
IList<IBlueRow>? cachedRows;
|
||||
|
||||
public BlueRowCollection(ICollection source, IBlueTableSchema schema, Func<int, IBlueRow>? findRow = null, Func<int, bool>? containsKey = null) {
|
||||
this.source = source;
|
||||
@@ -51,6 +54,11 @@ public class BlueRowCollection : IBlueRowCollection {
|
||||
public bool containsID(int id) =>
|
||||
containsKey(id);
|
||||
|
||||
public IList<object?> GetColumn(int index) {
|
||||
cachedRows ??= source as IList<IBlueRow> ?? source.Cast<IBlueRow>().ToList();
|
||||
return cachedRows.WrapSelector(r => r[index], null);
|
||||
}
|
||||
|
||||
public IEnumerator GetEnumerator() =>
|
||||
source.GetEnumerator();
|
||||
}
|
||||
@@ -1,6 +1,6 @@
|
||||
using Bts.DataStreamCore;
|
||||
using bts.collections;
|
||||
|
||||
using Spark.DataStream;
|
||||
using Bts.DataStreamCore;
|
||||
|
||||
using System;
|
||||
using System.Collections;
|
||||
@@ -21,7 +21,7 @@ namespace BCG {
|
||||
/// <summary>
|
||||
/// Hashtable indexed on "id" to speed up removes
|
||||
/// </summary>
|
||||
protected Hashtable myIdIndex = new Hashtable();
|
||||
protected SortedList<int, IBlueRow> rows = new();
|
||||
/// <summary>
|
||||
/// Schema the table adheres to
|
||||
/// </summary>
|
||||
@@ -42,7 +42,7 @@ namespace BCG {
|
||||
mySchema = new BlueTableSchema(cis, table.TableName);
|
||||
foreach (DataRow row in table.Rows) {
|
||||
IBlueRow newRow = new BlueRow(mySchema, row.ItemArray);
|
||||
myIdIndex[newRow["id"]] = newRow;
|
||||
rows[newRow.Id()] = newRow;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -81,8 +81,7 @@ namespace BCG {
|
||||
/// <param name="schema"></param>
|
||||
public BlueTable(IBlueTableSchema schema) { mySchema = schema; }
|
||||
|
||||
public ICollection<IBlueRow> Rows =>
|
||||
GenericCollectionWrapper.Create<IBlueRow>(myIdIndex.Values);
|
||||
public IList<IBlueRow> Rows => rows.Values;
|
||||
|
||||
public static BlueTable Empty(string name, bool includeIdColumn = true) =>
|
||||
new(BlueTableSchema.Empty(name, includeIdColumn));
|
||||
@@ -95,7 +94,7 @@ namespace BCG {
|
||||
/// </summary>
|
||||
public virtual IBlueRow this[int id] {
|
||||
get {
|
||||
object row = myIdIndex[id];
|
||||
object row = rows[id];
|
||||
if (row == null)
|
||||
throw new BlueDataException("ID " + id.ToString() + " does not exist in table " + mySchema.tableName);
|
||||
return (IBlueRow)row;
|
||||
@@ -113,18 +112,16 @@ namespace BCG {
|
||||
/// </summary>
|
||||
public virtual IBlueTableSchema schema { get { return mySchema; } }
|
||||
|
||||
public virtual bool containsID(int id) { return myIdIndex[id] != null; }
|
||||
public virtual bool containsID(int id) { return rows[id] != null; }
|
||||
|
||||
public virtual bool tryGetValue(int id, out IBlueRow row) {
|
||||
row = (IBlueRow)myIdIndex[id];
|
||||
return (row != null);
|
||||
}
|
||||
public virtual bool tryGetValue(int id, out IBlueRow row) =>
|
||||
rows.TryGetValue(id, out row);
|
||||
|
||||
#endregion
|
||||
|
||||
#region BlueTable members
|
||||
public virtual void clear() {
|
||||
myIdIndex.Clear();
|
||||
rows.Clear();
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
@@ -132,7 +129,7 @@ namespace BCG {
|
||||
/// </summary>
|
||||
/// <param name="r"></param>
|
||||
public virtual void forceAddRow(IBlueRow r) {
|
||||
myIdIndex[r["id"]] = r;
|
||||
rows[r.Id()] = r;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
@@ -145,7 +142,7 @@ namespace BCG {
|
||||
if (id == null || id == DBNull.Value)
|
||||
throw new BlueDataException("The ID of the row being added is null/DNBull");
|
||||
try {
|
||||
myIdIndex.Add(id, row);
|
||||
rows.Add((int)id, row);
|
||||
} catch (ArgumentException) {
|
||||
throw new BlueDataException($"ID: {id} already exists in the table: {mySchema.tableName}");
|
||||
}
|
||||
@@ -157,7 +154,10 @@ namespace BCG {
|
||||
|
||||
if (val == null || val == DBNull.Value)
|
||||
throw new BlueDataException("The ID of the row being added is null/DNBull");
|
||||
if (myIdIndex[val] != null) {
|
||||
if (val is not int id) {
|
||||
throw new ArgumentException($"Expected id to be of type int (actually {val?.GetType()})", nameof(row));
|
||||
}
|
||||
if (rows.ContainsKey(id)) {
|
||||
string err_msg = $"ID: {val} already exists in the table: {mySchema.tableName} duplication of field ID not allowed";
|
||||
// temp TODOds remove it
|
||||
bts.Log.assert(false, err_msg);
|
||||
@@ -165,7 +165,7 @@ namespace BCG {
|
||||
|
||||
}
|
||||
IBlueRow newRow = new BlueRow(mySchema, row);
|
||||
myIdIndex[val] = newRow;
|
||||
rows[id] = newRow;
|
||||
}
|
||||
|
||||
public virtual void addRows(IBlueRowCollection rows) {
|
||||
@@ -190,12 +190,8 @@ namespace BCG {
|
||||
/// Removes a row specified by the id. The index helps in making this operation O(1)
|
||||
/// </summary>
|
||||
/// <param name="id"></param>
|
||||
public virtual bool deleteRow(int id) {
|
||||
if (myIdIndex[id] == null)
|
||||
return false;
|
||||
myIdIndex.Remove(id);
|
||||
return true;
|
||||
}
|
||||
public virtual bool deleteRow(int id) =>
|
||||
rows.Remove(id);
|
||||
|
||||
public virtual bool deleteRows(IBlueRowCollection rows) {
|
||||
bool retVal = true;
|
||||
@@ -212,7 +208,7 @@ namespace BCG {
|
||||
}
|
||||
|
||||
public virtual void changeRow(IBlueRow r) {
|
||||
myIdIndex[r["id"]] = r;
|
||||
rows[r.Id()] = r;
|
||||
}
|
||||
|
||||
public virtual void changeRows(IBlueRowCollection rows) {
|
||||
@@ -224,17 +220,29 @@ namespace BCG {
|
||||
|
||||
public IDictionary<int, IBlueRow> AsDictionary() => this;
|
||||
|
||||
public IList<object> GetColumn(int index) {
|
||||
if (index < 0 || index >= schema.columnInfo.Length) {
|
||||
throw new IndexOutOfRangeException();
|
||||
}
|
||||
return Rows.WrapSelector(r => r[index], null);
|
||||
}
|
||||
|
||||
#region ICollection members
|
||||
public virtual IEnumerator GetEnumerator() //{return myRows.GetEnumerator ();}
|
||||
{ return myIdIndex.Values.GetEnumerator(); }
|
||||
public virtual IEnumerator GetEnumerator() =>
|
||||
rows.Values.GetEnumerator();
|
||||
|
||||
public virtual bool IsSynchronized { get { return false; } }
|
||||
|
||||
public virtual object SyncRoot { get { return this; } }
|
||||
|
||||
public virtual void CopyTo(Array array, int index) { myIdIndex.Values.CopyTo(array, index); }
|
||||
public virtual void CopyTo(Array array, int index) {
|
||||
foreach (var r in rows.Values) {
|
||||
array.SetValue(r, index++);
|
||||
}
|
||||
}
|
||||
|
||||
public virtual int Count { get { return myIdIndex.Count; } }
|
||||
public virtual int Count =>
|
||||
rows.Count;
|
||||
#endregion
|
||||
|
||||
#region IDictionary<int, IBlueRow> members
|
||||
@@ -262,24 +270,20 @@ namespace BCG {
|
||||
tryGetValue(item.Key, out var r)
|
||||
&& BlueRowEqualityComparer.Instance.Equals(r, item.Value);
|
||||
|
||||
void ICollection<KeyValuePair<int, IBlueRow>>.CopyTo(
|
||||
KeyValuePair<int, IBlueRow>[] array, int arrayIndex) {
|
||||
foreach (DictionaryEntry de in myIdIndex) {
|
||||
array[arrayIndex++] = new((int)de.Key, (IBlueRow)de.Value);
|
||||
void ICollection<KeyValuePair<int, IBlueRow>>.CopyTo(KeyValuePair<int, IBlueRow>[] array, int arrayIndex) {
|
||||
foreach (var kv in rows) {
|
||||
array[arrayIndex] = kv;
|
||||
}
|
||||
}
|
||||
|
||||
bool ICollection<KeyValuePair<int, IBlueRow>>.Remove(KeyValuePair<int, IBlueRow> item) =>
|
||||
deleteRow(item.Key);
|
||||
|
||||
IEnumerator<KeyValuePair<int, IBlueRow>> IEnumerable<KeyValuePair<int, IBlueRow>>.GetEnumerator() {
|
||||
foreach (DictionaryEntry de in myIdIndex) {
|
||||
yield return new((int)de.Key, (IBlueRow)de.Value);
|
||||
}
|
||||
}
|
||||
IEnumerator<KeyValuePair<int, IBlueRow>> IEnumerable<KeyValuePair<int, IBlueRow>>.GetEnumerator() =>
|
||||
rows.GetEnumerator();
|
||||
|
||||
ICollection<int> IDictionary<int, IBlueRow>.Keys =>
|
||||
GenericCollectionWrapper.Create<int>(myIdIndex.Keys);
|
||||
rows.Keys;
|
||||
|
||||
ICollection<IBlueRow> IDictionary<int, IBlueRow>.Values =>
|
||||
Rows;
|
||||
|
||||
@@ -4,8 +4,11 @@ namespace Bts.DataStreamCore;
|
||||
using BCG;
|
||||
|
||||
using bts.api.core;
|
||||
using bts.collections;
|
||||
|
||||
using Bts.DataStreamCore;
|
||||
using mbts.utils;
|
||||
|
||||
using Spark.Core.Collections;
|
||||
|
||||
using System;
|
||||
using System.Collections;
|
||||
@@ -15,14 +18,14 @@ using System.Linq;
|
||||
|
||||
// I would have named this ImmutableBlueTable, but that was already taken.
|
||||
public class BlueTableImmutable : IBlueRowCollection, IImmutableRowCollection {
|
||||
readonly ImmutableDictionary<int, IBlueRow> rows;
|
||||
readonly ImmutableSortedSet<KeyValuePair<int, IBlueRow>> rows;
|
||||
|
||||
public BlueTableImmutable(IBlueTableSchema schema)
|
||||
: this(schema, ImmutableDictionary<int, IBlueRow>.Empty) { }
|
||||
: this(schema, ImmutableSortedSet<KeyValuePair<int, IBlueRow>>.Empty) { }
|
||||
|
||||
public BlueTableImmutable(IBlueTableSchema schema, ImmutableDictionary<int, IBlueRow> rows) {
|
||||
public BlueTableImmutable(IBlueTableSchema schema, ImmutableSortedSet<KeyValuePair<int, IBlueRow>> rows) {
|
||||
this.schema = schema;
|
||||
this.rows = rows;
|
||||
this.rows = rows.WithComparer(new ComparerBuilder<KeyValuePair<int, IBlueRow>>() { kv => kv.Key });
|
||||
}
|
||||
|
||||
public int Count => rows.Count;
|
||||
@@ -30,30 +33,43 @@ public class BlueTableImmutable : IBlueRowCollection, IImmutableRowCollection {
|
||||
public IBlueTableSchema schema { get; }
|
||||
public object SyncRoot => this;
|
||||
|
||||
public IBlueRow this[int id] => rows[id];
|
||||
public IBlueRow this[int id] =>
|
||||
#nullable disable
|
||||
rows.TryGetValue(new(id, default), out var kv)
|
||||
#nullable enable
|
||||
? kv.Value
|
||||
: throw new KeyNotFoundException();
|
||||
|
||||
public bool containsID(int id) =>
|
||||
rows.ContainsKey(id);
|
||||
#nullable disable
|
||||
rows.Contains(new(id, default));
|
||||
#nullable enable
|
||||
|
||||
public void CopyTo(Array array, int index) {
|
||||
foreach (var r in rows.Values) {
|
||||
array.SetValue(r, index++);
|
||||
foreach (var kv in rows) {
|
||||
array.SetValue(kv.Value, index++);
|
||||
}
|
||||
}
|
||||
|
||||
public IEnumerator GetEnumerator() =>
|
||||
rows.Values.GetEnumerator();
|
||||
public IList<object?> GetColumn(int index) =>
|
||||
rows.WrapSelector(kv => kv.Value[index], null);
|
||||
|
||||
public IEnumerator GetEnumerator() {
|
||||
foreach (var kv in rows) {
|
||||
yield return kv.Value;
|
||||
}
|
||||
}
|
||||
|
||||
public DataPacketBuilder ToDataPacketBuilder() =>
|
||||
new(this);
|
||||
|
||||
public class DataPacketBuilder {
|
||||
readonly IBlueTableSchema schema;
|
||||
readonly ImmutableDictionary<int, IBlueRow>.Builder table;
|
||||
readonly ImmutableDictionary<int, IBlueRow>.Builder added =
|
||||
ImmutableDictionary<int, IBlueRow>.Empty.ToBuilder();
|
||||
readonly ImmutableDictionary<int, IBlueRow>.Builder removed =
|
||||
ImmutableDictionary<int, IBlueRow>.Empty.ToBuilder();
|
||||
readonly ImmutableSortedSet<KeyValuePair<int, IBlueRow>>.Builder table;
|
||||
readonly ImmutableSortedSet<KeyValuePair<int, IBlueRow>>.Builder added =
|
||||
ImmutableSortedSet<KeyValuePair<int, IBlueRow>>.Empty.ToBuilder();
|
||||
readonly ImmutableSortedSet<KeyValuePair<int, IBlueRow>>.Builder removed =
|
||||
ImmutableSortedSet<KeyValuePair<int, IBlueRow>>.Empty.ToBuilder();
|
||||
|
||||
public DataPacketBuilder(BlueTableImmutable table) {
|
||||
schema = table.schema;
|
||||
@@ -62,30 +78,37 @@ public class BlueTableImmutable : IBlueRowCollection, IImmutableRowCollection {
|
||||
|
||||
public void AddOrUpdate(IBlueRow row, IEqualityComparer<IBlueRow>? comparer = null) {
|
||||
var id = row.Id();
|
||||
if (table.TryGetValue(id, out var orig)) {
|
||||
if (comparer?.Equals(row, orig) ?? false) {
|
||||
#nullable disable
|
||||
if (table.TryGetValue(new(id, default), out var orig)) {
|
||||
#nullable enable
|
||||
if (comparer?.Equals(row, orig.Value) ?? false) {
|
||||
// No op: This row was already in the table (according to 'comparer').
|
||||
// Remove it from 'added' and 'removed'.
|
||||
added.Remove(id);
|
||||
removed.Remove(id);
|
||||
added.Remove(new(id, row));
|
||||
removed.Remove(new(id, orig.Value));
|
||||
return;
|
||||
}
|
||||
// Change op: A row with this id was already in the table,
|
||||
// but its values have changed (according to 'comparer').
|
||||
removed[id] = orig;
|
||||
removed.Remove(new(id, orig.Value));
|
||||
removed.Add(new(id, orig.Value));
|
||||
}
|
||||
added[id] = row;
|
||||
added.Remove(new(id, row));
|
||||
added.Add(new(id, row));
|
||||
}
|
||||
|
||||
public void Remove(int id) {
|
||||
added.Remove(id);
|
||||
if (table.TryGetValue(id, out var orig)) {
|
||||
removed[id] = orig;
|
||||
#nullable disable
|
||||
added.Remove(new(id, default));
|
||||
if (table.TryGetValue(new(id, default), out var orig)) {
|
||||
#nullable enable
|
||||
removed.Remove(new(id, orig.Value));
|
||||
removed.Add(new(id, orig.Value));
|
||||
}
|
||||
}
|
||||
|
||||
public DataPacket ToDataPacket(bool reset = true) {
|
||||
table.RemoveRange(removed.Keys);
|
||||
table.RemoveRange(removed);
|
||||
table.AddRange(added);
|
||||
var newRows = new BlueTableImmutable(schema, table.ToImmutable());
|
||||
var addedRows = new BlueTableImmutable(schema, added.ToImmutable());
|
||||
@@ -103,12 +126,12 @@ public class BlueTableImmutable : IBlueRowCollection, IImmutableRowCollection {
|
||||
}
|
||||
|
||||
public class BlueTableImmutable<T> : IBlueRowCollection<T> where T : IBlueRow {
|
||||
readonly ImmutableDictionary<int, T> rows;
|
||||
readonly ImmutableSortedSet<KeyValuePair<int, T>> rows;
|
||||
|
||||
public BlueTableImmutable(IBlueTableSchema schema)
|
||||
: this(schema, ImmutableDictionary<int, T>.Empty) { }
|
||||
: this(schema, ImmutableSortedSet<KeyValuePair<int, T>>.Empty) { }
|
||||
|
||||
public BlueTableImmutable(IBlueTableSchema schema, ImmutableDictionary<int, T> rows) {
|
||||
public BlueTableImmutable(IBlueTableSchema schema, ImmutableSortedSet<KeyValuePair<int, T>> rows) {
|
||||
this.schema = schema;
|
||||
this.rows = rows;
|
||||
}
|
||||
@@ -118,19 +141,32 @@ public class BlueTableImmutable<T> : IBlueRowCollection<T> where T : IBlueRow {
|
||||
public IBlueTableSchema schema { get; }
|
||||
public object SyncRoot => this;
|
||||
|
||||
public T this[int id] => rows[id];
|
||||
public T this[int id] =>
|
||||
#nullable disable
|
||||
rows.TryGetValue(new(id, default), out var kv)
|
||||
#nullable enable
|
||||
? kv.Value
|
||||
: throw new KeyNotFoundException();
|
||||
|
||||
public bool containsID(int id) =>
|
||||
rows.ContainsKey(id);
|
||||
#nullable disable
|
||||
rows.Contains(new(id, default));
|
||||
#nullable enable
|
||||
|
||||
public IList<object?> GetColumn(int index) =>
|
||||
rows.WrapSelector(kv => kv.Value[index], null);
|
||||
|
||||
public void CopyTo(Array array, int index) {
|
||||
foreach (var r in rows.Values) {
|
||||
array.SetValue(r, index++);
|
||||
foreach (var (k, v) in rows) {
|
||||
array.SetValue(v, index++);
|
||||
}
|
||||
}
|
||||
|
||||
public IEnumerator<T> GetEnumerator() =>
|
||||
rows.Values.GetEnumerator();
|
||||
public IEnumerator<T> GetEnumerator() {
|
||||
foreach (var kv in rows) {
|
||||
yield return kv.Value;
|
||||
}
|
||||
}
|
||||
|
||||
IEnumerator IEnumerable.GetEnumerator() =>
|
||||
GetEnumerator();
|
||||
@@ -140,11 +176,11 @@ public class BlueTableImmutable<T> : IBlueRowCollection<T> where T : IBlueRow {
|
||||
|
||||
public class DataPacketBuilder {
|
||||
readonly IBlueTableSchema schema;
|
||||
readonly ImmutableDictionary<int, T>.Builder table;
|
||||
readonly ImmutableDictionary<int, T>.Builder added =
|
||||
ImmutableDictionary<int, T>.Empty.ToBuilder();
|
||||
readonly ImmutableDictionary<int, T>.Builder removed =
|
||||
ImmutableDictionary<int, T>.Empty.ToBuilder();
|
||||
readonly ImmutableSortedSet<KeyValuePair<int, T>>.Builder table;
|
||||
readonly ImmutableSortedSet<KeyValuePair<int, T>>.Builder added =
|
||||
ImmutableSortedSet<KeyValuePair<int, T>>.Empty.ToBuilder();
|
||||
readonly ImmutableSortedSet<KeyValuePair<int, T>>.Builder removed =
|
||||
ImmutableSortedSet<KeyValuePair<int, T>>.Empty.ToBuilder();
|
||||
|
||||
public DataPacketBuilder(BlueTableImmutable<T> table) {
|
||||
schema = table.schema;
|
||||
@@ -153,30 +189,37 @@ public class BlueTableImmutable<T> : IBlueRowCollection<T> where T : IBlueRow {
|
||||
|
||||
public void AddOrUpdate(T row, IEqualityComparer<IBlueRow>? comparer = null) {
|
||||
var id = row.Id();
|
||||
if (table.TryGetValue(id, out var orig)) {
|
||||
if (comparer?.Equals(row, orig) ?? false) {
|
||||
#nullable disable
|
||||
if (table.TryGetValue(new(id, default), out var orig)) {
|
||||
#nullable enable
|
||||
if (comparer?.Equals(row, orig.Value) ?? false) {
|
||||
// No op: This row was already in the table (according to 'comparer').
|
||||
// Remove it from 'added' and 'removed'.
|
||||
added.Remove(id);
|
||||
removed.Remove(id);
|
||||
added.Remove(new(id, row));
|
||||
removed.Remove(new(id, orig.Value));
|
||||
return;
|
||||
}
|
||||
// Change op: A row with this id was already in the table,
|
||||
// but its values have changed (according to 'comparer').
|
||||
removed[id] = orig;
|
||||
removed.Remove(orig);
|
||||
removed.Add(orig);
|
||||
}
|
||||
added[id] = row;
|
||||
added.Remove(new(id, row));
|
||||
added.Add(new(id, row));
|
||||
}
|
||||
|
||||
public void Remove(int id) {
|
||||
added.Remove(id);
|
||||
if (table.TryGetValue(id, out var orig)) {
|
||||
removed[id] = orig;
|
||||
#nullable disable
|
||||
added.Remove(new(id, default));
|
||||
if (table.TryGetValue(new(id, default), out var orig)) {
|
||||
#nullable enable
|
||||
removed.Remove(orig);
|
||||
removed.Remove(orig);
|
||||
}
|
||||
}
|
||||
|
||||
public DataPacket ToDataPacket(bool reset = true) {
|
||||
table.RemoveRange(removed.Keys);
|
||||
table.RemoveRange(removed);
|
||||
table.AddRange(added);
|
||||
var newRows = new BlueTableImmutable<T>(schema, table.ToImmutable());
|
||||
var addedRows = new BlueTableImmutable<T>(schema, added.ToImmutable());
|
||||
@@ -189,7 +232,7 @@ public class BlueTableImmutable<T> : IBlueRowCollection<T> where T : IBlueRow {
|
||||
}
|
||||
|
||||
public IDataPacket<T> ToDataPacketT(bool reset = true) {
|
||||
table.RemoveRange(removed.Keys);
|
||||
table.RemoveRange(removed);
|
||||
table.AddRange(added);
|
||||
var newRows = new BlueTableImmutable<T>(schema, table.ToImmutable());
|
||||
var addedRows = new BlueTableImmutable<T>(schema, added.ToImmutable());
|
||||
@@ -207,7 +250,7 @@ public class BlueTableImmutable<T> : IBlueRowCollection<T> where T : IBlueRow {
|
||||
out BlueTableImmutable<T> removedRows,
|
||||
bool reset = true
|
||||
) {
|
||||
table.RemoveRange(removed.Keys);
|
||||
table.RemoveRange(removed);
|
||||
table.AddRange(added);
|
||||
newRows = new BlueTableImmutable<T>(schema, table.ToImmutable());
|
||||
addedRows = new BlueTableImmutable<T>(schema, added.ToImmutable());
|
||||
@@ -226,12 +269,12 @@ public class BlueTableImmutable<T> : IBlueRowCollection<T> where T : IBlueRow {
|
||||
|
||||
public class BlueTableImmutableAdapter<T> : IBlueRowCollection<BlueRowAdapter<T>> {
|
||||
readonly BlueTableSchemaAdapter<T> schemaAdapter;
|
||||
readonly ImmutableDictionary<int, T> rows;
|
||||
readonly ImmutableSortedSet<KeyValuePair<int, T>> rows;
|
||||
|
||||
public BlueTableImmutableAdapter(BlueTableSchemaAdapter<T> schemaAdapter)
|
||||
: this(schemaAdapter, ImmutableDictionary<int, T>.Empty) { }
|
||||
: this(schemaAdapter, ImmutableSortedSet<KeyValuePair<int, T>>.Empty) { }
|
||||
|
||||
public BlueTableImmutableAdapter(BlueTableSchemaAdapter<T> schemaAdapter, ImmutableDictionary<int, T> rows) {
|
||||
public BlueTableImmutableAdapter(BlueTableSchemaAdapter<T> schemaAdapter, ImmutableSortedSet<KeyValuePair<int, T>> rows) {
|
||||
this.schemaAdapter = schemaAdapter;
|
||||
this.rows = rows;
|
||||
}
|
||||
@@ -241,20 +284,26 @@ public class BlueTableImmutableAdapter<T> : IBlueRowCollection<BlueRowAdapter<T>
|
||||
public IBlueTableSchema schema => schemaAdapter.Schema;
|
||||
public object SyncRoot => this;
|
||||
|
||||
public BlueRowAdapter<T> this[int id] => new(schemaAdapter, rows[id]);
|
||||
public BlueRowAdapter<T> this[int id] =>
|
||||
new(schemaAdapter, rows[id].Value);
|
||||
|
||||
public bool containsID(int id) =>
|
||||
rows.ContainsKey(id);
|
||||
#nullable disable
|
||||
rows.Contains(new(id, default));
|
||||
#nullable enable
|
||||
|
||||
public IList<object?> GetColumn(int index) =>
|
||||
rows.WrapSelector(kv => schemaAdapter.GetBlueRow(kv.Value)[index], null);
|
||||
|
||||
public void CopyTo(Array array, int index) {
|
||||
foreach (var r in rows.Values) {
|
||||
array.SetValue(new BlueRowAdapter<T>(schemaAdapter, r), index++);
|
||||
foreach (var kv in rows) {
|
||||
array.SetValue(new BlueRowAdapter<T>(schemaAdapter, kv.Value), index++);
|
||||
}
|
||||
}
|
||||
|
||||
public IEnumerator<BlueRowAdapter<T>> GetEnumerator() {
|
||||
foreach (var row in rows.Values) {
|
||||
yield return new BlueRowAdapter<T>(schemaAdapter, row);
|
||||
foreach (var kv in rows) {
|
||||
yield return new BlueRowAdapter<T>(schemaAdapter, kv.Value);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -263,11 +312,11 @@ public class BlueTableImmutableAdapter<T> : IBlueRowCollection<BlueRowAdapter<T>
|
||||
|
||||
public class DataPacketBuilder {
|
||||
readonly BlueTableSchemaAdapter<T> schemaAdapter;
|
||||
readonly ImmutableDictionary<int, T>.Builder table;
|
||||
readonly ImmutableDictionary<int, T>.Builder added =
|
||||
ImmutableDictionary<int, T>.Empty.ToBuilder();
|
||||
readonly ImmutableDictionary<int, T>.Builder removed =
|
||||
ImmutableDictionary<int, T>.Empty.ToBuilder();
|
||||
readonly ImmutableSortedSet<KeyValuePair<int, T>>.Builder table;
|
||||
readonly ImmutableSortedSet<KeyValuePair<int, T>>.Builder added =
|
||||
ImmutableSortedSet<KeyValuePair<int, T>>.Empty.ToBuilder();
|
||||
readonly ImmutableSortedSet<KeyValuePair<int, T>>.Builder removed =
|
||||
ImmutableSortedSet<KeyValuePair<int, T>>.Empty.ToBuilder();
|
||||
|
||||
public DataPacketBuilder(BlueTableImmutableAdapter<T> table) {
|
||||
schemaAdapter = table.schemaAdapter;
|
||||
@@ -277,26 +326,33 @@ public class BlueTableImmutableAdapter<T> : IBlueRowCollection<BlueRowAdapter<T>
|
||||
public void AddOrUpdate(T rowData, IEqualityComparer<IBlueRow>? comparer = null) {
|
||||
var row = new BlueRowAdapter<T>(schemaAdapter, rowData);
|
||||
var id = row.Id();
|
||||
if (table.TryGetValue(id, out var origData)) {
|
||||
var orig = new BlueRowAdapter<T>(schemaAdapter, origData);
|
||||
#nullable disable
|
||||
if (table.TryGetValue(new(id, default), out var origData)) {
|
||||
#nullable enable
|
||||
var orig = new BlueRowAdapter<T>(schemaAdapter, origData.Value);
|
||||
if (comparer?.Equals(row, orig) ?? false) {
|
||||
// No op: This row was already in the table (according to 'comparer').
|
||||
// Remove it from 'added' and 'removed'.
|
||||
added.Remove(id);
|
||||
removed.Remove(id);
|
||||
added.Remove(origData);
|
||||
removed.Remove(origData);
|
||||
return;
|
||||
}
|
||||
// Change op: A row with this id was already in the table,
|
||||
// but its values have changed (according to 'comparer').
|
||||
removed[id] = origData;
|
||||
removed.Remove(origData);
|
||||
removed.Add(origData);
|
||||
}
|
||||
added[id] = rowData;
|
||||
removed.Remove(new(id, rowData));
|
||||
removed.Add(new(id, rowData));
|
||||
}
|
||||
|
||||
public void Remove(int id) {
|
||||
added.Remove(id);
|
||||
if (table.TryGetValue(id, out var orig)) {
|
||||
removed[id] = orig;
|
||||
#nullable disable
|
||||
added.Remove(new(id, default));
|
||||
if (table.TryGetValue(new(id, default), out var orig)) {
|
||||
#nullable enable
|
||||
removed.Remove(orig);
|
||||
removed.Add(orig);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -310,7 +366,7 @@ public class BlueTableImmutableAdapter<T> : IBlueRowCollection<BlueRowAdapter<T>
|
||||
}
|
||||
|
||||
public IDataPacket<BlueRowAdapter<T>> ToDataPacket(bool reset = true) {
|
||||
table.RemoveRange(removed.Keys);
|
||||
table.RemoveRange(removed);
|
||||
table.AddRange(added);
|
||||
var newRows = new BlueTableImmutableAdapter<T>(schemaAdapter, table.ToImmutable());
|
||||
var addedRows = new BlueTableImmutableAdapter<T>(schemaAdapter, added.ToImmutable());
|
||||
|
||||
@@ -31,6 +31,9 @@ public class BlueTableSelectorWrapper<T> : IBlueRowCollection<T> where T : IBlue
|
||||
public bool containsID(int id) =>
|
||||
source.containsID(id);
|
||||
|
||||
public IList<object?> GetColumn(int index) =>
|
||||
throw new NotImplementedException();
|
||||
|
||||
public IEnumerator<T> GetEnumerator() {
|
||||
foreach (IBlueRow r in source) {
|
||||
yield return selector(r);
|
||||
|
||||
@@ -6,6 +6,8 @@ using System.Linq;
|
||||
|
||||
using BCG;
|
||||
|
||||
using bts.collections;
|
||||
|
||||
namespace bts {
|
||||
public class CircularBufferTable : IBlueRowCollection {
|
||||
private BlueTable _table;
|
||||
@@ -49,5 +51,8 @@ namespace bts {
|
||||
public bool IsSynchronized { get { return false; } }
|
||||
public object SyncRoot { get { return this; } }
|
||||
public System.Collections.IEnumerator GetEnumerator() { return _table.GetEnumerator(); }
|
||||
|
||||
public IList<object> GetColumn(int index) =>
|
||||
_ordered_list.WrapSelector(r => r[index], null);
|
||||
}
|
||||
}
|
||||
@@ -16,6 +16,7 @@ namespace BCG
|
||||
IBlueRow this[int id]{get;}
|
||||
IBlueTableSchema schema {get;}
|
||||
bool containsID (int id);
|
||||
IList<object> GetColumn(int index);
|
||||
|
||||
bool ICollection.IsSynchronized => false;
|
||||
object ICollection.SyncRoot => this;
|
||||
@@ -35,4 +36,14 @@ namespace BCG
|
||||
IEnumerator IEnumerable.GetEnumerator() =>
|
||||
GetEnumerator();
|
||||
}
|
||||
|
||||
public static class BlueRowCollectionExtensions {
|
||||
public static IList<object> GetColumn(this IBlueRowCollection rows, string colName) {
|
||||
var idx = Array.IndexOf(rows.schema.columnNames, colName);
|
||||
if (idx == -1) {
|
||||
throw new KeyNotFoundException();
|
||||
}
|
||||
return rows.GetColumn(idx);
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -1,6 +1,7 @@
|
||||
using bts.collections;
|
||||
|
||||
using System;
|
||||
using BCG;
|
||||
//using BCG.Utilities;
|
||||
using System.Collections.Generic;
|
||||
|
||||
namespace BCG
|
||||
{
|
||||
@@ -87,6 +88,9 @@ namespace BCG
|
||||
{ return null; }
|
||||
}
|
||||
|
||||
public IList<object> GetColumn(int index) =>
|
||||
myRows.WrapSelector(r => r[index], null);
|
||||
|
||||
#endregion
|
||||
|
||||
#region IEnumerable Members
|
||||
|
||||
@@ -1,4 +1,5 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
|
||||
namespace BCG
|
||||
{
|
||||
@@ -132,6 +133,9 @@ namespace BCG
|
||||
return getRow(myGetKeyVals(r));
|
||||
}
|
||||
|
||||
public IList<object> GetColumn(int index) =>
|
||||
throw new NotImplementedException();
|
||||
|
||||
public IBlueRow this[params object[] keyVals] {get {return myHash.getValue (keyVals) as IBlueRow;}}
|
||||
|
||||
private object[] myGetKeyVals (IBlueRow r) {return BlueDataUtility.getSelectedValues (r, myKeys);}
|
||||
|
||||
@@ -1,8 +1,7 @@
|
||||
using System;
|
||||
using bts.collections;
|
||||
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
using System.Diagnostics;
|
||||
|
||||
namespace BCG
|
||||
{
|
||||
@@ -106,6 +105,9 @@ namespace BCG
|
||||
return this.myIDToRows.Value.ContainsKey(id);
|
||||
}
|
||||
|
||||
public IList<object> GetColumn(int index) =>
|
||||
mySortedRows.WrapSelector(r => r[index], null);
|
||||
|
||||
#endregion
|
||||
|
||||
#region ICollection Members
|
||||
|
||||
@@ -48,15 +48,16 @@ namespace BCG
|
||||
public override void addRow(DataRow row)
|
||||
{
|
||||
base.addRow(row);
|
||||
myOrderedRows.Add(myIdIndex[row["id"]]);
|
||||
myOrderedRows.Add(rows[(int)row["id"]]);
|
||||
}
|
||||
|
||||
public override bool deleteRow(int id)
|
||||
{
|
||||
if (myIdIndex[id] == null)
|
||||
if (!rows.TryGetValue(id, out var value)) {
|
||||
return false;
|
||||
myOrderedRows.Remove(myIdIndex[id]);
|
||||
myIdIndex.Remove(id);
|
||||
}
|
||||
myOrderedRows.Remove(value);
|
||||
rows.Remove(id);
|
||||
return true;
|
||||
}
|
||||
|
||||
|
||||
@@ -1,5 +1,7 @@
|
||||
using bts.collections;
|
||||
|
||||
using System;
|
||||
using System.Collections;
|
||||
using System.Collections.Generic;
|
||||
|
||||
namespace BCG
|
||||
{
|
||||
@@ -42,6 +44,9 @@ namespace BCG
|
||||
return id > -1 && id < myRows.Length;
|
||||
}
|
||||
|
||||
public IList<object> GetColumn(int index) =>
|
||||
myRows.WrapSelector(r => r[index], null);
|
||||
|
||||
#endregion
|
||||
|
||||
#region ICollection Members
|
||||
|
||||
@@ -1,6 +1,9 @@
|
||||
using bts.collections;
|
||||
|
||||
using System;
|
||||
using System.ComponentModel;
|
||||
using System.Collections;
|
||||
using System.Collections.Generic;
|
||||
using System.ComponentModel;
|
||||
|
||||
namespace BCG
|
||||
{
|
||||
@@ -51,6 +54,9 @@ namespace BCG
|
||||
|
||||
public IBlueTableSchema schema {get {return mySchema;}}
|
||||
|
||||
public IList<object> GetColumn(int index) =>
|
||||
myRows.WrapSelector(r => ((IBlueRow)r)[index], null);
|
||||
|
||||
#endregion
|
||||
|
||||
#region ICollection Members
|
||||
|
||||
@@ -1,8 +1,8 @@
|
||||
using System;
|
||||
using bts.collections;
|
||||
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
using System.Threading.Tasks;
|
||||
|
||||
namespace BCG
|
||||
{
|
||||
@@ -37,6 +37,9 @@ namespace BCG
|
||||
return (id >= 0 && id < _rows.Count && this[id] != null);
|
||||
}
|
||||
|
||||
public IList<object> GetColumn(int index) =>
|
||||
_rows.WrapSelector(r => r[index], null);
|
||||
|
||||
public void CopyTo(Array array, int index) { _rows.CopyTo((IBlueRow[])array, index);}
|
||||
public int Count { get { return _rows.Count; } }
|
||||
public bool IsSynchronized {get { return false; }}
|
||||
|
||||
Reference in new issue
Block a user