Skip to content

Commit

Permalink
Actually use quadratic probing in the TransformCache. Issue mmp#194.
Browse files Browse the repository at this point in the history
  • Loading branch information
mmp committed Oct 12, 2019
1 parent 8f7c841 commit a5a2eba
Showing 1 changed file with 8 additions and 12 deletions.
20 changes: 8 additions & 12 deletions src/core/api.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -323,16 +323,14 @@ void TransformCache::Insert(Transform *tNew) {
if (++hashTableOccupancy == hashTable.size() / 2)
Grow();

int offset = Hash(*tNew) & (hashTable.size() - 1);
int step = 1;
while (true) {
int baseOffset = Hash(*tNew) & (hashTable.size() - 1);
for (int nProbes = 0;; ++nProbes) {
// Quadratic probing.
int offset = (baseOffset + nProbes/2 + nProbes*nProbes/2) & (hashTable.size() - 1);
if (hashTable[offset] == nullptr) {
hashTable[offset] = tNew;
return;
}
// Advance using quadratic probing.
offset = (offset + step * step) & (hashTable.size() - 1);
++step;
}
}

Expand All @@ -344,16 +342,14 @@ void TransformCache::Grow() {
for (Transform *tEntry : hashTable) {
if (!tEntry) continue;

int offset = Hash(*tEntry) & (newTable.size() - 1);
int step = 1;
while (true) {
int baseOffset = Hash(*tEntry) & (hashTable.size() - 1);
for (int nProbes = 0;; ++nProbes) {
// Quadratic probing.
int offset = (baseOffset + nProbes/2 + nProbes*nProbes/2) & (hashTable.size() - 1);
if (newTable[offset] == nullptr) {
newTable[offset] = tEntry;
break;
}
// Advance using quadratic probing.
offset = (offset + step * step) & (hashTable.size() - 1);
++step;
}
}

Expand Down

0 comments on commit a5a2eba

Please sign in to comment.