aboutsummaryrefslogtreecommitdiff
path: root/src/common/Utilities/Containers.h
diff options
context:
space:
mode:
authorShauren <shauren.trinity@gmail.com>2023-01-07 22:38:21 +0100
committerShauren <shauren.trinity@gmail.com>2023-01-07 22:38:21 +0100
commita53e4a57565d3375a978effbbc32d3eed6aac7e3 (patch)
tree070e7bd084fde9b62769c7ad1e4dbb52e82a10d9 /src/common/Utilities/Containers.h
parent7830e5a7a1b93b0cd083baa3b70a0cfeb475f5f5 (diff)
Core/Misc: Replace enable_if overload selection with if constexpr
Diffstat (limited to 'src/common/Utilities/Containers.h')
-rw-r--r--src/common/Utilities/Containers.h76
1 files changed, 27 insertions, 49 deletions
diff --git a/src/common/Utilities/Containers.h b/src/common/Utilities/Containers.h
index b9a42a7ab3b..85e52887655 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 <algorithm>
#include <iterator>
@@ -29,18 +30,6 @@
namespace Trinity
{
- template<class T>
- constexpr inline T* AddressOrSelf(T* ptr)
- {
- return ptr;
- }
-
- template<class T>
- constexpr inline T* AddressOrSelf(T& not_ptr)
- {
- return std::addressof(not_ptr);
- }
-
template <class T>
class CheckedBufferOutputIterator
{
@@ -252,55 +241,44 @@ namespace Trinity
return false;
}
- /**
- * Returns a pointer to mapped value (or the value itself if map stores pointers)
- */
- template<class M>
- 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 K, class V, template<class, class, class...> class M, class... Rest>
- inline void MultimapErasePair(M<K, V, Rest...>& multimap, K const& key, V const& value)
+ namespace Impl
{
- auto range = multimap.equal_range(key);
- for (auto itr = range.first; itr != range.second;)
+ template <typename Container, typename Predicate>
+ 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 <typename Container, typename Predicate>
- std::enable_if_t<std::is_move_assignable_v<decltype(*std::declval<Container>().begin())>, void> EraseIf(Container& c, Predicate p)
- {
- auto wpos = c.begin();
- for (auto rpos = c.begin(), end = c.end(); rpos != end; ++rpos)
+ template <typename Container, typename Predicate>
+ 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 <typename Container, typename Predicate>
- std::enable_if_t<!std::is_move_assignable_v<decltype(*std::declval<Container>().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<decltype(*c.begin())>)
+ Impl::EraseIfMoveAssignable(c, std::ref(p));
+ else
+ Impl::EraseIfNotMoveAssignable(c, std::ref(p));
}
/**