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

1479 lines
46 KiB

  1. // algorithm standard header
  2. #ifndef _ALGORITHM_
  3. #define _ALGORITHM_
  4. #include <iterator>
  5. #include <memory>
  6. #include <xutility>
  7. #ifdef _MSC_VER
  8. #pragma pack(push,8)
  9. #endif /* _MSC_VER */
  10. _STD_BEGIN
  11. const int _CHUNK_SIZE = 7;
  12. const int _SORT_MAX = 16;
  13. // TEMPLATE FUNCTION _Median
  14. template<class _Ty> inline
  15. _Ty _Median(_Ty _X, _Ty _Y, _Ty _Z)
  16. {if (_X < _Y)
  17. return (_Y < _Z ? _Y : _X < _Z ? _Z : _X);
  18. else
  19. return (_X < _Z ? _X : _Y < _Z ? _Z : _Y); }
  20. // TEMPLATE FUNCTION _Median WITH PRED
  21. template<class _Ty, class _Pr> inline
  22. _Ty _Median(_Ty _X, _Ty _Y, _Ty _Z, _Pr _P)
  23. {if (_P(_X, _Y))
  24. return (_P(_Y, _Z) ? _Y : _P(_X, _Z) ? _Z : _X);
  25. else
  26. return (_P(_X, _Z) ? _X : _P(_Y, _Z) ? _Z : _Y); }
  27. // TEMPLATE FUNCTION for_each
  28. template<class _II, class _Fn> inline
  29. _Fn for_each(_II _F, _II _L, _Fn _Op)
  30. {for (; _F != _L; ++_F)
  31. _Op(*_F);
  32. return (_Op); }
  33. // TEMPLATE FUNCTION find
  34. template<class _II, class _Ty> inline
  35. _II find(_II _F, _II _L, const _Ty& _V)
  36. {for (; _F != _L; ++_F)
  37. if (*_F == _V)
  38. break;
  39. return (_F); }
  40. // TEMPLATE FUNCTION find_if
  41. template<class _II, class _Pr> inline
  42. _II find_if(_II _F, _II _L, _Pr _P)
  43. {for (; _F != _L; ++_F)
  44. if (_P(*_F))
  45. break;
  46. return (_F); }
  47. // TEMPLATE FUNCTION adjacent_find
  48. template<class _FI> inline
  49. _FI adjacent_find(_FI _F, _FI _L)
  50. {for (_FI _Fb; (_Fb = _F) != _L && ++_F != _L; )
  51. if (*_Fb == *_F)
  52. return (_Fb);
  53. return (_L); }
  54. // TEMPLATE FUNCTION adjacent_find WITH PRED
  55. template<class _FI, class _Pr> inline
  56. _FI adjacent_find(_FI _F, _FI _L, _Pr _P)
  57. {for (_FI _Fb; (_Fb = _F) != _L && ++_F != _L; )
  58. if (_P(*_Fb, *_F))
  59. return (_Fb);
  60. return (_L); }
  61. // TEMPLATE FUNCTION count
  62. template<class _II, class _Ty> inline
  63. _CNTSIZ(_II) count(_II _F, _II _L, const _Ty& _V)
  64. {_CNTSIZ(_II) _N = 0;
  65. for (; _F != _L; ++_F)
  66. if (*_F == _V)
  67. ++_N;
  68. return (_N); }
  69. // TEMPLATE FUNCTION count_if
  70. template<class _II, class _Pr> inline
  71. _CNTSIZ(_II) count_if(_II _F, _II _L, _Pr _P)
  72. {_CNTSIZ(_II) _N = 0;
  73. for (; _F != _L; ++_F)
  74. if (_P(*_F))
  75. ++_N;
  76. return (_N); }
  77. // TEMPLATE FUNCTION search
  78. template<class _FI1, class _FI2> inline
  79. _FI1 search(_FI1 _F1, _FI1 _L1, _FI2 _F2, _FI2 _L2)
  80. {return (_Search(_F1, _L1, _F2, _L2,
  81. _Dist_type(_F1), _Dist_type(_F2))); }
  82. template<class _FI1, class _FI2, class _Pd1, class _Pd2> inline
  83. _FI1 _Search(_FI1 _F1, _FI1 _L1, _FI2 _F2, _FI2 _L2,
  84. _Pd1 *, _Pd2 *)
  85. {_Pd1 _D1 = 0;
  86. _Distance(_F1, _L1, _D1);
  87. _Pd2 _D2 = 0;
  88. _Distance(_F2, _L2, _D2);
  89. for (; _D2 <= _D1; ++_F1, --_D1)
  90. {_FI1 _X1 = _F1;
  91. for (_FI2 _X2 = _F2; ; ++_X1, ++_X2)
  92. if (_X2 == _L2)
  93. return (_F1);
  94. else if (!(*_X1 == *_X2))
  95. break; }
  96. return (_L1); }
  97. // TEMPLATE FUNCTION search WITH PRED
  98. template<class _FI1, class _FI2, class _Pr> inline
  99. _FI1 search(_FI1 _F1, _FI1 _L1, _FI2 _F2, _FI2 _L2, _Pr _P)
  100. {return (_Search(_F1, _L1, _F2, _L2, _P,
  101. _Dist_type(_F1), _Dist_type(_F2))); }
  102. template<class _FI1, class _FI2, class _Pd1, class _Pd2,
  103. class _Pr> inline
  104. _FI1 _Search(_FI1 _F1, _FI1 _L1, _FI2 _F2, _FI2 _L2,
  105. _Pr _P, _Pd1 *, _Pd2 *)
  106. {_Pd1 _D1 = 0;
  107. _Distance(_F1, _L1, _D1);
  108. _Pd2 _D2 = 0;
  109. _Distance(_F2, _L2, _D2);
  110. for (; _D2 <= _D1; ++_F1, --_D1)
  111. {_FI1 _X1 = _F1;
  112. for (_FI2 _X2 = _F2; ; ++_X1, ++_X2)
  113. if (_X2 == _L2)
  114. return (_F1);
  115. else if (!_P(*_X1, *_X2))
  116. break; }
  117. return (_L1); }
  118. // TEMPLATE FUNCTION search_n
  119. template<class _FI1, class _Pd2, class _Ty> inline
  120. _FI1 search_n(_FI1 _F1, _FI1 _L1, _Pd2 _N, const _Ty& _V)
  121. {return (_Search_n(_F1, _L1, _N, _V, _Dist_type(_F1))); }
  122. template<class _FI1, class _Pd2, class _Ty, class _Pd1> inline
  123. _FI1 _Search_n(_FI1 _F1, _FI1 _L1,
  124. _Pd2 _N, const _Ty& _V, _Pd1 *)
  125. {_Pd1 _D1 = 0;
  126. _Distance(_F1, _L1, _D1);
  127. for (; _N <= _D1; ++_F1, --_D1)
  128. {_FI1 _X1 = _F1;
  129. for (_Pd2 _D2 = _N; ; ++_X1, --_D2)
  130. if (_D2 == 0)
  131. return (_F1);
  132. else if (!(*_X1 == _V))
  133. break; }
  134. return (_L1); }
  135. // TEMPLATE FUNCTION search_n WITH PRED
  136. template<class _FI1, class _Pd2, class _Ty, class _Pr> inline
  137. _FI1 search_n(_FI1 _F1, _FI1 _L1,
  138. _Pd2 _N, const _Ty& _V, _Pr _P)
  139. {return (_Search_n(_F1, _L1,
  140. _N, _V, _P, _Dist_type(_F1))); }
  141. template<class _FI1, class _Pd2,
  142. class _Ty, class _Pd1, class _Pr> inline
  143. _FI1 _Search_n(_FI1 _F1, _FI1 _L1,
  144. _Pd2 _N, const _Ty& _V, _Pr _P, _Pd1 *)
  145. {_Pd1 _D1 = 0;
  146. _Distance(_F1, _L1, _D1);
  147. for (; _N <= _D1; ++_F1, --_D1)
  148. {_FI1 _X1 = _F1;
  149. for (_Pd2 _D2 = _N; ; ++_X1, --_D2)
  150. if (_D2 == 0)
  151. return (_F1);
  152. else if (!_P(*_X1, _V))
  153. break; }
  154. return (_L1); }
  155. // TEMPLATE FUNCTION find_end
  156. template<class _FI1, class _FI2> inline
  157. _FI1 find_end(_FI1 _F1, _FI1 _L1, _FI2 _F2, _FI2 _L2)
  158. {return (_Find_end(_F1, _L1, _F2, _L2,
  159. _Dist_type(_F1), _Dist_type(_F2))); }
  160. template<class _FI1, class _FI2, class _Pd1, class _Pd2> inline
  161. _FI1 _Find_end(_FI1 _F1, _FI1 _L1, _FI2 _F2, _FI2 _L2,
  162. _Pd1 *, _Pd2 *)
  163. {_Pd1 _D1 = 0;
  164. _Distance(_F1, _L1, _D1);
  165. _Pd2 _D2 = 0;
  166. _Distance(_F2, _L2, _D2);
  167. _FI1 _Ans = _L1;
  168. if (0 < _D2)
  169. for (; _D2 <= _D1; ++_F1, --_D1)
  170. {_FI1 _X1 = _F1;
  171. for (_FI2 _X2 = _F2; ; ++_X1)
  172. if (!(*_X1 == *_X2))
  173. break;
  174. else if (++_X2 == _L2)
  175. {_Ans = _F1;
  176. break; }}
  177. return (_Ans); }
  178. // TEMPLATE FUNCTION find_end WITH PRED
  179. template<class _FI1, class _FI2, class _Pr> inline
  180. _FI1 find_end(_FI1 _F1, _FI1 _L1, _FI2 _F2, _FI2 _L2, _Pr _P)
  181. {return (_Find_end(_F1, _L1, _F2, _L2, _P,
  182. _Dist_type(_F1), _Dist_type(_F2))); }
  183. template<class _FI1, class _FI2, class _Pd1, class _Pd2,
  184. class _Pr> inline
  185. _FI1 _Find_end(_FI1 _F1, _FI1 _L1, _FI2 _F2, _FI2 _L2, _Pr _P,
  186. _Pd1 *, _Pd2 *)
  187. {_Pd1 _D1 = 0;
  188. _Distance(_F1, _L1, _D1);
  189. _Pd2 _D2 = 0;
  190. _Distance(_F2, _L2, _D2);
  191. _FI1 _Ans = _L1;
  192. if (0 < _D2)
  193. for (; _D2 <= _D1; ++_F1, --_D1)
  194. {_FI1 _X1 = _F1;
  195. for (_FI2 _X2 = _F2; ; ++_X1)
  196. if (!_P(*_X1, *_X2))
  197. break;
  198. else if (++_X2 == _L2)
  199. {_Ans = _F1;
  200. break; }}
  201. return (_Ans); }
  202. // TEMPLATE FUNCTION find_first_of
  203. template<class _FI1, class _FI2> inline
  204. _FI1 find_first_of(_FI1 _F1, _FI1 _L1, _FI2 _F2, _FI2 _L2)
  205. {for (; _F1 != _L1; ++_F1)
  206. for (_FI2 _X2 = _F2; _X2 != _L2; ++_X2)
  207. if (*_F1 == *_X2)
  208. return (_F1);
  209. return (_F1); }
  210. // TEMPLATE FUNCTION find_first_of WITH PRED
  211. template<class _FI1, class _FI2, class _Pr> inline
  212. _FI1 find_first_of(_FI1 _F1, _FI1 _L1, _FI2 _F2, _FI2 _L2,
  213. _Pr _P)
  214. {for (; _F1 != _L1; ++_F1)
  215. for (_FI2 _X2 = _F2; _X2 != _L2; ++_X2)
  216. if (_P(*_F1, *_X2))
  217. return (_F1);
  218. return (_F1); }
  219. // TEMPLATE FUNCTION iter_swap
  220. template<class _FI1, class _FI2> inline
  221. void iter_swap(_FI1 _X, _FI2 _Y)
  222. {_Iter_swap(_X, _Y, _Val_type(_X)); }
  223. template<class _FI1, class _FI2, class _Ty> inline
  224. void _Iter_swap(_FI1 _X, _FI2 _Y, _Ty *)
  225. {_Ty _Tmp = *_X;
  226. *_X = *_Y, *_Y = _Tmp; }
  227. // TEMPLATE FUNCTION swap_ranges
  228. template<class _FI1, class _FI2> inline
  229. _FI2 swap_ranges(_FI1 _F, _FI1 _L, _FI2 _X)
  230. {for (; _F != _L; ++_F, ++_X)
  231. iter_swap(_F, _X);
  232. return (_X); }
  233. // TEMPLATE FUNCTION transform WITH UNARY OP
  234. template<class _II, class _OI, class _Uop> inline
  235. _OI transform(_II _F, _II _L, _OI _X, _Uop _U)
  236. {for (; _F != _L; ++_F, ++_X)
  237. *_X = _U(*_F);
  238. return (_X); }
  239. // TEMPLATE FUNCTION transform WITH BINARY OP
  240. template<class _II1, class _II2, class _OI, class _Bop> inline
  241. _OI transform(_II1 _F1, _II1 _L1, _II2 _F2, _OI _X, _Bop _B)
  242. {for (; _F1 != _L1; ++_F1, ++_F2, ++_X)
  243. *_X = _B(*_F1, *_F2);
  244. return (_X); }
  245. // TEMPLATE FUNCTION replace
  246. template<class _FI, class _Ty> inline
  247. void replace(_FI _F, _FI _L, const _Ty& _Vo, const _Ty& _Vn)
  248. {for (; _F != _L; ++_F)
  249. if (*_F == _Vo)
  250. *_F = _Vn; }
  251. // TEMPLATE FUNCTION replace_if
  252. template<class _FI, class _Pr, class _Ty> inline
  253. void replace_if(_FI _F, _FI _L, _Pr _P, const _Ty& _V)
  254. {for (; _F != _L; ++_F)
  255. if (_P(*_F))
  256. *_F = _V; }
  257. // TEMPLATE FUNCTION replace_copy
  258. template<class _II, class _OI, class _Ty> inline
  259. _OI replace_copy(_II _F, _II _L, _OI _X,
  260. const _Ty& _Vo, const _Ty& _Vn)
  261. {for (; _F != _L; ++_F, ++_X)
  262. *_X = *_F == _Vo ? _Vn : *_F;
  263. return (_X); }
  264. // TEMPLATE FUNCTION replace_copy_if
  265. template<class _II, class _OI, class _Pr, class _Ty> inline
  266. _OI replace_copy_if(_II _F, _II _L, _OI _X,
  267. _Pr _P, const _Ty& _V)
  268. {for (; _F != _L; ++_F, ++_X)
  269. *_X = _P(*_F) ? _V : *_F;
  270. return (_X); }
  271. // TEMPLATE FUNCTION generate
  272. template<class _FI, class _Gen> inline
  273. void generate(_FI _F, _FI _L, _Gen _G)
  274. {for (; _F != _L; ++_F)
  275. *_F = _G(); }
  276. // TEMPLATE FUNCTION generate_n
  277. template<class _OI, class _Pd, class _Gen> inline
  278. void generate_n(_OI _F, _Pd _N, _Gen _G)
  279. {for (; 0 < _N; --_N, ++_F)
  280. *_F = _G(); }
  281. // TEMPLATE FUNCTION remove
  282. template<class _FI, class _Ty> inline
  283. _FI remove(_FI _F, _FI _L, const _Ty& _V)
  284. {_F = find(_F, _L, _V);
  285. if (_F == _L)
  286. return (_F);
  287. else
  288. {_FI _Fb = _F;
  289. return (remove_copy(++_F, _L, _Fb, _V)); }}
  290. // TEMPLATE FUNCTION remove_if
  291. template<class _FI, class _Pr> inline
  292. _FI remove_if(_FI _F, _FI _L, _Pr _P)
  293. {_F = find_if(_F, _L, _P);
  294. if (_F == _L)
  295. return (_F);
  296. else
  297. {_FI _Fb = _F;
  298. return (remove_copy_if(++_F, _L, _Fb, _P)); }}
  299. // TEMPLATE FUNCTION remove_copy
  300. template<class _II, class _OI, class _Ty> inline
  301. _OI remove_copy(_II _F, _II _L, _OI _X, const _Ty& _V)
  302. {for (; _F != _L; ++_F)
  303. if (!(*_F == _V))
  304. *_X++ = *_F;
  305. return (_X); }
  306. // TEMPLATE FUNCTION remove_copy_if
  307. template<class _II, class _OI, class _Pr> inline
  308. _OI remove_copy_if(_II _F, _II _L, _OI _X, _Pr _P)
  309. {for (; _F != _L; ++_F)
  310. if (!_P(*_F))
  311. *_X++ = *_F;
  312. return (_X); }
  313. // TEMPLATE FUNCTION unique
  314. template<class _FI> inline
  315. _FI unique(_FI _F, _FI _L)
  316. {_F = adjacent_find(_F, _L);
  317. return (unique_copy(_F, _L, _F)); }
  318. // TEMPLATE FUNCTION unique WITH PRED
  319. template<class _FI, class _Pr> inline
  320. _FI unique(_FI _F, _FI _L, _Pr _P)
  321. {_F = adjacent_find(_F, _L, _P);
  322. return (unique_copy(_F, _L, _F, _P)); }
  323. // TEMPLATE FUNCTION unique_copy
  324. template<class _II, class _OI> inline
  325. _OI unique_copy(_II _F, _II _L, _OI _X)
  326. {return (_F == _L ? _X :
  327. _Unique_copy(_F, _L, _X, _Iter_cat(_F))); }
  328. template<class _II, class _OI> inline
  329. _OI _Unique_copy(_II _F, _II _L, _OI _X, input_iterator_tag)
  330. {return (_Unique_copy(_F, _L, _X, _Val_type(_F))); }
  331. template<class _II, class _OI, class _Ty> inline
  332. _OI _Unique_copy(_II _F, _II _L, _OI _X, _Ty *)
  333. {_Ty _V = *_F;
  334. for (*_X++ = _V; ++_F != _L; )
  335. if (!(_V == *_F))
  336. _V = *_F, *_X++ = _V;
  337. return (_X); }
  338. template<class _FI, class _OI> inline
  339. _OI _Unique_copy(_FI _F, _FI _L, _OI _X, forward_iterator_tag)
  340. {_FI _Fb = _F;
  341. for (*_X++ = *_Fb; ++_F != _L; )
  342. if (!(*_Fb == *_F))
  343. _Fb = _F, *_X++ = *_Fb;
  344. return (_X); }
  345. template<class _BI, class _OI> inline
  346. _OI _Unique_copy(_BI _F, _BI _L, _OI _X,
  347. bidirectional_iterator_tag)
  348. {return (_Unique_copy(_F, _L, _X, forward_iterator_tag())); }
  349. template<class _RI, class _OI> inline
  350. _OI _Unique_copy(_RI _F, _RI _L, _OI _X,
  351. random_access_iterator_tag)
  352. {return (_Unique_copy(_F, _L, _X, forward_iterator_tag())); }
  353. // TEMPLATE FUNCTION unique_copy WITH PRED
  354. template<class _II, class _OI, class _Pr> inline
  355. _OI unique_copy(_II _F, _II _L, _OI _X, _Pr _P)
  356. {return (_F == _L ? _X :
  357. _Unique_copy(_F, _L, _X, _P, _Iter_cat(_F))); }
  358. template<class _II, class _OI, class _Pr> inline
  359. _OI _Unique_copy(_II _F, _II _L, _OI _X, _Pr _P,
  360. input_iterator_tag)
  361. {return (_Unique_copy(_F, _L, _X, _P, _Val_type(_F))); }
  362. template<class _II, class _OI, class _Ty, class _Pr> inline
  363. _OI _Unique_copy(_II _F, _II _L, _OI _X, _Pr _P, _Ty *)
  364. {_Ty _V = *_F;
  365. for (*_X++ = _V; ++_F != _L; )
  366. if (!_P(_V, *_F))
  367. _V = *_F, *_X++ = _V;
  368. return (_X); }
  369. template<class _FI, class _OI, class _Pr> inline
  370. _OI _Unique_copy(_FI _F, _FI _L, _OI _X, _Pr _P,
  371. forward_iterator_tag)
  372. {_FI _Fb = _F;
  373. for (*_X++ = *_Fb; ++_F != _L; )
  374. if (!_P(*_Fb, *_F))
  375. _Fb = _F, *_X++ = *_Fb;
  376. return (_X); }
  377. template<class _BI, class _OI, class _Pr> inline
  378. _OI _Unique_copy(_BI _F, _BI _L, _OI _X, _Pr _P,
  379. bidirectional_iterator_tag)
  380. {return (_Unique_copy(_F, _L, _X, _P,
  381. forward_iterator_tag())); }
  382. template<class _RI, class _OI, class _Pr> inline
  383. _OI _Unique_copy(_RI _F, _RI _L, _OI _X, _Pr _P,
  384. random_access_iterator_tag)
  385. {return (_Unique_copy(_F, _L, _X, _P,
  386. forward_iterator_tag())); }
  387. // TEMPLATE FUNCTION reverse
  388. template<class _BI> inline
  389. void reverse(_BI _F, _BI _L)
  390. {_Reverse(_F, _L, _Iter_cat(_F)); }
  391. template<class _BI> inline
  392. void _Reverse(_BI _F, _BI _L, bidirectional_iterator_tag)
  393. {for (; _F != _L && _F != --_L; ++_F)
  394. iter_swap(_F, _L); }
  395. template<class _RI> inline
  396. void _Reverse(_RI _F, _RI _L, random_access_iterator_tag)
  397. {for (; _F < _L; ++_F)
  398. iter_swap(_F, --_L); }
  399. // TEMPLATE FUNCTION reverse_copy
  400. template<class _BI, class _OI> inline
  401. _OI reverse_copy(_BI _F, _BI _L, _OI _X)
  402. {for (; _F != _L; ++_X)
  403. *_X = *--_L;
  404. return (_X); }
  405. // TEMPLATE FUNCTION rotate
  406. template<class _FI> inline
  407. void rotate(_FI _F, _FI _M, _FI _L)
  408. {if (_F != _M && _M != _L)
  409. _Rotate(_F, _M, _L, _Iter_cat(_F)); }
  410. template<class _FI> inline
  411. void _Rotate(_FI _F, _FI _M, _FI _L,
  412. forward_iterator_tag)
  413. {for (_FI _X = _M; ; )
  414. {iter_swap(_F, _X);
  415. if (++_F == _M)
  416. if (++_X == _L)
  417. break;
  418. else
  419. _M = _X;
  420. else if (++_X == _L)
  421. _X = _M; }}
  422. template<class _BI> inline
  423. void _Rotate(_BI _F, _BI _M, _BI _L,
  424. bidirectional_iterator_tag)
  425. {reverse(_F, _M);
  426. reverse(_M, _L);
  427. reverse(_F, _L); }
  428. template<class _RI> inline
  429. void _Rotate(_RI _F, _RI _M, _RI _L,
  430. random_access_iterator_tag)
  431. {_Rotate(_F, _M, _L, _Dist_type(_F), _Val_type(_F)); }
  432. template<class _RI, class _Pd, class _Ty> inline
  433. void _Rotate(_RI _F, _RI _M, _RI _L, _Pd *, _Ty *)
  434. {_Pd _D = _M - _F;
  435. _Pd _N = _L - _F;
  436. for (_Pd _I = _D; _I != 0; )
  437. {_Pd _J = _N % _I;
  438. _N = _I, _I = _J; }
  439. if (_N < _L - _F)
  440. for (; 0 < _N; --_N)
  441. {_RI _X = _F + _N;
  442. _RI _Y = _X;
  443. _Ty _V = *_X;
  444. _RI _Z = _Y + _D == _L ? _F : _Y + _D;
  445. while (_Z != _X)
  446. {*_Y = *_Z;
  447. _Y = _Z;
  448. _Z = _D < _L - _Z ? _Z + _D
  449. : _F + (_D - (_L - _Z)); }
  450. *_Y = _V; }}
  451. // TEMPLATE FUNCTION rotate_copy
  452. template<class _FI, class _OI> inline
  453. _OI rotate_copy(_FI _F, _FI _M, _FI _L, _OI _X)
  454. {return (copy(_F, _M, copy(_M, _L, _X))); }
  455. // TEMPLATE FUNCTION random_shuffle
  456. template<class _RI> inline
  457. void random_shuffle(_RI _F, _RI _L)
  458. {if (_F != _L)
  459. _Random_shuffle(_F, _L, _Dist_type(_F)); }
  460. template<class _RI, class _Pd> inline
  461. void _Random_shuffle(_RI _F, _RI _L, _Pd *)
  462. {const int _RBITS = 15;
  463. const int _RMAX = (1U << _RBITS) - 1;
  464. _RI _X = _F;
  465. for (_Pd _D = 1; ++_X != _L; ++_D)
  466. {unsigned long _Rm = _RMAX;
  467. unsigned long _Rn = rand() & _RMAX;
  468. for (; _Rm < _D && _Rm != ~0UL;
  469. _Rm = _Rm << _RBITS | _RMAX)
  470. _Rn = _Rn << _RBITS | _RMAX;
  471. iter_swap(_X, _F + _Pd(_Rn % _D)); }}
  472. template<class _RI, class _Pf> inline
  473. void random_shuffle(_RI _F, _RI _L, _Pf& _R)
  474. {if (_F != _L)
  475. _Random_shuffle(_F, _L, _R, _Dist_type(_F)); }
  476. template<class _RI, class _Pf, class _Pd> inline
  477. void _Random_shuffle(_RI _F, _RI _L, _Pf& _R, _Pd *)
  478. {_RI _X = _F;
  479. for (_Pd _D = 1; ++_X != _L; ++_D)
  480. iter_swap(_X, _F + _Pd(_R(_D))); }
  481. // TEMPLATE FUNCTION partition
  482. template<class _BI, class _Pr> inline
  483. _BI partition(_BI _F, _BI _L, _Pr _P)
  484. {for (; ; ++_F)
  485. {for (; _F != _L && _P(*_F); ++_F)
  486. ;
  487. if (_F == _L)
  488. break;
  489. for (; _F != --_L && !_P(*_L); )
  490. ;
  491. if (_F == _L)
  492. break;
  493. iter_swap(_F, _L); }
  494. return (_F); }
  495. // TEMPLATE FUNCTION stable_partition
  496. template<class _FI, class _Pr> inline
  497. _FI stable_partition(_FI _F, _FI _L, _Pr _P)
  498. {return (_F == _L ? _F : _Stable_partition(_F, _L, _P,
  499. _Dist_type(_F), _Val_type(_F))); }
  500. template<class _FI, class _Pr, class _Pd, class _Ty> inline
  501. _FI _Stable_partition(_FI _F, _FI _L, _Pr _P, _Pd *, _Ty *)
  502. {_Pd _N = 0;
  503. _Distance(_F, _L, _N);
  504. _Temp_iterator<_Ty> _Xb(_N);
  505. return (_Stable_partition(_F, _L, _P, _N, _Xb)); }
  506. template<class _FI, class _Pr, class _Pd, class _Ty> inline
  507. _FI _Stable_partition(_FI _F, _FI _L, _Pr _P, _Pd _N,
  508. _Temp_iterator<_Ty>& _Xb)
  509. {if (_N == 1)
  510. return (_P(*_F) ? _L : _F);
  511. else if (_N <= _Xb._Maxlen())
  512. {_FI _X = _F;
  513. for (_Xb._Init(); _F != _L; ++_F)
  514. if (_P(*_F))
  515. *_X++ = *_F;
  516. else
  517. *_Xb++ = *_F;
  518. copy(_Xb._First(), _Xb._Last(), _X);
  519. return (_X); }
  520. else
  521. {_FI _M = _F;
  522. advance(_M, _N / 2);
  523. _FI _Lp = _Stable_partition(_F, _M, _P, _N / 2, _Xb);
  524. _FI _Rp = _Stable_partition(_M, _L, _P, _N - _N / 2, _Xb);
  525. _Pd _D1 = 0;
  526. _Distance(_Lp, _M, _D1);
  527. _Pd _D2 = 0;
  528. _Distance(_M, _Rp, _D2);
  529. return (_Buffered_rotate(_Lp, _M, _Rp, _D1, _D2, _Xb)); }}
  530. // TEMPLATE FUNCTION sort
  531. template<class _RI> inline
  532. void sort(_RI _F, _RI _L)
  533. {_Sort_0(_F, _L, _Val_type(_F)); }
  534. template<class _RI, class _Ty> inline
  535. void _Sort_0(_RI _F, _RI _L, _Ty *)
  536. {if (_L - _F <= _SORT_MAX)
  537. _Insertion_sort(_F, _L);
  538. else
  539. {_Sort(_F, _L, (_Ty *)0);
  540. _Insertion_sort(_F, _F + _SORT_MAX);
  541. for (_F += _SORT_MAX; _F != _L; ++_F)
  542. _Unguarded_insert(_F, _Ty(*_F)); }}
  543. template<class _RI, class _Ty> inline
  544. void _Sort(_RI _F, _RI _L, _Ty *)
  545. {for (; _SORT_MAX < _L - _F; )
  546. {_RI _M = _Unguarded_partition(_F, _L, _Median(_Ty(*_F),
  547. _Ty(*(_F + (_L - _F) / 2)), _Ty(*(_L - 1))));
  548. if (_L - _M <= _M - _F)
  549. _Sort(_M, _L, _Val_type(_F)), _L = _M;
  550. else
  551. _Sort(_F, _M, _Val_type(_F)), _F = _M; }}
  552. template<class _RI, class _Ty> inline
  553. _RI _Unguarded_partition(_RI _F, _RI _L, _Ty _Piv)
  554. {for (; ; ++_F)
  555. {for (; *_F < _Piv; ++_F)
  556. ;
  557. for (; _Piv < *--_L; )
  558. ;
  559. if (_L <= _F)
  560. return (_F);
  561. iter_swap(_F, _L); }}
  562. template<class _RI> inline
  563. void _Insertion_sort(_RI _F, _RI _L)
  564. {_Insertion_sort_1(_F, _L, _Val_type(_F)); }
  565. template<class _BI, class _Ty> inline
  566. void _Insertion_sort_1(_BI _F, _BI _L, _Ty *)
  567. {if (_F != _L)
  568. for (_BI _M = _F; ++_M != _L; )
  569. {_Ty _V = *_M;
  570. if (!(_V < *_F))
  571. _Unguarded_insert(_M, _V);
  572. else
  573. {copy_backward(_F, _M, _M + 1);
  574. *_F = _V; }}}
  575. template<class _BI, class _Ty> inline
  576. void _Unguarded_insert(_BI _L, _Ty _V)
  577. {for (_BI _M = _L; _V < *--_M; _L = _M)
  578. *_L = *_M;
  579. *_L = _V; }
  580. // TEMPLATE FUNCTION sort WITH PRED
  581. template<class _RI, class _Pr> inline
  582. void sort(_RI _F, _RI _L, _Pr _P)
  583. {_Sort_0(_F, _L, _P, _Val_type(_F)); }
  584. template<class _RI, class _Ty, class _Pr> inline
  585. void _Sort_0(_RI _F, _RI _L, _Pr _P, _Ty *)
  586. {if (_L - _F <= _SORT_MAX)
  587. _Insertion_sort(_F, _L, _P);
  588. else
  589. {_Sort(_F, _L, _P, (_Ty *)0);
  590. _Insertion_sort(_F, _F + _SORT_MAX, _P);
  591. for (_F += _SORT_MAX; _F != _L; ++_F)
  592. _Unguarded_insert(_F, _Ty(*_F), _P); }}
  593. template<class _RI, class _Ty, class _Pr> inline
  594. void _Sort(_RI _F, _RI _L, _Pr _P, _Ty *)
  595. {for (; _SORT_MAX < _L - _F; )
  596. {_RI _M = _Unguarded_partition(_F, _L, _Median(_Ty(*_F),
  597. _Ty(*(_F + (_L - _F) / 2)), _Ty(*(_L - 1)), _P), _P);
  598. if (_L - _M <= _M - _F)
  599. _Sort(_M, _L, _P, _Val_type(_F)), _L = _M;
  600. else
  601. _Sort(_F, _M, _P, _Val_type(_F)), _F = _M; }}
  602. template<class _RI, class _Ty, class _Pr> inline
  603. _RI _Unguarded_partition(_RI _F, _RI _L, _Ty _Piv, _Pr _P)
  604. {for (; ; ++_F)
  605. {for (; _P(*_F, _Piv); ++_F)
  606. ;
  607. for (; _P(_Piv, *--_L); )
  608. ;
  609. if (_L <= _F)
  610. return (_F);
  611. iter_swap(_F, _L); }}
  612. template<class _RI, class _Pr> inline
  613. void _Insertion_sort(_RI _F, _RI _L, _Pr _P)
  614. {_Insertion_sort_1(_F, _L, _P, _Val_type(_F)); }
  615. template<class _RI, class _Ty, class _Pr> inline
  616. void _Insertion_sort_1(_RI _F, _RI _L, _Pr _P, _Ty *)
  617. {if (_F != _L)
  618. for (_RI _M = _F; ++_M != _L; )
  619. {_Ty _V = *_M;
  620. if (!_P(_V, *_F))
  621. _Unguarded_insert(_M, _V, _P);
  622. else
  623. {copy_backward(_F, _M, _M + 1);
  624. *_F = _V; }}}
  625. template<class _RI, class _Ty, class _Pr> inline
  626. void _Unguarded_insert(_RI _L, _Ty _V, _Pr _P)
  627. {for (_RI _M = _L; _P(_V, *--_M); _L = _M)
  628. *_L = *_M;
  629. *_L = _V; }
  630. // TEMPLATE FUNCTION stable_sort
  631. template<class _BI> inline
  632. void stable_sort(_BI _F, _BI _L)
  633. {if (_F != _L)
  634. _Stable_sort(_F, _L, _Dist_type(_F), _Val_type(_F)); }
  635. template<class _BI, class _Pd, class _Ty> inline
  636. void _Stable_sort(_BI _F, _BI _L, _Pd *, _Ty *)
  637. {_Pd _N = 0;
  638. _Distance(_F, _L, _N);
  639. _Temp_iterator<_Ty> _Xb(_N);
  640. _Stable_sort(_F, _L, _N, _Xb); }
  641. template<class _BI, class _Pd, class _Ty> inline
  642. void _Stable_sort(_BI _F, _BI _L, _Pd _N,
  643. _Temp_iterator<_Ty>& _Xb)
  644. {if (_N <= _SORT_MAX)
  645. _Insertion_sort(_F, _L);
  646. else
  647. {_Pd _N2 = (_N + 1) / 2;
  648. _BI _M = _F;
  649. advance(_M, _N2);
  650. if (_N2 <= _Xb._Maxlen())
  651. {_Buffered_merge_sort(_F, _M, _N2, _Xb);
  652. _Buffered_merge_sort(_M, _L, _N - _N2, _Xb); }
  653. else
  654. {_Stable_sort(_F, _M, _N2, _Xb);
  655. _Stable_sort(_M, _L, _N - _N2, _Xb); }
  656. _Buffered_merge(_F, _M, _L, _N2, _N - _N2, _Xb); }}
  657. template<class _BI, class _Pd, class _Ty> inline
  658. void _Buffered_merge_sort(_BI _F, _BI _L, _Pd _N,
  659. _Temp_iterator<_Ty>& _Xb)
  660. {_BI _M = _F;
  661. for (_Pd _I = _N; _CHUNK_SIZE <= _I; _I -= _CHUNK_SIZE)
  662. {_BI _Mn = _M;
  663. advance(_Mn, (int)_CHUNK_SIZE);
  664. _Insertion_sort(_M, _Mn);
  665. _M = _Mn; }
  666. _Insertion_sort(_M, _L);
  667. for (_Pd _D = _CHUNK_SIZE; _D < _N; _D *= 2)
  668. {_BI _Ft = _F;
  669. _Chunked_merge(_F, _L, _Xb._Init(), _D, _N);
  670. _Chunked_merge(_Xb._First(), _Xb._Last(), _Ft,
  671. _D *= 2, _N); }}
  672. template<class _BI, class _OI, class _Pd> inline
  673. void _Chunked_merge(_BI _F, _BI _L, _OI& _X, _Pd _D, _Pd _N)
  674. {_Pd _D2 = _D * 2;
  675. for (; _D2 <= _N; _N -= _D2)
  676. {_BI _F1 = _F;
  677. advance(_F1, _D);
  678. _BI _F2 = _F1;
  679. advance(_F2, _D);
  680. _X = merge(_F, _F1, _F1, _F2, _X);
  681. _F = _F2; }
  682. if (_N <= _D)
  683. copy(_F, _L, _X);
  684. else
  685. {_BI _F1 = _F;
  686. advance(_F1, _D);
  687. merge(_F, _F1, _F1, _L, _X); }}
  688. // TEMPLATE FUNCTION stable_sort WITH PRED
  689. template<class _BI, class _Pr> inline
  690. void stable_sort(_BI _F, _BI _L, _Pr _P)
  691. {if (_F != _L)
  692. _Stable_sort(_F, _L,
  693. _Dist_type(_F), _Val_type(_F), _P); }
  694. template<class _BI, class _Pd, class _Ty, class _Pr> inline
  695. void _Stable_sort(_BI _F, _BI _L, _Pd *, _Ty *, _Pr _P)
  696. {_Pd _N = 0;
  697. _Distance(_F, _L, _N);
  698. _Temp_iterator<_Ty> _Xb(_N);
  699. _Stable_sort(_F, _L, _N, _Xb, _P); }
  700. template<class _BI, class _Pd, class _Ty, class _Pr> inline
  701. void _Stable_sort(_BI _F, _BI _L, _Pd _N,
  702. _Temp_iterator<_Ty>& _Xb, _Pr _P)
  703. {if (_N <= _SORT_MAX)
  704. _Insertion_sort(_F, _L, _P);
  705. else
  706. {_Pd _N2 = (_N + 1) / 2;
  707. _BI _M = _F;
  708. advance(_M, _N2);
  709. if (_N2 <= _Xb._Maxlen())
  710. {_Buffered_merge_sort(_F, _M, _N2, _Xb, _P);
  711. _Buffered_merge_sort(_M, _L, _N - _N2, _Xb, _P); }
  712. else
  713. {_Stable_sort(_F, _M, _N2, _Xb, _P);
  714. _Stable_sort(_M, _L, _N - _N2, _Xb, _P); }
  715. _Buffered_merge(_F, _M, _L, _N2, _N - _N2, _Xb, _P); }}
  716. template<class _BI, class _Pd, class _Ty, class _Pr> inline
  717. void _Buffered_merge_sort(_BI _F, _BI _L, _Pd _N,
  718. _Temp_iterator<_Ty>& _Xb, _Pr _P)
  719. {_BI _M = _F;
  720. for (_Pd _I = _N; _CHUNK_SIZE <= _I; _I -= _CHUNK_SIZE)
  721. {_BI _Mn = _M;
  722. advance(_Mn, (int)_CHUNK_SIZE);
  723. _Insertion_sort(_M, _Mn, _P);
  724. _M = _Mn; }
  725. _Insertion_sort(_M, _L, _P);
  726. for (_Pd _D = _CHUNK_SIZE; _D < _N; _D *= 2)
  727. {_BI _Ft = _F;
  728. _Chunked_merge(_F, _L, _Xb._Init(), _D, _N, _P);
  729. _Chunked_merge(_Xb._First(), _Xb._Last(), _Ft,
  730. _D *= 2, _N, _P); }}
  731. template<class _BI, class _OI, class _Pd, class _Pr> inline
  732. void _Chunked_merge(_BI _F, _BI _L, _OI& _X,
  733. _Pd _D, _Pd _N, _Pr _P)
  734. {_Pd _D2 = _D * 2;
  735. for (; _D2 <= _N; _N -= _D2)
  736. {_BI _F1 = _F;
  737. advance(_F1, _D);
  738. _BI _F2 = _F1;
  739. advance(_F2, _D);
  740. _X = merge(_F, _F1, _F1, _F2, _X, _P);
  741. _F = _F2; }
  742. if (_N <= _D)
  743. copy(_F, _L, _X);
  744. else
  745. {_BI _F1 = _F;
  746. advance(_F1, _D);
  747. merge(_F, _F1, _F1, _L, _X, _P); }}
  748. // TEMPLATE FUNCTION partial_sort
  749. template<class _RI> inline
  750. void partial_sort(_RI _F, _RI _M, _RI _L)
  751. {_Partial_sort(_F, _M, _L, _Val_type(_F)); }
  752. template<class _RI, class _Ty> inline
  753. void _Partial_sort(_RI _F, _RI _M, _RI _L, _Ty *)
  754. {make_heap(_F, _M);
  755. for (_RI _I = _M; _I < _L; ++_I)
  756. if (*_I < *_F)
  757. _Pop_heap(_F, _M, _I, _Ty(*_I), _Dist_type(_F));
  758. sort_heap(_F, _M); }
  759. // TEMPLATE FUNCTION partial_sort WITH PRED
  760. template<class _RI, class _Pr> inline
  761. void partial_sort(_RI _F, _RI _M, _RI _L, _Pr _P)
  762. {_Partial_sort(_F, _M, _L, _P, _Val_type(_F)); }
  763. template<class _RI, class _Ty, class _Pr> inline
  764. void _Partial_sort(_RI _F, _RI _M, _RI _L, _Pr _P, _Ty *)
  765. {make_heap(_F, _M, _P);
  766. for (_RI _I = _M; _I < _L; ++_I)
  767. if (_P(*_I, *_F))
  768. _Pop_heap(_F, _M, _I, _Ty(*_I), _P, _Dist_type(_F));
  769. sort_heap(_F, _M, _P); }
  770. // TEMPLATE FUNCTION partial_sort_copy
  771. template<class _II, class _RI> inline
  772. _RI partial_sort_copy(_II _F1, _II _L1, _RI _F2, _RI _L2)
  773. {return (_Partial_sort_copy(_F1, _L1, _F2, _L2,
  774. _Dist_type(_F2), _Val_type(_F1))); }
  775. template<class _II, class _RI, class _Pd, class _Ty> inline
  776. _RI _Partial_sort_copy(_II _F1, _II _L1, _RI _F2, _RI _L2,
  777. _Pd *, _Ty *)
  778. {_RI _X = _F2;
  779. if (_X != _L2)
  780. {for (; _F1 != _L1 && _X != _L2; ++_F1, ++_X)
  781. *_X = *_F1;
  782. make_heap(_F2, _X);
  783. for (; _F1 != _L1; ++_F1)
  784. if (*_F1 < *_F2)
  785. _Adjust_heap(_F2, _Pd(0), _Pd(_X - _F2),
  786. _Ty(*_F1));
  787. sort_heap(_F2, _X); }
  788. return (_X); }
  789. // TEMPLATE FUNCTION partial_sort_copy WITH PRED
  790. template<class _II, class _RI, class _Pr> inline
  791. _RI partial_sort_copy(_II _F1, _II _L1, _RI _F2, _RI _L2,
  792. _Pr _P)
  793. {return (_Partial_sort_copy(_F1, _L1, _F2, _L2, _P,
  794. _Dist_type(_F2), _Val_type(_F1))); }
  795. template<class _II, class _RI, class _Pd,
  796. class _Ty, class _Pr> inline
  797. _RI _Partial_sort_copy(_II _F1, _II _L1, _RI _F2, _RI _L2,
  798. _Pr _P, _Pd *, _Ty *)
  799. {_RI _X = _F2;
  800. if (_X != _L2)
  801. {for (; _F1 != _L1 && _X != _L2; ++_F1, ++_X)
  802. *_X = *_F1;
  803. make_heap(_F2, _X, _P);
  804. for (; _F1 != _L1; ++_F1)
  805. if (_P(*_F1, *_F2))
  806. _Adjust_heap(_F2, _Pd(0), _Pd(_X - _F2),
  807. _Ty(*_F1), _P);
  808. sort_heap(_F2, _X, _P); }
  809. return (_X); }
  810. // TEMPLATE FUNCTION nth_element
  811. template<class _RI> inline
  812. void nth_element(_RI _F, _RI _Nth, _RI _L)
  813. {_Nth_element(_F, _Nth, _L, _Val_type(_F)); }
  814. template<class _RI, class _Ty> inline
  815. void _Nth_element(_RI _F, _RI _Nth, _RI _L, _Ty *)
  816. {for (; _SORT_MAX < _L - _F; )
  817. {_RI _M = _Unguarded_partition(_F, _L, _Median(_Ty(*_F),
  818. _Ty(*(_F + (_L - _F) / 2)), _Ty(*(_L - 1))));
  819. if (_M <= _Nth)
  820. _F = _M;
  821. else
  822. _L = _M; }
  823. _Insertion_sort(_F, _L); }
  824. // TEMPLATE FUNCTION nth_element WITH PRED
  825. template<class _RI, class _Pr> inline
  826. void nth_element(_RI _F, _RI _Nth, _RI _L, _Pr _P)
  827. {_Nth_element(_F, _Nth, _L, _P, _Val_type(_F)); }
  828. template<class _RI, class _Ty, class _Pr> inline
  829. void _Nth_element(_RI _F, _RI _Nth, _RI _L, _Pr _P, _Ty *)
  830. {for (; _SORT_MAX < _L - _F; )
  831. {_RI _M = _Unguarded_partition(_F, _L, _Median(_Ty(*_F),
  832. _Ty(*(_F + (_L - _F) / 2)), _Ty(*(_L - 1)), _P), _P);
  833. if (_M <= _Nth)
  834. _F = _M;
  835. else
  836. _L = _M; }
  837. _Insertion_sort(_F, _L, _P); }
  838. // TEMPLATE FUNCTION lower_bound
  839. template<class _FI, class _Ty> inline
  840. _FI lower_bound(_FI _F, _FI _L, const _Ty& _V)
  841. {return (_Lower_bound(_F, _L, _V, _Dist_type(_F))); }
  842. template<class _FI, class _Ty, class _Pd> inline
  843. _FI _Lower_bound(_FI _F, _FI _L, const _Ty& _V, _Pd *)
  844. {_Pd _N = 0;
  845. _Distance(_F, _L, _N);
  846. for (; 0 < _N; )
  847. {_Pd _N2 = _N / 2;
  848. _FI _M = _F;
  849. advance(_M, _N2);
  850. if (*_M < _V)
  851. _F = ++_M, _N -= _N2 + 1;
  852. else
  853. _N = _N2; }
  854. return (_F); }
  855. // TEMPLATE FUNCTION lower_bound WITH PRED
  856. template<class _FI, class _Ty, class _Pr> inline
  857. _FI lower_bound(_FI _F, _FI _L, const _Ty& _V, _Pr _P)
  858. {return (_Lower_bound(_F, _L, _V, _P, _Dist_type(_F))); }
  859. template<class _FI, class _Ty, class _Pd, class _Pr> inline
  860. _FI _Lower_bound(_FI _F, _FI _L, const _Ty& _V, _Pr _P, _Pd *)
  861. {_Pd _N = 0;
  862. _Distance(_F, _L, _N);
  863. for (; 0 < _N; )
  864. {_Pd _N2 = _N / 2;
  865. _FI _M = _F;
  866. advance(_M, _N2);
  867. if (_P(*_M, _V))
  868. _F = ++_M, _N -= _N2 + 1;
  869. else
  870. _N = _N2; }
  871. return (_F); }
  872. // TEMPLATE FUNCTION upper_bound
  873. template<class _FI, class _Ty> inline
  874. _FI upper_bound(_FI _F, _FI _L, const _Ty& _V)
  875. {return (_Upper_bound(_F, _L, _V, _Dist_type(_F))); }
  876. template<class _FI, class _Ty, class _Pd> inline
  877. _FI _Upper_bound(_FI _F, _FI _L, const _Ty& _V, _Pd *)
  878. {_Pd _N = 0;
  879. _Distance(_F, _L, _N);
  880. for (; 0 < _N; )
  881. {_Pd _N2 = _N / 2;
  882. _FI _M = _F;
  883. advance(_M, _N2);
  884. if (!(_V < *_M))
  885. _F = ++_M, _N -= _N2 + 1;
  886. else
  887. _N = _N2; }
  888. return (_F); }
  889. // TEMPLATE FUNCTION upper_bound WITH PRED
  890. template<class _FI, class _Ty, class _Pr> inline
  891. _FI upper_bound(_FI _F, _FI _L, const _Ty& _V, _Pr _P)
  892. {return (_Upper_bound(_F, _L, _V, _P, _Dist_type(_F))); }
  893. template<class _FI, class _Ty, class _Pd, class _Pr> inline
  894. _FI _Upper_bound(_FI _F, _FI _L, const _Ty& _V, _Pr _P, _Pd *)
  895. {_Pd _N = 0;
  896. _Distance(_F, _L, _N);
  897. for (; 0 < _N; )
  898. {_Pd _N2 = _N / 2;
  899. _FI _M = _F;
  900. advance(_M, _N2);
  901. if (!_P(_V, *_M))
  902. _F = ++_M, _N -= _N2 + 1;
  903. else
  904. _N = _N2; }
  905. return (_F); }
  906. // TEMPLATE FUNCTION equal_range
  907. template<class _FI, class _Ty> inline
  908. pair<_FI, _FI> equal_range(_FI _F, _FI _L, const _Ty& _V)
  909. {return (_Equal_range(_F, _L, _V, _Dist_type(_F))); }
  910. template<class _FI, class _Ty, class _Pd> inline
  911. pair<_FI, _FI> _Equal_range(_FI _F, _FI _L,
  912. const _Ty& _V, _Pd *)
  913. {_Pd _N = 0;
  914. _Distance(_F, _L, _N);
  915. for (; 0 < _N; )
  916. {_Pd _N2 = _N / 2;
  917. _FI _M = _F;
  918. advance(_M, _N2);
  919. if (*_M < _V)
  920. _F = ++_M, _N -= _N2 + 1;
  921. else if (_V < *_M)
  922. _N = _N2;
  923. else
  924. {_FI _F2 = lower_bound(_F, _M, _V);
  925. advance(_F, _N);
  926. _FI _L2 = upper_bound(++_M, _F, _V);
  927. return (pair<_FI, _FI>(_F2, _L2)); }}
  928. return (pair<_FI, _FI>(_F, _F)); }
  929. // TEMPLATE FUNCTION equal_range WITH PRED
  930. template<class _FI, class _Ty, class _Pr> inline
  931. pair<_FI, _FI> equal_range(_FI _F, _FI _L, const _Ty& _V,
  932. _Pr _P)
  933. {return (_Equal_range(_F, _L, _V, _P, _Dist_type(_F))); }
  934. template<class _FI, class _Ty, class _Pd, class _Pr> inline
  935. pair<_FI, _FI> _Equal_range(_FI _F, _FI _L, const _Ty& _V,
  936. _Pr _P, _Pd *)
  937. {_Pd _N = 0;
  938. _Distance(_F, _L, _N);
  939. for (; 0 < _N; )
  940. {_Pd _N2 = _N / 2;
  941. _FI _M = _F;
  942. advance(_M, _N2);
  943. if (_P(*_M, _V))
  944. _F = ++_M, _N -= _N2 + 1;
  945. else if (_P(_V, *_M))
  946. _N = _N2;
  947. else
  948. {_FI _F2 = lower_bound(_F, _M, _V, _P);
  949. advance(_F, _N);
  950. _FI _L2 = upper_bound(++_M, _F, _V, _P);
  951. return (pair<_FI, _FI>(_F2, _L2)); }}
  952. return (pair<_FI, _FI>(_F, _F)); }
  953. // TEMPLATE FUNCTION binary_search
  954. template<class _FI, class _Ty> inline
  955. bool binary_search(_FI _F, _FI _L, const _Ty& _V)
  956. {_FI _I = lower_bound(_F, _L, _V);
  957. return (_I != _L && !(_V < *_I)); }
  958. // TEMPLATE FUNCTION binary_search WITH PRED
  959. template<class _FI, class _Ty, class _Pr> inline
  960. bool binary_search(_FI _F, _FI _L, const _Ty& _V, _Pr _P)
  961. {_FI _I = lower_bound(_F, _L, _V, _P);
  962. return (_I != _L && !_P(_V, *_I)); }
  963. // TEMPLATE FUNCTION merge
  964. template<class _II1, class _II2, class _OI> inline
  965. _OI merge(_II1 _F1, _II1 _L1, _II2 _F2, _II2 _L2, _OI _X)
  966. {for (; _F1 != _L1 && _F2 != _L2; ++_X)
  967. if (*_F2 < *_F1)
  968. *_X = *_F2++;
  969. else
  970. *_X = *_F1++;
  971. return (copy(_F2, _L2, copy(_F1, _L1, _X))); }
  972. // TEMPLATE FUNCTION merge WITH PRED
  973. template<class _II1, class _II2, class _OI, class _Pr> inline
  974. _OI merge(_II1 _F1, _II1 _L1, _II2 _F2, _II2 _L2, _OI _X,
  975. _Pr _P)
  976. {for (; _F1 != _L1 && _F2 != _L2; ++_X)
  977. if (_P(*_F2, *_F1))
  978. *_X = *_F2++;
  979. else
  980. *_X = *_F1++;
  981. return (copy(_F2, _L2, copy(_F1, _L1, _X))); }
  982. // TEMPLATE FUNCTION inplace_merge
  983. template<class _BI> inline
  984. void inplace_merge(_BI _F, _BI _M, _BI _L)
  985. {if (_F != _L)
  986. _Inplace_merge(_F, _M, _L,
  987. _Dist_type(_F), _Val_type(_F)); }
  988. template<class _BI, class _Pd, class _Ty> inline
  989. void _Inplace_merge(_BI _F, _BI _M, _BI _L, _Pd *, _Ty *)
  990. {_Pd _D1 = 0;
  991. _Distance(_F, _M, _D1);
  992. _Pd _D2 = 0;
  993. _Distance(_M, _L, _D2);
  994. _Temp_iterator<_Ty> _Xb(_D1 < _D2 ? _D1 : _D2);
  995. _Buffered_merge(_F, _M, _L, _D1, _D2, _Xb); }
  996. template<class _BI, class _Pd, class _Ty> inline
  997. void _Buffered_merge(_BI _F, _BI _M, _BI _L,
  998. _Pd _D1, _Pd _D2, _Temp_iterator<_Ty>& _Xb)
  999. {if (_D1 == 0 || _D2 == 0)
  1000. ;
  1001. else if (_D1 + _D2 == 2)
  1002. {if (*_M < *_F)
  1003. iter_swap(_F, _M); }
  1004. else if (_D1 <= _D2 && _D1 <= _Xb._Maxlen())
  1005. {copy(_F, _M, _Xb._Init());
  1006. merge(_Xb._First(), _Xb._Last(), _M, _L, _F); }
  1007. else if (_D2 <= _Xb._Maxlen())
  1008. {copy(_M, _L, _Xb._Init());
  1009. _Merge_backward(_F, _M, _Xb._First(), _Xb._Last(), _L); }
  1010. else
  1011. {_BI _Fn, _Ln;
  1012. _Pd _D1n = 0;
  1013. _Pd _D2n = 0;
  1014. if (_D2 < _D1)
  1015. {_D1n = _D1 / 2;
  1016. _Fn = _F;
  1017. advance(_Fn, _D1n);
  1018. _Ln = lower_bound(_M, _L, _Ty(*_Fn));
  1019. _Distance(_M, _Ln, _D2n); }
  1020. else
  1021. {_D2n = _D2 / 2;
  1022. _Ln = _M;
  1023. advance(_Ln, _D2n);
  1024. _Fn = upper_bound(_F, _M, _Ty(*_Ln));
  1025. _Distance(_F, _Fn, _D1n); }
  1026. _BI _Mn = _Buffered_rotate(_Fn, _M, _Ln,
  1027. _D1 - _D1n, _D2n, _Xb);
  1028. _Buffered_merge(_F, _Fn, _Mn, _D1n, _D2n, _Xb);
  1029. _Buffered_merge(_Mn, _Ln, _L,
  1030. _D1 - _D1n, _D2 - _D2n, _Xb); }}
  1031. template<class _BI1, class _BI2, class _BI3> inline
  1032. _BI3 _Merge_backward(_BI1 _F1, _BI1 _L1, _BI2 _F2, _BI2 _L2,
  1033. _BI3 _X)
  1034. {for (; ; )
  1035. if (_F1 == _L1)
  1036. return (copy_backward(_F2, _L2, _X));
  1037. else if (_F2 == _L2)
  1038. return (copy_backward(_F1, _L1, _X));
  1039. else if (*--_L2 < *--_L1)
  1040. *--_X = *_L1, ++_L2;
  1041. else
  1042. *--_X = *_L2, ++_L1; }
  1043. template<class _BI, class _Pd, class _Ty> inline
  1044. _BI _Buffered_rotate(_BI _F, _BI _M, _BI _L,
  1045. _Pd _D1, _Pd _D2, _Temp_iterator<_Ty>& _Xb)
  1046. {if (_D1 <= _D2 && _D1 <= _Xb._Maxlen())
  1047. {copy(_F, _M, _Xb._Init());
  1048. copy(_M, _L, _F);
  1049. return (copy_backward(_Xb._First(), _Xb._Last(), _L)); }
  1050. else if (_D2 <= _Xb._Maxlen())
  1051. {copy(_M, _L, _Xb._Init());
  1052. copy_backward(_F, _M, _L);
  1053. return (copy(_Xb._First(), _Xb._Last(), _F)); }
  1054. else
  1055. {rotate(_F, _M, _L);
  1056. advance(_F, _D2);
  1057. return (_F); }}
  1058. // TEMPLATE FUNCTION inplace_merge WITH PRED
  1059. template<class _BI, class _Pr> inline
  1060. void inplace_merge(_BI _F, _BI _M, _BI _L, _Pr _P)
  1061. {if (_F != _L)
  1062. _Inplace_merge(_F, _M, _L, _P,
  1063. _Dist_type(_F), _Val_type(_F)); }
  1064. template<class _BI, class _Pd, class _Ty, class _Pr> inline
  1065. void _Inplace_merge(_BI _F, _BI _M, _BI _L, _Pr _P,
  1066. _Pd *, _Ty *)
  1067. {_Pd _D1 = 0;
  1068. _Distance(_F, _M, _D1);
  1069. _Pd _D2 = 0;
  1070. _Distance(_M, _L, _D2);
  1071. _Temp_iterator<_Ty> _Xb(_D1 < _D2 ? _D1 : _D2);
  1072. _Buffered_merge(_F, _M, _L, _D1, _D2, _Xb, _P); }
  1073. template<class _BI, class _Pd, class _Ty, class _Pr> inline
  1074. void _Buffered_merge(_BI _F, _BI _M, _BI _L,
  1075. _Pd _D1, _Pd _D2, _Temp_iterator<_Ty>& _Xb, _Pr _P)
  1076. {if (_D1 == 0 || _D2 == 0)
  1077. ;
  1078. else if (_D1 + _D2 == 2)
  1079. {if (_P(*_M, *_F))
  1080. iter_swap(_F, _M); }
  1081. else if (_D1 <= _D2 && _D1 <= _Xb._Maxlen())
  1082. {copy(_F, _M, _Xb._Init());
  1083. merge(_Xb._First(), _Xb._Last(), _M, _L, _F, _P); }
  1084. else if (_D2 <= _Xb._Maxlen())
  1085. {copy(_M, _L, _Xb._Init());
  1086. _Merge_backward(_F, _M, _Xb._First(), _Xb._Last(),
  1087. _L, _P); }
  1088. else
  1089. {_BI _Fn, _Ln;
  1090. _Pd _D1n = 0;
  1091. _Pd _D2n = 0;
  1092. if (_D2 < _D1)
  1093. {_D1n = _D1 / 2;
  1094. _Fn = _F;
  1095. advance(_Fn, _D1n);
  1096. _Ln = lower_bound(_M, _L, _Ty(*_Fn), _P);
  1097. _Distance(_M, _Ln, _D2n); }
  1098. else
  1099. {_D2n = _D2 / 2;
  1100. _Ln = _M;
  1101. advance(_Ln, _D2n);
  1102. _Fn = upper_bound(_F, _M, _Ty(*_Ln), _P);
  1103. _Distance(_F, _Fn, _D1n); }
  1104. _BI _Mn = _Buffered_rotate(_Fn, _M, _Ln,
  1105. _D1 - _D1n, _D2n, _Xb);
  1106. _Buffered_merge(_F, _Fn, _Mn, _D1n, _D2n, _Xb, _P);
  1107. _Buffered_merge(_Mn, _Ln, _L,
  1108. _D1 - _D1n, _D2 - _D2n, _Xb, _P); }}
  1109. template<class _BI1, class _BI2, class _BI3, class _Pr> inline
  1110. _BI3 _Merge_backward(_BI1 _F1, _BI1 _L1, _BI2 _F2, _BI2 _L2,
  1111. _BI3 _X, _Pr _P)
  1112. {for (; ; )
  1113. if (_F1 == _L1)
  1114. return (copy_backward(_F2, _L2, _X));
  1115. else if (_F2 == _L2)
  1116. return (copy_backward(_F1, _L1, _X));
  1117. else if (_P(*--_L2, *--_L1))
  1118. *--_X = *_L1, ++_L2;
  1119. else
  1120. *--_X = *_L2, ++_L1; }
  1121. // TEMPLATE FUNCTION includes
  1122. template<class _II1, class _II2> inline
  1123. bool includes(_II1 _F1, _II1 _L1, _II2 _F2, _II2 _L2)
  1124. {for (; _F1 != _L1 && _F2 != _L2; )
  1125. if (*_F2 < *_F1)
  1126. return (false);
  1127. else if (*_F1 < *_F2)
  1128. ++_F1;
  1129. else
  1130. ++_F2;
  1131. return (_F2 == _L2); }
  1132. // TEMPLATE FUNCTION includes WITH PRED
  1133. template<class _II1, class _II2, class _Pr> inline
  1134. bool includes(_II1 _F1, _II1 _L1, _II2 _F2, _II2 _L2, _Pr _P)
  1135. {for (; _F1 != _L1 && _F2 != _L2; )
  1136. if (_P(*_F2, *_F1))
  1137. return (false);
  1138. else if (_P(*_F1, *_F2))
  1139. ++_F1;
  1140. else
  1141. ++_F2;
  1142. return (_F2 == _L2); }
  1143. // TEMPLATE FUNCTION set_union
  1144. template<class _II1, class _II2, class _OI> inline
  1145. _OI set_union(_II1 _F1, _II1 _L1, _II2 _F2, _II2 _L2, _OI _X)
  1146. {for (; _F1 != _L1 && _F2 != _L2; )
  1147. if (*_F1 < *_F2)
  1148. *_X++ = *_F1++;
  1149. else if (*_F2 < *_F1)
  1150. *_X++ = *_F2++;
  1151. else
  1152. *_X++ = *_F1++, ++_F2;
  1153. return (copy(_F2, _L2, copy(_F1, _L1, _X))); }
  1154. // TEMPLATE FUNCTION set_union WITH PRED
  1155. template<class _II1, class _II2, class _OI, class _Pr> inline
  1156. _OI set_union(_II1 _F1, _II1 _L1, _II2 _F2, _II2 _L2, _OI _X,
  1157. _Pr _P)
  1158. {for (; _F1 != _L1 && _F2 != _L2; )
  1159. if (_P(*_F1, *_F2))
  1160. *_X++ = *_F1++;
  1161. else if (_P(*_F2, *_F1))
  1162. *_X++ = *_F2++;
  1163. else
  1164. *_X++ = *_F1++, ++_F2;
  1165. return (copy(_F2, _L2, copy(_F1, _L1, _X))); }
  1166. // TEMPLATE FUNCTION set_intersection
  1167. template<class _II1, class _II2, class _OI> inline
  1168. _OI set_intersection(_II1 _F1, _II1 _L1, _II2 _F2, _II2 _L2,
  1169. _OI _X)
  1170. {for (; _F1 != _L1 && _F2 != _L2; )
  1171. if (*_F1 < *_F2)
  1172. ++_F1;
  1173. else if (*_F2 < *_F1)
  1174. ++_F2;
  1175. else
  1176. *_X++ = *_F1++, ++_F2;
  1177. return (_X); }
  1178. // TEMPLATE FUNCTION set_intersection WITH PRED
  1179. template<class _II1, class _II2, class _OI, class _Pr> inline
  1180. _OI set_intersection(_II1 _F1, _II1 _L1, _II2 _F2, _II2 _L2,
  1181. _OI _X, _Pr _P)
  1182. {for (; _F1 != _L1 && _F2 != _L2; )
  1183. if (_P(*_F1, *_F2))
  1184. ++_F1;
  1185. else if (_P(*_F2, *_F1))
  1186. ++_F2;
  1187. else
  1188. *_X++ = *_F1++, ++_F2;
  1189. return (_X); }
  1190. // TEMPLATE FUNCTION set_difference
  1191. template<class _II1, class _II2, class _OI> inline
  1192. _OI set_difference(_II1 _F1, _II1 _L1, _II2 _F2, _II2 _L2,
  1193. _OI _X)
  1194. {for (; _F1 != _L1 && _F2 != _L2; )
  1195. if (*_F1 < *_F2)
  1196. *_X++ = *_F1++;
  1197. else if (*_F2 < *_F1)
  1198. ++_F2;
  1199. else
  1200. ++_F1, ++_F2;
  1201. return (copy(_F1, _L1, _X)); }
  1202. // TEMPLATE FUNCTION set_difference WITH PRED
  1203. template<class _II1, class _II2, class _OI, class _Pr> inline
  1204. _OI set_difference(_II1 _F1, _II1 _L1, _II2 _F2, _II2 _L2,
  1205. _OI _X, _Pr _P)
  1206. {for (; _F1 != _L1 && _F2 != _L2; )
  1207. if (_P(*_F1, *_F2))
  1208. *_X++ = *_F1++;
  1209. else if (_P(*_F2, *_F1))
  1210. ++_F2;
  1211. else
  1212. ++_F1, ++_F2;
  1213. return (copy(_F1, _L1, _X)); }
  1214. // TEMPLATE FUNCTION set_symmetric_difference
  1215. template<class _II1, class _II2, class _OI> inline
  1216. _OI set_symmetric_difference(_II1 _F1, _II1 _L1, _II2 _F2,
  1217. _II2 _L2, _OI _X)
  1218. {for (; _F1 != _L1 && _F2 != _L2; )
  1219. if (*_F1 < *_F2)
  1220. *_X++ = *_F1++;
  1221. else if (*_F2 < *_F1)
  1222. *_X++ = *_F2++;
  1223. else
  1224. ++_F1, ++_F2;
  1225. return (copy(_F2, _L2, copy(_F1, _L1, _X))); }
  1226. // TEMPLATE FUNCTION set_symmetric_difference WITH PRED
  1227. template<class _II1, class _II2, class _OI, class _Pr> inline
  1228. _OI set_symmetric_difference(_II1 _F1, _II1 _L1, _II2 _F2,
  1229. _II2 _L2, _OI _X, _Pr _P)
  1230. {for (; _F1 != _L1 && _F2 != _L2; )
  1231. if (_P(*_F1, *_F2))
  1232. *_X++ = *_F1++;
  1233. else if (_P(*_F2, *_F1))
  1234. *_X++ = *_F2++;
  1235. else
  1236. ++_F1, ++_F2;
  1237. return (copy(_F2, _L2, copy(_F1, _L1, _X))); }
  1238. // TEMPLATE FUNCTION push_heap
  1239. template<class _RI> inline
  1240. void push_heap(_RI _F, _RI _L)
  1241. {_Push_heap_0(_F, _L, _Dist_type(_F), _Val_type(_F)); }
  1242. template<class _RI, class _Pd, class _Ty> inline
  1243. void _Push_heap_0(_RI _F, _RI _L, _Pd *, _Ty *)
  1244. {_Push_heap(_F, _Pd(_L - _F - 1), _Pd(0), _Ty(*(_L - 1))); }
  1245. template<class _RI, class _Pd, class _Ty> inline
  1246. void _Push_heap(_RI _F, _Pd _H, _Pd _J, _Ty _V)
  1247. {for (_Pd _I = (_H - 1) / 2; _J < _H && *(_F + _I) < _V;
  1248. _I = (_H - 1) / 2)
  1249. *(_F + _H) = *(_F + _I), _H = _I;
  1250. *(_F + _H) = _V; }
  1251. // TEMPLATE FUNCTION push_heap WITH PRED
  1252. template<class _RI, class _Pr> inline
  1253. void push_heap(_RI _F, _RI _L, _Pr _P)
  1254. {_Push_heap_0(_F, _L, _P,
  1255. _Dist_type(_F), _Val_type(_F)); }
  1256. template<class _RI, class _Pd, class _Ty, class _Pr> inline
  1257. void _Push_heap_0(_RI _F, _RI _L, _Pr _P, _Pd *, _Ty *)
  1258. {_Push_heap(_F, _Pd(_L - _F - 1), _Pd(0),
  1259. _Ty(*(_L - 1)), _P); }
  1260. template<class _RI, class _Pd, class _Ty, class _Pr> inline
  1261. void _Push_heap(_RI _F, _Pd _H, _Pd _J, _Ty _V, _Pr _P)
  1262. {for (_Pd _I = (_H - 1) / 2; _J < _H && _P(*(_F + _I), _V);
  1263. _I = (_H - 1) / 2)
  1264. *(_F + _H) = *(_F + _I), _H = _I;
  1265. *(_F + _H) = _V; }
  1266. // TEMPLATE FUNCTION pop_heap
  1267. template<class _RI> inline
  1268. void pop_heap(_RI _F, _RI _L)
  1269. {_Pop_heap_0(_F, _L, _Val_type(_F)); }
  1270. template<class _RI, class _Ty> inline
  1271. void _Pop_heap_0(_RI _F, _RI _L, _Ty *)
  1272. {_Pop_heap(_F, _L - 1, _L - 1, _Ty(*(_L - 1)),
  1273. _Dist_type(_F)); }
  1274. template<class _RI, class _Pd, class _Ty> inline
  1275. void _Pop_heap(_RI _F, _RI _L, _RI _X, _Ty _V, _Pd *)
  1276. {*_X = *_F;
  1277. _Adjust_heap(_F, _Pd(0), _Pd(_L - _F), _V); }
  1278. template<class _RI, class _Pd, class _Ty> inline
  1279. void _Adjust_heap(_RI _F, _Pd _H, _Pd _N, _Ty _V)
  1280. {_Pd _J = _H;
  1281. _Pd _K = 2 * _H + 2;
  1282. for (; _K < _N; _K = 2 * _K + 2)
  1283. {if (*(_F + _K) < *(_F + (_K - 1)))
  1284. --_K;
  1285. *(_F + _H) = *(_F + _K), _H = _K; }
  1286. if (_K == _N)
  1287. *(_F + _H) = *(_F + (_K - 1)), _H = _K - 1;
  1288. _Push_heap(_F, _H, _J, _V); }
  1289. // TEMPLATE FUNCTION pop_heap WITH PRED
  1290. template<class _RI, class _Pr> inline
  1291. void pop_heap(_RI _F, _RI _L, _Pr _P)
  1292. {_Pop_heap_0(_F, _L, _P, _Val_type(_F)); }
  1293. template<class _RI, class _Ty, class _Pr> inline
  1294. void _Pop_heap_0(_RI _F, _RI _L, _Pr _P, _Ty *)
  1295. {_Pop_heap(_F, _L - 1, _L - 1, _Ty(*(_L - 1)), _P,
  1296. _Dist_type(_F)); }
  1297. template<class _RI, class _Pd, class _Ty, class _Pr> inline
  1298. void _Pop_heap(_RI _F, _RI _L, _RI _X, _Ty _V, _Pr _P, _Pd *)
  1299. {*_X = *_F;
  1300. _Adjust_heap(_F, _Pd(0), _Pd(_L - _F), _V, _P); }
  1301. template<class _RI, class _Pd, class _Ty, class _Pr> inline
  1302. void _Adjust_heap(_RI _F, _Pd _H, _Pd _N, _Ty _V, _Pr _P)
  1303. {_Pd _J = _H;
  1304. _Pd _K = 2 * _H + 2;
  1305. for (; _K < _N; _K = 2 * _K + 2)
  1306. {if (_P(*(_F + _K), *(_F + (_K - 1))))
  1307. --_K;
  1308. *(_F + _H) = *(_F + _K), _H = _K; }
  1309. if (_K == _N)
  1310. *(_F + _H) = *(_F + (_K - 1)), _H = _K - 1;
  1311. _Push_heap(_F, _H, _J, _V, _P); }
  1312. // TEMPLATE FUNCTION make_heap
  1313. template<class _RI> inline
  1314. void make_heap(_RI _F, _RI _L)
  1315. {if (2 <= _L - _F)
  1316. _Make_heap(_F, _L, _Dist_type(_F), _Val_type(_F)); }
  1317. template<class _RI, class _Pd, class _Ty> inline
  1318. void _Make_heap(_RI _F, _RI _L, _Pd *, _Ty *)
  1319. {_Pd _N = _L - _F;
  1320. for (_Pd _H = _N / 2; 0 < _H; )
  1321. --_H, _Adjust_heap(_F, _H, _N, _Ty(*(_F + _H))); }
  1322. // TEMPLATE FUNCTION make_heap WITH PRED
  1323. template<class _RI, class _Pr> inline
  1324. void make_heap(_RI _F, _RI _L, _Pr _P)
  1325. {if (2 <= _L - _F)
  1326. _Make_heap(_F, _L, _P,
  1327. _Dist_type(_F), _Val_type(_F)); }
  1328. template<class _RI, class _Pd, class _Ty, class _Pr> inline
  1329. void _Make_heap(_RI _F, _RI _L, _Pr _P, _Pd *, _Ty *)
  1330. {_Pd _N = _L - _F;
  1331. for (_Pd _H = _N / 2; 0 < _H; )
  1332. --_H, _Adjust_heap(_F, _H, _N, _Ty(*(_F + _H)), _P); }
  1333. // TEMPLATE FUNCTION sort_heap
  1334. template<class _RI> inline
  1335. void sort_heap(_RI _F, _RI _L)
  1336. {for (; 1 < _L - _F; --_L)
  1337. pop_heap(_F, _L); }
  1338. // TEMPLATE FUNCTION sort_heap WITH PRED
  1339. template<class _RI, class _Pr> inline
  1340. void sort_heap(_RI _F, _RI _L, _Pr _P)
  1341. {for (; 1 < _L - _F; --_L)
  1342. pop_heap(_F, _L, _P); }
  1343. // TEMPLATE FUNCTION max_element
  1344. template<class _FI> inline
  1345. _FI max_element(_FI _F, _FI _L)
  1346. {_FI _X = _F;
  1347. if (_F != _L)
  1348. for (; ++_F != _L; )
  1349. if (*_X < *_F)
  1350. _X = _F;
  1351. return (_X); }
  1352. // TEMPLATE FUNCTION max_element WITH PRED
  1353. template<class _FI, class _Pr> inline
  1354. _FI max_element(_FI _F, _FI _L, _Pr _P)
  1355. {_FI _X = _F;
  1356. if (_F != _L)
  1357. for (; ++_F != _L; )
  1358. if (_P(*_X, *_F))
  1359. _X = _F;
  1360. return (_X); }
  1361. // TEMPLATE FUNCTION min_element
  1362. template<class _FI> inline
  1363. _FI min_element(_FI _F, _FI _L)
  1364. {_FI _X = _F;
  1365. if (_F != _L)
  1366. for (; ++_F != _L; )
  1367. if (*_F < *_X)
  1368. _X = _F;
  1369. return (_X); }
  1370. // TEMPLATE FUNCTION min_element WITH PRED
  1371. template<class _FI, class _Pr> inline
  1372. _FI min_element(_FI _F, _FI _L, _Pr _P)
  1373. {_FI _X = _F;
  1374. if (_F != _L)
  1375. for (; ++_F != _L; )
  1376. if (_P(*_F, *_X))
  1377. _X = _F;
  1378. return (_X); }
  1379. // TEMPLATE FUNCTION next_permutation
  1380. template<class _BI> inline
  1381. bool next_permutation(_BI _F, _BI _L)
  1382. {_BI _I = _L;
  1383. if (_F == _L || _F == --_I)
  1384. return (false);
  1385. for (; ; )
  1386. {_BI _Ip = _I;
  1387. if (*--_I < *_Ip)
  1388. {_BI _J = _L;
  1389. for (; !(*_I < *--_J); )
  1390. ;
  1391. iter_swap(_I, _J);
  1392. reverse(_Ip, _L);
  1393. return (true); }
  1394. if (_I == _F)
  1395. {reverse(_F, _L);
  1396. return (false); }}}
  1397. // TEMPLATE FUNCTION next_permutation WITH PRED
  1398. template<class _BI, class _Pr> inline
  1399. bool next_permutation(_BI _F, _BI _L, _Pr _P)
  1400. {_BI _I = _L;
  1401. if (_F == _L || _F == --_I)
  1402. return (false);
  1403. for (; ; )
  1404. {_BI _Ip = _I;
  1405. if (_P(*--_I, *_Ip))
  1406. {_BI _J = _L;
  1407. for (; !_P(*_I, *--_J); )
  1408. ;
  1409. iter_swap(_I, _J);
  1410. reverse(_Ip, _L);
  1411. return (true); }
  1412. if (_I == _F)
  1413. {reverse(_F, _L);
  1414. return (false); }}}
  1415. // TEMPLATE FUNCTION prev_permutation
  1416. template<class _BI> inline
  1417. bool prev_permutation(_BI _F, _BI _L)
  1418. {_BI _I = _L;
  1419. if (_F == _L || _F == --_I)
  1420. return (false);
  1421. for (; ; )
  1422. {_BI _Ip = _I;
  1423. if (!(*--_I < *_Ip))
  1424. {_BI _J = _L;
  1425. for (; *_I < *--_J; )
  1426. ;
  1427. iter_swap(_I, _J);
  1428. reverse(_Ip, _L);
  1429. return (true); }
  1430. if (_I == _F)
  1431. {reverse(_F, _L);
  1432. return (false); }}}
  1433. // TEMPLATE FUNCTION prev_permutation WITH PRED
  1434. template<class _BI, class _Pr> inline
  1435. bool prev_permutation(_BI _F, _BI _L, _Pr _P)
  1436. {_BI _I = _L;
  1437. if (_F == _L || _F == --_I)
  1438. return (false);
  1439. for (; ; )
  1440. {_BI _Ip = _I;
  1441. if (!_P(*--_I, *_Ip))
  1442. {_BI _J = _L;
  1443. for (; _P(*_I, *--_J); )
  1444. ;
  1445. iter_swap(_I, _J);
  1446. reverse(_Ip, _L);
  1447. return (true); }
  1448. if (_I == _F)
  1449. {reverse(_F, _L);
  1450. return (false); }}}
  1451. _STD_END
  1452. #ifdef _MSC_VER
  1453. #pragma pack(pop)
  1454. #endif /* _MSC_VER */
  1455. #endif /* _ALGORITHM_ */
  1456. /*
  1457. * Copyright (c) 1995 by P.J. Plauger. ALL RIGHTS RESERVED.
  1458. * Consult your license regarding permissions and restrictions.
  1459. */
  1460. /*
  1461. * This file is derived from software bearing the following
  1462. * restrictions:
  1463. *
  1464. * Copyright (c) 1994
  1465. * Hewlett-Packard Company
  1466. *
  1467. * Permission to use, copy, modify, distribute and sell this
  1468. * software and its documentation for any purpose is hereby
  1469. * granted without fee, provided that the above copyright notice
  1470. * appear in all copies and that both that copyright notice and
  1471. * this permission notice appear in supporting documentation.
  1472. * Hewlett-Packard Company makes no representations about the
  1473. * suitability of this software for any purpose. It is provided
  1474. * "as is" without express or implied warranty.
  1475. */