diff --git a/src/DataStreamSuite/DataStream/Primitives/GroupBy/GroupByOutputTable.cs b/src/DataStreamSuite/DataStream/Primitives/GroupBy/GroupByOutputTable.cs index 103c5c074..cbdb0d04b 100644 --- a/src/DataStreamSuite/DataStream/Primitives/GroupBy/GroupByOutputTable.cs +++ b/src/DataStreamSuite/DataStream/Primitives/GroupBy/GroupByOutputTable.cs @@ -1,3 +1,5 @@ +using bts.collections; + using Bts.DataStreamCore; using System; @@ -11,7 +13,7 @@ namespace BCG /// public class GroupByOutputTable : IBlueRowCollection { - readonly Dictionary myDict = new(); + readonly SortedList myDict = new(); private BlueTable myTable; public GroupByOutputTable (IBlueTableSchema schema) {myTable = new BlueTable (schema);} @@ -58,7 +60,10 @@ namespace BCG public IBlueRow this[int id] {get{return myTable[id];}} public IBlueTableSchema schema {get{return myTable.schema;}} public bool containsID (int id) {return myTable.containsID (id);} - + public IList GetColumn(int index) => + index < 0 || index >= myTable.schema.columnInfo.Length + ? throw new IndexOutOfRangeException() + : myDict.Values.WrapSelector(r => r[index], null); #endregion #region ICollection members diff --git a/src/DataStreamSuite/DataStream/Primitives/GroupBy/RowDictionaryTable.cs b/src/DataStreamSuite/DataStream/Primitives/GroupBy/RowDictionaryTable.cs index 0f9ff8c8a..a0334945a 100644 --- a/src/DataStreamSuite/DataStream/Primitives/GroupBy/RowDictionaryTable.cs +++ b/src/DataStreamSuite/DataStream/Primitives/GroupBy/RowDictionaryTable.cs @@ -97,6 +97,8 @@ namespace BCG public IBlueRow this[int id] {get{return myLinearCollection[id];}} public IBlueTableSchema schema {get{return mySchema;}} public bool containsID (int id) {return myLinearCollection.containsID (id);} + public IList GetColumn(int index) => + myLinearCollection.GetColumn(index); #endregion #region ICollection members diff --git a/src/DataStreamSuite/DataStream/Primitives/Join/JoinTable.cs b/src/DataStreamSuite/DataStream/Primitives/Join/JoinTable.cs index 154a992dd..985c4b2c7 100644 --- a/src/DataStreamSuite/DataStream/Primitives/Join/JoinTable.cs +++ b/src/DataStreamSuite/DataStream/Primitives/Join/JoinTable.cs @@ -1,14 +1,17 @@ +using bts.collections; + using Bts.DataStreamCore; using System; using System.Collections; +using System.Collections.Generic; namespace BCG { internal class JoinTable : IBlueRowCollection { protected JoinTableSchema mySchema; - protected IDictionary myIdIndex = new Hashtable (); + protected SortedList myIdIndex = new(); protected JoinDeletionIndex myLeftIndex, myRightIndex; private int myNewRowId; @@ -20,13 +23,16 @@ namespace BCG } public IBlueRow this [int id] => - myIdIndex[id] as IBlueRow ?? throw new BlueDataException ("ID " + id.ToString() + " does not exist in table " + mySchema.tableName); + myIdIndex.TryGetValue(id, out var r) + ? r + : throw new BlueDataException("ID " + id.ToString() + " does not exist in table " + mySchema.tableName); #region JoinTable members public virtual void addRow (IBlueRow leftRow, IBlueRow? rightRow, DataPacket dp) { var newRow = new JoinRow(myNewRowId, leftRow, rightRow, mySchema); - myIdIndex[myNewRowId ++] = newRow; + myIdIndex.Add(myNewRowId, newRow); + myNewRowId++; ((BlueTable)(dp.addedRows)).addRow (newRow); myLeftIndex.add (newRow); if (rightRow != null) //in case of left joins when right row is null @@ -61,7 +67,11 @@ namespace BCG public object SyncRoot{get{return this;}} - public void CopyTo (Array array, int index) {myIdIndex.Values.CopyTo (array, index);} + public void CopyTo(Array array, int index) { + for (int i = 0; i < myIdIndex.Count; i++) { + array.SetValue(myIdIndex.Values[i], index++); + } + } #endregion #region IEnumerable members @@ -71,6 +81,12 @@ namespace BCG #region IBlueRowCollection members public IBlueTableSchema schema {get{return mySchema;}} public bool containsID (int id) {return myIdIndex[id] != null;} + public IList GetColumn(int index) { + if (index < 0 || index >= schema.columnInfo.Length) { + throw new IndexOutOfRangeException(); + } + return myIdIndex.Values.WrapSelector(r => r[index], null); + } #endregion } diff --git a/src/DataStreamSuite/DataStream/Primitives/LightJoinStreams/LightForeignJoinSimpleCollection.cs b/src/DataStreamSuite/DataStream/Primitives/LightJoinStreams/LightForeignJoinSimpleCollection.cs index 3f25fd7c0..891467434 100644 --- a/src/DataStreamSuite/DataStream/Primitives/LightJoinStreams/LightForeignJoinSimpleCollection.cs +++ b/src/DataStreamSuite/DataStream/Primitives/LightJoinStreams/LightForeignJoinSimpleCollection.cs @@ -1,5 +1,8 @@ +using bts.collections; + using System; using System.Collections; +using System.Collections.Generic; namespace BCG { @@ -39,6 +42,11 @@ namespace BCG public bool containsID (int id) {throw new NotSupportedException ("");} + public IList GetColumn(int index) => + index < 0 || index >= schema.columnInfo.Length + ? throw new IndexOutOfRangeException() + : myRows.WrapType().WrapSelector(r => r[index], null); + public IBlueTableSchema schema {get {return mySchema;}} #endregion diff --git a/src/DataStreamSuite/DataStream/Primitives/Project/ProjectedTable.cs b/src/DataStreamSuite/DataStream/Primitives/Project/ProjectedTable.cs index 628f54272..6f4efbce3 100644 --- a/src/DataStreamSuite/DataStream/Primitives/Project/ProjectedTable.cs +++ b/src/DataStreamSuite/DataStream/Primitives/Project/ProjectedTable.cs @@ -1,5 +1,6 @@ using System; using System.Collections; +using System.Collections.Generic; namespace BCG { @@ -31,6 +32,13 @@ namespace BCG public IBlueTableSchema schema {get {return myHelper.schema;}} + public IList GetColumn(int index) { + if (index < 0 || index >= schema.columnInfo.Length) { + throw new IndexOutOfRangeException(); + } + var origIdx = myHelper.colMap[index]; + return myRows.GetColumn(origIdx); + } #endregion #region ICollection Members diff --git a/src/DataStreamSuite/DataStream/Primitives/Sort/SortedTable.cs b/src/DataStreamSuite/DataStream/Primitives/Sort/SortedTable.cs index 8be32d9c4..85cfa33e5 100644 --- a/src/DataStreamSuite/DataStream/Primitives/Sort/SortedTable.cs +++ b/src/DataStreamSuite/DataStream/Primitives/Sort/SortedTable.cs @@ -3,6 +3,8 @@ using System.Collections; using System.Collections.Generic; using System.Diagnostics.CodeAnalysis; +using bts.collections; + using Bts.ColumnSortExpressions; using Bts.DataStreamCore; @@ -86,6 +88,11 @@ public class SortedTable : IBlueRowCollection { public bool containsID(int id) => rowIndex[id] is not null; + public IList GetColumn(int index) => + index < 0 || index >= schema.columnInfo.Length + ? throw new IndexOutOfRangeException() + : rows.WrapSelector(r => r[index], null); + public IEnumerator GetEnumerator() => rows.GetEnumerator(); diff --git a/src/DataStreamSuite/DataStream/Primitives/Union/AppendCollection.cs b/src/DataStreamSuite/DataStream/Primitives/Union/AppendCollection.cs index ca74f321f..449ca19af 100644 --- a/src/DataStreamSuite/DataStream/Primitives/Union/AppendCollection.cs +++ b/src/DataStreamSuite/DataStream/Primitives/Union/AppendCollection.cs @@ -1,9 +1,17 @@ +using bts; +using bts.collections; + +using Bts.DataStream; using Bts.DataStreamCore; using mbts.utils; +using Spark.Core.Collections; + using System; using System.Collections; +using System.Collections.Generic; +using System.Diagnostics.CodeAnalysis; namespace BCG { @@ -14,31 +22,38 @@ namespace BCG internal class AppendCollection : IBlueRowCollection { private int myId = 0; - private IDictionary myLeftIndex = new Hashtable (), - myRightIndex = new Hashtable (); + readonly SortedList myLeftIndex = new(); + readonly SortedList myRightIndex = new(); + readonly Dictionary leftIdMap = new(); + readonly Dictionary rightIdMap = new(); private IBlueTableSchema? mySchema; - + int? idColumnIndex; + public AppendCollection () {} public DataPacket mergeRows (DataPacket dp, bool isLeft) { - mySchema = mySchema == null ? dp.schema : mySchema; - BlueTable dRows = new BlueTable (mySchema), - aRows = new BlueTable (mySchema); - IDictionary index = isLeft ? myLeftIndex : myRightIndex; - foreach (IBlueRow r in dp.deletedRows) - { - var dr = ExceptionAssert.of_type(index[r.Id()]); - dRows.addRow (dr); - index.Remove (r.Id()); + ensureInit(dp); + BlueTable dRows = new BlueTable(mySchema), + aRows = new BlueTable(mySchema); + var (index, idMap) = isLeft ? (myLeftIndex, leftIdMap) : (myRightIndex, rightIdMap); + foreach (IBlueRow r in dp.deletedRows) { + int idIn = r.Id(); + var dr = ExceptionAssert.of_type(index[idIn]); + var id = idMap[idIn]; + var maskedRow = new AppendRow(dr, id, mySchema); + dRows.addRow(maskedRow); + index.Remove(r.Id()); } - foreach (IBlueRow r in dp.addedRows) - { - IBlueRow ar = new AppendRow (r, myId++, mySchema); - aRows.addRow (ar); - index.Add(r.Id(), ar); + foreach (IBlueRow r in dp.addedRows) { + int idIn = r.Id(); + var id = myId++; + idMap[idIn] = id; + IBlueRow ar = new AppendRow(r, id, mySchema); + aRows.addRow(ar); + index.Add(idIn, ar); } - return new DataPacket (this, aRows, dRows); + return new DataPacket(this, aRows, dRows); } #region IBlueRowCollection Members @@ -67,8 +82,27 @@ namespace BCG public object SyncRoot => this; + public IList GetColumn(int index) { + var leftList = SortedKeyValueList.Create(myLeftIndex).WrapSelector(kv => new KeyValuePair(kv.Key, leftRowFactory(kv)), null); + var rightList = SortedKeyValueList.Create(myRightIndex).WrapSelector(kv => new KeyValuePair(kv.Key, rightRowFactory(kv)), null); + var allRows = new MergedSortedList>(leftList, rightList, new ComparerBuilder>() { kv => kv.Key }); + return allRows.as_mutable().WrapSelector(kv => kv.Value[index], null); + } + + IBlueRow leftRowFactory(KeyValuePair kv) => + new AppendRow(kv.Value, leftIdMap[kv.Key], mySchema ?? kv.Value.schema); + + IBlueRow rightRowFactory(KeyValuePair kv) => + new AppendRow(kv.Value, rightIdMap[kv.Key], mySchema ?? kv.Value.schema); + #endregion + [MemberNotNull(nameof(mySchema), nameof(idColumnIndex))] + void ensureInit(IDataPacket dp) { + Log.assert(mySchema is null == idColumnIndex is null); + mySchema ??= dp.Schema; + idColumnIndex ??= mySchema.ResolveIndices("id"); + } #region IEnumerable Members public System.Collections.IEnumerator GetEnumerator() { diff --git a/src/DataStreamSuite/DataStream/Primitives/Union/MutableAppendStream.cs b/src/DataStreamSuite/DataStream/Primitives/Union/MutableAppendStream.cs index baa604a1a..840c28328 100644 --- a/src/DataStreamSuite/DataStream/Primitives/Union/MutableAppendStream.cs +++ b/src/DataStreamSuite/DataStream/Primitives/Union/MutableAppendStream.cs @@ -149,6 +149,9 @@ namespace BCG { public bool containsID(int id) { return myRows.containsID(id); } + public IList GetColumn(int index) => + myRows.GetColumn(index); + #endregion #region ICollection Members diff --git a/src/DataStreamSuite/DataStreamCore/BlueData/BlueColumnTable.cs b/src/DataStreamSuite/DataStreamCore/BlueData/BlueColumnTable.cs index 9dbaac1cd..662094eec 100644 --- a/src/DataStreamSuite/DataStreamCore/BlueData/BlueColumnTable.cs +++ b/src/DataStreamSuite/DataStreamCore/BlueData/BlueColumnTable.cs @@ -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 GetColumn(int index) => + columns[index].WrapSelector(v => v.GetValue(), null); + public IEnumerator GetEnumerator() { var count = rowIds.Count; for (int i = 0; i < count; i++) { diff --git a/src/DataStreamSuite/DataStreamCore/BlueData/BlueRowCollection.cs b/src/DataStreamSuite/DataStreamCore/BlueData/BlueRowCollection.cs index df7cdce54..fa8245e45 100644 --- a/src/DataStreamSuite/DataStreamCore/BlueData/BlueRowCollection.cs +++ b/src/DataStreamSuite/DataStreamCore/BlueData/BlueRowCollection.cs @@ -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 findRow; readonly Func containsKey; + IList? cachedRows; public BlueRowCollection(ICollection source, IBlueTableSchema schema, Func? findRow = null, Func? containsKey = null) { this.source = source; @@ -51,6 +54,11 @@ public class BlueRowCollection : IBlueRowCollection { public bool containsID(int id) => containsKey(id); + public IList GetColumn(int index) { + cachedRows ??= source as IList ?? source.Cast().ToList(); + return cachedRows.WrapSelector(r => r[index], null); + } + public IEnumerator GetEnumerator() => source.GetEnumerator(); } diff --git a/src/DataStreamSuite/DataStreamCore/BlueData/BlueTable.cs b/src/DataStreamSuite/DataStreamCore/BlueData/BlueTable.cs index 5d103c1fc..29c4c1cbe 100644 --- a/src/DataStreamSuite/DataStreamCore/BlueData/BlueTable.cs +++ b/src/DataStreamSuite/DataStreamCore/BlueData/BlueTable.cs @@ -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 { /// /// Hashtable indexed on "id" to speed up removes /// - protected Hashtable myIdIndex = new Hashtable(); + protected SortedList rows = new(); /// /// Schema the table adheres to /// @@ -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 { /// public BlueTable(IBlueTableSchema schema) { mySchema = schema; } - public ICollection Rows => - GenericCollectionWrapper.Create(myIdIndex.Values); + public IList Rows => rows.Values; public static BlueTable Empty(string name, bool includeIdColumn = true) => new(BlueTableSchema.Empty(name, includeIdColumn)); @@ -95,7 +94,7 @@ namespace BCG { /// 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 { /// 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(); } /// @@ -132,7 +129,7 @@ namespace BCG { /// /// public virtual void forceAddRow(IBlueRow r) { - myIdIndex[r["id"]] = r; + rows[r.Id()] = r; } /// @@ -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) /// /// - 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 AsDictionary() => this; + public IList 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 members @@ -262,24 +270,20 @@ namespace BCG { 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); + void ICollection>.CopyTo(KeyValuePair[] array, int arrayIndex) { + foreach (var kv in rows) { + array[arrayIndex] = kv; } } 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); - } - } + IEnumerator> IEnumerable>.GetEnumerator() => + rows.GetEnumerator(); ICollection IDictionary.Keys => - GenericCollectionWrapper.Create(myIdIndex.Keys); + rows.Keys; ICollection IDictionary.Values => Rows; diff --git a/src/DataStreamSuite/DataStreamCore/BlueData/BlueTableImmutable.cs b/src/DataStreamSuite/DataStreamCore/BlueData/BlueTableImmutable.cs index 0924eed07..d2a8b6413 100644 --- a/src/DataStreamSuite/DataStreamCore/BlueData/BlueTableImmutable.cs +++ b/src/DataStreamSuite/DataStreamCore/BlueData/BlueTableImmutable.cs @@ -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 rows; + readonly ImmutableSortedSet> rows; public BlueTableImmutable(IBlueTableSchema schema) - : this(schema, ImmutableDictionary.Empty) { } + : this(schema, ImmutableSortedSet>.Empty) { } - public BlueTableImmutable(IBlueTableSchema schema, ImmutableDictionary rows) { + public BlueTableImmutable(IBlueTableSchema schema, ImmutableSortedSet> rows) { this.schema = schema; - this.rows = rows; + this.rows = rows.WithComparer(new ComparerBuilder>() { 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 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.Builder table; - readonly ImmutableDictionary.Builder added = - ImmutableDictionary.Empty.ToBuilder(); - readonly ImmutableDictionary.Builder removed = - ImmutableDictionary.Empty.ToBuilder(); + readonly ImmutableSortedSet>.Builder table; + readonly ImmutableSortedSet>.Builder added = + ImmutableSortedSet>.Empty.ToBuilder(); + readonly ImmutableSortedSet>.Builder removed = + ImmutableSortedSet>.Empty.ToBuilder(); public DataPacketBuilder(BlueTableImmutable table) { schema = table.schema; @@ -62,30 +78,37 @@ public class BlueTableImmutable : IBlueRowCollection, IImmutableRowCollection { public void AddOrUpdate(IBlueRow row, IEqualityComparer? 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 : IBlueRowCollection where T : IBlueRow { - readonly ImmutableDictionary rows; + readonly ImmutableSortedSet> rows; public BlueTableImmutable(IBlueTableSchema schema) - : this(schema, ImmutableDictionary.Empty) { } + : this(schema, ImmutableSortedSet>.Empty) { } - public BlueTableImmutable(IBlueTableSchema schema, ImmutableDictionary rows) { + public BlueTableImmutable(IBlueTableSchema schema, ImmutableSortedSet> rows) { this.schema = schema; this.rows = rows; } @@ -118,19 +141,32 @@ public class BlueTableImmutable : IBlueRowCollection 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 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 GetEnumerator() => - rows.Values.GetEnumerator(); + public IEnumerator GetEnumerator() { + foreach (var kv in rows) { + yield return kv.Value; + } + } IEnumerator IEnumerable.GetEnumerator() => GetEnumerator(); @@ -140,11 +176,11 @@ public class BlueTableImmutable : IBlueRowCollection where T : IBlueRow { public class DataPacketBuilder { readonly IBlueTableSchema schema; - readonly ImmutableDictionary.Builder table; - readonly ImmutableDictionary.Builder added = - ImmutableDictionary.Empty.ToBuilder(); - readonly ImmutableDictionary.Builder removed = - ImmutableDictionary.Empty.ToBuilder(); + readonly ImmutableSortedSet>.Builder table; + readonly ImmutableSortedSet>.Builder added = + ImmutableSortedSet>.Empty.ToBuilder(); + readonly ImmutableSortedSet>.Builder removed = + ImmutableSortedSet>.Empty.ToBuilder(); public DataPacketBuilder(BlueTableImmutable table) { schema = table.schema; @@ -153,30 +189,37 @@ public class BlueTableImmutable : IBlueRowCollection where T : IBlueRow { public void AddOrUpdate(T row, IEqualityComparer? 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(schema, table.ToImmutable()); var addedRows = new BlueTableImmutable(schema, added.ToImmutable()); @@ -189,7 +232,7 @@ public class BlueTableImmutable : IBlueRowCollection where T : IBlueRow { } public IDataPacket ToDataPacketT(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()); @@ -207,7 +250,7 @@ public class BlueTableImmutable : IBlueRowCollection where T : IBlueRow { out BlueTableImmutable removedRows, bool reset = true ) { - table.RemoveRange(removed.Keys); + table.RemoveRange(removed); table.AddRange(added); newRows = new BlueTableImmutable(schema, table.ToImmutable()); addedRows = new BlueTableImmutable(schema, added.ToImmutable()); @@ -226,12 +269,12 @@ public class BlueTableImmutable : IBlueRowCollection where T : IBlueRow { public class BlueTableImmutableAdapter : IBlueRowCollection> { readonly BlueTableSchemaAdapter schemaAdapter; - readonly ImmutableDictionary rows; + readonly ImmutableSortedSet> rows; public BlueTableImmutableAdapter(BlueTableSchemaAdapter schemaAdapter) - : this(schemaAdapter, ImmutableDictionary.Empty) { } + : this(schemaAdapter, ImmutableSortedSet>.Empty) { } - public BlueTableImmutableAdapter(BlueTableSchemaAdapter schemaAdapter, ImmutableDictionary rows) { + public BlueTableImmutableAdapter(BlueTableSchemaAdapter schemaAdapter, ImmutableSortedSet> rows) { this.schemaAdapter = schemaAdapter; this.rows = rows; } @@ -241,20 +284,26 @@ public class BlueTableImmutableAdapter : IBlueRowCollection public IBlueTableSchema schema => schemaAdapter.Schema; public object SyncRoot => this; - public BlueRowAdapter this[int id] => new(schemaAdapter, rows[id]); + public BlueRowAdapter 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 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(schemaAdapter, r), index++); + foreach (var kv in rows) { + array.SetValue(new BlueRowAdapter(schemaAdapter, kv.Value), index++); } } public IEnumerator> GetEnumerator() { - foreach (var row in rows.Values) { - yield return new BlueRowAdapter(schemaAdapter, row); + foreach (var kv in rows) { + yield return new BlueRowAdapter(schemaAdapter, kv.Value); } } @@ -263,11 +312,11 @@ public class BlueTableImmutableAdapter : IBlueRowCollection public class DataPacketBuilder { readonly BlueTableSchemaAdapter schemaAdapter; - readonly ImmutableDictionary.Builder table; - readonly ImmutableDictionary.Builder added = - ImmutableDictionary.Empty.ToBuilder(); - readonly ImmutableDictionary.Builder removed = - ImmutableDictionary.Empty.ToBuilder(); + readonly ImmutableSortedSet>.Builder table; + readonly ImmutableSortedSet>.Builder added = + ImmutableSortedSet>.Empty.ToBuilder(); + readonly ImmutableSortedSet>.Builder removed = + ImmutableSortedSet>.Empty.ToBuilder(); public DataPacketBuilder(BlueTableImmutableAdapter table) { schemaAdapter = table.schemaAdapter; @@ -277,26 +326,33 @@ public class BlueTableImmutableAdapter : IBlueRowCollection public void AddOrUpdate(T rowData, IEqualityComparer? comparer = null) { var row = new BlueRowAdapter(schemaAdapter, rowData); var id = row.Id(); - if (table.TryGetValue(id, out var origData)) { - var orig = new BlueRowAdapter(schemaAdapter, origData); +#nullable disable + if (table.TryGetValue(new(id, default), out var origData)) { +#nullable enable + var orig = new BlueRowAdapter(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 : IBlueRowCollection } public IDataPacket> ToDataPacket(bool reset = true) { - table.RemoveRange(removed.Keys); + table.RemoveRange(removed); table.AddRange(added); var newRows = new BlueTableImmutableAdapter(schemaAdapter, table.ToImmutable()); var addedRows = new BlueTableImmutableAdapter(schemaAdapter, added.ToImmutable()); diff --git a/src/DataStreamSuite/DataStreamCore/BlueData/BlueTableSelectorWrapper.cs b/src/DataStreamSuite/DataStreamCore/BlueData/BlueTableSelectorWrapper.cs index 2f6fbd1f0..ed4eacd4d 100644 --- a/src/DataStreamSuite/DataStreamCore/BlueData/BlueTableSelectorWrapper.cs +++ b/src/DataStreamSuite/DataStreamCore/BlueData/BlueTableSelectorWrapper.cs @@ -31,6 +31,9 @@ public class BlueTableSelectorWrapper : IBlueRowCollection where T : IBlue public bool containsID(int id) => source.containsID(id); + public IList GetColumn(int index) => + throw new NotImplementedException(); + public IEnumerator GetEnumerator() { foreach (IBlueRow r in source) { yield return selector(r); diff --git a/src/DataStreamSuite/DataStreamCore/BlueData/CircularBufferTable.cs b/src/DataStreamSuite/DataStreamCore/BlueData/CircularBufferTable.cs index c1322be68..59c349620 100644 --- a/src/DataStreamSuite/DataStreamCore/BlueData/CircularBufferTable.cs +++ b/src/DataStreamSuite/DataStreamCore/BlueData/CircularBufferTable.cs @@ -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 GetColumn(int index) => + _ordered_list.WrapSelector(r => r[index], null); } } diff --git a/src/DataStreamSuite/DataStreamCore/BlueData/IBlueRowCollection.cs b/src/DataStreamSuite/DataStreamCore/BlueData/IBlueRowCollection.cs index 954431150..59ccdd910 100644 --- a/src/DataStreamSuite/DataStreamCore/BlueData/IBlueRowCollection.cs +++ b/src/DataStreamSuite/DataStreamCore/BlueData/IBlueRowCollection.cs @@ -16,6 +16,7 @@ namespace BCG IBlueRow this[int id]{get;} IBlueTableSchema schema {get;} bool containsID (int id); + IList 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 GetColumn(this IBlueRowCollection rows, string colName) { + var idx = Array.IndexOf(rows.schema.columnNames, colName); + if (idx == -1) { + throw new KeyNotFoundException(); + } + return rows.GetColumn(idx); + } + } } diff --git a/src/DataStreamSuite/DataStreamCore/BlueData/ImmutableBlueTable.cs b/src/DataStreamSuite/DataStreamCore/BlueData/ImmutableBlueTable.cs index e2bdcd708..347db29a7 100644 --- a/src/DataStreamSuite/DataStreamCore/BlueData/ImmutableBlueTable.cs +++ b/src/DataStreamSuite/DataStreamCore/BlueData/ImmutableBlueTable.cs @@ -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 GetColumn(int index) => + myRows.WrapSelector(r => r[index], null); + #endregion #region IEnumerable Members diff --git a/src/DataStreamSuite/DataStreamCore/BlueData/KeysBasedTable2.cs b/src/DataStreamSuite/DataStreamCore/BlueData/KeysBasedTable2.cs index 181526e3b..381debde7 100644 --- a/src/DataStreamSuite/DataStreamCore/BlueData/KeysBasedTable2.cs +++ b/src/DataStreamSuite/DataStreamCore/BlueData/KeysBasedTable2.cs @@ -1,4 +1,5 @@ using System; +using System.Collections.Generic; namespace BCG { @@ -132,6 +133,9 @@ namespace BCG return getRow(myGetKeyVals(r)); } + public IList 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);} diff --git a/src/DataStreamSuite/DataStreamCore/BlueData/MovingWindowTable.cs b/src/DataStreamSuite/DataStreamCore/BlueData/MovingWindowTable.cs index eae986b4a..6bf9218b6 100644 --- a/src/DataStreamSuite/DataStreamCore/BlueData/MovingWindowTable.cs +++ b/src/DataStreamSuite/DataStreamCore/BlueData/MovingWindowTable.cs @@ -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 GetColumn(int index) => + mySortedRows.WrapSelector(r => r[index], null); + #endregion #region ICollection Members diff --git a/src/DataStreamSuite/DataStreamCore/BlueData/OrderedBlueTable.cs b/src/DataStreamSuite/DataStreamCore/BlueData/OrderedBlueTable.cs index 43ce64e02..69a79c538 100644 --- a/src/DataStreamSuite/DataStreamCore/BlueData/OrderedBlueTable.cs +++ b/src/DataStreamSuite/DataStreamCore/BlueData/OrderedBlueTable.cs @@ -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; } diff --git a/src/DataStreamSuite/DataStreamCore/BlueData/SequentialIDBlueTable.cs b/src/DataStreamSuite/DataStreamCore/BlueData/SequentialIDBlueTable.cs index 33438d513..b3bdf2892 100644 --- a/src/DataStreamSuite/DataStreamCore/BlueData/SequentialIDBlueTable.cs +++ b/src/DataStreamSuite/DataStreamCore/BlueData/SequentialIDBlueTable.cs @@ -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 GetColumn(int index) => + myRows.WrapSelector(r => r[index], null); + #endregion #region ICollection Members diff --git a/src/DataStreamSuite/DataStreamCore/BlueData/SimpleBindableCollection.cs b/src/DataStreamSuite/DataStreamCore/BlueData/SimpleBindableCollection.cs index 09577d2f9..afa40635b 100644 --- a/src/DataStreamSuite/DataStreamCore/BlueData/SimpleBindableCollection.cs +++ b/src/DataStreamSuite/DataStreamCore/BlueData/SimpleBindableCollection.cs @@ -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 GetColumn(int index) => + myRows.WrapSelector(r => ((IBlueRow)r)[index], null); + #endregion #region ICollection Members diff --git a/src/DataStreamSuite/DataStreamCore/BlueData/SparseVectorBlueTable.cs b/src/DataStreamSuite/DataStreamCore/BlueData/SparseVectorBlueTable.cs index 33a3a0ff6..b0469668a 100644 --- a/src/DataStreamSuite/DataStreamCore/BlueData/SparseVectorBlueTable.cs +++ b/src/DataStreamSuite/DataStreamCore/BlueData/SparseVectorBlueTable.cs @@ -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 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; }} diff --git a/src/DataStreamSuite/DataStreamTests/DataPacketBuilder/DataPacketBuilderTests.cs b/src/DataStreamSuite/DataStreamTests/DataPacketBuilder/DataPacketBuilderTests.cs index 0f247605c..d311ad67e 100644 --- a/src/DataStreamSuite/DataStreamTests/DataPacketBuilder/DataPacketBuilderTests.cs +++ b/src/DataStreamSuite/DataStreamTests/DataPacketBuilder/DataPacketBuilderTests.cs @@ -7,6 +7,8 @@ using bts.collections; using Bts.DataStreamCore; +using mbts.utils; + using NUnit.Framework; using System.Collections.Immutable; @@ -184,7 +186,7 @@ public class DataPacketBuilderTests { } static BlueTableImmutable getTable(params (int Id, double Value)[] rows) => - new(schema, ImmutableDictionary.CreateRange( + new(schema, ImmutableSortedSet.CreateRange( rows.Select(tup => new KeyValuePair(tup.Id, getBlueRow(tup))))); static MutableBlueRow getBlueRow((int id, double value) tup) => @@ -203,13 +205,14 @@ public class DataPacketBuilderTests { (int Id, double Value)[]? removedRows = null) { addedRows ??= Array.Empty<(int, double)>(); removedRows ??= Array.Empty<(int, double)>(); + var kvpComp = new ComparerBuilder>() { kv => kv.Key }; return new DataPacket( - new BlueTableImmutable(schema, ImmutableDictionary.CreateRange( - newRows.Select(tup => new KeyValuePair(tup.Id, getBlueRow(tup))))), - new BlueTableImmutable(schema, ImmutableDictionary.CreateRange( - addedRows.Select(tup => new KeyValuePair(tup.Id, getBlueRow(tup))))), - new BlueTableImmutable(schema, ImmutableDictionary.CreateRange( - removedRows.Select(tup => new KeyValuePair(tup.Id, getBlueRow(tup)))))); + new BlueTableImmutable(schema, ImmutableSortedSet.CreateRange(kvpComp, GetRowKvps(newRows))), + new BlueTableImmutable(schema, ImmutableSortedSet.CreateRange(kvpComp, GetRowKvps(addedRows))), + new BlueTableImmutable(schema, ImmutableSortedSet.CreateRange(kvpComp, GetRowKvps(removedRows)))); + + static IEnumerable> GetRowKvps((int Id, double Value)[] rows) => + rows.Select(tup => new KeyValuePair(tup.Id, getBlueRow(tup))); } static (int, double) deconstructRow(IBlueRow row) => diff --git a/src/client/spark_core/Collections/SortedKeyValueList.cs b/src/client/spark_core/Collections/SortedKeyValueList.cs new file mode 100644 index 000000000..accb92864 --- /dev/null +++ b/src/client/spark_core/Collections/SortedKeyValueList.cs @@ -0,0 +1,133 @@ +namespace Spark.Core.Collections; + +using System; +using System.Collections; +using System.Collections.Generic; + +public static class SortedKeyValueList { + public static SortedKeyValueList Create(SortedList source) where TKey : notnull => + new(source); +} + +public class SortedKeyValueList : IList> where TKey : notnull { + readonly SortedList source; + + public SortedKeyValueList(SortedList source) => + this.source = source; + + public IComparer KeyComparer => source.Comparer; + + public KeyValuePair this[int index] { + get { + if (index < 0 || index >= source.Count) { + throw new IndexOutOfRangeException(); + } + return new(source.Keys[index], source.Values[index]); + } + set { + if (index < 0 || index >= source.Count) { + throw new IndexOutOfRangeException(); + } + if (!canReplace(index, value.Key)) { + throw new ArgumentException("Item key not sorted"); + } + source.RemoveAt(index); + source.Add(value.Key, value.Value); + } + } + + public int Count => source.Count; + + public bool IsReadOnly => false; + + public void Add(KeyValuePair item) => + source.Add(item.Key, item.Value); + + public void Clear() => + source.Clear(); + + public bool Contains(KeyValuePair item) => + source.TryGetValue(item.Key, out var val) && EqualityComparer.Default.Equals(item.Value, val); + + public void CopyTo(KeyValuePair[] array, int arrayIndex) { + foreach (var kv in source) { + array[arrayIndex++] = kv; + } + } + + public IEnumerator> GetEnumerator() => + source.GetEnumerator(); + + public int IndexOf(KeyValuePair item) { + int i = source.IndexOfKey(item.Key); + return i != -1 && !EqualityComparer.Default.Equals(source.GetValueAtIndex(i), item.Value) + ? -1 + : i; + } + + public void Insert(int index, KeyValuePair item) { + if (index < 0 || index > source.Count) { + throw new IndexOutOfRangeException(); + } + if (!canInsert(index, item.Key)) { + throw new ArgumentException("Item key not sorted", nameof(item)); + } + source.Add(item.Key, item.Value); + } + + public bool Remove(KeyValuePair item) { + int i = source.IndexOfKey(item.Key); + if (i < 0 || !EqualityComparer.Default.Equals(item.Value, source.GetValueAtIndex(i))) { + return false; + } + source.RemoveAt(i); + return true; + } + + public void RemoveAt(int index) => + source.RemoveAt(index); + + IEnumerator IEnumerable.GetEnumerator() => + GetEnumerator(); + + bool canInsert(int index, TKey key) { + if (index < 0 || index > source.Count) { + return false; + } + if (source.Count == 0) { + return true; + } + if (index < source.Count) { + var upperKey = source.GetKeyAtIndex(index); + if (KeyComparer.Compare(key, upperKey) > 0) { + return false; + } + } + if (index > 0) { + var lowerKey = source.GetKeyAtIndex(index - 1); + if (KeyComparer.Compare(key, lowerKey) < 0) { + return false; + } + } + return true; + } + + bool canReplace(int index, TKey key) { + if (index < 0 || index >= source.Count) { + return false; + } + if (index < source.Count - 1) { + var upperKey = source.GetKeyAtIndex(index + 1); + if (KeyComparer.Compare(key, upperKey) > 0) { + return false; + } + } + if (index > 0) { + var lowerKey = source.GetKeyAtIndex(index - 1); + if (KeyComparer.Compare(key, lowerKey) < 0) { + return false; + } + } + return true; + } +}