Source code of Windows XP (NT5)
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.

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