From 27cd5a90f4c1f34c47cd4e1bd1a616e9b11b10ec Mon Sep 17 00:00:00 2001 From: Shauren Date: Sat, 7 Jan 2023 22:38:21 +0100 Subject: Core/Misc: Replace enable_if overload selection with if constexpr (cherry picked from commit a53e4a57565d3375a978effbbc32d3eed6aac7e3) --- src/common/Utilities/Containers.h | 76 ++++++++++++++------------------------- 1 file changed, 27 insertions(+), 49 deletions(-) (limited to 'src/common/Utilities/Containers.h') diff --git a/src/common/Utilities/Containers.h b/src/common/Utilities/Containers.h index 916026b7408..a55602a3163 100644 --- a/src/common/Utilities/Containers.h +++ b/src/common/Utilities/Containers.h @@ -19,6 +19,7 @@ #define TRINITY_CONTAINERS_H #include "Define.h" +#include "MapUtils.h" #include "Random.h" #include #include @@ -29,18 +30,6 @@ namespace Trinity { - template - constexpr inline T* AddressOrSelf(T* ptr) - { - return ptr; - } - - template - constexpr inline T* AddressOrSelf(T& not_ptr) - { - return std::addressof(not_ptr); - } - template class CheckedBufferOutputIterator { @@ -207,55 +196,44 @@ namespace Trinity return false; } - /** - * Returns a pointer to mapped value (or the value itself if map stores pointers) - */ - template - inline auto MapGetValuePtr(M& map, typename M::key_type const& key) -> decltype(AddressOrSelf(map.find(key)->second)) - { - auto itr = map.find(key); - return itr != map.end() ? AddressOrSelf(itr->second) : nullptr; - } - - template class M, class... Rest> - void MultimapErasePair(M& multimap, K const& key, V const& value) + namespace Impl { - auto range = multimap.equal_range(key); - for (auto itr = range.first; itr != range.second;) + template + void EraseIfMoveAssignable(Container& c, Predicate p) { - if (itr->second == value) - itr = multimap.erase(itr); - else - ++itr; + auto wpos = c.begin(); + for (auto rpos = c.begin(), end = c.end(); rpos != end; ++rpos) + { + if (!p(*rpos)) + { + if (rpos != wpos) + std::swap(*rpos, *wpos); + ++wpos; + } + } + c.erase(wpos, c.end()); } - } - template - std::enable_if_t().begin())>, void> EraseIf(Container& c, Predicate p) - { - auto wpos = c.begin(); - for (auto rpos = c.begin(), end = c.end(); rpos != end; ++rpos) + template + void EraseIfNotMoveAssignable(Container& c, Predicate p) { - if (!p(*rpos)) + for (auto it = c.begin(); it != c.end();) { - if (rpos != wpos) - std::swap(*rpos, *wpos); - ++wpos; + if (p(*it)) + it = c.erase(it); + else + ++it; } } - c.erase(wpos, c.end()); } template - std::enable_if_t().begin())>, void> EraseIf(Container& c, Predicate p) + void EraseIf(Container& c, Predicate p) { - for (auto it = c.begin(); it != c.end();) - { - if (p(*it)) - it = c.erase(it); - else - ++it; - } + if constexpr (std::is_move_assignable_v) + Impl::EraseIfMoveAssignable(c, std::ref(p)); + else + Impl::EraseIfNotMoveAssignable(c, std::ref(p)); } } //! namespace Containers -- cgit v1.2.3