mirror of
https://github.com/beefytech/Beef.git
synced 2025-06-13 05:44:11 +02:00
Preserve equal-hash order during rehash
This commit is contained in:
parent
5077876ef7
commit
3da1881c9b
1 changed files with 20 additions and 7 deletions
|
@ -164,17 +164,30 @@ public:
|
||||||
{
|
{
|
||||||
auto newHashHeads = (Entry**)TFuncs::AllocateZero(sizeof(Entry*) * newHashSize, alignof(Entry*));
|
auto newHashHeads = (Entry**)TFuncs::AllocateZero(sizeof(Entry*) * newHashSize, alignof(Entry*));
|
||||||
|
|
||||||
|
SizedArray<Entry*, 32> entryList;
|
||||||
|
|
||||||
for (int hashIdx = 0; hashIdx < mHashSize; hashIdx++)
|
for (int hashIdx = 0; hashIdx < mHashSize; hashIdx++)
|
||||||
{
|
{
|
||||||
Entry* checkEntry = mHashHeads[hashIdx];
|
Entry* checkEntry = mHashHeads[hashIdx];
|
||||||
|
if (checkEntry != NULL)
|
||||||
|
{
|
||||||
|
// We want to keep elements with equal hashes in their insert order so we need to
|
||||||
|
// iterate through the linked list in reverse
|
||||||
|
entryList.Clear();
|
||||||
|
|
||||||
while (checkEntry != NULL)
|
while (checkEntry != NULL)
|
||||||
{
|
{
|
||||||
auto nextEntry = checkEntry->mNext;
|
entryList.Add(checkEntry);
|
||||||
|
checkEntry = checkEntry->mNext;
|
||||||
|
}
|
||||||
|
|
||||||
|
for (int i = (int)entryList.mSize - 1; i >= 0; i--)
|
||||||
|
{
|
||||||
|
auto checkEntry = entryList[i];
|
||||||
int newHashIdx = (checkEntry->mHash & 0x7FFFFFFF) % newHashSize;
|
int newHashIdx = (checkEntry->mHash & 0x7FFFFFFF) % newHashSize;
|
||||||
checkEntry->mNext = newHashHeads[newHashIdx];
|
checkEntry->mNext = newHashHeads[newHashIdx];
|
||||||
newHashHeads[newHashIdx] = checkEntry;
|
newHashHeads[newHashIdx] = checkEntry;
|
||||||
|
}
|
||||||
checkEntry = nextEntry;
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue