diff --git a/src/DataStreamSuite/DataStream/DataStreamExtensions.cs b/src/DataStreamSuite/DataStream/DataStreamExtensions.cs index 121e31bb9..49d9130d6 100644 --- a/src/DataStreamSuite/DataStream/DataStreamExtensions.cs +++ b/src/DataStreamSuite/DataStream/DataStreamExtensions.cs @@ -109,44 +109,4 @@ public static class DataStreamExtensions { public static ViewStream AsViewStream(this IObservable> source, IBlueTableSchema schema) where T : IBlueRow => source.AsDataPackets(schema).AsViewStream(); - - public static int ResolveIndices(this IBlueTableSchema schema, string name) { - var indices = ResolveIndices(schema, new[] { name }); - return indices[0]; - } - - public static (int, int) ResolveIndices(this IBlueTableSchema schema, string name1, string name2) { - var indices = ResolveIndices(schema, new[] { name1, name2 }); - return (indices[0], indices[1]); - } - - public static (int, int, int) ResolveIndices(this IBlueTableSchema schema, string name1, string name2, string name3) { - var indices = ResolveIndices(schema, new[] { name1, name2, name3 }); - return (indices[0], indices[1], indices[2]); - } - - public static (int, int, int, int) ResolveIndices(this IBlueTableSchema schema, string name1, string name2, string name3, string name4) { - var indices = ResolveIndices(schema, new[] { name1, name2, name3, name4 }); - return (indices[0], indices[1], indices[2], indices[3]); - } - - public static int[] ResolveIndices(this IBlueTableSchema schema, string[] columnNames) { - var columns = schema.columnInfo; - var indices = new int[columnNames.Length]; - 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]) { - indices[i] = j; - nResolved++; - break; - } - } - if (nResolved == origNResolved) { - throw new ArgumentException($"No column named '{columnNames[i]}'", nameof(schema)); - } - } - return indices; - } } diff --git a/src/DataStreamSuite/DataStreamCore/BlueData/BlueTableSchemaExtensions.cs b/src/DataStreamSuite/DataStreamCore/BlueData/BlueTableSchemaExtensions.cs new file mode 100644 index 000000000..27204fd8d --- /dev/null +++ b/src/DataStreamSuite/DataStreamCore/BlueData/BlueTableSchemaExtensions.cs @@ -0,0 +1,91 @@ +#nullable enable +namespace Bts.DataStreamCore; + +using BCG; + +using bts.collections; + +using System; + +public static class BlueTableSchemaExtensions { + public static int ColumnCount(this IBlueTableSchema schema) => + schema.columnInfo.Length; + + public static int ResolveIndices(this IBlueTableSchema schema, string name) { + var indices = ResolveIndices(schema, new[] { name }); + return indices[0]; + } + + public static (int, int) ResolveIndices(this IBlueTableSchema schema, string name1, string name2) { + var indices = ResolveIndices(schema, new[] { name1, name2 }); + return (indices[0], indices[1]); + } + + public static (int, int, int) ResolveIndices(this IBlueTableSchema schema, string name1, string name2, string name3) { + var indices = ResolveIndices(schema, new[] { name1, name2, name3 }); + return (indices[0], indices[1], indices[2]); + } + + public static (int, int, int, int) ResolveIndices(this IBlueTableSchema schema, string name1, string name2, string name3, string name4) { + var indices = ResolveIndices(schema, new[] { name1, name2, name3, name4 }); + return (indices[0], indices[1], indices[2], indices[3]); + } + + public static int[] ResolveIndices(this IBlueTableSchema schema, string[] columnNames) => + ResolveIndices(schema.columnInfo, columnNames); + + public static int ResolveIndices(this ColumnInfo[] columns, string name) { + var indices = ResolveIndices(columns, new[] { name }); + return indices[0]; + } + + public static int[] ResolveIndices(this ColumnInfo[] columns, string[] columnNames) { + var indices = new int[columnNames.Length]; + 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]) { + indices[i] = j; + nResolved++; + break; + } + } + if (nResolved == origNResolved) { + throw new ArgumentException($"No column named '{columnNames[i]}'", nameof(columns)); + } + } + return indices; + } + + public static bool TryResolveIndices(this IBlueTableSchema schema, string name, out int result) { + var res = TryResolveIndices(schema, new[] { name })[0]; + result = res ?? -1; + return res.HasValue; + } + + public static int? TryResolveIndices(this IBlueTableSchema schema, string name) => + TryResolveIndices(schema, new[] { name })[0]; + + public static int? TryResolveIndices(this ColumnInfo[] columns, string name) => + TryResolveIndices(columns, new[] { name })[0]; + + public static int?[] TryResolveIndices(this IBlueTableSchema schema, string[] columnNames) => + TryResolveIndices(schema.columnInfo, columnNames); + + public static int?[] TryResolveIndices(this ColumnInfo[] columns, string[] columnNames) { + var indices = new int?[columnNames.Length]; + indices.Fill(null); + int nResolved = 0; + for (int i = 0; i < columnNames.Length; i++) { + for (int j = 0; j < columns.Length; j++) { + if (columns[j].colName == columnNames[i]) { + indices[i] = j; + nResolved++; + break; + } + } + } + return indices; + } +}