diff --git a/src/DataStreamSuite/DataStreamCore/BlueData/BlueRowEqualityComparer.cs b/src/DataStreamSuite/DataStreamCore/BlueData/BlueRowEqualityComparer.cs new file mode 100644 index 000000000..25929cb48 --- /dev/null +++ b/src/DataStreamSuite/DataStreamCore/BlueData/BlueRowEqualityComparer.cs @@ -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 { + public static BlueRowEqualityComparer Instance { get; } = new(); + + public bool Equals(IBlueRow x, IBlueRow y) => + x.schema.columnInfo.SequenceEqual(y.schema.columnInfo) && + x.Values.Cast().SequenceEqual(y.Values.Cast()); + + public int GetHashCode([DisallowNull] IBlueRow obj) { + var hash = new HashCode(); + foreach (var v in obj.Values) { + hash.Add(v?.GetHashCode() ?? 0); + } + return hash.ToHashCode(); + } +} diff --git a/src/DataStreamSuite/DataStreamCore/BlueData/BlueTable.cs b/src/DataStreamSuite/DataStreamCore/BlueData/BlueTable.cs index 5024be042..cf155b9a2 100644 --- a/src/DataStreamSuite/DataStreamCore/BlueData/BlueTable.cs +++ b/src/DataStreamSuite/DataStreamCore/BlueData/BlueTable.cs @@ -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. /// [Serializable] - public class BlueTable : IBlueRowCollection { + public class BlueTable : IBlueRowCollection, IDictionary { /// /// Hashtable indexed on "id" to speed up removes /// @@ -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); + } } /// @@ -202,6 +215,8 @@ namespace BCG { #endregion + public IDictionary 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 members + public void Add(int key, IBlueRow value) { + throw new NotImplementedException(); + } + + bool IDictionary.ContainsKey(int key) => + containsID(key); + + bool IDictionary.Remove(int key) => + deleteRow(key); + + bool IDictionary.TryGetValue( + int key, [MaybeNullWhen(false)] out IBlueRow value) => + tryGetValue(key, out value); + + void ICollection>.Add(KeyValuePair item) => + addRow(item.Value); + + void ICollection>.Clear() => + clear(); + + bool ICollection>.Contains(KeyValuePair item) => + tryGetValue(item.Key, out var r) + && BlueRowEqualityComparer.Instance.Equals(r, item.Value); + + void ICollection>.CopyTo( + KeyValuePair[] array, int arrayIndex) { + foreach (DictionaryEntry de in myIdIndex) { + array[arrayIndex++] = new((int)de.Key, (IBlueRow)de.Value); + } + } + + bool ICollection>.Remove(KeyValuePair item) => + deleteRow(item.Key); + + IEnumerator> IEnumerable>.GetEnumerator() { + foreach (DictionaryEntry de in myIdIndex) { + yield return new((int)de.Key, (IBlueRow)de.Value); + } + } + + ICollection IDictionary.Keys => + GenericCollectionWrapper.Create(myIdIndex.Keys); + + ICollection IDictionary.Values => + GenericCollectionWrapper.Create(myIdIndex.Values); + + bool ICollection>.IsReadOnly => false; + #endregion } }