Skip to content

Commit 7bad876

Browse files
committed
Fix problem with MinPerfHash test
Invalid integer conversions were applied.
1 parent 567c374 commit 7bad876

File tree

1 file changed

+21
-15
lines changed

1 file changed

+21
-15
lines changed

tests/Examples/MinPerfHash.spr

+21-15
Original file line numberDiff line numberDiff line change
@@ -37,15 +37,21 @@ package _Impl
3737

3838
[ct] let hashSeed: UInt32 = -1
3939

40-
/// FNV hash generator
41-
fun doHash(key: StringRef, start: UInt32): UInt32
42-
var hash = start
43-
for i = 0..key.size()
44-
hash = (hash !^! UInt32(Int8(key(i)))) * 0x01000193U;
45-
//cout << "Hash(" << key << ", " << start << ")=" << hash << "\n";
46-
return hash;
47-
fun doHash(value, start: UInt32): UInt32
48-
return (start !^! value) * 0x01000193U;
40+
/// FNV hash generator
41+
fun _doHash(key: StringRef, start: UInt32): UInt32
42+
var hash = start
43+
for i = 0..key.size()
44+
hash = (hash !^! UInt32(Int8(key(i)))) * 0x01000193U;
45+
//cout << "Hash(" << key << ", " << start << ")=" << hash << "\n";
46+
return hash;
47+
fun _doHash(value, start: UInt32): UInt32
48+
return (start !^! value) * 0x01000193U;
49+
50+
fun _hashMod(key: StringRef, start: UInt32, numBuckets: Int): Int
51+
return Int(_doHash(key, start) % UInt32(numBuckets))
52+
53+
fun _hashMod(value, start: UInt32, numBuckets: Int): Int
54+
return Int(_doHash(value, start) % UInt32(numBuckets))
4955

5056
datatype MinPerfHash
5157
displacements: Array(Int)
@@ -71,7 +77,7 @@ fun ctor(this: !MinPerfHash, keys: !Vector(String), extraSpace: Float)
7177
// Split the keys into buckets
7278
for key: @AnyType = keys.all()
7379
//cout << "Key: " << key.asStringRef() << "\n";
74-
let pos = Int(_Impl.doHash(key.asStringRef(), _Impl.hashSeed)) % numBuckets
80+
let pos = _hashMod(key.asStringRef(), _Impl.hashSeed, numBuckets)
7581
keysToBuckets(pos).pushBack(key.asStringRef())
7682

7783
// Sort the buckets in failing order according to their size
@@ -95,7 +101,7 @@ fun ctor(this: !MinPerfHash, keys: !Vector(String), extraSpace: Float)
95101
// keys in this bucket without conflicts with keys from previous buckets.
96102
var item = 0
97103
while item < bucket.size()
98-
let slot: Int = Int(_Impl.doHash(bucket(item), UInt32(d))) % numValues
104+
let slot = _hashMod(bucket(item), UInt32(d), numValues)
99105
let slotAlreadyUsed = !find(slots.all(), slot).isEmpty()
100106
++numOp
101107
if ( placedKeys.testBit(slot) || slotAlreadyUsed )
@@ -113,7 +119,7 @@ fun ctor(this: !MinPerfHash, keys: !Vector(String), extraSpace: Float)
113119
// This is guaranteed to finish quickly
114120

115121
// We found a displacement that works for the current bucket (it doesn't yield overlaps with previous buckets)
116-
displacements(Int(_Impl.doHash(bucket(0), _Impl.hashSeed)) % numBuckets) = d
122+
displacements(_hashMod(bucket(0), _Impl.hashSeed, numBuckets)) = d
117123
for i=0..bucket.size()
118124
placedKeys.setBit(slots(i));
119125

@@ -130,7 +136,7 @@ fun ctor(this: !MinPerfHash, keys: !Vector(String), extraSpace: Float)
130136
++emptySlot;
131137

132138
// We subtract one to ensure it's negative even if the zero-th slot was used.
133-
displacements(Int(_Impl.doHash(bucket(0), _Impl.hashSeed)) % numBuckets) = -emptySlot-1
139+
displacements(_hashMod(bucket(0), _Impl.hashSeed, numBuckets)) = -emptySlot-1
134140
placedKeys.setBit(emptySlot)
135141

136142
//_dumpBuckets("buckets final, ", keysToBuckets);
@@ -146,9 +152,9 @@ fun ctorFromCt(this: !MinPerfHash, src: MinPerfHash ct)
146152

147153
fun search(this: !MinPerfHash, key: StringRef): Int
148154
// Get the displacement from the table...
149-
let d = displacements(Int(_Impl.doHash(key, _Impl.hashSeed)) % displacements.size())
155+
let d = displacements(_hashMod(key, _Impl.hashSeed, displacements.size()))
150156
// If the displacement is negative, directly return; otherwise compute the final hash
151-
return ife(d<0, Int(-d-1), Int(_Impl.doHash(key, UInt32(d))) % numValues)
157+
return ife(d<0, Int(-d-1), _hashMod(key, UInt32(d), numValues))
152158

153159
fun ()(this: !MinPerfHash, key: StringRef) = this.search(key)
154160

0 commit comments

Comments
 (0)