From db2bd425be9110a411ca8a0f569237cf68eeae75 Mon Sep 17 00:00:00 2001 From: John Colvin Date: Tue, 12 Nov 2024 15:11:37 +0000 Subject: [PATCH] Add BlueRowColumnMask to hide columns w/ minimal overhead Less overhead than ProjectStream --- .../BlueData/BlueRowColumnMask.cs | 62 +++++++++++++++++++ 1 file changed, 62 insertions(+) create mode 100644 src/DataStreamSuite/DataStreamCore/BlueData/BlueRowColumnMask.cs 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; +}