mirror of
https://github.com/beefytech/Beef.git
synced 2025-06-10 12:32:20 +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*));
|
||||
|
||||
SizedArray<Entry*, 32> entryList;
|
||||
|
||||
for (int hashIdx = 0; hashIdx < mHashSize; hashIdx++)
|
||||
{
|
||||
Entry* checkEntry = mHashHeads[hashIdx];
|
||||
while (checkEntry != NULL)
|
||||
if (checkEntry != NULL)
|
||||
{
|
||||
auto nextEntry = checkEntry->mNext;
|
||||
int newHashIdx = (checkEntry->mHash & 0x7FFFFFFF) % newHashSize;
|
||||
checkEntry->mNext = newHashHeads[newHashIdx];
|
||||
newHashHeads[newHashIdx] = checkEntry;
|
||||
|
||||
checkEntry = nextEntry;
|
||||
// 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)
|
||||
{
|
||||
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;
|
||||
checkEntry->mNext = newHashHeads[newHashIdx];
|
||||
newHashHeads[newHashIdx] = checkEntry;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue