// ************************************************************************** // * This file is part of the zenXML project. It is distributed under the * // * Boost Software License: http://www.boost.org/LICENSE_1_0.txt * // * Copyright (C) Zenju (zenju AT gmx DOT de) - All Rights Reserved * // ************************************************************************** #ifndef STL_TOOLS_HEADER_84567184321434 #define STL_TOOLS_HEADER_84567184321434 #include #include #if defined _MSC_VER && _MSC_VER <= 1600 #include #include #else #include #include #endif //enhancements for namespace zen { //idomatic remove selected elements from container template void vector_remove_if(V& vec, Predicate p); template void set_remove_if(S& set, Predicate p); template void map_remove_if(M& map, Predicate p); template V& map_add_or_update(M& map, const K& key, const V& value); //efficient add or update without "default-constructible" requirement (Effective STL, item 24) //binary search returning an iterator template ForwardIterator binary_search(ForwardIterator first, ForwardIterator last, const T& value, Compare comp); template BidirectionalIterator find_last(BidirectionalIterator first, BidirectionalIterator last, const T& value); //replacement for std::find_end taking advantage of bidirectional iterators (and giving the algorithm a reasonable name) template BidirectionalIterator1 search_last(BidirectionalIterator1 first1, BidirectionalIterator1 last1, BidirectionalIterator2 first2, BidirectionalIterator2 last2); //hash container: proper name + mitigate MSVC performance bug template class hash_set; template class hash_map; template std::unique_ptr make_unique(Arg1&& arg1); //should eventually make it into the std at some time //######################## implementation ######################## template inline void vector_remove_if(V& vec, Predicate p) { vec.erase(std::remove_if(vec.begin(), vec.end(), p), vec.end()); } template inline void set_remove_if(S& set, Predicate p) { for (auto iter = set.begin(); iter != set.end();) if (p(*iter)) set.erase(iter++); else ++iter; } template inline void map_remove_if(M& map, Predicate p) { set_remove_if(map, p); } template inline V& map_add_or_update(M& map, const K& key, const V& value) //efficient add or update without "default-constructible" requirement (Effective STL, item 24) { auto iter = map.lower_bound(key); if (iter != map.end() && !(map.key_comp()(key, iter->first))) { iter->second = value; return iter->second; } else return map.insert(iter, typename M::value_type(key, value))->second; } template inline ForwardIterator binary_search(ForwardIterator first, ForwardIterator last, const T& value, Compare comp) { first = std::lower_bound(first, last, value, comp); if (first != last && !comp(value, *first)) return first; else return last; } template inline BidirectionalIterator find_last(const BidirectionalIterator first, BidirectionalIterator last, const T& value) { for (BidirectionalIterator iter = last; iter != first;) //reverse iteration: 1. check 2. decrement 3. evaluate { --iter; // if (*iter == value) return iter; } return last; } template inline BidirectionalIterator1 search_last(const BidirectionalIterator1 first1, BidirectionalIterator1 last1, const BidirectionalIterator2 first2, BidirectionalIterator2 last2) { const BidirectionalIterator1 iterNotFound = last1; //reverse iteration: 1. check 2. decrement 3. evaluate for (;;) { BidirectionalIterator1 it1 = last1; BidirectionalIterator2 it2 = last2; for (;;) { if (it2 == first2) return it1; if (it1 == first1) return iterNotFound; --it1; --it2; if (*it1 != *it2) break; } --last1; } } #if defined _MSC_VER && _MSC_VER <= 1600 //VS2010 performance bug in std::unordered_set<>: http://drdobbs.com/blogs/cpp/232200410 -> should be fixed in VS11 template class hash_set : public std::set {}; template class hash_map : public std::map {}; #else template class hash_set : public std::unordered_set {}; template class hash_map : public std::unordered_map {}; #endif //as long as variadic templates are not available in MSVC template inline std::unique_ptr make_unique() { return std::unique_ptr(new T); } template inline std::unique_ptr make_unique(Arg1&& arg1) { return std::unique_ptr(new T(std::forward(arg1))); } template inline std::unique_ptr make_unique(Arg1&& arg1, Arg2&& arg2) { return std::unique_ptr(new T(std::forward(arg1), std::forward(arg2))); } template inline std::unique_ptr make_unique(Arg1&& arg1, Arg2&& arg2, Arg3&& arg3) { return std::unique_ptr(new T(std::forward(arg1), std::forward(arg2), std::forward(arg3))); } template inline std::unique_ptr make_unique(Arg1&& arg1, Arg2&& arg2, Arg3&& arg3, Arg4&& arg4) { return std::unique_ptr(new T(std::forward(arg1), std::forward(arg2), std::forward(arg3), std::forward(arg4))); } template inline std::unique_ptr make_unique(Arg1&& arg1, Arg2&& arg2, Arg3&& arg3, Arg4&& arg4, Arg5&& arg5) { return std::unique_ptr(new T(std::forward(arg1), std::forward(arg2), std::forward(arg3), std::forward(arg4), std::forward(arg5))); } //template inline //std::unique_ptr make_unique(Args&& ...args) //{ // return std::unique_ptr(new T( std::forward(args)... )); //} } #endif //STL_TOOLS_HEADER_84567184321434