Leaked source code of windows server 2003
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

2700 lines
79 KiB

  1. // algorithm standard header
  2. #pragma once
  3. #ifndef _ALGORITHM_
  4. #define _ALGORITHM_
  5. #include <memory>
  6. #pragma pack(push,8)
  7. #pragma warning(push,3)
  8. #pragma warning(disable: 4244)
  9. _STD_BEGIN
  10. // COMMON SORT PARAMETERS
  11. const int _ISORT_MAX = 32; // maximum size for insertion sort
  12. // TEMPLATE FUNCTION for_each
  13. template<class _InIt,
  14. class _Fn1> inline
  15. _Fn1 for_each(_InIt _First, _InIt _Last, _Fn1 _Func)
  16. { // perform function for each element
  17. for (; _First != _Last; ++_First)
  18. _Func(*_First);
  19. return (_Func);
  20. }
  21. // TEMPLATE FUNCTION find
  22. template<class _InIt,
  23. class _Ty> inline
  24. _InIt find(_InIt _First, _InIt _Last, const _Ty& _Val)
  25. { // find first matching _Val
  26. for (; _First != _Last; ++_First)
  27. if (*_First == _Val)
  28. break;
  29. return (_First);
  30. }
  31. inline const char *find(const char *_First, const char *_Last, int _Val)
  32. { // find first char that matches _Val
  33. _First = (const char *)::memchr(_First, _Val, _Last - _First);
  34. return (_First == 0 ? _Last : _First);
  35. }
  36. inline const signed char *find(const signed char *_First,
  37. const signed char *_Last, int _Val)
  38. { // find first signed char that matches _Val
  39. _First = (const signed char *)::memchr(_First, _Val,
  40. _Last - _First);
  41. return (_First == 0 ? _Last : _First);
  42. }
  43. inline const unsigned char *find(const unsigned char *_First,
  44. const unsigned char *_Last, int _Val)
  45. { // find first unsigned char that matches _Val
  46. _First = (const unsigned char *)::memchr(_First, _Val,
  47. _Last - _First);
  48. return (_First == 0 ? _Last : _First);
  49. }
  50. // TEMPLATE FUNCTION find_if
  51. template<class _InIt,
  52. class _Pr> inline
  53. _InIt find_if(_InIt _First, _InIt _Last, _Pr _Pred)
  54. { // find first satisfying _Pred
  55. for (; _First != _Last; ++_First)
  56. if (_Pred(*_First))
  57. break;
  58. return (_First);
  59. }
  60. // TEMPLATE FUNCTION adjacent_find
  61. template<class _FwdIt> inline
  62. _FwdIt adjacent_find(_FwdIt _First, _FwdIt _Last)
  63. { // find first matching successor
  64. for (_FwdIt _Firstb; (_Firstb = _First) != _Last && ++_First != _Last; )
  65. if (*_Firstb == *_First)
  66. return (_Firstb);
  67. return (_Last);
  68. }
  69. // TEMPLATE FUNCTION adjacent_find WITH PRED
  70. template<class _FwdIt,
  71. class _Pr> inline
  72. _FwdIt adjacent_find(_FwdIt _First, _FwdIt _Last, _Pr _Pred)
  73. { // find first satisfying _Pred with successor
  74. for (_FwdIt _Firstb; (_Firstb = _First) != _Last && ++_First != _Last; )
  75. if (_Pred(*_Firstb, *_First))
  76. return (_Firstb);
  77. return (_Last);
  78. }
  79. #if _HAS_PARTIAL_SPECIALIZATION
  80. // TEMPLATE FUNCTION count
  81. template<class _InIt,
  82. class _Ty> inline
  83. typename iterator_traits<_InIt>::difference_type
  84. count(_InIt _First, _InIt _Last, const _Ty& _Val)
  85. { // count elements that match _Val
  86. typename iterator_traits<_InIt>::difference_type _Count = 0;
  87. for (; _First != _Last; ++_First)
  88. if (*_First == _Val)
  89. ++_Count;
  90. return (_Count);
  91. }
  92. // TEMPLATE FUNCTION count_if
  93. template<class _InIt,
  94. class _Pr> inline
  95. typename iterator_traits<_InIt>::difference_type
  96. count_if(_InIt _First, _InIt _Last, _Pr _Pred)
  97. { // count elements satisfying _Pred
  98. typename iterator_traits<_InIt>::difference_type _Count = 0;
  99. for (; _First != _Last; ++_First)
  100. if (_Pred(*_First))
  101. ++_Count;
  102. return (_Count);
  103. }
  104. #else /* _HAS_PARTIAL_SPECIALIZATION */
  105. // TEMPLATE FUNCTION count
  106. template<class _InIt,
  107. class _Ty> inline
  108. ptrdiff_t count(_InIt _First, _InIt _Last, const _Ty& _Val)
  109. { // count elements that match _Val
  110. ptrdiff_t _Count = 0;
  111. for (; _First != _Last; ++_First)
  112. if (*_First == _Val)
  113. ++_Count;
  114. return (_Count);
  115. }
  116. // TEMPLATE FUNCTION count_if
  117. template<class _InIt,
  118. class _Pr> inline
  119. ptrdiff_t count_if(_InIt _First, _InIt _Last, _Pr _Pred)
  120. { // count elements satisfying _Pred
  121. ptrdiff_t _Count = 0;
  122. for (; _First != _Last; ++_First)
  123. if (_Pred(*_First))
  124. ++_Count;
  125. return (_Count);
  126. }
  127. #endif /* _HAS_PARTIAL_SPECIALIZATION */
  128. // TEMPLATE FUNCTION search
  129. template<class _FwdIt1,
  130. class _FwdIt2,
  131. class _Diff1,
  132. class _Diff2> inline
  133. _FwdIt1 _Search(_FwdIt1 _First1, _FwdIt1 _Last1,
  134. _FwdIt2 _First2, _FwdIt2 _Last2, _Diff1 *, _Diff2 *)
  135. { // find first [_First2, _Last2) match
  136. _Diff1 _Count1 = 0;
  137. _Distance(_First1, _Last1, _Count1);
  138. _Diff2 _Count2 = 0;
  139. _Distance(_First2, _Last2, _Count2);
  140. for (; _Count2 <= _Count1; ++_First1, --_Count1)
  141. { // room for match, try it
  142. _FwdIt1 _Mid1 = _First1;
  143. for (_FwdIt2 _Mid2 = _First2; ; ++_Mid1, ++_Mid2)
  144. if (_Mid2 == _Last2)
  145. return (_First1);
  146. else if (!(*_Mid1 == *_Mid2))
  147. break;
  148. }
  149. return (_Last1);
  150. }
  151. template<class _FwdIt1,
  152. class _FwdIt2> inline
  153. _FwdIt1 search(_FwdIt1 _First1, _FwdIt1 _Last1,
  154. _FwdIt2 _First2, _FwdIt2 _Last2)
  155. { // find first [_First2, _Last2) match
  156. return (_Search(_First1, _Last1, _First2, _Last2,
  157. _Dist_type(_First1), _Dist_type(_First2)));
  158. }
  159. // TEMPLATE FUNCTION search WITH PRED
  160. template<class _FwdIt1,
  161. class _FwdIt2,
  162. class _Diff1,
  163. class _Diff2,
  164. class _Pr> inline
  165. _FwdIt1 _Search(_FwdIt1 _First1, _FwdIt1 _Last1,
  166. _FwdIt2 _First2, _FwdIt2 _Last2, _Pr _Pred, _Diff1 *, _Diff2 *)
  167. { // find first [_First2, _Last2) satisfying _Pred
  168. _Diff1 _Count1 = 0;
  169. _Distance(_First1, _Last1, _Count1);
  170. _Diff2 _Count2 = 0;
  171. _Distance(_First2, _Last2, _Count2);
  172. for (; _Count2 <= _Count1; ++_First1, --_Count1)
  173. { // room for match, try it
  174. _FwdIt1 _Mid1 = _First1;
  175. for (_FwdIt2 _Mid2 = _First2; ; ++_Mid1, ++_Mid2)
  176. if (_Mid2 == _Last2)
  177. return (_First1);
  178. else if (!_Pred(*_Mid1, *_Mid2))
  179. break;
  180. }
  181. return (_Last1);
  182. }
  183. template<class _FwdIt1,
  184. class _FwdIt2,
  185. class _Pr> inline
  186. _FwdIt1 search(_FwdIt1 _First1, _FwdIt1 _Last1,
  187. _FwdIt2 _First2, _FwdIt2 _Last2, _Pr _Pred)
  188. { // find first [_First2, _Last2) satisfying _Pred
  189. return (_Search(_First1, _Last1, _First2, _Last2, _Pred,
  190. _Dist_type(_First1), _Dist_type(_First2)));
  191. }
  192. // TEMPLATE FUNCTION search_n
  193. template<class _FwdIt1,
  194. class _Diff2,
  195. class _Ty,
  196. class _Diff1> inline
  197. _FwdIt1 _Search_n(_FwdIt1 _First1, _FwdIt1 _Last1,
  198. _Diff2 _Count, const _Ty& _Val, _Diff1 *)
  199. { // find first _Count * _Val match
  200. _Diff1 _Count1 = 0;
  201. _Distance(_First1, _Last1, _Count1);
  202. for (; _Count <= _Count1; ++_First1, --_Count1)
  203. { // room for match, try it
  204. _FwdIt1 _Mid1 = _First1;
  205. for (_Diff2 _Count2 = _Count; ; ++_Mid1, --_Count2)
  206. if (_Count2 == 0)
  207. return (_First1);
  208. else if (!(*_Mid1 == _Val))
  209. break;
  210. }
  211. return (_Last1);
  212. }
  213. template<class _FwdIt1,
  214. class _Diff2,
  215. class _Ty> inline
  216. _FwdIt1 search_n(_FwdIt1 _First1, _FwdIt1 _Last1,
  217. _Diff2 _Count, const _Ty& _Val)
  218. { // find first _Count * _Val match
  219. return (_Search_n(_First1, _Last1, _Count, _Val, _Dist_type(_First1)));
  220. }
  221. // TEMPLATE FUNCTION search_n WITH PRED
  222. template<class _FwdIt1,
  223. class _Diff2,
  224. class _Ty,
  225. class _Diff1,
  226. class _Pr> inline
  227. _FwdIt1 _Search_n(_FwdIt1 _First1, _FwdIt1 _Last1,
  228. _Diff2 _Count, const _Ty& _Val, _Pr _Pred, _Diff1 *)
  229. { // find first _Count * _Val satisfying _Pred
  230. _Diff1 _Count1 = 0;
  231. _Distance(_First1, _Last1, _Count1);
  232. for (; _Count <= _Count1; ++_First1, --_Count1)
  233. { // room for match, try it
  234. _FwdIt1 _Mid1 = _First1;
  235. for (_Diff2 _Count2 = _Count; ; ++_Mid1, --_Count2)
  236. if (_Count2 == 0)
  237. return (_First1);
  238. else if (!_Pred(*_Mid1, _Val))
  239. break;
  240. }
  241. return (_Last1);
  242. }
  243. template<class _FwdIt1,
  244. class _Diff2,
  245. class _Ty,
  246. class _Pr> inline
  247. _FwdIt1 search_n(_FwdIt1 _First1, _FwdIt1 _Last1,
  248. _Diff2 _Count, const _Ty& _Val, _Pr _Pred)
  249. { // find first _Count * _Val satisfying _Pred
  250. return (_Search_n(_First1, _Last1,
  251. _Count, _Val, _Pred, _Dist_type(_First1)));
  252. }
  253. // TEMPLATE FUNCTION find_end
  254. template<class _FwdIt1,
  255. class _FwdIt2,
  256. class _Diff1,
  257. class _Diff2> inline
  258. _FwdIt1 _Find_end(_FwdIt1 _First1, _FwdIt1 _Last1,
  259. _FwdIt2 _First2, _FwdIt2 _Last2, _Diff1 *, _Diff2 *)
  260. { // find last [_First2, _Last2) match
  261. _Diff1 _Count1 = 0;
  262. _Distance(_First1, _Last1, _Count1);
  263. _Diff2 _Count2 = 0;
  264. _Distance(_First2, _Last2, _Count2);
  265. _FwdIt1 _Ans = _Last1;
  266. if (0 < _Count2)
  267. for (; _Count2 <= _Count1; ++_First1, --_Count1)
  268. { // room for match, try it
  269. _FwdIt1 _Mid1 = _First1;
  270. for (_FwdIt2 _Mid2 = _First2; ; ++_Mid1)
  271. if (!(*_Mid1 == *_Mid2))
  272. break;
  273. else if (++_Mid2 == _Last2)
  274. { // potential answer, save it
  275. _Ans = _First1;
  276. break;
  277. }
  278. }
  279. return (_Ans);
  280. }
  281. template<class _FwdIt1,
  282. class _FwdIt2> inline
  283. _FwdIt1 find_end(_FwdIt1 _First1, _FwdIt1 _Last1,
  284. _FwdIt2 _First2, _FwdIt2 _Last2)
  285. { // find last [_First2, _Last2) match
  286. return (_Find_end(_First1, _Last1, _First2, _Last2,
  287. _Dist_type(_First1), _Dist_type(_First2)));
  288. }
  289. // TEMPLATE FUNCTION find_end WITH PRED
  290. template<class _FwdIt1,
  291. class _FwdIt2,
  292. class _Diff1,
  293. class _Diff2,
  294. class _Pr> inline
  295. _FwdIt1 _Find_end(_FwdIt1 _First1, _FwdIt1 _Last1,
  296. _FwdIt2 _First2, _FwdIt2 _Last2, _Pr _Pred, _Diff1 *, _Diff2 *)
  297. { // find last [_First2, _Last2) satisfying _Pred
  298. _Diff1 _Count1 = 0;
  299. _Distance(_First1, _Last1, _Count1);
  300. _Diff2 _Count2 = 0;
  301. _Distance(_First2, _Last2, _Count2);
  302. _FwdIt1 _Ans = _Last1;
  303. if (0 < _Count2)
  304. for (; _Count2 <= _Count1; ++_First1, --_Count1)
  305. { // room for match, try it
  306. _FwdIt1 _Mid1 = _First1;
  307. for (_FwdIt2 _Mid2 = _First2; ; ++_Mid1)
  308. if (!_Pred(*_Mid1, *_Mid2))
  309. break;
  310. else if (++_Mid2 == _Last2)
  311. { // potential answer, save it
  312. _Ans = _First1;
  313. break;
  314. }
  315. }
  316. return (_Ans);
  317. }
  318. template<class _FwdIt1,
  319. class _FwdIt2,
  320. class _Pr> inline
  321. _FwdIt1 find_end(_FwdIt1 _First1, _FwdIt1 _Last1,
  322. _FwdIt2 _First2, _FwdIt2 _Last2, _Pr _Pred)
  323. { // find last [_First2, _Last2) satisfying _Pred
  324. return (_Find_end(_First1, _Last1, _First2, _Last2, _Pred,
  325. _Dist_type(_First1), _Dist_type(_First2)));
  326. }
  327. // TEMPLATE FUNCTION find_first_of
  328. template<class _FwdIt1,
  329. class _FwdIt2> inline
  330. _FwdIt1 find_first_of(_FwdIt1 _First1, _FwdIt1 _Last1,
  331. _FwdIt2 _First2, _FwdIt2 _Last2)
  332. { // look for one of [_First2, _Last2) that matches element
  333. for (; _First1 != _Last1; ++_First1)
  334. for (_FwdIt2 _Mid2 = _First2; _Mid2 != _Last2; ++_Mid2)
  335. if (*_First1 == *_Mid2)
  336. return (_First1);
  337. return (_First1);
  338. }
  339. // TEMPLATE FUNCTION find_first_of WITH PRED
  340. template<class _FwdIt1,
  341. class _FwdIt2,
  342. class _Pr> inline
  343. _FwdIt1 find_first_of(_FwdIt1 _First1, _FwdIt1 _Last1,
  344. _FwdIt2 _First2, _FwdIt2 _Last2, _Pr _Pred)
  345. { // look for one of [_First2, _Last2) satisfying _Pred with element
  346. for (; _First1 != _Last1; ++_First1)
  347. for (_FwdIt2 _Mid2 = _First2; _Mid2 != _Last2; ++_Mid2)
  348. if (_Pred(*_First1, *_Mid2))
  349. return (_First1);
  350. return (_First1);
  351. }
  352. // TEMPLATE FUNCTION iter_swap
  353. template<class _FwdIt1,
  354. class _FwdIt2> inline
  355. void iter_swap(_FwdIt1 _Left, _FwdIt2 _Right)
  356. { // swap *_Left and *_Right
  357. std::swap(*_Left, *_Right);
  358. }
  359. // TEMPLATE FUNCTION swap_ranges
  360. template<class _FwdIt1,
  361. class _FwdIt2> inline
  362. _FwdIt2 swap_ranges(_FwdIt1 _First1, _FwdIt1 _Last1, _FwdIt2 _First2)
  363. { // swap [_First1, _Last1) with [_First2, ...)
  364. for (; _First1 != _Last1; ++_First1, ++_First2)
  365. std::iter_swap(_First1, _First2);
  366. return (_First2);
  367. }
  368. // TEMPLATE FUNCTION transform WITH UNARY OP
  369. template<class _InIt,
  370. class _OutIt,
  371. class _Fn1> inline
  372. _OutIt transform(_InIt _First, _InIt _Last, _OutIt _Dest, _Fn1 _Func)
  373. { // transform [_First, _Last) with _Func
  374. for (; _First != _Last; ++_First, ++_Dest)
  375. *_Dest = _Func(*_First);
  376. return (_Dest);
  377. }
  378. // TEMPLATE FUNCTION transform WITH BINARY OP
  379. template<class _InIt1,
  380. class _InIt2,
  381. class _OutIt,
  382. class _Fn2> inline
  383. _OutIt transform(_InIt1 _First1, _InIt1 _Last1, _InIt2 _First2,
  384. _OutIt _Dest, _Fn2 _Func)
  385. { // transform [_First1, _Last1) and [_First2, _Last2) with _Func
  386. for (; _First1 != _Last1; ++_First1, ++_First2, ++_Dest)
  387. *_Dest = _Func(*_First1, *_First2);
  388. return (_Dest);
  389. }
  390. // TEMPLATE FUNCTION replace
  391. template<class _FwdIt,
  392. class _Ty> inline
  393. void replace(_FwdIt _First, _FwdIt _Last,
  394. const _Ty& _Oldval, const _Ty& _Newval)
  395. { // replace each matching _Oldval with _Newval
  396. for (; _First != _Last; ++_First)
  397. if (*_First == _Oldval)
  398. *_First = _Newval;
  399. }
  400. // TEMPLATE FUNCTION replace_if
  401. template<class _FwdIt,
  402. class _Pr,
  403. class _Ty> inline
  404. void replace_if(_FwdIt _First, _FwdIt _Last, _Pr _Pred, const _Ty& _Val)
  405. { // replace each satisfying _Pred with _Val
  406. for (; _First != _Last; ++_First)
  407. if (_Pred(*_First))
  408. *_First = _Val;
  409. }
  410. // TEMPLATE FUNCTION replace_copy
  411. template<class _InIt,
  412. class _OutIt,
  413. class _Ty> inline
  414. _OutIt replace_copy(_InIt _First, _InIt _Last, _OutIt _Dest,
  415. const _Ty& _Oldval, const _Ty& _Newval)
  416. { // copy replacing each matching _Oldval with _Newval
  417. for (; _First != _Last; ++_First, ++_Dest)
  418. *_Dest = *_First == _Oldval ? _Newval : *_First;
  419. return (_Dest);
  420. }
  421. // TEMPLATE FUNCTION replace_copy_if
  422. template<class _InIt,
  423. class _OutIt,
  424. class _Pr,
  425. class _Ty> inline
  426. _OutIt replace_copy_if(_InIt _First, _InIt _Last, _OutIt _Dest,
  427. _Pr _Pred, const _Ty& _Val)
  428. { // copy replacing each satisfying _Pred with _Val
  429. for (; _First != _Last; ++_First, ++_Dest)
  430. *_Dest = _Pred(*_First) ? _Val : *_First;
  431. return (_Dest);
  432. }
  433. // TEMPLATE FUNCTION generate
  434. template<class _FwdIt,
  435. class _Fn0> inline
  436. void generate(_FwdIt _First, _FwdIt _Last, _Fn0 _Func)
  437. { // replace [_First, _Last) with _Func()
  438. for (; _First != _Last; ++_First)
  439. *_First = _Func();
  440. }
  441. // TEMPLATE FUNCTION generate_n
  442. template<class _OutIt,
  443. class _Diff,
  444. class _Fn0> inline
  445. void generate_n(_OutIt _Dest, _Diff _Count, _Fn0 _Func)
  446. { // replace [_Dest, _Dest + _Count) with _Func()
  447. for (; 0 < _Count; --_Count, ++_Dest)
  448. *_Dest = _Func();
  449. }
  450. // TEMPLATE FUNCTION remove_copy
  451. template<class _InIt,
  452. class _OutIt,
  453. class _Ty> inline
  454. _OutIt remove_copy(_InIt _First, _InIt _Last,
  455. _OutIt _Dest, const _Ty& _Val)
  456. { // copy omitting each matching _Val
  457. for (; _First != _Last; ++_First)
  458. if (!(*_First == _Val))
  459. *_Dest++ = *_First;
  460. return (_Dest);
  461. }
  462. // TEMPLATE FUNCTION remove_copy_if
  463. template<class _InIt,
  464. class _OutIt,
  465. class _Pr> inline
  466. _OutIt remove_copy_if(_InIt _First, _InIt _Last, _OutIt _Dest, _Pr _Pred)
  467. { // copy omitting each element satisfying _Pred
  468. for (; _First != _Last; ++_First)
  469. if (!_Pred(*_First))
  470. *_Dest++ = *_First;
  471. return (_Dest);
  472. }
  473. // TEMPLATE FUNCTION remove
  474. template<class _FwdIt,
  475. class _Ty> inline
  476. _FwdIt remove(_FwdIt _First, _FwdIt _Last, const _Ty& _Val)
  477. { // remove each matching _Val
  478. _First = find(_First, _Last, _Val);
  479. if (_First == _Last)
  480. return (_First); // empty sequence, all done
  481. else
  482. { // nonempty sequence, worth doing
  483. _FwdIt _First1 = _First;
  484. return (std::remove_copy(++_First1, _Last, _First, _Val));
  485. }
  486. }
  487. // TEMPLATE FUNCTION remove_if
  488. template<class _FwdIt,
  489. class _Pr> inline
  490. _FwdIt remove_if(_FwdIt _First, _FwdIt _Last, _Pr _Pred)
  491. { // remove each satisfying _Pred
  492. _First = std::find_if(_First, _Last, _Pred);
  493. if (_First == _Last)
  494. return (_First); // empty sequence, all done
  495. else
  496. { // nonempty sequence, worth doing
  497. _FwdIt _First1 = _First;
  498. return (std::remove_copy_if(++_First1, _Last, _First, _Pred));
  499. }
  500. }
  501. // TEMPLATE FUNCTION unique
  502. template<class _FwdIt> inline
  503. _FwdIt unique(_FwdIt _First, _FwdIt _Last)
  504. { // remove each matching previous
  505. for (_FwdIt _Firstb; (_Firstb = _First) != _Last && ++_First != _Last; )
  506. if (*_Firstb == *_First)
  507. { // copy down
  508. for (; ++_First != _Last; )
  509. if (!(*_Firstb == *_First))
  510. *++_Firstb = *_First;
  511. return (++_Firstb);
  512. }
  513. return (_Last);
  514. }
  515. // TEMPLATE FUNCTION unique WITH PRED
  516. template<class _FwdIt,
  517. class _Pr> inline
  518. _FwdIt unique(_FwdIt _First, _FwdIt _Last, _Pr _Pred)
  519. { // remove each satisfying _Pred with previous
  520. for (_FwdIt _Firstb; (_Firstb = _First) != _Last && ++_First != _Last; )
  521. if (_Pred(*_Firstb, *_First))
  522. { // copy down
  523. for (; ++_First != _Last; )
  524. if (!_Pred(*_Firstb, *_First))
  525. *++_Firstb = *_First;
  526. return (++_Firstb);
  527. }
  528. return (_Last);
  529. }
  530. // TEMPLATE FUNCTION unique_copy
  531. template<class _InIt,
  532. class _OutIt,
  533. class _Ty> inline
  534. _OutIt _Unique_copy(_InIt _First, _InIt _Last, _OutIt _Dest, _Ty *)
  535. { // copy compressing pairs that match, input iterators
  536. _Ty _Val = *_First;
  537. for (*_Dest++ = _Val; ++_First != _Last; )
  538. if (!(_Val == *_First))
  539. _Val = *_First, *_Dest++ = _Val;
  540. return (_Dest);
  541. }
  542. template<class _InIt,
  543. class _OutIt> inline
  544. _OutIt _Unique_copy(_InIt _First, _InIt _Last, _OutIt _Dest,
  545. input_iterator_tag)
  546. { // copy compressing pairs that match, input iterators
  547. return (_Unique_copy(_First, _Last, _Dest, _Val_type(_First)));
  548. }
  549. template<class _FwdIt,
  550. class _OutIt> inline
  551. _OutIt _Unique_copy(_FwdIt _First, _FwdIt _Last, _OutIt _Dest,
  552. forward_iterator_tag)
  553. { // copy compressing pairs that match, forward iterators
  554. _FwdIt _Firstb = _First;
  555. for (*_Dest++ = *_Firstb; ++_First != _Last; )
  556. if (!(*_Firstb == *_First))
  557. _Firstb = _First, *_Dest++ = *_Firstb;
  558. return (_Dest);
  559. }
  560. template<class _BidIt,
  561. class _OutIt> inline
  562. _OutIt _Unique_copy(_BidIt _First, _BidIt _Last, _OutIt _Dest,
  563. bidirectional_iterator_tag)
  564. { // copy compressing pairs that match, bidirectional iterators
  565. return (_Unique_copy(_First, _Last, _Dest, forward_iterator_tag()));
  566. }
  567. template<class _RanIt,
  568. class _OutIt> inline
  569. _OutIt _Unique_copy(_RanIt _First, _RanIt _Last, _OutIt _Dest,
  570. random_access_iterator_tag)
  571. { // copy compressing pairs that match, random-access iterators
  572. return (_Unique_copy(_First, _Last, _Dest, forward_iterator_tag()));
  573. }
  574. template<class _InIt,
  575. class _OutIt> inline
  576. _OutIt unique_copy(_InIt _First, _InIt _Last, _OutIt _Dest)
  577. { // copy compressing pairs that match
  578. return (_First == _Last ? _Dest :
  579. _Unique_copy(_First, _Last, _Dest, _Iter_cat(_First)));
  580. }
  581. // TEMPLATE FUNCTION unique_copy WITH PRED
  582. template<class _InIt,
  583. class _OutIt,
  584. class _Ty,
  585. class _Pr> inline
  586. _OutIt _Unique_copy(_InIt _First, _InIt _Last, _OutIt _Dest, _Pr _Pred,
  587. _Ty *)
  588. { // copy compressing pairs satisfying _Pred, input iterators
  589. _Ty _Val = *_First;
  590. for (*_Dest++ = _Val; ++_First != _Last; )
  591. if (!_Pred(_Val, *_First))
  592. _Val = *_First, *_Dest++ = _Val;
  593. return (_Dest);
  594. }
  595. template<class _InIt,
  596. class _OutIt,
  597. class _Pr> inline
  598. _OutIt _Unique_copy(_InIt _First, _InIt _Last, _OutIt _Dest, _Pr _Pred,
  599. input_iterator_tag)
  600. { // copy compressing pairs satisfying _Pred, input iterators
  601. return (_Unique_copy(_First, _Last, _Dest, _Pred, _Val_type(_First)));
  602. }
  603. template<class _FwdIt,
  604. class _OutIt,
  605. class _Pr> inline
  606. _OutIt _Unique_copy(_FwdIt _First, _FwdIt _Last, _OutIt _Dest, _Pr _Pred,
  607. forward_iterator_tag)
  608. { // copy compressing pairs satisfying _Pred, forward iterators
  609. _FwdIt _Firstb = _First;
  610. for (*_Dest++ = *_Firstb; ++_First != _Last; )
  611. if (!_Pred(*_Firstb, *_First))
  612. _Firstb = _First, *_Dest++ = *_Firstb;
  613. return (_Dest);
  614. }
  615. template<class _BidIt,
  616. class _OutIt,
  617. class _Pr> inline
  618. _OutIt _Unique_copy(_BidIt _First, _BidIt _Last, _OutIt _Dest, _Pr _Pred,
  619. bidirectional_iterator_tag)
  620. { // copy compressing pairs satisfying _Pred, bidirectional iterators
  621. return (_Unique_copy(_First, _Last, _Dest, _Pred,
  622. forward_iterator_tag()));
  623. }
  624. template<class _RanIt,
  625. class _OutIt,
  626. class _Pr> inline
  627. _OutIt _Unique_copy(_RanIt _First, _RanIt _Last, _OutIt _Dest, _Pr _Pred,
  628. random_access_iterator_tag)
  629. { // copy compressing pairs satisfying _Pred, random-access iterators
  630. return (_Unique_copy(_First, _Last, _Dest, _Pred,
  631. forward_iterator_tag()));
  632. }
  633. template<class _InIt,
  634. class _OutIt,
  635. class _Pr> inline
  636. _OutIt unique_copy(_InIt _First, _InIt _Last, _OutIt _Dest, _Pr _Pred)
  637. { // copy compressing pairs satisfying _Pred
  638. return (_First == _Last ? _Dest
  639. : _Unique_copy(_First, _Last, _Dest, _Pred, _Iter_cat(_First)));
  640. }
  641. // TEMPLATE FUNCTION reverse
  642. template<class _BidIt> inline
  643. void _Reverse(_BidIt _First, _BidIt _Last, bidirectional_iterator_tag)
  644. { // reverse elements in [_First, _Last), bidirectional iterators
  645. for (; _First != _Last && _First != --_Last; ++_First)
  646. std::iter_swap(_First, _Last);
  647. }
  648. template<class _RanIt> inline
  649. void _Reverse(_RanIt _First, _RanIt _Last, random_access_iterator_tag)
  650. { // reverse elements in [_First, _Last), random-access iterators
  651. for (; _First < _Last; ++_First)
  652. std::iter_swap(_First, --_Last);
  653. }
  654. template<class _BidIt> inline
  655. void reverse(_BidIt _First, _BidIt _Last)
  656. { // reverse elements in [_First, _Last)
  657. _Reverse(_First, _Last, _Iter_cat(_First));
  658. }
  659. // TEMPLATE FUNCTION reverse_copy
  660. template<class _BidIt,
  661. class _OutIt> inline
  662. _OutIt reverse_copy(_BidIt _First, _BidIt _Last, _OutIt _Dest)
  663. { // copy reversing elements in [_First, _Last)
  664. for (; _First != _Last; ++_Dest)
  665. *_Dest = *--_Last;
  666. return (_Dest);
  667. }
  668. // TEMPLATE FUNCTION rotate
  669. template<class _FwdIt> inline
  670. void _Rotate(_FwdIt _First, _FwdIt _Mid, _FwdIt _Last,
  671. forward_iterator_tag)
  672. { // rotate [_First, _Last), forward iterators
  673. for (_FwdIt _Next = _Mid; ; )
  674. { // swap [_First, ...) into place
  675. std::iter_swap(_First, _Next);
  676. if (++_First == _Mid)
  677. if (++_Next == _Last)
  678. break; // done, quit
  679. else
  680. _Mid = _Next; // mark end of next interval
  681. else if (++_Next == _Last)
  682. _Next = _Mid; // wrap to last end
  683. }
  684. }
  685. template<class _BidIt> inline
  686. void _Rotate(_BidIt _First, _BidIt _Mid, _BidIt _Last,
  687. bidirectional_iterator_tag)
  688. { // rotate [_First, _Last), bidirectional iterators
  689. std::reverse(_First, _Mid);
  690. std::reverse(_Mid, _Last);
  691. std::reverse(_First, _Last);
  692. }
  693. template<class _RanIt,
  694. class _Diff,
  695. class _Ty> inline
  696. void _Rotate(_RanIt _First, _RanIt _Mid, _RanIt _Last, _Diff *, _Ty *)
  697. { // rotate [_First, _Last), random-access iterators
  698. _Diff _Shift = _Mid - _First;
  699. _Diff _Count = _Last - _First;
  700. for (_Diff _Factor = _Shift; _Factor != 0; )
  701. { // find subcycle count as GCD of shift count and length
  702. _Diff _Tmp = _Count % _Factor;
  703. _Count = _Factor, _Factor = _Tmp;
  704. }
  705. if (_Count < _Last - _First)
  706. for (; 0 < _Count; --_Count)
  707. { // rotate each subcycle
  708. _RanIt _Hole = _First + _Count;
  709. _RanIt _Next = _Hole;
  710. _Ty _Holeval = *_Hole;
  711. _RanIt _Next1 = _Next + _Shift == _Last ? _First : _Next + _Shift;
  712. while (_Next1 != _Hole)
  713. { // percolate elements back around subcycle
  714. *_Next = *_Next1;
  715. _Next = _Next1;
  716. _Next1 = _Shift < _Last - _Next1 ? _Next1 + _Shift
  717. : _First + (_Shift - (_Last - _Next1));
  718. }
  719. *_Next = _Holeval;
  720. }
  721. }
  722. template<class _RanIt> inline
  723. void _Rotate(_RanIt _First, _RanIt _Mid, _RanIt _Last,
  724. random_access_iterator_tag)
  725. { // rotate [_First, _Last), random-access iterators
  726. _Rotate(_First, _Mid, _Last, _Dist_type(_First), _Val_type(_First));
  727. }
  728. template<class _FwdIt> inline
  729. void rotate(_FwdIt _First, _FwdIt _Mid, _FwdIt _Last)
  730. { // rotate [_First, _Last)
  731. if (_First != _Mid && _Mid != _Last)
  732. _Rotate(_First, _Mid, _Last, _Iter_cat(_First));
  733. }
  734. // TEMPLATE FUNCTION rotate_copy
  735. template<class _FwdIt,
  736. class _OutIt> inline
  737. _OutIt rotate_copy(_FwdIt _First, _FwdIt _Mid, _FwdIt _Last, _OutIt _Dest)
  738. { // copy rotating [_First, _Last)
  739. _Dest = std::copy(_Mid, _Last, _Dest);
  740. return (std::copy(_First, _Mid, _Dest));
  741. }
  742. // TEMPLATE FUNCTION random_shuffle
  743. template<class _RanIt,
  744. class _Diff> inline
  745. void _Random_shuffle(_RanIt _First, _RanIt _Last, _Diff *)
  746. { // shuffle [_First, _Last)
  747. const int _RANDOM_BITS = 15; // minimum random bits from rand()
  748. const int _RANDOM_MAX = (1U << _RANDOM_BITS) - 1;
  749. _RanIt _Next = _First;
  750. for (unsigned long _Index = 2; ++_Next != _Last; ++_Index)
  751. { // assume unsigned long big enough for _Diff count
  752. unsigned long _Rm = _RANDOM_MAX;
  753. unsigned long _Rn = ::rand() & _RANDOM_MAX;
  754. for (; _Rm < _Index && _Rm != ~0UL;
  755. _Rm = _Rm << _RANDOM_BITS | _RANDOM_MAX)
  756. _Rn = _Rn << _RANDOM_BITS | _RANDOM_MAX; // build random value
  757. std::iter_swap(_Next, _First + _Diff(_Rn % _Index)); // swap a pair
  758. }
  759. }
  760. template<class _RanIt> inline
  761. void random_shuffle(_RanIt _First, _RanIt _Last)
  762. { // shuffle [_First, _Last)
  763. if (_First != _Last)
  764. _Random_shuffle(_First, _Last, _Dist_type(_First));
  765. }
  766. // TEMPLATE FUNCTION random_shuffle WITH RANDOM FN
  767. template<class _RanIt,
  768. class _Fn1,
  769. class _Diff> inline
  770. void _Random_shuffle(_RanIt _First, _RanIt _Last, _Fn1& _Func, _Diff *)
  771. { // shuffle nonempty [_First, _Last) using random function _Func
  772. _RanIt _Next = _First;
  773. for (_Diff _Index = 2; ++_Next != _Last; ++_Index)
  774. std::iter_swap(_Next, _First + _Diff(_Func(_Index)));
  775. }
  776. template<class _RanIt,
  777. class _Fn1> inline
  778. void random_shuffle(_RanIt _First, _RanIt _Last, _Fn1& _Func)
  779. { // shuffle [_First, _Last) using random function _Func
  780. if (_First != _Last)
  781. _Random_shuffle(_First, _Last, _Func, _Dist_type(_First));
  782. }
  783. // TEMPLATE FUNCTION partition
  784. template<class _BidIt,
  785. class _Pr> inline
  786. _BidIt partition(_BidIt _First, _BidIt _Last, _Pr _Pred)
  787. { // move elements satisfying _Pred to beginning of sequence
  788. for (; ; ++_First)
  789. { // find any out-of-order pair
  790. for (; _First != _Last && _Pred(*_First); ++_First)
  791. ; // skip in-place elements at beginning
  792. if (_First == _Last)
  793. break; // done
  794. for (; _First != --_Last && !_Pred(*_Last); )
  795. ; // skip in-place elements at end
  796. if (_First == _Last)
  797. break; // done
  798. std::iter_swap(_First, _Last); // swap out-of-place pair and loop
  799. }
  800. return (_First);
  801. }
  802. // TEMPLATE FUNCTION stable_partition
  803. template<class _BidIt,
  804. class _Pr,
  805. class _Diff,
  806. class _Ty> inline
  807. _BidIt _Stable_partition(_BidIt _First, _BidIt _Last, _Pr _Pred,
  808. _Diff _Count, _Temp_iterator<_Ty>& _Tempbuf)
  809. { // partition using _Pred, preserving order of equivalents
  810. if (_Count == 1)
  811. return (_Pred(*_First) ? _Last : _First);
  812. else if (_Count <= _Tempbuf._Maxlen())
  813. { // temp buffer big enough, copy right partition out and back
  814. _BidIt _Next = _First;
  815. for (_Tempbuf._Init(); _First != _Last; ++_First)
  816. if (_Pred(*_First))
  817. *_Next++ = *_First;
  818. else
  819. *_Tempbuf++ = *_First;
  820. std::copy(_Tempbuf._First(), _Tempbuf._Last(), _Next); // copy back
  821. return (_Next);
  822. }
  823. else
  824. { // temp buffer not big enough, divide and conquer
  825. _BidIt _Mid = _First;
  826. std::advance(_Mid, _Count / 2);
  827. _BidIt _Left = _Stable_partition(_First, _Mid, _Pred,
  828. _Count / 2, _Tempbuf); // form L1R1 in left half
  829. _BidIt _Right = _Stable_partition(_Mid, _Last, _Pred,
  830. _Count - _Count / 2, _Tempbuf); // form L2R2 in right half
  831. _Diff _Count1 = 0;
  832. _Distance(_Left, _Mid, _Count1);
  833. _Diff _Count2 = 0;
  834. _Distance(_Mid, _Right, _Count2);
  835. return (_Buffered_rotate(_Left, _Mid, _Right,
  836. _Count1, _Count2, _Tempbuf)); // rotate L1R1L2R2 to L1L2R1R2
  837. }
  838. }
  839. template<class _BidIt,
  840. class _Pr,
  841. class _Diff,
  842. class _Ty> inline
  843. _BidIt _Stable_partition(_BidIt _First, _BidIt _Last, _Pr _Pred,
  844. _Diff *, _Ty *)
  845. { // partition preserving order of equivalents, using _Pred
  846. _Diff _Count = 0;
  847. _Distance(_First, _Last, _Count);
  848. _Temp_iterator<_Ty> _Tempbuf(_Count);
  849. return (_Stable_partition(_First, _Last, _Pred, _Count, _Tempbuf));
  850. }
  851. template<class _BidIt,
  852. class _Pr> inline
  853. _BidIt stable_partition(_BidIt _First, _BidIt _Last, _Pr _Pred)
  854. { // partition preserving order of equivalents, using _Pred
  855. return (_First == _Last ? _First : _Stable_partition(_First, _Last, _Pred,
  856. _Dist_type(_First), _Val_type(_First)));
  857. }
  858. // TEMPLATE FUNCTION push_heap
  859. template<class _RanIt,
  860. class _Diff,
  861. class _Ty> inline
  862. void _Push_heap(_RanIt _First, _Diff _Hole,
  863. _Diff _Top, _Ty _Val)
  864. { // percolate _Hole to _Top or where _Val belongs, using operator<
  865. for (_Diff _Idx = (_Hole - 1) / 2;
  866. _Top < _Hole && *(_First + _Idx) < _Val;
  867. _Idx = (_Hole - 1) / 2)
  868. { // move _Hole up to parent
  869. *(_First + _Hole) = *(_First + _Idx);
  870. _Hole = _Idx;
  871. }
  872. *(_First + _Hole) = _Val; // drop _Val into final hole
  873. }
  874. template<class _RanIt,
  875. class _Diff,
  876. class _Ty> inline
  877. void _Push_heap_0(_RanIt _First, _RanIt _Last, _Diff *, _Ty *)
  878. { // push *(_Last - 1) onto heap at [_First, _Last - 1), using operator<
  879. _Diff _Count = _Last - _First;
  880. if (1 < _Count)
  881. _Push_heap(_First, --_Count, _Diff(0), _Ty(*(_Last - 1)));
  882. }
  883. template<class _RanIt> inline
  884. void push_heap(_RanIt _First, _RanIt _Last)
  885. { // push *(_Last - 1) onto heap at [_First, _Last - 1), using operator<
  886. _Push_heap_0(_First, _Last, _Dist_type(_First), _Val_type(_First));
  887. }
  888. // TEMPLATE FUNCTION push_heap WITH PRED
  889. template<class _RanIt,
  890. class _Diff,
  891. class _Ty,
  892. class _Pr> inline
  893. void _Push_heap(_RanIt _First, _Diff _Hole,
  894. _Diff _Top, _Ty _Val, _Pr _Pred)
  895. { // percolate _Hole to _Top or where _Val belongs, using operator<
  896. for (_Diff _Idx = (_Hole - 1) / 2;
  897. _Top < _Hole && _Pred(*(_First + _Idx), _Val);
  898. _Idx = (_Hole - 1) / 2)
  899. { // move _Hole up to parent
  900. *(_First + _Hole) = *(_First + _Idx);
  901. _Hole = _Idx;
  902. }
  903. *(_First + _Hole) = _Val; // drop _Val into final hole
  904. }
  905. template<class _RanIt,
  906. class _Diff,
  907. class _Ty,
  908. class _Pr> inline
  909. void _Push_heap_0(_RanIt _First, _RanIt _Last, _Pr _Pred, _Diff *, _Ty *)
  910. { // push *(_Last - 1) onto heap at [_First, _Last - 1), using _Pred
  911. _Diff _Count = _Last - _First;
  912. if (1 < _Count)
  913. _Push_heap(_First, --_Count, _Diff(0), _Ty(*(_Last - 1)), _Pred);
  914. }
  915. template<class _RanIt,
  916. class _Pr> inline
  917. void push_heap(_RanIt _First, _RanIt _Last, _Pr _Pred)
  918. { // push *(_Last - 1) onto heap at [_First, _Last - 1), using _Pred
  919. _Push_heap_0(_First, _Last, _Pred,
  920. _Dist_type(_First), _Val_type(_First));
  921. }
  922. // TEMPLATE FUNCTION pop_heap
  923. template<class _RanIt,
  924. class _Diff,
  925. class _Ty> inline
  926. void _Adjust_heap(_RanIt _First, _Diff _Hole, _Diff _Bottom, _Ty _Val)
  927. { // percolate _Hole to _Bottom, then push _Val, using operator<
  928. _Diff _Top = _Hole;
  929. _Diff _Idx = 2 * _Hole + 2;
  930. for (; _Idx < _Bottom; _Idx = 2 * _Idx + 2)
  931. { // move _Hole down to larger child
  932. if (*(_First + _Idx) < *(_First + (_Idx - 1)))
  933. --_Idx;
  934. *(_First + _Hole) = *(_First + _Idx), _Hole = _Idx;
  935. }
  936. if (_Idx == _Bottom)
  937. { // only child at bottom, move _Hole down to it
  938. *(_First + _Hole) = *(_First + (_Bottom - 1));
  939. _Hole = _Bottom - 1;
  940. }
  941. _Push_heap(_First, _Hole, _Top, _Val);
  942. }
  943. template<class _RanIt,
  944. class _Diff,
  945. class _Ty> inline
  946. void _Pop_heap(_RanIt _First, _RanIt _Last, _RanIt _Dest,
  947. _Ty _Val, _Diff *)
  948. { // pop *_First to *_Dest and reheap, using operator<
  949. *_Dest = *_First;
  950. _Adjust_heap(_First, _Diff(0), _Diff(_Last - _First), _Val);
  951. }
  952. template<class _RanIt,
  953. class _Ty> inline
  954. void _Pop_heap_0(_RanIt _First, _RanIt _Last, _Ty *)
  955. { // pop *_First to *(_Last - 1) and reheap, using operator<
  956. _Pop_heap(_First, _Last - 1, _Last - 1,
  957. _Ty(*(_Last - 1)), _Dist_type(_First));
  958. }
  959. template<class _RanIt> inline
  960. void pop_heap(_RanIt _First, _RanIt _Last)
  961. { // pop *_First to *(_Last - 1) and reheap, using operator<
  962. if (1 < _Last - _First)
  963. _Pop_heap_0(_First, _Last, _Val_type(_First));
  964. }
  965. // TEMPLATE FUNCTION pop_heap WITH PRED
  966. template<class _RanIt,
  967. class _Diff,
  968. class _Ty,
  969. class _Pr> inline
  970. void _Adjust_heap(_RanIt _First, _Diff _Hole, _Diff _Bottom,
  971. _Ty _Val, _Pr _Pred)
  972. { // percolate _Hole to _Bottom, then push _Val, using _Pred
  973. _Diff _Top = _Hole;
  974. _Diff _Idx = 2 * _Hole + 2;
  975. for (; _Idx < _Bottom; _Idx = 2 * _Idx + 2)
  976. { // move _Hole down to larger child
  977. if (_Pred(*(_First + _Idx), *(_First + (_Idx - 1))))
  978. --_Idx;
  979. *(_First + _Hole) = *(_First + _Idx), _Hole = _Idx;
  980. }
  981. if (_Idx == _Bottom)
  982. { // only child at bottom, move _Hole down to it
  983. *(_First + _Hole) = *(_First + (_Bottom - 1));
  984. _Hole = _Bottom - 1;
  985. }
  986. _Push_heap(_First, _Hole, _Top, _Val, _Pred);
  987. }
  988. template<class _RanIt,
  989. class _Diff,
  990. class _Ty,
  991. class _Pr> inline
  992. void _Pop_heap(_RanIt _First, _RanIt _Last, _RanIt _Dest,
  993. _Ty _Val, _Pr _Pred, _Diff *)
  994. { // pop *_First to *_Dest and reheap, using _Pred
  995. *_Dest = *_First;
  996. _Adjust_heap(_First, _Diff(0), _Diff(_Last - _First), _Val, _Pred);
  997. }
  998. template<class _RanIt,
  999. class _Ty,
  1000. class _Pr> inline
  1001. void _Pop_heap_0(_RanIt _First, _RanIt _Last, _Pr _Pred, _Ty *)
  1002. { // pop *_First to *(_Last - 1) and reheap, using _Pred
  1003. _Pop_heap(_First, _Last - 1, _Last - 1,
  1004. _Ty(*(_Last - 1)), _Pred, _Dist_type(_First));
  1005. }
  1006. template<class _RanIt,
  1007. class _Pr> inline
  1008. void pop_heap(_RanIt _First, _RanIt _Last, _Pr _Pred)
  1009. { // pop *_First to *(_Last - 1) and reheap, using _Pred
  1010. if (1 < _Last - _First)
  1011. _Pop_heap_0(_First, _Last, _Pred, _Val_type(_First));
  1012. }
  1013. // TEMPLATE FUNCTION make_heap
  1014. template<class _RanIt,
  1015. class _Diff,
  1016. class _Ty> inline
  1017. void _Make_heap(_RanIt _First, _RanIt _Last, _Diff *, _Ty *)
  1018. { // make nontrivial [_First, _Last) into a heap, using operator<
  1019. _Diff _Bottom = _Last - _First;
  1020. for (_Diff _Hole = _Bottom / 2; 0 < _Hole; )
  1021. { // reheap top half, bottom to top
  1022. --_Hole;
  1023. _Adjust_heap(_First, _Hole, _Bottom, _Ty(*(_First + _Hole)));
  1024. }
  1025. }
  1026. template<class _RanIt> inline
  1027. void make_heap(_RanIt _First, _RanIt _Last)
  1028. { // make [_First, _Last) into a heap, using operator<
  1029. if (1 < _Last - _First)
  1030. _Make_heap(_First, _Last,
  1031. _Dist_type(_First), _Val_type(_First));
  1032. }
  1033. // TEMPLATE FUNCTION make_heap WITH PRED
  1034. template<class _RanIt,
  1035. class _Diff,
  1036. class _Ty,
  1037. class _Pr> inline
  1038. void _Make_heap(_RanIt _First, _RanIt _Last, _Pr _Pred, _Diff *, _Ty *)
  1039. { // make nontrivial [_First, _Last) into a heap, using _Pred
  1040. _Diff _Bottom = _Last - _First;
  1041. for (_Diff _Hole = _Bottom / 2; 0 < _Hole; )
  1042. { // reheap top half, bottom to top
  1043. --_Hole;
  1044. _Adjust_heap(_First, _Hole, _Bottom,
  1045. _Ty(*(_First + _Hole)), _Pred);
  1046. }
  1047. }
  1048. template<class _RanIt,
  1049. class _Pr> inline
  1050. void make_heap(_RanIt _First, _RanIt _Last, _Pr _Pred)
  1051. { // make [_First, _Last) into a heap, using _Pred
  1052. if (1 < _Last - _First)
  1053. _Make_heap(_First, _Last, _Pred,
  1054. _Dist_type(_First), _Val_type(_First));
  1055. }
  1056. // TEMPLATE FUNCTION sort_heap
  1057. template<class _RanIt> inline
  1058. void sort_heap(_RanIt _First, _RanIt _Last)
  1059. { // order heap by repeatedly popping, using operator<
  1060. for (; 1 < _Last - _First; --_Last)
  1061. std::pop_heap(_First, _Last);
  1062. }
  1063. // TEMPLATE FUNCTION sort_heap WITH PRED
  1064. template<class _RanIt,
  1065. class _Pr> inline
  1066. void sort_heap(_RanIt _First, _RanIt _Last, _Pr _Pred)
  1067. { // order heap by repeatedly popping, using _Pred
  1068. for (; 1 < _Last - _First; --_Last)
  1069. std::pop_heap(_First, _Last, _Pred);
  1070. }
  1071. // TEMPLATE FUNCTION lower_bound
  1072. template<class _FwdIt,
  1073. class _Ty,
  1074. class _Diff> inline
  1075. _FwdIt _Lower_bound(_FwdIt _First, _FwdIt _Last, const _Ty& _Val, _Diff *)
  1076. { // find first element not before _Val, using operator<
  1077. _Diff _Count = 0;
  1078. _Distance(_First, _Last, _Count);
  1079. for (; 0 < _Count; )
  1080. { // divide and conquer, find half that contains answer
  1081. _Diff _Count2 = _Count / 2;
  1082. _FwdIt _Mid = _First;
  1083. std::advance(_Mid, _Count2);
  1084. if (*_Mid < _Val)
  1085. _First = ++_Mid, _Count -= _Count2 + 1;
  1086. else
  1087. _Count = _Count2;
  1088. }
  1089. return (_First);
  1090. }
  1091. template<class _FwdIt,
  1092. class _Ty> inline
  1093. _FwdIt lower_bound(_FwdIt _First, _FwdIt _Last, const _Ty& _Val)
  1094. { // find first element not before _Val, using operator<
  1095. return (_Lower_bound(_First, _Last, _Val, _Dist_type(_First)));
  1096. }
  1097. // TEMPLATE FUNCTION lower_bound WITH PRED
  1098. template<class _FwdIt,
  1099. class _Ty,
  1100. class _Diff,
  1101. class _Pr> inline
  1102. _FwdIt _Lower_bound(_FwdIt _First, _FwdIt _Last,
  1103. const _Ty& _Val, _Pr _Pred, _Diff *)
  1104. { // find first element not before _Val, using _Pred
  1105. _Diff _Count = 0;
  1106. _Distance(_First, _Last, _Count);
  1107. for (; 0 < _Count; )
  1108. { // divide and conquer, find half that contains answer
  1109. _Diff _Count2 = _Count / 2;
  1110. _FwdIt _Mid = _First;
  1111. std::advance(_Mid, _Count2);
  1112. if (_Pred(*_Mid, _Val))
  1113. _First = ++_Mid, _Count -= _Count2 + 1;
  1114. else
  1115. _Count = _Count2;
  1116. }
  1117. return (_First);
  1118. }
  1119. template<class _FwdIt,
  1120. class _Ty,
  1121. class _Pr> inline
  1122. _FwdIt lower_bound(_FwdIt _First, _FwdIt _Last,
  1123. const _Ty& _Val, _Pr _Pred)
  1124. { // find first element not before _Val, using _Pred
  1125. return (_Lower_bound(_First, _Last, _Val, _Pred, _Dist_type(_First)));
  1126. }
  1127. // TEMPLATE FUNCTION upper_bound
  1128. template<class _FwdIt,
  1129. class _Ty,
  1130. class _Diff> inline
  1131. _FwdIt _Upper_bound(_FwdIt _First, _FwdIt _Last, const _Ty& _Val, _Diff *)
  1132. { // find first element that _Val is before, using operator<
  1133. _Diff _Count = 0;
  1134. _Distance(_First, _Last, _Count);
  1135. for (; 0 < _Count; )
  1136. { // divide and conquer, find half that contains answer
  1137. _Diff _Count2 = _Count / 2;
  1138. _FwdIt _Mid = _First;
  1139. std::advance(_Mid, _Count2);
  1140. if (!(_Val < *_Mid))
  1141. _First = ++_Mid, _Count -= _Count2 + 1;
  1142. else
  1143. _Count = _Count2;
  1144. }
  1145. return (_First);
  1146. }
  1147. template<class _FwdIt,
  1148. class _Ty> inline
  1149. _FwdIt upper_bound(_FwdIt _First, _FwdIt _Last, const _Ty& _Val)
  1150. { // find first element that _Val is before, using operator<
  1151. return (_Upper_bound(_First, _Last, _Val, _Dist_type(_First)));
  1152. }
  1153. // TEMPLATE FUNCTION upper_bound WITH PRED
  1154. template<class _FwdIt,
  1155. class _Ty,
  1156. class _Diff,
  1157. class _Pr> inline
  1158. _FwdIt _Upper_bound(_FwdIt _First, _FwdIt _Last,
  1159. const _Ty& _Val, _Pr _Pred, _Diff *)
  1160. { // find first element that _Val is before, using _Pred
  1161. _Diff _Count = 0;
  1162. _Distance(_First, _Last, _Count);
  1163. for (; 0 < _Count; )
  1164. { // divide and conquer, find half that contains answer
  1165. _Diff _Count2 = _Count / 2;
  1166. _FwdIt _Mid = _First;
  1167. std::advance(_Mid, _Count2);
  1168. if (!_Pred(_Val, *_Mid))
  1169. _First = ++_Mid, _Count -= _Count2 + 1;
  1170. else
  1171. _Count = _Count2;
  1172. }
  1173. return (_First);
  1174. }
  1175. template<class _FwdIt,
  1176. class _Ty,
  1177. class _Pr> inline
  1178. _FwdIt upper_bound(_FwdIt _First, _FwdIt _Last,
  1179. const _Ty& _Val, _Pr _Pred)
  1180. { // find first element that _Val is before, using _Pred
  1181. return (_Upper_bound(_First, _Last, _Val, _Pred, _Dist_type(_First)));
  1182. }
  1183. // TEMPLATE FUNCTION equal_range
  1184. template<class _FwdIt,
  1185. class _Ty,
  1186. class _Diff> inline
  1187. pair<_FwdIt, _FwdIt> _Equal_range(_FwdIt _First, _FwdIt _Last,
  1188. const _Ty& _Val, _Diff *)
  1189. { // find range equivalent to _Val, using operator<
  1190. _Diff _Count = 0;
  1191. _Distance(_First, _Last, _Count);
  1192. for (; 0 < _Count; )
  1193. { // divide and conquer, check midpoint
  1194. _Diff _Count2 = _Count / 2;
  1195. _FwdIt _Mid = _First;
  1196. std::advance(_Mid, _Count2);
  1197. if (*_Mid < _Val)
  1198. { // range begins above _Mid, loop
  1199. _First = ++_Mid;
  1200. _Count -= _Count2 + 1;
  1201. }
  1202. else if (_Val < *_Mid)
  1203. _Count = _Count2; // range in first half, loop
  1204. else
  1205. { // range straddles mid, find each end and return
  1206. _FwdIt _First2 = lower_bound(_First, _Mid, _Val);
  1207. std::advance(_First, _Count);
  1208. _FwdIt _Last2 = upper_bound(++_Mid, _First, _Val);
  1209. return (pair<_FwdIt, _FwdIt>(_First2, _Last2));
  1210. }
  1211. }
  1212. return (pair<_FwdIt, _FwdIt>(_First, _First)); // empty range
  1213. }
  1214. template<class _FwdIt,
  1215. class _Ty> inline
  1216. pair<_FwdIt, _FwdIt> equal_range(_FwdIt _First, _FwdIt _Last,
  1217. const _Ty& _Val)
  1218. { // find range equivalent to _Val, using operator<
  1219. return (_Equal_range(_First, _Last, _Val, _Dist_type(_First)));
  1220. }
  1221. // TEMPLATE FUNCTION equal_range WITH PRED
  1222. template<class _FwdIt,
  1223. class _Ty,
  1224. class _Diff,
  1225. class _Pr> inline
  1226. pair<_FwdIt, _FwdIt> _Equal_range(_FwdIt _First, _FwdIt _Last,
  1227. const _Ty& _Val, _Pr _Pred, _Diff *)
  1228. { // find range equivalent to _Val, using _Pred
  1229. _Diff _Count = 0;
  1230. _Distance(_First, _Last, _Count);
  1231. for (; 0 < _Count; )
  1232. { // divide and conquer, check midpoint
  1233. _Diff _Count2 = _Count / 2;
  1234. _FwdIt _Mid = _First;
  1235. std::advance(_Mid, _Count2);
  1236. if (_Pred(*_Mid, _Val))
  1237. { // range begins above _Mid, loop
  1238. _First = ++_Mid;
  1239. _Count -= _Count2 + 1;
  1240. }
  1241. else if (_Pred(_Val, *_Mid))
  1242. _Count = _Count2; // range in first half, loop
  1243. else
  1244. { // range straddles _Mid, find each end and return
  1245. _FwdIt _First2 = lower_bound(_First, _Mid, _Val, _Pred);
  1246. std::advance(_First, _Count);
  1247. _FwdIt _Last2 = upper_bound(++_Mid, _First, _Val, _Pred);
  1248. return (pair<_FwdIt, _FwdIt>(_First2, _Last2));
  1249. }
  1250. }
  1251. return (pair<_FwdIt, _FwdIt>(_First, _First)); // empty range
  1252. }
  1253. template<class _FwdIt,
  1254. class _Ty,
  1255. class _Pr> inline
  1256. pair<_FwdIt, _FwdIt> equal_range(_FwdIt _First, _FwdIt _Last,
  1257. const _Ty& _Val, _Pr _Pred)
  1258. { // find range equivalent to _Val, using _Pred
  1259. return (_Equal_range(_First, _Last, _Val, _Pred, _Dist_type(_First)));
  1260. }
  1261. // TEMPLATE FUNCTION binary_search
  1262. template<class _FwdIt,
  1263. class _Ty> inline
  1264. bool binary_search(_FwdIt _First, _FwdIt _Last, const _Ty& _Val)
  1265. { // test if _Val equivalent to some element, using operator<
  1266. _First = std::lower_bound(_First, _Last, _Val);
  1267. return (_First != _Last && !(_Val < *_First));
  1268. }
  1269. // TEMPLATE FUNCTION binary_search WITH PRED
  1270. template<class _FwdIt,
  1271. class _Ty,
  1272. class _Pr> inline
  1273. bool binary_search(_FwdIt _First, _FwdIt _Last,
  1274. const _Ty& _Val, _Pr _Pred)
  1275. { // test if _Val equivalent to some element, using _Pred
  1276. _First = std::lower_bound(_First, _Last, _Val, _Pred);
  1277. return (_First != _Last && !_Pred(_Val, *_First));
  1278. }
  1279. // TEMPLATE FUNCTION merge
  1280. template<class _InIt1,
  1281. class _InIt2,
  1282. class _OutIt> inline
  1283. _OutIt merge(_InIt1 _First1, _InIt1 _Last1,
  1284. _InIt2 _First2, _InIt2 _Last2, _OutIt _Dest)
  1285. { // copy merging ranges, both using operator<
  1286. for (; _First1 != _Last1 && _First2 != _Last2; ++_Dest)
  1287. if (*_First2 < *_First1)
  1288. *_Dest = *_First2, ++_First2;
  1289. else
  1290. *_Dest = *_First1, ++_First1;
  1291. _Dest = std::copy(_First1, _Last1, _Dest); // copy any tail
  1292. return (std::copy(_First2, _Last2, _Dest));
  1293. }
  1294. // TEMPLATE FUNCTION merge WITH PRED
  1295. template<class _InIt1,
  1296. class _InIt2,
  1297. class _OutIt,
  1298. class _Pr> inline
  1299. _OutIt merge(_InIt1 _First1, _InIt1 _Last1,
  1300. _InIt2 _First2, _InIt2 _Last2, _OutIt _Dest, _Pr _Pred)
  1301. { // copy merging ranges, both using _Pred
  1302. for (; _First1 != _Last1 && _First2 != _Last2; ++_Dest)
  1303. if (_Pred(*_First2, *_First1))
  1304. *_Dest = *_First2, ++_First2;
  1305. else
  1306. *_Dest = *_First1, ++_First1;
  1307. _Dest = std::copy(_First1, _Last1, _Dest); // copy any tail
  1308. return (std::copy(_First2, _Last2, _Dest));
  1309. }
  1310. // TEMPLATE FUNCTION inplace_merge
  1311. template<class _BidIt,
  1312. class _Diff,
  1313. class _Ty> inline
  1314. _BidIt _Buffered_rotate(_BidIt _First, _BidIt _Mid, _BidIt _Last,
  1315. _Diff _Count1, _Diff _Count2, _Temp_iterator<_Ty>& _Tempbuf)
  1316. { // rotate [_First, _Last) using temp buffer
  1317. if (_Count1 <= _Count2 && _Count1 <= _Tempbuf._Maxlen())
  1318. { // buffer left partition, then copy parts
  1319. std::copy(_First, _Mid, _Tempbuf._Init());
  1320. std::copy(_Mid, _Last, _First);
  1321. return (std::copy_backward(_Tempbuf._First(), _Tempbuf._Last(),
  1322. _Last));
  1323. }
  1324. else if (_Count2 <= _Tempbuf._Maxlen())
  1325. { // buffer right partition, then copy parts
  1326. std::copy(_Mid, _Last, _Tempbuf._Init());
  1327. std::copy_backward(_First, _Mid, _Last);
  1328. return (std::copy(_Tempbuf._First(), _Tempbuf._Last(), _First));
  1329. }
  1330. else
  1331. { // buffer too small, rotate in place
  1332. std::rotate(_First, _Mid, _Last);
  1333. std::advance(_First, _Count2);
  1334. return (_First);
  1335. }
  1336. }
  1337. template<class _BidIt1,
  1338. class _BidIt2,
  1339. class _BidIt3> inline
  1340. _BidIt3 _Merge_backward(_BidIt1 _First1, _BidIt1 _Last1,
  1341. _BidIt2 _First2, _BidIt2 _Last2, _BidIt3 _Dest)
  1342. { // merge backwards to _Dest, using operator<
  1343. for (; ; )
  1344. if (_First1 == _Last1)
  1345. return (std::copy_backward(_First2, _Last2, _Dest));
  1346. else if (_First2 == _Last2)
  1347. return (std::copy_backward(_First1, _Last1, _Dest));
  1348. else if (*--_Last2 < *--_Last1)
  1349. *--_Dest = *_Last1, ++_Last2;
  1350. else
  1351. *--_Dest = *_Last2, ++_Last1;
  1352. }
  1353. template<class _BidIt,
  1354. class _Diff,
  1355. class _Ty> inline
  1356. void _Buffered_merge(_BidIt _First, _BidIt _Mid, _BidIt _Last,
  1357. _Diff _Count1, _Diff _Count2,
  1358. _Temp_iterator<_Ty>& _Tempbuf)
  1359. { // merge [_First, _Mid) with [_Mid, _Last), using operator<
  1360. if (_Count1 + _Count2 == 2)
  1361. { // order two one-element partitions
  1362. if (*_Mid < *_First)
  1363. std::iter_swap(_First, _Mid);
  1364. }
  1365. else if (_Count1 <= _Count2 && _Count1 <= _Tempbuf._Maxlen())
  1366. { // buffer left partition, then merge
  1367. std::copy(_First, _Mid, _Tempbuf._Init());
  1368. std::merge(_Tempbuf._First(), _Tempbuf._Last(), _Mid, _Last, _First);
  1369. }
  1370. else if (_Count2 <= _Tempbuf._Maxlen())
  1371. { // buffer right partition, then merge
  1372. std::copy(_Mid, _Last, _Tempbuf._Init());
  1373. _Merge_backward(_First, _Mid,
  1374. _Tempbuf._First(), _Tempbuf._Last(), _Last);
  1375. }
  1376. else
  1377. { // buffer too small, divide and conquer
  1378. _BidIt _Firstn, _Lastn;
  1379. _Diff _Count1n, _Count2n;
  1380. if (_Count2 < _Count1)
  1381. { // left larger, cut it in half and partition right to match
  1382. _Count1n = _Count1 / 2, _Count2n = 0;
  1383. _Firstn = _First;
  1384. std::advance(_Firstn, _Count1n);
  1385. _Lastn = std::lower_bound(_Mid, _Last, *_Firstn);
  1386. _Distance(_Mid, _Lastn, _Count2n);
  1387. }
  1388. else
  1389. { // right larger, cut it in half and partition left to match
  1390. _Count1n = 0, _Count2n = _Count2 / 2;
  1391. _Lastn = _Mid;
  1392. std::advance(_Lastn, _Count2n);
  1393. _Firstn = std::upper_bound(_First, _Mid, *_Lastn);
  1394. _Distance(_First, _Firstn, _Count1n);
  1395. }
  1396. _BidIt _Midn = _Buffered_rotate(_Firstn, _Mid, _Lastn,
  1397. _Count1 - _Count1n, _Count2n, _Tempbuf); // rearrange middle
  1398. _Buffered_merge(_First, _Firstn, _Midn,
  1399. _Count1n, _Count2n, _Tempbuf); // merge each new part
  1400. _Buffered_merge(_Midn, _Lastn, _Last,
  1401. _Count1 - _Count1n, _Count2 - _Count2n, _Tempbuf);
  1402. }
  1403. }
  1404. template<class _BidIt,
  1405. class _Diff,
  1406. class _Ty> inline
  1407. void _Inplace_merge(_BidIt _First, _BidIt _Mid, _BidIt _Last,
  1408. _Diff *, _Ty *)
  1409. { // merge [_First, _Mid) with [_Mid, _Last), using operator<
  1410. _Diff _Count1 = 0;
  1411. _Distance(_First, _Mid, _Count1);
  1412. _Diff _Count2 = 0;
  1413. _Distance(_Mid, _Last, _Count2);
  1414. _Temp_iterator<_Ty> _Tempbuf(_Count1 < _Count2 ? _Count1 : _Count2);
  1415. _Buffered_merge(_First, _Mid, _Last,
  1416. _Count1, _Count2, _Tempbuf);
  1417. }
  1418. template<class _BidIt> inline
  1419. void inplace_merge(_BidIt _First, _BidIt _Mid, _BidIt _Last)
  1420. { // merge [_First, _Mid) with [_Mid, _Last), using operator<
  1421. if (_First != _Mid && _Mid != _Last)
  1422. _Inplace_merge(_First, _Mid, _Last,
  1423. _Dist_type(_First), _Val_type(_First));
  1424. }
  1425. // TEMPLATE FUNCTION inplace_merge WITH PRED
  1426. template<class _BidIt1,
  1427. class _BidIt2,
  1428. class _BidIt3,
  1429. class _Pr> inline
  1430. _BidIt3 _Merge_backward(_BidIt1 _First1, _BidIt1 _Last1,
  1431. _BidIt2 _First2, _BidIt2 _Last2, _BidIt3 _Dest, _Pr _Pred)
  1432. { // merge backwards to _Dest, using _Pred
  1433. for (; ; )
  1434. if (_First1 == _Last1)
  1435. return (std::copy_backward(_First2, _Last2, _Dest));
  1436. else if (_First2 == _Last2)
  1437. return (std::copy_backward(_First1, _Last1, _Dest));
  1438. else if (_Pred(*--_Last2, *--_Last1))
  1439. *--_Dest = *_Last1, ++_Last2;
  1440. else
  1441. *--_Dest = *_Last2, ++_Last1;
  1442. }
  1443. template<class _BidIt,
  1444. class _Diff,
  1445. class _Ty,
  1446. class _Pr> inline
  1447. void _Buffered_merge(_BidIt _First, _BidIt _Mid, _BidIt _Last,
  1448. _Diff _Count1, _Diff _Count2,
  1449. _Temp_iterator<_Ty>& _Tempbuf, _Pr _Pred)
  1450. { // merge [_First, _Mid) with [_Mid, _Last), using _Pred
  1451. if (_Count1 + _Count2 == 2)
  1452. { // order two one-element partitions
  1453. if (_Pred(*_Mid, *_First))
  1454. std::iter_swap(_First, _Mid);
  1455. }
  1456. else if (_Count1 <= _Count2 && _Count1 <= _Tempbuf._Maxlen())
  1457. { // buffer left partition, then merge
  1458. std::copy(_First, _Mid, _Tempbuf._Init());
  1459. std::merge(_Tempbuf._First(), _Tempbuf._Last(),
  1460. _Mid, _Last, _First, _Pred);
  1461. }
  1462. else if (_Count2 <= _Tempbuf._Maxlen())
  1463. { // buffer right partition, then merge
  1464. std::copy(_Mid, _Last, _Tempbuf._Init());
  1465. _Merge_backward(_First, _Mid, _Tempbuf._First(), _Tempbuf._Last(),
  1466. _Last, _Pred);
  1467. }
  1468. else
  1469. { // buffer too small, divide and conquer
  1470. _BidIt _Firstn, _Lastn;
  1471. _Diff _Count1n, _Count2n;
  1472. if (_Count2 < _Count1)
  1473. { // left larger, cut it in half and partition right to match
  1474. _Count1n = _Count1 / 2, _Count2n = 0;
  1475. _Firstn = _First;
  1476. std::advance(_Firstn, _Count1n);
  1477. _Lastn = lower_bound(_Mid, _Last, *_Firstn, _Pred);
  1478. _Distance(_Mid, _Lastn, _Count2n);
  1479. }
  1480. else
  1481. { // right larger, cut it in half and partition left to match
  1482. _Count1n = 0, _Count2n = _Count2 / 2;
  1483. _Lastn = _Mid;
  1484. std::advance(_Lastn, _Count2n);
  1485. _Firstn = upper_bound(_First, _Mid, *_Lastn, _Pred);
  1486. _Distance(_First, _Firstn, _Count1n);
  1487. }
  1488. _BidIt _Midn = _Buffered_rotate(_Firstn, _Mid, _Lastn,
  1489. _Count1 - _Count1n, _Count2n, _Tempbuf); // rearrange middle
  1490. _Buffered_merge(_First, _Firstn, _Midn,
  1491. _Count1n, _Count2n, _Tempbuf, _Pred); // merge each new part
  1492. _Buffered_merge(_Midn, _Lastn, _Last,
  1493. _Count1 - _Count1n, _Count2 - _Count2n, _Tempbuf, _Pred);
  1494. }
  1495. }
  1496. template<class _BidIt,
  1497. class _Diff,
  1498. class _Ty,
  1499. class _Pr> inline
  1500. void _Inplace_merge(_BidIt _First, _BidIt _Mid, _BidIt _Last, _Pr _Pred,
  1501. _Diff *, _Ty *)
  1502. { // merge [_First, _Mid) with [_Mid, _Last), using _Pred
  1503. _Diff _Count1 = 0;
  1504. _Distance(_First, _Mid, _Count1);
  1505. _Diff _Count2 = 0;
  1506. _Distance(_Mid, _Last, _Count2);
  1507. _Temp_iterator<_Ty> _Tempbuf(_Count1 < _Count2 ? _Count1 : _Count2);
  1508. _Buffered_merge(_First, _Mid, _Last,
  1509. _Count1, _Count2, _Tempbuf, _Pred);
  1510. }
  1511. template<class _BidIt,
  1512. class _Pr> inline
  1513. void inplace_merge(_BidIt _First, _BidIt _Mid, _BidIt _Last, _Pr _Pred)
  1514. { // merge [_First, _Mid) with [_Mid, _Last), using _Pred
  1515. if (_First != _Mid && _Mid != _Last)
  1516. _Inplace_merge(_First, _Mid, _Last, _Pred,
  1517. _Dist_type(_First), _Val_type(_First));
  1518. }
  1519. // TEMPLATE FUNCTION sort
  1520. template<class _BidIt> inline
  1521. void _Insertion_sort(_BidIt _First, _BidIt _Last)
  1522. { // insertion sort [_First, _Last), using operator<
  1523. if (_First != _Last)
  1524. for (_BidIt _Next = _First; ++_Next != _Last; )
  1525. if (*_Next < *_First)
  1526. { // found new earliest element, rotate to front
  1527. _BidIt _Next1 = _Next;
  1528. std::rotate(_First, _Next, ++_Next1);
  1529. }
  1530. else
  1531. { // look for insertion point after first
  1532. _BidIt _Dest = _Next;
  1533. for (_BidIt _Dest0 = _Dest; *_Next < *--_Dest0; )
  1534. _Dest = _Dest0;
  1535. if (_Dest != _Next)
  1536. { // rotate into place
  1537. _BidIt _Next1 = _Next;
  1538. std::rotate(_Dest, _Next, ++_Next1);
  1539. }
  1540. }
  1541. }
  1542. template<class _RanIt> inline
  1543. void _Med3(_RanIt _First, _RanIt _Mid, _RanIt _Last)
  1544. { // sort median of three elements to middle
  1545. if (*_Mid < *_First)
  1546. std::iter_swap(_Mid, _First);
  1547. if (*_Last < *_Mid)
  1548. std::iter_swap(_Last, _Mid);
  1549. if (*_Mid < *_First)
  1550. std::iter_swap(_Mid, _First);
  1551. }
  1552. template<class _RanIt> inline
  1553. void _Median(_RanIt _First, _RanIt _Mid, _RanIt _Last)
  1554. { // sort median element to middle
  1555. if (40 < _Last - _First)
  1556. { // median of nine
  1557. int _Step = (_Last - _First + 1) / 8;
  1558. _Med3(_First, _First + _Step, _First + 2 * _Step);
  1559. _Med3(_Mid - _Step, _Mid, _Mid + _Step);
  1560. _Med3(_Last - 2 * _Step, _Last - _Step, _Last);
  1561. _Med3(_First + _Step, _Mid, _Last - _Step);
  1562. }
  1563. else
  1564. _Med3(_First, _Mid, _Last);
  1565. }
  1566. template<class _RanIt> inline
  1567. pair<_RanIt, _RanIt> _Unguarded_partition(_RanIt _First, _RanIt _Last)
  1568. { // partition [_First, _Last), using operator<
  1569. _RanIt _Mid = _First + (_Last - _First) / 2; // sort median to _Mid
  1570. _Median(_First, _Mid, _Last - 1);
  1571. _RanIt _Pfirst = _Mid;
  1572. _RanIt _Plast = _Pfirst + 1;
  1573. while (_First < _Pfirst
  1574. && !(*(_Pfirst - 1) < *_Pfirst)
  1575. && !(*_Pfirst < *(_Pfirst - 1)))
  1576. --_Pfirst;
  1577. while (_Plast < _Last
  1578. && !(*_Plast < *_Pfirst)
  1579. && !(*_Pfirst < *_Plast))
  1580. ++_Plast;
  1581. _RanIt _Gfirst = _Plast;
  1582. _RanIt _Glast = _Pfirst;
  1583. for (; ; )
  1584. { // partition
  1585. for (; _Gfirst < _Last; ++_Gfirst)
  1586. if (*_Pfirst < *_Gfirst)
  1587. ;
  1588. else if (*_Gfirst < *_Pfirst)
  1589. break;
  1590. else
  1591. std::iter_swap(_Plast++, _Gfirst);
  1592. for (; _First < _Glast; --_Glast)
  1593. if (*(_Glast - 1) < *_Pfirst)
  1594. ;
  1595. else if (*_Pfirst < *(_Glast - 1))
  1596. break;
  1597. else
  1598. std::iter_swap(--_Pfirst, _Glast - 1);
  1599. if (_Glast == _First && _Gfirst == _Last)
  1600. return (pair<_RanIt, _RanIt>(_Pfirst, _Plast));
  1601. if (_Glast == _First)
  1602. { // no room at bottom, rotate pivot upward
  1603. if (_Plast != _Gfirst)
  1604. std::iter_swap(_Pfirst, _Plast);
  1605. ++_Plast;
  1606. std::iter_swap(_Pfirst++, _Gfirst++);
  1607. }
  1608. else if (_Gfirst == _Last)
  1609. { // no room at top, rotate pivot downward
  1610. if (--_Glast != --_Pfirst)
  1611. std::iter_swap(_Glast, _Pfirst);
  1612. std::iter_swap(_Pfirst, --_Plast);
  1613. }
  1614. else
  1615. std::iter_swap(_Gfirst++, --_Glast);
  1616. }
  1617. }
  1618. template<class _RanIt,
  1619. class _Diff> inline
  1620. void _Sort(_RanIt _First, _RanIt _Last, _Diff _Ideal)
  1621. { // order [_First, _Last), using operator<
  1622. _Diff _Count;
  1623. for (; _ISORT_MAX < (_Count = _Last - _First) && 0 < _Ideal; )
  1624. { // divide and conquer by quicksort
  1625. pair<_RanIt, _RanIt> _Mid = _Unguarded_partition(_First, _Last);
  1626. _Ideal /= 2, _Ideal += _Ideal / 2; // allow 1.5 log2(N) divisions
  1627. if (_Mid.first - _First < _Last - _Mid.second) // loop on larger half
  1628. _Sort(_First, _Mid.first, _Ideal), _First = _Mid.second;
  1629. else
  1630. _Sort(_Mid.second, _Last, _Ideal), _Last = _Mid.first;
  1631. }
  1632. if (_ISORT_MAX < _Count)
  1633. { // heap sort if too many divisions
  1634. std::make_heap(_First, _Last);
  1635. std::sort_heap(_First, _Last);
  1636. }
  1637. else if (1 < _Count)
  1638. _Insertion_sort(_First, _Last); // small, insertion sort
  1639. }
  1640. template<class _RanIt> inline
  1641. void sort(_RanIt _First, _RanIt _Last)
  1642. { // order [_First, _Last), using operator<
  1643. _Sort(_First, _Last, _Last - _First);
  1644. }
  1645. // TEMPLATE FUNCTION sort WITH PRED
  1646. template<class _BidIt,
  1647. class _Pr> inline
  1648. void _Insertion_sort(_BidIt _First, _BidIt _Last, _Pr _Pred)
  1649. { // insertion sort [_First, _Last), using _Pred
  1650. if (_First != _Last)
  1651. for (_BidIt _Next = _First; ++_Next != _Last; )
  1652. if (_Pred(*_Next, *_First))
  1653. { // found new earliest element, rotate to front
  1654. _BidIt _Next1 = _Next;
  1655. std::rotate(_First, _Next, ++_Next1);
  1656. }
  1657. else
  1658. { // look for insertion point after first
  1659. _BidIt _Dest = _Next;
  1660. for (_BidIt _Dest0 = _Dest; _Pred(*_Next, *--_Dest0); )
  1661. _Dest = _Dest0;
  1662. if (_Dest != _Next)
  1663. { // rotate into place
  1664. _BidIt _Next1 = _Next;
  1665. std::rotate(_Dest, _Next, ++_Next1);
  1666. }
  1667. }
  1668. }
  1669. template<class _RanIt,
  1670. class _Pr> inline
  1671. void _Med3(_RanIt _First, _RanIt _Mid, _RanIt _Last, _Pr _Pred)
  1672. { // sort median of three elements to middle
  1673. if (_Pred(*_Mid, *_First))
  1674. std::iter_swap(_Mid, _First);
  1675. if (_Pred(*_Last, *_Mid))
  1676. std::iter_swap(_Last, _Mid);
  1677. if (_Pred(*_Mid, *_First))
  1678. std::iter_swap(_Mid, _First);
  1679. }
  1680. template<class _RanIt,
  1681. class _Pr> inline
  1682. void _Median(_RanIt _First, _RanIt _Mid, _RanIt _Last, _Pr _Pred)
  1683. { // sort median element to middle
  1684. if (40 < _Last - _First)
  1685. { // median of nine
  1686. int _Step = (_Last - _First + 1) / 8;
  1687. _Med3(_First, _First + _Step, _First + 2 * _Step, _Pred);
  1688. _Med3(_Mid - _Step, _Mid, _Mid + _Step, _Pred);
  1689. _Med3(_Last - 2 * _Step, _Last - _Step, _Last, _Pred);
  1690. _Med3(_First + _Step, _Mid, _Last - _Step, _Pred);
  1691. }
  1692. else
  1693. _Med3(_First, _Mid, _Last, _Pred);
  1694. }
  1695. template<class _RanIt,
  1696. class _Pr> inline
  1697. pair<_RanIt, _RanIt> _Unguarded_partition(_RanIt _First, _RanIt _Last,
  1698. _Pr _Pred)
  1699. { // partition [_First, _Last), using _Pred
  1700. _RanIt _Mid = _First + (_Last - _First) / 2;
  1701. _Median(_First, _Mid, _Last - 1, _Pred);
  1702. _RanIt _Pfirst = _Mid;
  1703. _RanIt _Plast = _Pfirst + 1;
  1704. while (_First < _Pfirst
  1705. && !_Pred(*(_Pfirst - 1), *_Pfirst)
  1706. && !_Pred(*_Pfirst, *(_Pfirst - 1)))
  1707. --_Pfirst;
  1708. while (_Plast < _Last
  1709. && !_Pred(*_Plast, *_Pfirst)
  1710. && !_Pred(*_Pfirst, *_Plast))
  1711. ++_Plast;
  1712. _RanIt _Gfirst = _Plast;
  1713. _RanIt _Glast = _Pfirst;
  1714. for (; ; )
  1715. { // partition
  1716. for (; _Gfirst < _Last; ++_Gfirst)
  1717. if (_Pred(*_Pfirst, *_Gfirst))
  1718. ;
  1719. else if (_Pred(*_Gfirst, *_Pfirst))
  1720. break;
  1721. else
  1722. std::iter_swap(_Plast++, _Gfirst);
  1723. for (; _First < _Glast; --_Glast)
  1724. if (_Pred(*(_Glast - 1), *_Pfirst))
  1725. ;
  1726. else if (_Pred(*_Pfirst, *(_Glast - 1)))
  1727. break;
  1728. else
  1729. std::iter_swap(--_Pfirst, _Glast - 1);
  1730. if (_Glast == _First && _Gfirst == _Last)
  1731. return (pair<_RanIt, _RanIt>(_Pfirst, _Plast));
  1732. if (_Glast == _First)
  1733. { // no room at bottom, rotate pivot upward
  1734. if (_Plast != _Gfirst)
  1735. std::iter_swap(_Pfirst, _Plast);
  1736. ++_Plast;
  1737. std::iter_swap(_Pfirst++, _Gfirst++);
  1738. }
  1739. else if (_Gfirst == _Last)
  1740. { // no room at top, rotate pivot downward
  1741. if (--_Glast != --_Pfirst)
  1742. std::iter_swap(_Glast, _Pfirst);
  1743. std::iter_swap(_Pfirst, --_Plast);
  1744. }
  1745. else
  1746. std::iter_swap(_Gfirst++, --_Glast);
  1747. }
  1748. }
  1749. template<class _RanIt,
  1750. class _Diff,
  1751. class _Pr> inline
  1752. void _Sort(_RanIt _First, _RanIt _Last, _Diff _Ideal, _Pr _Pred)
  1753. { // order [_First, _Last), using _Pred
  1754. _Diff _Count;
  1755. for (; _ISORT_MAX < (_Count = _Last - _First) && 0 < _Ideal; )
  1756. { // divide and conquer by quicksort
  1757. pair<_RanIt, _RanIt> _Mid =
  1758. _Unguarded_partition(_First, _Last, _Pred);
  1759. _Ideal /= 2, _Ideal += _Ideal / 2; // allow 1.5 log2(N) divisions
  1760. if (_Mid.first - _First < _Last - _Mid.second) // loop on larger half
  1761. _Sort(_First, _Mid.first, _Ideal, _Pred), _First = _Mid.second;
  1762. else
  1763. _Sort(_Mid.second, _Last, _Ideal, _Pred), _Last = _Mid.first;
  1764. }
  1765. if (_ISORT_MAX < _Count)
  1766. { // heap sort if too many divisions
  1767. std::make_heap(_First, _Last, _Pred);
  1768. std::sort_heap(_First, _Last, _Pred);
  1769. }
  1770. else if (1 < _Count)
  1771. _Insertion_sort(_First, _Last, _Pred); // small, insertion sort
  1772. }
  1773. template<class _RanIt,
  1774. class _Pr> inline
  1775. void sort(_RanIt _First, _RanIt _Last, _Pr _Pred)
  1776. { // order [_First, _Last), using _Pred
  1777. _Sort(_First, _Last, _Last - _First, _Pred);
  1778. }
  1779. // TEMPLATE FUNCTION stable_sort
  1780. template<class _BidIt,
  1781. class _OutIt,
  1782. class _Diff> inline
  1783. void _Chunked_merge(_BidIt _First, _BidIt _Last, _OutIt _Dest,
  1784. _Diff _Chunk, _Diff _Count)
  1785. { // copy merging chunks, using operator<
  1786. for (_Diff _Chunk2 = _Chunk * 2; _Chunk2 <= _Count; _Count -= _Chunk2)
  1787. { // copy merging pairs of adjacent chunks
  1788. _BidIt _Mid1 = _First;
  1789. std::advance(_Mid1, _Chunk);
  1790. _BidIt _Mid2 = _Mid1;
  1791. std::advance(_Mid2, _Chunk);
  1792. _Dest = std::merge(_First, _Mid1, _Mid1, _Mid2, _Dest);
  1793. _First = _Mid2;
  1794. }
  1795. if (_Count <= _Chunk)
  1796. std::copy(_First, _Last, _Dest); // copy partial last chunk
  1797. else
  1798. { // copy merging whole and partial last chunk
  1799. _BidIt _Mid = _First;
  1800. std::advance(_Mid, _Chunk);
  1801. std::merge(_First, _Mid, _Mid, _Last, _Dest);
  1802. }
  1803. }
  1804. template<class _BidIt,
  1805. class _Diff,
  1806. class _Ty> inline
  1807. void _Buffered_merge_sort(_BidIt _First, _BidIt _Last, _Diff _Count,
  1808. _Temp_iterator<_Ty>& _Tempbuf)
  1809. { // sort using temp buffer for merges, using operator<
  1810. _BidIt _Mid = _First;
  1811. for (_Diff _Nleft = _Count; _ISORT_MAX <= _Nleft; _Nleft -= _ISORT_MAX)
  1812. { // sort chunks
  1813. _BidIt _Midend = _Mid;
  1814. std::advance(_Midend, (int)_ISORT_MAX);
  1815. _Insertion_sort(_Mid, _Midend);
  1816. _Mid = _Midend;
  1817. }
  1818. _Insertion_sort(_Mid, _Last); // sort partial last chunk
  1819. for (_Diff _Chunk = _ISORT_MAX; _Chunk < _Count; _Chunk *= 2)
  1820. { // merge adjacent pairs of chunks to and from temp buffer
  1821. _Chunked_merge(_First, _Last, _Tempbuf._Init(),
  1822. _Chunk, _Count);
  1823. _Chunked_merge(_Tempbuf._First(), _Tempbuf._Last(), _First,
  1824. _Chunk *= 2, _Count);
  1825. }
  1826. }
  1827. template<class _BidIt,
  1828. class _Diff,
  1829. class _Ty> inline
  1830. void _Stable_sort(_BidIt _First, _BidIt _Last, _Diff _Count,
  1831. _Temp_iterator<_Ty>& _Tempbuf)
  1832. { // sort preserving order of equivalents, using operator<
  1833. if (_Count <= _ISORT_MAX)
  1834. _Insertion_sort(_First, _Last); // small, insertion sort
  1835. else
  1836. { // sort halves and merge
  1837. _Diff _Count2 = (_Count + 1) / 2;
  1838. _BidIt _Mid = _First;
  1839. std::advance(_Mid, _Count2);
  1840. if (_Count2 <= _Tempbuf._Maxlen())
  1841. { // temp buffer big enough, sort each half using buffer
  1842. _Buffered_merge_sort(_First, _Mid, _Count2, _Tempbuf);
  1843. _Buffered_merge_sort(_Mid, _Last, _Count - _Count2, _Tempbuf);
  1844. }
  1845. else
  1846. { // temp buffer not big enough, divide and conquer
  1847. _Stable_sort(_First, _Mid, _Count2, _Tempbuf);
  1848. _Stable_sort(_Mid, _Last, _Count - _Count2, _Tempbuf);
  1849. }
  1850. _Buffered_merge(_First, _Mid, _Last,
  1851. _Count2, _Count - _Count2, _Tempbuf); // merge sorted halves
  1852. }
  1853. }
  1854. template<class _BidIt,
  1855. class _Diff,
  1856. class _Ty> inline
  1857. void _Stable_sort(_BidIt _First, _BidIt _Last, _Diff *, _Ty *)
  1858. { // sort preserving order of equivalents, using operator<
  1859. _Diff _Count = 0;
  1860. _Distance(_First, _Last, _Count);
  1861. _Temp_iterator<_Ty> _Tempbuf(_Count);
  1862. _Stable_sort(_First, _Last, _Count, _Tempbuf);
  1863. }
  1864. template<class _BidIt> inline
  1865. void stable_sort(_BidIt _First, _BidIt _Last)
  1866. { // sort preserving order of equivalents, using operator<
  1867. if (_First != _Last)
  1868. _Stable_sort(_First, _Last, _Dist_type(_First), _Val_type(_First));
  1869. }
  1870. // TEMPLATE FUNCTION stable_sort WITH PRED
  1871. template<class _BidIt,
  1872. class _OutIt,
  1873. class _Diff,
  1874. class _Pr> inline
  1875. void _Chunked_merge(_BidIt _First, _BidIt _Last, _OutIt _Dest,
  1876. _Diff _Chunk, _Diff _Count, _Pr _Pred)
  1877. { // copy merging chunks, using _Pred
  1878. for (_Diff _Chunk2 = _Chunk * 2; _Chunk2 <= _Count; _Count -= _Chunk2)
  1879. { // copy merging pairs of adjacent chunks
  1880. _BidIt _Mid1 = _First;
  1881. std::advance(_Mid1, _Chunk);
  1882. _BidIt _Mid2 = _Mid1;
  1883. std::advance(_Mid2, _Chunk);
  1884. _Dest = std::merge(_First, _Mid1, _Mid1, _Mid2, _Dest, _Pred);
  1885. _First = _Mid2;
  1886. }
  1887. if (_Count <= _Chunk)
  1888. std::copy(_First, _Last, _Dest); // copy partial last chunk
  1889. else
  1890. { // copy merging whole and partial last chunk
  1891. _BidIt _Mid1 = _First;
  1892. std::advance(_Mid1, _Chunk);
  1893. std::merge(_First, _Mid1, _Mid1, _Last, _Dest, _Pred);
  1894. }
  1895. }
  1896. template<class _BidIt,
  1897. class _Diff,
  1898. class _Ty,
  1899. class _Pr> inline
  1900. void _Buffered_merge_sort(_BidIt _First, _BidIt _Last, _Diff _Count,
  1901. _Temp_iterator<_Ty>& _Tempbuf, _Pr _Pred)
  1902. { // sort using temp buffer for merges, using _Pred
  1903. _BidIt _Mid = _First;
  1904. for (_Diff _Nleft = _Count; _ISORT_MAX <= _Nleft; _Nleft -= _ISORT_MAX)
  1905. { // sort chunks
  1906. _BidIt _Midn = _Mid;
  1907. std::advance(_Midn, (int)_ISORT_MAX);
  1908. _Insertion_sort(_Mid, _Midn, _Pred);
  1909. _Mid = _Midn;
  1910. }
  1911. _Insertion_sort(_Mid, _Last, _Pred); // sort partial last chunk
  1912. for (_Diff _Chunk = _ISORT_MAX; _Chunk < _Count; _Chunk *= 2)
  1913. { // merge adjacent pairs of chunks to and from temp buffer
  1914. _Chunked_merge(_First, _Last, _Tempbuf._Init(),
  1915. _Chunk, _Count, _Pred);
  1916. _Chunked_merge(_Tempbuf._First(), _Tempbuf._Last(), _First,
  1917. _Chunk *= 2, _Count, _Pred);
  1918. }
  1919. }
  1920. template<class _BidIt,
  1921. class _Diff,
  1922. class _Ty,
  1923. class _Pr> inline
  1924. void _Stable_sort(_BidIt _First, _BidIt _Last, _Diff _Count,
  1925. _Temp_iterator<_Ty>& _Tempbuf, _Pr _Pred)
  1926. { // sort preserving order of equivalents, using _Pred
  1927. if (_Count <= _ISORT_MAX)
  1928. _Insertion_sort(_First, _Last, _Pred); // small, insertion sort
  1929. else
  1930. { // sort halves and merge
  1931. _Diff _Count2 = (_Count + 1) / 2;
  1932. _BidIt _Mid = _First;
  1933. std::advance(_Mid, _Count2);
  1934. if (_Count2 <= _Tempbuf._Maxlen())
  1935. { // temp buffer big enough, sort each half using buffer
  1936. _Buffered_merge_sort(_First, _Mid, _Count2, _Tempbuf, _Pred);
  1937. _Buffered_merge_sort(_Mid, _Last, _Count - _Count2,
  1938. _Tempbuf, _Pred);
  1939. }
  1940. else
  1941. { // temp buffer not big enough, divide and conquer
  1942. _Stable_sort(_First, _Mid, _Count2, _Tempbuf, _Pred);
  1943. _Stable_sort(_Mid, _Last, _Count - _Count2, _Tempbuf, _Pred);
  1944. }
  1945. _Buffered_merge(_First, _Mid, _Last,
  1946. _Count2, _Count - _Count2, _Tempbuf, _Pred); // merge halves
  1947. }
  1948. }
  1949. template<class _BidIt,
  1950. class _Diff,
  1951. class _Ty,
  1952. class _Pr> inline
  1953. void _Stable_sort(_BidIt _First, _BidIt _Last, _Diff *, _Ty *, _Pr _Pred)
  1954. { // sort preserving order of equivalents, using _Pred
  1955. _Diff _Count = 0;
  1956. _Distance(_First, _Last, _Count);
  1957. _Temp_iterator<_Ty> _Tempbuf(_Count);
  1958. _Stable_sort(_First, _Last, _Count, _Tempbuf, _Pred);
  1959. }
  1960. template<class _BidIt,
  1961. class _Pr> inline
  1962. void stable_sort(_BidIt _First, _BidIt _Last, _Pr _Pred)
  1963. { // sort preserving order of equivalents, using _Pred
  1964. if (_First != _Last)
  1965. _Stable_sort(_First, _Last,
  1966. _Dist_type(_First), _Val_type(_First), _Pred);
  1967. }
  1968. // TEMPLATE FUNCTION partial_sort
  1969. template<class _RanIt,
  1970. class _Ty> inline
  1971. void _Partial_sort(_RanIt _First, _RanIt _Mid, _RanIt _Last, _Ty *)
  1972. { // order [First, _Last) up to _Mid, using operator<
  1973. std::make_heap(_First, _Mid);
  1974. for (_RanIt _Next = _Mid; _Next < _Last; ++_Next)
  1975. if (*_Next < *_First)
  1976. _Pop_heap(_First, _Mid, _Next, _Ty(*_Next),
  1977. _Dist_type(_First)); // replace top with new largest
  1978. std::sort_heap(_First, _Mid);
  1979. }
  1980. template<class _RanIt> inline
  1981. void partial_sort(_RanIt _First, _RanIt _Mid, _RanIt _Last)
  1982. { // order [First, _Last) up to _Mid, using operator<
  1983. _Partial_sort(_First, _Mid, _Last, _Val_type(_First));
  1984. }
  1985. // TEMPLATE FUNCTION partial_sort WITH PRED
  1986. template<class _RanIt,
  1987. class _Ty,
  1988. class _Pr> inline
  1989. void _Partial_sort(_RanIt _First, _RanIt _Mid, _RanIt _Last,
  1990. _Pr _Pred, _Ty *)
  1991. { // order [First, _Last) up to _Mid, using _Pred
  1992. std::make_heap(_First, _Mid, _Pred);
  1993. for (_RanIt _Next = _Mid; _Next < _Last; ++_Next)
  1994. if (_Pred(*_Next, *_First))
  1995. _Pop_heap(_First, _Mid, _Next, _Ty(*_Next), _Pred,
  1996. _Dist_type(_First)); // replace top with new largest
  1997. std::sort_heap(_First, _Mid, _Pred);
  1998. }
  1999. template<class _RanIt,
  2000. class _Pr> inline
  2001. void partial_sort(_RanIt _First, _RanIt _Mid, _RanIt _Last, _Pr _Pred)
  2002. { // order [First, _Last) up to _Mid, using _Pred
  2003. _Partial_sort(_First, _Mid, _Last, _Pred, _Val_type(_First));
  2004. }
  2005. // TEMPLATE FUNCTION partial_sort_copy
  2006. template<class _InIt,
  2007. class _RanIt,
  2008. class _Diff,
  2009. class _Ty> inline
  2010. _RanIt _Partial_sort_copy(_InIt _First1, _InIt _Last1,
  2011. _RanIt _First2, _RanIt _Last2, _Diff *, _Ty *)
  2012. { // copy [First1, _Last1) into [_First2, _Last2), using operator<
  2013. _RanIt _Mid2 = _First2;
  2014. for (; _First1 != _Last1 && _Mid2 != _Last2; ++_First1, ++_Mid2)
  2015. *_Mid2 = *_First1; // copy min(_Last1 - _First1, _Last2 - _First2)
  2016. std::make_heap(_First2, _Mid2);
  2017. for (; _First1 != _Last1; ++_First1)
  2018. if (*_First1 < *_First2)
  2019. _Adjust_heap(_First2, _Diff(0), _Diff(_Mid2 - _First2),
  2020. _Ty(*_First1)); // replace top with new largest
  2021. std::sort_heap(_First2, _Mid2);
  2022. return (_Mid2);
  2023. }
  2024. template<class _InIt,
  2025. class _RanIt> inline
  2026. _RanIt partial_sort_copy(_InIt _First1, _InIt _Last1,
  2027. _RanIt _First2, _RanIt _Last2)
  2028. { // copy [First1, _Last1) into [_First2, _Last2), using operator<
  2029. return (_First1 == _Last1 || _First2 == _Last2 ? _First2
  2030. : _Partial_sort_copy(_First1, _Last1, _First2, _Last2,
  2031. _Dist_type(_First2), _Val_type(_First1)));
  2032. }
  2033. // TEMPLATE FUNCTION partial_sort_copy WITH PRED
  2034. template<class _InIt,
  2035. class _RanIt,
  2036. class _Diff,
  2037. class _Ty, class _Pr> inline
  2038. _RanIt _Partial_sort_copy(_InIt _First1, _InIt _Last1,
  2039. _RanIt _First2, _RanIt _Last2, _Pr _Pred, _Diff *, _Ty *)
  2040. { // copy [First1, _Last1) into [_First2, _Last2) using _Pred
  2041. _RanIt _Mid2 = _First2;
  2042. for (; _First1 != _Last1 && _Mid2 != _Last2; ++_First1, ++_Mid2)
  2043. *_Mid2 = *_First1; // copy min(_Last1 - _First1, _Last2 - _First2)
  2044. std::make_heap(_First2, _Mid2, _Pred);
  2045. for (; _First1 != _Last1; ++_First1)
  2046. if (_Pred(*_First1, *_First2))
  2047. _Adjust_heap(_First2, _Diff(0), _Diff(_Mid2 - _First2),
  2048. _Ty(*_First1), _Pred); // replace top with new largest
  2049. std::sort_heap(_First2, _Mid2, _Pred);
  2050. return (_Mid2);
  2051. }
  2052. template<class _InIt,
  2053. class _RanIt,
  2054. class _Pr> inline
  2055. _RanIt partial_sort_copy(_InIt _First1, _InIt _Last1,
  2056. _RanIt _First2, _RanIt _Last2, _Pr _Pred)
  2057. { // copy [First1, _Last1) into [_First2, _Last2) using _Pred
  2058. return (_First1 == _Last1 || _First2 == _Last2 ? _First2
  2059. : _Partial_sort_copy(_First1, _Last1, _First2, _Last2, _Pred,
  2060. _Dist_type(_First2), _Val_type(_First1)));
  2061. }
  2062. // TEMPLATE FUNCTION nth_element
  2063. template<class _RanIt> inline
  2064. void nth_element(_RanIt _First, _RanIt _Nth, _RanIt _Last)
  2065. { // order Nth element, using operator<
  2066. for (; _ISORT_MAX < _Last - _First; )
  2067. { // divide and conquer, ordering partition containing Nth
  2068. pair<_RanIt, _RanIt> _Mid =
  2069. _Unguarded_partition(_First, _Last);
  2070. if (_Mid.second <= _Nth)
  2071. _First = _Mid.second;
  2072. else if (_Mid.first <= _Nth)
  2073. return; // Nth inside fat pivot, done
  2074. else
  2075. _Last = _Mid.first;
  2076. }
  2077. _Insertion_sort(_First, _Last); // sort any remainder
  2078. }
  2079. // TEMPLATE FUNCTION nth_element WITH PRED
  2080. template<class _RanIt,
  2081. class _Pr> inline
  2082. void nth_element(_RanIt _First, _RanIt _Nth, _RanIt _Last, _Pr _Pred)
  2083. { // order Nth element, using _Pred
  2084. for (; _ISORT_MAX < _Last - _First; )
  2085. { // divide and conquer, ordering partition containing Nth
  2086. pair<_RanIt, _RanIt> _Mid =
  2087. _Unguarded_partition(_First, _Last, _Pred);
  2088. if (_Mid.second <= _Nth)
  2089. _First = _Mid.second;
  2090. else if (_Mid.first <= _Nth)
  2091. return; // Nth inside fat pivot, done
  2092. else
  2093. _Last = _Mid.first;
  2094. }
  2095. _Insertion_sort(_First, _Last, _Pred); // sort any remainder
  2096. }
  2097. // TEMPLATE FUNCTION includes
  2098. template<class _InIt1,
  2099. class _InIt2> inline
  2100. bool includes(_InIt1 _First1, _InIt1 _Last1,
  2101. _InIt2 _First2, _InIt2 _Last2)
  2102. { // test if all [_First1, _Last1) in [_First2, _Last2), using operator<
  2103. for (; _First1 != _Last1 && _First2 != _Last2; )
  2104. if (*_First2 < *_First1)
  2105. return (false);
  2106. else if (*_First1 < *_First2)
  2107. ++_First1;
  2108. else
  2109. ++_First1, ++_First2;
  2110. return (_First2 == _Last2);
  2111. }
  2112. // TEMPLATE FUNCTION includes WITH PRED
  2113. template<class _InIt1,
  2114. class _InIt2,
  2115. class _Pr> inline
  2116. bool includes(_InIt1 _First1, _InIt1 _Last1,
  2117. _InIt2 _First2, _InIt2 _Last2, _Pr _Pred)
  2118. { // test if set [_First1, _Last1) in [_First2, _Last2), using _Pred
  2119. for (; _First1 != _Last1 && _First2 != _Last2; )
  2120. if (_Pred(*_First2, *_First1))
  2121. return (false);
  2122. else if (_Pred(*_First1, *_First2))
  2123. ++_First1;
  2124. else
  2125. ++_First1, ++_First2;
  2126. return (_First2 == _Last2);
  2127. }
  2128. // TEMPLATE FUNCTION set_union
  2129. template<class _InIt1,
  2130. class _InIt2,
  2131. class _OutIt> inline
  2132. _OutIt set_union(_InIt1 _First1, _InIt1 _Last1,
  2133. _InIt2 _First2, _InIt2 _Last2, _OutIt _Dest)
  2134. { // OR sets [_First1, _Last1) and [_First2, _Last2), using operator<
  2135. for (; _First1 != _Last1 && _First2 != _Last2; )
  2136. if (*_First1 < *_First2)
  2137. *_Dest++ = *_First1, ++_First1;
  2138. else if (*_First2 < *_First1)
  2139. *_Dest++ = *_First2, ++_First2;
  2140. else
  2141. *_Dest++ = *_First1, ++_First1, ++_First2;
  2142. _Dest = std::copy(_First1, _Last1, _Dest);
  2143. return (std::copy(_First2, _Last2, _Dest));
  2144. }
  2145. // TEMPLATE FUNCTION set_union WITH PRED
  2146. template<class _InIt1,
  2147. class _InIt2,
  2148. class _OutIt,
  2149. class _Pr> inline
  2150. _OutIt set_union(_InIt1 _First1, _InIt1 _Last1,
  2151. _InIt2 _First2, _InIt2 _Last2, _OutIt _Dest, _Pr _Pred)
  2152. { // OR sets [_First1, _Last1) and [_First2, _Last2), using _Pred
  2153. for (; _First1 != _Last1 && _First2 != _Last2; )
  2154. if (_Pred(*_First1, *_First2))
  2155. *_Dest++ = *_First1, ++_First1;
  2156. else if (_Pred(*_First2, *_First1))
  2157. *_Dest++ = *_First2, ++_First2;
  2158. else
  2159. *_Dest++ = *_First1, ++_First1, ++_First2;
  2160. _Dest = std::copy(_First1, _Last1, _Dest);
  2161. return (std::copy(_First2, _Last2, _Dest));
  2162. }
  2163. // TEMPLATE FUNCTION set_intersection
  2164. template<class _InIt1,
  2165. class _InIt2,
  2166. class _OutIt> inline
  2167. _OutIt set_intersection(_InIt1 _First1, _InIt1 _Last1,
  2168. _InIt2 _First2, _InIt2 _Last2, _OutIt _Dest)
  2169. { // AND sets [_First1, _Last1) and [_First2, _Last2), using operator<
  2170. for (; _First1 != _Last1 && _First2 != _Last2; )
  2171. if (*_First1 < *_First2)
  2172. ++_First1;
  2173. else if (*_First2 < *_First1)
  2174. ++_First2;
  2175. else
  2176. *_Dest++ = *_First1++, ++_First2;
  2177. return (_Dest);
  2178. }
  2179. // TEMPLATE FUNCTION set_intersection WITH PRED
  2180. template<class _InIt1,
  2181. class _InIt2,
  2182. class _OutIt,
  2183. class _Pr> inline
  2184. _OutIt set_intersection(_InIt1 _First1, _InIt1 _Last1,
  2185. _InIt2 _First2, _InIt2 _Last2, _OutIt _Dest, _Pr _Pred)
  2186. { // AND sets [_First1, _Last1) and [_First2, _Last2), using _Pred
  2187. for (; _First1 != _Last1 && _First2 != _Last2; )
  2188. if (_Pred(*_First1, *_First2))
  2189. ++_First1;
  2190. else if (_Pred(*_First2, *_First1))
  2191. ++_First2;
  2192. else
  2193. *_Dest++ = *_First1++, ++_First2;
  2194. return (_Dest);
  2195. }
  2196. // TEMPLATE FUNCTION set_difference
  2197. template<class _InIt1,
  2198. class _InIt2,
  2199. class _OutIt> inline
  2200. _OutIt set_difference(_InIt1 _First1, _InIt1 _Last1,
  2201. _InIt2 _First2, _InIt2 _Last2, _OutIt _Dest)
  2202. { // take set [_First2, _Last2) from [_First1, _Last1), using operator<
  2203. for (; _First1 != _Last1 && _First2 != _Last2; )
  2204. if (*_First1 < *_First2)
  2205. *_Dest++ = *_First1, ++_First1;
  2206. else if (*_First2 < *_First1)
  2207. ++_First2;
  2208. else
  2209. ++_First1, ++_First2;
  2210. return (std::copy(_First1, _Last1, _Dest));
  2211. }
  2212. // TEMPLATE FUNCTION set_difference WITH PRED
  2213. template<class _InIt1,
  2214. class _InIt2,
  2215. class _OutIt,
  2216. class _Pr> inline
  2217. _OutIt set_difference(_InIt1 _First1, _InIt1 _Last1,
  2218. _InIt2 _First2, _InIt2 _Last2, _OutIt _Dest, _Pr _Pred)
  2219. { // take set [_First2, _Last2) from [_First1, _Last1), using _Pred
  2220. for (; _First1 != _Last1 && _First2 != _Last2; )
  2221. if (_Pred(*_First1, *_First2))
  2222. *_Dest++ = *_First1, ++_First1;
  2223. else if (_Pred(*_First2, *_First1))
  2224. ++_First2;
  2225. else
  2226. ++_First1, ++_First2;
  2227. return (std::copy(_First1, _Last1, _Dest));
  2228. }
  2229. // TEMPLATE FUNCTION set_symmetric_difference
  2230. template<class _InIt1,
  2231. class _InIt2,
  2232. class _OutIt> inline
  2233. _OutIt set_symmetric_difference(_InIt1 _First1, _InIt1 _Last1,
  2234. _InIt2 _First2, _InIt2 _Last2, _OutIt _Dest)
  2235. { // XOR sets [_First1, _Last1) and [_First2, _Last2), using operator<
  2236. for (; _First1 != _Last1 && _First2 != _Last2; )
  2237. if (*_First1 < *_First2)
  2238. *_Dest++ = *_First1, ++_First1;
  2239. else if (*_First2 < *_First1)
  2240. *_Dest++ = *_First2, ++_First2;
  2241. else
  2242. ++_First1, ++_First2;
  2243. _Dest = std::copy(_First1, _Last1, _Dest);
  2244. return (std::copy(_First2, _Last2, _Dest));
  2245. }
  2246. // TEMPLATE FUNCTION set_symmetric_difference WITH PRED
  2247. template<class _InIt1,
  2248. class _InIt2,
  2249. class _OutIt,
  2250. class _Pr> inline
  2251. _OutIt set_symmetric_difference(_InIt1 _First1, _InIt1 _Last1,
  2252. _InIt2 _First2, _InIt2 _Last2, _OutIt _Dest, _Pr _Pred)
  2253. { // XOR sets [_First1, _Last1) and [_First2, _Last2), using _Pred
  2254. for (; _First1 != _Last1 && _First2 != _Last2; )
  2255. if (_Pred(*_First1, *_First2))
  2256. *_Dest++ = *_First1, ++_First1;
  2257. else if (_Pred(*_First2, *_First1))
  2258. *_Dest++ = *_First2, ++_First2;
  2259. else
  2260. ++_First1, ++_First2;
  2261. _Dest = std::copy(_First1, _Last1, _Dest);
  2262. return (std::copy(_First2, _Last2, _Dest));
  2263. }
  2264. // TEMPLATE FUNCTION max_element
  2265. template<class _FwdIt> inline
  2266. _FwdIt max_element(_FwdIt _First, _FwdIt _Last)
  2267. { // find largest element, using operator<
  2268. _FwdIt _Found = _First;
  2269. if (_First != _Last)
  2270. for (; ++_First != _Last; )
  2271. if (*_Found < *_First)
  2272. _Found = _First;
  2273. return (_Found);
  2274. }
  2275. // TEMPLATE FUNCTION max_element WITH PRED
  2276. template<class _FwdIt,
  2277. class _Pr> inline
  2278. _FwdIt max_element(_FwdIt _First, _FwdIt _Last, _Pr _Pred)
  2279. { // find largest element, using _Pred
  2280. _FwdIt _Found = _First;
  2281. if (_First != _Last)
  2282. for (; ++_First != _Last; )
  2283. if (_Pred(*_Found, *_First))
  2284. _Found = _First;
  2285. return (_Found);
  2286. }
  2287. // TEMPLATE FUNCTION min_element
  2288. template<class _FwdIt> inline
  2289. _FwdIt min_element(_FwdIt _First, _FwdIt _Last)
  2290. { // find smallest element, using operator<
  2291. _FwdIt _Found = _First;
  2292. if (_First != _Last)
  2293. for (; ++_First != _Last; )
  2294. if (*_First < *_Found)
  2295. _Found = _First;
  2296. return (_Found);
  2297. }
  2298. // TEMPLATE FUNCTION min_element WITH PRED
  2299. template<class _FwdIt,
  2300. class _Pr> inline
  2301. _FwdIt min_element(_FwdIt _First, _FwdIt _Last, _Pr _Pred)
  2302. { // find smallest element, using _Pred
  2303. _FwdIt _Found = _First;
  2304. if (_First != _Last)
  2305. for (; ++_First != _Last; )
  2306. if (_Pred(*_First, *_Found))
  2307. _Found = _First;
  2308. return (_Found);
  2309. }
  2310. // TEMPLATE FUNCTION next_permutation
  2311. template<class _BidIt> inline
  2312. bool next_permutation(_BidIt _First, _BidIt _Last)
  2313. { // permute and test for pure ascending, using operator<
  2314. _BidIt _Next = _Last;
  2315. if (_First == _Last || _First == --_Next)
  2316. return (false);
  2317. for (; ; )
  2318. { // find rightmost element smaller than successor
  2319. _BidIt _Next1 = _Next;
  2320. if (*--_Next < *_Next1)
  2321. { // swap with rightmost element that's smaller, flip suffix
  2322. _BidIt _Mid = _Last;
  2323. for (; !(*_Next < *--_Mid); )
  2324. ;
  2325. std::iter_swap(_Next, _Mid);
  2326. std::reverse(_Next1, _Last);
  2327. return (true);
  2328. }
  2329. if (_Next == _First)
  2330. { // pure descending, flip all
  2331. std::reverse(_First, _Last);
  2332. return (false);
  2333. }
  2334. }
  2335. }
  2336. // TEMPLATE FUNCTION next_permutation WITH PRED
  2337. template<class _BidIt,
  2338. class _Pr> inline
  2339. bool next_permutation(_BidIt _First, _BidIt _Last, _Pr _Pred)
  2340. { // permute and test for pure ascending, using _Pred
  2341. _BidIt _Next = _Last;
  2342. if (_First == _Last || _First == --_Next)
  2343. return (false);
  2344. for (; ; )
  2345. { // find rightmost element smaller than successor
  2346. _BidIt _Next1 = _Next;
  2347. if (_Pred(*--_Next, *_Next1))
  2348. { // swap with rightmost element that's smaller, flip suffix
  2349. _BidIt _Mid = _Last;
  2350. for (; !_Pred(*_Next, *--_Mid); )
  2351. ;
  2352. std::iter_swap(_Next, _Mid);
  2353. std::reverse(_Next1, _Last);
  2354. return (true);
  2355. }
  2356. if (_Next == _First)
  2357. { // pure descending, flip all
  2358. std::reverse(_First, _Last);
  2359. return (false);
  2360. }
  2361. }
  2362. }
  2363. // TEMPLATE FUNCTION prev_permutation
  2364. template<class _BidIt> inline
  2365. bool prev_permutation(_BidIt _First, _BidIt _Last)
  2366. { // reverse permute and test for pure descending, using operator<
  2367. _BidIt _Next = _Last;
  2368. if (_First == _Last || _First == --_Next)
  2369. return (false);
  2370. for (; ; )
  2371. { // find rightmost element not smaller than successor
  2372. _BidIt _Next1 = _Next;
  2373. if (!(*--_Next < *_Next1))
  2374. { // swap with rightmost element that's not smaller, flip suffix
  2375. _BidIt _Mid = _Last;
  2376. for (; *_Next < *--_Mid; )
  2377. ;
  2378. std::iter_swap(_Next, _Mid);
  2379. std::reverse(_Next1, _Last);
  2380. return (true);
  2381. }
  2382. if (_Next == _First)
  2383. { // pure ascending, flip all
  2384. std::reverse(_First, _Last);
  2385. return (false);
  2386. }
  2387. }
  2388. }
  2389. // TEMPLATE FUNCTION prev_permutation WITH PRED
  2390. template<class _BidIt,
  2391. class _Pr> inline
  2392. bool prev_permutation(_BidIt _First, _BidIt _Last, _Pr _Pred)
  2393. { // reverse permute and test for pure descending, using _Pred
  2394. _BidIt _Next = _Last;
  2395. if (_First == _Last || _First == --_Next)
  2396. return (false);
  2397. for (; ; )
  2398. { // find rightmost element not smaller than successor
  2399. _BidIt _Next1 = _Next;
  2400. if (!_Pred(*--_Next, *_Next1))
  2401. { // swap with rightmost element that's not smaller, flip suffix
  2402. _BidIt _Mid = _Last;
  2403. for (; _Pred(*_Next, *--_Mid); )
  2404. ;
  2405. std::iter_swap(_Next, _Mid);
  2406. std::reverse(_Next1, _Last);
  2407. return (true);
  2408. }
  2409. if (_Next == _First)
  2410. { // pure ascending, flip all
  2411. std::reverse(_First, _Last);
  2412. return (false);
  2413. }
  2414. }
  2415. }
  2416. _STD_END
  2417. #pragma warning(default: 4244)
  2418. #pragma warning(pop)
  2419. #pragma pack(pop)
  2420. #endif /* _ALGORITHM_ */
  2421. /*
  2422. * Copyright (c) 1992-2001 by P.J. Plauger. ALL RIGHTS RESERVED.
  2423. * Consult your license regarding permissions and restrictions.
  2424. */
  2425. /*
  2426. * This file is derived from software bearing the following
  2427. * restrictions:
  2428. *
  2429. * Copyright (c) 1994
  2430. * Hewlett-Packard Company
  2431. *
  2432. * Permission to use, copy, modify, distribute and sell this
  2433. * software and its documentation for any purpose is hereby
  2434. * granted without fee, provided that the above copyright notice
  2435. * appear in all copies and that both that copyright notice and
  2436. * this permission notice appear in supporting documentation.
  2437. * Hewlett-Packard Company makes no representations about the
  2438. * suitability of this software for any purpose. It is provided
  2439. * "as is" without express or implied warranty.
  2440. V3.10:0009 */