1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000
1.2 +++ b/epoc32/include/tools/stlport/stl/_algobase.c Wed Mar 31 12:33:34 2010 +0100
1.3 @@ -0,0 +1,428 @@
1.4 +/*
1.5 + *
1.6 + * Copyright (c) 1994
1.7 + * Hewlett-Packard Company
1.8 + *
1.9 + * Copyright (c) 1996,1997
1.10 + * Silicon Graphics Computer Systems, Inc.
1.11 + *
1.12 + * Copyright (c) 1997
1.13 + * Moscow Center for SPARC Technology
1.14 + *
1.15 + * Copyright (c) 1999
1.16 + * Boris Fomitchev
1.17 + *
1.18 + * This material is provided "as is", with absolutely no warranty expressed
1.19 + * or implied. Any use is at your own risk.
1.20 + *
1.21 + * Permission to use or copy this software for any purpose is hereby granted
1.22 + * without fee, provided the above notices are retained on all copies.
1.23 + * Permission to modify the code and to distribute modified code is granted,
1.24 + * provided the above notices are retained, and a notice that the code was
1.25 + * modified is included with the above copyright notice.
1.26 + *
1.27 + */
1.28 +#ifndef _STLP_ALGOBASE_C
1.29 +#define _STLP_ALGOBASE_C
1.30 +
1.31 +#ifndef _STLP_INTERNAL_ALGOBASE_H
1.32 +# include <stl/_algobase.h>
1.33 +#endif
1.34 +
1.35 +_STLP_BEGIN_NAMESPACE
1.36 +
1.37 +template <class _InputIter1, class _InputIter2>
1.38 +bool lexicographical_compare(_InputIter1 __first1, _InputIter1 __last1,
1.39 + _InputIter2 __first2, _InputIter2 __last2) {
1.40 + _STLP_DEBUG_CHECK(_STLP_PRIV __check_range(__first1, __last1))
1.41 + _STLP_DEBUG_CHECK(_STLP_PRIV __check_range(__first2, __last2))
1.42 + for ( ; __first1 != __last1 && __first2 != __last2
1.43 + ; ++__first1, ++__first2) {
1.44 + if (*__first1 < *__first2) {
1.45 + _STLP_VERBOSE_ASSERT(!(*__first2 < *__first1), _StlMsg_INVALID_STRICT_WEAK_PREDICATE)
1.46 + return true;
1.47 + }
1.48 + if (*__first2 < *__first1)
1.49 + return false;
1.50 + }
1.51 + return __first1 == __last1 && __first2 != __last2;
1.52 +}
1.53 +
1.54 +template <class _InputIter1, class _InputIter2, class _Compare>
1.55 +bool lexicographical_compare(_InputIter1 __first1, _InputIter1 __last1,
1.56 + _InputIter2 __first2, _InputIter2 __last2,
1.57 + _Compare __comp) {
1.58 + _STLP_DEBUG_CHECK(_STLP_PRIV __check_range(__first1, __last1))
1.59 + _STLP_DEBUG_CHECK(_STLP_PRIV __check_range(__first2, __last2))
1.60 + for ( ; __first1 != __last1 && __first2 != __last2
1.61 + ; ++__first1, ++__first2) {
1.62 + if (__comp(*__first1, *__first2)) {
1.63 + _STLP_VERBOSE_ASSERT(!__comp(*__first2, *__first1), _StlMsg_INVALID_STRICT_WEAK_PREDICATE)
1.64 + return true;
1.65 + }
1.66 + if (__comp(*__first2, *__first1))
1.67 + return false;
1.68 + }
1.69 + return __first1 == __last1 && __first2 != __last2;
1.70 +}
1.71 +
1.72 +#if !defined (_STLP_NO_EXTENSIONS)
1.73 +_STLP_MOVE_TO_PRIV_NAMESPACE
1.74 +
1.75 +template <class _InputIter1, class _InputIter2>
1.76 +int __lexicographical_compare_3way(_InputIter1 __first1, _InputIter1 __last1,
1.77 + _InputIter2 __first2, _InputIter2 __last2) {
1.78 + while (__first1 != __last1 && __first2 != __last2) {
1.79 + if (*__first1 < *__first2) {
1.80 + _STLP_VERBOSE_ASSERT(!(*__first2 < *__first1), _StlMsg_INVALID_STRICT_WEAK_PREDICATE)
1.81 + return -1;
1.82 + }
1.83 + if (*__first2 < *__first1)
1.84 + return 1;
1.85 + ++__first1;
1.86 + ++__first2;
1.87 + }
1.88 + if (__first2 == __last2) {
1.89 + return !(__first1 == __last1);
1.90 + }
1.91 + else {
1.92 + return -1;
1.93 + }
1.94 +}
1.95 +
1.96 +_STLP_MOVE_TO_STD_NAMESPACE
1.97 +
1.98 +template <class _InputIter1, class _InputIter2>
1.99 +int lexicographical_compare_3way(_InputIter1 __first1, _InputIter1 __last1,
1.100 + _InputIter2 __first2, _InputIter2 __last2) {
1.101 + _STLP_DEBUG_CHECK(_STLP_PRIV __check_range(__first1, __last1))
1.102 + _STLP_DEBUG_CHECK(_STLP_PRIV __check_range(__first2, __last2))
1.103 + return _STLP_PRIV __lexicographical_compare_3way(__first1, __last1, __first2, __last2);
1.104 +}
1.105 +#endif
1.106 +
1.107 +_STLP_MOVE_TO_PRIV_NAMESPACE
1.108 +
1.109 +template <class _RandomAccessIter, class _Tp>
1.110 +_STLP_INLINE_LOOP _RandomAccessIter __find(_RandomAccessIter __first, _RandomAccessIter __last,
1.111 + const _Tp& __val,
1.112 + const random_access_iterator_tag &) {
1.113 + _STLP_DIFFERENCE_TYPE(_RandomAccessIter) __trip_count = (__last - __first) >> 2;
1.114 +
1.115 + for ( ; __trip_count > 0 ; --__trip_count) {
1.116 + if (*__first == __val) return __first;
1.117 + ++__first;
1.118 +
1.119 + if (*__first == __val) return __first;
1.120 + ++__first;
1.121 +
1.122 + if (*__first == __val) return __first;
1.123 + ++__first;
1.124 +
1.125 + if (*__first == __val) return __first;
1.126 + ++__first;
1.127 + }
1.128 +
1.129 + switch (__last - __first) {
1.130 + case 3:
1.131 + if (*__first == __val) return __first;
1.132 + ++__first;
1.133 + case 2:
1.134 + if (*__first == __val) return __first;
1.135 + ++__first;
1.136 + case 1:
1.137 + if (*__first == __val) return __first;
1.138 + //++__first;
1.139 + case 0:
1.140 + default:
1.141 + return __last;
1.142 + }
1.143 +}
1.144 +
1.145 +inline char*
1.146 +__find(char* __first, char* __last, char __val, const random_access_iterator_tag &) {
1.147 + void *res = memchr(__first, __val, __last - __first);
1.148 + return res != 0 ? __STATIC_CAST(char*, res) : __last;
1.149 +}
1.150 +inline const char*
1.151 +__find(const char* __first, const char* __last, char __val, const random_access_iterator_tag &) {
1.152 + const void *res = memchr(__first, __val, __last - __first);
1.153 + return res != 0 ? __STATIC_CAST(const char*, res) : __last;
1.154 +}
1.155 +
1.156 +template <class _RandomAccessIter, class _Predicate>
1.157 +_STLP_INLINE_LOOP _RandomAccessIter __find_if(_RandomAccessIter __first, _RandomAccessIter __last,
1.158 + _Predicate __pred,
1.159 + const random_access_iterator_tag &) {
1.160 + _STLP_DIFFERENCE_TYPE(_RandomAccessIter) __trip_count = (__last - __first) >> 2;
1.161 +
1.162 + for ( ; __trip_count > 0 ; --__trip_count) {
1.163 + if (__pred(*__first)) return __first;
1.164 + ++__first;
1.165 +
1.166 + if (__pred(*__first)) return __first;
1.167 + ++__first;
1.168 +
1.169 + if (__pred(*__first)) return __first;
1.170 + ++__first;
1.171 +
1.172 + if (__pred(*__first)) return __first;
1.173 + ++__first;
1.174 + }
1.175 +
1.176 + switch(__last - __first) {
1.177 + case 3:
1.178 + if (__pred(*__first)) return __first;
1.179 + ++__first;
1.180 + case 2:
1.181 + if (__pred(*__first)) return __first;
1.182 + ++__first;
1.183 + case 1:
1.184 + if (__pred(*__first)) return __first;
1.185 + //++__first;
1.186 + case 0:
1.187 + default:
1.188 + return __last;
1.189 + }
1.190 +}
1.191 +
1.192 +template <class _InputIter, class _Tp>
1.193 +_STLP_INLINE_LOOP _InputIter __find(_InputIter __first, _InputIter __last,
1.194 + const _Tp& __val,
1.195 + const input_iterator_tag &) {
1.196 + while (__first != __last && !(*__first == __val)) ++__first;
1.197 + return __first;
1.198 +}
1.199 +
1.200 +template <class _InputIter, class _Predicate>
1.201 +_STLP_INLINE_LOOP _InputIter __find_if(_InputIter __first, _STLP_MPW_EXTRA_CONST _InputIter __last,
1.202 + _Predicate __pred,
1.203 + const input_iterator_tag &) {
1.204 + while (__first != __last && !__pred(*__first))
1.205 + ++__first;
1.206 + return __first;
1.207 +}
1.208 +
1.209 +_STLP_MOVE_TO_STD_NAMESPACE
1.210 +
1.211 +template <class _InputIter, class _Predicate>
1.212 +_InputIter find_if(_InputIter __first, _InputIter __last,
1.213 + _Predicate __pred) {
1.214 + _STLP_DEBUG_CHECK(_STLP_PRIV __check_range(__first, __last))
1.215 + return _STLP_PRIV __find_if(__first, __last, __pred, _STLP_ITERATOR_CATEGORY(__first, _InputIter));
1.216 +}
1.217 +
1.218 +template <class _InputIter, class _Tp>
1.219 +_InputIter find(_InputIter __first, _InputIter __last, const _Tp& __val) {
1.220 + _STLP_DEBUG_CHECK(_STLP_PRIV __check_range(__first, __last))
1.221 + return _STLP_PRIV __find(__first, __last, __val, _STLP_ITERATOR_CATEGORY(__first, _InputIter));
1.222 +}
1.223 +
1.224 +template <class _ForwardIter1, class _ForwardIter2, class _BinaryPred>
1.225 +_ForwardIter1 search(_ForwardIter1 __first1, _ForwardIter1 __last1,
1.226 + _ForwardIter2 __first2, _ForwardIter2 __last2,
1.227 + _BinaryPred __pred) {
1.228 + _STLP_DEBUG_CHECK(_STLP_PRIV __check_range(__first1, __last1))
1.229 + _STLP_DEBUG_CHECK(_STLP_PRIV __check_range(__first2, __last2))
1.230 + // Test for empty ranges
1.231 + if (__first1 == __last1 || __first2 == __last2)
1.232 + return __first1;
1.233 +
1.234 + // Test for a pattern of length 1.
1.235 + _ForwardIter2 __tmp(__first2);
1.236 + ++__tmp;
1.237 + if (__tmp == __last2) {
1.238 + while (__first1 != __last1 && !__pred(*__first1, *__first2)) {
1.239 + _STLP_VERBOSE_ASSERT(!__pred(*__first2, *__first1), _StlMsg_INVALID_EQUIVALENT_PREDICATE)
1.240 + ++__first1;
1.241 + }
1.242 + _STLP_VERBOSE_ASSERT((__first1 == __last1) || __pred(*__first2, *__first1),
1.243 + _StlMsg_INVALID_EQUIVALENT_PREDICATE)
1.244 + return __first1;
1.245 + }
1.246 +
1.247 + // General case.
1.248 +
1.249 + _ForwardIter2 __p1, __p;
1.250 +
1.251 + __p1 = __first2; ++__p1;
1.252 +
1.253 + // _ForwardIter1 __current = __first1;
1.254 +
1.255 + while (__first1 != __last1) {
1.256 + while (__first1 != __last1) {
1.257 + if (__pred(*__first1, *__first2)) {
1.258 + _STLP_VERBOSE_ASSERT(__pred(*__first2, *__first1), _StlMsg_INVALID_EQUIVALENT_PREDICATE)
1.259 + break;
1.260 + }
1.261 + _STLP_VERBOSE_ASSERT(!__pred(*__first2, *__first1), _StlMsg_INVALID_EQUIVALENT_PREDICATE)
1.262 + ++__first1;
1.263 + }
1.264 + while (__first1 != __last1 && !__pred(*__first1, *__first2)) {
1.265 + _STLP_VERBOSE_ASSERT(!__pred(*__first2, *__first1), _StlMsg_INVALID_EQUIVALENT_PREDICATE)
1.266 + ++__first1;
1.267 + }
1.268 + if (__first1 == __last1)
1.269 + return __last1;
1.270 + _STLP_VERBOSE_ASSERT(__pred(*__first2, *__first1), _StlMsg_INVALID_EQUIVALENT_PREDICATE)
1.271 +
1.272 + __p = __p1;
1.273 + _ForwardIter1 __current = __first1;
1.274 + if (++__current == __last1) return __last1;
1.275 +
1.276 + while (__pred(*__current, *__p)) {
1.277 + _STLP_VERBOSE_ASSERT(__pred(*__p, *__current), _StlMsg_INVALID_EQUIVALENT_PREDICATE)
1.278 + if (++__p == __last2)
1.279 + return __first1;
1.280 + if (++__current == __last1)
1.281 + return __last1;
1.282 + }
1.283 +
1.284 + _STLP_VERBOSE_ASSERT(!__pred(*__p, *__current), _StlMsg_INVALID_EQUIVALENT_PREDICATE)
1.285 + ++__first1;
1.286 + }
1.287 + return __first1;
1.288 +}
1.289 +
1.290 +_STLP_MOVE_TO_PRIV_NAMESPACE
1.291 +
1.292 +// find_first_of, with and without an explicitly supplied comparison function.
1.293 +template <class _InputIter, class _ForwardIter, class _BinaryPredicate>
1.294 +_InputIter __find_first_of(_InputIter __first1, _InputIter __last1,
1.295 + _ForwardIter __first2, _ForwardIter __last2,
1.296 + _BinaryPredicate __comp) {
1.297 + for ( ; __first1 != __last1; ++__first1) {
1.298 + for (_ForwardIter __iter = __first2; __iter != __last2; ++__iter) {
1.299 + if (__comp(*__first1, *__iter)) {
1.300 + _STLP_VERBOSE_ASSERT(__comp(*__iter, *__first1), _StlMsg_INVALID_EQUIVALENT_PREDICATE)
1.301 + return __first1;
1.302 + }
1.303 + _STLP_VERBOSE_ASSERT(!__comp(*__iter, *__first1), _StlMsg_INVALID_EQUIVALENT_PREDICATE)
1.304 + }
1.305 + }
1.306 + return __last1;
1.307 +}
1.308 +
1.309 +// find_end, with and without an explicitly supplied comparison function.
1.310 +// Search [first2, last2) as a subsequence in [first1, last1), and return
1.311 +// the *last* possible match. Note that find_end for bidirectional iterators
1.312 +// is much faster than for forward iterators.
1.313 +
1.314 +// find_end for forward iterators.
1.315 +template <class _ForwardIter1, class _ForwardIter2,
1.316 + class _BinaryPredicate>
1.317 +_ForwardIter1 __find_end(_ForwardIter1 __first1, _ForwardIter1 __last1,
1.318 + _ForwardIter2 __first2, _ForwardIter2 __last2,
1.319 + const forward_iterator_tag &, const forward_iterator_tag &,
1.320 + _BinaryPredicate __comp) {
1.321 + if (__first2 == __last2)
1.322 + return __last1;
1.323 + else {
1.324 + _ForwardIter1 __result = __last1;
1.325 + for (;;) {
1.326 + _ForwardIter1 __new_result = search(__first1, __last1, __first2, __last2, __comp);
1.327 + if (__new_result == __last1)
1.328 + return __result;
1.329 + else {
1.330 + __result = __new_result;
1.331 + __first1 = __new_result;
1.332 + ++__first1;
1.333 + }
1.334 + }
1.335 + }
1.336 +}
1.337 +
1.338 +_STLP_MOVE_TO_STD_NAMESPACE
1.339 +
1.340 +// find_end for bidirectional iterators. Requires partial specialization.
1.341 +#if defined (_STLP_CLASS_PARTIAL_SPECIALIZATION)
1.342 +
1.343 +# ifndef _STLP_INTERNAL_ITERATOR_H
1.344 +_STLP_END_NAMESPACE
1.345 +# include <stl/_iterator.h>
1.346 +_STLP_BEGIN_NAMESPACE
1.347 +# endif /*_STLP_INTERNAL_ITERATOR_H*/
1.348 +
1.349 +_STLP_MOVE_TO_PRIV_NAMESPACE
1.350 +
1.351 +template <class _BidirectionalIter1, class _BidirectionalIter2,
1.352 + class _BinaryPredicate>
1.353 +_BidirectionalIter1
1.354 +__find_end(_BidirectionalIter1 __first1, _BidirectionalIter1 __last1,
1.355 + _BidirectionalIter2 __first2, _BidirectionalIter2 __last2,
1.356 + const bidirectional_iterator_tag &, const bidirectional_iterator_tag &,
1.357 + _BinaryPredicate __comp) {
1.358 + typedef reverse_iterator<_BidirectionalIter1> _RevIter1;
1.359 + typedef reverse_iterator<_BidirectionalIter2> _RevIter2;
1.360 +
1.361 + _RevIter1 __rlast1(__first1);
1.362 + _RevIter2 __rlast2(__first2);
1.363 + _RevIter1 __rresult = search(_RevIter1(__last1), __rlast1,
1.364 + _RevIter2(__last2), __rlast2,
1.365 + __comp);
1.366 +
1.367 + if (__rresult == __rlast1)
1.368 + return __last1;
1.369 + else {
1.370 + _BidirectionalIter1 __result = __rresult.base();
1.371 + advance(__result, -distance(__first2, __last2));
1.372 + return __result;
1.373 + }
1.374 +}
1.375 +
1.376 +_STLP_MOVE_TO_STD_NAMESPACE
1.377 +#endif /* _STLP_CLASS_PARTIAL_SPECIALIZATION */
1.378 +
1.379 +template <class _ForwardIter1, class _ForwardIter2,
1.380 + class _BinaryPredicate>
1.381 +_ForwardIter1
1.382 +find_end(_ForwardIter1 __first1, _ForwardIter1 __last1,
1.383 + _ForwardIter2 __first2, _ForwardIter2 __last2,
1.384 + _BinaryPredicate __comp) {
1.385 + _STLP_DEBUG_CHECK(_STLP_PRIV __check_range(__first1, __last1))
1.386 + _STLP_DEBUG_CHECK(_STLP_PRIV __check_range(__first2, __last2))
1.387 + return _STLP_PRIV __find_end(__first1, __last1, __first2, __last2,
1.388 +#if defined (_STLP_CLASS_PARTIAL_SPECIALIZATION)
1.389 + _STLP_ITERATOR_CATEGORY(__first1, _ForwardIter1),
1.390 + _STLP_ITERATOR_CATEGORY(__first2, _ForwardIter2),
1.391 +#else
1.392 + forward_iterator_tag(),
1.393 + forward_iterator_tag(),
1.394 +#endif
1.395 + __comp);
1.396 +}
1.397 +
1.398 +_STLP_MOVE_TO_PRIV_NAMESPACE
1.399 +
1.400 +template <class _ForwardIter, class _Tp, class _Compare1, class _Compare2, class _Distance>
1.401 +_ForwardIter __lower_bound(_ForwardIter __first, _ForwardIter __last, const _Tp& __val,
1.402 + _Compare1 __comp1, _Compare2 __comp2, _Distance*) {
1.403 + _Distance __len = distance(__first, __last);
1.404 + _Distance __half;
1.405 + _ForwardIter __middle;
1.406 +
1.407 + while (__len > 0) {
1.408 + __half = __len >> 1;
1.409 + __middle = __first;
1.410 + advance(__middle, __half);
1.411 + if (__comp1(*__middle, __val)) {
1.412 + _STLP_VERBOSE_ASSERT(!__comp2(__val, *__middle), _StlMsg_INVALID_STRICT_WEAK_PREDICATE)
1.413 + __first = __middle;
1.414 + ++__first;
1.415 + __len = __len - __half - 1;
1.416 + }
1.417 + else
1.418 + __len = __half;
1.419 + }
1.420 + return __first;
1.421 +}
1.422 +
1.423 +_STLP_MOVE_TO_STD_NAMESPACE
1.424 +
1.425 +_STLP_END_NAMESPACE
1.426 +
1.427 +#endif /* _STLP_ALGOBASE_C */
1.428 +
1.429 +// Local Variables:
1.430 +// mode:C++
1.431 +// End: