4 * Hewlett-Packard Company
6 * Copyright (c) 1996,1997
7 * Silicon Graphics Computer Systems, Inc.
10 * Moscow Center for SPARC Technology
15 * This material is provided "as is", with absolutely no warranty expressed
16 * or implied. Any use is at your own risk.
18 * Permission to use or copy this software for any purpose is hereby granted
19 * without fee, provided the above notices are retained on all copies.
20 * Permission to modify the code and to distribute modified code is granted,
21 * provided the above notices are retained, and a notice that the code was
22 * modified is included with the above copyright notice.
25 #ifndef _STLP_ALGOBASE_C
26 #define _STLP_ALGOBASE_C
28 #ifndef _STLP_INTERNAL_ALGOBASE_H
29 # include <stl/_algobase.h>
32 #ifndef _STLP_INTERNAL_FUNCTION_BASE_H
33 # include <stl/_function_base.h>
38 template <class _InputIter1, class _InputIter2>
39 bool lexicographical_compare(_InputIter1 __first1, _InputIter1 __last1,
40 _InputIter2 __first2, _InputIter2 __last2) {
41 _STLP_DEBUG_CHECK(_STLP_PRIV __check_range(__first1, __last1))
42 _STLP_DEBUG_CHECK(_STLP_PRIV __check_range(__first2, __last2))
43 for ( ; __first1 != __last1 && __first2 != __last2
44 ; ++__first1, ++__first2) {
45 if (*__first1 < *__first2) {
46 _STLP_VERBOSE_ASSERT(!(*__first2 < *__first1), _StlMsg_INVALID_STRICT_WEAK_PREDICATE)
49 if (*__first2 < *__first1)
52 return __first1 == __last1 && __first2 != __last2;
55 template <class _InputIter1, class _InputIter2, class _Compare>
56 bool lexicographical_compare(_InputIter1 __first1, _InputIter1 __last1,
57 _InputIter2 __first2, _InputIter2 __last2,
59 _STLP_DEBUG_CHECK(_STLP_PRIV __check_range(__first1, __last1))
60 _STLP_DEBUG_CHECK(_STLP_PRIV __check_range(__first2, __last2))
61 for ( ; __first1 != __last1 && __first2 != __last2
62 ; ++__first1, ++__first2) {
63 if (__comp(*__first1, *__first2)) {
64 _STLP_VERBOSE_ASSERT(!__comp(*__first2, *__first1),
65 _StlMsg_INVALID_STRICT_WEAK_PREDICATE)
68 if (__comp(*__first2, *__first1))
71 return __first1 == __last1 && __first2 != __last2;
74 #if !defined (_STLP_NO_EXTENSIONS)
75 _STLP_MOVE_TO_PRIV_NAMESPACE
77 template <class _InputIter1, class _InputIter2>
78 int __lexicographical_compare_3way(_InputIter1 __first1, _InputIter1 __last1,
79 _InputIter2 __first2, _InputIter2 __last2) {
80 while (__first1 != __last1 && __first2 != __last2) {
81 if (*__first1 < *__first2) {
82 _STLP_VERBOSE_ASSERT(!(*__first2 < *__first1), _StlMsg_INVALID_STRICT_WEAK_PREDICATE)
85 if (*__first2 < *__first1)
90 if (__first2 == __last2) {
91 return !(__first1 == __last1);
98 _STLP_MOVE_TO_STD_NAMESPACE
100 template <class _InputIter1, class _InputIter2>
101 int lexicographical_compare_3way(_InputIter1 __first1, _InputIter1 __last1,
102 _InputIter2 __first2, _InputIter2 __last2) {
103 _STLP_DEBUG_CHECK(_STLP_PRIV __check_range(__first1, __last1))
104 _STLP_DEBUG_CHECK(_STLP_PRIV __check_range(__first2, __last2))
105 return _STLP_PRIV __lexicographical_compare_3way(__first1, __last1, __first2, __last2);
109 _STLP_MOVE_TO_PRIV_NAMESPACE
111 template <class _RandomAccessIter, class _Tp>
112 _STLP_INLINE_LOOP _RandomAccessIter __find(_RandomAccessIter __first, _RandomAccessIter __last,
114 const random_access_iterator_tag &) {
115 _STLP_DIFFERENCE_TYPE(_RandomAccessIter) __trip_count = (__last - __first) >> 2;
117 for ( ; __trip_count > 0 ; --__trip_count) {
118 if (*__first == __val) return __first;
121 if (*__first == __val) return __first;
124 if (*__first == __val) return __first;
127 if (*__first == __val) return __first;
131 switch (__last - __first) {
133 if (*__first == __val) return __first;
136 if (*__first == __val) return __first;
139 if (*__first == __val) return __first;
148 __find(char* __first, char* __last, char __val, const random_access_iterator_tag &) {
149 void *res = memchr(__first, __val, __last - __first);
150 return res != 0 ? __STATIC_CAST(char*, res) : __last;
153 __find(const char* __first, const char* __last, char __val, const random_access_iterator_tag &) {
154 const void *res = memchr(__first, __val, __last - __first);
155 return res != 0 ? __STATIC_CAST(const char*, res) : __last;
158 template <class _RandomAccessIter, class _Predicate>
159 _STLP_INLINE_LOOP _RandomAccessIter __find_if(_RandomAccessIter __first, _RandomAccessIter __last,
161 const random_access_iterator_tag &) {
162 _STLP_DIFFERENCE_TYPE(_RandomAccessIter) __trip_count = (__last - __first) >> 2;
164 for ( ; __trip_count > 0 ; --__trip_count) {
165 if (__pred(*__first)) return __first;
168 if (__pred(*__first)) return __first;
171 if (__pred(*__first)) return __first;
174 if (__pred(*__first)) return __first;
178 switch(__last - __first) {
180 if (__pred(*__first)) return __first;
183 if (__pred(*__first)) return __first;
186 if (__pred(*__first)) return __first;
194 template <class _InputIter, class _Tp>
195 _STLP_INLINE_LOOP _InputIter __find(_InputIter __first, _InputIter __last,
197 const input_iterator_tag &) {
198 while (__first != __last && !(*__first == __val)) ++__first;
202 template <class _InputIter, class _Predicate>
203 _STLP_INLINE_LOOP _InputIter __find_if(_InputIter __first, _InputIter __last,
205 const input_iterator_tag &) {
206 while (__first != __last && !__pred(*__first))
211 _STLP_MOVE_TO_STD_NAMESPACE
213 template <class _InputIter, class _Predicate>
214 _InputIter find_if(_InputIter __first, _InputIter __last,
216 _STLP_DEBUG_CHECK(_STLP_PRIV __check_range(__first, __last))
217 return _STLP_PRIV __find_if(__first, __last, __pred, _STLP_ITERATOR_CATEGORY(__first, _InputIter));
220 template <class _InputIter, class _Tp>
221 _InputIter find(_InputIter __first, _InputIter __last, const _Tp& __val) {
222 _STLP_DEBUG_CHECK(_STLP_PRIV __check_range(__first, __last))
223 return _STLP_PRIV __find(__first, __last, __val, _STLP_ITERATOR_CATEGORY(__first, _InputIter));
226 template <class _ForwardIter1, class _ForwardIter2, class _BinaryPred>
227 _ForwardIter1 search(_ForwardIter1 __first1, _ForwardIter1 __last1,
228 _ForwardIter2 __first2, _ForwardIter2 __last2,
229 _BinaryPred __pred) {
230 _STLP_DEBUG_CHECK(_STLP_PRIV __check_range(__first1, __last1))
231 _STLP_DEBUG_CHECK(_STLP_PRIV __check_range(__first2, __last2))
232 // Test for empty ranges
233 if (__first1 == __last1 || __first2 == __last2)
236 // Test for a pattern of length 1.
237 _ForwardIter2 __p1(__first2);
239 if ( ++__p1 == __last2 ) {
240 while (__first1 != __last1 && !__pred(*__first1, *__first2)) {
248 for ( ; ; ) { // __first1 != __last1 will be checked below
249 while (__first1 != __last1 && !__pred(*__first1, *__first2)) {
252 if (__first1 == __last1) {
255 _ForwardIter2 __p = __p1;
256 _ForwardIter1 __current = __first1;
257 if (++__current == __last1) return __last1;
259 while (__pred(*__current, *__p)) {
260 if (++__p == __last2)
262 if (++__current == __last1)
270 _STLP_MOVE_TO_PRIV_NAMESPACE
272 struct _IsCharLikeType
273 { typedef __false_type _Ret; };
275 _STLP_TEMPLATE_NULL struct _IsCharLikeType<char>
276 { typedef __true_type _Ret; };
278 _STLP_TEMPLATE_NULL struct _IsCharLikeType<unsigned char>
279 { typedef __true_type _Ret; };
281 # ifndef _STLP_NO_SIGNED_BUILTINS
282 _STLP_TEMPLATE_NULL struct _IsCharLikeType<signed char>
283 { typedef __true_type _Ret; };
286 template <class _Tp1, class _Tp2>
287 inline bool __stlp_eq(_Tp1 __val1, _Tp2 __val2)
288 { return __val1 == __val2; }
290 #if defined (_STLP_FUNCTION_TMPL_PARTIAL_ORDER)
292 inline bool __stlp_eq(_Tp, _Tp)
296 template <class _InputIter, class _ForwardIter, class _Tp2, class _Predicate>
297 inline _InputIter __find_first_of_aux2(_InputIter __first1, _InputIter __last1,
298 _ForwardIter __first2, _ForwardIter __last2,
299 _Tp2*, _Predicate __pred,
300 const __true_type& /* _UseStrcspnLikeAlgo */) {
301 unsigned char __hints[(UCHAR_MAX + 1) / CHAR_BIT];
302 memset(__hints, 0, sizeof(__hints) / sizeof(unsigned char));
303 for (; __first2 != __last2; ++__first2) {
304 unsigned char __tmp = (unsigned char)*__first2;
305 __hints[__tmp / CHAR_BIT] |= (1 << (__tmp % CHAR_BIT));
308 for (; __first1 != __last1; ++__first1) {
309 _Tp2 __tmp = (_Tp2)*__first1;
310 if (__stlp_eq(*__first1, __tmp) &&
311 __pred((__hints[(unsigned char)__tmp / CHAR_BIT] & (1 << ((unsigned char)__tmp % CHAR_BIT))) != 0))
317 template <class _InputIter, class _ForwardIter, class _Tp2, class _Predicate>
318 inline _InputIter __find_first_of_aux2(_InputIter __first1, _InputIter __last1,
319 _ForwardIter __first2, _ForwardIter __last2,
320 _Tp2* /* __dummy */, _Predicate /* __pred */,
321 const __false_type& /* _UseStrcspnLikeAlgo */) {
322 return _STLP_PRIV __find_first_of(__first1, __last1, __first2, __last2,
323 _STLP_PRIV __equal_to(_STLP_VALUE_TYPE(__first1, _InputIter)));
326 template <class _InputIter, class _ForwardIter, class _Tp1, class _Tp2>
327 inline _InputIter __find_first_of_aux1(_InputIter __first1, _InputIter __last1,
328 _ForwardIter __first2, _ForwardIter __last2,
329 _Tp1* __pt1, _Tp2* __pt2) {
330 typedef _STLP_TYPENAME _STLP_STD::_IsIntegral<_Tp1>::_Ret _IsIntegral;
331 typedef _STLP_TYPENAME _STLP_PRIV _IsCharLikeType<_Tp2>::_Ret _IsCharLike;
332 typedef _STLP_TYPENAME _STLP_STD::_Land2<_IsIntegral, _IsCharLike>::_Ret _UseStrcspnLikeAlgo;
333 return _STLP_PRIV __find_first_of_aux2(__first1, __last1,
335 __pt2, _Identity<bool>(), _UseStrcspnLikeAlgo());
338 template <class _InputIter, class _ForwardIter>
339 inline _InputIter __find_first_of(_InputIter __first1, _InputIter __last1,
340 _ForwardIter __first2, _ForwardIter __last2) {
341 return _STLP_PRIV __find_first_of_aux1(__first1, __last1, __first2, __last2,
342 _STLP_VALUE_TYPE(__first1, _InputIter),
343 _STLP_VALUE_TYPE(__first2, _ForwardIter));
346 // find_first_of, with and without an explicitly supplied comparison function.
347 template <class _InputIter, class _ForwardIter, class _BinaryPredicate>
348 _InputIter __find_first_of(_InputIter __first1, _InputIter __last1,
349 _ForwardIter __first2, _ForwardIter __last2,
350 _BinaryPredicate __comp) {
351 for ( ; __first1 != __last1; ++__first1) {
352 for (_ForwardIter __iter = __first2; __iter != __last2; ++__iter) {
353 if (__comp(*__first1, *__iter)) {
361 // find_end, with and without an explicitly supplied comparison function.
362 // Search [first2, last2) as a subsequence in [first1, last1), and return
363 // the *last* possible match. Note that find_end for bidirectional iterators
364 // is much faster than for forward iterators.
366 // find_end for forward iterators.
367 template <class _ForwardIter1, class _ForwardIter2,
368 class _BinaryPredicate>
369 _ForwardIter1 __find_end(_ForwardIter1 __first1, _ForwardIter1 __last1,
370 _ForwardIter2 __first2, _ForwardIter2 __last2,
371 const forward_iterator_tag &, const forward_iterator_tag &,
372 _BinaryPredicate __comp) {
373 if (__first2 == __last2)
376 _ForwardIter1 __result = __last1;
378 _ForwardIter1 __new_result = _STLP_STD::search(__first1, __last1, __first2, __last2, __comp);
379 if (__new_result == __last1)
382 __result = __new_result;
383 __first1 = __new_result;
390 _STLP_MOVE_TO_STD_NAMESPACE
392 // find_end for bidirectional iterators. Requires partial specialization.
393 #if defined (_STLP_CLASS_PARTIAL_SPECIALIZATION)
395 # ifndef _STLP_INTERNAL_ITERATOR_H
397 # include <stl/_iterator.h>
398 _STLP_BEGIN_NAMESPACE
399 # endif /*_STLP_INTERNAL_ITERATOR_H*/
401 _STLP_MOVE_TO_PRIV_NAMESPACE
403 template <class _BidirectionalIter1, class _BidirectionalIter2,
404 class _BinaryPredicate>
406 __find_end(_BidirectionalIter1 __first1, _BidirectionalIter1 __last1,
407 _BidirectionalIter2 __first2, _BidirectionalIter2 __last2,
408 const bidirectional_iterator_tag &, const bidirectional_iterator_tag &,
409 _BinaryPredicate __comp) {
410 typedef _STLP_STD::reverse_iterator<_BidirectionalIter1> _RevIter1;
411 typedef _STLP_STD::reverse_iterator<_BidirectionalIter2> _RevIter2;
413 _RevIter1 __rlast1(__first1);
414 _RevIter2 __rlast2(__first2);
415 _RevIter1 __rresult = _STLP_STD::search(_RevIter1(__last1), __rlast1,
416 _RevIter2(__last2), __rlast2,
419 if (__rresult == __rlast1)
422 _BidirectionalIter1 __result = __rresult.base();
423 _STLP_STD::advance(__result, -_STLP_STD::distance(__first2, __last2));
428 _STLP_MOVE_TO_STD_NAMESPACE
429 #endif /* _STLP_CLASS_PARTIAL_SPECIALIZATION */
431 template <class _ForwardIter1, class _ForwardIter2,
432 class _BinaryPredicate>
434 find_end(_ForwardIter1 __first1, _ForwardIter1 __last1,
435 _ForwardIter2 __first2, _ForwardIter2 __last2,
436 _BinaryPredicate __comp) {
437 _STLP_DEBUG_CHECK(_STLP_PRIV __check_range(__first1, __last1))
438 _STLP_DEBUG_CHECK(_STLP_PRIV __check_range(__first2, __last2))
439 return _STLP_PRIV __find_end(__first1, __last1, __first2, __last2,
440 #if defined (_STLP_CLASS_PARTIAL_SPECIALIZATION)
441 _STLP_ITERATOR_CATEGORY(__first1, _ForwardIter1),
442 _STLP_ITERATOR_CATEGORY(__first2, _ForwardIter2),
444 forward_iterator_tag(),
445 forward_iterator_tag(),
450 _STLP_MOVE_TO_PRIV_NAMESPACE
452 template <class _ForwardIter, class _Tp, class _Compare1, class _Compare2, class _Distance>
453 _ForwardIter __lower_bound(_ForwardIter __first, _ForwardIter __last, const _Tp& __val,
454 _Compare1 __comp1, _Compare2 __comp2, _Distance*) {
455 _Distance __len = _STLP_STD::distance(__first, __last);
457 _ForwardIter __middle;
462 _STLP_STD::advance(__middle, __half);
463 if (__comp1(*__middle, __val)) {
464 _STLP_VERBOSE_ASSERT(!__comp2(__val, *__middle), _StlMsg_INVALID_STRICT_WEAK_PREDICATE)
467 __len = __len - __half - 1;
475 _STLP_MOVE_TO_STD_NAMESPACE
479 #endif /* _STLP_ALGOBASE_C */