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.

1989 lines
53 KiB

  1. #pragma once
  2. #ifndef _STLALGOR_H_
  3. #define _STLALGOR_H_
  4. //#include <iterator>
  5. //#include <memory>
  6. //#include <xutility>
  7. #include <stliter.h>
  8. #include <stlmem.h>
  9. #include <stlxutil.h>
  10. #ifdef _MSC_VER
  11. #pragma pack(push,8)
  12. #endif /* _MSC_VER */
  13. _STD_BEGIN
  14. const int _CHUNK_SIZE = 7;
  15. const int _SORT_MAX = 16;
  16. // TEMPLATE FUNCTION _Median
  17. template<class _Ty> inline
  18. _Ty _Median(_Ty _X, _Ty _Y, _Ty _Z)
  19. {
  20. if (_X < _Y)
  21. return (_Y < _Z ? _Y : _X < _Z ? _Z : _X);
  22. else
  23. return (_X < _Z ? _X : _Y < _Z ? _Z : _Y);
  24. }
  25. // TEMPLATE FUNCTION _Median WITH PRED
  26. template<class _Ty, class _Pr> inline
  27. _Ty _Median(_Ty _X, _Ty _Y, _Ty _Z, _Pr _P)
  28. {
  29. if (_P(_X, _Y))
  30. return (_P(_Y, _Z) ? _Y : _P(_X, _Z) ? _Z : _X);
  31. else
  32. return (_P(_X, _Z) ? _X : _P(_Y, _Z) ? _Z : _Y);
  33. }
  34. // TEMPLATE FUNCTION for_each
  35. template<class _II, class _Fn> inline
  36. _Fn for_each(_II _F, _II _L, _Fn _Op)
  37. {
  38. for (; _F != _L; ++_F)
  39. _Op(*_F);
  40. return (_Op);
  41. }
  42. // TEMPLATE FUNCTION find
  43. template<class _II, class _Ty> inline
  44. _II find(_II _F, _II _L, const _Ty& _V)
  45. {
  46. for (; _F != _L; ++_F)
  47. if (*_F == _V)
  48. break;
  49. return (_F);
  50. }
  51. // TEMPLATE FUNCTION find_if
  52. template<class _II, class _Pr> inline
  53. _II find_if(_II _F, _II _L, _Pr _P)
  54. {
  55. for (; _F != _L; ++_F)
  56. if (_P(*_F))
  57. break;
  58. return (_F);
  59. }
  60. // TEMPLATE FUNCTION adjacent_find
  61. template<class _FI> inline
  62. _FI adjacent_find(_FI _F, _FI _L)
  63. {
  64. for (_FI _Fb; (_Fb = _F) != _L && ++_F != _L; )
  65. if (*_Fb == *_F)
  66. return (_Fb);
  67. return (_L);
  68. }
  69. // TEMPLATE FUNCTION adjacent_find WITH PRED
  70. template<class _FI, class _Pr> inline
  71. _FI adjacent_find(_FI _F, _FI _L, _Pr _P)
  72. {
  73. for (_FI _Fb; (_Fb = _F) != _L && ++_F != _L; )
  74. if (_P(*_Fb, *_F))
  75. return (_Fb);
  76. return (_L);
  77. }
  78. // TEMPLATE FUNCTION count
  79. template<class _II, class _Ty> inline
  80. _CNTSIZ(_II) count(_II _F, _II _L, const _Ty& _V)
  81. {
  82. _CNTSIZ(_II) _N = 0;
  83. for (; _F != _L; ++_F)
  84. if (*_F == _V)
  85. ++_N;
  86. return (_N);
  87. }
  88. // TEMPLATE FUNCTION count_if
  89. template<class _II, class _Pr> inline
  90. _CNTSIZ(_II) count_if(_II _F, _II _L, _Pr _P)
  91. {
  92. _CNTSIZ(_II) _N = 0;
  93. for (; _F != _L; ++_F)
  94. if (_P(*_F))
  95. ++_N;
  96. return (_N);
  97. }
  98. // TEMPLATE FUNCTION search
  99. template<class _FI1, class _FI2> inline
  100. _FI1 search(_FI1 _F1, _FI1 _L1, _FI2 _F2, _FI2 _L2)
  101. {
  102. return (_Search(_F1, _L1, _F2, _L2,
  103. _Dist_type(_F1), _Dist_type(_F2)));
  104. }
  105. template<class _FI1, class _FI2, class _Pd1, class _Pd2> inline
  106. _FI1 _Search(_FI1 _F1, _FI1 _L1, _FI2 _F2, _FI2 _L2,
  107. _Pd1 *, _Pd2 *)
  108. {
  109. _Pd1 _D1 = 0;
  110. _Distance(_F1, _L1, _D1);
  111. _Pd2 _D2 = 0;
  112. _Distance(_F2, _L2, _D2);
  113. for (; _D2 <= _D1; ++_F1, --_D1)
  114. {
  115. _FI1 _X1 = _F1;
  116. for (_FI2 _X2 = _F2; ; ++_X1, ++_X2)
  117. if (_X2 == _L2)
  118. return (_F1);
  119. else if (!(*_X1 == *_X2))
  120. break;
  121. }
  122. return (_L1);
  123. }
  124. // TEMPLATE FUNCTION search WITH PRED
  125. template<class _FI1, class _FI2, class _Pr> inline
  126. _FI1 search(_FI1 _F1, _FI1 _L1, _FI2 _F2, _FI2 _L2, _Pr _P)
  127. {
  128. return (_Search(_F1, _L1, _F2, _L2, _P,
  129. _Dist_type(_F1), _Dist_type(_F2)));
  130. }
  131. template<class _FI1, class _FI2, class _Pd1, class _Pd2,
  132. class _Pr> inline
  133. _FI1 _Search(_FI1 _F1, _FI1 _L1, _FI2 _F2, _FI2 _L2,
  134. _Pr _P, _Pd1 *, _Pd2 *)
  135. {
  136. _Pd1 _D1 = 0;
  137. _Distance(_F1, _L1, _D1);
  138. _Pd2 _D2 = 0;
  139. _Distance(_F2, _L2, _D2);
  140. for (; _D2 <= _D1; ++_F1, --_D1)
  141. {
  142. _FI1 _X1 = _F1;
  143. for (_FI2 _X2 = _F2; ; ++_X1, ++_X2)
  144. if (_X2 == _L2)
  145. return (_F1);
  146. else if (!_P(*_X1, *_X2))
  147. break;
  148. }
  149. return (_L1);
  150. }
  151. // TEMPLATE FUNCTION search_n
  152. template<class _FI1, class _Pd2, class _Ty> inline
  153. _FI1 search_n(_FI1 _F1, _FI1 _L1, _Pd2 _N, const _Ty& _V)
  154. {
  155. return (_Search_n(_F1, _L1, _N, _V, _Dist_type(_F1)));
  156. }
  157. template<class _FI1, class _Pd2, class _Ty, class _Pd1> inline
  158. _FI1 _Search_n(_FI1 _F1, _FI1 _L1,
  159. _Pd2 _N, const _Ty& _V, _Pd1 *)
  160. {
  161. _Pd1 _D1 = 0;
  162. _Distance(_F1, _L1, _D1);
  163. for (; _N <= _D1; ++_F1, --_D1)
  164. {
  165. _FI1 _X1 = _F1;
  166. for (_Pd2 _D2 = _N; ; ++_X1, --_D2)
  167. if (_D2 == 0)
  168. return (_F1);
  169. else if (!(*_X1 == _V))
  170. break;
  171. }
  172. return (_L1);
  173. }
  174. // TEMPLATE FUNCTION search_n WITH PRED
  175. template<class _FI1, class _Pd2, class _Ty, class _Pr> inline
  176. _FI1 search_n(_FI1 _F1, _FI1 _L1,
  177. _Pd2 _N, const _Ty& _V, _Pr _P)
  178. {
  179. return (_Search_n(_F1, _L1,
  180. _N, _V, _P, _Dist_type(_F1)));
  181. }
  182. template<class _FI1, class _Pd2,
  183. class _Ty, class _Pd1, class _Pr> inline
  184. _FI1 _Search_n(_FI1 _F1, _FI1 _L1,
  185. _Pd2 _N, const _Ty& _V, _Pr _P, _Pd1 *)
  186. {
  187. _Pd1 _D1 = 0;
  188. _Distance(_F1, _L1, _D1);
  189. for (; _N <= _D1; ++_F1, --_D1)
  190. {
  191. _FI1 _X1 = _F1;
  192. for (_Pd2 _D2 = _N; ; ++_X1, --_D2)
  193. if (_D2 == 0)
  194. return (_F1);
  195. else if (!_P(*_X1, _V))
  196. break;
  197. }
  198. return (_L1);
  199. }
  200. // TEMPLATE FUNCTION find_end
  201. template<class _FI1, class _FI2> inline
  202. _FI1 find_end(_FI1 _F1, _FI1 _L1, _FI2 _F2, _FI2 _L2)
  203. {
  204. return (_Find_end(_F1, _L1, _F2, _L2,
  205. _Dist_type(_F1), _Dist_type(_F2)));
  206. }
  207. template<class _FI1, class _FI2, class _Pd1, class _Pd2> inline
  208. _FI1 _Find_end(_FI1 _F1, _FI1 _L1, _FI2 _F2, _FI2 _L2,
  209. _Pd1 *, _Pd2 *)
  210. {
  211. _Pd1 _D1 = 0;
  212. _Distance(_F1, _L1, _D1);
  213. _Pd2 _D2 = 0;
  214. _Distance(_F2, _L2, _D2);
  215. _FI1 _Ans = _L1;
  216. if (0 < _D2)
  217. for (; _D2 <= _D1; ++_F1, --_D1)
  218. {
  219. _FI1 _X1 = _F1;
  220. for (_FI2 _X2 = _F2; ; ++_X1)
  221. if (!(*_X1 == *_X2))
  222. break;
  223. else if (++_X2 == _L2)
  224. {
  225. _Ans = _F1;
  226. break;
  227. }
  228. }
  229. return (_Ans);
  230. }
  231. // TEMPLATE FUNCTION find_end WITH PRED
  232. template<class _FI1, class _FI2, class _Pr> inline
  233. _FI1 find_end(_FI1 _F1, _FI1 _L1, _FI2 _F2, _FI2 _L2, _Pr _P)
  234. {
  235. return (_Find_end(_F1, _L1, _F2, _L2, _P,
  236. _Dist_type(_F1), _Dist_type(_F2)));
  237. }
  238. template<class _FI1, class _FI2, class _Pd1, class _Pd2,
  239. class _Pr> inline
  240. _FI1 _Find_end(_FI1 _F1, _FI1 _L1, _FI2 _F2, _FI2 _L2, _Pr _P,
  241. _Pd1 *, _Pd2 *)
  242. {
  243. _Pd1 _D1 = 0;
  244. _Distance(_F1, _L1, _D1);
  245. _Pd2 _D2 = 0;
  246. _Distance(_F2, _L2, _D2);
  247. _FI1 _Ans = _L1;
  248. if (0 < _D2)
  249. for (; _D2 <= _D1; ++_F1, --_D1)
  250. {
  251. _FI1 _X1 = _F1;
  252. for (_FI2 _X2 = _F2; ; ++_X1)
  253. if (!_P(*_X1, *_X2))
  254. break;
  255. else if (++_X2 == _L2)
  256. {
  257. _Ans = _F1;
  258. break;
  259. }
  260. }
  261. return (_Ans);
  262. }
  263. // TEMPLATE FUNCTION find_first_of
  264. template<class _FI1, class _FI2> inline
  265. _FI1 find_first_of(_FI1 _F1, _FI1 _L1, _FI2 _F2, _FI2 _L2)
  266. {
  267. for (; _F1 != _L1; ++_F1)
  268. for (_FI2 _X2 = _F2; _X2 != _L2; ++_X2)
  269. if (*_F1 == *_X2)
  270. return (_F1);
  271. return (_F1);
  272. }
  273. // TEMPLATE FUNCTION find_first_of WITH PRED
  274. template<class _FI1, class _FI2, class _Pr> inline
  275. _FI1 find_first_of(_FI1 _F1, _FI1 _L1, _FI2 _F2, _FI2 _L2,
  276. _Pr _P)
  277. {
  278. for (; _F1 != _L1; ++_F1)
  279. for (_FI2 _X2 = _F2; _X2 != _L2; ++_X2)
  280. if (_P(*_F1, *_X2))
  281. return (_F1);
  282. return (_F1);
  283. }
  284. // TEMPLATE FUNCTION iter_swap
  285. template<class _FI1, class _FI2> inline
  286. void iter_swap(_FI1 _X, _FI2 _Y)
  287. {
  288. _Iter_swap(_X, _Y, _Val_type(_X));
  289. }
  290. template<class _FI1, class _FI2, class _Ty> inline
  291. void _Iter_swap(_FI1 _X, _FI2 _Y, _Ty *)
  292. {
  293. _Ty _Tmp = *_X;
  294. *_X = *_Y, *_Y = _Tmp;
  295. }
  296. // TEMPLATE FUNCTION swap_ranges
  297. template<class _FI1, class _FI2> inline
  298. _FI2 swap_ranges(_FI1 _F, _FI1 _L, _FI2 _X)
  299. {
  300. for (; _F != _L; ++_F, ++_X)
  301. iter_swap(_F, _X);
  302. return (_X);
  303. }
  304. // TEMPLATE FUNCTION transform WITH UNARY OP
  305. template<class _II, class _OI, class _Uop> inline
  306. _OI transform(_II _F, _II _L, _OI _X, _Uop _U)
  307. {
  308. for (; _F != _L; ++_F, ++_X)
  309. *_X = _U(*_F);
  310. return (_X);
  311. }
  312. // TEMPLATE FUNCTION transform WITH BINARY OP
  313. template<class _II1, class _II2, class _OI, class _Bop> inline
  314. _OI transform(_II1 _F1, _II1 _L1, _II2 _F2, _OI _X, _Bop _B)
  315. {
  316. for (; _F1 != _L1; ++_F1, ++_F2, ++_X)
  317. *_X = _B(*_F1, *_F2);
  318. return (_X);
  319. }
  320. // TEMPLATE FUNCTION replace
  321. template<class _FI, class _Ty> inline
  322. void replace(_FI _F, _FI _L, const _Ty& _Vo, const _Ty& _Vn)
  323. {
  324. for (; _F != _L; ++_F)
  325. if (*_F == _Vo)
  326. *_F = _Vn;
  327. }
  328. // TEMPLATE FUNCTION replace_if
  329. template<class _FI, class _Pr, class _Ty> inline
  330. void replace_if(_FI _F, _FI _L, _Pr _P, const _Ty& _V)
  331. {
  332. for (; _F != _L; ++_F)
  333. if (_P(*_F))
  334. *_F = _V;
  335. }
  336. // TEMPLATE FUNCTION replace_copy
  337. template<class _II, class _OI, class _Ty> inline
  338. _OI replace_copy(_II _F, _II _L, _OI _X,
  339. const _Ty& _Vo, const _Ty& _Vn)
  340. {
  341. for (; _F != _L; ++_F, ++_X)
  342. *_X = *_F == _Vo ? _Vn : *_F;
  343. return (_X);
  344. }
  345. // TEMPLATE FUNCTION replace_copy_if
  346. template<class _II, class _OI, class _Pr, class _Ty> inline
  347. _OI replace_copy_if(_II _F, _II _L, _OI _X,
  348. _Pr _P, const _Ty& _V)
  349. {
  350. for (; _F != _L; ++_F, ++_X)
  351. *_X = _P(*_F) ? _V : *_F;
  352. return (_X);
  353. }
  354. // TEMPLATE FUNCTION generate
  355. template<class _FI, class _Gen> inline
  356. void generate(_FI _F, _FI _L, _Gen _G)
  357. {
  358. for (; _F != _L; ++_F)
  359. *_F = _G();
  360. }
  361. // TEMPLATE FUNCTION generate_n
  362. template<class _OI, class _Pd, class _Gen> inline
  363. void generate_n(_OI _F, _Pd _N, _Gen _G)
  364. {
  365. for (; 0 < _N; --_N, ++_F)
  366. *_F = _G();
  367. }
  368. // TEMPLATE FUNCTION remove
  369. template<class _FI, class _Ty> inline
  370. _FI remove(_FI _F, _FI _L, const _Ty& _V)
  371. {
  372. _F = find(_F, _L, _V);
  373. if (_F == _L)
  374. return (_F);
  375. else
  376. {
  377. _FI _Fb = _F;
  378. return (remove_copy(++_F, _L, _Fb, _V));
  379. }
  380. }
  381. // TEMPLATE FUNCTION remove_if
  382. template<class _FI, class _Pr> inline
  383. _FI remove_if(_FI _F, _FI _L, _Pr _P)
  384. {
  385. _F = find_if(_F, _L, _P);
  386. if (_F == _L)
  387. return (_F);
  388. else
  389. {
  390. _FI _Fb = _F;
  391. return (remove_copy_if(++_F, _L, _Fb, _P));
  392. }
  393. }
  394. // TEMPLATE FUNCTION remove_copy
  395. template<class _II, class _OI, class _Ty> inline
  396. _OI remove_copy(_II _F, _II _L, _OI _X, const _Ty& _V)
  397. {
  398. for (; _F != _L; ++_F)
  399. if (!(*_F == _V))
  400. *_X++ = *_F;
  401. return (_X);
  402. }
  403. // TEMPLATE FUNCTION remove_copy_if
  404. template<class _II, class _OI, class _Pr> inline
  405. _OI remove_copy_if(_II _F, _II _L, _OI _X, _Pr _P)
  406. {
  407. for (; _F != _L; ++_F)
  408. if (!_P(*_F))
  409. *_X++ = *_F;
  410. return (_X);
  411. }
  412. // TEMPLATE FUNCTION unique
  413. template<class _FI> inline
  414. _FI unique(_FI _F, _FI _L)
  415. {
  416. _F = adjacent_find(_F, _L);
  417. return (unique_copy(_F, _L, _F));
  418. }
  419. // TEMPLATE FUNCTION unique WITH PRED
  420. template<class _FI, class _Pr> inline
  421. _FI unique(_FI _F, _FI _L, _Pr _P)
  422. {
  423. _F = adjacent_find(_F, _L, _P);
  424. return (unique_copy(_F, _L, _F, _P));
  425. }
  426. // TEMPLATE FUNCTION unique_copy
  427. template<class _II, class _OI> inline
  428. _OI unique_copy(_II _F, _II _L, _OI _X)
  429. {
  430. return (_F == _L ? _X :
  431. _Unique_copy(_F, _L, _X, _Iter_cat(_F)));
  432. }
  433. template<class _II, class _OI> inline
  434. _OI _Unique_copy(_II _F, _II _L, _OI _X, input_iterator_tag)
  435. {
  436. return (_Unique_copy(_F, _L, _X, _Val_type(_F)));
  437. }
  438. template<class _II, class _OI, class _Ty> inline
  439. _OI _Unique_copy(_II _F, _II _L, _OI _X, _Ty *)
  440. {
  441. _Ty _V = *_F;
  442. for (*_X++ = _V; ++_F != _L; )
  443. if (!(_V == *_F))
  444. _V = *_F, *_X++ = _V;
  445. return (_X);
  446. }
  447. template<class _FI, class _OI> inline
  448. _OI _Unique_copy(_FI _F, _FI _L, _OI _X, forward_iterator_tag)
  449. {
  450. _FI _Fb = _F;
  451. for (*_X++ = *_Fb; ++_F != _L; )
  452. if (!(*_Fb == *_F))
  453. _Fb = _F, *_X++ = *_Fb;
  454. return (_X);
  455. }
  456. template<class _BI, class _OI> inline
  457. _OI _Unique_copy(_BI _F, _BI _L, _OI _X,
  458. bidirectional_iterator_tag)
  459. {
  460. return (_Unique_copy(_F, _L, _X, forward_iterator_tag()));
  461. }
  462. template<class _RI, class _OI> inline
  463. _OI _Unique_copy(_RI _F, _RI _L, _OI _X,
  464. random_access_iterator_tag)
  465. {
  466. return (_Unique_copy(_F, _L, _X, forward_iterator_tag()));
  467. }
  468. // TEMPLATE FUNCTION unique_copy WITH PRED
  469. template<class _II, class _OI, class _Pr> inline
  470. _OI unique_copy(_II _F, _II _L, _OI _X, _Pr _P)
  471. {
  472. return (_F == _L ? _X :
  473. _Unique_copy(_F, _L, _X, _P, _Iter_cat(_F)));
  474. }
  475. template<class _II, class _OI, class _Pr> inline
  476. _OI _Unique_copy(_II _F, _II _L, _OI _X, _Pr _P,
  477. input_iterator_tag)
  478. {
  479. return (_Unique_copy(_F, _L, _X, _P, _Val_type(_F)));
  480. }
  481. template<class _II, class _OI, class _Ty, class _Pr> inline
  482. _OI _Unique_copy(_II _F, _II _L, _OI _X, _Pr _P, _Ty *)
  483. {
  484. _Ty _V = *_F;
  485. for (*_X++ = _V; ++_F != _L; )
  486. if (!_P(_V, *_F))
  487. _V = *_F, *_X++ = _V;
  488. return (_X);
  489. }
  490. template<class _FI, class _OI, class _Pr> inline
  491. _OI _Unique_copy(_FI _F, _FI _L, _OI _X, _Pr _P,
  492. forward_iterator_tag)
  493. {
  494. _FI _Fb = _F;
  495. for (*_X++ = *_Fb; ++_F != _L; )
  496. if (!_P(*_Fb, *_F))
  497. _Fb = _F, *_X++ = *_Fb;
  498. return (_X);
  499. }
  500. template<class _BI, class _OI, class _Pr> inline
  501. _OI _Unique_copy(_BI _F, _BI _L, _OI _X, _Pr _P,
  502. bidirectional_iterator_tag)
  503. {
  504. return (_Unique_copy(_F, _L, _X, _P,
  505. forward_iterator_tag()));
  506. }
  507. template<class _RI, class _OI, class _Pr> inline
  508. _OI _Unique_copy(_RI _F, _RI _L, _OI _X, _Pr _P,
  509. random_access_iterator_tag)
  510. {
  511. return (_Unique_copy(_F, _L, _X, _P,
  512. forward_iterator_tag()));
  513. }
  514. // TEMPLATE FUNCTION reverse
  515. template<class _BI> inline
  516. void reverse(_BI _F, _BI _L)
  517. {
  518. _Reverse(_F, _L, _Iter_cat(_F));
  519. }
  520. template<class _BI> inline
  521. void _Reverse(_BI _F, _BI _L, bidirectional_iterator_tag)
  522. {
  523. for (; _F != _L && _F != --_L; ++_F)
  524. iter_swap(_F, _L);
  525. }
  526. template<class _RI> inline
  527. void _Reverse(_RI _F, _RI _L, random_access_iterator_tag)
  528. {
  529. for (; _F < _L; ++_F)
  530. iter_swap(_F, --_L);
  531. }
  532. // TEMPLATE FUNCTION reverse_copy
  533. template<class _BI, class _OI> inline
  534. _OI reverse_copy(_BI _F, _BI _L, _OI _X)
  535. {
  536. for (; _F != _L; ++_X)
  537. *_X = *--_L;
  538. return (_X);
  539. }
  540. // TEMPLATE FUNCTION rotate
  541. template<class _FI> inline
  542. void rotate(_FI _F, _FI _M, _FI _L)
  543. {
  544. if (_F != _M && _M != _L)
  545. _Rotate(_F, _M, _L, _Iter_cat(_F));
  546. }
  547. template<class _FI> inline
  548. void _Rotate(_FI _F, _FI _M, _FI _L,
  549. forward_iterator_tag)
  550. {
  551. for (_FI _X = _M; ; )
  552. {
  553. iter_swap(_F, _X);
  554. if (++_F == _M)
  555. if (++_X == _L)
  556. break;
  557. else
  558. _M = _X;
  559. else if (++_X == _L)
  560. _X = _M;
  561. }
  562. }
  563. template<class _BI> inline
  564. void _Rotate(_BI _F, _BI _M, _BI _L,
  565. bidirectional_iterator_tag)
  566. {
  567. reverse(_F, _M);
  568. reverse(_M, _L);
  569. reverse(_F, _L);
  570. }
  571. template<class _RI> inline
  572. void _Rotate(_RI _F, _RI _M, _RI _L,
  573. random_access_iterator_tag)
  574. {
  575. _Rotate(_F, _M, _L, _Dist_type(_F), _Val_type(_F));
  576. }
  577. template<class _RI, class _Pd, class _Ty> inline
  578. void _Rotate(_RI _F, _RI _M, _RI _L, _Pd *, _Ty *)
  579. {
  580. _Pd _D = _M - _F;
  581. _Pd _N = _L - _F;
  582. for (_Pd _I = _D; _I != 0; )
  583. {
  584. _Pd _J = _N % _I;
  585. _N = _I, _I = _J;
  586. }
  587. if (_N < _L - _F)
  588. for (; 0 < _N; --_N)
  589. {
  590. _RI _X = _F + _N;
  591. _RI _Y = _X;
  592. _Ty _V = *_X;
  593. _RI _Z = _Y + _D == _L ? _F : _Y + _D;
  594. while (_Z != _X)
  595. {
  596. *_Y = *_Z;
  597. _Y = _Z;
  598. _Z = _D < _L - _Z ? _Z + _D
  599. : _F + (_D - (_L - _Z));
  600. }
  601. *_Y = _V;
  602. }
  603. }
  604. // TEMPLATE FUNCTION rotate_copy
  605. template<class _FI, class _OI> inline
  606. _OI rotate_copy(_FI _F, _FI _M, _FI _L, _OI _X)
  607. {
  608. return (copy(_F, _M, copy(_M, _L, _X)));
  609. }
  610. // TEMPLATE FUNCTION random_shuffle
  611. template<class _RI> inline
  612. void random_shuffle(_RI _F, _RI _L)
  613. {
  614. if (_F != _L)
  615. _Random_shuffle(_F, _L, _Dist_type(_F));
  616. }
  617. template<class _RI, class _Pd> inline
  618. void _Random_shuffle(_RI _F, _RI _L, _Pd *)
  619. {
  620. const int _RBITS = 15;
  621. const int _RMAX = (1U << _RBITS) - 1;
  622. _RI _X = _F;
  623. for (_Pd _D = 1; ++_X != _L; ++_D)
  624. {
  625. unsigned long _Rm = _RMAX;
  626. unsigned long _Rn = rand() & _RMAX;
  627. for (; _Rm < _D && _Rm != ~0UL;
  628. _Rm = _Rm << _RBITS | _RMAX)
  629. _Rn = _Rn << _RBITS | _RMAX;
  630. iter_swap(_X, _F + _Pd(_Rn % _D));
  631. }
  632. }
  633. template<class _RI, class _Pf> inline
  634. void random_shuffle(_RI _F, _RI _L, _Pf& _R)
  635. {
  636. if (_F != _L)
  637. _Random_shuffle(_F, _L, _R, _Dist_type(_F));
  638. }
  639. template<class _RI, class _Pf, class _Pd> inline
  640. void _Random_shuffle(_RI _F, _RI _L, _Pf& _R, _Pd *)
  641. {
  642. _RI _X = _F;
  643. for (_Pd _D = 1; ++_X != _L; ++_D)
  644. iter_swap(_X, _F + _Pd(_R(_D)));
  645. }
  646. // TEMPLATE FUNCTION partition
  647. template<class _BI, class _Pr> inline
  648. _BI partition(_BI _F, _BI _L, _Pr _P)
  649. {
  650. for (; ; ++_F)
  651. {
  652. for (; _F != _L && _P(*_F); ++_F)
  653. ;
  654. if (_F == _L)
  655. break;
  656. for (; _F != --_L && !_P(*_L); )
  657. ;
  658. if (_F == _L)
  659. break;
  660. iter_swap(_F, _L);
  661. }
  662. return (_F);
  663. }
  664. // TEMPLATE FUNCTION stable_partition
  665. template<class _FI, class _Pr> inline
  666. _FI stable_partition(_FI _F, _FI _L, _Pr _P)
  667. {
  668. return (_F == _L ? _F : _Stable_partition(_F, _L, _P,
  669. _Dist_type(_F), _Val_type(_F)));
  670. }
  671. template<class _FI, class _Pr, class _Pd, class _Ty> inline
  672. _FI _Stable_partition(_FI _F, _FI _L, _Pr _P, _Pd *, _Ty *)
  673. {
  674. _Pd _N = 0;
  675. _Distance(_F, _L, _N);
  676. _Temp_iterator<_Ty> _Xb(_N);
  677. return (_Stable_partition(_F, _L, _P, _N, _Xb));
  678. }
  679. template<class _FI, class _Pr, class _Pd, class _Ty> inline
  680. _FI _Stable_partition(_FI _F, _FI _L, _Pr _P, _Pd _N,
  681. _Temp_iterator<_Ty>& _Xb)
  682. {
  683. if (_N == 1)
  684. return (_P(*_F) ? _L : _F);
  685. else if (_N <= _Xb._Maxlen())
  686. {
  687. _FI _X = _F;
  688. for (_Xb._Init(); _F != _L; ++_F)
  689. if (_P(*_F))
  690. *_X++ = *_F;
  691. else
  692. *_Xb++ = *_F;
  693. copy(_Xb._First(), _Xb._Last(), _X);
  694. return (_X);
  695. }
  696. else
  697. {
  698. _FI _M = _F;
  699. advance(_M, _N / 2);
  700. _FI _Lp = _Stable_partition(_F, _M, _P, _N / 2, _Xb);
  701. _FI _Rp = _Stable_partition(_M, _L, _P, _N - _N / 2, _Xb);
  702. _Pd _D1 = 0;
  703. _Distance(_Lp, _M, _D1);
  704. _Pd _D2 = 0;
  705. _Distance(_M, _Rp, _D2);
  706. return (_Buffered_rotate(_Lp, _M, _Rp, _D1, _D2, _Xb));
  707. }
  708. }
  709. // TEMPLATE FUNCTION sort
  710. template<class _RI> inline
  711. void sort(_RI _F, _RI _L)
  712. {
  713. _Sort_0(_F, _L, _Val_type(_F));
  714. }
  715. template<class _RI, class _Ty> inline
  716. void _Sort_0(_RI _F, _RI _L, _Ty *)
  717. {
  718. if (_L - _F <= _SORT_MAX)
  719. _Insertion_sort(_F, _L);
  720. else
  721. {
  722. _Sort(_F, _L, (_Ty *)0);
  723. _Insertion_sort(_F, _F + _SORT_MAX);
  724. for (_F += _SORT_MAX; _F != _L; ++_F)
  725. _Unguarded_insert(_F, _Ty(*_F));
  726. }
  727. }
  728. template<class _RI, class _Ty> inline
  729. void _Sort(_RI _F, _RI _L, _Ty *)
  730. {
  731. for (; _SORT_MAX < _L - _F; )
  732. {
  733. _RI _M = _Unguarded_partition(_F, _L, _Median(_Ty(*_F),
  734. _Ty(*(_F + (_L - _F) / 2)), _Ty(*(_L - 1))));
  735. if (_L - _M <= _M - _F)
  736. _Sort(_M, _L, _Val_type(_F)), _L = _M;
  737. else
  738. _Sort(_F, _M, _Val_type(_F)), _F = _M;
  739. }
  740. }
  741. template<class _RI, class _Ty> inline
  742. _RI _Unguarded_partition(_RI _F, _RI _L, _Ty _Piv)
  743. {
  744. for (; ; ++_F)
  745. {
  746. for (; *_F < _Piv; ++_F)
  747. ;
  748. for (; _Piv < *--_L; )
  749. ;
  750. if (_L <= _F)
  751. return (_F);
  752. iter_swap(_F, _L);
  753. }
  754. }
  755. template<class _RI> inline
  756. void _Insertion_sort(_RI _F, _RI _L)
  757. {
  758. _Insertion_sort_1(_F, _L, _Val_type(_F));
  759. }
  760. template<class _BI, class _Ty> inline
  761. void _Insertion_sort_1(_BI _F, _BI _L, _Ty *)
  762. {
  763. if (_F != _L)
  764. for (_BI _M = _F; ++_M != _L; )
  765. {
  766. _Ty _V = *_M;
  767. if (!(_V < *_F))
  768. _Unguarded_insert(_M, _V);
  769. else
  770. {
  771. copy_backward(_F, _M, _M + 1);
  772. *_F = _V;
  773. }
  774. }
  775. }
  776. template<class _BI, class _Ty> inline
  777. void _Unguarded_insert(_BI _L, _Ty _V)
  778. {
  779. for (_BI _M = _L; _V < *--_M; _L = _M)
  780. *_L = *_M;
  781. *_L = _V;
  782. }
  783. // TEMPLATE FUNCTION sort WITH PRED
  784. template<class _RI, class _Pr> inline
  785. void sort(_RI _F, _RI _L, _Pr _P)
  786. {
  787. _Sort_0(_F, _L, _P, _Val_type(_F));
  788. }
  789. template<class _RI, class _Ty, class _Pr> inline
  790. void _Sort_0(_RI _F, _RI _L, _Pr _P, _Ty *)
  791. {
  792. if (_L - _F <= _SORT_MAX)
  793. _Insertion_sort(_F, _L, _P);
  794. else
  795. {
  796. _Sort(_F, _L, _P, (_Ty *)0);
  797. _Insertion_sort(_F, _F + _SORT_MAX, _P);
  798. for (_F += _SORT_MAX; _F != _L; ++_F)
  799. _Unguarded_insert(_F, _Ty(*_F), _P);
  800. }
  801. }
  802. template<class _RI, class _Ty, class _Pr> inline
  803. void _Sort(_RI _F, _RI _L, _Pr _P, _Ty *)
  804. {
  805. for (; _SORT_MAX < _L - _F; )
  806. {
  807. _RI _M = _Unguarded_partition(_F, _L, _Median(_Ty(*_F),
  808. _Ty(*(_F + (_L - _F) / 2)), _Ty(*(_L - 1)), _P), _P);
  809. if (_L - _M <= _M - _F)
  810. _Sort(_M, _L, _P, _Val_type(_F)), _L = _M;
  811. else
  812. _Sort(_F, _M, _P, _Val_type(_F)), _F = _M;
  813. }
  814. }
  815. template<class _RI, class _Ty, class _Pr> inline
  816. _RI _Unguarded_partition(_RI _F, _RI _L, _Ty _Piv, _Pr _P)
  817. {
  818. for (; ; ++_F)
  819. {
  820. for (; _P(*_F, _Piv); ++_F)
  821. ;
  822. for (; _P(_Piv, *--_L); )
  823. ;
  824. if (_L <= _F)
  825. return (_F);
  826. iter_swap(_F, _L);
  827. }
  828. }
  829. template<class _RI, class _Pr> inline
  830. void _Insertion_sort(_RI _F, _RI _L, _Pr _P)
  831. {
  832. _Insertion_sort_1(_F, _L, _P, _Val_type(_F));
  833. }
  834. template<class _RI, class _Ty, class _Pr> inline
  835. void _Insertion_sort_1(_RI _F, _RI _L, _Pr _P, _Ty *)
  836. {
  837. if (_F != _L)
  838. for (_RI _M = _F; ++_M != _L; )
  839. {
  840. _Ty _V = *_M;
  841. if (!_P(_V, *_F))
  842. _Unguarded_insert(_M, _V, _P);
  843. else
  844. {
  845. copy_backward(_F, _M, _M + 1);
  846. *_F = _V;
  847. }
  848. }
  849. }
  850. template<class _RI, class _Ty, class _Pr> inline
  851. void _Unguarded_insert(_RI _L, _Ty _V, _Pr _P)
  852. {
  853. for (_RI _M = _L; _P(_V, *--_M); _L = _M)
  854. *_L = *_M;
  855. *_L = _V;
  856. }
  857. // TEMPLATE FUNCTION stable_sort
  858. template<class _BI> inline
  859. void stable_sort(_BI _F, _BI _L)
  860. {
  861. if (_F != _L)
  862. _Stable_sort(_F, _L, _Dist_type(_F), _Val_type(_F));
  863. }
  864. template<class _BI, class _Pd, class _Ty> inline
  865. void _Stable_sort(_BI _F, _BI _L, _Pd *, _Ty *)
  866. {
  867. _Pd _N = 0;
  868. _Distance(_F, _L, _N);
  869. _Temp_iterator<_Ty> _Xb(_N);
  870. _Stable_sort(_F, _L, _N, _Xb);
  871. }
  872. template<class _BI, class _Pd, class _Ty> inline
  873. void _Stable_sort(_BI _F, _BI _L, _Pd _N,
  874. _Temp_iterator<_Ty>& _Xb)
  875. {
  876. if (_N <= _SORT_MAX)
  877. _Insertion_sort(_F, _L);
  878. else
  879. {
  880. _Pd _N2 = (_N + 1) / 2;
  881. _BI _M = _F;
  882. advance(_M, _N2);
  883. if (_N2 <= _Xb._Maxlen())
  884. {
  885. _Buffered_merge_sort(_F, _M, _N2, _Xb);
  886. _Buffered_merge_sort(_M, _L, _N - _N2, _Xb);
  887. }
  888. else
  889. {
  890. _Stable_sort(_F, _M, _N2, _Xb);
  891. _Stable_sort(_M, _L, _N - _N2, _Xb);
  892. }
  893. _Buffered_merge(_F, _M, _L, _N2, _N - _N2, _Xb);
  894. }
  895. }
  896. template<class _BI, class _Pd, class _Ty> inline
  897. void _Buffered_merge_sort(_BI _F, _BI _L, _Pd _N,
  898. _Temp_iterator<_Ty>& _Xb)
  899. {
  900. _BI _M = _F;
  901. for (_Pd _I = _N; _CHUNK_SIZE <= _I; _I -= _CHUNK_SIZE)
  902. {
  903. _BI _Mn = _M;
  904. advance(_Mn, (int)_CHUNK_SIZE);
  905. _Insertion_sort(_M, _Mn);
  906. _M = _Mn;
  907. }
  908. _Insertion_sort(_M, _L);
  909. for (_Pd _D = _CHUNK_SIZE; _D < _N; _D *= 2)
  910. {
  911. _BI _Ft = _F;
  912. _Chunked_merge(_F, _L, _Xb._Init(), _D, _N);
  913. _Chunked_merge(_Xb._First(), _Xb._Last(), _Ft,
  914. _D *= 2, _N);
  915. }
  916. }
  917. template<class _BI, class _OI, class _Pd> inline
  918. void _Chunked_merge(_BI _F, _BI _L, _OI& _X, _Pd _D, _Pd _N)
  919. {
  920. _Pd _D2 = _D * 2;
  921. for (; _D2 <= _N; _N -= _D2)
  922. {
  923. _BI _F1 = _F;
  924. advance(_F1, _D);
  925. _BI _F2 = _F1;
  926. advance(_F2, _D);
  927. _X = merge(_F, _F1, _F1, _F2, _X);
  928. _F = _F2;
  929. }
  930. if (_N <= _D)
  931. copy(_F, _L, _X);
  932. else
  933. {
  934. _BI _F1 = _F;
  935. advance(_F1, _D);
  936. merge(_F, _F1, _F1, _L, _X);
  937. }
  938. }
  939. // TEMPLATE FUNCTION stable_sort WITH PRED
  940. template<class _BI, class _Pr> inline
  941. void stable_sort(_BI _F, _BI _L, _Pr _P)
  942. {
  943. if (_F != _L)
  944. _Stable_sort(_F, _L,
  945. _Dist_type(_F), _Val_type(_F), _P);
  946. }
  947. template<class _BI, class _Pd, class _Ty, class _Pr> inline
  948. void _Stable_sort(_BI _F, _BI _L, _Pd *, _Ty *, _Pr _P)
  949. {
  950. _Pd _N = 0;
  951. _Distance(_F, _L, _N);
  952. _Temp_iterator<_Ty> _Xb(_N);
  953. _Stable_sort(_F, _L, _N, _Xb, _P);
  954. }
  955. template<class _BI, class _Pd, class _Ty, class _Pr> inline
  956. void _Stable_sort(_BI _F, _BI _L, _Pd _N,
  957. _Temp_iterator<_Ty>& _Xb, _Pr _P)
  958. {
  959. if (_N <= _SORT_MAX)
  960. _Insertion_sort(_F, _L, _P);
  961. else
  962. {
  963. _Pd _N2 = (_N + 1) / 2;
  964. _BI _M = _F;
  965. advance(_M, _N2);
  966. if (_N2 <= _Xb._Maxlen())
  967. {
  968. _Buffered_merge_sort(_F, _M, _N2, _Xb, _P);
  969. _Buffered_merge_sort(_M, _L, _N - _N2, _Xb, _P);
  970. }
  971. else
  972. {
  973. _Stable_sort(_F, _M, _N2, _Xb, _P);
  974. _Stable_sort(_M, _L, _N - _N2, _Xb, _P);
  975. }
  976. _Buffered_merge(_F, _M, _L, _N2, _N - _N2, _Xb, _P);
  977. }
  978. }
  979. template<class _BI, class _Pd, class _Ty, class _Pr> inline
  980. void _Buffered_merge_sort(_BI _F, _BI _L, _Pd _N,
  981. _Temp_iterator<_Ty>& _Xb, _Pr _P)
  982. {
  983. _BI _M = _F;
  984. for (_Pd _I = _N; _CHUNK_SIZE <= _I; _I -= _CHUNK_SIZE)
  985. {
  986. _BI _Mn = _M;
  987. advance(_Mn, (int)_CHUNK_SIZE);
  988. _Insertion_sort(_M, _Mn, _P);
  989. _M = _Mn;
  990. }
  991. _Insertion_sort(_M, _L, _P);
  992. for (_Pd _D = _CHUNK_SIZE; _D < _N; _D *= 2)
  993. {
  994. _BI _Ft = _F;
  995. _Chunked_merge(_F, _L, _Xb._Init(), _D, _N, _P);
  996. _Chunked_merge(_Xb._First(), _Xb._Last(), _Ft,
  997. _D *= 2, _N, _P);
  998. }
  999. }
  1000. template<class _BI, class _OI, class _Pd, class _Pr> inline
  1001. void _Chunked_merge(_BI _F, _BI _L, _OI& _X,
  1002. _Pd _D, _Pd _N, _Pr _P)
  1003. {
  1004. _Pd _D2 = _D * 2;
  1005. for (; _D2 <= _N; _N -= _D2)
  1006. {
  1007. _BI _F1 = _F;
  1008. advance(_F1, _D);
  1009. _BI _F2 = _F1;
  1010. advance(_F2, _D);
  1011. _X = merge(_F, _F1, _F1, _F2, _X, _P);
  1012. _F = _F2;
  1013. }
  1014. if (_N <= _D)
  1015. copy(_F, _L, _X);
  1016. else
  1017. {
  1018. _BI _F1 = _F;
  1019. advance(_F1, _D);
  1020. merge(_F, _F1, _F1, _L, _X, _P);
  1021. }
  1022. }
  1023. // TEMPLATE FUNCTION partial_sort
  1024. template<class _RI> inline
  1025. void partial_sort(_RI _F, _RI _M, _RI _L)
  1026. {
  1027. _Partial_sort(_F, _M, _L, _Val_type(_F));
  1028. }
  1029. template<class _RI, class _Ty> inline
  1030. void _Partial_sort(_RI _F, _RI _M, _RI _L, _Ty *)
  1031. {
  1032. make_heap(_F, _M);
  1033. for (_RI _I = _M; _I < _L; ++_I)
  1034. if (*_I < *_F)
  1035. _Pop_heap(_F, _M, _I, _Ty(*_I), _Dist_type(_F));
  1036. sort_heap(_F, _M);
  1037. }
  1038. // TEMPLATE FUNCTION partial_sort WITH PRED
  1039. template<class _RI, class _Pr> inline
  1040. void partial_sort(_RI _F, _RI _M, _RI _L, _Pr _P)
  1041. {
  1042. _Partial_sort(_F, _M, _L, _P, _Val_type(_F));
  1043. }
  1044. template<class _RI, class _Ty, class _Pr> inline
  1045. void _Partial_sort(_RI _F, _RI _M, _RI _L, _Pr _P, _Ty *)
  1046. {
  1047. make_heap(_F, _M, _P);
  1048. for (_RI _I = _M; _I < _L; ++_I)
  1049. if (_P(*_I, *_F))
  1050. _Pop_heap(_F, _M, _I, _Ty(*_I), _P, _Dist_type(_F));
  1051. sort_heap(_F, _M, _P);
  1052. }
  1053. // TEMPLATE FUNCTION partial_sort_copy
  1054. template<class _II, class _RI> inline
  1055. _RI partial_sort_copy(_II _F1, _II _L1, _RI _F2, _RI _L2)
  1056. {
  1057. return (_Partial_sort_copy(_F1, _L1, _F2, _L2,
  1058. _Dist_type(_F2), _Val_type(_F1)));
  1059. }
  1060. template<class _II, class _RI, class _Pd, class _Ty> inline
  1061. _RI _Partial_sort_copy(_II _F1, _II _L1, _RI _F2, _RI _L2,
  1062. _Pd *, _Ty *)
  1063. {
  1064. _RI _X = _F2;
  1065. if (_X != _L2)
  1066. {
  1067. for (; _F1 != _L1 && _X != _L2; ++_F1, ++_X)
  1068. *_X = *_F1;
  1069. make_heap(_F2, _X);
  1070. for (; _F1 != _L1; ++_F1)
  1071. if (*_F1 < *_F2)
  1072. _Adjust_heap(_F2, _Pd(0), _Pd(_X - _F2),
  1073. _Ty(*_F1));
  1074. sort_heap(_F2, _X);
  1075. }
  1076. return (_X);
  1077. }
  1078. // TEMPLATE FUNCTION partial_sort_copy WITH PRED
  1079. template<class _II, class _RI, class _Pr> inline
  1080. _RI partial_sort_copy(_II _F1, _II _L1, _RI _F2, _RI _L2,
  1081. _Pr _P)
  1082. {
  1083. return (_Partial_sort_copy(_F1, _L1, _F2, _L2, _P,
  1084. _Dist_type(_F2), _Val_type(_F1)));
  1085. }
  1086. template<class _II, class _RI, class _Pd,
  1087. class _Ty, class _Pr> inline
  1088. _RI _Partial_sort_copy(_II _F1, _II _L1, _RI _F2, _RI _L2,
  1089. _Pr _P, _Pd *, _Ty *)
  1090. {
  1091. _RI _X = _F2;
  1092. if (_X != _L2)
  1093. {
  1094. for (; _F1 != _L1 && _X != _L2; ++_F1, ++_X)
  1095. *_X = *_F1;
  1096. make_heap(_F2, _X, _P);
  1097. for (; _F1 != _L1; ++_F1)
  1098. if (_P(*_F1, *_F2))
  1099. _Adjust_heap(_F2, _Pd(0), _Pd(_X - _F2),
  1100. _Ty(*_F1), _P);
  1101. sort_heap(_F2, _X, _P);
  1102. }
  1103. return (_X);
  1104. }
  1105. // TEMPLATE FUNCTION nth_element
  1106. template<class _RI> inline
  1107. void nth_element(_RI _F, _RI _Nth, _RI _L)
  1108. {
  1109. _Nth_element(_F, _Nth, _L, _Val_type(_F));
  1110. }
  1111. template<class _RI, class _Ty> inline
  1112. void _Nth_element(_RI _F, _RI _Nth, _RI _L, _Ty *)
  1113. {
  1114. for (; _SORT_MAX < _L - _F; )
  1115. {
  1116. _RI _M = _Unguarded_partition(_F, _L, _Median(_Ty(*_F),
  1117. _Ty(*(_F + (_L - _F) / 2)), _Ty(*(_L - 1))));
  1118. if (_M <= _Nth)
  1119. _F = _M;
  1120. else
  1121. _L = _M;
  1122. }
  1123. _Insertion_sort(_F, _L);
  1124. }
  1125. // TEMPLATE FUNCTION nth_element WITH PRED
  1126. template<class _RI, class _Pr> inline
  1127. void nth_element(_RI _F, _RI _Nth, _RI _L, _Pr _P)
  1128. {
  1129. _Nth_element(_F, _Nth, _L, _P, _Val_type(_F));
  1130. }
  1131. template<class _RI, class _Ty, class _Pr> inline
  1132. void _Nth_element(_RI _F, _RI _Nth, _RI _L, _Pr _P, _Ty *)
  1133. {
  1134. for (; _SORT_MAX < _L - _F; )
  1135. {
  1136. _RI _M = _Unguarded_partition(_F, _L, _Median(_Ty(*_F),
  1137. _Ty(*(_F + (_L - _F) / 2)), _Ty(*(_L - 1)), _P), _P);
  1138. if (_M <= _Nth)
  1139. _F = _M;
  1140. else
  1141. _L = _M;
  1142. }
  1143. _Insertion_sort(_F, _L, _P);
  1144. }
  1145. // TEMPLATE FUNCTION lower_bound
  1146. template<class _FI, class _Ty> inline
  1147. _FI lower_bound(_FI _F, _FI _L, const _Ty& _V)
  1148. {
  1149. return (_Lower_bound(_F, _L, _V, _Dist_type(_F)));
  1150. }
  1151. template<class _FI, class _Ty, class _Pd> inline
  1152. _FI _Lower_bound(_FI _F, _FI _L, const _Ty& _V, _Pd *)
  1153. {
  1154. _Pd _N = 0;
  1155. _Distance(_F, _L, _N);
  1156. for (; 0 < _N; )
  1157. {
  1158. _Pd _N2 = _N / 2;
  1159. _FI _M = _F;
  1160. advance(_M, _N2);
  1161. if (*_M < _V)
  1162. _F = ++_M, _N -= _N2 + 1;
  1163. else
  1164. _N = _N2;
  1165. }
  1166. return (_F);
  1167. }
  1168. // TEMPLATE FUNCTION lower_bound WITH PRED
  1169. template<class _FI, class _Ty, class _Pr> inline
  1170. _FI lower_bound(_FI _F, _FI _L, const _Ty& _V, _Pr _P)
  1171. {
  1172. return (_Lower_bound(_F, _L, _V, _P, _Dist_type(_F)));
  1173. }
  1174. template<class _FI, class _Ty, class _Pd, class _Pr> inline
  1175. _FI _Lower_bound(_FI _F, _FI _L, const _Ty& _V, _Pr _P, _Pd *)
  1176. {
  1177. _Pd _N = 0;
  1178. _Distance(_F, _L, _N);
  1179. for (; 0 < _N; )
  1180. {
  1181. _Pd _N2 = _N / 2;
  1182. _FI _M = _F;
  1183. advance(_M, _N2);
  1184. if (_P(*_M, _V))
  1185. _F = ++_M, _N -= _N2 + 1;
  1186. else
  1187. _N = _N2;
  1188. }
  1189. return (_F);
  1190. }
  1191. // TEMPLATE FUNCTION upper_bound
  1192. template<class _FI, class _Ty> inline
  1193. _FI upper_bound(_FI _F, _FI _L, const _Ty& _V)
  1194. {
  1195. return (_Upper_bound(_F, _L, _V, _Dist_type(_F)));
  1196. }
  1197. template<class _FI, class _Ty, class _Pd> inline
  1198. _FI _Upper_bound(_FI _F, _FI _L, const _Ty& _V, _Pd *)
  1199. {
  1200. _Pd _N = 0;
  1201. _Distance(_F, _L, _N);
  1202. for (; 0 < _N; )
  1203. {
  1204. _Pd _N2 = _N / 2;
  1205. _FI _M = _F;
  1206. advance(_M, _N2);
  1207. if (!(_V < *_M))
  1208. _F = ++_M, _N -= _N2 + 1;
  1209. else
  1210. _N = _N2;
  1211. }
  1212. return (_F);
  1213. }
  1214. // TEMPLATE FUNCTION upper_bound WITH PRED
  1215. template<class _FI, class _Ty, class _Pr> inline
  1216. _FI upper_bound(_FI _F, _FI _L, const _Ty& _V, _Pr _P)
  1217. {
  1218. return (_Upper_bound(_F, _L, _V, _P, _Dist_type(_F)));
  1219. }
  1220. template<class _FI, class _Ty, class _Pd, class _Pr> inline
  1221. _FI _Upper_bound(_FI _F, _FI _L, const _Ty& _V, _Pr _P, _Pd *)
  1222. {
  1223. _Pd _N = 0;
  1224. _Distance(_F, _L, _N);
  1225. for (; 0 < _N; )
  1226. {
  1227. _Pd _N2 = _N / 2;
  1228. _FI _M = _F;
  1229. advance(_M, _N2);
  1230. if (!_P(_V, *_M))
  1231. _F = ++_M, _N -= _N2 + 1;
  1232. else
  1233. _N = _N2;
  1234. }
  1235. return (_F);
  1236. }
  1237. // TEMPLATE FUNCTION equal_range
  1238. template<class _FI, class _Ty> inline
  1239. pair<_FI, _FI> equal_range(_FI _F, _FI _L, const _Ty& _V)
  1240. {
  1241. return (_Equal_range(_F, _L, _V, _Dist_type(_F)));
  1242. }
  1243. template<class _FI, class _Ty, class _Pd> inline
  1244. pair<_FI, _FI> _Equal_range(_FI _F, _FI _L,
  1245. const _Ty& _V, _Pd *)
  1246. {
  1247. _Pd _N = 0;
  1248. _Distance(_F, _L, _N);
  1249. for (; 0 < _N; )
  1250. {
  1251. _Pd _N2 = _N / 2;
  1252. _FI _M = _F;
  1253. advance(_M, _N2);
  1254. if (*_M < _V)
  1255. _F = ++_M, _N -= _N2 + 1;
  1256. else if (_V < *_M)
  1257. _N = _N2;
  1258. else
  1259. {
  1260. _FI _F2 = lower_bound(_F, _M, _V);
  1261. advance(_F, _N);
  1262. _FI _L2 = upper_bound(++_M, _F, _V);
  1263. return (pair<_FI, _FI>(_F2, _L2));
  1264. }
  1265. }
  1266. return (pair<_FI, _FI>(_F, _F));
  1267. }
  1268. // TEMPLATE FUNCTION equal_range WITH PRED
  1269. template<class _FI, class _Ty, class _Pr> inline
  1270. pair<_FI, _FI> equal_range(_FI _F, _FI _L, const _Ty& _V,
  1271. _Pr _P)
  1272. {
  1273. return (_Equal_range(_F, _L, _V, _P, _Dist_type(_F)));
  1274. }
  1275. template<class _FI, class _Ty, class _Pd, class _Pr> inline
  1276. pair<_FI, _FI> _Equal_range(_FI _F, _FI _L, const _Ty& _V,
  1277. _Pr _P, _Pd *)
  1278. {
  1279. _Pd _N = 0;
  1280. _Distance(_F, _L, _N);
  1281. for (; 0 < _N; )
  1282. {
  1283. _Pd _N2 = _N / 2;
  1284. _FI _M = _F;
  1285. advance(_M, _N2);
  1286. if (_P(*_M, _V))
  1287. _F = ++_M, _N -= _N2 + 1;
  1288. else if (_P(_V, *_M))
  1289. _N = _N2;
  1290. else
  1291. {
  1292. _FI _F2 = lower_bound(_F, _M, _V, _P);
  1293. advance(_F, _N);
  1294. _FI _L2 = upper_bound(++_M, _F, _V, _P);
  1295. return (pair<_FI, _FI>(_F2, _L2));
  1296. }
  1297. }
  1298. return (pair<_FI, _FI>(_F, _F));
  1299. }
  1300. // TEMPLATE FUNCTION binary_search
  1301. template<class _FI, class _Ty> inline
  1302. bool binary_search(_FI _F, _FI _L, const _Ty& _V)
  1303. {
  1304. _FI _I = lower_bound(_F, _L, _V);
  1305. return (_I != _L && !(_V < *_I));
  1306. }
  1307. // TEMPLATE FUNCTION binary_search WITH PRED
  1308. template<class _FI, class _Ty, class _Pr> inline
  1309. bool binary_search(_FI _F, _FI _L, const _Ty& _V, _Pr _P)
  1310. {
  1311. _FI _I = lower_bound(_F, _L, _V, _P);
  1312. return (_I != _L && !_P(_V, *_I));
  1313. }
  1314. // TEMPLATE FUNCTION merge
  1315. template<class _II1, class _II2, class _OI> inline
  1316. _OI merge(_II1 _F1, _II1 _L1, _II2 _F2, _II2 _L2, _OI _X)
  1317. {
  1318. for (; _F1 != _L1 && _F2 != _L2; ++_X)
  1319. if (*_F2 < *_F1)
  1320. *_X = *_F2++;
  1321. else
  1322. *_X = *_F1++;
  1323. return (copy(_F2, _L2, copy(_F1, _L1, _X)));
  1324. }
  1325. // TEMPLATE FUNCTION merge WITH PRED
  1326. template<class _II1, class _II2, class _OI, class _Pr> inline
  1327. _OI merge(_II1 _F1, _II1 _L1, _II2 _F2, _II2 _L2, _OI _X,
  1328. _Pr _P)
  1329. {
  1330. for (; _F1 != _L1 && _F2 != _L2; ++_X)
  1331. if (_P(*_F2, *_F1))
  1332. *_X = *_F2++;
  1333. else
  1334. *_X = *_F1++;
  1335. return (copy(_F2, _L2, copy(_F1, _L1, _X)));
  1336. }
  1337. // TEMPLATE FUNCTION inplace_merge
  1338. template<class _BI> inline
  1339. void inplace_merge(_BI _F, _BI _M, _BI _L)
  1340. {
  1341. if (_F != _L)
  1342. _Inplace_merge(_F, _M, _L,
  1343. _Dist_type(_F), _Val_type(_F));
  1344. }
  1345. template<class _BI, class _Pd, class _Ty> inline
  1346. void _Inplace_merge(_BI _F, _BI _M, _BI _L, _Pd *, _Ty *)
  1347. {
  1348. _Pd _D1 = 0;
  1349. _Distance(_F, _M, _D1);
  1350. _Pd _D2 = 0;
  1351. _Distance(_M, _L, _D2);
  1352. _Temp_iterator<_Ty> _Xb(_D1 < _D2 ? _D1 : _D2);
  1353. _Buffered_merge(_F, _M, _L, _D1, _D2, _Xb);
  1354. }
  1355. template<class _BI, class _Pd, class _Ty> inline
  1356. void _Buffered_merge(_BI _F, _BI _M, _BI _L,
  1357. _Pd _D1, _Pd _D2, _Temp_iterator<_Ty>& _Xb)
  1358. {
  1359. if (_D1 == 0 || _D2 == 0)
  1360. ;
  1361. else if (_D1 + _D2 == 2)
  1362. {
  1363. if (*_M < *_F)
  1364. iter_swap(_F, _M);
  1365. }
  1366. else if (_D1 <= _D2 && _D1 <= _Xb._Maxlen())
  1367. {
  1368. copy(_F, _M, _Xb._Init());
  1369. merge(_Xb._First(), _Xb._Last(), _M, _L, _F);
  1370. }
  1371. else if (_D2 <= _Xb._Maxlen())
  1372. {
  1373. copy(_M, _L, _Xb._Init());
  1374. _Merge_backward(_F, _M, _Xb._First(), _Xb._Last(), _L);
  1375. }
  1376. else
  1377. {
  1378. _BI _Fn, _Ln;
  1379. _Pd _D1n, _D2n;
  1380. if (_D2 < _D1)
  1381. {
  1382. _D1n = _D1 / 2;
  1383. _Fn = _F;
  1384. advance(_Fn, _D1n);
  1385. _Ln = lower_bound(_M, _L, _Ty(*_Fn));
  1386. _Distance(_M, _Ln, _D2n);
  1387. }
  1388. else
  1389. {
  1390. _D2n = _D2 / 2;
  1391. _Ln = _M;
  1392. advance(_Ln, _D2n);
  1393. _Fn = upper_bound(_F, _M, _Ty(*_Ln));
  1394. _Distance(_F, _Fn, _D1n);
  1395. }
  1396. _BI _Mn = _Buffered_rotate(_Fn, _M, _Ln,
  1397. _D1 - _D1n, _D2n, _Xb);
  1398. _Buffered_merge(_F, _Fn, _Mn, _D1n, _D2n, _Xb);
  1399. _Buffered_merge(_Mn, _Ln, _L,
  1400. _D1 - _D1n, _D2 - _D2n, _Xb);
  1401. }
  1402. }
  1403. template<class _BI1, class _BI2, class _BI3> inline
  1404. _BI3 _Merge_backward(_BI1 _F1, _BI1 _L1, _BI2 _F2, _BI2 _L2,
  1405. _BI3 _X)
  1406. {
  1407. for (; ; )
  1408. if (_F1 == _L1)
  1409. return (copy_backward(_F2, _L2, _X));
  1410. else if (_F2 == _L2)
  1411. return (copy_backward(_F1, _L1, _X));
  1412. else if (*--_L2 < *--_L1)
  1413. *--_X = *_L1, ++_L2;
  1414. else
  1415. *--_X = *_L2, ++_L1;
  1416. }
  1417. template<class _BI, class _Pd, class _Ty> inline
  1418. _BI _Buffered_rotate(_BI _F, _BI _M, _BI _L,
  1419. _Pd _D1, _Pd _D2, _Temp_iterator<_Ty>& _Xb)
  1420. {
  1421. if (_D1 <= _D2 && _D1 <= _Xb._Maxlen())
  1422. {
  1423. copy(_F, _M, _Xb._Init());
  1424. copy(_M, _L, _F);
  1425. return (copy_backward(_Xb._First(), _Xb._Last(), _L));
  1426. }
  1427. else if (_D2 <= _Xb._Maxlen())
  1428. {
  1429. copy(_M, _L, _Xb._Init());
  1430. copy_backward(_F, _M, _L);
  1431. return (copy(_Xb._First(), _Xb._Last(), _F));
  1432. }
  1433. else
  1434. {
  1435. rotate(_F, _M, _L);
  1436. advance(_F, _D2);
  1437. return (_F);
  1438. }
  1439. }
  1440. // TEMPLATE FUNCTION inplace_merge WITH PRED
  1441. template<class _BI, class _Pr> inline
  1442. void inplace_merge(_BI _F, _BI _M, _BI _L, _Pr _P)
  1443. {
  1444. if (_F != _L)
  1445. _Inplace_merge(_F, _M, _L, _P,
  1446. _Dist_type(_F), _Val_type(_F));
  1447. }
  1448. template<class _BI, class _Pd, class _Ty, class _Pr> inline
  1449. void _Inplace_merge(_BI _F, _BI _M, _BI _L, _Pr _P,
  1450. _Pd *, _Ty *)
  1451. {
  1452. _Pd _D1 = 0;
  1453. _Distance(_F, _M, _D1);
  1454. _Pd _D2 = 0;
  1455. _Distance(_M, _L, _D2);
  1456. _Temp_iterator<_Ty> _Xb(_D1 < _D2 ? _D1 : _D2);
  1457. _Buffered_merge(_F, _M, _L, _D1, _D2, _Xb, _P);
  1458. }
  1459. template<class _BI, class _Pd, class _Ty, class _Pr> inline
  1460. void _Buffered_merge(_BI _F, _BI _M, _BI _L,
  1461. _Pd _D1, _Pd _D2, _Temp_iterator<_Ty>& _Xb, _Pr _P)
  1462. {
  1463. if (_D1 == 0 || _D2 == 0)
  1464. ;
  1465. else if (_D1 + _D2 == 2)
  1466. {
  1467. if (_P(*_M, *_F))
  1468. iter_swap(_F, _M);
  1469. }
  1470. else if (_D1 <= _D2 && _D1 <= _Xb._Maxlen())
  1471. {
  1472. copy(_F, _M, _Xb._Init());
  1473. merge(_Xb._First(), _Xb._Last(), _M, _L, _F, _P);
  1474. }
  1475. else if (_D2 <= _Xb._Maxlen())
  1476. {
  1477. copy(_M, _L, _Xb._Init());
  1478. _Merge_backward(_F, _M, _Xb._First(), _Xb._Last(),
  1479. _L, _P);
  1480. }
  1481. else
  1482. {
  1483. _BI _Fn, _Ln;
  1484. _Pd _D1n, _D2n;
  1485. if (_D2 < _D1)
  1486. {
  1487. _D1n = _D1 / 2;
  1488. _Fn = _F;
  1489. advance(_Fn, _D1n);
  1490. _Ln = lower_bound(_M, _L, _Ty(*_Fn), _P);
  1491. _Distance(_M, _Ln, _D2n);
  1492. }
  1493. else
  1494. {
  1495. _D2n = _D2 / 2;
  1496. _Ln = _M;
  1497. advance(_Ln, _D2n);
  1498. _Fn = upper_bound(_F, _M, _Ty(*_Ln), _P);
  1499. _Distance(_F, _Fn, _D1n);
  1500. }
  1501. _BI _Mn = _Buffered_rotate(_Fn, _M, _Ln,
  1502. _D1 - _D1n, _D2n, _Xb);
  1503. _Buffered_merge(_F, _Fn, _Mn, _D1n, _D2n, _Xb, _P);
  1504. _Buffered_merge(_Mn, _Ln, _L,
  1505. _D1 - _D1n, _D2 - _D2n, _Xb, _P);
  1506. }
  1507. }
  1508. template<class _BI1, class _BI2, class _BI3, class _Pr> inline
  1509. _BI3 _Merge_backward(_BI1 _F1, _BI1 _L1, _BI2 _F2, _BI2 _L2,
  1510. _BI3 _X, _Pr _P)
  1511. {
  1512. for (; ; )
  1513. if (_F1 == _L1)
  1514. return (copy_backward(_F2, _L2, _X));
  1515. else if (_F2 == _L2)
  1516. return (copy_backward(_F1, _L1, _X));
  1517. else if (_P(*--_L2, *--_L1))
  1518. *--_X = *_L1, ++_L2;
  1519. else
  1520. *--_X = *_L2, ++_L1;
  1521. }
  1522. // TEMPLATE FUNCTION includes
  1523. template<class _II1, class _II2> inline
  1524. bool includes(_II1 _F1, _II1 _L1, _II2 _F2, _II2 _L2)
  1525. {
  1526. for (; _F1 != _L1 && _F2 != _L2; )
  1527. if (*_F2 < *_F1)
  1528. return (false);
  1529. else if (*_F1 < *_F2)
  1530. ++_F1;
  1531. else
  1532. ++_F2;
  1533. return (_F2 == _L2);
  1534. }
  1535. // TEMPLATE FUNCTION includes WITH PRED
  1536. template<class _II1, class _II2, class _Pr> inline
  1537. bool includes(_II1 _F1, _II1 _L1, _II2 _F2, _II2 _L2, _Pr _P)
  1538. {
  1539. for (; _F1 != _L1 && _F2 != _L2; )
  1540. if (_P(*_F2, *_F1))
  1541. return (false);
  1542. else if (_P(*_F1, *_F2))
  1543. ++_F1;
  1544. else
  1545. ++_F2;
  1546. return (_F2 == _L2);
  1547. }
  1548. // TEMPLATE FUNCTION set_union
  1549. template<class _II1, class _II2, class _OI> inline
  1550. _OI set_union(_II1 _F1, _II1 _L1, _II2 _F2, _II2 _L2, _OI _X)
  1551. {
  1552. for (; _F1 != _L1 && _F2 != _L2; )
  1553. if (*_F1 < *_F2)
  1554. *_X++ = *_F1++;
  1555. else if (*_F2 < *_F1)
  1556. *_X++ = *_F2++;
  1557. else
  1558. *_X++ = *_F1++, ++_F2;
  1559. return (copy(_F2, _L2, copy(_F1, _L1, _X)));
  1560. }
  1561. // TEMPLATE FUNCTION set_union WITH PRED
  1562. template<class _II1, class _II2, class _OI, class _Pr> inline
  1563. _OI set_union(_II1 _F1, _II1 _L1, _II2 _F2, _II2 _L2, _OI _X,
  1564. _Pr _P)
  1565. {
  1566. for (; _F1 != _L1 && _F2 != _L2; )
  1567. if (_P(*_F1, *_F2))
  1568. *_X++ = *_F1++;
  1569. else if (_P(*_F2, *_F1))
  1570. *_X++ = *_F2++;
  1571. else
  1572. *_X++ = *_F1++, ++_F2;
  1573. return (copy(_F2, _L2, copy(_F1, _L1, _X)));
  1574. }
  1575. // TEMPLATE FUNCTION set_intersection
  1576. template<class _II1, class _II2, class _OI> inline
  1577. _OI set_intersection(_II1 _F1, _II1 _L1, _II2 _F2, _II2 _L2,
  1578. _OI _X)
  1579. {
  1580. for (; _F1 != _L1 && _F2 != _L2; )
  1581. if (*_F1 < *_F2)
  1582. ++_F1;
  1583. else if (*_F2 < *_F1)
  1584. ++_F2;
  1585. else
  1586. *_X++ = *_F1++, ++_F2;
  1587. return (_X);
  1588. }
  1589. // TEMPLATE FUNCTION set_intersection WITH PRED
  1590. template<class _II1, class _II2, class _OI, class _Pr> inline
  1591. _OI set_intersection(_II1 _F1, _II1 _L1, _II2 _F2, _II2 _L2,
  1592. _OI _X, _Pr _P)
  1593. {
  1594. for (; _F1 != _L1 && _F2 != _L2; )
  1595. if (_P(*_F1, *_F2))
  1596. ++_F1;
  1597. else if (_P(*_F2, *_F1))
  1598. ++_F2;
  1599. else
  1600. *_X++ = *_F1++, ++_F2;
  1601. return (_X);
  1602. }
  1603. // TEMPLATE FUNCTION set_difference
  1604. template<class _II1, class _II2, class _OI> inline
  1605. _OI set_difference(_II1 _F1, _II1 _L1, _II2 _F2, _II2 _L2,
  1606. _OI _X)
  1607. {
  1608. for (; _F1 != _L1 && _F2 != _L2; )
  1609. if (*_F1 < *_F2)
  1610. *_X++ = *_F1++;
  1611. else if (*_F2 < *_F1)
  1612. ++_F2;
  1613. else
  1614. ++_F1, ++_F2;
  1615. return (copy(_F1, _L1, _X));
  1616. }
  1617. // TEMPLATE FUNCTION set_difference WITH PRED
  1618. template<class _II1, class _II2, class _OI, class _Pr> inline
  1619. _OI set_difference(_II1 _F1, _II1 _L1, _II2 _F2, _II2 _L2,
  1620. _OI _X, _Pr _P)
  1621. {
  1622. for (; _F1 != _L1 && _F2 != _L2; )
  1623. if (_P(*_F1, *_F2))
  1624. *_X++ = *_F1++;
  1625. else if (_P(*_F2, *_F1))
  1626. ++_F2;
  1627. else
  1628. ++_F1, ++_F2;
  1629. return (copy(_F1, _L1, _X));
  1630. }
  1631. // TEMPLATE FUNCTION set_symmetric_difference
  1632. template<class _II1, class _II2, class _OI> inline
  1633. _OI set_symmetric_difference(_II1 _F1, _II1 _L1, _II2 _F2,
  1634. _II2 _L2, _OI _X)
  1635. {
  1636. for (; _F1 != _L1 && _F2 != _L2; )
  1637. if (*_F1 < *_F2)
  1638. *_X++ = *_F1++;
  1639. else if (*_F2 < *_F1)
  1640. *_X++ = *_F2++;
  1641. else
  1642. ++_F1, ++_F2;
  1643. return (copy(_F2, _L2, copy(_F1, _L1, _X)));
  1644. }
  1645. // TEMPLATE FUNCTION set_symmetric_difference WITH PRED
  1646. template<class _II1, class _II2, class _OI, class _Pr> inline
  1647. _OI set_symmetric_difference(_II1 _F1, _II1 _L1, _II2 _F2,
  1648. _II2 _L2, _OI _X, _Pr _P)
  1649. {
  1650. for (; _F1 != _L1 && _F2 != _L2; )
  1651. if (_P(*_F1, *_F2))
  1652. *_X++ = *_F1++;
  1653. else if (_P(*_F2, *_F1))
  1654. *_X++ = *_F2++;
  1655. else
  1656. ++_F1, ++_F2;
  1657. return (copy(_F2, _L2, copy(_F1, _L1, _X)));
  1658. }
  1659. // TEMPLATE FUNCTION push_heap
  1660. template<class _RI> inline
  1661. void push_heap(_RI _F, _RI _L)
  1662. {
  1663. _Push_heap_0(_F, _L, _Dist_type(_F), _Val_type(_F));
  1664. }
  1665. template<class _RI, class _Pd, class _Ty> inline
  1666. void _Push_heap_0(_RI _F, _RI _L, _Pd *, _Ty *)
  1667. {
  1668. _Push_heap(_F, _Pd(_L - _F - 1), _Pd(0), _Ty(*(_L - 1)));
  1669. }
  1670. template<class _RI, class _Pd, class _Ty> inline
  1671. void _Push_heap(_RI _F, _Pd _H, _Pd _J, _Ty _V)
  1672. {
  1673. for (_Pd _I = (_H - 1) / 2; _J < _H && *(_F + _I) < _V;
  1674. _I = (_H - 1) / 2)
  1675. *(_F + _H) = *(_F + _I), _H = _I;
  1676. *(_F + _H) = _V;
  1677. }
  1678. // TEMPLATE FUNCTION push_heap WITH PRED
  1679. template<class _RI, class _Pr> inline
  1680. void push_heap(_RI _F, _RI _L, _Pr _P)
  1681. {
  1682. _Push_heap_0(_F, _L, _P,
  1683. _Dist_type(_F), _Val_type(_F));
  1684. }
  1685. template<class _RI, class _Pd, class _Ty, class _Pr> inline
  1686. void _Push_heap_0(_RI _F, _RI _L, _Pr _P, _Pd *, _Ty *)
  1687. {
  1688. _Push_heap(_F, _Pd(_L - _F - 1), _Pd(0),
  1689. _Ty(*(_L - 1)), _P);
  1690. }
  1691. template<class _RI, class _Pd, class _Ty, class _Pr> inline
  1692. void _Push_heap(_RI _F, _Pd _H, _Pd _J, _Ty _V, _Pr _P)
  1693. {
  1694. for (_Pd _I = (_H - 1) / 2; _J < _H && _P(*(_F + _I), _V);
  1695. _I = (_H - 1) / 2)
  1696. *(_F + _H) = *(_F + _I), _H = _I;
  1697. *(_F + _H) = _V;
  1698. }
  1699. // TEMPLATE FUNCTION pop_heap
  1700. template<class _RI> inline
  1701. void pop_heap(_RI _F, _RI _L)
  1702. {
  1703. _Pop_heap_0(_F, _L, _Val_type(_F));
  1704. }
  1705. template<class _RI, class _Ty> inline
  1706. void _Pop_heap_0(_RI _F, _RI _L, _Ty *)
  1707. {
  1708. _Pop_heap(_F, _L - 1, _L - 1, _Ty(*(_L - 1)),
  1709. _Dist_type(_F));
  1710. }
  1711. template<class _RI, class _Pd, class _Ty> inline
  1712. void _Pop_heap(_RI _F, _RI _L, _RI _X, _Ty _V, _Pd *)
  1713. {
  1714. *_X = *_F;
  1715. _Adjust_heap(_F, _Pd(0), _Pd(_L - _F), _V);
  1716. }
  1717. template<class _RI, class _Pd, class _Ty> inline
  1718. void _Adjust_heap(_RI _F, _Pd _H, _Pd _N, _Ty _V)
  1719. {
  1720. _Pd _J = _H;
  1721. _Pd _K = 2 * _H + 2;
  1722. for (; _K < _N; _K = 2 * _K + 2)
  1723. {
  1724. if (*(_F + _K) < *(_F + (_K - 1)))
  1725. --_K;
  1726. *(_F + _H) = *(_F + _K), _H = _K;
  1727. }
  1728. if (_K == _N)
  1729. *(_F + _H) = *(_F + (_K - 1)), _H = _K - 1;
  1730. _Push_heap(_F, _H, _J, _V);
  1731. }
  1732. // TEMPLATE FUNCTION pop_heap WITH PRED
  1733. template<class _RI, class _Pr> inline
  1734. void pop_heap(_RI _F, _RI _L, _Pr _P)
  1735. {
  1736. _Pop_heap_0(_F, _L, _P, _Val_type(_F));
  1737. }
  1738. template<class _RI, class _Ty, class _Pr> inline
  1739. void _Pop_heap_0(_RI _F, _RI _L, _Pr _P, _Ty *)
  1740. {
  1741. _Pop_heap(_F, _L - 1, _L - 1, _Ty(*(_L - 1)), _P,
  1742. _Dist_type(_F));
  1743. }
  1744. template<class _RI, class _Pd, class _Ty, class _Pr> inline
  1745. void _Pop_heap(_RI _F, _RI _L, _RI _X, _Ty _V, _Pr _P, _Pd *)
  1746. {
  1747. *_X = *_F;
  1748. _Adjust_heap(_F, _Pd(0), _Pd(_L - _F), _V, _P);
  1749. }
  1750. template<class _RI, class _Pd, class _Ty, class _Pr> inline
  1751. void _Adjust_heap(_RI _F, _Pd _H, _Pd _N, _Ty _V, _Pr _P)
  1752. {
  1753. _Pd _J = _H;
  1754. _Pd _K = 2 * _H + 2;
  1755. for (; _K < _N; _K = 2 * _K + 2)
  1756. {
  1757. if (_P(*(_F + _K), *(_F + (_K - 1))))
  1758. --_K;
  1759. *(_F + _H) = *(_F + _K), _H = _K;
  1760. }
  1761. if (_K == _N)
  1762. *(_F + _H) = *(_F + (_K - 1)), _H = _K - 1;
  1763. _Push_heap(_F, _H, _J, _V, _P);
  1764. }
  1765. // TEMPLATE FUNCTION make_heap
  1766. template<class _RI> inline
  1767. void make_heap(_RI _F, _RI _L)
  1768. {
  1769. if (2 <= _L - _F)
  1770. _Make_heap(_F, _L, _Dist_type(_F), _Val_type(_F));
  1771. }
  1772. template<class _RI, class _Pd, class _Ty> inline
  1773. void _Make_heap(_RI _F, _RI _L, _Pd *, _Ty *)
  1774. {
  1775. _Pd _N = _L - _F;
  1776. for (_Pd _H = _N / 2; 0 < _H; )
  1777. --_H, _Adjust_heap(_F, _H, _N, _Ty(*(_F + _H)));
  1778. }
  1779. // TEMPLATE FUNCTION make_heap WITH PRED
  1780. template<class _RI, class _Pr> inline
  1781. void make_heap(_RI _F, _RI _L, _Pr _P)
  1782. {
  1783. if (2 <= _L - _F)
  1784. _Make_heap(_F, _L, _P,
  1785. _Dist_type(_F), _Val_type(_F));
  1786. }
  1787. template<class _RI, class _Pd, class _Ty, class _Pr> inline
  1788. void _Make_heap(_RI _F, _RI _L, _Pr _P, _Pd *, _Ty *)
  1789. {
  1790. _Pd _N = _L - _F;
  1791. for (_Pd _H = _N / 2; 0 < _H; )
  1792. --_H, _Adjust_heap(_F, _H, _N, _Ty(*(_F + _H)), _P);
  1793. }
  1794. // TEMPLATE FUNCTION sort_heap
  1795. template<class _RI> inline
  1796. void sort_heap(_RI _F, _RI _L)
  1797. {
  1798. for (; 1 < _L - _F; --_L)
  1799. pop_heap(_F, _L);
  1800. }
  1801. // TEMPLATE FUNCTION sort_heap WITH PRED
  1802. template<class _RI, class _Pr> inline
  1803. void sort_heap(_RI _F, _RI _L, _Pr _P)
  1804. {
  1805. for (; 1 < _L - _F; --_L)
  1806. pop_heap(_F, _L, _P);
  1807. }
  1808. // TEMPLATE FUNCTION max_element
  1809. template<class _FI> inline
  1810. _FI max_element(_FI _F, _FI _L)
  1811. {
  1812. _FI _X = _F;
  1813. if (_F != _L)
  1814. for (; ++_F != _L; )
  1815. if (*_X < *_F)
  1816. _X = _F;
  1817. return (_X);
  1818. }
  1819. // TEMPLATE FUNCTION max_element WITH PRED
  1820. template<class _FI, class _Pr> inline
  1821. _FI max_element(_FI _F, _FI _L, _Pr _P)
  1822. {
  1823. _FI _X = _F;
  1824. if (_F != _L)
  1825. for (; ++_F != _L; )
  1826. if (_P(*_X, *_F))
  1827. _X = _F;
  1828. return (_X);
  1829. }
  1830. // TEMPLATE FUNCTION min_element
  1831. template<class _FI> inline
  1832. _FI min_element(_FI _F, _FI _L)
  1833. {
  1834. _FI _X = _F;
  1835. if (_F != _L)
  1836. for (; ++_F != _L; )
  1837. if (*_F < *_X)
  1838. _X = _F;
  1839. return (_X);
  1840. }
  1841. // TEMPLATE FUNCTION min_element WITH PRED
  1842. template<class _FI, class _Pr> inline
  1843. _FI min_element(_FI _F, _FI _L, _Pr _P)
  1844. {
  1845. _FI _X = _F;
  1846. if (_F != _L)
  1847. for (; ++_F != _L; )
  1848. if (_P(*_F, *_X))
  1849. _X = _F;
  1850. return (_X);
  1851. }
  1852. // TEMPLATE FUNCTION next_permutation
  1853. template<class _BI> inline
  1854. bool next_permutation(_BI _F, _BI _L)
  1855. {
  1856. _BI _I = _L;
  1857. if (_F == _L || _F == --_I)
  1858. return (false);
  1859. for (; ; )
  1860. {
  1861. _BI _Ip = _I;
  1862. if (*--_I < *_Ip)
  1863. {
  1864. _BI _J = _L;
  1865. for (; !(*_I < *--_J); )
  1866. ;
  1867. iter_swap(_I, _J);
  1868. reverse(_Ip, _L);
  1869. return (true);
  1870. }
  1871. if (_I == _F)
  1872. {
  1873. reverse(_F, _L);
  1874. return (false);
  1875. }
  1876. }
  1877. }
  1878. // TEMPLATE FUNCTION next_permutation WITH PRED
  1879. template<class _BI, class _Pr> inline
  1880. bool next_permutation(_BI _F, _BI _L, _Pr _P)
  1881. {
  1882. _BI _I = _L;
  1883. if (_F == _L || _F == --_I)
  1884. return (false);
  1885. for (; ; )
  1886. {
  1887. _BI _Ip = _I;
  1888. if (_P(*--_I, *_Ip))
  1889. {
  1890. _BI _J = _L;
  1891. for (; !_P(*_I, *--_J); )
  1892. ;
  1893. iter_swap(_I, _J);
  1894. reverse(_Ip, _L);
  1895. return (true);
  1896. }
  1897. if (_I == _F)
  1898. {
  1899. reverse(_F, _L);
  1900. return (false);
  1901. }
  1902. }
  1903. }
  1904. // TEMPLATE FUNCTION prev_permutation
  1905. template<class _BI> inline
  1906. bool prev_permutation(_BI _F, _BI _L)
  1907. {
  1908. _BI _I = _L;
  1909. if (_F == _L || _F == --_I)
  1910. return (false);
  1911. for (; ; )
  1912. {
  1913. _BI _Ip = _I;
  1914. if (!(*--_I < *_Ip))
  1915. {
  1916. _BI _J = _L;
  1917. for (; *_I < *--_J; )
  1918. ;
  1919. iter_swap(_I, _J);
  1920. reverse(_Ip, _L);
  1921. return (true);
  1922. }
  1923. if (_I == _F)
  1924. {
  1925. reverse(_F, _L);
  1926. return (false);
  1927. }
  1928. }
  1929. }
  1930. // TEMPLATE FUNCTION prev_permutation WITH PRED
  1931. template<class _BI, class _Pr> inline
  1932. bool prev_permutation(_BI _F, _BI _L, _Pr _P)
  1933. {
  1934. _BI _I = _L;
  1935. if (_F == _L || _F == --_I)
  1936. return (false);
  1937. for (; ; )
  1938. {
  1939. _BI _Ip = _I;
  1940. if (!_P(*--_I, *_Ip))
  1941. {
  1942. _BI _J = _L;
  1943. for (; _P(*_I, *--_J); )
  1944. ;
  1945. iter_swap(_I, _J);
  1946. reverse(_Ip, _L);
  1947. return (true);
  1948. }
  1949. if (_I == _F)
  1950. {
  1951. reverse(_F, _L);
  1952. return (false);
  1953. }
  1954. }
  1955. }
  1956. _STD_END
  1957. #ifdef _MSC_VER
  1958. #pragma pack(pop)
  1959. #endif /* _MSC_VER */
  1960. #endif /* _STLALGOR_H_ */
  1961. /*
  1962. * Copyright (c) 1995 by P.J. Plauger. ALL RIGHTS RESERVED.
  1963. * Consult your license regarding permissions and restrictions.
  1964. */
  1965. /*
  1966. * This file is derived from software bearing the following
  1967. * restrictions:
  1968. *
  1969. * Copyright (c) 1994
  1970. * Hewlett-Packard Company
  1971. *
  1972. * Permission to use, copy, modify, distribute and sell this
  1973. * software and its documentation for any purpose is hereby
  1974. * granted without fee, provided that the above copyright notice
  1975. * appear in all copies and that both that copyright notice and
  1976. * this permission notice appear in supporting documentation.
  1977. * Hewlett-Packard Company makes no representations about the
  1978. * suitability of this software for any purpose. It is provided
  1979. * "as is" without express or implied warranty.
  1980. */