diff --git a/Source/Engine/Core/Collections/Config.h b/Source/Engine/Core/Collections/Config.h deleted file mode 100644 index c0346319b..000000000 --- a/Source/Engine/Core/Collections/Config.h +++ /dev/null @@ -1,27 +0,0 @@ -// Copyright (c) 2012-2024 Wojciech Figat. All rights reserved. - -#pragma once - -#include "Engine/Platform/Defines.h" - -/// -/// Default capacity for the dictionaries (amount of space for the elements). -/// -#ifndef DICTIONARY_DEFAULT_CAPACITY -#if PLATFORM_DESKTOP -#define DICTIONARY_DEFAULT_CAPACITY 256 -#else -#define DICTIONARY_DEFAULT_CAPACITY 64 -#endif -#endif - -/// -/// Default slack space divider for the dictionaries. -/// -#define DICTIONARY_DEFAULT_SLACK_SCALE 3 - -/// -/// Function for dictionary that tells how change hash index during iteration (size param is a buckets table size). -/// -#define DICTIONARY_PROB_FUNC(size, numChecks) (numChecks) -//#define DICTIONARY_PROB_FUNC(size, numChecks) (1) diff --git a/Source/Engine/Core/Collections/HashSetBase.h b/Source/Engine/Core/Collections/HashSetBase.h index 0ad2d7035..4b28016af 100644 --- a/Source/Engine/Core/Collections/HashSetBase.h +++ b/Source/Engine/Core/Collections/HashSetBase.h @@ -6,7 +6,35 @@ #include "Engine/Core/Memory/Allocation.h" #include "Engine/Core/Memory/AllocationUtils.h" #include "Engine/Core/Collections/HashFunctions.h" -#include "Engine/Core/Collections/Config.h" + +/// +/// Default capacity for the hash set collections (minimum initial amount of space for the elements). +/// +#ifndef HASH_SET_DEFAULT_CAPACITY +#if PLATFORM_DESKTOP +#define HASH_SET_DEFAULT_CAPACITY 256 +#else +#define HASH_SET_DEFAULT_CAPACITY 64 +#endif +#endif + +/// +/// Default slack space divider for the hash sets. +/// +#define HASH_SET_DEFAULT_SLACK_SCALE 3 + +/// +/// Function for hash set that tells how change hash index during iteration (size param is a buckets table size). +/// +#define HASH_SET_PROB_FUNC(size, numChecks) (numChecks) +//#define HASH_SET_PROB_FUNC(size, numChecks) (1) + +// [Deprecated in v1.10] Use HASH_SET_DEFAULT_CAPACITY +#define DICTIONARY_DEFAULT_CAPACITY HASH_SET_DEFAULT_CAPACITY +// [Deprecated in v1.10] Use HASH_SET_DEFAULT_SLACK_SCALE +#define DICTIONARY_DEFAULT_SLACK_SCALE HASH_SET_DEFAULT_SLACK_SCALE +// [Deprecated in v1.10] Use HASH_SET_PROB_FUNC +#define DICTIONARY_PROB_FUNC(size, numChecks) (numChecks) HASH_SET_PROB_FUNC(size, numChecks) /// /// Tells if the object is occupied, and if not, if the bucket is a subject of compaction. @@ -162,12 +190,12 @@ public: /// True if preserve collection data when changing its size, otherwise collection after resize will be empty. void EnsureCapacity(int32 minCapacity, const bool preserveContents = true) { - minCapacity *= DICTIONARY_DEFAULT_SLACK_SCALE; + minCapacity *= HASH_SET_DEFAULT_SLACK_SCALE; if (_size >= minCapacity) return; int32 capacity = _allocation.CalculateCapacityGrow(_size, minCapacity); - if (capacity < DICTIONARY_DEFAULT_CAPACITY) - capacity = DICTIONARY_DEFAULT_CAPACITY; + if (capacity < HASH_SET_DEFAULT_CAPACITY) + capacity = HASH_SET_DEFAULT_CAPACITY; SetCapacity(capacity, preserveContents); } @@ -324,7 +352,7 @@ protected: // Move to the next bucket checksCount++; - bucketIndex = (bucketIndex + DICTIONARY_PROB_FUNC(_size, checksCount)) & tableSizeMinusOne; + bucketIndex = (bucketIndex + HASH_SET_PROB_FUNC(_size, checksCount)) & tableSizeMinusOne; } result.ObjectIndex = -1; result.FreeSlotIndex = insertPos; @@ -334,11 +362,11 @@ protected: BucketType* OnAdd(const KeyComparableType& key, bool checkUnique = true) { // Check if need to rehash elements (prevent many deleted elements that use too much of capacity) - if (_deletedCount > _size / DICTIONARY_DEFAULT_SLACK_SCALE) + if (_deletedCount > _size / HASH_SET_DEFAULT_SLACK_SCALE) Compact(); // Ensure to have enough memory for the next item (in case of new element insertion) - EnsureCapacity(((_elementsCount + 1) * DICTIONARY_DEFAULT_SLACK_SCALE + _deletedCount) / DICTIONARY_DEFAULT_SLACK_SCALE); + EnsureCapacity(((_elementsCount + 1) * HASH_SET_DEFAULT_SLACK_SCALE + _deletedCount) / HASH_SET_DEFAULT_SLACK_SCALE); // Find location of the item or place to insert it FindPositionResult pos; diff --git a/Source/Engine/Tests/TestCollections.cpp b/Source/Engine/Tests/TestCollections.cpp index f9b0e47ca..522c3008d 100644 --- a/Source/Engine/Tests/TestCollections.cpp +++ b/Source/Engine/Tests/TestCollections.cpp @@ -185,8 +185,8 @@ TEST_CASE("HashSet") SECTION("Test Allocators") { HashSet a1; - HashSet> a2; - HashSet> a3; + HashSet> a2; + HashSet> a3; for (int32 i = 0; i < 7; i++) { a1.Add(i); @@ -236,7 +236,7 @@ TEST_CASE("HashSet") { HashSet a1; a1.Add(1); - CHECK(a1.Capacity() <= DICTIONARY_DEFAULT_CAPACITY); + CHECK(a1.Capacity() <= HASH_SET_DEFAULT_CAPACITY); } SECTION("Test Add/Remove") @@ -248,7 +248,7 @@ TEST_CASE("HashSet") a1.Remove(i); } CHECK(a1.Count() == 0); - CHECK(a1.Capacity() <= DICTIONARY_DEFAULT_CAPACITY); + CHECK(a1.Capacity() <= HASH_SET_DEFAULT_CAPACITY); a1.Clear(); for (int32 i = 1; i <= 10; i++) a1.Add(-i); @@ -258,7 +258,7 @@ TEST_CASE("HashSet") a1.Remove(i); } CHECK(a1.Count() == 10); - CHECK(a1.Capacity() <= DICTIONARY_DEFAULT_CAPACITY); + CHECK(a1.Capacity() <= HASH_SET_DEFAULT_CAPACITY); } } @@ -267,8 +267,8 @@ TEST_CASE("Dictionary") SECTION("Test Allocators") { Dictionary a1; - Dictionary> a2; - Dictionary> a3; + Dictionary> a2; + Dictionary> a3; for (int32 i = 0; i < 7; i++) { a1.Add(i, i); @@ -322,7 +322,7 @@ TEST_CASE("Dictionary") { Dictionary a1; a1.Add(1, 1); - CHECK(a1.Capacity() <= DICTIONARY_DEFAULT_CAPACITY); + CHECK(a1.Capacity() <= HASH_SET_DEFAULT_CAPACITY); } SECTION("Test Add/Remove") @@ -334,7 +334,7 @@ TEST_CASE("Dictionary") a1.Remove(i); } CHECK(a1.Count() == 0); - CHECK(a1.Capacity() <= DICTIONARY_DEFAULT_CAPACITY); + CHECK(a1.Capacity() <= HASH_SET_DEFAULT_CAPACITY); a1.Clear(); for (int32 i = 1; i <= 10; i++) a1.Add(-i, -i); @@ -344,6 +344,6 @@ TEST_CASE("Dictionary") a1.Remove(i); } CHECK(a1.Count() == 10); - CHECK(a1.Capacity() <= DICTIONARY_DEFAULT_CAPACITY); + CHECK(a1.Capacity() <= HASH_SET_DEFAULT_CAPACITY); } }