diff --git a/headers/Containers/DualkeyMap.hpp b/headers/Containers/DualkeyMap.hpp index 6b8d22d..b055a0a 100644 --- a/headers/Containers/DualkeyMap.hpp +++ b/headers/Containers/DualkeyMap.hpp @@ -11,6 +11,7 @@ #include "../Concepts.hpp" #include "../Systems/Logging.hpp" +#include #include #include #include @@ -30,11 +31,16 @@ template + template requires Concepts::Either - using MultiQueryResult = - std::pair, Count>>; + struct MultiQueryResult { + // Having to use pointers here over references was not fun + // but it was for greater good + const OppositeKey *oppositeKey; + std::size_t howManyFound = 1; + std::array valueQueryResults; + }; + using QueryResult = std::pair, std::reference_wrapper>, @@ -201,19 +207,16 @@ public: std::size_t keyCount> requires Concepts::Either [[nodiscard("Discarding a very expensive query!")]] - int QueryWithAll(const Key (&keys)[keyCount]) { - std::vector> - queryResults = queryWithMany(keys); - - // You could very well use auto here but this helps - // with LSP hints - for (const unprocessedMultiQueryResult &queryRecord : - queryResults) { - if (queryRecord.howManyFound == keyCount) { - } - } - - return 0; + std::vector> + QueryWithAll(const Key (&keys)[keyCount]) { + std::vector> queryResult = + queryWithMany(keys); + std::erase_if( + queryResult, + [](const MultiQueryResult &queryRecord) { + return queryRecord.howManyFound != keyCount; + }); + return queryResult; } void Scan(std::function - struct unprocessedMultiQueryResult { - OppositeKey *resultKey = nullptr; - std::size_t howManyFound = 1; - std::array valueQueryResults; - }; - struct DualkeyHash { DualkeyHash(AKey &&firstKey, BKey &&secondKey, Value &&value) : firstKey(std::move(firstKey)), secondKey(std::move(secondKey)), @@ -273,7 +269,7 @@ private: template , std::size_t keyCount> - inline std::vector> + inline std::vector> queryWithMany(const Key (&keys)[keyCount]) { constexpr bool searchingInFirstKey = std::is_same_v; @@ -301,9 +297,9 @@ private: uint64_t hashToCompare; Key *keyToCompare; - OppositeKey *resultKey; - std::vector> - queryResults; + OppositeKey *oppositeKey; + std::vector> queryResults; + queryResults.reserve(512); for (DualkeyHash *hash : hashList) { // The hell of doing 2 conditions with similar logics in @@ -311,11 +307,11 @@ private: if constexpr (searchingInFirstKey) { hashToCompare = hash->firstKeyHash; keyToCompare = const_cast(&hash->firstKey); - resultKey = const_cast(&hash->secondKey); + oppositeKey = const_cast(&hash->secondKey); } else { hashToCompare = hash->secondKeyHash; keyToCompare = const_cast(&hash->secondKey); - resultKey = const_cast(&hash->firstKey); + oppositeKey = const_cast(&hash->firstKey); } // The code above was done to make this code more uniform @@ -324,7 +320,7 @@ private: bool doesExist = false; for (auto &queryRecord : queryResults) { - if (*queryRecord.resultKey == *resultKey) { + if (*queryRecord.oppositeKey == *oppositeKey) { queryRecord.valueQueryResults[index] = &hash->value; ++queryRecord.howManyFound; doesExist = true; @@ -338,9 +334,8 @@ private: // Since the result record is not present // we have to make it - queryResults.emplace_back(); + queryResults.emplace_back(oppositeKey); auto &newRecord = queryResults.back(); - newRecord.resultKey = resultKey; newRecord.valueQueryResults[index] = &hash->value; } }