_algobase.c 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484
  1. /*
  2. *
  3. * Copyright (c) 1994
  4. * Hewlett-Packard Company
  5. *
  6. * Copyright (c) 1996,1997
  7. * Silicon Graphics Computer Systems, Inc.
  8. *
  9. * Copyright (c) 1997
  10. * Moscow Center for SPARC Technology
  11. *
  12. * Copyright (c) 1999
  13. * Boris Fomitchev
  14. *
  15. * This material is provided "as is", with absolutely no warranty expressed
  16. * or implied. Any use is at your own risk.
  17. *
  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.
  23. *
  24. */
  25. #ifndef _STLP_ALGOBASE_C
  26. #define _STLP_ALGOBASE_C
  27. #ifndef _STLP_INTERNAL_ALGOBASE_H
  28. # include <stl/_algobase.h>
  29. #endif
  30. #ifndef _STLP_INTERNAL_FUNCTION_BASE_H
  31. # include <stl/_function_base.h>
  32. #endif
  33. _STLP_BEGIN_NAMESPACE
  34. template <class _InputIter1, class _InputIter2>
  35. bool lexicographical_compare(_InputIter1 __first1, _InputIter1 __last1,
  36. _InputIter2 __first2, _InputIter2 __last2) {
  37. _STLP_DEBUG_CHECK(_STLP_PRIV __check_range(__first1, __last1))
  38. _STLP_DEBUG_CHECK(_STLP_PRIV __check_range(__first2, __last2))
  39. for ( ; __first1 != __last1 && __first2 != __last2
  40. ; ++__first1, ++__first2) {
  41. if (*__first1 < *__first2) {
  42. _STLP_VERBOSE_ASSERT(!(*__first2 < *__first1), _StlMsg_INVALID_STRICT_WEAK_PREDICATE)
  43. return true;
  44. }
  45. if (*__first2 < *__first1)
  46. return false;
  47. }
  48. return __first1 == __last1 && __first2 != __last2;
  49. }
  50. template <class _InputIter1, class _InputIter2, class _Compare>
  51. bool lexicographical_compare(_InputIter1 __first1, _InputIter1 __last1,
  52. _InputIter2 __first2, _InputIter2 __last2,
  53. _Compare __comp) {
  54. _STLP_DEBUG_CHECK(_STLP_PRIV __check_range(__first1, __last1))
  55. _STLP_DEBUG_CHECK(_STLP_PRIV __check_range(__first2, __last2))
  56. for ( ; __first1 != __last1 && __first2 != __last2
  57. ; ++__first1, ++__first2) {
  58. if (__comp(*__first1, *__first2)) {
  59. _STLP_VERBOSE_ASSERT(!__comp(*__first2, *__first1),
  60. _StlMsg_INVALID_STRICT_WEAK_PREDICATE)
  61. return true;
  62. }
  63. if (__comp(*__first2, *__first1))
  64. return false;
  65. }
  66. return __first1 == __last1 && __first2 != __last2;
  67. }
  68. #if !defined (_STLP_NO_EXTENSIONS)
  69. _STLP_MOVE_TO_PRIV_NAMESPACE
  70. template <class _InputIter1, class _InputIter2>
  71. int __lexicographical_compare_3way(_InputIter1 __first1, _InputIter1 __last1,
  72. _InputIter2 __first2, _InputIter2 __last2) {
  73. while (__first1 != __last1 && __first2 != __last2) {
  74. if (*__first1 < *__first2) {
  75. _STLP_VERBOSE_ASSERT(!(*__first2 < *__first1), _StlMsg_INVALID_STRICT_WEAK_PREDICATE)
  76. return -1;
  77. }
  78. if (*__first2 < *__first1)
  79. return 1;
  80. ++__first1;
  81. ++__first2;
  82. }
  83. if (__first2 == __last2) {
  84. return !(__first1 == __last1);
  85. }
  86. else {
  87. return -1;
  88. }
  89. }
  90. _STLP_MOVE_TO_STD_NAMESPACE
  91. template <class _InputIter1, class _InputIter2>
  92. int lexicographical_compare_3way(_InputIter1 __first1, _InputIter1 __last1,
  93. _InputIter2 __first2, _InputIter2 __last2) {
  94. _STLP_DEBUG_CHECK(_STLP_PRIV __check_range(__first1, __last1))
  95. _STLP_DEBUG_CHECK(_STLP_PRIV __check_range(__first2, __last2))
  96. return _STLP_PRIV __lexicographical_compare_3way(__first1, __last1, __first2, __last2);
  97. }
  98. #endif
  99. _STLP_MOVE_TO_PRIV_NAMESPACE
  100. template <class _RandomAccessIter, class _Tp>
  101. _STLP_INLINE_LOOP _RandomAccessIter __find(_RandomAccessIter __first, _RandomAccessIter __last,
  102. const _Tp& __val,
  103. const random_access_iterator_tag &) {
  104. _STLP_DIFFERENCE_TYPE(_RandomAccessIter) __trip_count = (__last - __first) >> 2;
  105. for ( ; __trip_count > 0 ; --__trip_count) {
  106. if (*__first == __val) return __first;
  107. ++__first;
  108. if (*__first == __val) return __first;
  109. ++__first;
  110. if (*__first == __val) return __first;
  111. ++__first;
  112. if (*__first == __val) return __first;
  113. ++__first;
  114. }
  115. switch (__last - __first) {
  116. case 3:
  117. if (*__first == __val) return __first;
  118. ++__first;
  119. case 2:
  120. if (*__first == __val) return __first;
  121. ++__first;
  122. case 1:
  123. if (*__first == __val) return __first;
  124. //++__first;
  125. case 0:
  126. default:
  127. return __last;
  128. }
  129. }
  130. inline char*
  131. __find(char* __first, char* __last, char __val, const random_access_iterator_tag &) {
  132. void *res = memchr(__first, __val, __last - __first);
  133. return res != 0 ? __STATIC_CAST(char*, res) : __last;
  134. }
  135. inline const char*
  136. __find(const char* __first, const char* __last, char __val, const random_access_iterator_tag &) {
  137. const void *res = memchr(__first, __val, __last - __first);
  138. return res != 0 ? __STATIC_CAST(const char*, res) : __last;
  139. }
  140. template <class _RandomAccessIter, class _Predicate>
  141. _STLP_INLINE_LOOP _RandomAccessIter __find_if(_RandomAccessIter __first, _RandomAccessIter __last,
  142. _Predicate __pred,
  143. const random_access_iterator_tag &) {
  144. _STLP_DIFFERENCE_TYPE(_RandomAccessIter) __trip_count = (__last - __first) >> 2;
  145. for ( ; __trip_count > 0 ; --__trip_count) {
  146. if (__pred(*__first)) return __first;
  147. ++__first;
  148. if (__pred(*__first)) return __first;
  149. ++__first;
  150. if (__pred(*__first)) return __first;
  151. ++__first;
  152. if (__pred(*__first)) return __first;
  153. ++__first;
  154. }
  155. switch(__last - __first) {
  156. case 3:
  157. if (__pred(*__first)) return __first;
  158. ++__first;
  159. case 2:
  160. if (__pred(*__first)) return __first;
  161. ++__first;
  162. case 1:
  163. if (__pred(*__first)) return __first;
  164. //++__first;
  165. case 0:
  166. default:
  167. return __last;
  168. }
  169. }
  170. template <class _InputIter, class _Tp>
  171. _STLP_INLINE_LOOP _InputIter __find(_InputIter __first, _InputIter __last,
  172. const _Tp& __val,
  173. const input_iterator_tag &) {
  174. while (__first != __last && !(*__first == __val)) ++__first;
  175. return __first;
  176. }
  177. template <class _InputIter, class _Predicate>
  178. _STLP_INLINE_LOOP _InputIter __find_if(_InputIter __first, _InputIter __last,
  179. _Predicate __pred,
  180. const input_iterator_tag &) {
  181. while (__first != __last && !__pred(*__first))
  182. ++__first;
  183. return __first;
  184. }
  185. _STLP_MOVE_TO_STD_NAMESPACE
  186. template <class _InputIter, class _Predicate>
  187. _InputIter find_if(_InputIter __first, _InputIter __last,
  188. _Predicate __pred) {
  189. _STLP_DEBUG_CHECK(_STLP_PRIV __check_range(__first, __last))
  190. return _STLP_PRIV __find_if(__first, __last, __pred, _STLP_ITERATOR_CATEGORY(__first, _InputIter));
  191. }
  192. template <class _InputIter, class _Tp>
  193. _InputIter find(_InputIter __first, _InputIter __last, const _Tp& __val) {
  194. _STLP_DEBUG_CHECK(_STLP_PRIV __check_range(__first, __last))
  195. return _STLP_PRIV __find(__first, __last, __val, _STLP_ITERATOR_CATEGORY(__first, _InputIter));
  196. }
  197. template <class _ForwardIter1, class _ForwardIter2, class _BinaryPred>
  198. _ForwardIter1 search(_ForwardIter1 __first1, _ForwardIter1 __last1,
  199. _ForwardIter2 __first2, _ForwardIter2 __last2,
  200. _BinaryPred __pred) {
  201. _STLP_DEBUG_CHECK(_STLP_PRIV __check_range(__first1, __last1))
  202. _STLP_DEBUG_CHECK(_STLP_PRIV __check_range(__first2, __last2))
  203. // Test for empty ranges
  204. if (__first1 == __last1 || __first2 == __last2)
  205. return __first1;
  206. // Test for a pattern of length 1.
  207. _ForwardIter2 __p1(__first2);
  208. if ( ++__p1 == __last2 ) {
  209. while (__first1 != __last1 && !__pred(*__first1, *__first2)) {
  210. ++__first1;
  211. }
  212. return __first1;
  213. }
  214. // General case.
  215. for ( ; ; ) { // __first1 != __last1 will be checked below
  216. while (__first1 != __last1 && !__pred(*__first1, *__first2)) {
  217. ++__first1;
  218. }
  219. if (__first1 == __last1) {
  220. return __last1;
  221. }
  222. _ForwardIter2 __p = __p1;
  223. _ForwardIter1 __current = __first1;
  224. if (++__current == __last1) return __last1;
  225. while (__pred(*__current, *__p)) {
  226. if (++__p == __last2)
  227. return __first1;
  228. if (++__current == __last1)
  229. return __last1;
  230. }
  231. ++__first1;
  232. }
  233. //return __first1; /* NOTREACHED */
  234. }
  235. _STLP_MOVE_TO_PRIV_NAMESPACE
  236. template <class _Tp>
  237. struct _IsCharLikeType
  238. { typedef __false_type _Ret; };
  239. _STLP_TEMPLATE_NULL struct _IsCharLikeType<char>
  240. { typedef __true_type _Ret; };
  241. _STLP_TEMPLATE_NULL struct _IsCharLikeType<unsigned char>
  242. { typedef __true_type _Ret; };
  243. # ifndef _STLP_NO_SIGNED_BUILTINS
  244. _STLP_TEMPLATE_NULL struct _IsCharLikeType<signed char>
  245. { typedef __true_type _Ret; };
  246. # endif
  247. template <class _Tp1, class _Tp2>
  248. inline bool __stlp_eq(_Tp1 __val1, _Tp2 __val2)
  249. { return __val1 == __val2; }
  250. #if defined (_STLP_FUNCTION_TMPL_PARTIAL_ORDER)
  251. template <class _Tp>
  252. inline bool __stlp_eq(_Tp, _Tp)
  253. { return true; }
  254. #endif
  255. template <class _InputIter, class _ForwardIter, class _Tp2, class _Predicate>
  256. inline _InputIter __find_first_of_aux2(_InputIter __first1, _InputIter __last1,
  257. _ForwardIter __first2, _ForwardIter __last2,
  258. _Tp2*, _Predicate __pred,
  259. const __true_type& /* _UseStrcspnLikeAlgo */) {
  260. unsigned char __hints[(UCHAR_MAX + 1) / CHAR_BIT];
  261. memset(__hints, 0, sizeof(__hints) / sizeof(unsigned char));
  262. for (; __first2 != __last2; ++__first2) {
  263. unsigned char __tmp = (unsigned char)*__first2;
  264. __hints[__tmp / CHAR_BIT] |= (1 << (__tmp % CHAR_BIT));
  265. }
  266. for (; __first1 != __last1; ++__first1) {
  267. _Tp2 __tmp = (_Tp2)*__first1;
  268. if (__stlp_eq(*__first1, __tmp) &&
  269. __pred((__hints[(unsigned char)__tmp / CHAR_BIT] & (1 << ((unsigned char)__tmp % CHAR_BIT))) != 0))
  270. break;
  271. }
  272. return __first1;
  273. }
  274. template <class _InputIter, class _ForwardIter, class _Tp2, class _Predicate>
  275. inline _InputIter __find_first_of_aux2(_InputIter __first1, _InputIter __last1,
  276. _ForwardIter __first2, _ForwardIter __last2,
  277. _Tp2* /* __dummy */, _Predicate /* __pred */,
  278. const __false_type& /* _UseStrcspnLikeAlgo */) {
  279. return _STLP_PRIV __find_first_of(__first1, __last1, __first2, __last2,
  280. _STLP_PRIV __equal_to(_STLP_VALUE_TYPE(__first1, _InputIter)));
  281. }
  282. template <class _InputIter, class _ForwardIter, class _Tp1, class _Tp2>
  283. inline _InputIter __find_first_of_aux1(_InputIter __first1, _InputIter __last1,
  284. _ForwardIter __first2, _ForwardIter __last2,
  285. _Tp1* __pt1, _Tp2* __pt2) {
  286. typedef _STLP_TYPENAME _STLP_STD::_IsIntegral<_Tp1>::_Ret _IsIntegral;
  287. typedef _STLP_TYPENAME _STLP_PRIV _IsCharLikeType<_Tp2>::_Ret _IsCharLike;
  288. typedef _STLP_TYPENAME _STLP_STD::_Land2<_IsIntegral, _IsCharLike>::_Ret _UseStrcspnLikeAlgo;
  289. _STLP_MARK_PARAMETER_AS_UNUSED(__pt1);
  290. return _STLP_PRIV __find_first_of_aux2(__first1, __last1,
  291. __first2, __last2,
  292. __pt2, _Identity<bool>(), _UseStrcspnLikeAlgo());
  293. }
  294. template <class _InputIter, class _ForwardIter>
  295. inline _InputIter __find_first_of(_InputIter __first1, _InputIter __last1,
  296. _ForwardIter __first2, _ForwardIter __last2) {
  297. return _STLP_PRIV __find_first_of_aux1(__first1, __last1, __first2, __last2,
  298. _STLP_VALUE_TYPE(__first1, _InputIter),
  299. _STLP_VALUE_TYPE(__first2, _ForwardIter));
  300. }
  301. // find_first_of, with and without an explicitly supplied comparison function.
  302. template <class _InputIter, class _ForwardIter, class _BinaryPredicate>
  303. _InputIter __find_first_of(_InputIter __first1, _InputIter __last1,
  304. _ForwardIter __first2, _ForwardIter __last2,
  305. _BinaryPredicate __comp) {
  306. for ( ; __first1 != __last1; ++__first1) {
  307. for (_ForwardIter __iter = __first2; __iter != __last2; ++__iter) {
  308. if (__comp(*__first1, *__iter)) {
  309. return __first1;
  310. }
  311. }
  312. }
  313. return __last1;
  314. }
  315. // find_end, with and without an explicitly supplied comparison function.
  316. // Search [first2, last2) as a subsequence in [first1, last1), and return
  317. // the *last* possible match. Note that find_end for bidirectional iterators
  318. // is much faster than for forward iterators.
  319. // find_end for forward iterators.
  320. template <class _ForwardIter1, class _ForwardIter2,
  321. class _BinaryPredicate>
  322. _ForwardIter1 __find_end(_ForwardIter1 __first1, _ForwardIter1 __last1,
  323. _ForwardIter2 __first2, _ForwardIter2 __last2,
  324. const forward_iterator_tag &, const forward_iterator_tag &,
  325. _BinaryPredicate __comp) {
  326. if (__first2 == __last2)
  327. return __last1;
  328. else {
  329. _ForwardIter1 __result = __last1;
  330. for (;;) {
  331. _ForwardIter1 __new_result = _STLP_STD::search(__first1, __last1, __first2, __last2, __comp);
  332. if (__new_result == __last1)
  333. return __result;
  334. else {
  335. __result = __new_result;
  336. __first1 = __new_result;
  337. ++__first1;
  338. }
  339. }
  340. }
  341. }
  342. _STLP_MOVE_TO_STD_NAMESPACE
  343. // find_end for bidirectional iterators. Requires partial specialization.
  344. #if defined (_STLP_CLASS_PARTIAL_SPECIALIZATION)
  345. # ifndef _STLP_INTERNAL_ITERATOR_H
  346. _STLP_END_NAMESPACE
  347. # include <stl/_iterator.h>
  348. _STLP_BEGIN_NAMESPACE
  349. # endif /*_STLP_INTERNAL_ITERATOR_H*/
  350. _STLP_MOVE_TO_PRIV_NAMESPACE
  351. template <class _BidirectionalIter1, class _BidirectionalIter2,
  352. class _BinaryPredicate>
  353. _BidirectionalIter1
  354. __find_end(_BidirectionalIter1 __first1, _BidirectionalIter1 __last1,
  355. _BidirectionalIter2 __first2, _BidirectionalIter2 __last2,
  356. const bidirectional_iterator_tag &, const bidirectional_iterator_tag &,
  357. _BinaryPredicate __comp) {
  358. typedef _STLP_STD::reverse_iterator<_BidirectionalIter1> _RevIter1;
  359. typedef _STLP_STD::reverse_iterator<_BidirectionalIter2> _RevIter2;
  360. _RevIter1 __rlast1(__first1);
  361. _RevIter2 __rlast2(__first2);
  362. _RevIter1 __rresult = _STLP_STD::search(_RevIter1(__last1), __rlast1,
  363. _RevIter2(__last2), __rlast2,
  364. __comp);
  365. if (__rresult == __rlast1)
  366. return __last1;
  367. else {
  368. _BidirectionalIter1 __result = __rresult.base();
  369. _STLP_STD::advance(__result, -_STLP_STD::distance(__first2, __last2));
  370. return __result;
  371. }
  372. }
  373. _STLP_MOVE_TO_STD_NAMESPACE
  374. #endif /* _STLP_CLASS_PARTIAL_SPECIALIZATION */
  375. template <class _ForwardIter1, class _ForwardIter2,
  376. class _BinaryPredicate>
  377. _ForwardIter1
  378. find_end(_ForwardIter1 __first1, _ForwardIter1 __last1,
  379. _ForwardIter2 __first2, _ForwardIter2 __last2,
  380. _BinaryPredicate __comp) {
  381. _STLP_DEBUG_CHECK(_STLP_PRIV __check_range(__first1, __last1))
  382. _STLP_DEBUG_CHECK(_STLP_PRIV __check_range(__first2, __last2))
  383. return _STLP_PRIV __find_end(__first1, __last1, __first2, __last2,
  384. #if defined (_STLP_CLASS_PARTIAL_SPECIALIZATION)
  385. _STLP_ITERATOR_CATEGORY(__first1, _ForwardIter1),
  386. _STLP_ITERATOR_CATEGORY(__first2, _ForwardIter2),
  387. #else
  388. forward_iterator_tag(),
  389. forward_iterator_tag(),
  390. #endif
  391. __comp);
  392. }
  393. _STLP_MOVE_TO_PRIV_NAMESPACE
  394. template <class _ForwardIter, class _Tp, class _Compare1, class _Compare2, class _Distance>
  395. _ForwardIter __lower_bound(_ForwardIter __first, _ForwardIter __last, const _Tp& __val,
  396. _Compare1 __comp1, _Compare2 __comp2, _Distance*) {
  397. _Distance __len = _STLP_STD::distance(__first, __last);
  398. _Distance __half;
  399. _ForwardIter __middle;
  400. while (__len > 0) {
  401. __half = __len >> 1;
  402. __middle = __first;
  403. _STLP_STD::advance(__middle, __half);
  404. if (__comp1(*__middle, __val)) {
  405. _STLP_VERBOSE_ASSERT(!__comp2(__val, *__middle), _StlMsg_INVALID_STRICT_WEAK_PREDICATE)
  406. __first = __middle;
  407. ++__first;
  408. __len = __len - __half - 1;
  409. }
  410. else
  411. __len = __half;
  412. }
  413. return __first;
  414. }
  415. _STLP_MOVE_TO_STD_NAMESPACE
  416. _STLP_END_NAMESPACE
  417. #endif /* _STLP_ALGOBASE_C */
  418. // Local Variables:
  419. // mode:C++
  420. // End: