Skip to content

Commit 1ce802b

Browse files
author
Prashanth Govindarajan
committed
Move RowCursor back
1 parent 9d9f224 commit 1ce802b

File tree

2 files changed

+88
-98
lines changed

2 files changed

+88
-98
lines changed

src/Microsoft.Data.Analysis/DataFrame.IDataView.cs

Lines changed: 88 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,9 @@
44

55
using System;
66
using System.Collections.Generic;
7+
using System.Diagnostics;
78
using Microsoft.ML;
9+
using Microsoft.ML.Data;
810

911
namespace Microsoft.Data.Analysis
1012
{
@@ -62,5 +64,91 @@ DataViewRowCursor[] IDataView.GetRowCursorSet(IEnumerable<DataViewSchema.Column>
6264
// TODO: change to support parallel cursors
6365
return new DataViewRowCursor[] { GetRowCursorCore(columnsNeeded) };
6466
}
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+
}
65153
}
66154
}

src/Microsoft.Data.Analysis/RowCursor.cs

Lines changed: 0 additions & 98 deletions
This file was deleted.

0 commit comments

Comments
 (0)