BWAPI
SPAR/AIModule/BWTA/vendors/CGAL/CGAL/algorithm.h
Go to the documentation of this file.
00001 // Copyright (c) 2003  Utrecht University (The Netherlands),
00002 // ETH Zurich (Switzerland), Freie Universitaet Berlin (Germany),
00003 // INRIA Sophia-Antipolis (France), Martin-Luther-University Halle-Wittenberg
00004 // (Germany), Max-Planck-Institute Saarbruecken (Germany), RISC Linz (Austria),
00005 // and Tel-Aviv University (Israel).  All rights reserved.
00006 //
00007 // This file is part of CGAL (www.cgal.org); you can redistribute it and/or
00008 // modify it under the terms of the GNU Lesser General Public License as
00009 // published by the Free Software Foundation; version 2.1 of the License.
00010 // See the file LICENSE.LGPL distributed with CGAL.
00011 //
00012 // Licensees holding a valid commercial license may use this file in
00013 // accordance with the commercial license agreement provided with the software.
00014 //
00015 // This file is provided AS IS with NO WARRANTY OF ANY KIND, INCLUDING THE
00016 // WARRANTY OF DESIGN, MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.
00017 //
00018 // $URL: svn+ssh://scm.gforge.inria.fr/svn/cgal/branches/CGAL-3.5-branch/STL_Extension/include/CGAL/algorithm.h $
00019 // $Id: algorithm.h 46206 2008-10-11 20:21:08Z spion $
00020 //
00021 //
00022 // Author(s)     : Michael Hoffmann <hoffmann@inf.ethz.ch>
00023 //                 Lutz Kettner <kettner@mpi-sb.mpg.de>
00024 //                 Sylvain Pion
00025 
00026 #ifndef CGAL_ALGORITHM_H
00027 #define CGAL_ALGORITHM_H
00028 
00029 #include <CGAL/basic.h>
00030 #include <algorithm>
00031 #include <iosfwd>
00032 
00033 CGAL_BEGIN_NAMESPACE
00034 
00035 // copy_n is usually in the STL as well, but not in the official
00036 // standard. We provide our own copy_n.  It is planned for C++0x.
00037 
00038 template <class InputIterator, class Size, class OutputIterator>
00039 OutputIterator copy_n( InputIterator first,
00040                        Size n,
00041                        OutputIterator result)
00042 {
00043   // copies the first `n' items from `first' to `result'. Returns
00044   // the value of `result' after inserting the `n' items.
00045   while( n--) {
00046     *result = *first;
00047     first++;
00048     result++;
00049   }
00050   return result;
00051 }
00052 
00053 
00054 // Not documented
00055 template <class T> inline
00056 bool
00057 are_sorted(const T & a, const T & b, const T & c)
00058 {
00059   return a <= b && b <= c;
00060 }
00061 
00062 // Not documented
00063 template <class T, class Compare> inline
00064 bool
00065 are_sorted(const T & a, const T & b, const T & c, Compare cmp)
00066 {
00067   return !cmp(b, a) && !cmp(c, b);
00068 }
00069 
00070 // Not documented
00071 template <class T> inline
00072 bool
00073 are_strictly_sorted(const T & a, const T & b, const T & c)
00074 {
00075   return a < b && b < c;
00076 }
00077 
00078 // Not documented
00079 template <class T, class Compare> inline
00080 bool
00081 are_strictly_sorted(const T & a, const T & b, const T & c, Compare cmp)
00082 {
00083   return cmp(a, b) && cmp(b, c);
00084 }
00085 
00086 // Not documented
00087 // Checks that b is in the interval [min(a, c) , max(a, c)].
00088 template <class T> inline
00089 bool
00090 are_ordered(const T & a, const T & b, const T & c)
00091 {
00092   const T& min = (CGAL::min)(a, c);
00093   const T& max = (CGAL::max)(a, c);
00094   return min <= b && b <= max;
00095 }
00096 
00097 // Not documented
00098 // Checks that b is in the interval [min(a, c) , max(a, c)].
00099 template <class T, class Compare> inline
00100 bool
00101 are_ordered(const T & a, const T & b, const T & c, Compare cmp)
00102 {
00103   const T& min = (std::min)(a, c, cmp);
00104   const T& max = (std::max)(a, c, cmp);
00105   return !cmp(b, min) && !cmp(max, b);
00106 }
00107 
00108 // Not documented
00109 // Checks that b is in the interval ]min(a, c) , max(a, c)[.
00110 template <class T> inline
00111 bool
00112 are_strictly_ordered(const T & a, const T & b, const T & c)
00113 {
00114   const T& min = (CGAL::min)(a, c);
00115   const T& max = (CGAL::max)(a, c);
00116   return min < b && b < max;
00117 }
00118 
00119 // Not documented
00120 // Checks that b is in the interval ]min(a, c) , max(a, c)[.
00121 template <class T, class Compare> inline
00122 bool
00123 are_strictly_ordered(const T & a, const T & b, const T & c, Compare cmp)
00124 {
00125   const T& min = (std::min)(a, c, cmp);
00126   const T& max = (std::max)(a, c, cmp);
00127   return cmp(min, b) && cmp(b, max);
00128 }
00129 
00130 
00131 template <class ForwardIterator>
00132 inline
00133 ForwardIterator
00134 successor( ForwardIterator it )
00135 {
00136   return ++it;
00137 }
00138 
00139 template <class BidirectionalIterator>
00140 inline
00141 BidirectionalIterator
00142 predecessor( BidirectionalIterator it )
00143 {
00144   return --it;
00145 }
00146 
00147 template < class ForwardIterator >
00148 std::pair< ForwardIterator, ForwardIterator >
00149 min_max_element(ForwardIterator first, ForwardIterator last)
00150 {
00151   typedef std::pair< ForwardIterator, ForwardIterator > FP;
00152   FP result(first, first);
00153   if (first != last)
00154     while (++first != last) {
00155       if (*first < *result.first)
00156         result.first = first;
00157       if (*result.second < *first)
00158         result.second = first;
00159     }
00160   return result;
00161 }
00162 
00163 template < class ForwardIterator, class CompareMin, class CompareMax >
00164 std::pair< ForwardIterator, ForwardIterator >
00165 min_max_element(ForwardIterator first,
00166                 ForwardIterator last,
00167                 CompareMin comp_min,
00168                 CompareMax comp_max)
00169 {
00170   typedef std::pair< ForwardIterator, ForwardIterator > FP;
00171   FP result(first, first);
00172   if (first != last)
00173     while (++first != last) {
00174       if (comp_min(*first, *result.first))
00175         result.first = first;
00176       if (comp_max(*result.second, *first))
00177         result.second = first;
00178     }
00179   return result;
00180 }
00181 
00182 template < class ForwardIterator, class Predicate >
00183 ForwardIterator
00184 min_element_if(ForwardIterator first,
00185                ForwardIterator last,
00186                Predicate pred)
00187 {
00188   ForwardIterator result = first = std::find_if(first, last, pred);
00189   if (first != last)
00190     while (++first != last)
00191       if (*first < *result && pred(*first))
00192         result = first;
00193   return result;
00194 }
00195 
00196 template < class ForwardIterator, class Compare, class Predicate >
00197 ForwardIterator
00198 min_element_if(ForwardIterator first,
00199                ForwardIterator last,
00200                Compare comp,
00201                Predicate pred)
00202 {
00203   ForwardIterator result = first = std::find_if(first, last, pred);
00204   if (first != last)
00205     while (++first != last)
00206       if (comp(*first, *result) && pred(*first))
00207         result = first;
00208   return result;
00209 }
00210 
00211 template < class ForwardIterator, class Predicate >
00212 ForwardIterator
00213 max_element_if(ForwardIterator first,
00214                ForwardIterator last,
00215                Predicate pred)
00216 {
00217   ForwardIterator result = first = std::find_if(first, last, pred);
00218   if (first != last)
00219     while (++first != last)
00220       if (*result < *first && pred(*first))
00221         result = first;
00222   return result;
00223 }
00224 
00225 template < class ForwardIterator, class Compare, class Predicate >
00226 ForwardIterator
00227 max_element_if(ForwardIterator first,
00228                ForwardIterator last,
00229                Compare comp,
00230                Predicate pred)
00231 {
00232   ForwardIterator result = first = std::find_if(first, last, pred);
00233   if (first != last)
00234     while (++first != last)
00235       if (comp(*result, *first) && pred(*first))
00236         result = first;
00237   return result;
00238 }
00239 
00240 
00241 
00260 template <class InputIterator1, class InputIterator2, class BinaryFunction>
00261 CGAL::Comparison_result
00262 lexicographical_compare_three_valued( InputIterator1 first1, InputIterator1 last1,
00263              InputIterator2 first2, InputIterator2 last2,
00264              BinaryFunction cmp) {
00265     while ( first1 != last1 && first2 != last2) {
00266         CGAL::Comparison_result result = cmp( *first1, *first2);
00267         if ( result != CGAL::EQUAL)
00268             return result;
00269         ++first1;
00270         ++first2;
00271     }
00272     if ( first1 != last1)
00273         return CGAL::LARGER;
00274     if ( first2 != last2)
00275         return CGAL::SMALLER;
00276     return CGAL::EQUAL;
00277 }
00278 
00294 template <class InputIterator>
00295 std::ostream& 
00296 output_range(std::ostream& os,
00297              InputIterator first, InputIterator beyond,
00298              const char* sep = ", ", const char* pre = "", const char* post = "") 
00299 {
00300     InputIterator it = first;
00301     if (it != beyond) {
00302         os << pre << oformat(*it) << post;
00303         while (++it != beyond) os << sep << pre << oformat(*it) << post;
00304     }
00305     return os;
00306 }
00307 
00308 CGAL_END_NAMESPACE
00309 
00310 #endif // CGAL_ALGORITHM_H
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Defines