1 // GnashAlgorithm.h: useful templates and functors for generic algorithms
3 // Copyright (C) 2007, 2008, 2009, 2010 Free Software Foundation, Inc.
5 // This program is free software; you can redistribute it and/or modify
6 // it under the terms of the GNU General Public License as published by
7 // the Free Software Foundation; either version 3 of the License, or
8 // (at your option) any later version.
10 // This program is distributed in the hope that it will be useful,
11 // but WITHOUT ANY WARRANTY; without even the implied warranty of
12 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 // GNU General Public License for more details.
15 // You should have received a copy of the GNU General Public License
16 // along with this program; if not, write to the Free Software
17 // Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
20 #ifndef GNASH_ALGORITHM_H
21 #define GNASH_ALGORITHM_H
24 #include <boost/checked_delete.hpp>
25 #include <boost/intrusive_ptr.hpp>
26 #include <boost/shared_ptr.hpp>
27 #include <boost/bind.hpp>
31 /// Retrieve the second element of a container with std::pairs.
35 typedef typename
T::second_type result_type
;
37 const result_type
& operator()(const T
& pair
) const {
42 /// Retrieve the first element of a container with std::pairs.
46 typedef typename
T::first_type result_type
;
48 const result_type
& operator()(const T
& pair
) const {
53 /// Return a pointer to a type
57 typedef T
* result_type
;
58 result_type
operator()(T
& t
) {
63 /// Erase elements from an associative container based on a predicate
65 /// This removes elements from a container such as a map if they fulfil a
66 /// particular condition. Because keys of associative container are const,
67 /// we can't do this using iterators, because we can't write to them.
68 template<typename Container
, typename Predicate
>
69 void EraseIf(Container
& c
, Predicate p
)
71 typedef typename
Container::iterator iterator
;
73 for (iterator i
= c
.begin(), e
= c
.end(); i
!= e
; ) {
74 iterator stored
= i
++;
75 if (p(*stored
)) c
.erase(stored
);
80 /// Get the size of an array without passing a pointer by mistake
81 template<typename T
, size_t N
>
88 /// Call a functor on the second element of each element in a range.
90 /// @tparam T An iterator type satisfying the requirements of a
92 /// @tparam U The type of the functor op.
93 /// @param begin The start of the range to call op on.
94 /// @param end The end of the range to call op on.
95 /// @param op The function to call on each second element.
96 template<typename T
, typename U
>
98 foreachSecond(T begin
, T end
, U op
)
100 typedef SecondElement
<typename
std::iterator_traits
<T
>::value_type
> S
;
101 std::for_each(begin
, end
, boost::bind(op
, boost::bind(S(), _1
)));