_algobase.h 27 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728
  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. /* NOTE: This is an internal header file, included by other STL headers.
  26. * You should not attempt to use it directly.
  27. */
  28. #ifndef _STLP_INTERNAL_ALGOBASE_H
  29. #define _STLP_INTERNAL_ALGOBASE_H
  30. #ifndef _STLP_INTERNAL_CSTDDEF
  31. # include <stl/_cstddef.h>
  32. #endif
  33. #ifndef _STLP_INTERNAL_CSTRING
  34. # include <stl/_cstring.h>
  35. #endif
  36. #ifndef _STLP_CLIMITS
  37. # include <climits>
  38. #endif
  39. #ifndef _STLP_INTERNAL_CSTDLIB
  40. # include <stl/_cstdlib.h>
  41. #endif
  42. #ifndef _STLP_INTERNAL_PAIR_H
  43. # include <stl/_pair.h>
  44. #endif
  45. #ifndef _STLP_INTERNAL_ITERATOR_BASE_H
  46. # include <stl/_iterator_base.h>
  47. #endif
  48. #ifndef _STLP_TYPE_TRAITS_H
  49. # include <stl/type_traits.h>
  50. #endif
  51. _STLP_BEGIN_NAMESPACE
  52. #if defined (_STLP_USE_PARTIAL_SPEC_WORKAROUND) && !defined (_STLP_FUNCTION_TMPL_PARTIAL_ORDER)
  53. _STLP_MOVE_TO_PRIV_NAMESPACE
  54. template <class _Tp>
  55. inline void __swap_aux(_Tp& __a, _Tp& __b, const __true_type& /*SwapImplemented*/) {
  56. __a._M_swap_workaround(__b);
  57. }
  58. template <class _Tp>
  59. inline void __swap_aux(_Tp& __a, _Tp& __b, const __false_type& /*SwapImplemented*/) {
  60. _Tp __tmp = __a;
  61. __a = __b;
  62. __b = __tmp;
  63. }
  64. _STLP_MOVE_TO_STD_NAMESPACE
  65. #endif
  66. // swap and iter_swap
  67. template <class _Tp>
  68. inline void swap(_Tp& __a, _Tp& __b) {
  69. #if defined (_STLP_USE_PARTIAL_SPEC_WORKAROUND) && !defined (_STLP_FUNCTION_TMPL_PARTIAL_ORDER)
  70. # if !defined(__BORLANDC__)
  71. typedef typename _SwapImplemented<_Tp>::_Ret _Implemented;
  72. # else
  73. enum { _Is = _SwapImplemented<_Tp>::_Is };
  74. typedef typename __bool2type<_Is>::_Ret _Implemented;
  75. # endif
  76. _STLP_PRIV __swap_aux(__a, __b, _Implemented());
  77. #else
  78. _Tp __tmp = __a;
  79. __a = __b;
  80. __b = __tmp;
  81. #endif
  82. }
  83. _STLP_MOVE_TO_PRIV_NAMESPACE
  84. template <class _ForwardIter1, class _ForwardIter2, class _Value>
  85. inline void __iter_swap_aux_aux(_ForwardIter1& __i1, _ForwardIter2& __i2, _Value *) {
  86. _Value tmp = *__i1;
  87. *__i1 = *__i2;
  88. *__i2 = tmp;
  89. }
  90. template <class _ForwardIter1, class _ForwardIter2>
  91. inline void __iter_swap_aux(_ForwardIter1& __i1, _ForwardIter2& __i2, const __true_type& /*OKToSwap*/) {
  92. /* namespace specification breaks access to the right swap template overload (at least for gcc) */
  93. /*_STLP_STD::*/ swap(*__i1, *__i2);
  94. }
  95. template <class _ForwardIter1, class _ForwardIter2>
  96. inline void __iter_swap_aux(_ForwardIter1& __i1, _ForwardIter2& __i2, const __false_type& /*OKToSwap*/) {
  97. _STLP_PRIV __iter_swap_aux_aux( __i1, __i2, _STLP_VALUE_TYPE(__i1,_ForwardIter1) );
  98. }
  99. _STLP_MOVE_TO_STD_NAMESPACE
  100. template <class _ForwardIter1, class _ForwardIter2>
  101. inline void iter_swap(_ForwardIter1 __i1, _ForwardIter2 __i2) {
  102. _STLP_PRIV __iter_swap_aux( __i1, __i2, _IsOKToSwap(_STLP_VALUE_TYPE(__i1, _ForwardIter1), _STLP_VALUE_TYPE(__i2, _ForwardIter2),
  103. _STLP_IS_REF_TYPE_REAL_REF(__i1, _ForwardIter1),
  104. _STLP_IS_REF_TYPE_REAL_REF(__i2, _ForwardIter2))._Answer());
  105. }
  106. //--------------------------------------------------
  107. // min and max
  108. #if !defined (__BORLANDC__) || defined (_STLP_USE_OWN_NAMESPACE)
  109. # if (defined (__BORLANDC__) && (__BORLANDC__ < 0x580)) && !defined (__STDC__)
  110. //In not ANSI mode Borland import min/max in global namespace which conflict
  111. //with STLport min/max when user does a 'using namespace std' in its code
  112. //(see test/unit/alg_test.cpp). To avoid this clash we simply import Borland min/max
  113. //in STLport namespace.
  114. using _STLP_VENDOR_STD::min;
  115. using _STLP_VENDOR_STD::max;
  116. # else
  117. template <class _Tp>
  118. inline const _Tp& (min)(const _Tp& __a, const _Tp& __b) { return __b < __a ? __b : __a; }
  119. template <class _Tp>
  120. inline const _Tp& (max)(const _Tp& __a, const _Tp& __b) { return __a < __b ? __b : __a; }
  121. # endif
  122. #endif
  123. # if defined (__BORLANDC__) && defined (_STLP_USE_OWN_NAMESPACE)
  124. inline unsigned long (min) (unsigned long __a, unsigned long __b) { return __b < __a ? __b : __a; }
  125. inline unsigned long (max) (unsigned long __a, unsigned long __b) { return __a < __b ? __b : __a; }
  126. # endif
  127. # if !defined (__BORLANDC__) || (__BORLANDC__ < 0x590)
  128. template <class _Tp, class _Compare>
  129. inline const _Tp& (min)(const _Tp& __a, const _Tp& __b, _Compare __comp) {
  130. return __comp(__b, __a) ? __b : __a;
  131. }
  132. template <class _Tp, class _Compare>
  133. inline const _Tp& (max)(const _Tp& __a, const _Tp& __b, _Compare __comp) {
  134. return __comp(__a, __b) ? __b : __a;
  135. }
  136. # else
  137. template <class _Tp, class _Compare>
  138. inline const _Tp (min)(const _Tp __a, const _Tp __b, _Compare __comp) {
  139. return __comp(__b, __a) ? __b : __a;
  140. }
  141. template <class _Tp, class _Compare>
  142. inline const _Tp (max)(const _Tp __a, const _Tp __b, _Compare __comp) {
  143. return __comp(__a, __b) ? __b : __a;
  144. }
  145. # endif
  146. //--------------------------------------------------
  147. // copy
  148. // All of these auxiliary functions serve two purposes. (1) Replace
  149. // calls to copy with memmove whenever possible. (Memmove, not memcpy,
  150. // because the input and output ranges are permitted to overlap.)
  151. // (2) If we're using random access iterators, then write the loop as
  152. // a for loop with an explicit count.
  153. _STLP_MOVE_TO_PRIV_NAMESPACE
  154. template <class _InputIter, class _OutputIter, class _Distance>
  155. inline _OutputIter __copy(_InputIter __first, _InputIter __last,
  156. _OutputIter __result, const input_iterator_tag &, _Distance*) {
  157. for ( ; __first != __last; ++__result, ++__first)
  158. *__result = *__first;
  159. return __result;
  160. }
  161. #if defined (_STLP_NONTEMPL_BASE_MATCH_BUG)
  162. template <class _InputIter, class _OutputIter, class _Distance>
  163. inline _OutputIter __copy(_InputIter __first, _InputIter __last,
  164. _OutputIter __result, const forward_iterator_tag &, _Distance* ) {
  165. for ( ; __first != __last; ++__result, ++__first)
  166. *__result = *__first;
  167. return __result;
  168. }
  169. template <class _InputIter, class _OutputIter, class _Distance>
  170. inline _OutputIter __copy(_InputIter __first, _InputIter __last,
  171. _OutputIter __result, const bidirectional_iterator_tag &, _Distance* ) {
  172. for ( ; __first != __last; ++__result, ++__first)
  173. *__result = *__first;
  174. return __result;
  175. }
  176. #endif
  177. template <class _RandomAccessIter, class _OutputIter, class _Distance>
  178. inline _OutputIter
  179. __copy(_RandomAccessIter __first, _RandomAccessIter __last,
  180. _OutputIter __result, const random_access_iterator_tag &, _Distance*) {
  181. for (_Distance __n = __last - __first; __n > 0; --__n) {
  182. *__result = *__first;
  183. ++__first;
  184. ++__result;
  185. }
  186. return __result;
  187. }
  188. inline void*
  189. __copy_trivial(const void* __first, const void* __last, void* __result) {
  190. size_t __n = (const char*)__last - (const char*)__first;
  191. return __n ? (void *)((char*)memmove(__result, __first, __n) + __n) : __result;
  192. }
  193. //--------------------------------------------------
  194. // copy_backward auxiliary functions
  195. template <class _BidirectionalIter1, class _BidirectionalIter2,
  196. class _Distance>
  197. inline _BidirectionalIter2 __copy_backward(_BidirectionalIter1 __first,
  198. _BidirectionalIter1 __last,
  199. _BidirectionalIter2 __result,
  200. const bidirectional_iterator_tag &,
  201. _Distance*) {
  202. while (__first != __last)
  203. *--__result = *--__last;
  204. return __result;
  205. }
  206. template <class _RandomAccessIter, class _BidirectionalIter, class _Distance>
  207. inline _BidirectionalIter __copy_backward(_RandomAccessIter __first,
  208. _RandomAccessIter __last,
  209. _BidirectionalIter __result,
  210. const random_access_iterator_tag &,
  211. _Distance*) {
  212. for (_Distance __n = __last - __first; __n > 0; --__n)
  213. *--__result = *--__last;
  214. return __result;
  215. }
  216. inline void*
  217. __copy_trivial_backward(const void* __first, const void* __last, void* __result) {
  218. const ptrdiff_t _Num = (const char*)__last - (const char*)__first;
  219. return (_Num > 0) ? memmove((char*)__result - _Num, __first, _Num) : __result ;
  220. }
  221. template <class _InputIter, class _OutputIter>
  222. inline _OutputIter __copy_ptrs(_InputIter __first, _InputIter __last, _OutputIter __result,
  223. const __false_type& /*IsOKToMemCpy*/) {
  224. return _STLP_PRIV __copy(__first, __last, __result, random_access_iterator_tag(), (ptrdiff_t*)0);
  225. }
  226. template <class _InputIter, class _OutputIter>
  227. inline _OutputIter __copy_ptrs(_InputIter __first, _InputIter __last, _OutputIter __result,
  228. const __true_type& /*IsOKToMemCpy*/) {
  229. // we know they all pointers, so this cast is OK
  230. // return (_OutputIter)__copy_trivial(&(*__first), &(*__last), &(*__result));
  231. return (_OutputIter)_STLP_PRIV __copy_trivial(__first, __last, __result);
  232. }
  233. template <class _InputIter, class _OutputIter>
  234. inline _OutputIter __copy_aux(_InputIter __first, _InputIter __last, _OutputIter __result,
  235. const __true_type& /*BothPtrType*/) {
  236. return _STLP_PRIV __copy_ptrs(__first, __last, __result,
  237. _UseTrivialCopy(_STLP_VALUE_TYPE(__first, _InputIter),
  238. _STLP_VALUE_TYPE(__result, _OutputIter))._Answer());
  239. }
  240. template <class _InputIter, class _OutputIter>
  241. inline _OutputIter __copy_aux(_InputIter __first, _InputIter __last, _OutputIter __result,
  242. const __false_type& /*BothPtrType*/) {
  243. return _STLP_PRIV __copy(__first, __last, __result,
  244. _STLP_ITERATOR_CATEGORY(__first, _InputIter),
  245. _STLP_DISTANCE_TYPE(__first, _InputIter));
  246. }
  247. _STLP_MOVE_TO_STD_NAMESPACE
  248. template <class _InputIter, class _OutputIter>
  249. inline _OutputIter copy(_InputIter __first, _InputIter __last, _OutputIter __result) {
  250. _STLP_DEBUG_CHECK(_STLP_PRIV __check_range(__first, __last))
  251. return _STLP_PRIV __copy_aux(__first, __last, __result, _BothPtrType< _InputIter, _OutputIter>::_Answer());
  252. }
  253. _STLP_MOVE_TO_PRIV_NAMESPACE
  254. template <class _InputIter, class _OutputIter>
  255. inline _OutputIter __copy_backward_ptrs(_InputIter __first, _InputIter __last,
  256. _OutputIter __result, const __false_type& /*TrivialAssignment*/) {
  257. return _STLP_PRIV __copy_backward(__first, __last, __result,
  258. _STLP_ITERATOR_CATEGORY(__first, _InputIter),
  259. _STLP_DISTANCE_TYPE(__first, _InputIter));
  260. }
  261. template <class _InputIter, class _OutputIter>
  262. inline _OutputIter __copy_backward_ptrs(_InputIter __first, _InputIter __last,
  263. _OutputIter __result, const __true_type& /*TrivialAssignment*/) {
  264. return (_OutputIter)_STLP_PRIV __copy_trivial_backward(__first, __last, __result);
  265. }
  266. template <class _InputIter, class _OutputIter>
  267. inline _OutputIter __copy_backward_aux(_InputIter __first, _InputIter __last, _OutputIter __result, const __false_type&) {
  268. return _STLP_PRIV __copy_backward(__first, __last, __result,
  269. _STLP_ITERATOR_CATEGORY(__first,_InputIter),
  270. _STLP_DISTANCE_TYPE(__first, _InputIter));
  271. }
  272. template <class _InputIter, class _OutputIter>
  273. inline _OutputIter __copy_backward_aux(_InputIter __first, _InputIter __last, _OutputIter __result, const __true_type&) {
  274. return _STLP_PRIV __copy_backward_ptrs(__first, __last, __result,
  275. _UseTrivialCopy(_STLP_VALUE_TYPE(__first, _InputIter),
  276. _STLP_VALUE_TYPE(__result, _OutputIter))._Answer());
  277. }
  278. _STLP_MOVE_TO_STD_NAMESPACE
  279. template <class _InputIter, class _OutputIter>
  280. inline _OutputIter copy_backward(_InputIter __first, _InputIter __last, _OutputIter __result) {
  281. _STLP_DEBUG_CHECK(_STLP_PRIV __check_range(__first, __last))
  282. return _STLP_PRIV __copy_backward_aux(__first, __last, __result, _BothPtrType< _InputIter, _OutputIter>::_Answer() );
  283. }
  284. #if !defined (_STLP_CLASS_PARTIAL_SPECIALIZATION) && !defined (_STLP_SIMULATE_PARTIAL_SPEC_FOR_TYPE_TRAITS)
  285. # define _STLP_DECLARE_COPY_TRIVIAL(_Tp) \
  286. inline _Tp* copy(const _Tp* __first, const _Tp* __last, _Tp* __result) \
  287. { return (_Tp*)_STLP_PRIV __copy_trivial(__first, __last, __result); } \
  288. inline _Tp* copy_backward(const _Tp* __first, const _Tp* __last, _Tp* __result) \
  289. { return (_Tp*)_STLP_PRIV __copy_trivial_backward(__first, __last, __result); }
  290. # if !defined (_STLP_NO_BOOL)
  291. _STLP_DECLARE_COPY_TRIVIAL(bool)
  292. # endif
  293. _STLP_DECLARE_COPY_TRIVIAL(char)
  294. # if !defined (_STLP_NO_SIGNED_BUILTINS)
  295. _STLP_DECLARE_COPY_TRIVIAL(signed char)
  296. # endif
  297. _STLP_DECLARE_COPY_TRIVIAL(unsigned char)
  298. _STLP_DECLARE_COPY_TRIVIAL(short)
  299. _STLP_DECLARE_COPY_TRIVIAL(unsigned short)
  300. _STLP_DECLARE_COPY_TRIVIAL(int)
  301. _STLP_DECLARE_COPY_TRIVIAL(unsigned int)
  302. _STLP_DECLARE_COPY_TRIVIAL(long)
  303. _STLP_DECLARE_COPY_TRIVIAL(unsigned long)
  304. # if !defined(_STLP_NO_WCHAR_T) && !defined (_STLP_WCHAR_T_IS_USHORT)
  305. _STLP_DECLARE_COPY_TRIVIAL(wchar_t)
  306. # endif
  307. # if defined (_STLP_LONG_LONG)
  308. _STLP_DECLARE_COPY_TRIVIAL(_STLP_LONG_LONG)
  309. _STLP_DECLARE_COPY_TRIVIAL(unsigned _STLP_LONG_LONG)
  310. # endif
  311. _STLP_DECLARE_COPY_TRIVIAL(float)
  312. _STLP_DECLARE_COPY_TRIVIAL(double)
  313. # if !defined (_STLP_NO_LONG_DOUBLE)
  314. _STLP_DECLARE_COPY_TRIVIAL(long double)
  315. # endif
  316. # undef _STLP_DECLARE_COPY_TRIVIAL
  317. #endif
  318. //--------------------------------------------------
  319. // copy_n (not part of the C++ standard)
  320. #if !defined (_STLP_NO_EXTENSIONS)
  321. _STLP_MOVE_TO_PRIV_NAMESPACE
  322. template <class _InputIter, class _Size, class _OutputIter>
  323. _STLP_INLINE_LOOP _STLP_STD::pair<_InputIter, _OutputIter>
  324. __copy_n(_InputIter __first, _Size __count, _OutputIter __result,
  325. const input_iterator_tag &) {
  326. for ( ; __count > 0; --__count) {
  327. *__result = *__first;
  328. ++__first;
  329. ++__result;
  330. }
  331. return _STLP_STD::pair<_InputIter, _OutputIter>(__first, __result);
  332. }
  333. template <class _RAIter, class _Size, class _OutputIter>
  334. inline _STLP_STD::pair<_RAIter, _OutputIter>
  335. __copy_n(_RAIter __first, _Size __count, _OutputIter __result,
  336. const random_access_iterator_tag &) {
  337. _RAIter __last = __first + __count;
  338. return _STLP_STD::pair<_RAIter, _OutputIter>(__last, _STLP_STD::copy(__first, __last, __result));
  339. }
  340. _STLP_MOVE_TO_STD_NAMESPACE
  341. template <class _InputIter, class _Size, class _OutputIter>
  342. inline pair<_InputIter, _OutputIter>
  343. copy_n(_InputIter __first, _Size __count, _OutputIter __result) {
  344. _STLP_FIX_LITERAL_BUG(__first)
  345. return _STLP_PRIV __copy_n(__first, __count, __result, _STLP_ITERATOR_CATEGORY(__first, _InputIter));
  346. }
  347. #endif
  348. //--------------------------------------------------
  349. // fill and fill_n
  350. _STLP_MOVE_TO_PRIV_NAMESPACE
  351. template <class _ForwardIter, class _Tp>
  352. _STLP_INLINE_LOOP
  353. void __fill_fwd(_ForwardIter __first, _ForwardIter __last, const _Tp& __val) {
  354. for ( ; __first != __last; ++__first)
  355. *__first = __val;
  356. }
  357. template <class _ForwardIter, class _Tp, class _Distance>
  358. inline void __fill(_ForwardIter __first, _ForwardIter __last, const _Tp& __val,
  359. const input_iterator_tag &, _Distance*) {
  360. _STLP_PRIV __fill_fwd(__first, __last, __val);
  361. }
  362. #if defined (_STLP_NONTEMPL_BASE_MATCH_BUG)
  363. template <class _ForwardIter, class _Tp, class _Distance>
  364. _STLP_INLINE_LOOP
  365. void __fill(_ForwardIter __first, _ForwardIter __last, const _Tp& __val,
  366. const forward_iterator_tag &, _Distance*) {
  367. _STLP_PRIV __fill_fwd(__first, __last, __val);
  368. }
  369. template <class _ForwardIter, class _Tp, class _Distance>
  370. _STLP_INLINE_LOOP
  371. void __fill(_ForwardIter __first, _ForwardIter __last, const _Tp& __val,
  372. const bidirectional_iterator_tag &, _Distance*) {
  373. _STLP_PRIV __fill_fwd(__first, __last, __val);
  374. }
  375. #endif
  376. template <class _RandomAccessIter, class _Tp, class _Distance>
  377. _STLP_INLINE_LOOP
  378. void __fill(_RandomAccessIter __first, _RandomAccessIter __last, const _Tp& __val,
  379. const random_access_iterator_tag &, _Distance*) {
  380. for (_Distance __n = __last - __first ; __n > 0; ++__first, --__n)
  381. *__first = __val;
  382. }
  383. _STLP_MOVE_TO_STD_NAMESPACE
  384. template <class _ForwardIter, class _Tp>
  385. inline void fill(_ForwardIter __first, _ForwardIter __last, const _Tp& __val) {
  386. _STLP_DEBUG_CHECK(_STLP_PRIV __check_range(__first, __last))
  387. _STLP_PRIV __fill(__first, __last, __val,
  388. _STLP_ITERATOR_CATEGORY(__first, _ForwardIter),
  389. _STLP_DISTANCE_TYPE(__first, _ForwardIter));
  390. }
  391. // Specialization: for one-byte types we can use memset.
  392. inline void fill(unsigned char* __first, unsigned char* __last,
  393. const unsigned char& __val) {
  394. unsigned char __tmp = __val;
  395. memset(__first, __tmp, __last - __first);
  396. }
  397. #if !defined (_STLP_NO_SIGNED_BUILTINS)
  398. inline void fill(signed char* __first, signed char* __last,
  399. const signed char& __val) {
  400. signed char __tmp = __val;
  401. memset(__first, __STATIC_CAST(unsigned char,__tmp), __last - __first);
  402. }
  403. #endif
  404. inline void fill(char* __first, char* __last, const char& __val) {
  405. char __tmp = __val;
  406. memset(__first, __STATIC_CAST(unsigned char,__tmp), __last - __first);
  407. }
  408. _STLP_MOVE_TO_PRIV_NAMESPACE
  409. template <class _OutputIter, class _Size, class _Tp>
  410. _STLP_INLINE_LOOP
  411. _OutputIter __fill_n(_OutputIter __first, _Size __n, const _Tp& __val) {
  412. _STLP_FIX_LITERAL_BUG(__first)
  413. for ( ; __n > 0; --__n, ++__first)
  414. *__first = __val;
  415. return __first;
  416. }
  417. #if defined (_STLP_FUNCTION_TMPL_PARTIAL_ORDER)
  418. template <class _Size>
  419. inline unsigned char* __fill_n(unsigned char* __first, _Size __n,
  420. const unsigned char& __val) {
  421. _STLP_STD::fill(__first, __first + __n, __val);
  422. return __first + __n;
  423. }
  424. #if !defined (_STLP_NO_SIGNED_BUILTINS)
  425. template <class _Size>
  426. inline signed char* __fill_n(signed char* __first, _Size __n,
  427. const signed char& __val) {
  428. _STLP_STD::fill(__first, __first + __n, __val);
  429. return __first + __n;
  430. }
  431. #endif
  432. template <class _Size>
  433. inline char* __fill_n(char* __first, _Size __n,
  434. const char& __val) {
  435. _STLP_STD::fill(__first, __first + __n, __val);
  436. return __first + __n;
  437. }
  438. #endif
  439. _STLP_MOVE_TO_STD_NAMESPACE
  440. template <class _OutputIter, class _Size, class _Tp>
  441. inline void fill_n(_OutputIter __first, _Size __n, const _Tp& __val) {
  442. _STLP_FIX_LITERAL_BUG(__first)
  443. _STLP_PRIV __fill_n(__first, __n, __val);
  444. }
  445. //--------------------------------------------------
  446. // equal and mismatch
  447. template <class _InputIter1, class _InputIter2>
  448. _STLP_INLINE_LOOP
  449. _STLP_STD::pair<_InputIter1, _InputIter2> mismatch(_InputIter1 __first1,
  450. _InputIter1 __last1,
  451. _InputIter2 __first2) {
  452. _STLP_FIX_LITERAL_BUG(__first2)
  453. _STLP_DEBUG_CHECK(_STLP_PRIV __check_range(__first1, __last1))
  454. while (__first1 != __last1 && *__first1 == *__first2) {
  455. ++__first1;
  456. ++__first2;
  457. }
  458. return _STLP_STD::pair<_InputIter1, _InputIter2>(__first1, __first2);
  459. }
  460. template <class _InputIter1, class _InputIter2, class _BinaryPredicate>
  461. _STLP_INLINE_LOOP
  462. _STLP_STD::pair<_InputIter1, _InputIter2> mismatch(_InputIter1 __first1,
  463. _InputIter1 __last1,
  464. _InputIter2 __first2,
  465. _BinaryPredicate __binary_pred) {
  466. _STLP_FIX_LITERAL_BUG(__first2)
  467. _STLP_DEBUG_CHECK(_STLP_PRIV __check_range(__first1, __last1))
  468. while (__first1 != __last1 && __binary_pred(*__first1, *__first2)) {
  469. ++__first1;
  470. ++__first2;
  471. }
  472. return _STLP_STD::pair<_InputIter1, _InputIter2>(__first1, __first2);
  473. }
  474. template <class _InputIter1, class _InputIter2>
  475. _STLP_INLINE_LOOP
  476. bool equal(_InputIter1 __first1, _InputIter1 __last1,
  477. _InputIter2 __first2) {
  478. _STLP_FIX_LITERAL_BUG(__first1) _STLP_FIX_LITERAL_BUG(__last1) _STLP_FIX_LITERAL_BUG(__first2)
  479. _STLP_DEBUG_CHECK(_STLP_PRIV __check_range(__first1, __last1))
  480. for ( ; __first1 != __last1; ++__first1, ++__first2)
  481. if (!(*__first1 == *__first2))
  482. return false;
  483. return true;
  484. }
  485. template <class _InputIter1, class _InputIter2, class _BinaryPredicate>
  486. _STLP_INLINE_LOOP
  487. bool equal(_InputIter1 __first1, _InputIter1 __last1,
  488. _InputIter2 __first2, _BinaryPredicate __binary_pred) {
  489. _STLP_FIX_LITERAL_BUG(__first2)
  490. _STLP_DEBUG_CHECK(_STLP_PRIV __check_range(__first1, __last1))
  491. for ( ; __first1 != __last1; ++__first1, ++__first2)
  492. if (!__binary_pred(*__first1, *__first2))
  493. return false;
  494. return true;
  495. }
  496. //--------------------------------------------------
  497. // lexicographical_compare and lexicographical_compare_3way.
  498. // (the latter is not part of the C++ standard.)
  499. template <class _InputIter1, class _InputIter2>
  500. bool lexicographical_compare(_InputIter1 __first1, _InputIter1 __last1,
  501. _InputIter2 __first2, _InputIter2 __last2);
  502. template <class _InputIter1, class _InputIter2, class _Compare>
  503. bool lexicographical_compare(_InputIter1 __first1, _InputIter1 __last1,
  504. _InputIter2 __first2, _InputIter2 __last2,
  505. _Compare __comp);
  506. inline bool
  507. lexicographical_compare(const unsigned char* __first1,
  508. const unsigned char* __last1,
  509. const unsigned char* __first2,
  510. const unsigned char* __last2) {
  511. const size_t __len1 = __last1 - __first1;
  512. const size_t __len2 = __last2 - __first2;
  513. _STLP_DEBUG_CHECK(_STLP_PRIV __check_range(__first1, __last1))
  514. _STLP_DEBUG_CHECK(_STLP_PRIV __check_range(__first2, __last2))
  515. const int __result = memcmp(__first1, __first2, (min) (__len1, __len2));
  516. return __result != 0 ? (__result < 0) : (__len1 < __len2);
  517. }
  518. #if !(CHAR_MAX == SCHAR_MAX)
  519. inline bool lexicographical_compare(const char* __first1, const char* __last1,
  520. const char* __first2, const char* __last2) {
  521. _STLP_DEBUG_CHECK(_STLP_PRIV __check_range(__first1, __last1))
  522. _STLP_DEBUG_CHECK(_STLP_PRIV __check_range(__first2, __last2))
  523. return lexicographical_compare((const unsigned char*) __first1,
  524. (const unsigned char*) __last1,
  525. (const unsigned char*) __first2,
  526. (const unsigned char*) __last2);
  527. }
  528. #endif /* CHAR_MAX == SCHAR_MAX */
  529. _STLP_MOVE_TO_PRIV_NAMESPACE
  530. template <class _InputIter1, class _InputIter2>
  531. int __lexicographical_compare_3way(_InputIter1 __first1, _InputIter1 __last1,
  532. _InputIter2 __first2, _InputIter2 __last2);
  533. inline int
  534. __lexicographical_compare_3way(const unsigned char* __first1,
  535. const unsigned char* __last1,
  536. const unsigned char* __first2,
  537. const unsigned char* __last2) {
  538. const ptrdiff_t __len1 = __last1 - __first1;
  539. const ptrdiff_t __len2 = __last2 - __first2;
  540. const int __result = memcmp(__first1, __first2, (min) (__len1, __len2));
  541. return __result != 0 ? __result
  542. : (__len1 == __len2 ? 0 : (__len1 < __len2 ? -1 : 1));
  543. }
  544. #if !(CHAR_MAX == SCHAR_MAX)
  545. inline int
  546. __lexicographical_compare_3way(const char* __first1, const char* __last1,
  547. const char* __first2, const char* __last2) {
  548. return __lexicographical_compare_3way((const unsigned char*) __first1,
  549. (const unsigned char*) __last1,
  550. (const unsigned char*) __first2,
  551. (const unsigned char*) __last2);
  552. }
  553. #endif
  554. _STLP_MOVE_TO_STD_NAMESPACE
  555. #if !defined (_STLP_NO_EXTENSIONS)
  556. template <class _InputIter1, class _InputIter2>
  557. int lexicographical_compare_3way(_InputIter1 __first1, _InputIter1 __last1,
  558. _InputIter2 __first2, _InputIter2 __last2);
  559. #endif
  560. // count
  561. template <class _InputIter, class _Tp>
  562. _STLP_INLINE_LOOP _STLP_DIFFERENCE_TYPE(_InputIter)
  563. count(_InputIter __first, _InputIter __last, const _Tp& __val) {
  564. _STLP_DEBUG_CHECK(_STLP_PRIV __check_range(__first, __last))
  565. _STLP_DIFFERENCE_TYPE(_InputIter) __n = 0;
  566. for ( ; __first != __last; ++__first)
  567. if (*__first == __val)
  568. ++__n;
  569. return __n;
  570. }
  571. // find and find_if. Note find may be expressed in terms of find_if if appropriate binder was available.
  572. template <class _InputIter, class _Tp>
  573. _InputIter find(_InputIter __first, _InputIter __last, const _Tp& __val);
  574. template <class _InputIter, class _Predicate>
  575. _InputIter find_if(_InputIter __first, _InputIter __last, _Predicate __pred);
  576. // search.
  577. template <class _ForwardIter1, class _ForwardIter2, class _BinaryPred>
  578. _ForwardIter1 search(_ForwardIter1 __first1, _ForwardIter1 __last1,
  579. _ForwardIter2 __first2, _ForwardIter2 __last2, _BinaryPred __predicate);
  580. _STLP_MOVE_TO_PRIV_NAMESPACE
  581. // find_first_of
  582. template <class _InputIter, class _ForwardIter>
  583. _InputIter __find_first_of(_InputIter __first1, _InputIter __last1,
  584. _ForwardIter __first2, _ForwardIter __last2);
  585. template <class _InputIter, class _ForwardIter, class _BinaryPredicate>
  586. _InputIter __find_first_of(_InputIter __first1, _InputIter __last1,
  587. _ForwardIter __first2, _ForwardIter __last2,
  588. _BinaryPredicate __comp);
  589. _STLP_MOVE_TO_STD_NAMESPACE
  590. template <class _ForwardIter1, class _ForwardIter2,
  591. class _BinaryPredicate>
  592. _ForwardIter1
  593. find_end(_ForwardIter1 __first1, _ForwardIter1 __last1,
  594. _ForwardIter2 __first2, _ForwardIter2 __last2,
  595. _BinaryPredicate __comp);
  596. // replace
  597. template <class _ForwardIter, class _Tp>
  598. _STLP_INLINE_LOOP void
  599. replace(_ForwardIter __first, _ForwardIter __last,
  600. const _Tp& __old_value, const _Tp& __new_value) {
  601. _STLP_DEBUG_CHECK(_STLP_PRIV __check_range(__first, __last))
  602. for ( ; __first != __last; ++__first)
  603. if (*__first == __old_value)
  604. *__first = __new_value;
  605. }
  606. _STLP_MOVE_TO_PRIV_NAMESPACE
  607. template <class _ForwardIter, class _Tp, class _Compare1, class _Compare2, class _Distance>
  608. _ForwardIter __lower_bound(_ForwardIter __first, _ForwardIter __last,
  609. const _Tp& __val, _Compare1 __comp1, _Compare2 __comp2, _Distance*);
  610. _STLP_MOVE_TO_STD_NAMESPACE
  611. _STLP_END_NAMESPACE
  612. #if !defined (_STLP_LINK_TIME_INSTANTIATION)
  613. # include <stl/_algobase.c>
  614. #endif
  615. #endif /* _STLP_INTERNAL_ALGOBASE_H */
  616. // Local Variables:
  617. // mode:C++
  618. // End: