refactor: Impl IDictionary<int, IBlueRow> in BlueTable
This commit is contained in:
1 parent
d6fae238f9
commit
d3eb29087b
2 files changed
+90
-1
No files matched your search
@@ -0,0 +1,24 @@
|
||||
namespace Bts.DataStreamCore;
|
||||
|
||||
using BCG;
|
||||
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Diagnostics.CodeAnalysis;
|
||||
using System.Linq;
|
||||
|
||||
public class BlueRowEqualityComparer : IEqualityComparer<IBlueRow> {
|
||||
public static BlueRowEqualityComparer Instance { get; } = new();
|
||||
|
||||
public bool Equals(IBlueRow x, IBlueRow y) =>
|
||||
x.schema.columnInfo.SequenceEqual(y.schema.columnInfo) &&
|
||||
x.Values.Cast<object>().SequenceEqual(y.Values.Cast<object>());
|
||||
|
||||
public int GetHashCode([DisallowNull] IBlueRow obj) {
|
||||
var hash = new HashCode();
|
||||
foreach (var v in obj.Values) {
|
||||
hash.Add(v?.GetHashCode() ?? 0);
|
||||
}
|
||||
return hash.ToHashCode();
|
||||
}
|
||||
}
|
||||
@@ -1,8 +1,15 @@
|
||||
using Bts.DataStreamCore;
|
||||
|
||||
using Spark.DataStream;
|
||||
|
||||
using System;
|
||||
using System.Collections;
|
||||
using System.Collections.Generic;
|
||||
using System.Data;
|
||||
using System.Diagnostics.CodeAnalysis;
|
||||
|
||||
//using BCG.Utilities;
|
||||
|
||||
using System.Linq;
|
||||
|
||||
namespace BCG {
|
||||
@@ -10,7 +17,7 @@ namespace BCG {
|
||||
/// Anologue of the dotNet DataTable.
|
||||
/// </summary>
|
||||
[Serializable]
|
||||
public class BlueTable : IBlueRowCollection {
|
||||
public class BlueTable : IBlueRowCollection, IDictionary<int, IBlueRow> {
|
||||
/// <summary>
|
||||
/// Hashtable indexed on "id" to speed up removes
|
||||
/// </summary>
|
||||
@@ -86,6 +93,12 @@ namespace BCG {
|
||||
throw new BlueDataException("ID " + id.ToString() + " does not exist in table " + mySchema.tableName);
|
||||
return (IBlueRow)row;
|
||||
}
|
||||
set {
|
||||
if ((int)value["id"] != id) {
|
||||
throw new ArgumentException("id arg not equal to row's actual id", nameof(id));
|
||||
}
|
||||
changeRow(value);
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
@@ -202,6 +215,8 @@ namespace BCG {
|
||||
|
||||
#endregion
|
||||
|
||||
public IDictionary<int, IBlueRow> AsDictionary() => this;
|
||||
|
||||
#region ICollection members
|
||||
public virtual IEnumerator GetEnumerator() //{return myRows.GetEnumerator ();}
|
||||
{ return myIdIndex.Values.GetEnumerator(); }
|
||||
@@ -214,5 +229,55 @@ namespace BCG {
|
||||
|
||||
public virtual int Count { get { return myIdIndex.Count; } }
|
||||
#endregion
|
||||
|
||||
#region IDictionary<int, IBlueRow> members
|
||||
public void Add(int key, IBlueRow value) {
|
||||
throw new NotImplementedException();
|
||||
}
|
||||
|
||||
bool IDictionary<int, IBlueRow>.ContainsKey(int key) =>
|
||||
containsID(key);
|
||||
|
||||
bool IDictionary<int, IBlueRow>.Remove(int key) =>
|
||||
deleteRow(key);
|
||||
|
||||
bool IDictionary<int, IBlueRow>.TryGetValue(
|
||||
int key, [MaybeNullWhen(false)] out IBlueRow value) =>
|
||||
tryGetValue(key, out value);
|
||||
|
||||
void ICollection<KeyValuePair<int, IBlueRow>>.Add(KeyValuePair<int, IBlueRow> item) =>
|
||||
addRow(item.Value);
|
||||
|
||||
void ICollection<KeyValuePair<int, IBlueRow>>.Clear() =>
|
||||
clear();
|
||||
|
||||
bool ICollection<KeyValuePair<int, IBlueRow>>.Contains(KeyValuePair<int, IBlueRow> item) =>
|
||||
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);
|
||||
}
|
||||
}
|
||||
|
||||
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);
|
||||
}
|
||||
}
|
||||
|
||||
ICollection<int> IDictionary<int, IBlueRow>.Keys =>
|
||||
GenericCollectionWrapper.Create<int>(myIdIndex.Keys);
|
||||
|
||||
ICollection<IBlueRow> IDictionary<int, IBlueRow>.Values =>
|
||||
GenericCollectionWrapper.Create<IBlueRow>(myIdIndex.Values);
|
||||
|
||||
bool ICollection<KeyValuePair<int, IBlueRow>>.IsReadOnly => false;
|
||||
#endregion
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user