00001
00002 #ifndef _INSTIGATE_STL_INTERNAL_HEADER_IN_ALGORITHM
00003 #error This is an internal header file used to implement Instigate STL.\
00004 It should never be included directly by a program.
00005 #endif
00006
00007 #ifndef INSTIGATE_STL_MAX_ELEMENT_HPP
00008 #define INSTIGATE_STL_MAX_ELEMENT_HPP
00009
00059
00060 #include "concept.hpp"
00061 #include "_basis.hpp"
00062
00063
00064
00065
00066
00067
00068 namespace instigate {
00069 namespace stl {
00070 template <typename F>
00071 F max_element(F b, F e);
00072 template <typename F, typename BP>
00073 F max_element(F b, F e, BP p);
00074 }
00075 }
00076
00106 template <typename F>
00107 F instigate::stl::max_element(F b, F e)
00108 {
00109 namespace FI = instigate::stl::forward_iterator;
00110 namespace RI = instigate::stl::readable_iterator;
00111 namespace LT = instigate::generic::less_than_comparable;
00112 typedef typename RI::interface<F>::value_type value_type;
00113 CHECK(FI::requirements<F>);
00114 CHECK(RI::requirements<F>);
00115 CHECK(LT::requirements<value_type>);
00116 if (equal(b, e)) {
00117 return b;
00118 }
00119 char k[sizeof(F)];
00120 F* r = 0;
00121 assign(r, copy_constructor(k, b));
00122 assert(0 != r);
00123 increment(b);
00124 while (!equal(b, e)) {
00125 if (less_than(const_dereference(*r), const_dereference(b))) {
00126 assign(*r, b);
00127 }
00128 increment(b);
00129 }
00130 return *r;
00131 }
00132
00165 template <typename F, typename BP>
00166 F instigate::stl::max_element(F b, F e, BP p)
00167 {
00168 namespace FI = instigate::stl::forward_iterator;
00169 namespace RI = instigate::stl::readable_iterator;
00170 namespace BPI = instigate::stl::binary_predicate;
00171 typedef typename RI::interface<F>::value_type value_type;
00172 CHECK(FI::requirements<F>);
00173 CHECK(RI::requirements<F>);
00174 CHECK(BPI::requirements<BP>);
00175 typedef typename RI::interface<F>::value_type value_type;
00176 typedef typename BPI::interface<BP>::first_argument_type
00177 first_argument_type;
00178 typedef typename BPI::interface<BP>::second_argument_type
00179 second_argument_type;
00180 CHECK_CONVERTIBILITY(value_type, first_argument_type);
00181 CHECK_CONVERTIBILITY(value_type, second_argument_type);
00182 if (equal(b, e)) {
00183 return b;
00184 }
00185 char k[sizeof(F)];
00186 F* r = 0;
00187 assign(r, copy_constructor(k, b));
00188 assert(0 != r);
00189 increment(b);
00190 while (!equal(b, e)) {
00191 if (invoke_predicate(p, const_dereference(*r),
00192 const_dereference(b)))
00193 {
00194 assign(*r, b);
00195 }
00196 increment(b);
00197 }
00198 return *r;
00199 }
00200
00201
00202
00203 #endif // INSTIGATE_STL_MAX_ELEMENT_HPP