diff --git a/src/DataStreamSuite/DataStreamCore/BlueData/BlueRowColumnMask.cs b/src/DataStreamSuite/DataStreamCore/BlueData/BlueRowColumnMask.cs new file mode 100644 index 000000000..879f83831 --- /dev/null +++ b/src/DataStreamSuite/DataStreamCore/BlueData/BlueRowColumnMask.cs @@ -0,0 +1,62 @@ +#nullable enable +namespace Bts.DataStreamCore; + +using BCG; + +using bts.collections; + +using System; +using System.Collections.Generic; + +public class BlueRowColumnMask : IBlueRow { + readonly IBlueRow row; + readonly int[] hiddenColumns; + + public BlueRowColumnMask(IBlueRow row, params int[] hiddenColumns) { + this.row = row; + this.hiddenColumns = hiddenColumns; + Array.Sort(hiddenColumns); + } + + /// + /// Provides a way to explicitly skip sorting hiddenColumns. This is done at the callers risk, + /// as the index (int) operator performs a binary search on hiddenColumns. + /// + /// The underlying row whose columns to hide + /// Whether or not to sort in + /// the constructor. They are sorted by default to ensure correct behavior in the index (int) + /// operator, which performs a binary search on this array. + /// The columns (indices) to hide + public BlueRowColumnMask(IBlueRow row, bool sortHiddenColumns, params int[] hiddenColumns) { + this.row = row; + this.hiddenColumns = hiddenColumns; + if (sortHiddenColumns) { + Array.Sort(hiddenColumns); + } + } + + public object? this[string colName] { + get { + for (int i = 0; i < hiddenColumns.Length; i++) { + if (row.schema.columnInfo[i].colName == colName) { + throw new KeyNotFoundException(); + } + } + return row[colName]; + } + } + public object? this[int index] { + get { + if (index > row.schema.columnInfo.Length - hiddenColumns.Length) { + throw new IndexOutOfRangeException(); + } + int pos = hiddenColumns.BinarySearch(index); + if (pos < 0) { + pos = ~pos; + } + return row[index + pos]; + } + } + + public IBlueTableSchema schema => row.schema; +} diff --git a/src/client/spark_core/Collections/Extensions/ArrayExtensions.cs b/src/client/spark_core/Collections/Extensions/ArrayExtensions.cs index 0e224392f..0e9d26d57 100644 --- a/src/client/spark_core/Collections/Extensions/ArrayExtensions.cs +++ b/src/client/spark_core/Collections/Extensions/ArrayExtensions.cs @@ -1,5 +1,6 @@ namespace bts.collections { using System; + using System.Collections.Generic; using System.Diagnostics.CodeAnalysis; using System.Diagnostics.Contracts; @@ -15,6 +16,18 @@ public static TResult[] SelectArray(this T[] array, Func selector) => Array.ConvertAll(array, new Converter(selector)); + public static T[] ToArray(this T[] array) { + var rv = new T[array.Length]; + Array.Copy(array, rv, array.Length); + return rv; + } + + public static int BinarySearch(this T[] array, T value) => + Array.BinarySearch(array, value); + + public static int BinarySearch(this T[] array, T value, IComparer comparer) => + Array.BinarySearch(array, value, comparer); + public static T[] empty_if_null(this T[] source) => source ?? Array.Empty();