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_SEARCH_N_HPP
00008 #define INSTIGATE_STL_SEARCH_N_HPP
00009
00058
00059 #include "_basis.hpp"
00060 #include "concept.hpp"
00061 #include "_find.hpp"
00062
00063
00064
00065
00066
00067
00068 namespace instigate {
00069 namespace stl {
00070 template <typename F, typename I, typename T>
00071 F search_n(F b, F e, I c, const T& v);
00072 template <typename F, typename I, typename T, typename P>
00073 F search_n(F b, F e, I c, const T& v, P p);
00074 }
00075 }
00125 template <typename F, typename I, typename T>
00126 F instigate::stl::search_n(F b, F e, I c, const T& v)
00127 {
00128 typedef typename instigate::stl::readable_iterator::interface<F>::
00129 value_type value_type;
00130 CHECK(instigate::stl::forward_iterator::requirements<F>);
00131 CHECK(instigate::stl::readable_iterator::requirements<F>);
00132 CHECK(instigate::generic::less_than_comparable::requirements<I>);
00133 CHECK(instigate::generic::equality_comparable::
00134 requirements<value_type>);
00135 if (!less_than(0, c)) {
00136 return b;
00137 } else {
00138 b = instigate::stl::find(b, e, v);
00139 while (!equal(b, e)) {
00140 I n = c - 1;
00141 char k[sizeof(F)];
00142 F* i = 0;
00143 assign(i, copy_constructor(k, b));
00144 assert(0 != i);
00145 increment(*i);
00146 while(!equal((*i), e) && !equal(0, n) &&
00147 equal(const_dereference(*i), v))
00148 {
00149 increment(*i);
00150 assign(n, n - 1);
00151 }
00152 if (equal(0, n)) {
00153 return b;
00154 } else {
00155 b = instigate::stl::find(*i, e, v);
00156 }
00157 }
00158 return e;
00159 }
00160 }
00215 template <typename F, typename I, typename T, typename P>
00216 F instigate::stl::search_n(F b, F e, I c, const T& v, P p)
00217 {
00218 typedef typename instigate::stl::readable_iterator::interface<F>::
00219 value_type value_type;
00220 CHECK(instigate::stl::forward_iterator::requirements<F>);
00221 CHECK(instigate::stl::readable_iterator::requirements<F>);
00222 CHECK(instigate::generic::less_than_comparable::requirements<I>);
00223 CHECK(instigate::generic::equality_comparable::
00224 requirements<value_type>);
00225 CHECK(instigate::stl::binary_predicate::requirements<P>);
00226 if (!less_than(0, c)) {
00227 return b;
00228 } else {
00229 while (!equal(b, e)) {
00230 if (invoke_predicate(p, *b, v)) {
00231 break;
00232 }
00233 increment(b);
00234 }
00235 while (!equal(b, e)) {
00236 I n = c - 1;
00237 char k[sizeof(F)];
00238 F* i = 0;
00239 assign(i, copy_constructor(k, b));
00240 assert(0 != i);
00241 increment(*i);
00242 while(!equal(*i, e) && !equal(0, n) &&
00243 invoke_predicate(p,
00244 const_dereference(*i), v))
00245 {
00246 increment(*i);
00247 assign(n, n - 1);
00248 }
00249 if (equal(0, n)) {
00250 return b;
00251 } else {
00252 while(!equal((*i), e)) {
00253 if(invoke_predicate(p,
00254 const_dereference(*i), v))
00255 {
00256 break;
00257 }
00258 increment(*i);
00259 }
00260 assign(b, (*i));
00261 }
00262 }
00263 return e;
00264 }
00265 }
00266
00267
00268
00269 #endif // INSTIGATE_STL_SEARCH_N_HPP
00270