Add ColumnIndex class to resolve column names to indices

Improves performance compared to accessing columns by name
This commit is contained in:
John Colvin committed 2025-11-14 14:59:33 -05:00
1 parent 519ef8b0af
commit 1bb7d6eca2
1 file changed
+64
@@ -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;
}
}