1 // GnashAlgorithm.h: useful templates and functors for generic algorithms
3 // Copyright (C) 2007, 2008, 2009, 2010, 2011, 2012
4 // Free Software Foundation, Inc.
6 // This program is free software; you can redistribute it and/or modify
7 // it under the terms of the GNU General Public License as published by
8 // the Free Software Foundation; either version 3 of the License, or
9 // (at your option) any later version.
11 // This program is distributed in the hope that it will be useful,
12 // but WITHOUT ANY WARRANTY; without even the implied warranty of
13 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 // GNU General Public License for more details.
16 // You should have received a copy of the GNU General Public License
17 // along with this program; if not, write to the Free Software
18 // Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
21 #ifndef GNASH_ALGORITHM_H
22 #define GNASH_ALGORITHM_H
29 /// Return a pointer to a type
33 typedef T
* result_type
;
34 result_type
operator()(T
& t
) {
39 /// Erase elements from an associative container based on a predicate
41 /// This removes elements from a container such as a map if they fulfil a
42 /// particular condition. Because keys of associative container are const,
43 /// we can't do this using iterators, because we can't write to them.
44 template<typename Container
, typename Predicate
>
45 void EraseIf(Container
& c
, Predicate p
)
47 typedef typename
Container::iterator iterator
;
49 for (iterator i
= c
.begin(), e
= c
.end(); i
!= e
; ) {
50 iterator stored
= i
++;
51 if (p(*stored
)) c
.erase(stored
);
55 /// Get the size of an array without passing a pointer by mistake
56 template<typename T
, size_t N
>
63 /// Call a functor on the second element of each element in a range.
65 /// @tparam T An iterator type satisfying the requirements of a
67 /// @tparam U The type of the functor op.
68 /// @param begin The start of the range to call op on.
69 /// @param end The end of the range to call op on.
70 /// @param op The function to call on each second element.
71 template<typename T
, typename U
>
73 foreachSecond(T begin
, T end
, U op
)
75 typedef typename
std::iterator_traits
<T
>::value_type value_type
;
77 std::for_each(begin
, end
, std::bind(op
,
78 std::bind(&value_type::second
, std::placeholders::_1
)));