Fix HashSet::Add returning incorrect value

This commit is contained in:
Wojtek Figat
2025-09-02 21:41:54 +02:00
parent 1042ad4e7d
commit ad1163bccc
2 changed files with 5 additions and 3 deletions

View File

@@ -408,7 +408,7 @@ public:
template<typename ItemType>
bool Add(const ItemType& item)
{
Bucket* bucket = Base::OnAdd(item, false);
Bucket* bucket = Base::OnAdd(item, false, true);
if (bucket)
bucket->Occupy(item);
return bucket != nullptr;
@@ -421,7 +421,7 @@ public:
/// <returns>True if element has been added to the collection, otherwise false if the element is already present.</returns>
bool Add(T&& item)
{
Bucket* bucket = Base::OnAdd(item, false);
Bucket* bucket = Base::OnAdd(item, false, true);
if (bucket)
bucket->Occupy(MoveTemp(item));
return bucket != nullptr;

View File

@@ -359,7 +359,7 @@ protected:
}
template<typename KeyComparableType>
BucketType* OnAdd(const KeyComparableType& key, bool checkUnique = true)
BucketType* OnAdd(const KeyComparableType& key, bool checkUnique = true, bool nullIfNonUnique = false)
{
// Check if need to rehash elements (prevent many deleted elements that use too much of capacity)
if (_deletedCount * HASH_SET_DEFAULT_SLACK_SCALE > _size)
@@ -380,6 +380,8 @@ protected:
Platform::CheckFailed("That key has been already added to the collection.", __FILE__, __LINE__);
return nullptr;
}
if (nullIfNonUnique)
return nullptr;
return &_allocation.Get()[pos.ObjectIndex];
}