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.

548 lines
12 KiB

  1. #pragma once
  2. #ifndef _STLUTIL_H_
  3. #define _STLUTIL_H_
  4. #include <stlxstdd.h>
  5. #ifdef _MSC_VER
  6. #pragma pack(push,8)
  7. #endif /* _MSC_VER */
  8. _STD_BEGIN
  9. // TEMPLATE STRUCT pair
  10. template<class _T1, class _T2> struct pair
  11. {
  12. typedef _T1 first_type;
  13. typedef _T2 second_type;
  14. pair()
  15. : first(_T1()), second(_T2())
  16. {
  17. }
  18. pair(const _T1& _V1, const _T2& _V2)
  19. : first(_V1), second(_V2)
  20. {
  21. }
  22. _T1 first;
  23. _T2 second;
  24. };
  25. template<class _T1, class _T2> inline
  26. bool __cdecl operator==(const pair<_T1, _T2>& _X,
  27. const pair<_T1, _T2>& _Y)
  28. {
  29. return (_X.first == _Y.first && _X.second == _Y.second);
  30. }
  31. template<class _T1, class _T2> inline
  32. bool __cdecl operator!=(const pair<_T1, _T2>& _X,
  33. const pair<_T1, _T2>& _Y)
  34. {
  35. return (!(_X == _Y));
  36. }
  37. template<class _T1, class _T2> inline
  38. bool __cdecl operator<(const pair<_T1, _T2>& _X,
  39. const pair<_T1, _T2>& _Y)
  40. {
  41. return (_X.first < _Y.first ||
  42. !(_Y.first < _X.first) && _X.second < _Y.second);
  43. }
  44. template<class _T1, class _T2> inline
  45. bool __cdecl operator>(const pair<_T1, _T2>& _X,
  46. const pair<_T1, _T2>& _Y)
  47. {
  48. return (_Y < _X);
  49. }
  50. template<class _T1, class _T2> inline
  51. bool __cdecl operator<=(const pair<_T1, _T2>& _X,
  52. const pair<_T1, _T2>& _Y)
  53. {
  54. return (!(_Y < _X));
  55. }
  56. template<class _T1, class _T2> inline
  57. bool __cdecl operator>=(const pair<_T1, _T2>& _X,
  58. const pair<_T1, _T2>& _Y)
  59. {
  60. return (!(_X < _Y));
  61. }
  62. template<class _T1, class _T2> inline
  63. pair<_T1, _T2> __cdecl make_pair(const _T1& _X, const _T2& _Y)
  64. {
  65. return (pair<_T1, _T2>(_X, _Y));
  66. }
  67. // ITERATOR TAGS (from <iterator>)
  68. struct input_iterator_tag
  69. {
  70. };
  71. struct output_iterator_tag
  72. {
  73. };
  74. struct forward_iterator_tag
  75. : public input_iterator_tag
  76. {
  77. };
  78. struct bidirectional_iterator_tag
  79. : public forward_iterator_tag
  80. {
  81. };
  82. struct random_access_iterator_tag
  83. : public bidirectional_iterator_tag
  84. {
  85. };
  86. // TEMPLATE CLASS iterator (from <iterator>)
  87. template<class _C, class _Ty, class _D = ptrdiff_t>
  88. struct iterator
  89. {
  90. typedef _C iterator_category;
  91. typedef _Ty value_type;
  92. typedef _D distance_type;
  93. };
  94. template<class _Ty, class _D>
  95. struct _Bidit : public iterator<bidirectional_iterator_tag, _Ty, _D>
  96. {
  97. };
  98. template<class _Ty, class _D>
  99. struct _Ranit : public iterator<random_access_iterator_tag, _Ty, _D>
  100. {
  101. };
  102. // TEMPLATE CLASS iterator_traits (from <iterator>)
  103. template<class _It>
  104. struct iterator_traits
  105. {
  106. typedef _It::iterator_category iterator_category;
  107. typedef _It::value_type value_type;
  108. typedef _It::distance_type distance_type;
  109. };
  110. // TEMPLATE FUNCTION _Iter_cat (from <iterator>)
  111. #if _MSC_VER >= 1200
  112. #pragma warning(push)
  113. #endif
  114. #pragma warning(disable:4700)
  115. template<class _C, class _Ty, class _D>
  116. inline
  117. _C __cdecl _Iter_cat(const iterator<_C, _Ty, _D>&)
  118. {
  119. _C _X;
  120. return (_X);
  121. }
  122. template<class _Ty>
  123. inline
  124. random_access_iterator_tag __cdecl _Iter_cat(const _Ty *)
  125. {
  126. random_access_iterator_tag _X;
  127. return (_X);
  128. }
  129. #if _MSC_VER >= 1200
  130. #pragma warning(pop)
  131. #else
  132. #pragma warning(default:4700)
  133. #endif
  134. // TEMPLATE FUNCTION _Distance
  135. template<class _II>
  136. inline
  137. _CNTSIZ(_II) __cdecl distance(_II _F, _II _L)
  138. {
  139. _CNTSIZ(_II) _N = 0;
  140. _Distance(_F, _L, _N, _Iter_cat(_F));
  141. return (_N);
  142. }
  143. template<class _II, class _D>
  144. inline
  145. void __cdecl _Distance(_II _F, _II _L, _D& _N)
  146. {
  147. _Distance(_F, _L, _N, _Iter_cat(_F));
  148. }
  149. template<class _II, class _D>
  150. inline
  151. void __cdecl _Distance(_II _F, _II _L, _D& _N, input_iterator_tag)
  152. {
  153. for (; _F != _L; ++_F)
  154. ++_N;
  155. }
  156. template<class _II, class _D>
  157. inline
  158. void __cdecl _Distance(_II _F, _II _L, _D& _N, forward_iterator_tag)
  159. {
  160. for (; _F != _L; ++_F)
  161. ++_N;
  162. }
  163. template<class _II, class _D>
  164. inline
  165. void __cdecl _Distance(_II _F, _II _L, _D& _N,
  166. bidirectional_iterator_tag)
  167. {
  168. for (; _F != _L; ++_F)
  169. ++_N;
  170. }
  171. template<class _RI, class _D>
  172. inline
  173. void __cdecl _Distance(_RI _F, _RI _L, _D& _N,
  174. random_access_iterator_tag)
  175. {
  176. _N += (_D)(_L - _F);
  177. }
  178. // TEMPLATE CLASS reverse_iterator (from <iterator>)
  179. template<class _RI, class _Ty, class _Rt = _Ty&, class _Pt = _Ty *,
  180. class _D = ptrdiff_t>
  181. class reverse_iterator : public _Ranit<_Ty, _D>
  182. {
  183. public:
  184. typedef reverse_iterator<_RI, _Ty, _Rt, _Pt, _D> _Myt;
  185. typedef _RI iter_type;
  186. typedef _Rt reference_type;
  187. typedef _Pt pointer_type;
  188. reverse_iterator()
  189. {
  190. }
  191. explicit reverse_iterator(_RI _X)
  192. : current(_X)
  193. {
  194. }
  195. _RI base() const
  196. {
  197. return (current);
  198. }
  199. _Rt operator*() const
  200. {
  201. return (*(current - 1));
  202. }
  203. _Pt operator->() const
  204. {
  205. return (&**this);
  206. }
  207. _Myt& operator++()
  208. {
  209. --current;
  210. return (*this);
  211. }
  212. _Myt operator++(int)
  213. {
  214. _Myt _Tmp = *this;
  215. --current;
  216. return (_Tmp);
  217. }
  218. _Myt& operator--()
  219. {
  220. ++current;
  221. return (*this);
  222. }
  223. _Myt operator--(int)
  224. {
  225. _Myt _Tmp = *this;
  226. ++current;
  227. return (_Tmp);
  228. }
  229. _Myt& operator+=(_D _N)
  230. {
  231. current -= _N;
  232. return (*this);
  233. }
  234. _Myt operator+(_D _N) const
  235. {
  236. return (_Myt(current - _N));
  237. }
  238. _Myt& operator-=(_D _N)
  239. {
  240. current += _N;
  241. return (*this);
  242. }
  243. _Myt operator-(_D _N) const
  244. {
  245. return (_Myt(current + _N));
  246. }
  247. _Rt operator[](_D _N) const
  248. {
  249. return (*(*this + _N));
  250. }
  251. protected:
  252. _RI current;
  253. };
  254. template<class _RI, class _Ty, class _Rt, class _Pt, class _D>
  255. inline
  256. bool __cdecl operator==(
  257. const reverse_iterator<_RI, _Ty, _Rt, _Pt, _D>& _X,
  258. const reverse_iterator<_RI, _Ty, _Rt, _Pt, _D>& _Y)
  259. {
  260. return (_X.base() == _Y.base());
  261. }
  262. template<class _RI, class _Ty, class _Rt, class _Pt, class _D>
  263. inline
  264. bool __cdecl operator!=(
  265. const reverse_iterator<_RI, _Ty, _Rt, _Pt, _D>& _X,
  266. const reverse_iterator<_RI, _Ty, _Rt, _Pt, _D>& _Y)
  267. {
  268. return (!(_X == _Y));
  269. }
  270. template<class _RI, class _Ty, class _Rt, class _Pt, class _D>
  271. inline
  272. bool __cdecl operator<(
  273. const reverse_iterator<_RI, _Ty, _Rt, _Pt, _D>& _X,
  274. const reverse_iterator<_RI, _Ty, _Rt, _Pt, _D>& _Y)
  275. {
  276. return (_Y.base() < _X.base());
  277. }
  278. template<class _RI, class _Ty, class _Rt, class _Pt, class _D>
  279. inline
  280. bool __cdecl operator>(
  281. const reverse_iterator<_RI, _Ty, _Rt, _Pt, _D>& _X,
  282. const reverse_iterator<_RI, _Ty, _Rt, _Pt, _D>& _Y)
  283. {
  284. return (_Y < _X);
  285. }
  286. template<class _RI, class _Ty, class _Rt, class _Pt, class _D>
  287. inline
  288. bool __cdecl operator<=(
  289. const reverse_iterator<_RI, _Ty, _Rt, _Pt, _D>& _X,
  290. const reverse_iterator<_RI, _Ty, _Rt, _Pt, _D>& _Y)
  291. {
  292. return (!(_Y < _X));
  293. }
  294. template<class _RI, class _Ty, class _Rt, class _Pt, class _D>
  295. inline
  296. bool __cdecl operator>=(
  297. const reverse_iterator<_RI, _Ty, _Rt, _Pt, _D>& _X,
  298. const reverse_iterator<_RI, _Ty, _Rt, _Pt, _D>& _Y)
  299. {
  300. return (!(_X < _Y));
  301. }
  302. template<class _RI, class _Ty, class _Rt, class _Pt, class _D>
  303. inline
  304. _D __cdecl operator-(
  305. const reverse_iterator<_RI, _Ty, _Rt, _Pt, _D>& _X,
  306. const reverse_iterator<_RI, _Ty, _Rt, _Pt, _D>& _Y)
  307. {
  308. return (_Y.base() - _X.base());
  309. }
  310. template<class _RI, class _Ty, class _Rt, class _Pt, class _D>
  311. inline
  312. reverse_iterator<_RI, _Ty, _Rt, _Pt, _D> __cdecl operator+(_D _N,
  313. const reverse_iterator<_RI, _Ty, _Rt, _Pt, _D>& _Y)
  314. {
  315. return (reverse_iterator<_RI, _Ty, _Rt, _Pt, _D>(
  316. _Y.base() - _N));
  317. }
  318. /*
  319. // TEMPLATE CLASS istreambuf_iterator (from <iterator>)
  320. template<class _E, class _Tr = char_traits<_E> >
  321. class istreambuf_iterator :
  322. public iterator<input_iterator_tag, _E, _Tr::off_type>
  323. {
  324. public:
  325. typedef istreambuf_iterator<_E, _Tr> _Myt;
  326. typedef _E char_type;
  327. typedef _Tr traits_type;
  328. typedef _Tr::int_type int_type;
  329. typedef basic_streambuf<_E, _Tr> streambuf_type;
  330. typedef basic_istream<_E, _Tr> istream_type;
  331. istreambuf_iterator(streambuf_type *_Sb = 0) _THROW0()
  332. : _Sbuf(_Sb), _Got(_Sb == 0)
  333. {
  334. }
  335. istreambuf_iterator(istream_type& _I) _THROW0()
  336. : _Sbuf(_I.rdbuf()), _Got(_I.rdbuf() == 0)
  337. {
  338. }
  339. const _E& operator*() const
  340. {
  341. if (!_Got)
  342. ((_Myt *)this)->_Peek();
  343. return (_Val);
  344. }
  345. const _E *operator->() const
  346. {
  347. return (&**this);
  348. }
  349. _Myt& operator++()
  350. {
  351. _Inc();
  352. return (*this);
  353. }
  354. _Myt operator++(int)
  355. {
  356. if (!_Got)
  357. _Peek();
  358. _Myt _Tmp = *this;
  359. _Inc();
  360. return (_Tmp);
  361. }
  362. bool equal(const _Myt& _X) const
  363. {
  364. if (!_Got)
  365. ((_Myt *)this)->_Peek();
  366. if (!_X._Got)
  367. ((_Myt *)&_X)->_Peek();
  368. return (_Sbuf == 0 && _X._Sbuf == 0
  369. || _Sbuf != 0 && _X._Sbuf != 0);
  370. }
  371. private:
  372. void _Inc()
  373. {
  374. if (_Sbuf == 0
  375. || _Tr::eq_int_type(_Tr::eof(), _Sbuf->sbumpc()))
  376. _Sbuf = 0, _Got = true;
  377. else
  378. _Got = false;
  379. }
  380. _E _Peek()
  381. {
  382. int_type _C;
  383. if (_Sbuf == 0
  384. || _Tr::eq_int_type(_Tr::eof(), _C = _Sbuf->sgetc()))
  385. _Sbuf = 0;
  386. else
  387. _Val = _Tr::to_char_type(_C);
  388. _Got = true;
  389. return (_Val);
  390. }
  391. streambuf_type *_Sbuf;
  392. bool _Got;
  393. _E _Val;
  394. };
  395. template<class _E, class _Tr> inline
  396. bool __cdecl operator==(const istreambuf_iterator<_E, _Tr>& _X,
  397. const istreambuf_iterator<_E, _Tr>& _Y)
  398. {
  399. return (_X.equal(_Y));
  400. }
  401. template<class _E, class _Tr> inline
  402. bool __cdecl operator!=(const istreambuf_iterator<_E, _Tr>& _X,
  403. const istreambuf_iterator<_E, _Tr>& _Y)
  404. {
  405. return (!(_X == _Y));
  406. }
  407. // TEMPLATE CLASS ostreambuf_iterator (from <iterator>)
  408. template<class _E, class _Tr = char_traits<_E> >
  409. class ostreambuf_iterator :
  410. public iterator<output_iterator_tag, void, void>
  411. {
  412. typedef ostreambuf_iterator<_E, _Tr> _Myt;
  413. public:
  414. typedef _E char_type;
  415. typedef _Tr traits_type;
  416. typedef basic_streambuf<_E, _Tr> streambuf_type;
  417. typedef basic_ostream<_E, _Tr> ostream_type;
  418. ostreambuf_iterator(streambuf_type *_Sb) _THROW0()
  419. : _Failed(false), _Sbuf(_Sb)
  420. {
  421. }
  422. ostreambuf_iterator(ostream_type& _O) _THROW0()
  423. : _Failed(false), _Sbuf(_O.rdbuf())
  424. {
  425. }
  426. _Myt& operator=(_E _X)
  427. {
  428. if (_Sbuf == 0
  429. || _Tr::eq_int_type(_Tr::eof(), _Sbuf->sputc(_X)))
  430. _Failed = true;
  431. return (*this);
  432. }
  433. _Myt& operator*()
  434. {
  435. return (*this);
  436. }
  437. _Myt& operator++()
  438. {
  439. return (*this);
  440. }
  441. _Myt& operator++(int)
  442. {
  443. return (*this);
  444. }
  445. bool failed() const _THROW0()
  446. {
  447. return (_Failed);
  448. }
  449. private:
  450. bool _Failed;
  451. streambuf_type *_Sbuf;
  452. };
  453. */
  454. // TEMPLATE OPERATORS
  455. namespace rel_ops
  456. {
  457. template<class _Ty> inline
  458. bool __cdecl operator!=(const _Ty& _X, const _Ty& _Y)
  459. {
  460. return (!(_X == _Y));
  461. }
  462. template<class _Ty> inline
  463. bool __cdecl operator>(const _Ty& _X, const _Ty& _Y)
  464. {
  465. return (_Y < _X);
  466. }
  467. template<class _Ty> inline
  468. bool __cdecl operator<=(const _Ty& _X, const _Ty& _Y)
  469. {
  470. return (!(_Y < _X));
  471. }
  472. template<class _Ty> inline
  473. bool __cdecl operator>=(const _Ty& _X, const _Ty& _Y)
  474. {
  475. return (!(_X < _Y));
  476. }
  477. }
  478. _STD_END
  479. #ifdef _MSC_VER
  480. #pragma pack(pop)
  481. #endif /* _MSC_VER */
  482. #endif /* _STLUTIL_H_ */
  483. /*
  484. * Copyright (c) 1995 by P.J. Plauger. ALL RIGHTS RESERVED.
  485. * Consult your license regarding permissions and restrictions.
  486. */
  487. /*
  488. * This file is derived from software bearing the following
  489. * restrictions:
  490. *
  491. * Copyright (c) 1994
  492. * Hewlett-Packard Company
  493. *
  494. * Permission to use, copy, modify, distribute and sell this
  495. * software and its documentation for any purpose is hereby
  496. * granted without fee, provided that the above copyright notice
  497. * appear in all copies and that both that copyright notice and
  498. * this permission notice appear in supporting documentation.
  499. * Hewlett-Packard Company makes no representations about the
  500. * suitability of this software for any purpose. It is provided
  501. * "as is" without express or implied warranty.
  502. */