Skip to content

Commit

Permalink
fix #1: creation of q dictionary with values represented as table
Browse files Browse the repository at this point in the history
  • Loading branch information
maciejlach committed May 26, 2014
1 parent bc49fd2 commit c774866
Show file tree
Hide file tree
Showing 7 changed files with 97 additions and 7 deletions.
6 changes: 6 additions & 0 deletions CHANGELOG.txt
@@ -1,3 +1,9 @@
------------------------------------------------------------------------------
qSharp 2.0.1 [2014.05.26]
------------------------------------------------------------------------------

- Fix: creation of q dictionary with values represented as table

------------------------------------------------------------------------------
qSharp 2.0 [2014.04.02]
------------------------------------------------------------------------------
Expand Down
71 changes: 65 additions & 6 deletions qSharp/src/QDictionary.cs
Expand Up @@ -25,7 +25,8 @@ namespace qSharp
public sealed class QDictionary : IEnumerable
{
private readonly Array keys;
private readonly Array values;
private readonly object values;
private readonly bool areValuesArray;

/// <summary>
/// Creates new QDictionary instance with given keys and values arrays.
Expand All @@ -49,6 +50,32 @@ public QDictionary(Array keys, Array values)

this.keys = keys;
this.values = values;
this.areValuesArray = true;
}

/// <summary>
/// Creates new QDictionary instance with given keys array and table values.
/// </summary>
public QDictionary(Array keys, QTable values)
{
if (keys == null || keys.Length == 0)
{
throw new ArgumentException("Keys array cannot be null or 0-length");
}

if (values == null || values.RowsCount == 0)
{
throw new ArgumentException("Values table cannot be null or 0-length");
}

if (keys.Length != values.RowsCount)
{
throw new ArgumentException("Keys and value arrays cannot have different length");
}

this.keys = keys;
this.values = values;
this.areValuesArray = false;
}

/// <summary>
Expand All @@ -62,7 +89,7 @@ public Array Keys
/// <summary>
/// Gets an array with dictionary values.
/// </summary>
public Array Values
public object Values
{
get { return values; }
}
Expand Down Expand Up @@ -94,7 +121,14 @@ public override bool Equals(Object obj)
return false;
}

return Utils.ArrayEquals(Keys, d.Keys) && Utils.ArrayEquals(Values, d.Values);
if (areValuesArray)
{
return Utils.ArrayEquals(Keys, d.Keys) && Utils.ArrayEquals(Values as Array, d.Values as Array);
}
else
{
return Utils.ArrayEquals(Keys, d.Keys) && (Values as QTable).Equals(d.Values);
}
}

public override int GetHashCode()
Expand All @@ -114,7 +148,14 @@ public bool Equals(QDictionary d)
return false;
}

return Utils.ArrayEquals(Keys, d.Keys) && Utils.ArrayEquals(Values, d.Values);
if (areValuesArray)
{
return Utils.ArrayEquals(Keys, d.Keys) && Utils.ArrayEquals(Values as Array, d.Values as Array);
}
else
{
return Utils.ArrayEquals(Keys, d.Keys) && (Values as QTable).Equals(d.Values);
}
}

/// <summary>
Expand All @@ -123,7 +164,14 @@ public bool Equals(QDictionary d)
/// <returns>A System.String that represents the current QDictionary</returns>
public override string ToString()
{
return "QDictionary: " + Utils.ArrayToString(Keys) + "!" + Utils.ArrayToString(Values);
if (areValuesArray)
{
return "QDictionary: " + Utils.ArrayToString(Keys) + "!" + Utils.ArrayToString(Values as Array);
}
else
{
return "QDictionary: " + Utils.ArrayToString(Keys) + "!" + (Values as QTable).ToString();
}
}

/// <summary>
Expand Down Expand Up @@ -156,8 +204,19 @@ public object Key
/// </summary>
public object Value
{
get { return _dictionary.values.GetValue(_index); }
get
{
if (_dictionary.areValuesArray)
{
return (_dictionary.values as Array).GetValue(_index);
}
else
{
return (_dictionary.values as QTable)[_index];
}
}
}

}

/// <summary>
Expand Down
4 changes: 4 additions & 0 deletions qSharp/src/QReader.cs
Expand Up @@ -526,6 +526,10 @@ private object ReadDictionary()
{
return new QDictionary(keys as Array, values as Array);
}
if (keys is Array && values is QTable)
{
return new QDictionary(keys as Array, values as QTable);
}
if (keys is QTable && values is QTable)
{
return new QKeyedTable(keys as QTable, values as QTable);
Expand Down
9 changes: 8 additions & 1 deletion qSharp/src/QTable.cs
Expand Up @@ -270,7 +270,14 @@ public object[] ToArray()
return row;
}


/// <summary>
/// Returns a System.String that represents the current QTable row.
/// </summary>
/// <returns>A System.String that represents the current QTable row</returns>
public override string ToString()
{
return Utils.ArrayToString(_table.Columns) + "!" + Utils.ArrayToString(ToArray());
}
}

/// <summary>
Expand Down
10 changes: 10 additions & 0 deletions qSharp/test/QExpression.cs
Expand Up @@ -121,6 +121,11 @@ internal class QExpressions
{"{x+y}[3]", new QLambda("{x+y}", new object[] {3})},
{"(enlist `a)!(enlist 1)", new QDictionary(new[] {"a"}, new[] {1})},
{"1 2!`abc`cdefgh", new QDictionary(new[] {1, 2}, new[] {"abc", "cdefgh"})},
{
"`abc`def`gh!([] one: 1 2 3; two: 4 5 6)",
new QDictionary(new string[] { "abc", "def", "gh" },
new QTable(new[] {"one", "two"}, new object[] {new[] {1L, 2L, 3L}, new[] {4L, 5L, 6L}}))
},
{
"(1;2h;3.3;\"4\")!(`one;2 3;\"456\";(7;8 9))",
new QDictionary(new object[] {1, (short) 2, 3.3, '4'},
Expand Down Expand Up @@ -283,6 +288,11 @@ internal class QExpressions
{"{x+y}[3]", new QLambda("{x+y}", new object[] {(long) 3})},
{"(enlist `a)!(enlist 1)", new QDictionary(new[] {"a"}, new long[] {1})},
{"1 2!`abc`cdefgh", new QDictionary(new long[] {1, 2}, new[] {"abc", "cdefgh"})},
{
"`abc`def`gh!([] one: 1 2 3; two: 4 5 6)",
new QDictionary(new string[] { "abc", "def", "gh" },
new QTable(new[] {"one", "two"}, new object[] {new[] {1L, 2L, 3L}, new[] {4L, 5L, 6L}}))
},
{
"(1;2h;3.3;\"4\")!(`one;2 3;\"456\";(7;8 9))",
new QDictionary(new object[] {(long) 1, (short) 2, 3.3, '4'},
Expand Down
2 changes: 2 additions & 0 deletions qSharp/test/QExpressions.3.out
Expand Up @@ -132,6 +132,8 @@ ED00000080
630B000100000061000700010000000100000000000000
1 2!`abc`cdefgh
63070002000000010000000000000002000000000000000B00020000006162630063646566676800
`abc`def`gh!([] one: 1 2 3; two: 4 5 6)
630B000300000061626300646566006768006200630B00020000006F6E650074776F00000002000000070003000000010000000000000002000000000000000300000000000000070003000000040000000000000005000000000000000600000000000000
(1;2h;3.3;"4")!(`one;2 3;"456";(7;8 9))
63000004000000F90100000000000000FB0200F76666666666660A40F634000004000000F56F6E6500070002000000020000000000000003000000000000000A0003000000343536000002000000F9070000000000000007000200000008000000000000000900000000000000
(0 1; 2 3)!`first`second
Expand Down
2 changes: 2 additions & 0 deletions qSharp/test/QExpressions.out
Expand Up @@ -132,6 +132,8 @@ ED00000080
630B0001000000610006000100000001000000
1 2!`abc`cdefgh
6306000200000001000000020000000B00020000006162630063646566676800
`abc`def`gh!([] one: 1 2 3; two: 4 5 6)
630B000300000061626300646566006768006200630B00020000006F6E650074776F00000002000000070003000000010000000000000002000000000000000300000000000000070003000000040000000000000005000000000000000600000000000000
(1;2h;3.3;"4")!(`one;2 3;"456";(7;8 9))
63000004000000FA01000000FB0200F76666666666660A40F634000004000000F56F6E650006000200000002000000030000000A0003000000343536000002000000FA070000000600020000000800000009000000
(0 1; 2 3)!`first`second
Expand Down

0 comments on commit c774866

Please sign in to comment.