refactor: Make IBlueRowCollection inherit IList<IBlueRow>
This commit is contained in:
1 parent
144b29c728
commit
d2a1b67717
39 files changed
+1035
-439
No files matched your search
@@ -143,7 +143,18 @@ public class BlueColumnTable : IBlueRowCollection {
|
||||
public IList<object> GetColumn(int index) =>
|
||||
columns[index].WrapSelector(v => v.GetValue(), null);
|
||||
|
||||
public IEnumerator GetEnumerator() {
|
||||
public IBlueRow GetRow(int index) =>
|
||||
new MyRow(this, index);
|
||||
|
||||
public int IndexOf(IBlueRow item) {
|
||||
int id = item.Id();
|
||||
var idx = rowIds.BinarySearch(id);
|
||||
return idx != -1 && BlueRowEqualityComparer.Equals(GetRow(idx), item)
|
||||
? idx
|
||||
: -1;
|
||||
}
|
||||
|
||||
public IEnumerator<IBlueRow> GetEnumerator() {
|
||||
var count = rowIds.Count;
|
||||
for (int i = 0; i < count; i++) {
|
||||
yield return new MyRow(this, i);
|
||||
|
||||
@@ -14,7 +14,7 @@ public class BlueRowCollection : IBlueRowCollection {
|
||||
readonly ICollection source;
|
||||
readonly Func<int, IBlueRow> findRow;
|
||||
readonly Func<int, bool> containsKey;
|
||||
IList<IBlueRow>? cachedRows;
|
||||
IList<IBlueRow>? cachedRowsBacking;
|
||||
|
||||
public BlueRowCollection(ICollection source, IBlueTableSchema schema, Func<int, IBlueRow>? findRow = null, Func<int, bool>? containsKey = null) {
|
||||
this.source = source;
|
||||
@@ -45,20 +45,41 @@ public class BlueRowCollection : IBlueRowCollection {
|
||||
this.containsKey = containsKey;
|
||||
}
|
||||
|
||||
IList<IBlueRow> cachedRows =>
|
||||
cachedRowsBacking ??= source as IList<IBlueRow> ?? source.Cast<IBlueRow>().ToList();
|
||||
|
||||
public IBlueRow this[int id] => findRow(id);
|
||||
|
||||
public IBlueTableSchema schema { get; }
|
||||
|
||||
public IBlueRow GetRow(int index) =>
|
||||
cachedRows[index];
|
||||
|
||||
public int IndexOf(IBlueRow item) {
|
||||
int id = item.Id();
|
||||
for (int i = 0; i < cachedRows.Count; i++) {
|
||||
var r = cachedRows[i];
|
||||
if (r.Id() == id) {
|
||||
if (BlueRowEqualityComparer.Equals(r, item)) {
|
||||
return i;
|
||||
}
|
||||
break;
|
||||
}
|
||||
}
|
||||
return -1;
|
||||
}
|
||||
|
||||
public int Count => source.Count;
|
||||
|
||||
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 IList<object?> GetColumn(int index) =>
|
||||
cachedRows.WrapSelector(r => r[index], null);
|
||||
|
||||
public IEnumerator GetEnumerator() =>
|
||||
source.GetEnumerator();
|
||||
public IEnumerator<IBlueRow> GetEnumerator() {
|
||||
foreach (IBlueRow row in source) {
|
||||
yield return row;
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,10 @@
|
||||
namespace Bts.DataStreamCore;
|
||||
|
||||
using BCG;
|
||||
|
||||
using System.Collections.Generic;
|
||||
|
||||
public static class BlueRowCollectionExtensions {
|
||||
public static IList<IBlueRow> AsList(this IBlueRowCollection rowCollection) =>
|
||||
rowCollection;
|
||||
}
|
||||
@@ -93,12 +93,7 @@ namespace BCG {
|
||||
/// Returns the row by the index, interface property
|
||||
/// </summary>
|
||||
public virtual IBlueRow this[int id] {
|
||||
get {
|
||||
object row = rows[id];
|
||||
if (row == null)
|
||||
throw new BlueDataException("ID " + id.ToString() + " does not exist in table " + mySchema.tableName);
|
||||
return (IBlueRow)row;
|
||||
}
|
||||
get => rows[id];
|
||||
set {
|
||||
if ((int)value["id"] != id) {
|
||||
throw new ArgumentException("id arg not equal to row's actual id", nameof(id));
|
||||
@@ -112,7 +107,8 @@ namespace BCG {
|
||||
/// </summary>
|
||||
public virtual IBlueTableSchema schema { get { return mySchema; } }
|
||||
|
||||
public virtual bool containsID(int id) { return rows[id] != null; }
|
||||
public virtual bool containsID(int id) =>
|
||||
rows.ContainsKey(id);
|
||||
|
||||
public virtual bool tryGetValue(int id, out IBlueRow row) =>
|
||||
rows.TryGetValue(id, out row);
|
||||
@@ -219,6 +215,7 @@ namespace BCG {
|
||||
#endregion
|
||||
|
||||
public IDictionary<int, IBlueRow> AsDictionary() => this;
|
||||
public IList<IBlueRow> AsList() => this;
|
||||
|
||||
public IList<object> GetColumn(int index) {
|
||||
if (index < 0 || index >= schema.columnInfo.Length) {
|
||||
@@ -227,8 +224,11 @@ namespace BCG {
|
||||
return Rows.WrapSelector(r => r[index], null);
|
||||
}
|
||||
|
||||
public IBlueRow GetRow(int index) =>
|
||||
Rows[index];
|
||||
|
||||
#region ICollection members
|
||||
public virtual IEnumerator GetEnumerator() =>
|
||||
public virtual IEnumerator<IBlueRow> GetEnumerator() =>
|
||||
rows.Values.GetEnumerator();
|
||||
|
||||
public virtual bool IsSynchronized { get { return false; } }
|
||||
@@ -243,6 +243,18 @@ namespace BCG {
|
||||
|
||||
public virtual int Count =>
|
||||
rows.Count;
|
||||
|
||||
public int IndexOf(IBlueRow item) {
|
||||
int id = item.Id();
|
||||
int index = rows.IndexOfKey(id);
|
||||
if (index != -1) {
|
||||
var row = rows.GetValueAtIndex(index);
|
||||
if (BlueRowEqualityComparer.Equals(row, item)) {
|
||||
return index;
|
||||
}
|
||||
}
|
||||
return -1;
|
||||
}
|
||||
#endregion
|
||||
|
||||
#region IDictionary<int, IBlueRow> members
|
||||
@@ -268,7 +280,7 @@ namespace BCG {
|
||||
|
||||
bool ICollection<KeyValuePair<int, IBlueRow>>.Contains(KeyValuePair<int, IBlueRow> item) =>
|
||||
tryGetValue(item.Key, out var r)
|
||||
&& BlueRowEqualityComparer.Instance.Equals(r, item.Value);
|
||||
&& BlueRowEqualityComparer.Equals(r, item.Value);
|
||||
|
||||
void ICollection<KeyValuePair<int, IBlueRow>>.CopyTo(KeyValuePair<int, IBlueRow>[] array, int arrayIndex) {
|
||||
foreach (var kv in rows) {
|
||||
|
||||
@@ -6,12 +6,9 @@ using BCG;
|
||||
using bts.api.core;
|
||||
using bts.collections;
|
||||
|
||||
using mbts.utils;
|
||||
|
||||
using Spark.Core.Collections;
|
||||
|
||||
using System;
|
||||
using System.Collections;
|
||||
using System.Collections.Generic;
|
||||
using System.Collections.Immutable;
|
||||
using System.Linq;
|
||||
@@ -21,11 +18,11 @@ public class BlueTableImmutable : IBlueRowCollection, IImmutableRowCollection {
|
||||
readonly ImmutableSortedSet<KeyValuePair<int, IBlueRow>> rows;
|
||||
|
||||
public BlueTableImmutable(IBlueTableSchema schema)
|
||||
: this(schema, ImmutableSortedSet<KeyValuePair<int, IBlueRow>>.Empty) { }
|
||||
: this(schema, ImmutableSortedSet.Create(KeyValuePairComparer.KeyComparer<int, IBlueRow>())) { }
|
||||
|
||||
public BlueTableImmutable(IBlueTableSchema schema, ImmutableSortedSet<KeyValuePair<int, IBlueRow>> rows) {
|
||||
this.schema = schema;
|
||||
this.rows = rows.WithComparer(new ComparerBuilder<KeyValuePair<int, IBlueRow>>() { kv => kv.Key });
|
||||
this.rows = rows;
|
||||
}
|
||||
|
||||
public int Count => rows.Count;
|
||||
@@ -40,6 +37,25 @@ public class BlueTableImmutable : IBlueRowCollection, IImmutableRowCollection {
|
||||
? kv.Value
|
||||
: throw new KeyNotFoundException();
|
||||
|
||||
IBlueRow IList<IBlueRow>.this[int index] {
|
||||
get => GetRow(index);
|
||||
set => throw new NotSupportedException();
|
||||
}
|
||||
|
||||
public IBlueRow GetRow(int index) =>
|
||||
rows[index].Value;
|
||||
|
||||
public int IndexOf(IBlueRow item) {
|
||||
int id = item.Id();
|
||||
if (rows.TryGetValue(new(id, item), out var rowKvp)) {
|
||||
var vals = schema.columnInfo.Select(c => rowKvp.Value[c.colNo]);
|
||||
var itemVals = item.schema.columnInfo.Select(c => item[c.colNo]);
|
||||
if (vals.SequenceEqual(itemVals)) {
|
||||
return rows.IndexOf(rowKvp);
|
||||
}
|
||||
}
|
||||
return -1;
|
||||
}
|
||||
public bool containsID(int id) =>
|
||||
#nullable disable
|
||||
rows.Contains(new(id, default));
|
||||
@@ -54,7 +70,7 @@ public class BlueTableImmutable : IBlueRowCollection, IImmutableRowCollection {
|
||||
public IList<object?> GetColumn(int index) =>
|
||||
rows.WrapSelector(kv => kv.Value[index], null);
|
||||
|
||||
public IEnumerator GetEnumerator() {
|
||||
public IEnumerator<IBlueRow> GetEnumerator() {
|
||||
foreach (var kv in rows) {
|
||||
yield return kv.Value;
|
||||
}
|
||||
@@ -67,9 +83,9 @@ public class BlueTableImmutable : IBlueRowCollection, IImmutableRowCollection {
|
||||
readonly IBlueTableSchema schema;
|
||||
readonly ImmutableSortedSet<KeyValuePair<int, IBlueRow>>.Builder table;
|
||||
readonly ImmutableSortedSet<KeyValuePair<int, IBlueRow>>.Builder added =
|
||||
ImmutableSortedSet<KeyValuePair<int, IBlueRow>>.Empty.ToBuilder();
|
||||
ImmutableSortedSet.CreateBuilder(KeyValuePairComparer.KeyComparer<int, IBlueRow>());
|
||||
readonly ImmutableSortedSet<KeyValuePair<int, IBlueRow>>.Builder removed =
|
||||
ImmutableSortedSet<KeyValuePair<int, IBlueRow>>.Empty.ToBuilder();
|
||||
ImmutableSortedSet.CreateBuilder(KeyValuePairComparer.KeyComparer<int, IBlueRow>());
|
||||
|
||||
public DataPacketBuilder(BlueTableImmutable table) {
|
||||
schema = table.schema;
|
||||
@@ -128,154 +144,154 @@ public class BlueTableImmutable : IBlueRowCollection, IImmutableRowCollection {
|
||||
}
|
||||
}
|
||||
|
||||
public class BlueTableImmutable<T> : IBlueRowCollection<T> where T : IBlueRow {
|
||||
readonly ImmutableSortedSet<KeyValuePair<int, T>> rows;
|
||||
// public class BlueTableImmutable<T> : IBlueRowCollection<T> where T : IBlueRow {
|
||||
// readonly ImmutableSortedSet<KeyValuePair<int, T>> rows;
|
||||
|
||||
public BlueTableImmutable(IBlueTableSchema schema)
|
||||
: this(schema, ImmutableSortedSet<KeyValuePair<int, T>>.Empty) { }
|
||||
// public BlueTableImmutable(IBlueTableSchema schema)
|
||||
// : this(schema, ImmutableSortedSet<KeyValuePair<int, T>>.Empty) { }
|
||||
|
||||
public BlueTableImmutable(IBlueTableSchema schema, ImmutableSortedSet<KeyValuePair<int, T>> rows) {
|
||||
this.schema = schema;
|
||||
this.rows = rows;
|
||||
}
|
||||
// public BlueTableImmutable(IBlueTableSchema schema, ImmutableSortedSet<KeyValuePair<int, T>> rows) {
|
||||
// this.schema = schema;
|
||||
// this.rows = rows;
|
||||
// }
|
||||
|
||||
public int Count => rows.Count;
|
||||
public bool IsSynchronized => false;
|
||||
public IBlueTableSchema schema { get; }
|
||||
public object SyncRoot => this;
|
||||
// public int Count => rows.Count;
|
||||
// public bool IsSynchronized => false;
|
||||
// public IBlueTableSchema schema { get; }
|
||||
// public object SyncRoot => this;
|
||||
|
||||
public T this[int id] =>
|
||||
#nullable disable
|
||||
rows.TryGetValue(new(id, default), out var kv)
|
||||
#nullable enable
|
||||
? kv.Value
|
||||
: throw new KeyNotFoundException();
|
||||
// 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) =>
|
||||
#nullable disable
|
||||
rows.Contains(new(id, default));
|
||||
#nullable enable
|
||||
// public bool containsID(int id) =>
|
||||
// #nullable disable
|
||||
// rows.Contains(new(id, default));
|
||||
// #nullable enable
|
||||
|
||||
public IList<object?> GetColumn(int index) =>
|
||||
rows.WrapSelector(kv => kv.Value[index], null);
|
||||
// public IList<object?> GetColumn(int index) =>
|
||||
// rows.WrapSelector(kv => kv.Value[index], null);
|
||||
|
||||
public void CopyTo(Array array, int index) {
|
||||
foreach (var (k, v) in rows) {
|
||||
array.SetValue(v, index++);
|
||||
}
|
||||
}
|
||||
// public void CopyTo(Array array, int index) {
|
||||
// foreach (var (k, v) in rows) {
|
||||
// array.SetValue(v, index++);
|
||||
// }
|
||||
// }
|
||||
|
||||
public IEnumerator<T> GetEnumerator() {
|
||||
foreach (var kv in rows) {
|
||||
yield return kv.Value;
|
||||
}
|
||||
}
|
||||
// public IEnumerator<T> GetEnumerator() {
|
||||
// foreach (var kv in rows) {
|
||||
// yield return kv.Value;
|
||||
// }
|
||||
// }
|
||||
|
||||
IEnumerator IEnumerable.GetEnumerator() =>
|
||||
GetEnumerator();
|
||||
// IEnumerator IEnumerable.GetEnumerator() =>
|
||||
// GetEnumerator();
|
||||
|
||||
public DataPacketBuilder ToDataPacketBuilder() =>
|
||||
new(this);
|
||||
// public DataPacketBuilder ToDataPacketBuilder() =>
|
||||
// new(this);
|
||||
|
||||
public class DataPacketBuilder {
|
||||
readonly IBlueTableSchema schema;
|
||||
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 class DataPacketBuilder {
|
||||
// readonly IBlueTableSchema schema;
|
||||
// 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;
|
||||
this.table = table.rows.ToBuilder();
|
||||
}
|
||||
// public DataPacketBuilder(BlueTableImmutable<T> table) {
|
||||
// schema = table.schema;
|
||||
// this.table = table.rows.ToBuilder();
|
||||
// }
|
||||
|
||||
public void AddOrUpdate(T row, IEqualityComparer<IBlueRow>? comparer = null) {
|
||||
var id = row.Id();
|
||||
#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(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.Remove(orig);
|
||||
removed.Add(orig);
|
||||
}
|
||||
added.Remove(new(id, row));
|
||||
added.Add(new(id, row));
|
||||
}
|
||||
// public void AddOrUpdate(T row, IEqualityComparer<IBlueRow>? comparer = null) {
|
||||
// var id = row.Id();
|
||||
// #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(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.Remove(orig);
|
||||
// removed.Add(orig);
|
||||
// }
|
||||
// added.Remove(new(id, row));
|
||||
// added.Add(new(id, row));
|
||||
// }
|
||||
|
||||
public void Remove(int id) {
|
||||
#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 void Remove(int id) {
|
||||
// #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);
|
||||
table.AddRange(added);
|
||||
var newRows = new BlueTableImmutable<T>(schema, table.ToImmutable());
|
||||
var addedRows = new BlueTableImmutable<T>(schema, added.ToImmutable());
|
||||
var removedRows = new BlueTableImmutable<T>(schema, removed.ToImmutable());
|
||||
if (reset) {
|
||||
added.Clear();
|
||||
removed.Clear();
|
||||
}
|
||||
return new(newRows, addedRows, removedRows);
|
||||
}
|
||||
// public DataPacket ToDataPacket(bool reset = true) {
|
||||
// table.RemoveRange(removed);
|
||||
// table.AddRange(added);
|
||||
// var newRows = new BlueTableImmutable<T>(schema, table.ToImmutable());
|
||||
// var addedRows = new BlueTableImmutable<T>(schema, added.ToImmutable());
|
||||
// var removedRows = new BlueTableImmutable<T>(schema, removed.ToImmutable());
|
||||
// if (reset) {
|
||||
// added.Clear();
|
||||
// removed.Clear();
|
||||
// }
|
||||
// return new(newRows, addedRows, removedRows);
|
||||
// }
|
||||
|
||||
public IDataPacket<T> ToDataPacketT(bool reset = true) {
|
||||
table.RemoveRange(removed);
|
||||
table.AddRange(added);
|
||||
var newRows = new BlueTableImmutable<T>(schema, table.ToImmutable());
|
||||
var addedRows = new BlueTableImmutable<T>(schema, added.ToImmutable());
|
||||
var removedRows = new BlueTableImmutable<T>(schema, removed.ToImmutable());
|
||||
if (reset) {
|
||||
added.Clear();
|
||||
removed.Clear();
|
||||
}
|
||||
return new DataPacket<T>(newRows, addedRows, removedRows);
|
||||
}
|
||||
// public IDataPacket<T> ToDataPacketT(bool reset = true) {
|
||||
// table.RemoveRange(removed);
|
||||
// table.AddRange(added);
|
||||
// var newRows = new BlueTableImmutable<T>(schema, table.ToImmutable());
|
||||
// var addedRows = new BlueTableImmutable<T>(schema, added.ToImmutable());
|
||||
// var removedRows = new BlueTableImmutable<T>(schema, removed.ToImmutable());
|
||||
// if (reset) {
|
||||
// added.Clear();
|
||||
// removed.Clear();
|
||||
// }
|
||||
// return new DataPacket<T>(newRows, addedRows, removedRows);
|
||||
// }
|
||||
|
||||
public IDataPacket<T> ToDataPacketT(
|
||||
out BlueTableImmutable<T> newRows,
|
||||
out BlueTableImmutable<T> addedRows,
|
||||
out BlueTableImmutable<T> removedRows,
|
||||
bool reset = true
|
||||
) {
|
||||
table.RemoveRange(removed);
|
||||
table.AddRange(added);
|
||||
newRows = new BlueTableImmutable<T>(schema, table.ToImmutable());
|
||||
addedRows = new BlueTableImmutable<T>(schema, added.ToImmutable());
|
||||
removedRows = new BlueTableImmutable<T>(schema, removed.ToImmutable());
|
||||
if (reset) {
|
||||
added.Clear();
|
||||
removed.Clear();
|
||||
}
|
||||
return new DataPacket<T>(newRows, addedRows, removedRows);
|
||||
}
|
||||
// public IDataPacket<T> ToDataPacketT(
|
||||
// out BlueTableImmutable<T> newRows,
|
||||
// out BlueTableImmutable<T> addedRows,
|
||||
// out BlueTableImmutable<T> removedRows,
|
||||
// bool reset = true
|
||||
// ) {
|
||||
// table.RemoveRange(removed);
|
||||
// table.AddRange(added);
|
||||
// newRows = new BlueTableImmutable<T>(schema, table.ToImmutable());
|
||||
// addedRows = new BlueTableImmutable<T>(schema, added.ToImmutable());
|
||||
// removedRows = new BlueTableImmutable<T>(schema, removed.ToImmutable());
|
||||
// if (reset) {
|
||||
// added.Clear();
|
||||
// removed.Clear();
|
||||
// }
|
||||
// return new DataPacket<T>(newRows, addedRows, removedRows);
|
||||
// }
|
||||
|
||||
public BlueTableImmutable<T> ToImmutable() =>
|
||||
new(schema, table.ToImmutable());
|
||||
}
|
||||
}
|
||||
// public BlueTableImmutable<T> ToImmutable() =>
|
||||
// new(schema, table.ToImmutable());
|
||||
// }
|
||||
// }
|
||||
|
||||
public class BlueTableImmutableAdapter<T> : IBlueRowCollection<BlueRowAdapter<T>> {
|
||||
public class BlueTableImmutableAdapter<T> : IBlueRowCollection {
|
||||
readonly BlueTableSchemaAdapter<T> schemaAdapter;
|
||||
readonly ImmutableSortedSet<KeyValuePair<int, T>> rows;
|
||||
|
||||
public BlueTableImmutableAdapter(BlueTableSchemaAdapter<T> schemaAdapter)
|
||||
: this(schemaAdapter, ImmutableSortedSet<KeyValuePair<int, T>>.Empty) { }
|
||||
: this(schemaAdapter, ImmutableSortedSet.Create(KeyValuePairComparer.KeyComparer<int, T>())) { }
|
||||
|
||||
public BlueTableImmutableAdapter(BlueTableSchemaAdapter<T> schemaAdapter, ImmutableSortedSet<KeyValuePair<int, T>> rows) {
|
||||
this.schemaAdapter = schemaAdapter;
|
||||
@@ -287,8 +303,8 @@ 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].Value);
|
||||
public IBlueRow this[int id] =>
|
||||
new BlueRowAdapter<T>(schemaAdapter, rows[id].Value);
|
||||
|
||||
public bool containsID(int id) =>
|
||||
#nullable disable
|
||||
@@ -298,13 +314,30 @@ public class BlueTableImmutableAdapter<T> : IBlueRowCollection<BlueRowAdapter<T>
|
||||
public IList<object?> GetColumn(int index) =>
|
||||
rows.WrapSelector(kv => schemaAdapter.GetBlueRow(kv.Value)[index], null);
|
||||
|
||||
public IBlueRow GetRow(int index) =>
|
||||
new BlueRowAdapter<T>(schemaAdapter, rows[index].Value);
|
||||
|
||||
public void CopyTo(Array array, int index) {
|
||||
foreach (var kv in rows) {
|
||||
array.SetValue(new BlueRowAdapter<T>(schemaAdapter, kv.Value), index++);
|
||||
}
|
||||
}
|
||||
|
||||
public IEnumerator<BlueRowAdapter<T>> GetEnumerator() {
|
||||
public int IndexOf(IBlueRow item) {
|
||||
int id = item.Id();
|
||||
#nullable disable
|
||||
int index = rows.IndexOf(new(id, default));
|
||||
if (index != -1) {
|
||||
var r = this[index];
|
||||
if (BlueRowEqualityComparer.Equals(r, item)) {
|
||||
return index;
|
||||
}
|
||||
}
|
||||
#nullable enable
|
||||
return -1;
|
||||
}
|
||||
|
||||
public IEnumerator<IBlueRow> GetEnumerator() {
|
||||
foreach (var kv in rows) {
|
||||
yield return new BlueRowAdapter<T>(schemaAdapter, kv.Value);
|
||||
}
|
||||
@@ -315,11 +348,12 @@ public class BlueTableImmutableAdapter<T> : IBlueRowCollection<BlueRowAdapter<T>
|
||||
|
||||
public class DataPacketBuilder {
|
||||
readonly BlueTableSchemaAdapter<T> schemaAdapter;
|
||||
readonly ImmutableSortedSet<KeyValuePair<int, T>>.Builder table;
|
||||
readonly ImmutableSortedSet<KeyValuePair<int, T>>.Builder table =
|
||||
ImmutableSortedSet.CreateBuilder(KeyValuePairComparer.KeyComparer<int, T>());
|
||||
readonly ImmutableSortedSet<KeyValuePair<int, T>>.Builder added =
|
||||
ImmutableSortedSet<KeyValuePair<int, T>>.Empty.ToBuilder();
|
||||
ImmutableSortedSet.CreateBuilder(KeyValuePairComparer.KeyComparer<int, T>());
|
||||
readonly ImmutableSortedSet<KeyValuePair<int, T>>.Builder removed =
|
||||
ImmutableSortedSet<KeyValuePair<int, T>>.Empty.ToBuilder();
|
||||
ImmutableSortedSet.CreateBuilder(KeyValuePairComparer.KeyComparer<int, T>());
|
||||
|
||||
public DataPacketBuilder(BlueTableImmutableAdapter<T> table) {
|
||||
schemaAdapter = table.schemaAdapter;
|
||||
@@ -340,13 +374,10 @@ public class BlueTableImmutableAdapter<T> : IBlueRowCollection<BlueRowAdapter<T>
|
||||
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.Remove(origData);
|
||||
removed.Add(origData);
|
||||
}
|
||||
removed.Remove(new(id, rowData));
|
||||
removed.Add(new(id, rowData));
|
||||
added.Remove(new(id, default!));
|
||||
added.Add(new(id, rowData));
|
||||
}
|
||||
|
||||
public void Remove(int id) {
|
||||
@@ -367,11 +398,10 @@ public class BlueTableImmutableAdapter<T> : IBlueRowCollection<BlueRowAdapter<T>
|
||||
AddOrUpdate(r);
|
||||
}
|
||||
}
|
||||
|
||||
public IDataPacket<BlueRowAdapter<T>> ToDataPacket(bool reset = true) =>
|
||||
public IDataPacket ToDataPacket(bool reset = true) =>
|
||||
ToDataPacket(out _, reset);
|
||||
|
||||
public IDataPacket<BlueRowAdapter<T>> ToDataPacket(out BlueTableImmutableAdapter<T> newRows, bool reset = true) {
|
||||
public IDataPacket ToDataPacket(out IBlueRowCollection newRows, bool reset = true) {
|
||||
table.RemoveRange(removed);
|
||||
table.AddRange(added);
|
||||
newRows = new BlueTableImmutableAdapter<T>(schemaAdapter, table.ToImmutable());
|
||||
@@ -381,7 +411,7 @@ public class BlueTableImmutableAdapter<T> : IBlueRowCollection<BlueRowAdapter<T>
|
||||
added.Clear();
|
||||
removed.Clear();
|
||||
}
|
||||
return new DataPacket<BlueRowAdapter<T>>(newRows, addedRows, removedRows);
|
||||
return new DataPacket(newRows, addedRows, removedRows);
|
||||
}
|
||||
|
||||
public BlueTableImmutableAdapter<T> ToImmutable() =>
|
||||
|
||||
@@ -6,37 +6,37 @@ using BCG;
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
|
||||
public static class BlueTableSelectorWrapper {
|
||||
public static BlueTableSelectorWrapper<T> Create<T>(
|
||||
IBlueRowCollection source, Func<IBlueRow, T> selector, IBlueTableSchema? schema = null) where T : IBlueRow =>
|
||||
new(source, selector, schema);
|
||||
}
|
||||
// public static class BlueTableSelectorWrapper {
|
||||
// public static BlueTableSelectorWrapper<T> Create<T>(
|
||||
// IBlueRowCollection source, Func<IBlueRow, T> selector, IBlueTableSchema? schema = null) where T : IBlueRow =>
|
||||
// new(source, selector, schema);
|
||||
// }
|
||||
|
||||
public class BlueTableSelectorWrapper<T> : IBlueRowCollection<T> where T : IBlueRow {
|
||||
readonly IBlueRowCollection source;
|
||||
readonly Func<IBlueRow, T> selector;
|
||||
// public class BlueTableSelectorWrapper<T> : IBlueRowCollection where T : IBlueRow {
|
||||
// readonly IBlueRowCollection source;
|
||||
// readonly Func<IBlueRow, T> selector;
|
||||
|
||||
public BlueTableSelectorWrapper(IBlueRowCollection source, Func<IBlueRow, T> selector, IBlueTableSchema? schema = null) {
|
||||
this.source = source;
|
||||
this.selector = selector;
|
||||
this.schema = schema ?? source.schema;
|
||||
}
|
||||
// public BlueTableSelectorWrapper(IBlueRowCollection source, Func<IBlueRow, T> selector, IBlueTableSchema? schema = null) {
|
||||
// this.source = source;
|
||||
// this.selector = selector;
|
||||
// this.schema = schema ?? source.schema;
|
||||
// }
|
||||
|
||||
public T this[int id] => selector(source[id]);
|
||||
// public T this[int id] => selector(source[id]);
|
||||
|
||||
public IBlueTableSchema schema { get; }
|
||||
// public IBlueTableSchema schema { get; }
|
||||
|
||||
public int Count => source.Count;
|
||||
// public int Count => source.Count;
|
||||
|
||||
public bool containsID(int id) =>
|
||||
source.containsID(id);
|
||||
// public bool containsID(int id) =>
|
||||
// source.containsID(id);
|
||||
|
||||
public IList<object?> GetColumn(int index) =>
|
||||
throw new NotImplementedException();
|
||||
// public IList<object?> GetColumn(int index) =>
|
||||
// throw new NotImplementedException();
|
||||
|
||||
public IEnumerator<T> GetEnumerator() {
|
||||
foreach (IBlueRow r in source) {
|
||||
yield return selector(r);
|
||||
}
|
||||
}
|
||||
}
|
||||
// public IEnumerator<T> GetEnumerator() {
|
||||
// foreach (IBlueRow r in source) {
|
||||
// yield return selector(r);
|
||||
// }
|
||||
// }
|
||||
// }
|
||||
@@ -1,6 +1,7 @@
|
||||
using System;
|
||||
using System.Collections;
|
||||
using System.Collections.Generic;
|
||||
using System.Collections.Immutable;
|
||||
using System.Data;
|
||||
using System.Linq;
|
||||
|
||||
@@ -8,51 +9,83 @@ using BCG;
|
||||
|
||||
using bts.collections;
|
||||
|
||||
using Bts.DataStreamCore;
|
||||
|
||||
namespace bts {
|
||||
public class CircularBufferTable : IBlueRowCollection {
|
||||
private BlueTable _table;
|
||||
private List<IBlueRow> _ordered_list = new List<IBlueRow>();
|
||||
readonly ImmutableList<IBlueRow> orderedRows;
|
||||
readonly ImmutableDictionary<int, IBlueRow> rowIndex;
|
||||
private int _buffer_sz;
|
||||
public CircularBufferTable(IBlueTableSchema schema, int buffer_sz) {
|
||||
_table = new BlueTable(schema);
|
||||
_buffer_sz = buffer_sz;
|
||||
public CircularBufferTable(IBlueTableSchema schema, int buffer_sz)
|
||||
: this(schema, buffer_sz, ImmutableList<IBlueRow>.Empty, ImmutableDictionary<int, IBlueRow>.Empty) { }
|
||||
|
||||
CircularBufferTable(IBlueTableSchema schema, int bufferSize, ImmutableList<IBlueRow> orderedRows, ImmutableDictionary<int, IBlueRow> rowIndex) {
|
||||
this.schema = schema;
|
||||
_buffer_sz = bufferSize;
|
||||
this.orderedRows = orderedRows;
|
||||
this.rowIndex = rowIndex;
|
||||
}
|
||||
|
||||
public CircularBufferTable Add(IBlueRow row) =>
|
||||
Add(row, out _);
|
||||
|
||||
// returns the row that might need to be deleted to keep
|
||||
// the circular buffer intact
|
||||
public IBlueRow add_row(IBlueRow row) {
|
||||
_ordered_list.Add(row);
|
||||
_table.addRow(row);
|
||||
if (_ordered_list.Count > _buffer_sz) {
|
||||
IBlueRow rv = _ordered_list.ElementAt(0);
|
||||
_ordered_list.RemoveAt(0);
|
||||
_table.deleteRow(rv);
|
||||
return rv;
|
||||
public CircularBufferTable Add(IBlueRow row, out IBlueRow oldRow) {
|
||||
int id = row.Id();
|
||||
var rowIndexBd = rowIndex.ToBuilder();
|
||||
var orderedRowsBd = orderedRows.ToBuilder();
|
||||
if (rowIndexBd.TryGetValue(id, out oldRow)) {
|
||||
Log.assert(orderedRowsBd.RemoveFirst(r => r.Id() == id));
|
||||
} else if (rowIndexBd.Count > _buffer_sz) {
|
||||
oldRow = orderedRowsBd[0];
|
||||
rowIndexBd.Remove(oldRow.Id());
|
||||
orderedRowsBd.RemoveAt(0);
|
||||
}
|
||||
return null;
|
||||
rowIndexBd[id] = row;
|
||||
orderedRowsBd.Add(row);
|
||||
return new(schema, _buffer_sz, orderedRowsBd.ToImmutable(), rowIndexBd.ToImmutable());
|
||||
}
|
||||
|
||||
public IBlueRow this[int id] {
|
||||
get {
|
||||
return _table[id];
|
||||
}
|
||||
}
|
||||
public IBlueTableSchema schema {
|
||||
get {
|
||||
return _table.schema;
|
||||
return orderedRows[id];
|
||||
}
|
||||
}
|
||||
public IBlueTableSchema schema { get; }
|
||||
public bool containsID(int id) {
|
||||
return _table.containsID(id);
|
||||
return rowIndex.ContainsKey(id);
|
||||
}
|
||||
|
||||
public void CopyTo(Array array, int index) { _table.CopyTo((IBlueRow[])array, index); }
|
||||
public int Count { get { return _table.Count; } }
|
||||
public void CopyTo(Array array, int index) {
|
||||
foreach (var row in orderedRows) {
|
||||
array.SetValue(row, index++);
|
||||
}
|
||||
}
|
||||
public int Count => rowIndex.Count;
|
||||
public bool IsSynchronized { get { return false; } }
|
||||
public object SyncRoot { get { return this; } }
|
||||
public System.Collections.IEnumerator GetEnumerator() { return _table.GetEnumerator(); }
|
||||
public IEnumerator<IBlueRow> GetEnumerator() =>
|
||||
orderedRows.GetEnumerator();
|
||||
|
||||
public IList<object> GetColumn(int index) =>
|
||||
_ordered_list.WrapSelector(r => r[index], null);
|
||||
orderedRows.WrapSelector(r => r[index], null);
|
||||
|
||||
public IBlueRow GetRow(int index) =>
|
||||
orderedRows[index];
|
||||
|
||||
public int IndexOf(IBlueRow item) {
|
||||
int id = item.Id();
|
||||
if (rowIndex.TryGetValue(id, out var row) && BlueRowEqualityComparer.Equals(item, row)) {
|
||||
int i = 0;
|
||||
foreach (var r in orderedRows) {
|
||||
if (r.Id() == id) {
|
||||
return i;
|
||||
}
|
||||
i++;
|
||||
}
|
||||
}
|
||||
return -1;
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -1,3 +1,5 @@
|
||||
using bts.collections;
|
||||
|
||||
using System;
|
||||
using System.Collections;
|
||||
using System.Collections.Generic;
|
||||
@@ -8,30 +10,47 @@ namespace BCG
|
||||
/// Represents a readonly, very primitive form of dotNet DataTable. All it has has an indexer for
|
||||
/// getting the rows
|
||||
/// </summary>
|
||||
public interface IBlueRowCollection : ICollection
|
||||
{
|
||||
public interface IBlueRowCollection : IList<IBlueRow> {
|
||||
/// <summary>
|
||||
/// Gets the row corresponding to the id
|
||||
/// </summary>
|
||||
IBlueRow this[int id]{get;}
|
||||
IBlueTableSchema schema {get;}
|
||||
bool containsID (int id);
|
||||
IList<object> GetColumn(int index);
|
||||
new IBlueRow this[int id] { get; }
|
||||
IBlueTableSchema schema { get; }
|
||||
bool containsID(int id);
|
||||
IList<object> GetColumn(int index) =>
|
||||
this.WrapSelector(r => r[index], null);
|
||||
|
||||
bool ICollection.IsSynchronized => false;
|
||||
object ICollection.SyncRoot => this;
|
||||
IBlueRow GetRow(int index);
|
||||
|
||||
void ICollection.CopyTo(Array array, int index) {
|
||||
foreach (IBlueRow r in this) {
|
||||
array.SetValue(r, index++);
|
||||
bool ICollection<IBlueRow>.IsReadOnly => true;
|
||||
IBlueRow IList<IBlueRow>.this[int index] {
|
||||
get => GetRow(index);
|
||||
set => throw new NotSupportedException();
|
||||
}
|
||||
|
||||
bool ICollection<IBlueRow>.Contains(IBlueRow item) =>
|
||||
IndexOf(item) != -1;
|
||||
|
||||
void ICollection<IBlueRow>.CopyTo(IBlueRow[] array, int arrayIndex) {
|
||||
foreach (var row in this) {
|
||||
array[arrayIndex++] = row;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public interface IBlueRowCollection<T> : IBlueRowCollection, IReadOnlyCollection<T> where T : IBlueRow {
|
||||
new T this[int id] { get; }
|
||||
void ICollection<IBlueRow>.Add(IBlueRow item) =>
|
||||
throw new NotSupportedException();
|
||||
|
||||
IBlueRow IBlueRowCollection.this[int id] => this[id];
|
||||
void ICollection<IBlueRow>.Clear() =>
|
||||
throw new NotSupportedException();
|
||||
|
||||
bool ICollection<IBlueRow>.Remove(IBlueRow item) =>
|
||||
throw new NotSupportedException();
|
||||
|
||||
void IList<IBlueRow>.RemoveAt(int index) =>
|
||||
throw new NotSupportedException();
|
||||
|
||||
void IList<IBlueRow>.Insert(int index, IBlueRow item) =>
|
||||
throw new NotSupportedException();
|
||||
|
||||
IEnumerator IEnumerable.GetEnumerator() =>
|
||||
GetEnumerator();
|
||||
|
||||
@@ -1,5 +1,7 @@
|
||||
using bts.collections;
|
||||
|
||||
using Bts.DataStreamCore;
|
||||
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
|
||||
@@ -48,6 +50,11 @@ namespace BCG
|
||||
}
|
||||
}
|
||||
|
||||
IBlueRow IList<IBlueRow>.this[int index] {
|
||||
get => GetRow(index);
|
||||
set => throw new NotSupportedException();
|
||||
}
|
||||
|
||||
public IBlueTableSchema schema
|
||||
{
|
||||
get
|
||||
@@ -59,6 +66,8 @@ namespace BCG
|
||||
public bool containsID(int id)
|
||||
{ return myHash.isPresent(id); }
|
||||
|
||||
public IBlueRow GetRow(int index) =>
|
||||
myRows[index];
|
||||
#endregion
|
||||
|
||||
#region ICollection Members
|
||||
@@ -82,6 +91,17 @@ namespace BCG
|
||||
myRows.CopyTo(array, index);
|
||||
}
|
||||
|
||||
public int IndexOf(IBlueRow item) {
|
||||
int id = item.Id();
|
||||
if (myHash.isPresent(id)) {
|
||||
int index = myHash.find(id);
|
||||
if (BlueRowEqualityComparer.Equals(item, myRows[index])) {
|
||||
return index;
|
||||
}
|
||||
}
|
||||
return -1;
|
||||
}
|
||||
|
||||
public object SyncRoot
|
||||
{
|
||||
get
|
||||
@@ -95,8 +115,11 @@ namespace BCG
|
||||
|
||||
#region IEnumerable Members
|
||||
|
||||
public System.Collections.IEnumerator GetEnumerator()
|
||||
{ return myRows.GetEnumerator(); }
|
||||
public IEnumerator<IBlueRow> GetEnumerator() {
|
||||
for (int i = 0; i < myRows.Length; i++) {
|
||||
yield return myRows[i];
|
||||
}
|
||||
}
|
||||
|
||||
#endregion
|
||||
|
||||
|
||||
@@ -37,11 +37,15 @@ namespace BCG
|
||||
|
||||
public object SyncRoot {get {return myTable.SyncRoot;}}
|
||||
|
||||
public int IndexOf(IBlueRow item) =>
|
||||
myTable.IndexOf(item);
|
||||
|
||||
#endregion
|
||||
|
||||
#region IEnumerable Members
|
||||
|
||||
public System.Collections.IEnumerator GetEnumerator() {return myTable.GetEnumerator ();}
|
||||
public IEnumerator<IBlueRow> GetEnumerator() =>
|
||||
myTable.GetEnumerator();
|
||||
|
||||
#endregion
|
||||
|
||||
@@ -136,6 +140,9 @@ namespace BCG
|
||||
public IList<object> GetColumn(int index) =>
|
||||
throw new NotImplementedException();
|
||||
|
||||
public IBlueRow GetRow(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,5 +1,7 @@
|
||||
using bts.collections;
|
||||
|
||||
using Bts.DataStreamCore;
|
||||
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
|
||||
@@ -108,6 +110,17 @@ namespace BCG
|
||||
public IList<object> GetColumn(int index) =>
|
||||
mySortedRows.WrapSelector(r => r[index], null);
|
||||
|
||||
public IBlueRow GetRow(int index) =>
|
||||
mySortedRows[index];
|
||||
|
||||
public int IndexOf(IBlueRow item) {
|
||||
int id = item.Id();
|
||||
if (myIDToRows.Value.TryGetValue(id, out var row) && BlueRowEqualityComparer.Equals(item, row)) {
|
||||
return mySortedRows.BinarySearch(row);
|
||||
}
|
||||
return -1;
|
||||
}
|
||||
|
||||
#endregion
|
||||
|
||||
#region ICollection Members
|
||||
@@ -137,7 +150,7 @@ namespace BCG
|
||||
|
||||
#region IEnumerable Members
|
||||
|
||||
public System.Collections.IEnumerator GetEnumerator()
|
||||
public IEnumerator<IBlueRow> GetEnumerator()
|
||||
{
|
||||
return this.mySortedRows.GetEnumerator();
|
||||
}
|
||||
|
||||
@@ -1,5 +1,6 @@
|
||||
using System;
|
||||
using System.Collections;
|
||||
using System.Collections.Generic;
|
||||
using System.Data;
|
||||
//using BCG.Utilities;
|
||||
|
||||
@@ -62,7 +63,11 @@ namespace BCG
|
||||
}
|
||||
|
||||
#region ICollection members
|
||||
public override IEnumerator GetEnumerator() { return myOrderedRows.GetEnumerator(); }
|
||||
public override IEnumerator<IBlueRow> GetEnumerator() {
|
||||
foreach (IBlueRow row in myOrderedRows) {
|
||||
yield return row;
|
||||
}
|
||||
}
|
||||
|
||||
public override void CopyTo(Array array, int index) { myOrderedRows.CopyTo(array, index); }
|
||||
|
||||
|
||||
@@ -1,7 +1,10 @@
|
||||
using bts.collections;
|
||||
|
||||
using Bts.DataStreamCore;
|
||||
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
|
||||
namespace BCG
|
||||
{
|
||||
@@ -36,6 +39,13 @@ namespace BCG
|
||||
#region IBlueRowCollection Members
|
||||
|
||||
public IBlueRow this[int id] { get { return myRows[id]; } }
|
||||
public IBlueRow GetRow(int index) => myRows[index];
|
||||
public int IndexOf(IBlueRow item) {
|
||||
int id = item.Id();
|
||||
return id >= 0 && id < myRows.Length && BlueRowEqualityComparer.Equals(myRows[id], item)
|
||||
? id
|
||||
: -1;
|
||||
}
|
||||
|
||||
public IBlueTableSchema schema {get {return mySchema;}}
|
||||
|
||||
@@ -63,7 +73,11 @@ namespace BCG
|
||||
|
||||
#region IEnumerable Members
|
||||
|
||||
public System.Collections.IEnumerator GetEnumerator() {return myRows.GetEnumerator ();}
|
||||
public IEnumerator<IBlueRow> GetEnumerator() {
|
||||
for (int i = 0; i < myRows.Length; i++) {
|
||||
yield return myRows[i];
|
||||
}
|
||||
}
|
||||
|
||||
#endregion
|
||||
}
|
||||
|
||||
@@ -1,5 +1,7 @@
|
||||
using bts.collections;
|
||||
|
||||
using Bts.DataStreamCore;
|
||||
|
||||
using System;
|
||||
using System.Collections;
|
||||
using System.Collections.Generic;
|
||||
@@ -9,22 +11,22 @@ namespace BCG
|
||||
{
|
||||
public class SimpleBindableCollection : IBlueRowCollection, ITypedList, IList
|
||||
{
|
||||
private ArrayList myRows;
|
||||
private List<IBlueRow> myRows;
|
||||
private IBlueTableSchema mySchema;
|
||||
private PropertyDescriptorCollection myPDColl;
|
||||
|
||||
public SimpleBindableCollection (IBlueTableSchema schema, ICollection rows)
|
||||
public SimpleBindableCollection (IBlueTableSchema schema, ICollection<IBlueRow> rows)
|
||||
{
|
||||
mySchema = schema;
|
||||
ArrayList temp = new ArrayList ();
|
||||
foreach (ColumnInfo ci in mySchema.columnInfo)
|
||||
temp.Add (new BlueDataPropertyDescriptor (ci));
|
||||
myPDColl = new PropertyDescriptorCollection (temp.ToArray (typeof(PropertyDescriptor)) as PropertyDescriptor[]);
|
||||
myRows = new ArrayList (rows);
|
||||
myRows = new(rows);
|
||||
}
|
||||
|
||||
public SimpleBindableCollection (IBlueTableSchema schema)
|
||||
: this (schema, new ArrayList ())
|
||||
: this(schema, new List<IBlueRow>(0))
|
||||
{}
|
||||
|
||||
#region IBlueRowCollection Members
|
||||
@@ -57,6 +59,9 @@ namespace BCG
|
||||
public IList<object> GetColumn(int index) =>
|
||||
myRows.WrapSelector(r => ((IBlueRow)r)[index], null);
|
||||
|
||||
public IBlueRow GetRow(int index) =>
|
||||
(IBlueRow)myRows[index];
|
||||
|
||||
#endregion
|
||||
|
||||
#region ICollection Members
|
||||
@@ -65,7 +70,25 @@ namespace BCG
|
||||
|
||||
public int Count {get {return myRows.Count;}}
|
||||
|
||||
public void CopyTo(Array array, int index) {myRows.CopyTo (array, index);}
|
||||
public void CopyTo(Array array, int index) {
|
||||
for (int i = 0; i < myRows.Count; i++) {
|
||||
array.SetValue(myRows[i], index++);
|
||||
}
|
||||
}
|
||||
|
||||
public int IndexOf(IBlueRow item) {
|
||||
int id = item.Id();
|
||||
for (int i = 0; i < myRows.Count; i++) {
|
||||
var r = myRows[i];
|
||||
if (r.Id() == id) {
|
||||
if (BlueRowEqualityComparer.Equals(item, r)) {
|
||||
return i;
|
||||
}
|
||||
break;
|
||||
}
|
||||
}
|
||||
return -1;
|
||||
}
|
||||
|
||||
public object SyncRoot { get {return null;}}
|
||||
|
||||
@@ -73,9 +96,10 @@ namespace BCG
|
||||
|
||||
#region IEnumerable Members
|
||||
|
||||
public System.Collections.IEnumerator GetEnumerator()
|
||||
{
|
||||
return myRows.GetEnumerator ();
|
||||
public IEnumerator<IBlueRow> GetEnumerator() {
|
||||
foreach (IBlueRow row in myRows) {
|
||||
yield return row;
|
||||
}
|
||||
}
|
||||
|
||||
#endregion
|
||||
@@ -110,11 +134,13 @@ namespace BCG
|
||||
|
||||
public void Remove(object value) {throw new ApplicationException ("Collection is read only");}
|
||||
|
||||
public bool Contains(object value) {return myRows.Contains (value);}
|
||||
public bool Contains(object value) =>
|
||||
value is IBlueRow r && myRows.Contains(r);
|
||||
|
||||
public void Clear() {throw new ApplicationException ("Collection is read only");}
|
||||
|
||||
public int IndexOf(object value) {return myRows.IndexOf (value);}
|
||||
public int IndexOf(object value) =>
|
||||
value is IBlueRow r ? myRows.IndexOf(r) : -1;
|
||||
|
||||
public int Add(object value) {throw new ApplicationException ("Collection is read only");}
|
||||
|
||||
@@ -122,6 +148,7 @@ namespace BCG
|
||||
|
||||
#endregion
|
||||
|
||||
public void reset (ICollection rows) {myRows = new ArrayList(rows);}
|
||||
public void reset(ICollection<IBlueRow> rows) =>
|
||||
myRows = new(rows);
|
||||
}
|
||||
}
|
||||
@@ -1,8 +1,9 @@
|
||||
using bts.collections;
|
||||
|
||||
using Bts.DataStreamCore;
|
||||
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
|
||||
namespace BCG
|
||||
{
|
||||
@@ -32,6 +33,17 @@ namespace BCG
|
||||
_rows[id] = value;
|
||||
}
|
||||
}
|
||||
|
||||
public IBlueRow GetRow(int index) =>
|
||||
_rows[index];
|
||||
|
||||
public int IndexOf(IBlueRow item) {
|
||||
int index = item.Id();
|
||||
return index >= 0 && index < _rows.Count && BlueRowEqualityComparer.Equals(_rows[index], item)
|
||||
? index
|
||||
: -1;
|
||||
}
|
||||
|
||||
public IBlueTableSchema schema { get; private set; }
|
||||
public bool containsID(int id) {
|
||||
return (id >= 0 && id < _rows.Count && this[id] != null);
|
||||
@@ -42,8 +54,13 @@ namespace BCG
|
||||
|
||||
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; }}
|
||||
public object SyncRoot { get { return this; } }
|
||||
public System.Collections.IEnumerator GetEnumerator() { return _rows.Where(r => r != null).GetEnumerator(); }
|
||||
|
||||
public IEnumerator<IBlueRow> GetEnumerator() {
|
||||
foreach (var r in _rows) {
|
||||
if (r is not null) {
|
||||
yield return r;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -1,12 +1,10 @@
|
||||
using Spark.Core.Collections;
|
||||
|
||||
using System;
|
||||
using System.Collections;
|
||||
using System.Collections.Generic;
|
||||
using System.Data;
|
||||
|
||||
using BCG.Utilities.General;
|
||||
|
||||
using Bts.DataStreamCore;
|
||||
|
||||
namespace BCG {
|
||||
[Serializable]
|
||||
public class DataPacket : ICloneable, IDataPacket {
|
||||
@@ -45,7 +43,11 @@ namespace BCG {
|
||||
public IBlueRowCollection newRows, addedRows, deletedRows;
|
||||
public Dictionary<string, object> meta_data = null;
|
||||
public DataPacket() { }
|
||||
public DataPacket(IBlueRowCollection newRows, IBlueRowCollection addedRows, IBlueRowCollection deletedRows) {
|
||||
|
||||
public DataPacket(IBlueTableSchema schema)
|
||||
: this(new BlueTable(schema), new BlueTable(schema), new BlueTable(schema)) { }
|
||||
|
||||
public DataPacket (IBlueRowCollection newRows, IBlueRowCollection addedRows, IBlueRowCollection deletedRows) {
|
||||
this.newRows = newRows;
|
||||
this.addedRows = addedRows;
|
||||
this.deletedRows = deletedRows;
|
||||
@@ -57,9 +59,9 @@ namespace BCG {
|
||||
IReadOnlyDictionary<string, object> IDataPacket.Metadata => meta_data;
|
||||
|
||||
public void get_new_added_deleted(out ICollection new_rows, out ICollection added_rows, out ICollection deleted_rows) {
|
||||
new_rows = newRows;
|
||||
added_rows = addedRows;
|
||||
deleted_rows = deletedRows;
|
||||
new_rows = ListNonGenericCollectionWrapper.Create(newRows);
|
||||
added_rows = ListNonGenericCollectionWrapper.Create(addedRows);
|
||||
deleted_rows = ListNonGenericCollectionWrapper.Create(deletedRows);
|
||||
}
|
||||
|
||||
public IBlueRowCollection getTrulyAddedRows(params string[] idColumns) {
|
||||
@@ -69,7 +71,9 @@ namespace BCG {
|
||||
CollectionHelpers.EqualDelegate ed =
|
||||
new CollectionHelpers.EqualDelegate(c.blueRowIdEqualEvaluator);
|
||||
|
||||
ICollection ta = CollectionHelpers.difference(addedRows, deletedRows, ed);
|
||||
var addedRowCollection = ListNonGenericCollectionWrapper.Create(addedRows);
|
||||
var deletedRowCollection = ListNonGenericCollectionWrapper.Create(deletedRows);
|
||||
ICollection ta = CollectionHelpers.difference(addedRowCollection, deletedRowCollection, ed);
|
||||
|
||||
foreach (IBlueRow r in ta) {
|
||||
truly.addRow(r);
|
||||
@@ -111,7 +115,9 @@ namespace BCG {
|
||||
CollectionHelpers.EqualDelegate ed =
|
||||
new CollectionHelpers.EqualDelegate(c.blueRowIdEqualEvaluator);
|
||||
|
||||
ICollection td = CollectionHelpers.difference(deletedRows, addedRows, ed);
|
||||
var addedRowCollection = ListNonGenericCollectionWrapper.Create(addedRows);
|
||||
var deletedRowCollection = ListNonGenericCollectionWrapper.Create(deletedRows);
|
||||
ICollection td = CollectionHelpers.difference(deletedRowCollection, addedRowCollection, ed);
|
||||
|
||||
foreach (IBlueRow r in td) {
|
||||
truly.addRow(r);
|
||||
@@ -128,8 +134,10 @@ namespace BCG {
|
||||
CollectionHelpers.EqualDelegate ed =
|
||||
new CollectionHelpers.EqualDelegate(c.blueRowIdEqualEvaluator);
|
||||
|
||||
ICollection ta = CollectionHelpers.difference(addedRows, deletedRows, ed);
|
||||
ICollection tc = CollectionHelpers.difference(addedRows, ta, ed);
|
||||
var addedRowCollection = ListNonGenericCollectionWrapper.Create(addedRows);
|
||||
var deletedRowCollection = ListNonGenericCollectionWrapper.Create(deletedRows);
|
||||
ICollection ta = CollectionHelpers.difference(addedRowCollection, deletedRowCollection, ed);
|
||||
ICollection tc = CollectionHelpers.difference(addedRowCollection, ta, ed);
|
||||
|
||||
foreach (IBlueRow r in tc) {
|
||||
truly.addRow(r);
|
||||
@@ -145,8 +153,10 @@ namespace BCG {
|
||||
CollectionHelpers.EqualDelegate ed =
|
||||
new CollectionHelpers.EqualDelegate(c.blueRowIdEqualEvaluator);
|
||||
|
||||
ICollection td = CollectionHelpers.difference(deletedRows, addedRows, ed);
|
||||
ICollection tco = CollectionHelpers.difference(deletedRows, td, ed);
|
||||
var deletedRowCollection = ListNonGenericCollectionWrapper.Create(deletedRows);
|
||||
var addedRowCollection = ListNonGenericCollectionWrapper.Create(addedRows);
|
||||
ICollection td = CollectionHelpers.difference(deletedRowCollection, addedRowCollection, ed);
|
||||
ICollection tco = CollectionHelpers.difference(deletedRowCollection, td, ed);
|
||||
|
||||
foreach (IBlueRow r in tco) {
|
||||
truly.addRow(r);
|
||||
@@ -223,13 +233,15 @@ namespace BCG {
|
||||
}
|
||||
|
||||
deleted.AddRange(oldDeleted);
|
||||
var deletedRowCollection = ListNonGenericCollectionWrapper.Create(dp.deletedRows);
|
||||
deleted.AddRange(CollectionHelpers.difference(
|
||||
CollectionHelpers.difference(dp.deletedRows, oldAdded, ed),
|
||||
CollectionHelpers.difference(deletedRowCollection, oldAdded, ed),
|
||||
oldDeleted, ed));
|
||||
|
||||
added.AddRange(dp.addedRows);
|
||||
var addedRowCollection = ListNonGenericCollectionWrapper.Create(dp.addedRows);
|
||||
added.AddRange(addedRowCollection);
|
||||
added.AddRange(CollectionHelpers.difference(
|
||||
CollectionHelpers.difference(oldAdded, dp.deletedRows, ed),
|
||||
CollectionHelpers.difference(oldAdded, deletedRowCollection, ed),
|
||||
added, ed));
|
||||
nt = dp.newRows;
|
||||
}
|
||||
@@ -287,30 +299,30 @@ namespace BCG {
|
||||
}
|
||||
}
|
||||
|
||||
public class DataPacket<T> : IDataPacket<T> where T : IBlueRow {
|
||||
public DataPacket(
|
||||
IBlueRowCollection<T> newRows,
|
||||
IBlueRowCollection<T> addedRows,
|
||||
IBlueRowCollection<T> deletedRows,
|
||||
IReadOnlyDictionary<string, object> metadata = null
|
||||
) {
|
||||
this.newRows = newRows;
|
||||
this.addedRows = addedRows;
|
||||
this.deletedRows = deletedRows;
|
||||
Metadata = metadata ?? DictionaryHelper.empty<string, object>();
|
||||
}
|
||||
// public class DataPacket<T> : IDataPacket<T> where T : IBlueRow {
|
||||
// public DataPacket(
|
||||
// IBlueRowCollection<T> newRows,
|
||||
// IBlueRowCollection<T> addedRows,
|
||||
// IBlueRowCollection<T> deletedRows,
|
||||
// IReadOnlyDictionary<string, object> metadata = null
|
||||
// ) {
|
||||
// this.newRows = newRows;
|
||||
// this.addedRows = addedRows;
|
||||
// this.deletedRows = deletedRows;
|
||||
// Metadata = metadata ?? DictionaryHelper.empty<string, object>();
|
||||
// }
|
||||
|
||||
public IBlueRowCollection<T> newRows { get; }
|
||||
public IBlueRowCollection<T> addedRows { get; }
|
||||
public IBlueRowCollection<T> deletedRows { get; }
|
||||
public IReadOnlyDictionary<string, object> Metadata { get; }
|
||||
// public IBlueRowCollection<T> newRows { get; }
|
||||
// public IBlueRowCollection<T> addedRows { get; }
|
||||
// public IBlueRowCollection<T> deletedRows { get; }
|
||||
// public IReadOnlyDictionary<string, object> Metadata { get; }
|
||||
|
||||
public static DataPacket<T> Empty(IBlueTableSchema schema) =>
|
||||
new(
|
||||
new BlueTableImmutable<T>(schema),
|
||||
new BlueTableImmutable<T>(schema),
|
||||
new BlueTableImmutable<T>(schema));
|
||||
}
|
||||
// public static DataPacket<T> Empty(IBlueTableSchema schema) =>
|
||||
// new(
|
||||
// new BlueTableImmutable<T>(schema),
|
||||
// new BlueTableImmutable<T>(schema),
|
||||
// new BlueTableImmutable<T>(schema));
|
||||
// }
|
||||
|
||||
public static class DataPacketExtensions {
|
||||
public static bool HasChanges(this DataPacket dataPacket) =>
|
||||
|
||||
@@ -1,4 +1,4 @@
|
||||
namespace BCG;
|
||||
|
||||
public delegate void DataStreamChangeDelegate(object newValue);
|
||||
public delegate void DataStreamChangeDelegate<T>(IDataPacket<T> dp) where T : IBlueRow;
|
||||
// public delegate void DataStreamChangeDelegate<T>(IDataPacket dp) where T : IBlueRow;
|
||||
@@ -18,22 +18,22 @@ namespace BCG {
|
||||
void get_new_added_deleted(out ICollection new_rows, out ICollection added_rows, out ICollection deleted_rows);
|
||||
}
|
||||
|
||||
public interface IDataPacket<T> : IDataPacket where T : IBlueRow {
|
||||
new IBlueRowCollection<T> newRows { get; }
|
||||
new IBlueRowCollection<T> addedRows { get; }
|
||||
new IBlueRowCollection<T> deletedRows { get; }
|
||||
// public interface IDataPacket<T> : IDataPacket where T : IBlueRow {
|
||||
// new IBlueRowCollection<T> newRows { get; }
|
||||
// new IBlueRowCollection<T> addedRows { get; }
|
||||
// new IBlueRowCollection<T> deletedRows { get; }
|
||||
|
||||
IBlueRowCollection IDataPacket.newRows => newRows;
|
||||
IBlueRowCollection IDataPacket.addedRows => addedRows;
|
||||
IBlueRowCollection IDataPacket.deletedRows => deletedRows;
|
||||
IBlueTableSchema IDataPacket.Schema => newRows.schema;
|
||||
// IBlueRowCollection IDataPacket.newRows => newRows;
|
||||
// IBlueRowCollection IDataPacket.addedRows => addedRows;
|
||||
// IBlueRowCollection IDataPacket.deletedRows => deletedRows;
|
||||
// IBlueTableSchema IDataPacket.Schema => newRows.schema;
|
||||
|
||||
void IDataPacket.get_new_added_deleted(out ICollection new_rows, out ICollection added_rows, out ICollection deleted_rows) {
|
||||
new_rows = newRows;
|
||||
added_rows = addedRows;
|
||||
deleted_rows = deletedRows;
|
||||
}
|
||||
}
|
||||
// void IDataPacket.get_new_added_deleted(out ICollection new_rows, out ICollection added_rows, out ICollection deleted_rows) {
|
||||
// new_rows = newRows;
|
||||
// added_rows = addedRows;
|
||||
// deleted_rows = deletedRows;
|
||||
// }
|
||||
// }
|
||||
|
||||
public static class IDataPacketExtensions {
|
||||
public static IDataPacket ToImmutable(this IDataPacket dataPacket) =>
|
||||
|
||||
@@ -6,8 +6,8 @@ public interface IViewStream {
|
||||
IBlueRowCollection rows { get; }
|
||||
}
|
||||
|
||||
public interface IViewStream<T> : IViewStream where T : IBlueRow {
|
||||
new event DataStreamChangeDelegate<T> onChange;
|
||||
// public interface IViewStream<T> : IViewStream where T : IBlueRow {
|
||||
// new event DataStreamChangeDelegate<T> onChange;
|
||||
|
||||
new IBlueRowCollection<T> rows { get; }
|
||||
}
|
||||
// new IBlueRowCollection<T> rows { get; }
|
||||
// }
|
||||
Reference in new issue
Block a user