|
4 | 4 |
|
5 | 5 | using System; |
6 | 6 | using System.Collections.Generic; |
| 7 | +using System.Diagnostics; |
7 | 8 | using Microsoft.ML; |
| 9 | +using Microsoft.ML.Data; |
8 | 10 |
|
9 | 11 | namespace Microsoft.Data.Analysis |
10 | 12 | { |
@@ -62,5 +64,91 @@ DataViewRowCursor[] IDataView.GetRowCursorSet(IEnumerable<DataViewSchema.Column> |
62 | 64 | // TODO: change to support parallel cursors |
63 | 65 | return new DataViewRowCursor[] { GetRowCursorCore(columnsNeeded) }; |
64 | 66 | } |
| 67 | + |
| 68 | + private sealed class RowCursor : DataViewRowCursor |
| 69 | + { |
| 70 | + private bool _disposed; |
| 71 | + private long _position; |
| 72 | + private readonly DataFrame _dataFrame; |
| 73 | + private readonly List<Delegate> _getters; |
| 74 | + private Dictionary<int, int> _columnIndexToGetterIndex; |
| 75 | + |
| 76 | + public RowCursor(DataFrame dataFrame, bool[] activeColumns) |
| 77 | + { |
| 78 | + Debug.Assert(dataFrame != null); |
| 79 | + Debug.Assert(activeColumns != null); |
| 80 | + |
| 81 | + _columnIndexToGetterIndex = new Dictionary<int, int>(); |
| 82 | + _position = -1; |
| 83 | + _dataFrame = dataFrame; |
| 84 | + _getters = new List<Delegate>(); |
| 85 | + for (int i = 0; i < Schema.Count; i++) |
| 86 | + { |
| 87 | + if (!activeColumns[i]) |
| 88 | + { |
| 89 | + continue; |
| 90 | + } |
| 91 | + |
| 92 | + Delegate getter = CreateGetterDelegate(i); |
| 93 | + _getters.Add(getter); |
| 94 | + Debug.Assert(getter != null); |
| 95 | + _columnIndexToGetterIndex[i] = _getters.Count - 1; |
| 96 | + } |
| 97 | + } |
| 98 | + |
| 99 | + public override long Position => _position; |
| 100 | + public override long Batch => 0; |
| 101 | + public override DataViewSchema Schema => _dataFrame.DataViewSchema; |
| 102 | + |
| 103 | + protected override void Dispose(bool disposing) |
| 104 | + { |
| 105 | + if (_disposed) |
| 106 | + { |
| 107 | + return; |
| 108 | + } |
| 109 | + |
| 110 | + if (disposing) |
| 111 | + { |
| 112 | + _position = -1; |
| 113 | + } |
| 114 | + |
| 115 | + _disposed = true; |
| 116 | + base.Dispose(disposing); |
| 117 | + } |
| 118 | + |
| 119 | + private Delegate CreateGetterDelegate(int col) |
| 120 | + { |
| 121 | + DataFrameColumn column = _dataFrame.Columns[col]; |
| 122 | + return column.GetDataViewGetter(this); |
| 123 | + } |
| 124 | + |
| 125 | + public override ValueGetter<TValue> GetGetter<TValue>(DataViewSchema.Column column) |
| 126 | + { |
| 127 | + if (!IsColumnActive(column)) |
| 128 | + throw new ArgumentOutOfRangeException(nameof(column)); |
| 129 | + |
| 130 | + return (ValueGetter<TValue>)_getters[_columnIndexToGetterIndex[column.Index]]; |
| 131 | + } |
| 132 | + |
| 133 | + public override ValueGetter<DataViewRowId> GetIdGetter() |
| 134 | + { |
| 135 | + return (ref DataViewRowId value) => value = new DataViewRowId((ulong)_position, 0); |
| 136 | + } |
| 137 | + |
| 138 | + public override bool IsColumnActive(DataViewSchema.Column column) |
| 139 | + { |
| 140 | + return _getters[_columnIndexToGetterIndex[column.Index]] != null; |
| 141 | + } |
| 142 | + |
| 143 | + public override bool MoveNext() |
| 144 | + { |
| 145 | + if (_disposed) |
| 146 | + { |
| 147 | + return false; |
| 148 | + } |
| 149 | + _position++; |
| 150 | + return _position < _dataFrame.Rows.Count; |
| 151 | + } |
| 152 | + } |
65 | 153 | } |
66 | 154 | } |
0 commit comments