algorithm.hpp

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

Generated on Wed Jan 21 15:32:09 2009 for SystemC by  doxygen 1.5.5