Small update for packedsets (#17037)
Rename PTrunk to Trunk Use ord instead of cast[int]
This commit is contained in:
parent
260a5dacb7
commit
4886f8a02d
1 changed files with 11 additions and 12 deletions
|
|
@ -38,19 +38,18 @@ const
|
||||||
IntMask = 1 shl IntShift - 1
|
IntMask = 1 shl IntShift - 1
|
||||||
|
|
||||||
type
|
type
|
||||||
PTrunk = ref Trunk
|
Trunk = ref object
|
||||||
Trunk = object
|
next: Trunk # all nodes are connected with this pointer
|
||||||
next: PTrunk # all nodes are connected with this pointer
|
|
||||||
key: int # start address at bit 0
|
key: int # start address at bit 0
|
||||||
bits: array[0..IntsPerTrunk - 1, BitScalar] # a bit vector
|
bits: array[0..IntsPerTrunk - 1, BitScalar] # a bit vector
|
||||||
|
|
||||||
TrunkSeq = seq[PTrunk]
|
TrunkSeq = seq[Trunk]
|
||||||
|
|
||||||
PackedSet*[A: Ordinal] = object
|
PackedSet*[A: Ordinal] = object
|
||||||
## An efficient set of `Ordinal` types implemented as a sparse bit set.
|
## An efficient set of `Ordinal` types implemented as a sparse bit set.
|
||||||
elems: int # only valid for small numbers
|
elems: int # only valid for small numbers
|
||||||
counter, max: int
|
counter, max: int
|
||||||
head: PTrunk
|
head: Trunk
|
||||||
data: TrunkSeq
|
data: TrunkSeq
|
||||||
a: array[0..33, int] # profiling shows that 34 elements are enough
|
a: array[0..33, int] # profiling shows that 34 elements are enough
|
||||||
|
|
||||||
|
|
@ -65,7 +64,7 @@ proc nextTry(h, maxHash: Hash, perturb: var Hash): Hash {.inline.} =
|
||||||
perturb = cast[Hash](perturb2)
|
perturb = cast[Hash](perturb2)
|
||||||
result = ((5 * h) + 1 + perturb) and maxHash
|
result = ((5 * h) + 1 + perturb) and maxHash
|
||||||
|
|
||||||
proc packedSetGet[A](t: PackedSet[A], key: int): PTrunk =
|
proc packedSetGet[A](t: PackedSet[A], key: int): Trunk =
|
||||||
var h = key and t.max
|
var h = key and t.max
|
||||||
var perturb = key
|
var perturb = key
|
||||||
while t.data[h] != nil:
|
while t.data[h] != nil:
|
||||||
|
|
@ -74,7 +73,7 @@ proc packedSetGet[A](t: PackedSet[A], key: int): PTrunk =
|
||||||
h = nextTry(h, t.max, perturb)
|
h = nextTry(h, t.max, perturb)
|
||||||
result = nil
|
result = nil
|
||||||
|
|
||||||
proc intSetRawInsert[A](t: PackedSet[A], data: var TrunkSeq, desc: PTrunk) =
|
proc intSetRawInsert[A](t: PackedSet[A], data: var TrunkSeq, desc: Trunk) =
|
||||||
var h = desc.key and t.max
|
var h = desc.key and t.max
|
||||||
var perturb = desc.key
|
var perturb = desc.key
|
||||||
while data[h] != nil:
|
while data[h] != nil:
|
||||||
|
|
@ -92,7 +91,7 @@ proc intSetEnlarge[A](t: var PackedSet[A]) =
|
||||||
if t.data[i] != nil: intSetRawInsert(t, n, t.data[i])
|
if t.data[i] != nil: intSetRawInsert(t, n, t.data[i])
|
||||||
swap(t.data, n)
|
swap(t.data, n)
|
||||||
|
|
||||||
proc intSetPut[A](t: var PackedSet[A], key: int): PTrunk =
|
proc intSetPut[A](t: var PackedSet[A], key: int): Trunk =
|
||||||
var h = key and t.max
|
var h = key and t.max
|
||||||
var perturb = key
|
var perturb = key
|
||||||
while t.data[h] != nil:
|
while t.data[h] != nil:
|
||||||
|
|
@ -112,7 +111,7 @@ proc intSetPut[A](t: var PackedSet[A], key: int): PTrunk =
|
||||||
t.data[h] = result
|
t.data[h] = result
|
||||||
|
|
||||||
proc bitincl[A](s: var PackedSet[A], key: int) {.inline.} =
|
proc bitincl[A](s: var PackedSet[A], key: int) {.inline.} =
|
||||||
var ret: PTrunk
|
var ret: Trunk
|
||||||
var t = intSetPut(s, key shr TrunkShift)
|
var t = intSetPut(s, key shr TrunkShift)
|
||||||
var u = key and TrunkMask
|
var u = key and TrunkMask
|
||||||
t.bits[u shr IntShift] = t.bits[u shr IntShift] or
|
t.bits[u shr IntShift] = t.bits[u shr IntShift] or
|
||||||
|
|
@ -327,7 +326,7 @@ proc excl*[A](s: var PackedSet[A], key: A) =
|
||||||
a.excl(99)
|
a.excl(99)
|
||||||
assert len(a) == 0
|
assert len(a) == 0
|
||||||
|
|
||||||
exclImpl[A](s, cast[int](key))
|
exclImpl[A](s, ord(key))
|
||||||
|
|
||||||
proc excl*[A](s: var PackedSet[A], other: PackedSet[A]) =
|
proc excl*[A](s: var PackedSet[A], other: PackedSet[A]) =
|
||||||
## Excludes all elements from `other` from `s`.
|
## Excludes all elements from `other` from `s`.
|
||||||
|
|
@ -379,7 +378,7 @@ proc missingOrExcl*[A](s: var PackedSet[A], key: A): bool =
|
||||||
assert a.missingOrExcl(5) == true
|
assert a.missingOrExcl(5) == true
|
||||||
|
|
||||||
var count = s.len
|
var count = s.len
|
||||||
exclImpl(s, cast[int](key))
|
exclImpl(s, ord(key))
|
||||||
result = count == s.len
|
result = count == s.len
|
||||||
|
|
||||||
proc clear*[A](result: var PackedSet[A]) =
|
proc clear*[A](result: var PackedSet[A]) =
|
||||||
|
|
@ -441,7 +440,7 @@ proc assign*[A](dest: var PackedSet[A], src: PackedSet[A]) =
|
||||||
var perturb = it.key
|
var perturb = it.key
|
||||||
while dest.data[h] != nil: h = nextTry(h, dest.max, perturb)
|
while dest.data[h] != nil: h = nextTry(h, dest.max, perturb)
|
||||||
assert dest.data[h] == nil
|
assert dest.data[h] == nil
|
||||||
var n: PTrunk
|
var n: Trunk
|
||||||
new(n)
|
new(n)
|
||||||
n.next = dest.head
|
n.next = dest.head
|
||||||
n.key = it.key
|
n.key = it.key
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue