Merge pull request #7242 from konqoro/patch-23
Fix toCountTable and newCountTable
This commit is contained in:
commit
0cf207a452
1 changed files with 20 additions and 21 deletions
|
|
@ -955,6 +955,17 @@ proc `[]=`*[A](t: var CountTable[A], key: A, val: int) =
|
||||||
#t.data[h].key = key
|
#t.data[h].key = key
|
||||||
#t.data[h].val = val
|
#t.data[h].val = val
|
||||||
|
|
||||||
|
proc inc*[A](t: var CountTable[A], key: A, val = 1) =
|
||||||
|
## increments `t[key]` by `val`.
|
||||||
|
var index = rawGet(t, key)
|
||||||
|
if index >= 0:
|
||||||
|
inc(t.data[index].val, val)
|
||||||
|
if t.data[index].val == 0: dec(t.counter)
|
||||||
|
else:
|
||||||
|
if mustRehash(len(t.data), t.counter): enlarge(t)
|
||||||
|
rawInsert(t, t.data, key, val)
|
||||||
|
inc(t.counter)
|
||||||
|
|
||||||
proc initCountTable*[A](initialSize=64): CountTable[A] =
|
proc initCountTable*[A](initialSize=64): CountTable[A] =
|
||||||
## creates a new count table that is empty.
|
## creates a new count table that is empty.
|
||||||
##
|
##
|
||||||
|
|
@ -969,7 +980,7 @@ proc toCountTable*[A](keys: openArray[A]): CountTable[A] =
|
||||||
## creates a new count table with every key in `keys` having a count
|
## creates a new count table with every key in `keys` having a count
|
||||||
## of how many times it occurs in `keys`.
|
## of how many times it occurs in `keys`.
|
||||||
result = initCountTable[A](rightSize(keys.len))
|
result = initCountTable[A](rightSize(keys.len))
|
||||||
for key in items(keys): result.inc key
|
for key in items(keys): result.inc(key)
|
||||||
|
|
||||||
proc `$`*[A](t: CountTable[A]): string =
|
proc `$`*[A](t: CountTable[A]): string =
|
||||||
## The `$` operator for count tables.
|
## The `$` operator for count tables.
|
||||||
|
|
@ -980,17 +991,6 @@ proc `==`*[A](s, t: CountTable[A]): bool =
|
||||||
## contain the same keys with the same count. Insert order does not matter.
|
## contain the same keys with the same count. Insert order does not matter.
|
||||||
equalsImpl(s, t)
|
equalsImpl(s, t)
|
||||||
|
|
||||||
proc inc*[A](t: var CountTable[A], key: A, val = 1) =
|
|
||||||
## increments `t[key]` by `val`.
|
|
||||||
var index = rawGet(t, key)
|
|
||||||
if index >= 0:
|
|
||||||
inc(t.data[index].val, val)
|
|
||||||
if t.data[index].val == 0: dec(t.counter)
|
|
||||||
else:
|
|
||||||
if mustRehash(len(t.data), t.counter): enlarge(t)
|
|
||||||
rawInsert(t, t.data, key, val)
|
|
||||||
inc(t.counter)
|
|
||||||
|
|
||||||
proc smallest*[A](t: CountTable[A]): tuple[key: A, val: int] =
|
proc smallest*[A](t: CountTable[A]): tuple[key: A, val: int] =
|
||||||
## returns the (key,val)-pair with the smallest `val`. Efficiency: O(n)
|
## returns the (key,val)-pair with the smallest `val`. Efficiency: O(n)
|
||||||
assert t.len > 0
|
assert t.len > 0
|
||||||
|
|
@ -1088,6 +1088,10 @@ proc `[]=`*[A](t: CountTableRef[A], key: A, val: int) =
|
||||||
assert val > 0
|
assert val > 0
|
||||||
t[][key] = val
|
t[][key] = val
|
||||||
|
|
||||||
|
proc inc*[A](t: CountTableRef[A], key: A, val = 1) =
|
||||||
|
## increments `t[key]` by `val`.
|
||||||
|
t[].inc(key, val)
|
||||||
|
|
||||||
proc newCountTable*[A](initialSize=64): CountTableRef[A] =
|
proc newCountTable*[A](initialSize=64): CountTableRef[A] =
|
||||||
## creates a new count table that is empty.
|
## creates a new count table that is empty.
|
||||||
##
|
##
|
||||||
|
|
@ -1098,9 +1102,10 @@ proc newCountTable*[A](initialSize=64): CountTableRef[A] =
|
||||||
result[] = initCountTable[A](initialSize)
|
result[] = initCountTable[A](initialSize)
|
||||||
|
|
||||||
proc newCountTable*[A](keys: openArray[A]): CountTableRef[A] =
|
proc newCountTable*[A](keys: openArray[A]): CountTableRef[A] =
|
||||||
## creates a new count table with every key in `keys` having a count of 1.
|
## creates a new count table with every key in `keys` having a count
|
||||||
|
## of how many times it occurs in `keys`.
|
||||||
result = newCountTable[A](rightSize(keys.len))
|
result = newCountTable[A](rightSize(keys.len))
|
||||||
for key in items(keys): result[key] = 1
|
for key in items(keys): result.inc(key)
|
||||||
|
|
||||||
proc `$`*[A](t: CountTableRef[A]): string =
|
proc `$`*[A](t: CountTableRef[A]): string =
|
||||||
## The `$` operator for count tables.
|
## The `$` operator for count tables.
|
||||||
|
|
@ -1114,10 +1119,6 @@ proc `==`*[A](s, t: CountTableRef[A]): bool =
|
||||||
elif isNil(t): result = false
|
elif isNil(t): result = false
|
||||||
else: result = s[] == t[]
|
else: result = s[] == t[]
|
||||||
|
|
||||||
proc inc*[A](t: CountTableRef[A], key: A, val = 1) =
|
|
||||||
## increments `t[key]` by `val`.
|
|
||||||
t[].inc(key, val)
|
|
||||||
|
|
||||||
proc smallest*[A](t: CountTableRef[A]): (A, int) =
|
proc smallest*[A](t: CountTableRef[A]): (A, int) =
|
||||||
## returns the (key,val)-pair with the smallest `val`. Efficiency: O(n)
|
## returns the (key,val)-pair with the smallest `val`. Efficiency: O(n)
|
||||||
t[].smallest
|
t[].smallest
|
||||||
|
|
@ -1316,7 +1317,6 @@ when isMainModule:
|
||||||
assert a == b
|
assert a == b
|
||||||
assert a == c
|
assert a == c
|
||||||
|
|
||||||
|
|
||||||
block: #6250
|
block: #6250
|
||||||
let
|
let
|
||||||
a = {3: 1}.toOrderedTable
|
a = {3: 1}.toOrderedTable
|
||||||
|
|
@ -1332,6 +1332,5 @@ when isMainModule:
|
||||||
assert((b == a) == true)
|
assert((b == a) == true)
|
||||||
|
|
||||||
block: # CountTable.smallest
|
block: # CountTable.smallest
|
||||||
var t = initCountTable[int]()
|
let t = toCountTable([0, 0, 5, 5, 5])
|
||||||
for v in items([0, 0, 5, 5, 5]): t.inc(v)
|
|
||||||
doAssert t.smallest == (0, 2)
|
doAssert t.smallest == (0, 2)
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue