Compare commits
1 Commits
main
...
2a2c435b88
| Author | SHA1 | Date | |
|---|---|---|---|
| 2a2c435b88 |
28
headers/Containers/ContainerOptions.hpp
Normal file
28
headers/Containers/ContainerOptions.hpp
Normal file
@@ -0,0 +1,28 @@
|
||||
/*
|
||||
* SPDX-FileCopyrightText: Dora "cat" <cat@thenight.club>
|
||||
* SPDX-License-Identifier: MPL-2.0
|
||||
*
|
||||
* This Source Code Form is subject to the terms of the Mozilla Public License,
|
||||
* v. 2.0. If a copy of the MPL was not distributed with this file, You can
|
||||
* obtain one at http://mozilla.org/MPL/2.0/.
|
||||
*/
|
||||
#ifndef GUARD_TOURMALINE_CONTAINEROPTIONS_H
|
||||
#define GUARD_TOURMALINE_CONTAINEROPTIONS_H
|
||||
|
||||
#include <cstddef>
|
||||
#include <cstdint>
|
||||
|
||||
namespace Tourmaline::Containers {
|
||||
struct HashmapOptions {
|
||||
float loadFactor = 0.75f;
|
||||
float minimizeFactor = 0.20f;
|
||||
float leaningFactor = 2.5f;
|
||||
std::size_t minimumBucketCount = 256;
|
||||
std::size_t reservedBucketSpace = 4;
|
||||
};
|
||||
|
||||
struct DualKeyMapOptions {
|
||||
std::uint64_t baseReservation = 2048;
|
||||
};
|
||||
} // namespace Tourmaline::Containers
|
||||
#endif
|
||||
@@ -10,6 +10,7 @@
|
||||
#define GUARD_TOURMALINE_DUALKEYMAP_H
|
||||
#include "../Concepts.hpp"
|
||||
#include "../Systems/Logging.hpp"
|
||||
#include "ContainerOptions.hpp"
|
||||
#include "Hashmap.hpp"
|
||||
|
||||
#include <algorithm>
|
||||
@@ -28,7 +29,7 @@
|
||||
|
||||
namespace Tourmaline::Containers {
|
||||
template <Concepts::Hashable AKey, Concepts::Hashable BKey, typename Value,
|
||||
uint64_t baseReservation = 2048>
|
||||
DualKeyMapOptions Options = {}>
|
||||
class DualkeyMap {
|
||||
public:
|
||||
// Return Types
|
||||
@@ -49,7 +50,7 @@ public:
|
||||
using Entry = std::tuple<const AKey &, const BKey &, Value &>;
|
||||
|
||||
// Construct/Destruct
|
||||
DualkeyMap() { hashList.reserve(baseReservation); }
|
||||
DualkeyMap() { hashList.reserve(Options.baseReservation); }
|
||||
~DualkeyMap() {
|
||||
// I'm sure there is a better way to do this
|
||||
for (DualkeyHash *hash : hashList) {
|
||||
@@ -301,7 +302,7 @@ private:
|
||||
OppositeKey *oppositeKey;
|
||||
|
||||
Containers::Hashmap<OppositeKey, MultiQueryResult<OppositeKey, keyCount>,
|
||||
8.0f, 2048, 0.01f> // Aggressive hashmap :o
|
||||
{8.0f, 0.01f, 2.5f, 2048, 8}> // Aggressive hashmap :o
|
||||
queryResults;
|
||||
|
||||
for (DualkeyHash *hash : hashList) {
|
||||
@@ -325,7 +326,7 @@ private:
|
||||
// The code above was done to make this code more uniform
|
||||
for (uint64_t index = 0; index < keyCount; index++) {
|
||||
if (keyHashes[index] == hashToCompare && keys[index] == *keyToCompare) {
|
||||
if (queryResults.Has(*oppositeKey)) {
|
||||
if (queryResults.Has(*oppositeKey)) [[likely]] {
|
||||
auto &entry = queryResults.Get(*oppositeKey);
|
||||
entry.valueQueryResults[index] = &hash->value;
|
||||
++entry.howManyFound;
|
||||
|
||||
@@ -10,20 +10,21 @@
|
||||
#define GUARD_TOURMALINE_HASHMAP_H
|
||||
#include "../Concepts.hpp"
|
||||
#include "../Systems/Logging.hpp"
|
||||
#include "ContainerOptions.hpp"
|
||||
|
||||
#include <cstddef>
|
||||
#include <vector>
|
||||
|
||||
namespace Tourmaline::Containers {
|
||||
template <Concepts::Hashable Key, typename Value, float loadFactor = 0.75f,
|
||||
std::size_t minimumBucketCount = 256, float minimizeFactor = 0.20f>
|
||||
template <Concepts::Hashable Key, typename Value, HashmapOptions Options = {}>
|
||||
class Hashmap {
|
||||
public:
|
||||
Hashmap() { storage.resize(minimumBucketCount); }
|
||||
Hashmap() { storage.resize(Options.minimumBucketCount); }
|
||||
~Hashmap() { Clear(); }
|
||||
|
||||
Value &Insert(Key key, Value value) {
|
||||
if (currentLoadFactor >= loadFactor && currentlyRehashing == false) {
|
||||
if (currentLoadFactor >= Options.loadFactor &&
|
||||
currentlyRehashing == false) {
|
||||
rehash();
|
||||
}
|
||||
std::size_t keyHash = std::hash<Key>{}(key),
|
||||
@@ -35,6 +36,8 @@ public:
|
||||
Systems::Logging::Log("Trying to insert the same key twice! Throwing...",
|
||||
"Hashmap", Systems::Logging::LogLevel::Error,
|
||||
Has(key));
|
||||
} else {
|
||||
storage[keyHashPosition].reserve(Options.reservedBucketSpace);
|
||||
}
|
||||
|
||||
storage[keyHashPosition].emplace_back(key, std::move(value), keyHash);
|
||||
@@ -56,7 +59,7 @@ public:
|
||||
});
|
||||
|
||||
currentLoadFactor = (--count) / static_cast<float>(bucketCount);
|
||||
if (currentLoadFactor <= minimizeFactor) {
|
||||
if (currentLoadFactor <= Options.minimizeFactor) {
|
||||
rehash();
|
||||
}
|
||||
}
|
||||
@@ -113,7 +116,7 @@ public:
|
||||
}
|
||||
|
||||
count = 0;
|
||||
bucketCount = minimumBucketCount;
|
||||
bucketCount = Options.minimumBucketCount;
|
||||
std::vector<bucket> newStorage;
|
||||
storage.swap(newStorage);
|
||||
return result;
|
||||
@@ -134,16 +137,15 @@ private:
|
||||
// Minimum
|
||||
goalSize = goalSize == 0 ? count : goalSize;
|
||||
float wouldBeLoadFactor = goalSize / static_cast<float>(bucketCount);
|
||||
if (wouldBeLoadFactor < loadFactor && wouldBeLoadFactor > minimizeFactor)
|
||||
[[unlikely]] {
|
||||
if (wouldBeLoadFactor < Options.loadFactor &&
|
||||
wouldBeLoadFactor > Options.minimizeFactor) [[unlikely]] {
|
||||
return false; // No rehashing is required
|
||||
}
|
||||
|
||||
// Putting it closer to minimizeFactor
|
||||
std::size_t goalBucketCount =
|
||||
goalSize / ((loadFactor + minimizeFactor) / 2.5); // Magic number
|
||||
if (goalBucketCount < minimumBucketCount) [[unlikely]] {
|
||||
goalBucketCount = minimumBucketCount;
|
||||
std::size_t goalBucketCount = goalSize / preferredLoadFactor;
|
||||
if (goalBucketCount < Options.minimumBucketCount) [[unlikely]] {
|
||||
goalBucketCount = Options.minimumBucketCount;
|
||||
}
|
||||
|
||||
// No need to reallocate
|
||||
@@ -181,8 +183,10 @@ private:
|
||||
|
||||
using bucket = std::vector<hashStorage>;
|
||||
std::vector<bucket> storage;
|
||||
std::size_t count = 0, bucketCount = minimumBucketCount;
|
||||
float currentLoadFactor = 0;
|
||||
std::size_t count = 0, bucketCount = Options.minimumBucketCount;
|
||||
float currentLoadFactor = 0,
|
||||
preferredLoadFactor = (Options.loadFactor + Options.minimizeFactor) /
|
||||
Options.leaningFactor;
|
||||
bool currentlyRehashing = false; // Lock for Insert in rehash
|
||||
};
|
||||
} // namespace Tourmaline::Containers
|
||||
|
||||
@@ -10,7 +10,6 @@
|
||||
#include <Systems/ECS.hpp>
|
||||
#include <Systems/ECS/BuiltinComponents.hpp>
|
||||
#include <Systems/Random.hpp>
|
||||
#include <optional>
|
||||
|
||||
using namespace Tourmaline::Systems;
|
||||
using namespace ECS;
|
||||
|
||||
Reference in New Issue
Block a user