|
|
@ -28,14 +28,42 @@ |
|
|
|
|
|
|
|
|
|
|
|
#pragma once |
|
|
|
#pragma once |
|
|
|
|
|
|
|
|
|
|
|
namespace Dict |
|
|
|
#include <type_traits> |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
namespace Algorithm |
|
|
|
{ |
|
|
|
{ |
|
|
|
// To be used with QMap, QHash and it's variants
|
|
|
|
template <typename ...> |
|
|
|
template <typename Dictionary, typename BinaryPredicate> |
|
|
|
using void_t = void; // replace this with std::void_t in C++17
|
|
|
|
void removeIf(Dictionary &&dict, BinaryPredicate p) |
|
|
|
|
|
|
|
|
|
|
|
template <typename T, typename = void> |
|
|
|
|
|
|
|
struct HasMappedType |
|
|
|
|
|
|
|
: std::false_type |
|
|
|
|
|
|
|
{ |
|
|
|
|
|
|
|
}; |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
template <typename T> |
|
|
|
|
|
|
|
struct HasMappedType<T, void_t<typename T::mapped_type>> |
|
|
|
|
|
|
|
: std::true_type |
|
|
|
|
|
|
|
{ |
|
|
|
|
|
|
|
}; |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// To be used with associative array types, such as QMap, QHash and it's variants
|
|
|
|
|
|
|
|
template <typename T, typename BinaryPredicate |
|
|
|
|
|
|
|
, typename std::enable_if_t<HasMappedType<T>::value, int> = 0> |
|
|
|
|
|
|
|
void removeIf(T &dict, BinaryPredicate p) |
|
|
|
{ |
|
|
|
{ |
|
|
|
auto it = dict.begin(); |
|
|
|
auto it = dict.begin(); |
|
|
|
while (it != dict.end()) |
|
|
|
while (it != dict.end()) |
|
|
|
it = (p(it.key(), it.value()) ? dict.erase(it) : it + 1); |
|
|
|
it = (p(it.key(), it.value()) ? dict.erase(it) : (it + 1)); |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// To be used with set types, such as QSet, std::set
|
|
|
|
|
|
|
|
template <typename T, typename UnaryPredicate |
|
|
|
|
|
|
|
, typename std::enable_if_t<!HasMappedType<T>::value, int> = 0> |
|
|
|
|
|
|
|
void removeIf(T &set, UnaryPredicate p) |
|
|
|
|
|
|
|
{ |
|
|
|
|
|
|
|
auto it = set.begin(); |
|
|
|
|
|
|
|
while (it != set.end()) |
|
|
|
|
|
|
|
it = (p(*it) ? set.erase(it) : (it + 1)); |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|