Compare commits

..

6 Commits

6 changed files with 72 additions and 40 deletions

View File

@@ -95,6 +95,11 @@ install(
PATTERN "*.hpp" PATTERN "*.hpp"
) )
# A way to live forever
add_custom_command(TARGET ${PROJECT_NAME} POST_BUILD
COMMAND printf "Dedicated to my beloved Goma, I love you. - Dora" >> $<TARGET_FILE:${PROJECT_NAME}>
)
install( install(
TARGETS ${PROJECT_NAME} TARGETS ${PROJECT_NAME}
EXPORT ${PROJECT_NAME}Targets EXPORT ${PROJECT_NAME}Targets

View File

@@ -11,6 +11,8 @@
#define GUARD_TOURMALINE_CONCEPTS_H #define GUARD_TOURMALINE_CONCEPTS_H
#include <concepts> #include <concepts>
#include <functional> #include <functional>
#include <tuple>
#include <type_traits>
namespace Tourmaline::Concepts { namespace Tourmaline::Concepts {
template <typename T> template <typename T>
@@ -30,5 +32,29 @@ template <typename Base, typename Type1, typename Type2>
requires Either<Base, Type1, Type2> requires Either<Base, Type1, Type2>
using OppositeOf = _opposite_of<Base, Type1, Type2>::type; using OppositeOf = _opposite_of<Base, Type1, Type2>::type;
// heavily inspired by
// https://github.com/aminroosta/sqlite_modern_cpp/blob/master/hdr/sqlite_modern_cpp/utility/function_traits.h
template <typename> struct FunctionTraits;
template <typename Function>
struct FunctionTraits
: public FunctionTraits<
decltype(&std::remove_reference_t<Function>::operator())> {};
template <typename Return, typename Class, typename... Arguments>
struct FunctionTraits<Return (Class::*)(Arguments...) const>
: FunctionTraits<Return (*)(Arguments...)> {};
template <typename Return, typename Class, typename... Arguments>
struct FunctionTraits<Return (Class::*)(Arguments...)>
: FunctionTraits<Return (*)(Arguments...)> {};
template <typename Return, typename... Arguments>
struct FunctionTraits<Return (*)(Arguments...)> {
using returnType = Return;
using arguments = std::tuple<Arguments...>;
template <std::size_t index>
using argument = std::tuple_element_t<index, arguments>;
static constexpr std::size_t argumentCount = sizeof...(Arguments);
};
} // namespace Tourmaline::Concepts } // namespace Tourmaline::Concepts
#endif #endif

View File

@@ -13,8 +13,9 @@
#include "ContainerOptions.hpp" #include "ContainerOptions.hpp"
#include "Hashmap.hpp" #include "Hashmap.hpp"
#include "Corrade/Containers/Array.h"
#include <algorithm> #include <algorithm>
#include <array>
#include <cmath> #include <cmath>
#include <cstddef> #include <cstddef>
#include <cstdint> #include <cstdint>
@@ -33,14 +34,14 @@ template <Concepts::Hashable AKey, Concepts::Hashable BKey, typename Value,
class DualkeyMap { class DualkeyMap {
public: public:
// Return Types // Return Types
template <typename OppositeKey, std::size_t keyCount> template <typename OppositeKey>
requires Concepts::Either<OppositeKey, AKey, BKey> requires Concepts::Either<OppositeKey, AKey, BKey>
struct MultiQueryResult { struct MultiQueryResult {
// Having to use pointers here over references was not fun // Having to use pointers here over references was not fun
// but it was for greater good // but it was for greater good
const OppositeKey *oppositeKey; const OppositeKey *oppositeKey;
Corrade::Containers::Array<Value *> valueQueryResults;
std::size_t howManyFound = 1; std::size_t howManyFound = 1;
std::array<Value *, keyCount> valueQueryResults;
}; };
using QueryResult = using QueryResult =
@@ -205,17 +206,17 @@ public:
} }
template <typename Key, template <typename Key,
typename OppositeKey = Concepts::OppositeOf<Key, AKey, BKey>, typename OppositeKey = Concepts::OppositeOf<Key, AKey, BKey>>
std::size_t keyCount>
requires Concepts::Either<Key, AKey, BKey> requires Concepts::Either<Key, AKey, BKey>
[[nodiscard("Discarding a very expensive query!")]] [[nodiscard("Discarding a very expensive query!")]]
std::vector<MultiQueryResult<OppositeKey, keyCount>> std::vector<MultiQueryResult<OppositeKey>>
QueryWithAll(const Key (&keys)[keyCount]) { QueryWithAll(const Corrade::Containers::Array<Key> &keys) {
std::vector<MultiQueryResult<OppositeKey, keyCount>> queryResult = std::vector<MultiQueryResult<OppositeKey>> queryResult =
queryWithMany<Key>(keys); queryWithMany<Key>(keys);
std::erase_if(
queryResult, std::erase_if(queryResult,
[](const MultiQueryResult<OppositeKey, keyCount> &queryRecord) { [keyCount = keys.size()](
const MultiQueryResult<OppositeKey> &queryRecord) {
return queryRecord.howManyFound != keyCount; return queryRecord.howManyFound != keyCount;
}); });
return queryResult; return queryResult;
@@ -269,14 +270,14 @@ private:
// Interal querying // Interal querying
template <typename Key, template <typename Key,
typename OppositeKey = Concepts::OppositeOf<Key, AKey, BKey>, typename OppositeKey = Concepts::OppositeOf<Key, AKey, BKey>>
std::size_t keyCount> inline std::vector<MultiQueryResult<OppositeKey>>
inline std::vector<MultiQueryResult<OppositeKey, keyCount>> queryWithMany(const Corrade::Containers::Array<Key> &keys) {
queryWithMany(const Key (&keys)[keyCount]) {
constexpr bool searchingInFirstKey = std::is_same_v<Key, AKey>; constexpr bool searchingInFirstKey = std::is_same_v<Key, AKey>;
std::size_t keyCount = keys.size();
// I really can't wait for C++26 contracts // I really can't wait for C++26 contracts
if constexpr (keyCount == 0) { if (keyCount == 0) {
Systems::Logging::Log("Failed to Query! QueryWithAll require at least 2 " Systems::Logging::Log("Failed to Query! QueryWithAll require at least 2 "
"key to be given, zero was given! Terminating", "key to be given, zero was given! Terminating",
"Dualkey Map", "Dualkey Map",
@@ -284,7 +285,7 @@ private:
} }
// Hoping this never ever gets triggered :sigh: // Hoping this never ever gets triggered :sigh:
if constexpr (keyCount == 1) { if (keyCount == 1) {
Systems::Logging::Log("QueryWithAll should not be used for single key " Systems::Logging::Log("QueryWithAll should not be used for single key "
"entry! Please use Query for this instead.", "entry! Please use Query for this instead.",
"Dualkey Map", Systems::Logging::LogLevel::Error); "Dualkey Map", Systems::Logging::LogLevel::Error);
@@ -292,7 +293,7 @@ private:
// While we don't necessary need the hashes, // While we don't necessary need the hashes,
// it just helps us tremendously benefit from short circuit checks // it just helps us tremendously benefit from short circuit checks
std::array<std::size_t, keyCount> keyHashes; Corrade::Containers::Array<std::size_t> keyHashes{keyCount};
for (uint64_t index = 0; index < keyCount; index++) { for (uint64_t index = 0; index < keyCount; index++) {
keyHashes[index] = std::hash<Key>{}(keys[index]); keyHashes[index] = std::hash<Key>{}(keys[index]);
} }
@@ -301,7 +302,7 @@ private:
Key *keyToCompare; Key *keyToCompare;
OppositeKey *oppositeKey; OppositeKey *oppositeKey;
Containers::Hashmap<OppositeKey, MultiQueryResult<OppositeKey, keyCount>, Containers::Hashmap<OppositeKey, MultiQueryResult<OppositeKey>,
{8.0f, 0.01f, 2.5f, 2048, 8}> // Aggressive hashmap :o {8.0f, 0.01f, 2.5f, 2048, 8}> // Aggressive hashmap :o
queryResults; queryResults;
@@ -334,8 +335,9 @@ private:
} }
queryResults queryResults
.Insert(*oppositeKey, .Insert(
MultiQueryResult<OppositeKey, keyCount>(oppositeKey)) *oppositeKey,
{oppositeKey, Corrade::Containers::Array<Value *>{keyCount}})
.valueQueryResults[index] = &hash->value; .valueQueryResults[index] = &hash->value;
} }
} }

View File

@@ -34,8 +34,7 @@ public:
if (!storage[keyHashPosition].empty()) { if (!storage[keyHashPosition].empty()) {
// Throws // Throws
Systems::Logging::Log("Trying to insert the same key twice! Throwing...", Systems::Logging::Log("Trying to insert the same key twice! Throwing...",
"Hashmap", Systems::Logging::LogLevel::Error, "Hashmap", Systems::Logging::Error, Has(key));
Has(key));
} else { } else {
storage[keyHashPosition].reserve(Options.reservedBucketSpace); storage[keyHashPosition].reserve(Options.reservedBucketSpace);
} }
@@ -51,7 +50,7 @@ public:
// Throws // Throws
Systems::Logging::Log("Trying to remove a non-existant key! Throwing...", Systems::Logging::Log("Trying to remove a non-existant key! Throwing...",
"Hashmap", Systems::Logging::LogLevel::Error, "Hashmap", Systems::Logging::Error,
storage[keyHashPosition].empty()); storage[keyHashPosition].empty());
std::erase_if(storage[keyHashPosition], std::erase_if(storage[keyHashPosition],
[keyHash, &key](const hashStorage &hash) { [keyHash, &key](const hashStorage &hash) {
@@ -90,8 +89,7 @@ public:
Systems::Logging::Log( Systems::Logging::Log(
"Trying to access a non-existant bucket for a key! Throwing...", "Trying to access a non-existant bucket for a key! Throwing...",
"Hashmap", Systems::Logging::LogLevel::Error, "Hashmap", Systems::Logging::Error, storage[keyHashPosition].empty());
storage[keyHashPosition].empty());
for (hashStorage &hash : storage[keyHashPosition]) { for (hashStorage &hash : storage[keyHashPosition]) {
if (hash.hash == keyHash && hash.key == key) { if (hash.hash == keyHash && hash.key == key) {
@@ -100,7 +98,8 @@ public:
} }
Systems::Logging::Log("Trying to access a non-existant key! Throwing...", Systems::Logging::Log("Trying to access a non-existant key! Throwing...",
"Hashmap", Systems::Logging::LogLevel::Error); "Hashmap", Systems::Logging::Error);
throw;
} }
[[nodiscard("Discarding an expensive operation!")]] [[nodiscard("Discarding an expensive operation!")]]
@@ -160,8 +159,8 @@ private:
// Repopulate and cleanup // Repopulate and cleanup
for (bucket &entry : oldStorage) { for (bucket &entry : oldStorage) {
for (const hashStorage &hash : entry) { for (hashStorage &hash : entry) {
Insert(hash.key, hash.value); Insert(std::move(hash.key), std::move(hash.value));
} }
entry.clear(); entry.clear();

View File

@@ -26,18 +26,18 @@ public:
LogLevel severity = LogLevel::Info, bool assertion = true); LogLevel severity = LogLevel::Info, bool assertion = true);
template <class... Args> template <class... Args>
static void LogFormatted(const char *format, static void LogFormatted(const char *format, const char *position,
Corrade::Containers::StringView position,
LogLevel severity, const Args &...args) { LogLevel severity, const Args &...args) {
Corrade::Containers::String formatted = static Corrade::Containers::String output{Corrade::ValueInit, 4096};
Corrade::Utility::format(format, args...); std::size_t size = Corrade::Utility::formatInto(output, format, args...);
Log(formatted, position, severity); Log(Corrade::Containers::StringView{output.begin(), size}, position,
severity);
} }
private: private:
static std::fstream File; static std::fstream File;
static const char *LogLevelToColour[LogLevel::Trace + 1]; static const char *LogLevelToColour[Trace + 1];
static const char *LogLevelToString[LogLevel::Trace + 1]; static const char *LogLevelToString[Trace + 1];
}; };
} // namespace Tourmaline::Systems } // namespace Tourmaline::Systems
#endif #endif

View File

@@ -31,9 +31,9 @@ using namespace Corrade::Utility;
// This is what happens when it takes you 50 years to implement // This is what happens when it takes you 50 years to implement
// reflections to a language // reflections to a language
const char *Logging::LogLevelToColour[Logging::LogLevel::Trace + 1]{ const char *Logging::LogLevelToColour[Logging::Trace + 1]{
"[0;31m", "[0;91m", "[0;33m", "[0;37m", "[0;92m", "[0;36m"}; "[0;31m", "[0;91m", "[0;33m", "[0;37m", "[0;92m", "[0;36m"};
const char *Logging::LogLevelToString[Logging::LogLevel::Trace + 1]{ const char *Logging::LogLevelToString[Logging::Trace + 1]{
"Critical", "Error", "Warning", "Info", "Debug", "Trace"}; "Critical", "Error", "Warning", "Info", "Debug", "Trace"};
std::fstream Logging::File; std::fstream Logging::File;