Update with current status
[gnash.git] / libbase / GnashAlgorithm.h
blob2174832d8c23500cb94fd639ca01da2d1fcfb841
1 // GnashAlgorithm.h: useful templates and functors for generic algorithms
2 //
3 // Copyright (C) 2007, 2008, 2009, 2010, 2011, 2012
4 // Free Software Foundation, Inc.
5 //
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
24 #include <algorithm>
25 #include <functional>
27 namespace gnash {
29 /// Return a pointer to a type
30 template<typename T>
31 struct CreatePointer
33 typedef T* result_type;
34 result_type operator()(T& t) {
35 return &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>
57 size_t
58 arraySize(T(&)[N])
60 return 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
66 /// forward iterator
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>
72 void
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)));
81 } // namespace gnash
83 #endif