From c7748666830a6fdaa431aa8891b708f3d640bf38 Mon Sep 17 00:00:00 2001 From: maciejlach Date: Mon, 26 May 2014 15:26:35 +0200 Subject: [PATCH] fix #1: creation of q dictionary with values represented as table --- CHANGELOG.txt | 6 +++ qSharp/src/QDictionary.cs | 71 +++++++++++++++++++++++++++++++--- qSharp/src/QReader.cs | 4 ++ qSharp/src/QTable.cs | 9 ++++- qSharp/test/QExpression.cs | 10 +++++ qSharp/test/QExpressions.3.out | 2 + qSharp/test/QExpressions.out | 2 + 7 files changed, 97 insertions(+), 7 deletions(-) diff --git a/CHANGELOG.txt b/CHANGELOG.txt index 32e4bed..1a29698 100644 --- a/CHANGELOG.txt +++ b/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] ------------------------------------------------------------------------------ diff --git a/qSharp/src/QDictionary.cs b/qSharp/src/QDictionary.cs index 4be34a8..8f6e7f9 100644 --- a/qSharp/src/QDictionary.cs +++ b/qSharp/src/QDictionary.cs @@ -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; /// /// Creates new QDictionary instance with given keys and values arrays. @@ -49,6 +50,32 @@ public QDictionary(Array keys, Array values) this.keys = keys; this.values = values; + this.areValuesArray = true; + } + + /// + /// Creates new QDictionary instance with given keys array and table values. + /// + 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; } /// @@ -62,7 +89,7 @@ public Array Keys /// /// Gets an array with dictionary values. /// - public Array Values + public object Values { get { return values; } } @@ -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() @@ -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); + } } /// @@ -123,7 +164,14 @@ public bool Equals(QDictionary d) /// A System.String that represents the current QDictionary 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(); + } } /// @@ -156,8 +204,19 @@ public object Key /// 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]; + } + } } + } /// diff --git a/qSharp/src/QReader.cs b/qSharp/src/QReader.cs index ba99967..72200b0 100644 --- a/qSharp/src/QReader.cs +++ b/qSharp/src/QReader.cs @@ -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); diff --git a/qSharp/src/QTable.cs b/qSharp/src/QTable.cs index 22af000..32e40bf 100644 --- a/qSharp/src/QTable.cs +++ b/qSharp/src/QTable.cs @@ -270,7 +270,14 @@ public object[] ToArray() return row; } - + /// + /// Returns a System.String that represents the current QTable row. + /// + /// A System.String that represents the current QTable row + public override string ToString() + { + return Utils.ArrayToString(_table.Columns) + "!" + Utils.ArrayToString(ToArray()); + } } /// diff --git a/qSharp/test/QExpression.cs b/qSharp/test/QExpression.cs index ceabdb2..caad4aa 100644 --- a/qSharp/test/QExpression.cs +++ b/qSharp/test/QExpression.cs @@ -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'}, @@ -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'}, diff --git a/qSharp/test/QExpressions.3.out b/qSharp/test/QExpressions.3.out index d3f06c0..8dd0e49 100644 --- a/qSharp/test/QExpressions.3.out +++ b/qSharp/test/QExpressions.3.out @@ -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 diff --git a/qSharp/test/QExpressions.out b/qSharp/test/QExpressions.out index 3641a84..b5b3d70 100644 --- a/qSharp/test/QExpressions.out +++ b/qSharp/test/QExpressions.out @@ -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