151 lines
4.4 KiB
C#
151 lines
4.4 KiB
C#
namespace bts.BlueOrb;
|
|
|
|
using BCG;
|
|
|
|
using Bts.DataStreamCore;
|
|
|
|
using System.Collections.Generic;
|
|
|
|
public class ShiftTable : IBlueRowCollection {
|
|
readonly IBlueRowCollection source;
|
|
readonly int shift;
|
|
readonly int idColIndex;
|
|
|
|
public ShiftTable(IBlueRowCollection source, int shift) {
|
|
this.source = source;
|
|
this.shift = shift;
|
|
idColIndex = source.schema.ResolveIndices("id");
|
|
}
|
|
|
|
public IBlueRow this[int id] {
|
|
get {
|
|
var row = source[id];
|
|
int idx = source.IndexOf(row);
|
|
return shift switch {
|
|
> 0 => new Row(schema, id, idColIndex, idx >= shift ? source.GetRow(idx - shift) : null),
|
|
< 0 => new Row(schema, id, idColIndex, idx < Count + shift ? source.GetRow(idx - shift) : null),
|
|
0 => row,
|
|
};
|
|
}
|
|
}
|
|
|
|
public IBlueTableSchema schema => source.schema;
|
|
|
|
public int Count => source.Count;
|
|
|
|
public bool containsID(int id) =>
|
|
source.containsID(id);
|
|
|
|
public IEnumerator<IBlueRow> GetEnumerator() {
|
|
return shift switch {
|
|
> 0 => ShiftUpImpl(),
|
|
< 0 => ShiftDownImpl(),
|
|
0 => source.GetEnumerator(),
|
|
};
|
|
|
|
IEnumerator<IBlueRow> ShiftUpImpl() {
|
|
var buffer = new List<IBlueRow>();
|
|
int bufferPos = 0;
|
|
// Columns have been "shifted up", so output row vals come from earlier rows.
|
|
foreach (var r in source) {
|
|
if (buffer.Count < shift) {
|
|
yield return new Row(schema, r.Id(), idColIndex, null);
|
|
buffer.Add(r);
|
|
} else {
|
|
int rowValsPos = (bufferPos == 0 ? shift : bufferPos) - 1;
|
|
yield return new Row(schema, r.Id(), idColIndex, buffer[rowValsPos]);
|
|
buffer[bufferPos] = r;
|
|
}
|
|
if (++bufferPos == shift) {
|
|
bufferPos = 0;
|
|
}
|
|
}
|
|
}
|
|
|
|
IEnumerator<IBlueRow> ShiftDownImpl() {
|
|
var buffer = new List<IBlueRow>();
|
|
int bufferPos = 0;
|
|
int shift = -this.shift;
|
|
// Columns have been shifted down, so output row vals come from later rows.
|
|
foreach (var r in source) {
|
|
if (buffer.Count < shift) {
|
|
buffer.Add(r);
|
|
} else {
|
|
int rowIdPos = bufferPos == shift - 1 ? 0 : bufferPos + 1;
|
|
yield return new Row(schema, buffer[rowIdPos].Id(), idColIndex, r);
|
|
buffer[bufferPos] = r;
|
|
}
|
|
if (++bufferPos == shift) {
|
|
bufferPos = 0;
|
|
}
|
|
}
|
|
if (buffer.Count < shift) {
|
|
// Column values have been shifted down by an amount that exceeds the total row count.
|
|
// All values (other than 'id') are nan/null.
|
|
for (int i = 0; i < buffer.Count; i++) {
|
|
var r = buffer[i];
|
|
yield return new Row(schema, r.Id(), idColIndex, null);
|
|
}
|
|
} else {
|
|
int i = bufferPos;
|
|
while (i < shift) {
|
|
var r = buffer[i];
|
|
yield return new Row(schema, r.Id(), idColIndex, null);
|
|
i++;
|
|
}
|
|
for (i = 0; i < bufferPos; i++) {
|
|
var r = buffer[i];
|
|
yield return new Row(schema, r.Id(), idColIndex, null);
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
public IBlueRow GetRow(int index) =>
|
|
source.GetRow(index - shift);
|
|
|
|
public int IndexOf(IBlueRow item) {
|
|
int idx = source.IndexOf(item);
|
|
if (idx != -1) {
|
|
idx -= shift;
|
|
}
|
|
return idx;
|
|
}
|
|
|
|
class Row : IBlueRow {
|
|
readonly IBlueTableSchema schema;
|
|
readonly int id;
|
|
readonly int idColIndex;
|
|
readonly IBlueRow? values;
|
|
|
|
public Row(IBlueTableSchema schema, int id, int idColIndex, IBlueRow? values) {
|
|
this.schema = schema;
|
|
this.id = id;
|
|
this.idColIndex = idColIndex;
|
|
this.values = values;
|
|
}
|
|
|
|
IBlueTableSchema IBlueRow.schema => schema;
|
|
|
|
public object? this[int index] {
|
|
get {
|
|
if (index == idColIndex) {
|
|
return id;
|
|
}
|
|
if (values is not null) {
|
|
return values[index];
|
|
}
|
|
var cols = schema.columnInfo;
|
|
var colType = cols[index].colType;
|
|
if (colType == typeof(double)) {
|
|
return double.NaN;
|
|
}
|
|
if (colType == typeof(float)) {
|
|
return float.NaN;
|
|
}
|
|
return null;
|
|
}
|
|
}
|
|
}
|
|
}
|