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