Compare commits

...

5 Commits

Author SHA1 Message Date
cat
151b72749a Cmake complains about line lenght, sigh 2026-01-27 21:14:36 +02:00
cat
d12eb97fa5 Tweaked #include's to use new CMake include change 2026-01-27 21:13:33 +02:00
cat
6ad549df79 Moved libraries to TourmalineExternal, as suggested by mosra 2026-01-27 21:11:57 +02:00
cat
1afb6558aa Changed CMake to have better #includes 2026-01-27 21:11:25 +02:00
cat
5846c5bf34 DKM 4th iteration (tested)
Added Insert and replaced operator[] with Query for clarity, still need to add a delete(and subsequently a tombstoning mechanism)
2026-01-27 21:08:23 +02:00
10 changed files with 78 additions and 46 deletions

View File

@@ -17,7 +17,6 @@ endif()
include(GNUInstallDirs) include(GNUInstallDirs)
include_directories(headers)
add_library(${PROJECT_NAME} SHARED add_library(${PROJECT_NAME} SHARED
"source/Systems/ECS/Component.cpp" "source/Systems/ECS/Component.cpp"
"source/Systems/ECS/World.cpp" "source/Systems/ECS/World.cpp"
@@ -29,11 +28,21 @@ set_target_properties(${PROJECT_NAME} PROPERTIES VERSION ${PROJECT_VERSION})
# Nothing to link right now # Nothing to link right now
target_link_libraries(${PROJECT_NAME}) target_link_libraries(${PROJECT_NAME})
target_include_directories(${PROJECT_NAME}
PUBLIC
$<BUILD_INTERFACE:${CMAKE_CURRENT_SOURCE_DIR}/headers>
$<BUILD_INTERFACE:${CMAKE_CURRENT_SOURCE_DIR}/>
$<INSTALL_INTERFACE:include/${PROJECT_NAME}>
)
install( install(
TARGETS ${PROJECT_NAME} TARGETS ${PROJECT_NAME}
EXPORT ${PROJECT_NAME}Targets EXPORT ${PROJECT_NAME}Targets
LIBRARY DESTINATION lib LIBRARY DESTINATION lib
ARCHIVE DESTINATION lib ARCHIVE DESTINATION lib
RUNTIME DESTINATION bin) INCLUDES DESTINATION include
)
install(DIRECTORY headers/ DESTINATION include/${PROJECT_NAME}) install(DIRECTORY headers/ DESTINATION include/${PROJECT_NAME})
install(DIRECTORY ${PROJECT_NAME}External/ DESTINATION
include/${PROJECT_NAME}External)

View File

@@ -11,69 +11,88 @@
#include "../Systems/Logging.hpp" #include "../Systems/Logging.hpp"
#include "Hashing.hpp" #include "Hashing.hpp"
#include <cmath>
#include <cstddef> #include <cstddef>
#include <cstdint> #include <cstdint>
#include <functional> #include <functional>
#include <optional> #include <optional>
#include <span>
#include <utility> #include <utility>
#include <variant> #include <variant>
#include <vector> #include <vector>
namespace Tourmaline::Containers { namespace Tourmaline::Containers {
template <Hashable AKey, Hashable BKey, typename Value, template <Hashable AKey, Hashable BKey, typename Value,
uint64_t baseReservation = 1024, uint64_t baseReservation = 2048>
float reservationGrowthExponent = 1.5>
class DualkeyMap { class DualkeyMap {
DualkeyMap() { HashList.reserve(baseReservation); } public:
using ResultPair =
std::pair<std::variant<std::monostate, std::reference_wrapper<AKey>,
std::reference_wrapper<BKey>>,
Value &>;
DualkeyMap() { HashList.reserve(baseReservation); }
~DualkeyMap() { ~DualkeyMap() {
// I'm sure there is a better way to do this // I'm sure there is a better way to do this
for (DualkeyHash hash : HashList) { for (DualkeyHash *hash : HashList) {
delete hash.Apointer; delete hash;
delete hash.Bpointer;
delete hash.ValuePointer;
} }
} }
std::span<std::pair<std::variant<std::monostate, AKey &, BKey &>, Value &>> // Insertion
operator[](std::optional<AKey> FirstKey, std::optional<BKey> SecondKey) { void Insert(AKey firstKey, BKey secondKey, Value value) {
bool isFirstKeyGiven = FirstKey.has_value(); std::size_t firstKeyHash = std::hash<AKey>{}(firstKey);
bool isSecondKeyGiven = SecondKey.has_value(); std::size_t secondKeyHash = std::hash<BKey>{}(secondKey);
HashList.push_back(new DualkeyHash(firstKeyHash, std::move(firstKey),
secondKeyHash, std::move(secondKey),
std::move(value)));
}
// Indexing
std::vector<ResultPair> Query(std::optional<AKey> firstKey,
std::optional<BKey> secondKey) {
bool isFirstKeyGiven = firstKey.has_value();
bool isSecondKeyGiven = secondKey.has_value();
if (!isFirstKeyGiven && !isSecondKeyGiven) [[unlikely]] { if (!isFirstKeyGiven && !isSecondKeyGiven) [[unlikely]] {
Systems::Logging::Log("Failed to index! Dualkey maps require at least 1 " Systems::Logging::Log("Failed to index! Dualkey maps require at least 1 "
"key to be given, returning an empty span.", "key to be given, returning an empty vector.",
"Dualkey Map", Systems::Logging::LogLevel::Warning); "Dualkey Map", Systems::Logging::LogLevel::Warning);
return {}; return {};
} }
std::size_t firstKeyHash = std::size_t firstKeyHash =
isFirstKeyGiven ? std::hash<AKey>{}(*FirstKey.value()) : 0; isFirstKeyGiven ? std::hash<AKey>{}(firstKey.value()) : 0;
std::size_t secondKeyHash = std::size_t secondKeyHash =
isSecondKeyGiven ? std::hash<BKey>{}(*SecondKey.value()) : 0; isSecondKeyGiven ? std::hash<BKey>{}(secondKey.value()) : 0;
std::vector< std::vector<ResultPair> finishedQuery{};
std::pair<std::variant<std::monostate, AKey &, BKey &>, Value &>>
finishedQuery{};
uint8_t stateOfIndexing = isFirstKeyGiven + (isSecondKeyGiven << 1); uint8_t stateOfIndexing = isFirstKeyGiven + (isSecondKeyGiven << 1);
for (DualkeyHash hash : HashList) { // Putting hash checks first to benefit from short circuits
for (DualkeyHash *hash : HashList) {
switch (stateOfIndexing) { switch (stateOfIndexing) {
case 1: // Only first key is given case 1: // Only first key is given
if (firstKeyHash == hash.AKeyHash) { if (firstKeyHash == hash->firstKeyHash &&
finishedQuery.emplace_back(hash.BPointer, hash.ValuePointer); firstKey.value() == hash->firstKey) {
finishedQuery.emplace_back(
std::reference_wrapper<BKey>{hash->secondKey}, hash->value);
} }
continue; continue;
case 2: // Only second key is given case 2: // Only second key is given
if (secondKeyHash == hash.BKeyHash) { if (secondKeyHash == hash->secondKeyHash &&
finishedQuery.emplace_back(hash.APointer, hash.ValuePointer); secondKey.value() == hash->secondKey) {
finishedQuery.emplace_back(
std::reference_wrapper<AKey>{hash->firstKey}, hash->value);
} }
continue; continue;
case 3: // Both are given case 3: // Both are given
if (firstKeyHash == hash.AKeyHash && secondKeyHash == hash.BKeyHash) { if (firstKeyHash == hash->firstKeyHash &&
finishedQuery.emplace_back(std::monostate{}, hash.ValuePointer); secondKeyHash == hash->secondKeyHash &&
firstKey.value() == hash->firstKey &&
secondKey.value() == hash->secondKey) {
finishedQuery.emplace_back(std::monostate{}, hash->value);
break;
} }
break; continue;
} }
break; break;
} }
@@ -90,18 +109,21 @@ class DualkeyMap {
private: private:
struct DualkeyHash { struct DualkeyHash {
DualkeyHash(std::size_t AHash, AKey *APointer, std::size_t BHash, DualkeyHash(std::size_t firstKeyHash, AKey &&firstKey,
BKey *BPointer) std::size_t secondKeyHash, BKey &&secondKey, Value &&value)
: AKeyHash(AHash), APointer(APointer), BKeyHash(BHash), : firstKeyHash(firstKeyHash), firstKey(std::move(firstKey)),
BPointer(BPointer) {} secondKeyHash(secondKeyHash), secondKey(std::move(secondKey)),
std::size_t AKeyHash = 0; value(std::move(value)) {}
std::size_t BKeyHash = 0;
AKey *APointer; std::size_t firstKeyHash = 0;
BKey *BPointer; std::size_t secondKeyHash = 0;
Value *ValuePointer; AKey firstKey;
BKey secondKey;
Value value;
}; };
std::vector<DualkeyHash> HashList; // It makes more sense to store the individual hash
std::vector<DualkeyHash *> HashList;
}; };
} // namespace Tourmaline::Containers } // namespace Tourmaline::Containers
#endif #endif

View File

@@ -15,7 +15,7 @@
namespace Tourmaline::Containers { namespace Tourmaline::Containers {
template <typename T> template <typename T>
concept Hashable = requires(T x) { concept Hashable = requires(T x) {
{ std::hash<T>{x}() } -> std::convertible_to<std::size_t>; { std::hash<T>{}(x) } -> std::convertible_to<std::size_t>;
}; };
} // namespace Tourmaline::Containers } // namespace Tourmaline::Containers
#endif #endif

View File

@@ -9,8 +9,9 @@
#ifndef GUARD_TOURMALINE_RANDOM_H #ifndef GUARD_TOURMALINE_RANDOM_H
#define GUARD_TOURMALINE_RANDOM_H #define GUARD_TOURMALINE_RANDOM_H
#include "../../libraries/random/xoshiro.h"
#include "../Types.hpp" #include "../Types.hpp"
#include <TourmalineExternal/random/xoshiro.h>
#include <type_traits> #include <type_traits>
namespace Tourmaline::Systems { namespace Tourmaline::Systems {

View File

@@ -7,7 +7,7 @@
* obtain one at http://mozilla.org/MPL/2.0/. * obtain one at http://mozilla.org/MPL/2.0/.
*/ */
#include "../../../headers/Systems/ECS.hpp" #include <Systems/ECS.hpp>
using namespace Tourmaline::Systems::ECS; using namespace Tourmaline::Systems::ECS;

View File

@@ -7,8 +7,8 @@
* obtain one at http://mozilla.org/MPL/2.0/. * obtain one at http://mozilla.org/MPL/2.0/.
*/ */
#include "../../../headers/Systems/ECS.hpp" #include <Systems/ECS.hpp>
#include "../../../headers/Systems/Random.hpp" #include <Systems/Random.hpp>
using namespace Tourmaline::Systems::ECS; using namespace Tourmaline::Systems::ECS;

View File

@@ -7,7 +7,7 @@
* obtain one at http://mozilla.org/MPL/2.0/. * obtain one at http://mozilla.org/MPL/2.0/.
*/ */
#include "../../headers/Systems/Logging.hpp" #include <Systems/Logging.hpp>
#include <cerrno> #include <cerrno>
#include <chrono> #include <chrono>

View File

@@ -6,7 +6,7 @@
* v. 2.0. If a copy of the MPL was not distributed with this file, You can * 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/. * obtain one at http://mozilla.org/MPL/2.0/.
*/ */
#include "../../headers/Systems/Random.hpp" #include <Systems/Random.hpp>
#include <bit> #include <bit>
#include <cstdint> #include <cstdint>

View File

@@ -7,7 +7,7 @@
* obtain one at http://mozilla.org/MPL/2.0/. * obtain one at http://mozilla.org/MPL/2.0/.
*/ */
#include "../../headers/Types.hpp" #include <Types.hpp>
#include <charconv> #include <charconv>
#include <cstdint> #include <cstdint>