Skip to content

Commit

Permalink
Extend the Table interface API
Browse files Browse the repository at this point in the history
  • Loading branch information
maciejlach committed Aug 14, 2015
1 parent f842d90 commit 12605fd
Show file tree
Hide file tree
Showing 5 changed files with 74 additions and 6 deletions.
2 changes: 1 addition & 1 deletion CHANGELOG.txt
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
------------------------------------------------------------------------------
qSharp 2.2.0 [2015.08.x]
qSharp 2.2.0 [2015.08.14]
------------------------------------------------------------------------------

- API convenience extensions
Expand Down
15 changes: 15 additions & 0 deletions qSharp/src/IQTable.cs
Original file line number Diff line number Diff line change
Expand Up @@ -32,5 +32,20 @@ public interface IQTable
/// Gets a number of columns in table.
/// </summary>
int ColumnsCount { get; }

/// <summary>
/// Checks whether table contains column with given name.
/// </summary>
/// <param name="column">Name of the column</param>
/// <returns>true if table contains column with given name, false otherwise</returns>
bool HasColumn(string column);

/// <summary>
/// Gets a column index for specified name.
/// </summary>
/// <param name="column">Name of the column</param>
/// <returns>0 based column index
int GetColumnIndex(string column);

}
}
27 changes: 27 additions & 0 deletions qSharp/src/QKeyedTable.cs
Original file line number Diff line number Diff line change
Expand Up @@ -157,6 +157,33 @@ System.Collections.IEnumerator System.Collections.IEnumerable.GetEnumerator()
return this.GetEnumerator();
}

/// <summary>
/// Checks whether table contains column with given name.
/// </summary>
/// <param name="column">Name of the column</param>
/// <returns>true if table contains column with given name, false otherwise</returns>
public bool HasColumn(string column)
{
return _keys.HasColumn(column) || _values.HasColumn(column);
}

/// <summary>
/// Gets a column index for specified name.
/// </summary>
/// <param name="column">Name of the column</param>
/// <returns>0 based column index
public int GetColumnIndex(string column)
{
if (_keys.HasColumn(column))
{
return _keys.GetColumnIndex(column);
}
else
{
return _values.GetColumnIndex(column) + _keys.ColumnsCount;
}
}

/// <summary>
/// Determines whether the specified System.Object is equal to the current QKeyedTable.
/// </summary>
Expand Down
21 changes: 16 additions & 5 deletions qSharp/src/QTable.cs
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ public QTable(string[] columns, Array data)

_columns = columns;
Data = data;
RowsCount = ((Array) data.GetValue(0)).Length;
RowsCount = ((Array)data.GetValue(0)).Length;
}

/// <summary>
Expand Down Expand Up @@ -120,14 +120,24 @@ System.Collections.IEnumerator System.Collections.IEnumerable.GetEnumerator()
return this.GetEnumerator();
}

/// <summary>
/// Checks whether table contains column with given name.
/// </summary>
/// <param name="column">Name of the column</param>
/// <returns>true if table contains column with given name, false otherwise</returns>
public bool HasColumn(string column)
{
return _columnsMap.Contains(column);
}

/// <summary>
/// Gets a column index for specified name.
/// </summary>
/// <param name="column">Name of the column</param>
/// <returns>0 based column index</returns>
/// <returns>0 based column index
public int GetColumnIndex(string column)
{
return (int) _columnsMap[column];
return (int)_columnsMap[column];
}

/// <summary>
Expand All @@ -148,7 +158,7 @@ public override bool Equals(object obj)

public override int GetHashCode()
{
return 31*Columns.GetHashCode() + Data.GetHashCode();
return 31 * Columns.GetHashCode() + Data.GetHashCode();
}

/// <summary>
Expand Down Expand Up @@ -228,7 +238,8 @@ public struct Row : IEnumerable
/// <summary>
/// Initializes a new instance of the Row.
/// </summary>
public Row(QTable table, int rowIndex) : this()
public Row(QTable table, int rowIndex)
: this()
{
if (rowIndex < 0 || rowIndex > table.RowsCount)
{
Expand Down
15 changes: 15 additions & 0 deletions qSharp/test/DataTypeTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
//

using NUnit.Framework;
using System;

namespace qSharp.test
{
Expand Down Expand Up @@ -88,6 +89,12 @@ public void TestQTable()
var t = new QTable(columns, data);
Assert.AreEqual(t, new QTable(columns, data));

Assert.IsTrue(t.HasColumn("pos"));
Assert.IsFalse(t.HasColumn("unknown"));

Assert.AreEqual(1, t.GetColumnIndex("dates"));
Assert.Throws<NullReferenceException>(() => t.GetColumnIndex("unknown"));

int i = 0;
var e = t.GetEnumerator();

Expand All @@ -111,6 +118,14 @@ public void TestQKeyedTable()
var kt = new QKeyedTable(columns, keyColumns, data);
Assert.AreEqual(kt, new QKeyedTable(columns, keyColumns, data));

Assert.IsTrue(kt.HasColumn("pos"));
Assert.IsFalse(kt.HasColumn("unknown"));

Assert.AreEqual(0, kt.GetColumnIndex("eid"));
Assert.AreEqual(1, kt.GetColumnIndex("pos"));
Assert.AreEqual(2, kt.GetColumnIndex("dates"));
Assert.Throws<NullReferenceException>(() => kt.GetColumnIndex("unknown"));

int i = 0;
var e = kt.GetEnumerator();

Expand Down

0 comments on commit 12605fd

Please sign in to comment.