Skip to content

Commit

Permalink
Improve memory reuse while iterating over QTable, QDictionary and QKe…
Browse files Browse the repository at this point in the history
…yedTable
  • Loading branch information
maciejlach committed Aug 12, 2015
1 parent 4352820 commit f842d90
Show file tree
Hide file tree
Showing 4 changed files with 35 additions and 27 deletions.
2 changes: 2 additions & 0 deletions CHANGELOG.txt
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@
- API convenience extensions
- QTable, QDictionary and QKeyedTable now implements generic version of
IEnumerable interface
- Improved memory reuse while iterating over QTable, QDictionary
and QKeyedTable
- Fix: QDictionary enumerator failing to retrieve values

------------------------------------------------------------------------------
Expand Down
20 changes: 11 additions & 9 deletions qSharp/src/QDictionary.cs
Original file line number Diff line number Diff line change
Expand Up @@ -185,23 +185,23 @@ public override string ToString()
public struct KeyValuePair
{
private readonly QDictionary _dictionary;
private readonly int _index;
public int Index { get; internal set; }

/// <summary>
/// Initializes a new instance of the KeyValuePair.
/// </summary>
public KeyValuePair(QDictionary dictionary, int index)
public KeyValuePair(QDictionary dictionary, int index) : this()
{
_dictionary = dictionary;
_index = index;
Index = index;
}

/// <summary>
/// Gets the key in the key/value pair.
/// </summary>
public object Key
{
get { return _dictionary._keys.GetValue(_index); }
get { return _dictionary._keys.GetValue(Index); }
}

/// <summary>
Expand All @@ -214,14 +214,13 @@ public object Value
if (!_dictionary._areValuesArray)
{
var qTable = _dictionary._values as QTable;
if (qTable != null) return qTable[_index];

return qTable[Index];
}
else
{
return (_dictionary._values as Array).GetValue(_index);
return (_dictionary._values as Array).GetValue(Index);
}

throw new NotFiniteNumberException(string.Format("Type {0} is unsupported.", _dictionary._values != null ? _dictionary._values.GetType().Name : "null"));
}
}
}
Expand All @@ -233,15 +232,17 @@ private sealed class QDictionaryEnumerator : IEnumerator<QDictionary.KeyValuePai
{
private readonly QDictionary _dictionary;
private int _index = -1;
private QDictionary.KeyValuePair _current;

public QDictionaryEnumerator(QDictionary dictionary)
{
_dictionary = dictionary;
_current = new QDictionary.KeyValuePair(_dictionary, _index);
}

public QDictionary.KeyValuePair Current
{
get { return new KeyValuePair(_dictionary, _index); }
get { return _current; }
}

object IEnumerator.Current
Expand All @@ -252,6 +253,7 @@ object IEnumerator.Current
public bool MoveNext()
{
_index++;
_current.Index = _index;
return _index < _dictionary._keys.Length;
}

Expand Down
20 changes: 11 additions & 9 deletions qSharp/src/QKeyedTable.cs
Original file line number Diff line number Diff line change
Expand Up @@ -207,32 +207,32 @@ public override string ToString()
/// </summary>
public struct KeyValuePair
{
private readonly int _index;
private readonly QKeyedTable _kt;
public int Index { get; internal set; }

/// <summary>
/// Initializes a new instance of the KeyValuePair.
/// </summary>
public KeyValuePair(QKeyedTable table, int index)
public KeyValuePair(QKeyedTable table, int index) : this()
{
_kt = table;
_index = index;
Index = index;
}

/// <summary>
/// Gets the key in the key/value pair.
/// </summary>
public object Key
{
get { return _kt._keys[_index]; }
get { return _kt._keys[Index]; }
}

/// <summary>
/// Gets the value in the key/value pair.
/// </summary>
public object Value
{
get { return _kt._values[_index]; }
get { return _kt._values[Index]; }
}
}

Expand All @@ -242,16 +242,18 @@ public object Value
private sealed class QKeyedTableEnumerator : IEnumerator<QKeyedTable.KeyValuePair>
{
private readonly QKeyedTable _kt;
private int _index = -1;
private int _index = 0;
private QKeyedTable.KeyValuePair _current;

public QKeyedTableEnumerator(QKeyedTable table)
{
_kt = table;
_current = new KeyValuePair(_kt, _index);
}

public QKeyedTable.KeyValuePair Current
{
get { return new KeyValuePair(_kt, _index); }
get { return _current; }
}

object IEnumerator.Current
Expand All @@ -261,8 +263,8 @@ object IEnumerator.Current

public bool MoveNext()
{
_index++;
return _index < _kt._keys.RowsCount;
_current.Index = _index;
return _index++ < _kt._keys.RowsCount;
}

public void Reset()
Expand Down
20 changes: 11 additions & 9 deletions qSharp/src/QTable.cs
Original file line number Diff line number Diff line change
Expand Up @@ -181,16 +181,18 @@ public override string ToString()
private sealed class QTableEnumerator : IEnumerator<QTable.Row>
{
private readonly QTable _table;
private int _index = -1;
private int _index = 0;
private QTable.Row _current;

public QTableEnumerator(QTable table)
{
_table = table;
_current = new Row(_table, _index);
}

public QTable.Row Current
{
get { return new Row(_table, _index); }
get { return _current; }
}

object IEnumerator.Current
Expand All @@ -200,8 +202,8 @@ object IEnumerator.Current

public bool MoveNext()
{
_index++;
return _index < _table.RowsCount;
_current.Index = _index;
return _index++ < _table.RowsCount;
}

public void Reset()
Expand All @@ -220,20 +222,20 @@ public void Dispose()
/// </summary>
public struct Row : IEnumerable
{
private readonly int _rowIndex;
private readonly QTable _table;
public int Index { get; internal set; }

/// <summary>
/// Initializes a new instance of the Row.
/// </summary>
public Row(QTable table, int rowIndex)
public Row(QTable table, int rowIndex) : this()
{
if (rowIndex < 0 || rowIndex > table.RowsCount)
{
throw new ArgumentOutOfRangeException();
}
_table = table;
_rowIndex = rowIndex;
Index = rowIndex;
}

/// <summary>
Expand All @@ -251,8 +253,8 @@ public int Length
/// <returns>object</returns>
public object this[int index]
{
get { return ((Array) _table.Data.GetValue(index)).GetValue(_rowIndex); }
set { ((Array) _table.Data.GetValue(index)).SetValue(value, _rowIndex); }
get { return ((Array)_table.Data.GetValue(index)).GetValue(Index); }
set { ((Array)_table.Data.GetValue(index)).SetValue(value, Index); }
}

/// <summary>
Expand Down

0 comments on commit f842d90

Please sign in to comment.