Source code of Windows XP (NT5)
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

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