Add contains proc for tables to allow usage of in
This commit is contained in:
parent
b9e02b1efc
commit
8e4af5ace0
3 changed files with 43 additions and 15 deletions
|
|
@ -215,6 +215,10 @@ proc hasKey*[A, B](t: Table[A, B], key: A): bool =
|
||||||
var hc: THash
|
var hc: THash
|
||||||
result = rawGet(t, key, hc) >= 0
|
result = rawGet(t, key, hc) >= 0
|
||||||
|
|
||||||
|
proc contains*[A, B](t: Table[A, B], key: A): bool =
|
||||||
|
## alias of `hasKey` for use with the `in` operator.
|
||||||
|
return hasKey[A, B](t, key)
|
||||||
|
|
||||||
proc rawInsert[A, B](t: var Table[A, B], data: var KeyValuePairSeq[A, B],
|
proc rawInsert[A, B](t: var Table[A, B], data: var KeyValuePairSeq[A, B],
|
||||||
key: A, val: B, hc: THash, h: THash) =
|
key: A, val: B, hc: THash, h: THash) =
|
||||||
rawInsertImpl()
|
rawInsertImpl()
|
||||||
|
|
@ -411,6 +415,10 @@ proc hasKey*[A, B](t: TableRef[A, B], key: A): bool =
|
||||||
## returns true iff `key` is in the table `t`.
|
## returns true iff `key` is in the table `t`.
|
||||||
result = t[].hasKey(key)
|
result = t[].hasKey(key)
|
||||||
|
|
||||||
|
proc contains*[A, B](t: TableRef[A, B], key: A): bool =
|
||||||
|
## alias of `hasKey` for use with the `in` operator.
|
||||||
|
return hasKey[A, B](t, key)
|
||||||
|
|
||||||
proc `[]=`*[A, B](t: TableRef[A, B], key: A, val: B) =
|
proc `[]=`*[A, B](t: TableRef[A, B], key: A, val: B) =
|
||||||
## puts a (key, value)-pair into `t`.
|
## puts a (key, value)-pair into `t`.
|
||||||
t[][key] = val
|
t[][key] = val
|
||||||
|
|
@ -532,6 +540,10 @@ proc hasKey*[A, B](t: OrderedTable[A, B], key: A): bool =
|
||||||
var hc: THash
|
var hc: THash
|
||||||
result = rawGet(t, key, hc) >= 0
|
result = rawGet(t, key, hc) >= 0
|
||||||
|
|
||||||
|
proc contains*[A, B](t: OrderedTable[A, B], key: A): bool =
|
||||||
|
## alias of `hasKey` for use with the `in` operator.
|
||||||
|
return hasKey[A, B](t, key)
|
||||||
|
|
||||||
proc rawInsert[A, B](t: var OrderedTable[A, B],
|
proc rawInsert[A, B](t: var OrderedTable[A, B],
|
||||||
data: var OrderedKeyValuePairSeq[A, B],
|
data: var OrderedKeyValuePairSeq[A, B],
|
||||||
key: A, val: B, hc: THash, h: THash) =
|
key: A, val: B, hc: THash, h: THash) =
|
||||||
|
|
@ -704,6 +716,10 @@ proc hasKey*[A, B](t: OrderedTableRef[A, B], key: A): bool =
|
||||||
## returns true iff `key` is in the table `t`.
|
## returns true iff `key` is in the table `t`.
|
||||||
result = t[].hasKey(key)
|
result = t[].hasKey(key)
|
||||||
|
|
||||||
|
proc contains*[A, B](t: OrderedTableRef[A, B], key: A): bool =
|
||||||
|
## alias of `hasKey` for use with the `in` operator.
|
||||||
|
return hasKey[A, B](t, key)
|
||||||
|
|
||||||
proc `[]=`*[A, B](t: OrderedTableRef[A, B], key: A, val: B) =
|
proc `[]=`*[A, B](t: OrderedTableRef[A, B], key: A, val: B) =
|
||||||
## puts a (key, value)-pair into `t`.
|
## puts a (key, value)-pair into `t`.
|
||||||
t[][key] = val
|
t[][key] = val
|
||||||
|
|
@ -804,6 +820,10 @@ proc hasKey*[A](t: CountTable[A], key: A): bool =
|
||||||
## returns true iff `key` is in the table `t`.
|
## returns true iff `key` is in the table `t`.
|
||||||
result = rawGet(t, key) >= 0
|
result = rawGet(t, key) >= 0
|
||||||
|
|
||||||
|
proc contains*[A](t: CountTable[A], key: A): bool =
|
||||||
|
## alias of `hasKey` for use with the `in` operator.
|
||||||
|
return hasKey[A](t, key)
|
||||||
|
|
||||||
proc rawInsert[A](t: CountTable[A], data: var seq[tuple[key: A, val: int]],
|
proc rawInsert[A](t: CountTable[A], data: var seq[tuple[key: A, val: int]],
|
||||||
key: A, val: int) =
|
key: A, val: int) =
|
||||||
var h: THash = hash(key) and high(data)
|
var h: THash = hash(key) and high(data)
|
||||||
|
|
@ -945,6 +965,10 @@ proc hasKey*[A](t: CountTableRef[A], key: A): bool =
|
||||||
## returns true iff `key` is in the table `t`.
|
## returns true iff `key` is in the table `t`.
|
||||||
result = t[].hasKey(key)
|
result = t[].hasKey(key)
|
||||||
|
|
||||||
|
proc contains*[A](t: CountTableRef[A], key: A): bool =
|
||||||
|
## alias of `hasKey` for use with the `in` operator.
|
||||||
|
return hasKey[A](t, key)
|
||||||
|
|
||||||
proc `[]=`*[A](t: CountTableRef[A], key: A, val: int) =
|
proc `[]=`*[A](t: CountTableRef[A], key: A, val: int) =
|
||||||
## puts a (key, value)-pair into `t`. `val` has to be positive.
|
## puts a (key, value)-pair into `t`. `val` has to be positive.
|
||||||
assert val > 0
|
assert val > 0
|
||||||
|
|
|
||||||
|
|
@ -22,15 +22,15 @@ const
|
||||||
"---00": 346677844,
|
"---00": 346677844,
|
||||||
"0": 34404,
|
"0": 34404,
|
||||||
"1": 344004,
|
"1": 344004,
|
||||||
"10": 34484,
|
"10": 34484,
|
||||||
"11": 34474,
|
"11": 34474,
|
||||||
"12": 789,
|
"12": 789,
|
||||||
"19": 34464,
|
"19": 34464,
|
||||||
"2": 344774, "20": 34454,
|
"2": 344774, "20": 34454,
|
||||||
"3": 342244, "30": 34141244,
|
"3": 342244, "30": 34141244,
|
||||||
"34": 123456,
|
"34": 123456,
|
||||||
"4": 3412344, "40": 344114,
|
"4": 3412344, "40": 344114,
|
||||||
"5": 341232144, "50": 344490,
|
"5": 341232144, "50": 344490,
|
||||||
"6": 34214544, "60": 344491,
|
"6": 34214544, "60": 344491,
|
||||||
"7": 3434544, "70": 344492,
|
"7": 3434544, "70": 344492,
|
||||||
"8": 344544, "80": 344497,
|
"8": 344544, "80": 344497,
|
||||||
|
|
@ -46,7 +46,7 @@ block tableTest1:
|
||||||
for x in 0..1:
|
for x in 0..1:
|
||||||
for y in 0..1:
|
for y in 0..1:
|
||||||
assert t[(x,y)] == $x & $y
|
assert t[(x,y)] == $x & $y
|
||||||
assert($t ==
|
assert($t ==
|
||||||
"{(x: 0, y: 1): 01, (x: 0, y: 0): 00, (x: 1, y: 0): 10, (x: 1, y: 1): 11}")
|
"{(x: 0, y: 1): 01, (x: 0, y: 0): 00, (x: 1, y: 0): 10, (x: 1, y: 1): 11}")
|
||||||
|
|
||||||
block tableTest2:
|
block tableTest2:
|
||||||
|
|
@ -55,14 +55,16 @@ block tableTest2:
|
||||||
t["111"] = 1.000043
|
t["111"] = 1.000043
|
||||||
t["123"] = 1.23
|
t["123"] = 1.23
|
||||||
t.del("111")
|
t.del("111")
|
||||||
|
|
||||||
t["012"] = 67.9
|
t["012"] = 67.9
|
||||||
t["123"] = 1.5 # test overwriting
|
t["123"] = 1.5 # test overwriting
|
||||||
|
|
||||||
assert t["123"] == 1.5
|
assert t["123"] == 1.5
|
||||||
assert t["111"] == 0.0 # deleted
|
assert t["111"] == 0.0 # deleted
|
||||||
assert(not hasKey(t, "111"))
|
assert(not hasKey(t, "111"))
|
||||||
|
assert "123" in t
|
||||||
|
assert("111" notin t)
|
||||||
|
|
||||||
for key, val in items(data): t[key] = val.toFloat
|
for key, val in items(data): t[key] = val.toFloat
|
||||||
for key, val in items(data): assert t[key] == val.toFloat
|
for key, val in items(data): assert t[key] == val.toFloat
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -22,15 +22,15 @@ const
|
||||||
"---00": 346677844,
|
"---00": 346677844,
|
||||||
"0": 34404,
|
"0": 34404,
|
||||||
"1": 344004,
|
"1": 344004,
|
||||||
"10": 34484,
|
"10": 34484,
|
||||||
"11": 34474,
|
"11": 34474,
|
||||||
"12": 789,
|
"12": 789,
|
||||||
"19": 34464,
|
"19": 34464,
|
||||||
"2": 344774, "20": 34454,
|
"2": 344774, "20": 34454,
|
||||||
"3": 342244, "30": 34141244,
|
"3": 342244, "30": 34141244,
|
||||||
"34": 123456,
|
"34": 123456,
|
||||||
"4": 3412344, "40": 344114,
|
"4": 3412344, "40": 344114,
|
||||||
"5": 341232144, "50": 344490,
|
"5": 341232144, "50": 344490,
|
||||||
"6": 34214544, "60": 344491,
|
"6": 34214544, "60": 344491,
|
||||||
"7": 3434544, "70": 344492,
|
"7": 3434544, "70": 344492,
|
||||||
"8": 344544, "80": 344497,
|
"8": 344544, "80": 344497,
|
||||||
|
|
@ -46,7 +46,7 @@ block tableTest1:
|
||||||
for x in 0..1:
|
for x in 0..1:
|
||||||
for y in 0..1:
|
for y in 0..1:
|
||||||
assert t[(x,y)] == $x & $y
|
assert t[(x,y)] == $x & $y
|
||||||
assert($t ==
|
assert($t ==
|
||||||
"{(x: 0, y: 1): 01, (x: 0, y: 0): 00, (x: 1, y: 0): 10, (x: 1, y: 1): 11}")
|
"{(x: 0, y: 1): 01, (x: 0, y: 0): 00, (x: 1, y: 0): 10, (x: 1, y: 1): 11}")
|
||||||
|
|
||||||
block tableTest2:
|
block tableTest2:
|
||||||
|
|
@ -55,17 +55,19 @@ block tableTest2:
|
||||||
t["111"] = 1.000043
|
t["111"] = 1.000043
|
||||||
t["123"] = 1.23
|
t["123"] = 1.23
|
||||||
t.del("111")
|
t.del("111")
|
||||||
|
|
||||||
t["012"] = 67.9
|
t["012"] = 67.9
|
||||||
t["123"] = 1.5 # test overwriting
|
t["123"] = 1.5 # test overwriting
|
||||||
|
|
||||||
assert t["123"] == 1.5
|
assert t["123"] == 1.5
|
||||||
assert t["111"] == 0.0 # deleted
|
assert t["111"] == 0.0 # deleted
|
||||||
|
assert "123" in t
|
||||||
assert(not hasKey(t, "111"))
|
assert(not hasKey(t, "111"))
|
||||||
|
assert "111" notin t
|
||||||
|
|
||||||
for key, val in items(data): t[key] = val.toFloat
|
for key, val in items(data): t[key] = val.toFloat
|
||||||
for key, val in items(data): assert t[key] == val.toFloat
|
for key, val in items(data): assert t[key] == val.toFloat
|
||||||
|
|
||||||
|
|
||||||
block orderedTableTest1:
|
block orderedTableTest1:
|
||||||
var t = newOrderedTable[string, int](2)
|
var t = newOrderedTable[string, int](2)
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue