hashtables: 2nd version
This commit is contained in:
parent
d1b766cec0
commit
c3b16311dd
1 changed files with 31 additions and 29 deletions
|
|
@ -11,12 +11,13 @@
|
||||||
## a mapping from keys to values.
|
## a mapping from keys to values.
|
||||||
|
|
||||||
import
|
import
|
||||||
os, hashes, strutils
|
os, hashes, math
|
||||||
|
|
||||||
type
|
type
|
||||||
TKeyValuePair[A, B] = tuple[key: A, val: B]
|
TSlotEnum = enum seEmpty, seFilled, seDeleted
|
||||||
|
TKeyValuePair[A, B] = tuple[slot: TSlotEnum, key: A, val: B]
|
||||||
TKeyValuePairSeq[A, B] = seq[TKeyValuePair[A, B]]
|
TKeyValuePairSeq[A, B] = seq[TKeyValuePair[A, B]]
|
||||||
THashTable*[A, B] = object of TObject
|
THashTable[A, B] = object of TObject
|
||||||
counter: int
|
counter: int
|
||||||
data: TKeyValuePairSeq[A, B]
|
data: TKeyValuePairSeq[A, B]
|
||||||
|
|
||||||
|
|
@ -29,20 +30,22 @@ proc len*[A, B](t: PHashTable[A, B]): int =
|
||||||
iterator pairs*[A, B](t: PHashTable[A, B]): tuple[key: A, val: B] =
|
iterator pairs*[A, B](t: PHashTable[A, B]): tuple[key: A, val: B] =
|
||||||
## iterates over any (key, value) pair in the table `t`.
|
## iterates over any (key, value) pair in the table `t`.
|
||||||
for h in 0..high(t.data):
|
for h in 0..high(t.data):
|
||||||
if not isNil(t.data[h].key) and not isNil(t.data[h].val):
|
if t.data[h].slot == seFilled: yield (t.data[h].key, t.data[h].val)
|
||||||
yield (t.data[h].key, t.data[h].val)
|
|
||||||
|
iterator keys*[A, B](t: PHashTable[A, B]): tuple[key: A, val: B] =
|
||||||
|
## iterates over any key in the table `t`.
|
||||||
|
for h in 0..high(t.data):
|
||||||
|
if t.data[h].slot == seFilled: yield t.data[h].key
|
||||||
|
|
||||||
|
iterator values*[A, B](t: PHashTable[A, B]): tuple[key: A, val: B] =
|
||||||
|
## iterates over any value in the table `t`.
|
||||||
|
for h in 0..high(t.data):
|
||||||
|
if t.data[h].slot == seFilled: yield t.data[h].val
|
||||||
|
|
||||||
const
|
const
|
||||||
growthFactor = 2
|
growthFactor = 2
|
||||||
startSize = 64
|
|
||||||
|
|
||||||
proc myhash[A](key: A): THash =
|
proc mustRehash(length, counter: int): bool {.inline.} =
|
||||||
result = hashes.hash(key)
|
|
||||||
|
|
||||||
proc myCmp[A](key: A, key2: A): bool =
|
|
||||||
result = cmp(key, key2) == 0
|
|
||||||
|
|
||||||
proc mustRehash(length, counter: int): bool =
|
|
||||||
assert(length > counter)
|
assert(length > counter)
|
||||||
result = (length * 2 < counter * 3) or (length - counter < 4)
|
result = (length * 2 < counter * 3) or (length - counter < 4)
|
||||||
|
|
||||||
|
|
@ -50,9 +53,9 @@ proc nextTry(h, maxHash: THash): THash {.inline.} =
|
||||||
result = ((5 * h) + 1) and maxHash
|
result = ((5 * h) + 1) and maxHash
|
||||||
|
|
||||||
proc RawGet[A, B](t: PHashTable[A, B], key: A): int =
|
proc RawGet[A, B](t: PHashTable[A, B], key: A): int =
|
||||||
var h: THash = myhash(key) and high(t.data) # start with real hash value
|
var h: THash = hash(key) and high(t.data) # start with real hash value
|
||||||
while not isNil(t.data[h].key) and not isNil(t.data[h].val):
|
while t.data[h].slot != seEmpty:
|
||||||
if mycmp(t.data[h].key, key):
|
if t.data[h].key == key and t.data[h].slot == seFilled:
|
||||||
return h
|
return h
|
||||||
h = nextTry(h, high(t.data))
|
h = nextTry(h, high(t.data))
|
||||||
result = -1
|
result = -1
|
||||||
|
|
@ -71,17 +74,18 @@ proc hasKey*[A, B](t: PHashTable[A, B], key: A): bool =
|
||||||
|
|
||||||
proc RawInsert[A, B](t: PHashTable[A, B], data: var TKeyValuePairSeq[A, B],
|
proc RawInsert[A, B](t: PHashTable[A, B], data: var TKeyValuePairSeq[A, B],
|
||||||
key: A, val: B) =
|
key: A, val: B) =
|
||||||
var h: THash = myhash(key) and high(data)
|
var h: THash = hash(key) and high(data)
|
||||||
while not isNil(data[h].key):
|
while data[h].slot == seFilled:
|
||||||
h = nextTry(h, high(data))
|
h = nextTry(h, high(data))
|
||||||
data[h].key = key
|
data[h].key = key
|
||||||
data[h].val = val
|
data[h].val = val
|
||||||
|
data[h].slot = seFilled
|
||||||
|
|
||||||
proc Enlarge[A, B](t: PHashTable[A, B]) =
|
proc Enlarge[A, B](t: PHashTable[A, B]) =
|
||||||
var n: TKeyValuePairSeq[A, B]
|
var n: TKeyValuePairSeq[A, B]
|
||||||
newSeq(n, len(t.data) * growthFactor)
|
newSeq(n, len(t.data) * growthFactor)
|
||||||
for i in countup(0, high(t.data)):
|
for i in countup(0, high(t.data)):
|
||||||
if not isNil(t.data[i].key): RawInsert(t, n, t.data[i].key, t.data[i].val)
|
if t.data[i].slot == seFilled: RawInsert(t, n, t.data[i].key, t.data[i].val)
|
||||||
swap(t.data, n)
|
swap(t.data, n)
|
||||||
|
|
||||||
proc `[]=`*[A, B](t: PHashTable[A, B], key: A, val: B) =
|
proc `[]=`*[A, B](t: PHashTable[A, B], key: A, val: B) =
|
||||||
|
|
@ -94,21 +98,19 @@ proc `[]=`*[A, B](t: PHashTable[A, B], key: A, val: B) =
|
||||||
RawInsert(t, t.data, key, val)
|
RawInsert(t, t.data, key, val)
|
||||||
inc(t.counter)
|
inc(t.counter)
|
||||||
|
|
||||||
proc default[T](): T = nil
|
|
||||||
|
|
||||||
proc del*[A, B](t: PHashTable[A, B], key: A) =
|
proc del*[A, B](t: PHashTable[A, B], key: A) =
|
||||||
## deletes `key` from hash table `t`.
|
## deletes `key` from hash table `t`.
|
||||||
var index = RawGet(t, key)
|
var index = RawGet(t, key)
|
||||||
if index >= 0:
|
if index >= 0:
|
||||||
t.data[index].key = default[A]()
|
t.data[index].slot = seDeleted
|
||||||
else:
|
|
||||||
raise newException(EInvalidIndex, "Key not found.")
|
|
||||||
|
|
||||||
proc newHashTable*[A, B](): PHashTable[A, B] =
|
proc newHashTable*[A, B](initialSize = 64): PHashTable[A, B] =
|
||||||
## creates a new string table that is empty.
|
## creates a new string table that is empty. `initialSize` needs to be
|
||||||
|
## a power of two.
|
||||||
|
assert isPowerOfTwo(initialSize)
|
||||||
new(result)
|
new(result)
|
||||||
result.counter = 0
|
result.counter = 0
|
||||||
newSeq(result.data, startSize)
|
newSeq(result.data, initialSize)
|
||||||
|
|
||||||
proc `$`*[A, B](t: PHashTable[A, B]): string =
|
proc `$`*[A, B](t: PHashTable[A, B]): string =
|
||||||
## The `$` operator for string tables.
|
## The `$` operator for string tables.
|
||||||
|
|
@ -130,8 +132,8 @@ when isMainModule:
|
||||||
echo table
|
echo table
|
||||||
table.del("111")
|
table.del("111")
|
||||||
echo table
|
echo table
|
||||||
echo repr(table["111"])
|
#echo repr(table["111"])
|
||||||
echo(repr(table["1212"]))
|
#echo(repr(table["1212"]))
|
||||||
table["111"] = 1.5
|
table["111"] = 1.5
|
||||||
table["011"] = 67.9
|
table["011"] = 67.9
|
||||||
echo table
|
echo table
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue