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_SET_LEXICOGRAPHICAL_COMPARE_HPP
00008 #define INSTIGATE_STL_SET_LEXICOGRAPHICAL_COMPARE_HPP
00009
00059
00060 #include "concept.hpp"
00061 #include "_min.hpp"
00062 #include "_basis.hpp"
00063
00064
00065 #include <generic/base.hpp>
00066
00067
00068 #include <string.h>
00069
00070
00071 namespace instigate {
00072 namespace stl {
00073 template <typename I, typename I1>
00074 bool lexicographical_compare(I b, I e, I1 f, I1 l);
00075 template <typename I, typename I1, typename B>
00076 bool lexicographical_compare(I b, I e, I1 f, I1 l, B p);
00077 inline bool lexicographical_compare(const char* b,const char* e,
00078 const char* f, const char* l);
00079 }
00080 }
00081
00134 template <typename I, typename I1>
00135 bool instigate::stl::lexicographical_compare(I b, I e, I1 f, I1 l)
00136 {
00137 for (; !equal(b, e) && !equal(f, l); increment(b), increment(f)) {
00138 if (less_than(const_dereference(b), const_dereference(f))) {
00139 return true;
00140 }
00141 if (less_than(const_dereference(f), const_dereference(b))) {
00142 return false;
00143 }
00144 }
00145 return equal(b, e) && !equal(f, l);
00146 }
00147
00186 template <typename I, typename I1, typename B>
00187 bool instigate::stl::lexicographical_compare(I b, I e, I1 f, I1 l, B p)
00188 {
00189 namespace RI = instigate::stl::readable_iterator;
00190 namespace BP = instigate::stl::binary_predicate;
00191 typedef typename RI::interface<I>::value_type value_type1;
00192 typedef typename RI::interface<I1>::value_type value_type2;
00193 typedef typename BP::interface<B>::first_argument_type
00194 first_argument_type;
00195 typedef typename BP::interface<B>::second_argument_type
00196 second_argument_type;
00197 CHECK_CONVERTIBILITY(value_type1, first_argument_type);
00198 CHECK_CONVERTIBILITY(value_type2, second_argument_type);
00199 for (; !equal(b, e) && !equal(f, l); increment(b), increment(f)) {
00200 if(invoke_predicate(p, const_dereference(b),
00201 const_dereference(f))) {
00202 return true;
00203 }
00204 if(invoke_predicate(p, const_dereference(f),
00205 const_dereference(b)))
00206 {
00207 return false;
00208 }
00209 }
00210 return equal(b, e) && !equal(f, l);
00211 }
00212
00216 inline bool instigate::stl::
00217 lexicographical_compare(const char* b, const char* e,
00218 const char* f, const char* l)
00219 {
00220 using namespace instigate::stl;
00221 assert(e >= b);
00222 assert(l >= f);
00223 const size_t len1 = e - b;
00224 const size_t len2 = l - f;
00225 int r = 0;
00226 assign(r, memcmp(b, f, instigate::stl::min(len1, len2)));
00227 if (0 != r) {
00228 return less_than(r, 0);
00229 } else {
00230 return less_than(len1, len2);
00231 }
00232 }
00233
00234
00235
00236 #endif // INSTIGATE_STL_SET_LEXICOGRAPHICAL_COMPARE_HPP