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_MIN_ELEMENT_HPP
00008 #define INSTIGATE_STL_MIN_ELEMENT_HPP
00009
00059
00060 #include "concept.hpp"
00061 #include "_basis.hpp"
00062
00063
00064
00065
00066
00067 namespace instigate {
00068 namespace stl {
00069 template <typename F>
00070 F min_element(F b, F e);
00071 template <typename F, typename BP>
00072 F min_element(F b, F e, BP p);
00073 }
00074 }
00075
00106 template <typename F>
00107 F instigate::stl::min_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(b), const_dereference(*r))) {
00126 assign(*r, b);
00127 }
00128 increment(b);
00129 }
00130 return *r;
00131 }
00132
00169 template <typename F, typename BP>
00170 F instigate::stl::min_element(F b, F e, BP p)
00171 {
00172 namespace FI = instigate::stl::forward_iterator;
00173 namespace RI = instigate::stl::readable_iterator;
00174 namespace BPI = instigate::stl::binary_predicate;
00175 typedef typename RI::interface<F>::value_type value_type;
00176 CHECK(FI::requirements<F>);
00177 CHECK(RI::requirements<F>);
00178 CHECK(BPI::requirements<BP>);
00179 typedef typename RI::interface<F>::value_type value_type;
00180 typedef typename BPI::interface<BP>::first_argument_type
00181 first_argument_type;
00182 typedef typename BPI::interface<BP>::second_argument_type
00183 second_argument_type;
00184 CHECK_CONVERTIBILITY(value_type, first_argument_type);
00185 CHECK_CONVERTIBILITY(value_type, second_argument_type);
00186 if (equal(b, e)) {
00187 return b;
00188 }
00189 char k[sizeof(F)];
00190 F* r = 0;
00191 assign(r, copy_constructor(k, b));
00192 assert(0 != r);
00193 increment(b);
00194 while (!equal(b, e)) {
00195 if (invoke_predicate(p, const_dereference(b),
00196 const_dereference(*r)))
00197 {
00198 assign(*r, b);
00199 }
00200 increment(b);
00201 }
00202 return *r;
00203 }
00204
00205
00206
00207 #endif // INSTIGATE_STL_MIN_ELEMENT_HPP