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_3WAY_HPP
00008 #define INSTIGATE_STL_SET_LEXICOGRAPHICAL_COMPARE_3WAY_HPP
00009
00059
00060 #include "concept.hpp"
00061
00062
00063 #include <generic/base.hpp>
00064
00065
00066
00067
00068 namespace instigate {
00069 namespace stl {
00070 template <typename I, typename I1>
00071 int lexicographical_compare_3way(I b, I e, I1 f, I1 l);
00072
00073 int lexicographical_compare_3way(const char* b, const char* e,
00074 const char* f, const char* l);
00075 }
00076 }
00077
00128 template <typename I, typename I1>
00129 int instigate::stl::lexicographical_compare_3way(I b, I e, I1 f, I1 l)
00130 {
00131 namespace SP = instigate::stl::single_pass_iterator;
00132 namespace RI = instigate::stl::readable_iterator;
00133 namespace EQ = instigate::generic::equality_comparable;
00134 namespace LT = instigate::generic::less_than_comparable;
00135 namespace INC = instigate::stl::incrementable_iterator;
00136 namespace WR = instigate::stl::writable_iterator;
00137 CHECK(SP::requirements<I>);
00138 CHECK(RI::requirements<I>);
00139 CHECK(SP::requirements<I1>);
00140 CHECK(RI::requirements<I1>);
00141 typedef typename RI::interface<I>::value_type value_type1;
00142 typedef typename RI::interface<I1>::value_type value_type2;
00143 CHECK(LT::requirements<value_type1>);
00144 CHECK(LT::requirements<value_type2>);
00145 while (!equal(b, e) && !equal(f, l)) {
00146 if (less_than(dereference(b), dereference(f))) {
00147 return -1;
00148 }
00149 if (less_than(dereference(f), dereference(b))) {
00150 return 1;
00151 }
00152 increment(b);
00153 increment(f);
00154 }
00155 if(equal(f, l)) {
00156 return !equal(b, e);
00157 } else {
00158 return -1;
00159 }
00160 }
00161
00166 inline int lexicographical_compare_3way(const char* b, const char* e,
00167 const char* f, const char* l)
00168 {
00169 using namespace instigate::stl;
00170 ptrdiff_t len1 = 0;
00171 assert(e >= b);
00172 assign(len1, e - b);
00173 ptrdiff_t len2 = 0;
00174 assert(l >= f);
00175 assign(len2, l - f);
00176 int r = 0;
00177 assign(r, memcmp(b, f, instigate::stl::min(len1, len2)));
00178 if (0 != r) {
00179 return r;
00180 } else {
00181 if (equal(len1, len2)) {
00182 return 0;
00183 } else {
00184 if (less_than(len1, len2)) {
00185 return -1;
00186 } else {
00187 return 1;
00188 }
00189 }
00190 }
00191 }
00192
00193
00194
00195 #endif // INSTIGATE_STL_SET_LEXICOGRAPHICAL_COMPARE_3WAY_HPP