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.

1535 lines
40 KiB

  1. // xutility internal header
  2. #pragma once
  3. #ifndef _XUTILITY_
  4. #define _XUTILITY_
  5. #include <climits>
  6. #include <utility>
  7. #pragma pack(push,8)
  8. #pragma warning(push,3)
  9. #pragma warning(disable:4786)
  10. _STD_BEGIN
  11. // ITERATOR STUFF (from <iterator>)
  12. // ITERATOR TAGS
  13. struct input_iterator_tag
  14. { // identifying tag for input iterators
  15. };
  16. struct output_iterator_tag
  17. { // identifying tag for output iterators
  18. };
  19. struct forward_iterator_tag
  20. : public input_iterator_tag
  21. { // identifying tag for forward iterators
  22. };
  23. struct bidirectional_iterator_tag
  24. : public forward_iterator_tag
  25. { // identifying tag for bidirectional iterators
  26. };
  27. struct random_access_iterator_tag
  28. : public bidirectional_iterator_tag
  29. { // identifying tag for random-access iterators
  30. };
  31. struct _Int_iterator_tag
  32. { // identifying tag for integer types, not an iterator
  33. };
  34. // POINTER ITERATOR TAGS
  35. struct _Nonscalar_ptr_iterator_tag
  36. { // pointer to unknown type
  37. };
  38. struct _Scalar_ptr_iterator_tag
  39. { // pointer to scalar type
  40. };
  41. // TEMPLATE CLASS iterator
  42. template<class _Category,
  43. class _Ty,
  44. class _Diff = ptrdiff_t,
  45. class _Pointer = _Ty *,
  46. class _Reference = _Ty&>
  47. struct iterator
  48. { // base type for all iterator classes
  49. typedef _Category iterator_category;
  50. typedef _Ty value_type;
  51. typedef _Diff difference_type;
  52. typedef _Diff distance_type; // retained
  53. typedef _Pointer pointer;
  54. typedef _Reference reference;
  55. };
  56. template<class _Ty,
  57. class _Diff,
  58. class _Pointer,
  59. class _Reference>
  60. struct _Bidit
  61. : public iterator<bidirectional_iterator_tag, _Ty, _Diff,
  62. _Pointer, _Reference>
  63. { // base for bidirectional iterators
  64. };
  65. template<class _Ty,
  66. class _Diff,
  67. class _Pointer,
  68. class _Reference>
  69. struct _Ranit
  70. : public iterator<random_access_iterator_tag, _Ty, _Diff,
  71. _Pointer, _Reference>
  72. { // base for random-access iterators
  73. };
  74. struct _Outit
  75. : public iterator<output_iterator_tag, void, void,
  76. void, void>
  77. { // base for output iterators
  78. };
  79. // TEMPLATE CLASS iterator_traits
  80. template<class _Iter>
  81. struct iterator_traits
  82. { // get traits from iterator _Iter
  83. typedef typename _Iter::iterator_category iterator_category;
  84. typedef typename _Iter::value_type value_type;
  85. typedef typename _Iter::difference_type difference_type;
  86. typedef difference_type distance_type; // retained
  87. typedef typename _Iter::pointer pointer;
  88. typedef typename _Iter::reference reference;
  89. };
  90. #if _HAS_PARTIAL_SPECIALIZATION
  91. template<class _Ty>
  92. struct iterator_traits<_Ty *>
  93. { // get traits from pointer
  94. typedef random_access_iterator_tag iterator_category;
  95. typedef _Ty value_type;
  96. typedef ptrdiff_t difference_type;
  97. typedef ptrdiff_t distance_type; // retained
  98. typedef _Ty *pointer;
  99. typedef _Ty& reference;
  100. };
  101. template<class _Ty>
  102. struct iterator_traits<const _Ty *>
  103. { // get traits from const pointer
  104. typedef random_access_iterator_tag iterator_category;
  105. typedef _Ty value_type;
  106. typedef ptrdiff_t difference_type;
  107. typedef ptrdiff_t distance_type; // retained
  108. typedef const _Ty *pointer;
  109. typedef const _Ty& reference;
  110. };
  111. template<> struct iterator_traits<_Bool>
  112. { // get traits from integer type
  113. typedef _Int_iterator_tag iterator_category;
  114. };
  115. template<> struct iterator_traits<char>
  116. { // get traits from integer type
  117. typedef _Int_iterator_tag iterator_category;
  118. };
  119. template<> struct iterator_traits<signed char>
  120. { // get traits from integer type
  121. typedef _Int_iterator_tag iterator_category;
  122. };
  123. template<> struct iterator_traits<unsigned char>
  124. { // get traits from integer type
  125. typedef _Int_iterator_tag iterator_category;
  126. };
  127. #ifdef _NATIVE_WCHAR_T_DEFINED
  128. template<> struct iterator_traits<wchar_t>
  129. { // get traits from integer type
  130. typedef _Int_iterator_tag iterator_category;
  131. };
  132. #else
  133. #endif
  134. template<> struct iterator_traits<short>
  135. { // get traits from integer type
  136. typedef _Int_iterator_tag iterator_category;
  137. };
  138. template<> struct iterator_traits<unsigned short>
  139. { // get traits from integer type
  140. typedef _Int_iterator_tag iterator_category;
  141. };
  142. template<> struct iterator_traits<int>
  143. { // get traits from integer type
  144. typedef _Int_iterator_tag iterator_category;
  145. };
  146. template<> struct iterator_traits<unsigned int>
  147. { // get traits from integer type
  148. typedef _Int_iterator_tag iterator_category;
  149. };
  150. template<> struct iterator_traits<long>
  151. { // get traits from integer type
  152. typedef _Int_iterator_tag iterator_category;
  153. };
  154. template<> struct iterator_traits<unsigned long>
  155. { // get traits from integer type
  156. typedef _Int_iterator_tag iterator_category;
  157. };
  158. #ifdef _LONGLONG
  159. template<> struct iterator_traits<_LONGLONG>
  160. { // get traits from integer type
  161. typedef _Int_iterator_tag iterator_category;
  162. };
  163. template<> struct iterator_traits<_ULONGLONG>
  164. { // get traits from integer type
  165. typedef _Int_iterator_tag iterator_category;
  166. };
  167. #endif
  168. // TEMPLATE FUNCTION _Iter_cat
  169. template<class _Iter> inline
  170. typename iterator_traits<_Iter>::iterator_category
  171. _Iter_cat(const _Iter&)
  172. { // return category from iterator argument
  173. typename iterator_traits<_Iter>::iterator_category _Cat;
  174. return (_Cat);
  175. }
  176. #else /* _HAS_PARTIAL_SPECIALIZATION */
  177. // TEMPLATE FUNCTION _Iter_cat
  178. template<class _Category,
  179. class _Ty,
  180. class _Diff,
  181. class _Pointer,
  182. class _Reference> inline
  183. _Category _Iter_cat(const iterator<_Category, _Ty, _Diff,
  184. _Pointer, _Reference>&)
  185. { // return category from iterator argument
  186. _Category _Cat;
  187. return (_Cat);
  188. }
  189. template<class _Ty> inline
  190. random_access_iterator_tag _Iter_cat(const _Ty *)
  191. { // return category from pointer argument
  192. random_access_iterator_tag _Cat;
  193. return (_Cat);
  194. }
  195. // INTEGER FUNCTION _Iter_cat
  196. inline _Int_iterator_tag _Iter_cat(_Bool)
  197. { // return category from bool argument
  198. _Int_iterator_tag _Cat;
  199. return (_Cat);
  200. }
  201. inline _Int_iterator_tag _Iter_cat(char)
  202. { // return category from char argument
  203. _Int_iterator_tag _Cat;
  204. return (_Cat);
  205. }
  206. inline _Int_iterator_tag _Iter_cat(signed char)
  207. { // return category from signed char argument
  208. _Int_iterator_tag _Cat;
  209. return (_Cat);
  210. }
  211. inline _Int_iterator_tag _Iter_cat(unsigned char)
  212. { // return category from unsigned char argument
  213. _Int_iterator_tag _Cat;
  214. return (_Cat);
  215. }
  216. #ifdef _NATIVE_WCHAR_T_DEFINED
  217. inline _Int_iterator_tag _Iter_cat(wchar_t)
  218. { // return category from wchar_t argument
  219. _Int_iterator_tag _Cat;
  220. return (_Cat);
  221. }
  222. #else
  223. #endif
  224. inline _Int_iterator_tag _Iter_cat(short)
  225. { // return category from short argument
  226. _Int_iterator_tag _Cat;
  227. return (_Cat);
  228. }
  229. inline _Int_iterator_tag _Iter_cat(unsigned short)
  230. { // return category from unsigned short argument
  231. _Int_iterator_tag _Cat;
  232. return (_Cat);
  233. }
  234. inline _Int_iterator_tag _Iter_cat(int)
  235. { // return category from int argument
  236. _Int_iterator_tag _Cat;
  237. return (_Cat);
  238. }
  239. inline _Int_iterator_tag _Iter_cat(unsigned int)
  240. { // return category from unsigned int argument
  241. _Int_iterator_tag _Cat;
  242. return (_Cat);
  243. }
  244. inline _Int_iterator_tag _Iter_cat(long)
  245. { // return category from long argument
  246. _Int_iterator_tag _Cat;
  247. return (_Cat);
  248. }
  249. inline _Int_iterator_tag _Iter_cat(unsigned long)
  250. { // return category from unsigned long argument
  251. _Int_iterator_tag _Cat;
  252. return (_Cat);
  253. }
  254. #ifdef _LONGLONG
  255. inline _Int_iterator_tag _Iter_cat(_LONGLONG)
  256. { // return category from long long argument
  257. _Int_iterator_tag _Cat;
  258. return (_Cat);
  259. }
  260. inline _Int_iterator_tag _Iter_cat(_ULONGLONG)
  261. { // return category from ulong long argument
  262. _Int_iterator_tag _Cat;
  263. return (_Cat);
  264. }
  265. #endif /* _LONGLONG */
  266. #endif /* _HAS_PARTIAL_SPECIALIZATION */
  267. // TEMPLATE FUNCTION _Ptr_cat
  268. template<class _T1,
  269. class _T2> inline
  270. _Nonscalar_ptr_iterator_tag _Ptr_cat(const _T1&, _T2&)
  271. { // return pointer category from arbitrary arguments
  272. _Nonscalar_ptr_iterator_tag _Cat;
  273. return (_Cat);
  274. }
  275. #if _HAS_TEMPLATE_PARTIAL_ORDERING
  276. template<class _Ty> inline
  277. _Scalar_ptr_iterator_tag _Ptr_cat(const _Ty **, const _Ty **)
  278. { // return pointer category from pointer to pointer arguments
  279. _Scalar_ptr_iterator_tag _Cat;
  280. return (_Cat);
  281. }
  282. template<class _Ty> inline
  283. _Scalar_ptr_iterator_tag _Ptr_cat(const _Ty *const *, const _Ty **)
  284. { // return pointer category from pointer to pointer arguments
  285. _Scalar_ptr_iterator_tag _Cat;
  286. return (_Cat);
  287. }
  288. #endif /* _HAS_TEMPLATE_PARTIAL_ORDERING */
  289. // INTEGER FUNCTION _Ptr_cat
  290. inline _Scalar_ptr_iterator_tag _Ptr_cat(const _Bool *, _Bool *)
  291. { // return pointer category from pointer to bool arguments
  292. _Scalar_ptr_iterator_tag _Cat;
  293. return (_Cat);
  294. }
  295. inline _Scalar_ptr_iterator_tag _Ptr_cat(const char *, char *)
  296. { // return pointer category from pointer to char arguments
  297. _Scalar_ptr_iterator_tag _Cat;
  298. return (_Cat);
  299. }
  300. inline _Scalar_ptr_iterator_tag _Ptr_cat(const signed char *, signed char *)
  301. { // return pointer category from pointer to signed char arguments
  302. _Scalar_ptr_iterator_tag _Cat;
  303. return (_Cat);
  304. }
  305. inline _Scalar_ptr_iterator_tag _Ptr_cat(const unsigned char *,
  306. unsigned char *)
  307. { // return pointer category from pointer to unsigned char arguments
  308. _Scalar_ptr_iterator_tag _Cat;
  309. return (_Cat);
  310. }
  311. #ifdef _NATIVE_WCHAR_T_DEFINED
  312. inline _Scalar_ptr_iterator_tag _Ptr_cat(const wchar_t *, wchar_t *)
  313. { // return pointer category from pointer to wchar_t arguments
  314. _Scalar_ptr_iterator_tag _Cat;
  315. return (_Cat);
  316. }
  317. #else
  318. #endif
  319. inline _Scalar_ptr_iterator_tag _Ptr_cat(const short *, short *)
  320. { // return pointer category from pointer to short arguments
  321. _Scalar_ptr_iterator_tag _Cat;
  322. return (_Cat);
  323. }
  324. inline _Scalar_ptr_iterator_tag _Ptr_cat(const unsigned short *,
  325. unsigned short *)
  326. { // return pointer category from pointer to unsigned short arguments
  327. _Scalar_ptr_iterator_tag _Cat;
  328. return (_Cat);
  329. }
  330. inline _Scalar_ptr_iterator_tag _Ptr_cat(const int *, int *)
  331. { // return pointer category from pointer to int arguments
  332. _Scalar_ptr_iterator_tag _Cat;
  333. return (_Cat);
  334. }
  335. inline _Scalar_ptr_iterator_tag _Ptr_cat(const unsigned int *, unsigned int *)
  336. { // return pointer category from pointer to unsigned int arguments
  337. _Scalar_ptr_iterator_tag _Cat;
  338. return (_Cat);
  339. }
  340. inline _Scalar_ptr_iterator_tag _Ptr_cat(const long *, long *)
  341. { // return pointer category from pointer to long arguments
  342. _Scalar_ptr_iterator_tag _Cat;
  343. return (_Cat);
  344. }
  345. inline _Scalar_ptr_iterator_tag _Ptr_cat(const unsigned long *,
  346. unsigned long *)
  347. { // return pointer category from pointer to unsigned long arguments
  348. _Scalar_ptr_iterator_tag _Cat;
  349. return (_Cat);
  350. }
  351. inline _Scalar_ptr_iterator_tag _Ptr_cat(const float *, float *)
  352. { // return pointer category from pointer to float arguments
  353. _Scalar_ptr_iterator_tag _Cat;
  354. return (_Cat);
  355. }
  356. inline _Scalar_ptr_iterator_tag _Ptr_cat(const double *, double *)
  357. { // return pointer category from pointer to double arguments
  358. _Scalar_ptr_iterator_tag _Cat;
  359. return (_Cat);
  360. }
  361. inline _Scalar_ptr_iterator_tag _Ptr_cat(const long double *, long double *)
  362. { // return pointer category from pointer to long double arguments
  363. _Scalar_ptr_iterator_tag _Cat;
  364. return (_Cat);
  365. }
  366. #ifdef _LONGLONG
  367. inline _Scalar_ptr_iterator_tag _Ptr_cat(const _LONGLONG *, _LONGLONG *)
  368. { // return pointer category from pointer to long long arguments
  369. _Scalar_ptr_iterator_tag _Cat;
  370. return (_Cat);
  371. }
  372. inline _Scalar_ptr_iterator_tag _Ptr_cat(const _ULONGLONG *, _ULONGLONG *)
  373. { // return pointer category from pointer to ulong long arguments
  374. _Scalar_ptr_iterator_tag _Cat;
  375. return (_Cat);
  376. }
  377. #endif /* _LONGLONG */
  378. // TEMPLATE FUNCTIONS distance and _Distance
  379. template<class _InIt> inline
  380. ptrdiff_t distance(_InIt _First, _InIt _Last)
  381. { // return distance between iterators
  382. ptrdiff_t _Off = 0;
  383. _Distance2(_First, _Last, _Off, _Iter_cat(_First));
  384. return (_Off);
  385. }
  386. template<class _InIt,
  387. class _Diff> inline
  388. void _Distance(_InIt _First, _InIt _Last, _Diff& _Off)
  389. { // add to _Off distance between iterators
  390. _Distance2(_First, _Last, _Off, _Iter_cat(_First));
  391. }
  392. template<class _InIt,
  393. class _Diff> inline
  394. void _Distance2(_InIt _First, _InIt _Last, _Diff& _Off,
  395. input_iterator_tag)
  396. { // add to _Off distance between input iterators
  397. for (; _First != _Last; ++_First)
  398. ++_Off;
  399. }
  400. template<class _FwdIt,
  401. class _Diff> inline
  402. void _Distance2(_FwdIt _First, _FwdIt _Last, _Diff& _Off,
  403. forward_iterator_tag)
  404. { // add to _Off distance between forward iterators (redundant)
  405. for (; _First != _Last; ++_First)
  406. ++_Off;
  407. }
  408. template<class _BidIt,
  409. class _Diff> inline
  410. void _Distance2(_BidIt _First, _BidIt _Last, _Diff& _Off,
  411. bidirectional_iterator_tag)
  412. { // add to _Off distance between bidirectional iterators (redundant)
  413. for (; _First != _Last; ++_First)
  414. ++_Off;
  415. }
  416. template<class _RanIt,
  417. class _Diff> inline
  418. void _Distance2(_RanIt _First, _RanIt _Last, _Diff& _Off,
  419. random_access_iterator_tag)
  420. { // add to _Off distance between random-access iterators
  421. _Off += _Last - _First;
  422. }
  423. // TEMPLATE CLASS _Ptrit
  424. template<class _Ty,
  425. class _Diff,
  426. class _Pointer,
  427. class _Reference,
  428. class _Pointer2,
  429. class _Reference2>
  430. class _Ptrit
  431. : public _Ranit<_Ty, _Diff, _Pointer, _Reference>
  432. { // wrap pointer as random-access iterator
  433. public:
  434. typedef _Ptrit<_Ty, _Diff, _Pointer, _Reference,
  435. _Pointer2, _Reference2> _Myt;
  436. _Ptrit()
  437. { // construct with uninitialized wrapped pointer
  438. }
  439. _Ptrit(_Pointer _Ptr)
  440. : current(_Ptr)
  441. { // construct wrapped pointer from _Ptr
  442. }
  443. _Ptrit(const _Ptrit<_Ty, _Diff, _Pointer2, _Reference2,
  444. _Pointer2, _Reference2>& _Iter)
  445. : current(_Iter.base())
  446. { // const converter or copy constructor
  447. }
  448. _Pointer base() const
  449. { // return wrapped pointer
  450. return (current);
  451. }
  452. _Reference operator*() const
  453. { // return designated value
  454. return (*current);
  455. }
  456. _Pointer operator->() const
  457. { // return pointer to class object
  458. return (&**this);
  459. }
  460. _Myt& operator++()
  461. { // preincrement
  462. ++current;
  463. return (*this);
  464. }
  465. _Myt operator++(int)
  466. { // postincrement
  467. _Myt _Tmp = *this;
  468. ++current;
  469. return (_Tmp);
  470. }
  471. _Myt& operator--()
  472. { // predecrement
  473. --current;
  474. return (*this);
  475. }
  476. _Myt operator--(int)
  477. { // postdecrement
  478. _Myt _Tmp = *this;
  479. --current;
  480. return (_Tmp);
  481. }
  482. bool operator==(int _Right) const
  483. { // test if wrapped pointer == integer (null pointer constant)
  484. return (current == (_Pointer)_Right);
  485. }
  486. bool operator==(const _Myt& _Right) const
  487. { // test for iterator equality
  488. return (current == _Right.current);
  489. }
  490. bool operator!=(const _Myt& _Right) const
  491. { // test for iterator inequality
  492. return (!(*this == _Right));
  493. }
  494. _Myt& operator+=(_Diff _Off)
  495. { // increment by integer
  496. current += _Off;
  497. return (*this);
  498. }
  499. _Myt operator+(_Diff _Off) const
  500. { // return this + integer
  501. return (_Myt(current + _Off));
  502. }
  503. _Myt& operator-=(_Diff _Off)
  504. { // decrement by integer
  505. current -= _Off;
  506. return (*this);
  507. }
  508. _Myt operator-(_Diff _Off) const
  509. { // return this - integer
  510. return (_Myt(current - _Off));
  511. }
  512. _Reference operator[](_Diff _Off) const
  513. { // subscript
  514. return (*(*this + _Off));
  515. }
  516. bool operator<(const _Myt& _Right) const
  517. { // test if this < _Right
  518. return (current < _Right.current);
  519. }
  520. bool operator>(const _Myt& _Right) const
  521. { // test if this > _Right
  522. return (_Right < *this);
  523. }
  524. bool operator<=(const _Myt& _Right) const
  525. { // test if this <= _Right
  526. return (!(_Right < *this));
  527. }
  528. bool operator>=(const _Myt& _Right) const
  529. { // test if this >= _Right
  530. return (!(*this < _Right));
  531. }
  532. _Diff operator-(const _Myt& _Right) const
  533. { // return difference of iterators
  534. return (current - _Right.current);
  535. }
  536. protected:
  537. _Pointer current; // the wrapped pointer
  538. };
  539. // _Ptrit TEMPLATE FUNCTIONS
  540. template<class _Ty,
  541. class _Diff,
  542. class _Pointer,
  543. class _Reference,
  544. class _Pointer2,
  545. class _Reference2> inline
  546. _Ptrit<_Ty, _Diff, _Pointer, _Reference, _Pointer2, _Reference2>
  547. __cdecl operator+(_Diff _Off,
  548. const _Ptrit<_Ty, _Diff, _Pointer, _Reference,
  549. _Pointer2, _Reference2>& _Right)
  550. { // return iterator + integer
  551. return (_Right + _Off);
  552. }
  553. template<class _Ty,
  554. class _Diff,
  555. class _Pointer,
  556. class _Reference,
  557. class _Pointer2,
  558. class _Reference2> inline
  559. bool __cdecl operator==(
  560. const _Ptrit<_Ty, _Diff, _Pointer2, _Reference2,
  561. _Pointer2, _Reference2>& _Left,
  562. const _Ptrit<_Ty, _Diff, _Pointer, _Reference,
  563. _Pointer2, _Reference2>& _Right)
  564. { // test for _Ptrit<non-const *> == _Ptrit<const *>
  565. return (_Right == _Left);
  566. }
  567. template<class _Ty,
  568. class _Diff,
  569. class _Pointer,
  570. class _Reference,
  571. class _Pointer2,
  572. class _Reference2> inline
  573. bool __cdecl operator!=(
  574. const _Ptrit<_Ty, _Diff, _Pointer2, _Reference2,
  575. _Pointer2, _Reference2>& _Left,
  576. const _Ptrit<_Ty, _Diff, _Pointer, _Reference,
  577. _Pointer2, _Reference2>& _Right)
  578. { // test for _Ptrit<non-const *> != _Ptrit<const *>
  579. return (_Right != _Left);
  580. }
  581. template<class _Ty,
  582. class _Diff,
  583. class _Pointer,
  584. class _Reference,
  585. class _Pointer2,
  586. class _Reference2> inline
  587. bool __cdecl operator<(
  588. const _Ptrit<_Ty, _Diff, _Pointer2, _Reference2,
  589. _Pointer2, _Reference2>& _Left,
  590. const _Ptrit<_Ty, _Diff, _Pointer, _Reference,
  591. _Pointer2, _Reference2>& _Right)
  592. { // test for _Ptrit<non-const *> < _Ptrit<const *>
  593. return (_Right > _Left);
  594. }
  595. template<class _Ty,
  596. class _Diff,
  597. class _Pointer,
  598. class _Reference,
  599. class _Pointer2,
  600. class _Reference2> inline
  601. bool __cdecl operator>(
  602. const _Ptrit<_Ty, _Diff, _Pointer2, _Reference2,
  603. _Pointer2, _Reference2>& _Left,
  604. const _Ptrit<_Ty, _Diff, _Pointer, _Reference,
  605. _Pointer2, _Reference2>& _Right)
  606. { // test for _Ptrit<non-const *> > _Ptrit<const *>
  607. return (_Right < _Left);
  608. }
  609. template<class _Ty,
  610. class _Diff,
  611. class _Pointer,
  612. class _Reference,
  613. class _Pointer2,
  614. class _Reference2> inline
  615. bool __cdecl operator<=(
  616. const _Ptrit<_Ty, _Diff, _Pointer2, _Reference2,
  617. _Pointer2, _Reference2>& _Left,
  618. const _Ptrit<_Ty, _Diff, _Pointer, _Reference,
  619. _Pointer2, _Reference2>& _Right)
  620. { // test for _Ptrit<non-const *> <= _Ptrit<const *>
  621. return (_Right >= _Left);
  622. }
  623. template<class _Ty,
  624. class _Diff,
  625. class _Pointer,
  626. class _Reference,
  627. class _Pointer2,
  628. class _Reference2> inline
  629. bool __cdecl operator>=(
  630. const _Ptrit<_Ty, _Diff, _Pointer2, _Reference2,
  631. _Pointer2, _Reference2>& _Left,
  632. const _Ptrit<_Ty, _Diff, _Pointer, _Reference,
  633. _Pointer2, _Reference2>& _Right)
  634. { // test for _Ptrit<non-const *> >= _Ptrit<const *>
  635. return (_Right <= _Left);
  636. }
  637. // TEMPLATE CLASS reverse_iterator
  638. template<class _RanIt>
  639. class reverse_iterator
  640. : public iterator<
  641. typename iterator_traits<_RanIt>::iterator_category,
  642. typename iterator_traits<_RanIt>::value_type,
  643. typename iterator_traits<_RanIt>::difference_type,
  644. typename iterator_traits<_RanIt>::pointer,
  645. typename iterator_traits<_RanIt>::reference>
  646. { // wrap iterator to run it backwards
  647. public:
  648. typedef reverse_iterator<_RanIt> _Myt;
  649. typedef typename iterator_traits<_RanIt>::difference_type difference_type;
  650. typedef typename iterator_traits<_RanIt>::pointer pointer;
  651. typedef typename iterator_traits<_RanIt>::reference reference;
  652. typedef _RanIt iterator_type;
  653. reverse_iterator()
  654. { // construct with default wrapped iterator
  655. }
  656. explicit reverse_iterator(_RanIt _Right)
  657. : current(_Right)
  658. { // construct wrapped iterator from _Right
  659. }
  660. template<class _Other>
  661. reverse_iterator(const reverse_iterator<_Other>& _Right)
  662. : current(_Right.base())
  663. { // initialize with compatible base
  664. }
  665. _RanIt base() const
  666. { // return wrapped iterator
  667. return (current);
  668. }
  669. reference operator*() const
  670. { // return designated value
  671. _RanIt _Tmp = current;
  672. return (*--_Tmp);
  673. }
  674. pointer operator->() const
  675. { // return pointer to class object
  676. return (&**this);
  677. }
  678. _Myt& operator++()
  679. { // preincrement
  680. --current;
  681. return (*this);
  682. }
  683. _Myt operator++(int)
  684. { // postincrement
  685. _Myt _Tmp = *this;
  686. --current;
  687. return (_Tmp);
  688. }
  689. _Myt& operator--()
  690. { // predecrement
  691. ++current;
  692. return (*this);
  693. }
  694. _Myt operator--(int)
  695. { // postdecrement
  696. _Myt _Tmp = *this;
  697. ++current;
  698. return (_Tmp);
  699. }
  700. bool _Equal(const _Myt& _Right) const
  701. { // test for iterator equality
  702. return (current == _Right.current);
  703. }
  704. // N.B. functions valid for random-access iterators only beyond this point
  705. _Myt& operator+=(difference_type _Off)
  706. { // increment by integer
  707. current -= _Off;
  708. return (*this);
  709. }
  710. _Myt operator+(difference_type _Off) const
  711. { // return this + integer
  712. return (_Myt(current - _Off));
  713. }
  714. _Myt& operator-=(difference_type _Off)
  715. { // decrement by integer
  716. current += _Off;
  717. return (*this);
  718. }
  719. _Myt operator-(difference_type _Off) const
  720. { // return this - integer
  721. return (_Myt(current + _Off));
  722. }
  723. reference operator[](difference_type _Off) const
  724. { // subscript
  725. return (*(*this + _Off));
  726. }
  727. bool _Less(const _Myt& _Right) const
  728. { // test if this < _Right
  729. return (_Right.current < current);
  730. }
  731. difference_type _Minus(const _Myt& _Right) const
  732. { // return difference of iterators
  733. return (_Right.current - current);
  734. }
  735. protected:
  736. _RanIt current; // the wrapped iterator
  737. };
  738. // reverse_iterator TEMPLATE OPERATORS
  739. template<class _RanIt,
  740. class _Diff> inline
  741. reverse_iterator<_RanIt> __cdecl operator+(_Diff _Off,
  742. const reverse_iterator<_RanIt>& _Right)
  743. { // return reverse_iterator + integer
  744. return (_Right + _Off);
  745. }
  746. template<class _RanIt> inline
  747. ptrdiff_t __cdecl operator-(const reverse_iterator<_RanIt>& _Left,
  748. const reverse_iterator<_RanIt>& _Right)
  749. { // return difference of reverse_iterators
  750. return (_Left._Minus(_Right));
  751. }
  752. template<class _RanIt> inline
  753. bool __cdecl operator==(const reverse_iterator<_RanIt>& _Left,
  754. const reverse_iterator<_RanIt>& _Right)
  755. { // test for reverse_iterator equality
  756. return (_Left._Equal(_Right));
  757. }
  758. template<class _RanIt> inline
  759. bool __cdecl operator!=(const reverse_iterator<_RanIt>& _Left,
  760. const reverse_iterator<_RanIt>& _Right)
  761. { // test for reverse_iterator inequality
  762. return (!(_Left == _Right));
  763. }
  764. template<class _RanIt> inline
  765. bool __cdecl operator<(const reverse_iterator<_RanIt>& _Left,
  766. const reverse_iterator<_RanIt>& _Right)
  767. { // test for reverse_iterator < reverse_iterator
  768. return (_Left._Less(_Right));
  769. }
  770. template<class _RanIt> inline
  771. bool __cdecl operator>(const reverse_iterator<_RanIt>& _Left,
  772. const reverse_iterator<_RanIt>& _Right)
  773. { // test for reverse_iterator > reverse_iterator
  774. return (_Right < _Left);
  775. }
  776. template<class _RanIt> inline
  777. bool __cdecl operator<=(const reverse_iterator<_RanIt>& _Left,
  778. const reverse_iterator<_RanIt>& _Right)
  779. { // test for reverse_iterator <= reverse_iterator
  780. return (!(_Right < _Left));
  781. }
  782. template<class _RanIt> inline
  783. bool __cdecl operator>=(const reverse_iterator<_RanIt>& _Left,
  784. const reverse_iterator<_RanIt>& _Right)
  785. { // test for reverse_iterator >= reverse_iterator
  786. return (!(_Left < _Right));
  787. }
  788. // TEMPLATE CLASS reverse_bidirectional_iterator (retained)
  789. template<class _BidIt,
  790. class _Ty,
  791. class _Reference = _Ty&,
  792. class _Pointer = _Ty *,
  793. class _Diff = ptrdiff_t>
  794. class reverse_bidirectional_iterator
  795. : public _Bidit<_Ty, _Diff, _Pointer, _Reference>
  796. { // wrap bidirectional iterator to run it backwards
  797. public:
  798. typedef reverse_bidirectional_iterator<_BidIt, _Ty, _Reference,
  799. _Pointer, _Diff> _Myt;
  800. typedef _BidIt iterator_type;
  801. reverse_bidirectional_iterator()
  802. { // construct with default wrapped iterator
  803. }
  804. explicit reverse_bidirectional_iterator(_BidIt _Right)
  805. : current(_Right)
  806. { // construct wrapped iterator from _Right
  807. }
  808. _BidIt base() const
  809. { // return wrapped iterator
  810. return (current);
  811. }
  812. _Reference operator*() const
  813. { // return designated value
  814. _BidIt _Tmp = current;
  815. return (*--_Tmp);
  816. }
  817. _Pointer operator->() const
  818. { // return pointer to class object
  819. _Reference _Tmp = **this;
  820. return (&_Tmp);
  821. }
  822. _Myt& operator++()
  823. { // preincrement
  824. --current;
  825. return (*this);
  826. }
  827. _Myt operator++(int)
  828. { // postincrement
  829. _Myt _Tmp = *this;
  830. --current;
  831. return (_Tmp);
  832. }
  833. _Myt& operator--()
  834. { // predecrement
  835. ++current;
  836. return (*this);
  837. }
  838. _Myt operator--(int)
  839. { // postdecrement
  840. _Myt _Tmp = *this;
  841. ++current;
  842. return (_Tmp);
  843. }
  844. bool operator==(const _Myt& _Right) const
  845. { // test for iterator equality
  846. return (current == _Right.current);
  847. }
  848. bool operator!=(const _Myt& _Right) const
  849. { // test for iterator inequality
  850. return (!(*this == _Right));
  851. }
  852. protected:
  853. _BidIt current; // the wrapped iterator
  854. };
  855. // TEMPLATE CLASS _Revbidit
  856. template<class _BidIt,
  857. class _BidIt2 = _BidIt>
  858. class _Revbidit
  859. : public iterator<
  860. typename iterator_traits<_BidIt>::iterator_category,
  861. typename iterator_traits<_BidIt>::value_type,
  862. typename iterator_traits<_BidIt>::difference_type,
  863. typename iterator_traits<_BidIt>::pointer,
  864. typename iterator_traits<_BidIt>::reference>
  865. { // wrap bidirectional iterator to run it backwards
  866. public:
  867. typedef _Revbidit<_BidIt, _BidIt2> _Myt;
  868. typedef typename iterator_traits<_BidIt>::difference_type _Diff;
  869. typedef typename iterator_traits<_BidIt>::pointer _Pointer;
  870. typedef typename iterator_traits<_BidIt>::reference _Reference;
  871. typedef _BidIt iterator_type;
  872. _Revbidit()
  873. { // construct with default wrapped iterator
  874. }
  875. explicit _Revbidit(_BidIt _Right)
  876. : current(_Right)
  877. { // construct wrapped iterator from _Right
  878. }
  879. _Revbidit(const _Revbidit<_BidIt2>& _Other)
  880. : current (_Other.base())
  881. { // const converter or copy constructor
  882. }
  883. _BidIt base() const
  884. { // return wrapped iterator
  885. return (current);
  886. }
  887. _Reference operator*() const
  888. { // return designated value
  889. _BidIt _Tmp = current;
  890. return (*--_Tmp);
  891. }
  892. _Pointer operator->() const
  893. { // return pointer to class object
  894. _Reference _Tmp = **this;
  895. return (&_Tmp);
  896. }
  897. _Myt& operator++()
  898. { // preincrement
  899. --current;
  900. return (*this);
  901. }
  902. _Myt operator++(int)
  903. { // postincrement
  904. _Myt _Tmp = *this;
  905. --current;
  906. return (_Tmp);
  907. }
  908. _Myt& operator--()
  909. { // predecrement
  910. ++current;
  911. return (*this);
  912. }
  913. _Myt operator--(int)
  914. { // postdecrement
  915. _Myt _Tmp = *this;
  916. ++current;
  917. return (_Tmp);
  918. }
  919. bool operator==(const _Myt& _Right) const
  920. { // test for iterator equality
  921. return (current == _Right.current);
  922. }
  923. bool operator!=(const _Myt& _Right) const
  924. { // test for iterator inequality
  925. return (!(*this == _Right));
  926. }
  927. protected:
  928. _BidIt current;
  929. };
  930. // TEMPLATE CLASS istreambuf_iterator
  931. template<class _Elem,
  932. class _Traits>
  933. class istreambuf_iterator
  934. : public iterator<input_iterator_tag,
  935. _Elem, typename _Traits::off_type, _Elem *, _Elem&>
  936. { // wrap stream buffer as input iterator
  937. public:
  938. typedef istreambuf_iterator<_Elem, _Traits> _Myt;
  939. typedef _Elem char_type;
  940. typedef _Traits traits_type;
  941. typedef basic_streambuf<_Elem, _Traits> streambuf_type;
  942. typedef basic_istream<_Elem, _Traits> istream_type;
  943. typedef typename traits_type::int_type int_type;
  944. istreambuf_iterator(streambuf_type *_Sb = 0) _THROW0()
  945. : _Strbuf(_Sb), _Got(_Sb == 0)
  946. { // construct from stream buffer _Sb
  947. }
  948. istreambuf_iterator(istream_type& _Istr) _THROW0()
  949. : _Strbuf(_Istr.rdbuf()), _Got(_Istr.rdbuf() == 0)
  950. { // construct from stream buffer in istream _Istr
  951. }
  952. _Elem operator*() const
  953. { // return designated value
  954. if (!_Got)
  955. ((_Myt *)this)->_Peek();
  956. return (_Val);
  957. }
  958. _Myt& operator++()
  959. { // preincrement
  960. _Inc();
  961. return (*this);
  962. }
  963. _Myt operator++(int)
  964. { // postincrement
  965. if (!_Got)
  966. _Peek();
  967. _Myt _Tmp = *this;
  968. _Inc();
  969. return (_Tmp);
  970. }
  971. bool equal(const _Myt& _Right) const
  972. { // test for equality
  973. if (!_Got)
  974. ((_Myt *)this)->_Peek();
  975. if (!_Right._Got)
  976. ((_Myt *)&_Right)->_Peek();
  977. return (_Strbuf == 0 && _Right._Strbuf == 0
  978. || _Strbuf != 0 && _Right._Strbuf != 0);
  979. }
  980. private:
  981. void _Inc()
  982. { // skip to next input element
  983. if (_Strbuf == 0
  984. || traits_type::eq_int_type(traits_type::eof(),
  985. _Strbuf->sbumpc()))
  986. _Strbuf = 0, _Got = true;
  987. else
  988. _Got = false;
  989. }
  990. _Elem _Peek()
  991. { // peek at next input element
  992. int_type _Meta;
  993. if (_Strbuf == 0
  994. || traits_type::eq_int_type(traits_type::eof(),
  995. _Meta = _Strbuf->sgetc()))
  996. _Strbuf = 0;
  997. else
  998. _Val = traits_type::to_char_type(_Meta);
  999. _Got = true;
  1000. return (_Val);
  1001. }
  1002. streambuf_type *_Strbuf; // the wrapped stream buffer
  1003. bool _Got; // true if _Val is valid
  1004. _Elem _Val; // next element to deliver
  1005. };
  1006. // istreambuf_iterator TEMPLATE OPERATORS
  1007. template<class _Elem,
  1008. class _Traits> inline
  1009. bool __cdecl operator==(
  1010. const istreambuf_iterator<_Elem, _Traits>& _Left,
  1011. const istreambuf_iterator<_Elem, _Traits>& _Right)
  1012. { // test for istreambuf_iterator equality
  1013. return (_Left.equal(_Right));
  1014. }
  1015. template<class _Elem,
  1016. class _Traits> inline
  1017. bool __cdecl operator!=(
  1018. const istreambuf_iterator<_Elem, _Traits>& _Left,
  1019. const istreambuf_iterator<_Elem, _Traits>& _Right)
  1020. { // test for istreambuf_iterator inequality
  1021. return (!(_Left == _Right));
  1022. }
  1023. // TEMPLATE CLASS ostreambuf_iterator
  1024. template<class _Elem,
  1025. class _Traits>
  1026. class ostreambuf_iterator
  1027. : public _Outit
  1028. { // wrap stream buffer as output iterator
  1029. typedef ostreambuf_iterator<_Elem, _Traits> _Myt;
  1030. public:
  1031. typedef _Elem char_type;
  1032. typedef _Traits traits_type;
  1033. typedef basic_streambuf<_Elem, _Traits> streambuf_type;
  1034. typedef basic_ostream<_Elem, _Traits> ostream_type;
  1035. ostreambuf_iterator(streambuf_type *_Sb) _THROW0()
  1036. : _Failed(false), _Strbuf(_Sb)
  1037. { // construct from stream buffer _Sb
  1038. }
  1039. ostreambuf_iterator(ostream_type& _Ostr) _THROW0()
  1040. : _Failed(false), _Strbuf(_Ostr.rdbuf())
  1041. { // construct from stream buffer in _Ostr
  1042. }
  1043. _Myt& operator=(_Elem _Right)
  1044. { // store element and increment
  1045. if (_Strbuf == 0
  1046. || traits_type::eq_int_type(_Traits::eof(),
  1047. _Strbuf->sputc(_Right)))
  1048. _Failed = true;
  1049. return (*this);
  1050. }
  1051. _Myt& operator*()
  1052. { // pretend to get designated element
  1053. return (*this);
  1054. }
  1055. _Myt& operator++()
  1056. { // pretend to preincrement
  1057. return (*this);
  1058. }
  1059. _Myt& operator++(int)
  1060. { // pretend to postincrement
  1061. return (*this);
  1062. }
  1063. bool failed() const _THROW0()
  1064. { // return true if any stores failed
  1065. return (_Failed);
  1066. }
  1067. private:
  1068. bool _Failed; // true if any stores have failed
  1069. streambuf_type *_Strbuf; // the wrapped stream buffer
  1070. };
  1071. // ALGORITHM STUFF (from <algorithm>)
  1072. // TEMPLATE FUNCTION copy
  1073. template<class _InIt,
  1074. class _OutIt> inline
  1075. _OutIt copy(_InIt _First, _InIt _Last, _OutIt _Dest)
  1076. { // copy [_First, _Last) to [_Dest, ...)
  1077. return (_Copy_opt(_First, _Last, _Dest, _Ptr_cat(_First, _Dest)));
  1078. }
  1079. template<class _InIt,
  1080. class _OutIt> inline
  1081. _OutIt _Copy_opt(_InIt _First, _InIt _Last, _OutIt _Dest,
  1082. _Nonscalar_ptr_iterator_tag)
  1083. { // copy [_First, _Last) to [_Dest, ...), arbitrary iterators
  1084. for (; _First != _Last; ++_Dest, ++_First)
  1085. *_Dest = *_First;
  1086. return (_Dest);
  1087. }
  1088. template<class _InIt,
  1089. class _OutIt> inline
  1090. _OutIt _Copy_opt(_InIt _First, _InIt _Last, _OutIt _Dest,
  1091. _Scalar_ptr_iterator_tag)
  1092. { // copy [_First, _Last) to [_Dest, ...), pointers to scalars
  1093. ptrdiff_t _Off = _Last - _First; // NB: non-overlapping move
  1094. return ((_OutIt)::memmove(&*_Dest, &*_First,
  1095. _Off * sizeof (*_First)) + _Off);
  1096. }
  1097. // TEMPLATE FUNCTION copy_backward
  1098. template<class _BidIt1,
  1099. class _BidIt2> inline
  1100. _BidIt2 copy_backward(_BidIt1 _First, _BidIt1 _Last, _BidIt2 _Dest)
  1101. { // copy [_First, _Last) backwards to [..., _Dest)
  1102. return (_Copy_backward_opt(_First, _Last, _Dest,
  1103. _Ptr_cat(_First, _Dest)));
  1104. }
  1105. template<class _BidIt1,
  1106. class _BidIt2> inline
  1107. _BidIt2 _Copy_backward_opt(_BidIt1 _First, _BidIt1 _Last, _BidIt2 _Dest,
  1108. _Nonscalar_ptr_iterator_tag)
  1109. { // copy [_First, _Last) backwards to [..., _Dest), arbitrary iterators
  1110. while (_First != _Last)
  1111. *--_Dest = *--_Last;
  1112. return (_Dest);
  1113. }
  1114. template<class _InIt,
  1115. class _OutIt> inline
  1116. _OutIt _Copy_backward_opt(_InIt _First, _InIt _Last, _OutIt _Dest,
  1117. _Scalar_ptr_iterator_tag)
  1118. { // copy [_First, _Last) backwards to [..., _Dest), pointers to scalars
  1119. ptrdiff_t _Off = _Last - _First; // NB: non-overlapping move
  1120. return ((_OutIt)memmove(&*_Dest - _Off, &*_First,
  1121. _Off * sizeof (*_First)));
  1122. }
  1123. // TEMPLATE FUNCTION mismatch
  1124. template<class _InIt1,
  1125. class _InIt2> inline
  1126. pair<_InIt1, _InIt2>
  1127. mismatch(_InIt1 _First1, _InIt1 _Last1, _InIt2 _First2)
  1128. { // return [_First1, _Last1) and [_First2, _Last2) mismatch
  1129. for (; _First1 != _Last1 && *_First1 == *_First2; )
  1130. ++_First1, ++_First2;
  1131. return (pair<_InIt1, _InIt2>(_First1, _First2));
  1132. }
  1133. // TEMPLATE FUNCTION mismatch WITH PRED
  1134. template<class _InIt1,
  1135. class _InIt2,
  1136. class _Pr> inline
  1137. pair<_InIt1, _InIt2>
  1138. mismatch(_InIt1 _First1, _InIt1 _Last1, _InIt2 _First2, _Pr _Pred)
  1139. { // return [_First1, _Last1) and [_First2, _Last2) mismatch using _Pred
  1140. for (; _First1 != _Last1 && _Pred(*_First1, *_First2); )
  1141. ++_First1, ++_First2;
  1142. return (pair<_InIt1, _InIt2>(_First1, _First2));
  1143. }
  1144. // TEMPLATE FUNCTION equal
  1145. template<class _InIt1,
  1146. class _InIt2> inline
  1147. bool equal(_InIt1 _First1, _InIt1 _Last1, _InIt2 _First2)
  1148. { // compare [_First1, _Last1) to [First2, ...)
  1149. return (mismatch(_First1, _Last1, _First2).first == _Last1);
  1150. }
  1151. inline bool equal(const char *_First1,
  1152. const char *_Last1, const char *_First2)
  1153. { // compare [_First1, _Last1) to [First2, ...), for chars
  1154. return (::memcmp(_First1, _First2, _Last1 - _First1) == 0);
  1155. }
  1156. inline bool equal(const signed char *_First1,
  1157. const signed char *_Last1, const signed char *_First2)
  1158. { // compare [_First1, _Last1) to [First2, ...), for signed chars
  1159. return (::memcmp(_First1, _First2, _Last1 - _First1) == 0);
  1160. }
  1161. inline bool equal(const unsigned char *_First1,
  1162. const unsigned char *_Last1, const unsigned char *_First2)
  1163. { // compare [_First1, _Last1) to [First2, ...), for unsigned chars
  1164. return (::memcmp(_First1, _First2, _Last1 - _First1) == 0);
  1165. }
  1166. // TEMPLATE FUNCTION equal WITH PRED
  1167. template<class _InIt1,
  1168. class _InIt2,
  1169. class _Pr> inline
  1170. bool equal(_InIt1 _First1, _InIt1 _Last1, _InIt2 _First2, _Pr _Pred)
  1171. { // compare [_First1, _Last1) to [First2, ...) using _Pred
  1172. return (mismatch(_First1, _Last1, _First2, _Pred).first == _Last1);
  1173. }
  1174. // TEMPLATE FUNCTION fill
  1175. template<class _FwdIt,
  1176. class _Ty> inline
  1177. void fill(_FwdIt _First, _FwdIt _Last, const _Ty& _Val)
  1178. { // copy _Val through [_First, _Last)
  1179. for (; _First != _Last; ++_First)
  1180. *_First = _Val;
  1181. }
  1182. inline void fill(char *_First, char *_Last, int _Val)
  1183. { // copy char _Val through [_First, _Last)
  1184. ::memset(_First, _Val, _Last - _First);
  1185. }
  1186. inline void fill(signed char *_First, signed char *_Last, int _Val)
  1187. { // copy signed char _Val through [_First, _Last)
  1188. ::memset(_First, _Val, _Last - _First);
  1189. }
  1190. inline void fill(unsigned char *_First, unsigned char *_Last, int _Val)
  1191. { // copy unsigned char _Val through [_First, _Last)
  1192. ::memset(_First, _Val, _Last - _First);
  1193. }
  1194. // TEMPLATE FUNCTION fill_n
  1195. template<class _OutIt,
  1196. class _Diff,
  1197. class _Ty> inline
  1198. void fill_n(_OutIt _First, _Diff _Count, const _Ty& _Val)
  1199. { // copy _Val _Count times through [_First, ...)
  1200. for (; 0 < _Count; --_Count, ++_First)
  1201. *_First = _Val;
  1202. }
  1203. inline void fill_n(char *_First, size_t _Count, int _Val)
  1204. { // copy char _Val _Count times through [_First, ...)
  1205. ::memset(_First, _Val, _Count);
  1206. }
  1207. inline void fill_n(signed char *_First, size_t _Count, int _Val)
  1208. { // copy signed char _Val _Count times through [_First, ...)
  1209. ::memset(_First, _Val, _Count);
  1210. }
  1211. inline void fill_n(unsigned char *_First, size_t _Count, int _Val)
  1212. { // copy unsigned char _Val _Count times through [_First, ...)
  1213. ::memset(_First, _Val, _Count);
  1214. }
  1215. // TEMPLATE FUNCTION lexicographical_compare
  1216. template<class _InIt1,
  1217. class _InIt2> inline
  1218. bool lexicographical_compare(_InIt1 _First1, _InIt1 _Last1,
  1219. _InIt2 _First2, _InIt2 _Last2)
  1220. { // order [_First1, _Last1) vs. [First2, Last2)
  1221. for (; _First1 != _Last1 && _First2 != _Last2; ++_First1, ++_First2)
  1222. if (*_First1 < *_First2)
  1223. return (true);
  1224. else if (*_First2 < *_First1)
  1225. return (false);
  1226. return (_First1 == _Last1 && _First2 != _Last2);
  1227. }
  1228. inline bool lexicographical_compare(
  1229. const unsigned char *_First1, const unsigned char *_Last1,
  1230. const unsigned char *_First2, const unsigned char *_Last2)
  1231. { // order [_First1, _Last1) vs. [First2, Last2), for unsigned char
  1232. ptrdiff_t _Num1 = _Last1 - _First1;
  1233. ptrdiff_t _Num2 = _Last2 - _First2;
  1234. int _Ans = ::memcmp(_First1, _First2, _Num1 < _Num2 ? _Num1 : _Num2);
  1235. return (_Ans < 0 || _Ans == 0 && _Num1 < _Num2);
  1236. }
  1237. #if CHAR_MAX == UCHAR_MAX
  1238. inline bool lexicographical_compare(
  1239. const char *_First1, const char *_Last1,
  1240. const char *_First2, const char *_Last2)
  1241. { // order [_First1, _Last1) vs. [First2, Last2), for nonnegative char
  1242. ptrdiff_t _Num1 = _Last1 - _First1;
  1243. ptrdiff_t _Num2 = _Last2 - _First2;
  1244. int _Ans = ::memcmp(_First1, _First2, _Num1 < _Num2 ? _Num1 : _Num2);
  1245. return (_Ans < 0 || _Ans == 0 && _Num1 < _Num2);
  1246. }
  1247. #endif
  1248. // TEMPLATE FUNCTION lexicographical_compare WITH PRED
  1249. template<class _InIt1,
  1250. class _InIt2,
  1251. class _Pr> inline
  1252. bool lexicographical_compare(_InIt1 _First1, _InIt1 _Last1,
  1253. _InIt2 _First2, _InIt2 _Last2, _Pr _Pred)
  1254. { // order [_First1, _Last1) vs. [First2, Last2) using _Pred
  1255. for (; _First1 != _Last1 && _First2 != _Last2; ++_First1, ++_First2)
  1256. if (_Pred(*_First1, *_First2))
  1257. return (true);
  1258. else if (_Pred(*_First2, *_First1))
  1259. return (false);
  1260. return (_First1 == _Last1 && _First2 != _Last2);
  1261. }
  1262. #ifndef _MAX /* avoid collision with common (nonconforming) macros */
  1263. #define _MAX (max)
  1264. #define _MIN (min)
  1265. #endif
  1266. // TEMPLATE FUNCTION max
  1267. template<class _Ty> inline
  1268. const _Ty& _MAX(const _Ty& _Left, const _Ty& _Right)
  1269. { // return larger of _Left and _Right
  1270. return (_Left < _Right ? _Right : _Left);
  1271. }
  1272. // TEMPLATE FUNCTION max WITH PRED
  1273. template<class _Ty,
  1274. class _Pr> inline
  1275. const _Ty& _MAX(const _Ty& _Left, const _Ty& _Right, _Pr _Pred)
  1276. { // return larger of _Left and _Right using _Pred
  1277. return (_Pred(_Left, _Right) ? _Right : _Left);
  1278. }
  1279. // TEMPLATE FUNCTION min
  1280. template<class _Ty> inline
  1281. const _Ty& _MIN(const _Ty& _Left, const _Ty& _Right)
  1282. { // return smaller of _Left and _Right
  1283. return (_Right < _Left ? _Right : _Left);
  1284. }
  1285. // TEMPLATE FUNCTION min WITH PRED
  1286. template<class _Ty,
  1287. class _Pr> inline
  1288. const _Ty& _MIN(const _Ty& _Left, const _Ty& _Right, _Pr _Pred)
  1289. { // return smaller of _Left and _Right using _Pred
  1290. return (_Pred(_Right, _Left) ? _Right : _Left);
  1291. }
  1292. #ifndef _cpp_max /* retained from VC++ 6.0 */
  1293. #define _cpp_max max /* retained */
  1294. #define _cpp_min min /* retained */
  1295. #endif
  1296. #pragma warning(default:4786)
  1297. _STD_END
  1298. #pragma warning(pop)
  1299. #pragma pack(pop)
  1300. #endif /* _XUTILITY_ */
  1301. /*
  1302. * Copyright (c) 1992-2001 by P.J. Plauger. ALL RIGHTS RESERVED.
  1303. * Consult your license regarding permissions and restrictions.
  1304. */
  1305. /*
  1306. * This file is derived from software bearing the following
  1307. * restrictions:
  1308. *
  1309. * Copyright (c) 1994
  1310. * Hewlett-Packard Company
  1311. *
  1312. * Permission to use, copy, modify, distribute and sell this
  1313. * software and its documentation for any purpose is hereby
  1314. * granted without fee, provided that the above copyright notice
  1315. * appear in all copies and that both that copyright notice and
  1316. * this permission notice appear in supporting documentation.
  1317. * Hewlett-Packard Company makes no representations about the
  1318. * suitability of this software for any purpose. It is provided
  1319. * "as is" without express or implied warranty.
  1320. V3.10:0009 */