algorithm.hpp
Go to the documentation of this file.
00001 // (C) Copyright Jeremy Siek 2001.
00002 // Distributed under the Boost Software License, Version 1.0. (See accompany-
00003 // ing file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
00004 
00005 /*
00006  *
00007  * Copyright (c) 1994
00008  * Hewlett-Packard Company
00009  *
00010  * Permission to use, copy, modify, distribute and sell this software
00011  * and its documentation for any purpose is hereby granted without fee,
00012  * provided that the above copyright notice appear in all copies and
00013  * that both that copyright notice and this permission notice appear
00014  * in supporting documentation.  Hewlett-Packard Company makes no
00015  * representations about the suitability of this software for any
00016  * purpose.  It is provided "as is" without express or implied warranty.
00017  *
00018  *
00019  * Copyright (c) 1996
00020  * Silicon Graphics Computer Systems, Inc.
00021  *
00022  * Permission to use, copy, modify, distribute and sell this software
00023  * and its documentation for any purpose is hereby granted without fee,
00024  * provided that the above copyright notice appear in all copies and
00025  * that both that copyright notice and this permission notice appear
00026  * in supporting documentation.  Silicon Graphics makes no
00027  * representations about the suitability of this software for any
00028  * purpose.  It is provided "as is" without express or implied warranty.
00029  */
00030 
00031 #ifndef BOOST_ALGORITHM_HPP
00032 # define BOOST_ALGORITHM_HPP
00033 # include <boost/detail/iterator.hpp>
00034 // Algorithms on sequences
00035 //
00036 // The functions in this file have not yet gone through formal
00037 // review, and are subject to change. This is a work in progress.
00038 // They have been checked into the detail directory because
00039 // there are some graph algorithms that use these functions.
00040 
00041 #include <algorithm>
00042 #include <vector>
00043 
00044 namespace boost {
00045 
00046   template <typename Iter1, typename Iter2>
00047   Iter1 begin(const std::pair<Iter1, Iter2>& p) { return p.first; }
00048 
00049   template <typename Iter1, typename Iter2>
00050   Iter2 end(const std::pair<Iter1, Iter2>& p) { return p.second; }
00051 
00052   template <typename Iter1, typename Iter2>
00053   typename boost::detail::iterator_traits<Iter1>::difference_type
00054   size(const std::pair<Iter1, Iter2>& p) {
00055     return std::distance(p.first, p.second);
00056   }
00057 
00058 #if 0
00059   // These seem to interfere with the std::pair overloads :(
00060   template <typename Container>
00061   typename Container::iterator
00062   begin(Container& c) { return c.begin(); }
00063 
00064   template <typename Container>
00065   typename Container::const_iterator
00066   begin(const Container& c) { return c.begin(); }
00067 
00068   template <typename Container>
00069   typename Container::iterator
00070   end(Container& c) { return c.end(); }
00071 
00072   template <typename Container>
00073   typename Container::const_iterator
00074   end(const Container& c) { return c.end(); }
00075 
00076   template <typename Container>
00077   typename Container::size_type
00078   size(const Container& c) { return c.size(); }
00079 #else
00080   template <typename T>
00081   typename std::vector<T>::iterator
00082   begin(std::vector<T>& c) { return c.begin(); }
00083 
00084   template <typename T>
00085   typename std::vector<T>::const_iterator
00086   begin(const std::vector<T>& c) { return c.begin(); }
00087 
00088   template <typename T>
00089   typename std::vector<T>::iterator
00090   end(std::vector<T>& c) { return c.end(); }
00091 
00092   template <typename T>
00093   typename std::vector<T>::const_iterator
00094   end(const std::vector<T>& c) { return c.end(); }
00095 
00096   template <typename T>
00097   typename std::vector<T>::size_type
00098   size(const std::vector<T>& c) { return c.size(); }
00099 #endif
00100   
00101   template <class ForwardIterator, class T>
00102   void iota(ForwardIterator first, ForwardIterator last, T value)
00103   {
00104     for (; first != last; ++first, ++value)
00105       *first = value;
00106   }
00107   template <typename Container, typename T>
00108   void iota(Container& c, const T& value)
00109   {
00110     iota(begin(c), end(c), value);
00111   }
00112  
00113   // Also do version with 2nd container?
00114   template <typename Container, typename OutIter>
00115   OutIter copy(const Container& c, OutIter result) {
00116     return std::copy(begin(c), end(c), result);
00117   }
00118 
00119   template <typename Container1, typename Container2>
00120   bool equal(const Container1& c1, const Container2& c2)
00121   {
00122     if (size(c1) != size(c2))
00123       return false;
00124     return std::equal(begin(c1), end(c1), begin(c2));
00125   }
00126 
00127   template <typename Container>
00128   void sort(Container& c) { std::sort(begin(c), end(c)); }
00129 
00130   template <typename Container, typename Predicate>
00131   void sort(Container& c, const Predicate& p) { 
00132     std::sort(begin(c), end(c), p);
00133   }
00134 
00135   template <typename Container>
00136   void stable_sort(Container& c) { std::stable_sort(begin(c), end(c)); }
00137 
00138   template <typename Container, typename Predicate>
00139   void stable_sort(Container& c, const Predicate& p) { 
00140     std::stable_sort(begin(c), end(c), p);
00141   }
00142 
00143   template <typename InputIterator, typename Predicate>
00144   bool any_if(InputIterator first, InputIterator last, Predicate p)
00145   {
00146     return std::find_if(first, last, p) != last;
00147   }
00148   template <typename Container, typename Predicate>
00149   bool any_if(const Container& c, Predicate p)
00150   {
00151     return any_if(begin(c), end(c), p);
00152   }
00153 
00154   template <typename InputIterator, typename T>
00155   bool container_contains(InputIterator first, InputIterator last, T value)
00156   {
00157     return std::find(first, last, value) != last;
00158   }
00159   template <typename Container, typename T>
00160   bool container_contains(const Container& c, const T& value)
00161   {
00162     return container_contains(begin(c), end(c), value);
00163   }
00164 
00165   template <typename Container, typename T>
00166   std::size_t count(const Container& c, const T& value)
00167   {
00168     return std::count(begin(c), end(c), value);
00169   }
00170 
00171   template <typename Container, typename Predicate>
00172   std::size_t count_if(const Container& c, Predicate p)
00173   {
00174     return std::count_if(begin(c), end(c), p);
00175   }
00176 
00177   template <typename ForwardIterator>
00178   bool is_sorted(ForwardIterator first, ForwardIterator last)
00179   {
00180     if (first == last)
00181       return true;
00182 
00183     ForwardIterator next = first;
00184     for (++next; next != last; first = next, ++next) {
00185       if (*next < *first)
00186         return false;
00187     }
00188 
00189     return true;
00190   }
00191 
00192   template <typename ForwardIterator, typename StrictWeakOrdering>
00193   bool is_sorted(ForwardIterator first, ForwardIterator last,
00194                  StrictWeakOrdering comp)
00195   {
00196     if (first == last)
00197       return true;
00198 
00199     ForwardIterator next = first;
00200     for (++next; next != last; first = next, ++next) {
00201       if (comp(*next, *first))
00202         return false;
00203     }
00204 
00205     return true;
00206   }
00207 
00208   template <typename Container>
00209   bool is_sorted(const Container& c)
00210   {
00211     return is_sorted(begin(c), end(c));
00212   }
00213 
00214   template <typename Container, typename StrictWeakOrdering>
00215   bool is_sorted(const Container& c, StrictWeakOrdering comp)
00216   {
00217     return is_sorted(begin(c), end(c), comp);
00218   }
00219 
00220 } // namespace boost
00221 
00222 #endif // BOOST_ALGORITHM_HPP


appl
Author(s): petercai
autogenerated on Tue Jan 7 2014 11:02:28