Merge pull request #4463 from flyx/tables-equals-no-KeyError

`==` in tables should not raise KeyError
This commit is contained in:
Andreas Rumpf 2016-07-19 00:16:58 +02:00 • committed by GitHub
commit 358f582939

View file

@ -331,18 +331,17 @@ 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)
template equalsImpl() = template equalsImpl(t) =
if s.counter == t.counter: if s.counter == t.counter:
# different insertion orders mean different 'data' seqs, so we have # different insertion orders mean different 'data' seqs, so we have
# to use the slow route here: # to use the slow route here:
for key, val in s: for key, val in s:
# prefix notation leads to automatic dereference in case of PTable
if not t.hasKey(key): return false if not t.hasKey(key): return false
if t[key] != val: return false if t.getOrDefault(key) != val: return false
return true return true
proc `==`*[A, B](s, t: Table[A, B]): bool = proc `==`*[A, B](s, t: Table[A, B]): bool =
equalsImpl() equalsImpl(t)
proc indexBy*[A, B, C](collection: A, index: proc(x: B): C): Table[C, B] = proc indexBy*[A, B, C](collection: A, index: proc(x: B): C): Table[C, B] =
## Index the collection with the proc provided. ## Index the collection with the proc provided.
@ -432,7 +431,7 @@ proc `$`*[A, B](t: TableRef[A, B]): string =
proc `==`*[A, B](s, t: TableRef[A, B]): bool = proc `==`*[A, B](s, t: TableRef[A, B]): bool =
if isNil(s): result = isNil(t) if isNil(s): result = isNil(t)
elif isNil(t): result = false elif isNil(t): result = false
else: equalsImpl() else: equalsImpl(t[])
proc newTableFrom*[A, B, C](collection: A, index: proc(x: B): C): TableRef[C, B] = proc newTableFrom*[A, B, C](collection: A, index: proc(x: B): C): TableRef[C, B] =
## Index the collection with the proc provided. ## Index the collection with the proc provided.