From 2c81be72eef5363736cf1892646c74a3311ee4c1 Mon Sep 17 00:00:00 2001 From: "B. Stack" Date: Sun, 22 May 2022 17:03:17 -0400 Subject: add upstream 11.21 --- zen/stl_tools.h | 94 ++++++++++++++++++++++++++++++++------------------------- 1 file changed, 53 insertions(+), 41 deletions(-) (limited to 'zen/stl_tools.h') diff --git a/zen/stl_tools.h b/zen/stl_tools.h index 0d359641..9f7977db 100644 --- a/zen/stl_tools.h +++ b/zen/stl_tools.h @@ -10,16 +10,28 @@ #include #include #include +#include +#include #include #include #include #include -#include "string_traits.h" +#include "type_traits.h" //enhancements for namespace zen { +//unfortunately std::erase_if is useless garbage on GCC 12 (requires non-modifying predicate) +template +void eraseIf(std::vector& v, Predicate p); + +template +void eraseIf(std::set& s, Predicate p); + +template +void eraseIf(std::map& m, Predicate p); + //append STL containers template void append(std::vector& v, const C& c); @@ -104,6 +116,44 @@ SharedRef makeSharedRef(Args&& ... args) { return SharedRef(std::make_shar //######################## implementation ######################## + +template inline +void eraseIf(std::vector& v, Predicate p) +{ + v.erase(std::remove_if(v.begin(), v.end(), p), v.end()); +} + + +namespace impl +{ +template inline +void setOrMapEraseIf(S& s, Predicate p) +{ + for (auto it = s.begin(); it != s.end();) + if (p(*it)) + s.erase(it++); + else + ++it; +} +} + + +template inline +void eraseIf(std::set& s, Predicate p) { impl::setOrMapEraseIf(s, p); } //don't make this any more generic! e.g. must not compile for std::vector!!! + + +template inline +void eraseIf(std::map& m, Predicate p) { impl::setOrMapEraseIf(m, p); } + + +template inline +void eraseIf(std::unordered_set& s, Predicate p) { impl::setOrMapEraseIf(s, p); } + + +template inline +void eraseIf(std::unordered_map& m, Predicate p) { impl::setOrMapEraseIf(m, p); } + + template inline void append(std::vector& v, const C& c) { v.insert(v.end(), c.begin(), c.end()); } @@ -249,9 +299,8 @@ void mergeTraversal(Iterator first1, Iterator last1, } -//FNV-1a: https://en.wikipedia.org/wiki/Fowler%E2%80%93Noll%E2%80%93Vo_hash_function template -class FNV1aHash +class FNV1aHash //FNV-1a: https://en.wikipedia.org/wiki/Fowler%E2%80%93Noll%E2%80%93Vo_hash_function { public: FNV1aHash() {} @@ -266,50 +315,13 @@ public: Num get() const { return hashVal_; } private: - static_assert(IsUnsignedIntV); + static_assert(isUnsignedInt); static_assert(sizeof(Num) == 4 || sizeof(Num) == 8); static constexpr Num base_ = sizeof(Num) == 4 ? 2166136261U : 14695981039346656037ULL; static constexpr Num prime_ = sizeof(Num) == 4 ? 16777619U : 1099511628211ULL; Num hashVal_ = base_; }; - - -template inline -Num hashArray(ByteIterator first, ByteIterator last) -{ - using ValType = typename std::iterator_traits::value_type; - static_assert(sizeof(ValType) <= sizeof(Num)); - static_assert(IsIntegerV || std::is_same_v || std::is_same_v); - - FNV1aHash hash; - std::for_each(first, last, [&hash](ValType v) { hash.add(v); }); - return hash.get(); -} - - -struct StringHash //support for custom string classes with std::unordered_set/map -{ - using is_transparent = int; //allow heterogenous lookup! - - template - size_t operator()(const String& str) const - { - const auto* const strFirst = strBegin(str); - return hashArray(strFirst, strFirst + strLength(str)); - } -}; - -struct StringEqual -{ - using is_transparent = int; //allow heterogenous lookup! - - template - bool operator()(const String1& lhs, const String2& rhs) const - { - return equalString(lhs, rhs); - } -}; } #endif //STL_TOOLS_H_84567184321434 -- cgit