Skip to content

Commit

Permalink
Move RowCursor back
Browse files Browse the repository at this point in the history
  • Loading branch information
Prashanth Govindarajan committed Mar 18, 2021
1 parent 9d9f224 commit 1ce802b
Show file tree
Hide file tree
Showing 2 changed files with 88 additions and 98 deletions.
88 changes: 88 additions & 0 deletions src/Microsoft.Data.Analysis/DataFrame.IDataView.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,9 @@

using System;
using System.Collections.Generic;
using System.Diagnostics;
using Microsoft.ML;
using Microsoft.ML.Data;

namespace Microsoft.Data.Analysis
{
Expand Down Expand Up @@ -62,5 +64,91 @@ DataViewRowCursor[] IDataView.GetRowCursorSet(IEnumerable<DataViewSchema.Column>
// TODO: change to support parallel cursors
return new DataViewRowCursor[] { GetRowCursorCore(columnsNeeded) };
}

private sealed class RowCursor : DataViewRowCursor
{
private bool _disposed;
private long _position;
private readonly DataFrame _dataFrame;
private readonly List<Delegate> _getters;
private Dictionary<int, int> _columnIndexToGetterIndex;

public RowCursor(DataFrame dataFrame, bool[] activeColumns)
{
Debug.Assert(dataFrame != null);
Debug.Assert(activeColumns != null);

_columnIndexToGetterIndex = new Dictionary<int, int>();
_position = -1;
_dataFrame = dataFrame;
_getters = new List<Delegate>();
for (int i = 0; i < Schema.Count; i++)
{
if (!activeColumns[i])
{
continue;
}

Delegate getter = CreateGetterDelegate(i);
_getters.Add(getter);
Debug.Assert(getter != null);
_columnIndexToGetterIndex[i] = _getters.Count - 1;
}
}

public override long Position => _position;
public override long Batch => 0;
public override DataViewSchema Schema => _dataFrame.DataViewSchema;

protected override void Dispose(bool disposing)
{
if (_disposed)
{
return;
}

if (disposing)
{
_position = -1;
}

_disposed = true;
base.Dispose(disposing);
}

private Delegate CreateGetterDelegate(int col)
{
DataFrameColumn column = _dataFrame.Columns[col];
return column.GetDataViewGetter(this);
}

public override ValueGetter<TValue> GetGetter<TValue>(DataViewSchema.Column column)
{
if (!IsColumnActive(column))
throw new ArgumentOutOfRangeException(nameof(column));

return (ValueGetter<TValue>)_getters[_columnIndexToGetterIndex[column.Index]];
}

public override ValueGetter<DataViewRowId> GetIdGetter()
{
return (ref DataViewRowId value) => value = new DataViewRowId((ulong)_position, 0);
}

public override bool IsColumnActive(DataViewSchema.Column column)
{
return _getters[_columnIndexToGetterIndex[column.Index]] != null;
}

public override bool MoveNext()
{
if (_disposed)
{
return false;
}
_position++;
return _position < _dataFrame.Rows.Count;
}
}
}
}
98 changes: 0 additions & 98 deletions src/Microsoft.Data.Analysis/RowCursor.cs

This file was deleted.

0 comments on commit 1ce802b

Please sign in to comment.