Gnash 0.8.9

GnashAlgorithm.h

Go to the documentation of this file.
00001 // GnashAlgorithm.h: useful templates and functors for generic algorithms
00002 //
00003 //   Copyright (C) 2007, 2008, 2009, 2010, 2011 Free Software Foundation, Inc.
00004 //
00005 // This program is free software; you can redistribute it and/or modify
00006 // it under the terms of the GNU General Public License as published by
00007 // the Free Software Foundation; either version 3 of the License, or
00008 // (at your option) any later version.
00009 //
00010 // This program is distributed in the hope that it will be useful,
00011 // but WITHOUT ANY WARRANTY; without even the implied warranty of
00012 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00013 // GNU General Public License for more details.
00014 //
00015 // You should have received a copy of the GNU General Public License
00016 // along with this program; if not, write to the Free Software
00017 // Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
00018 //
00019 
00020 #ifndef GNASH_ALGORITHM_H
00021 #define GNASH_ALGORITHM_H
00022 
00023 #include <algorithm>
00024 #include <boost/checked_delete.hpp>
00025 #include <boost/intrusive_ptr.hpp>
00026 #include <boost/shared_ptr.hpp>
00027 #include <boost/bind.hpp>
00028 
00029 namespace gnash {
00030 
00032 template<typename T>
00033 struct SecondElement
00034 {
00035     typedef typename T::second_type result_type;
00036 
00037     const result_type& operator()(const T& pair) const {
00038         return pair.second;
00039     }
00040 };
00041 
00043 template<typename T>
00044 struct FirstElement
00045 {
00046     typedef typename T::first_type result_type;
00047 
00048     const result_type& operator()(const T& pair) const {
00049         return pair.first;
00050     }
00051 };
00052 
00054 template<typename T>
00055 struct CreatePointer
00056 {
00057     typedef T* result_type;
00058     result_type operator()(T& t) { 
00059         return &t;
00060     }
00061 };
00062 
00064 //
00068 template<typename Container, typename Predicate>
00069 void EraseIf(Container& c, Predicate p)
00070 {
00071     typedef typename Container::iterator iterator;
00072 
00073     for (iterator i = c.begin(), e = c.end(); i != e; ) {
00074         iterator stored = i++;
00075         if (p(*stored)) c.erase(stored);
00076     }
00077 }
00078 
00079 
00081 template<typename T, size_t N>
00082 size_t
00083 arraySize(T(&)[N])
00084 {
00085     return N;
00086 }
00087 
00089 //
00096 template<typename T, typename U>
00097 void
00098 foreachSecond(T begin, T end, U op)
00099 {
00100     typedef SecondElement<typename std::iterator_traits<T>::value_type> S;
00101     std::for_each(begin, end, boost::bind(op, boost::bind(S(), _1)));
00102 }
00103 
00104 } // namespace gnash
00105 
00106 #endif
00107