From d2854834e18443876c8f75e0a7f3b88d1d549fc4 Mon Sep 17 00:00:00 2001 From: Daniel Wilhelm Date: Fri, 18 Apr 2014 17:15:39 +0200 Subject: 4.2 --- zen/stl_tools.h | 106 ++++++++++++++++++++++++++++++++++++++++++++++++++++---- 1 file changed, 99 insertions(+), 7 deletions(-) (limited to 'zen/stl_tools.h') diff --git a/zen/stl_tools.h b/zen/stl_tools.h index 6cfe35f8..1707a1eb 100644 --- a/zen/stl_tools.h +++ b/zen/stl_tools.h @@ -1,16 +1,68 @@ // ************************************************************************** -// * This file is part of the FreeFileSync project. It is distributed under * -// * GNU General Public License: http://www.gnu.org/licenses/gpl.html * -// * Copyright (C) 2008-2011 ZenJu (zhnmju123 AT gmx.de) * +// * This file is part of the zenXML project. It is distributed under the * +// * Boost Software License, Version 1.0. See accompanying file * +// * LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt. * +// * Copyright (C) 2011 ZenJu (zhnmju123 AT gmx.de) * // ************************************************************************** #ifndef STL_TOOLS_HEADER_84567184321434 #define STL_TOOLS_HEADER_84567184321434 -//no need to drag in any STL includes :) +//no need to drag in any STL includes + +//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); + +//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); + + + + + + + + + + + + + + + + + + + + + + + + + + +//######################## implementation ######################## + template inline void vector_remove_if(V& vec, Predicate p) { @@ -33,9 +85,8 @@ template inline void map_remove_if(M& map, Predicate p) { set_remove_if(map, p); } -// binary search returning an iterator template inline -ForwardIterator custom_binary_search(ForwardIterator first, ForwardIterator last, const T& value, Compare comp) +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)) @@ -43,7 +94,48 @@ ForwardIterator custom_binary_search(ForwardIterator first, ForwardIterator last else return last; } + + +template inline +BidirectionalIterator find_last(const BidirectionalIterator first, BidirectionalIterator last, const T& value) +{ + const BidirectionalIterator iterNotFound = last; + do + { + if (last == first) + return iterNotFound; + --last; + } + while (!(*last == value)); //loop over "last": 1. check 2. decrement 3. evaluate + return last; } -#endif //STL_TOOLS_HEADER_84567184321434 \ No newline at end of file +template inline +BidirectionalIterator1 search_last(const BidirectionalIterator1 first1, BidirectionalIterator1 last1, + const BidirectionalIterator2 first2, BidirectionalIterator2 last2) +{ + if (first1 == last1 || + first2 == last2) + return last1; + + int diff = static_cast(std::distance(first1, last1)) - + static_cast(std::distance(first2, last2)); + + const BidirectionalIterator1 iterNotFound = last1; + --last2; + + while (diff-- >= 0) //loop over "last1": 1. check 2. decrement 3. evaluate + { + --last1; + + BidirectionalIterator1 iter1 = last1; + for (BidirectionalIterator2 iter2 = last2; *iter1 == *iter2; --iter1, --iter2) + if (iter2 == first2) + return iter1; + } + return iterNotFound; +} +} + +#endif //STL_TOOLS_HEADER_84567184321434 -- cgit