diff --git a/src/DataStreamSuite/DataStreamCore/BlueData/ColumnIndex.cs b/src/DataStreamSuite/DataStreamCore/BlueData/ColumnIndex.cs new file mode 100644 index 000000000..76cc540fc --- /dev/null +++ b/src/DataStreamSuite/DataStreamCore/BlueData/ColumnIndex.cs @@ -0,0 +1,64 @@ +namespace Bts.DataStreamCore; + +using BCG; + +using System; + +public struct ColumnIndex { + public readonly string[] ColumnNames; + public readonly int[] ColumnIndices; + + public ColumnIndex(params string[] columnNames) { + ColumnNames = columnNames; + ColumnIndices = new int[columnNames.Length]; + } + + public readonly int this[int nameIndex] => ColumnIndices[nameIndex]; + + public static int Resolve(ColumnInfo[] columns, string name) { + var indices = Resolve(columns, new[] { name }); + return indices[0]; + } + + public static (int, int) Resolve(ColumnInfo[] columns, string name1, string name2) { + var indices = Resolve(columns, new[] { name1, name2 }); + return (indices[0], indices[1]); + } + + public static int[] Resolve(ColumnInfo[] columns, params string[] names) { + var indices = new int[names.Length]; + int nResolved = 0; + for (int i = 0; i < names.Length; i++) { + int origNResolved = nResolved; + for (int j = 0; j < columns.Length; j++) { + if (columns[j].colName == names[i]) { + indices[i] = j; + nResolved++; + break; + } + } + if (nResolved == origNResolved) { + throw new ArgumentException($"No column named '{names[i]}'", nameof(columns)); + } + } + return indices; + } + + public int Resolve(ColumnInfo[] columns, bool requireSuccess = true) { + int nResolved = 0; + for (int i = 0; i < ColumnNames.Length; i++) { + int origNResolved = nResolved; + for (int j = 0; j < columns.Length; j++) { + if (columns[j].colName == ColumnNames[i]) { + ColumnIndices[i] = j; + nResolved++; + break; + } + } + if (requireSuccess && nResolved == origNResolved) { + throw new ArgumentException($"No column named '{ColumnNames[i]}'", nameof(columns)); + } + } + return nResolved; + } +}