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.

363 lines
11 KiB

  1. // utility standard header
  2. #ifndef _UTILITY_
  3. #define _UTILITY_
  4. #include <iosfwd>
  5. #ifdef _MSC_VER
  6. #pragma pack(push,8)
  7. #if 1200 <= _MSC_VER
  8. #pragma warning(push,3)
  9. #pragma warning(disable:4786)
  10. #endif
  11. #endif /* _MSC_VER */
  12. _STD_BEGIN
  13. // TEMPLATE STRUCT pair
  14. template<class _T1, class _T2> struct pair {
  15. typedef _T1 first_type;
  16. typedef _T2 second_type;
  17. pair()
  18. : first(_T1()), second(_T2()) {}
  19. pair(const _T1& _V1, const _T2& _V2)
  20. : first(_V1), second(_V2) {}
  21. template<class U, class V> pair(const pair<U, V> &p)
  22. : first(p.first), second(p.second) {}
  23. _T1 first;
  24. _T2 second;
  25. };
  26. template<class _T1, class _T2> inline
  27. bool __cdecl operator==(const pair<_T1, _T2>& _X,
  28. const pair<_T1, _T2>& _Y)
  29. {return (_X.first == _Y.first && _X.second == _Y.second); }
  30. template<class _T1, class _T2> inline
  31. bool __cdecl operator!=(const pair<_T1, _T2>& _X,
  32. const pair<_T1, _T2>& _Y)
  33. {return (!(_X == _Y)); }
  34. template<class _T1, class _T2> inline
  35. bool __cdecl operator<(const pair<_T1, _T2>& _X,
  36. const pair<_T1, _T2>& _Y)
  37. {return (_X.first < _Y.first ||
  38. !(_Y.first < _X.first) && _X.second < _Y.second); }
  39. template<class _T1, class _T2> inline
  40. bool __cdecl operator>(const pair<_T1, _T2>& _X,
  41. const pair<_T1, _T2>& _Y)
  42. {return (_Y < _X); }
  43. template<class _T1, class _T2> inline
  44. bool __cdecl operator<=(const pair<_T1, _T2>& _X,
  45. const pair<_T1, _T2>& _Y)
  46. {return (!(_Y < _X)); }
  47. template<class _T1, class _T2> inline
  48. bool __cdecl operator>=(const pair<_T1, _T2>& _X,
  49. const pair<_T1, _T2>& _Y)
  50. {return (!(_X < _Y)); }
  51. template<class _T1, class _T2> inline
  52. pair<_T1, _T2> __cdecl make_pair(const _T1& _X, const _T2& _Y)
  53. {return (pair<_T1, _T2>(_X, _Y)); }
  54. // ITERATOR TAGS (from <iterator>)
  55. struct input_iterator_tag {};
  56. struct output_iterator_tag {};
  57. struct forward_iterator_tag
  58. : public input_iterator_tag {};
  59. struct bidirectional_iterator_tag
  60. : public forward_iterator_tag {};
  61. struct random_access_iterator_tag
  62. : public bidirectional_iterator_tag {};
  63. // TEMPLATE CLASS iterator (from <iterator>)
  64. template<class _C, class _Ty, class _D = ptrdiff_t>
  65. struct iterator {
  66. typedef _C iterator_category;
  67. typedef _Ty value_type;
  68. typedef _D distance_type;
  69. };
  70. template<class _Ty, class _D>
  71. struct _Bidit : public iterator<bidirectional_iterator_tag,
  72. _Ty, _D> {};
  73. template<class _Ty, class _D>
  74. struct _Ranit : public iterator<random_access_iterator_tag,
  75. _Ty, _D> {};
  76. // TEMPLATE CLASS iterator_traits (from <iterator>)
  77. template<class _It>
  78. struct iterator_traits {
  79. typedef typename _It::iterator_category iterator_category;
  80. typedef typename _It::value_type value_type;
  81. typedef typename _It::distance_type distance_type;
  82. };
  83. // TEMPLATE FUNCTION _Iter_cat (from <iterator>)
  84. template<class _C, class _Ty, class _D> inline
  85. _C __cdecl _Iter_cat(const iterator<_C, _Ty, _D>&)
  86. {_C _IterCatTag;
  87. _C* _pIterCatTag;
  88. _pIterCatTag = &_IterCatTag; // Workaround for C4700 warning
  89. return (_IterCatTag); }
  90. template<class _Ty> inline
  91. random_access_iterator_tag __cdecl _Iter_cat(const _Ty *)
  92. {random_access_iterator_tag _RandIterTag;
  93. random_access_iterator_tag* _pRandIterTag;
  94. _pRandIterTag = &_RandIterTag; // Workaround for C4700 warning
  95. return (_RandIterTag); }
  96. // TEMPLATE FUNCTION _Distance
  97. template<class _II> inline
  98. _CNTSIZ(_II) __cdecl distance(_II _F, _II _L)
  99. {_CNTSIZ(_II) _N = 0;
  100. _Distance(_F, _L, _N, _Iter_cat(_F));
  101. return (_N); }
  102. template<class _II, class _D> inline
  103. void __cdecl _Distance(_II _F, _II _L, _D& _N)
  104. {_Distance(_F, _L, _N, _Iter_cat(_F)); }
  105. template<class _II, class _D> inline
  106. void __cdecl _Distance(_II _F, _II _L, _D& _N, input_iterator_tag)
  107. {for (; _F != _L; ++_F)
  108. ++_N; }
  109. template<class _II, class _D> inline
  110. void __cdecl _Distance(_II _F, _II _L, _D& _N, forward_iterator_tag)
  111. {for (; _F != _L; ++_F)
  112. ++_N; }
  113. template<class _II, class _D> inline
  114. void __cdecl _Distance(_II _F, _II _L, _D& _N,
  115. bidirectional_iterator_tag)
  116. {for (; _F != _L; ++_F)
  117. ++_N; }
  118. template<class _RI, class _D> inline
  119. void __cdecl _Distance(_RI _F, _RI _L, _D& _N,
  120. random_access_iterator_tag)
  121. {_N += (_D) (_L - _F); }
  122. // TEMPLATE CLASS reverse_iterator (from <iterator>)
  123. template<class _RI,
  124. class _Ty,
  125. class _Rt = _Ty&,
  126. class _Pt = _Ty *,
  127. class _D = ptrdiff_t>
  128. class reverse_iterator : public _Ranit<_Ty, _D> {
  129. public:
  130. typedef reverse_iterator<_RI, _Ty, _Rt, _Pt, _D> _Myt;
  131. typedef _RI iter_type;
  132. typedef _Rt reference_type;
  133. typedef _Pt pointer_type;
  134. reverse_iterator()
  135. {}
  136. explicit reverse_iterator(_RI _X)
  137. : current(_X) {}
  138. _RI base() const
  139. {return (current); }
  140. _Rt operator*() const
  141. {return (*(current - 1)); }
  142. _Pt operator->() const
  143. {return (&**this); }
  144. _Myt& operator++()
  145. {--current;
  146. return (*this); }
  147. _Myt operator++(int)
  148. {_Myt _Tmp = *this;
  149. --current;
  150. return (_Tmp); }
  151. _Myt& operator--()
  152. {++current;
  153. return (*this); }
  154. _Myt operator--(int)
  155. {_Myt _Tmp = *this;
  156. ++current;
  157. return (_Tmp); }
  158. _Myt& operator+=(_D _N)
  159. {current -= _N;
  160. return (*this); }
  161. _Myt operator+(_D _N) const
  162. {return (_Myt(current - _N)); }
  163. _Myt& operator-=(_D _N)
  164. {current += _N;
  165. return (*this); }
  166. _Myt operator-(_D _N) const
  167. {return (_Myt(current + _N)); }
  168. _Rt operator[](_D _N) const
  169. {return (*(*this + _N)); }
  170. protected:
  171. _RI current;
  172. };
  173. template<class _RI, class _Ty, class _Rt, class _Pt,
  174. class _D> inline
  175. bool __cdecl operator==(
  176. const reverse_iterator<_RI, _Ty, _Rt, _Pt, _D>& _X,
  177. const reverse_iterator<_RI, _Ty, _Rt, _Pt, _D>& _Y)
  178. {return (_X.base() == _Y.base()); }
  179. template<class _RI, class _Ty, class _Rt, class _Pt,
  180. class _D> inline
  181. bool __cdecl operator!=(
  182. const reverse_iterator<_RI, _Ty, _Rt, _Pt, _D>& _X,
  183. const reverse_iterator<_RI, _Ty, _Rt, _Pt, _D>& _Y)
  184. {return (!(_X == _Y)); }
  185. template<class _RI, class _Ty, class _Rt, class _Pt,
  186. class _D> inline
  187. bool __cdecl operator<(
  188. const reverse_iterator<_RI, _Ty, _Rt, _Pt, _D>& _X,
  189. const reverse_iterator<_RI, _Ty, _Rt, _Pt, _D>& _Y)
  190. {return (_Y.base() < _X.base()); }
  191. template<class _RI, class _Ty, class _Rt, class _Pt,
  192. class _D> inline
  193. bool __cdecl operator>(
  194. const reverse_iterator<_RI, _Ty, _Rt, _Pt, _D>& _X,
  195. const reverse_iterator<_RI, _Ty, _Rt, _Pt, _D>& _Y)
  196. {return (_Y < _X); }
  197. template<class _RI, class _Ty, class _Rt, class _Pt,
  198. class _D> inline
  199. bool __cdecl operator<=(
  200. const reverse_iterator<_RI, _Ty, _Rt, _Pt, _D>& _X,
  201. const reverse_iterator<_RI, _Ty, _Rt, _Pt, _D>& _Y)
  202. {return (!(_Y < _X)); }
  203. template<class _RI, class _Ty, class _Rt, class _Pt,
  204. class _D> inline
  205. bool __cdecl operator>=(
  206. const reverse_iterator<_RI, _Ty, _Rt, _Pt, _D>& _X,
  207. const reverse_iterator<_RI, _Ty, _Rt, _Pt, _D>& _Y)
  208. {return (!(_X < _Y)); }
  209. template<class _RI, class _Ty, class _Rt, class _Pt,
  210. class _D> inline
  211. _D __cdecl operator-(
  212. const reverse_iterator<_RI, _Ty, _Rt, _Pt, _D>& _X,
  213. const reverse_iterator<_RI, _Ty, _Rt, _Pt, _D>& _Y)
  214. {return (_Y.base() - _X.base()); }
  215. template<class _RI, class _Ty, class _Rt, class _Pt,
  216. class _D> inline
  217. reverse_iterator<_RI, _Ty, _Rt, _Pt, _D> __cdecl operator+(_D _N,
  218. const reverse_iterator<_RI, _Ty, _Rt, _Pt, _D>& _Y)
  219. {return (reverse_iterator<_RI, _Ty, _Rt, _Pt, _D>(
  220. _Y.base() - _N)); }
  221. // TEMPLATE CLASS istreambuf_iterator (from <iterator>)
  222. template<class _E, class _Tr /* = char_traits<_E> */ >
  223. class istreambuf_iterator
  224. : public iterator<input_iterator_tag, _E, typename _Tr::off_type> {
  225. public:
  226. typedef istreambuf_iterator<_E, _Tr> _Myt;
  227. typedef _E char_type;
  228. typedef _Tr traits_type;
  229. typedef typename _Tr::int_type int_type;
  230. typedef basic_streambuf<_E, _Tr> streambuf_type;
  231. typedef basic_istream<_E, _Tr> istream_type;
  232. istreambuf_iterator(streambuf_type *_Sb = 0) _THROW0()
  233. : _Sbuf(_Sb), _Got(_Sb == 0) {}
  234. istreambuf_iterator(istream_type& _I) _THROW0()
  235. : _Sbuf(_I.rdbuf()), _Got(_I.rdbuf() == 0) {}
  236. const _E& operator*() const
  237. {if (!_Got)
  238. ((_Myt *)this)->_Peek();
  239. return (_Val); }
  240. const _E *operator->() const
  241. {return (&**this); }
  242. _Myt& operator++()
  243. {_Inc();
  244. return (*this); }
  245. _Myt operator++(int)
  246. {if (!_Got)
  247. _Peek();
  248. _Myt _Tmp = *this;
  249. _Inc();
  250. return (_Tmp); }
  251. bool equal(const _Myt& _X) const
  252. {if (!_Got)
  253. ((_Myt *)this)->_Peek();
  254. if (!_X._Got)
  255. ((_Myt *)&_X)->_Peek();
  256. return (_Sbuf == 0 && _X._Sbuf == 0
  257. || _Sbuf != 0 && _X._Sbuf != 0); }
  258. private:
  259. void _Inc()
  260. {if (_Sbuf == 0
  261. || _Tr::eq_int_type(_Tr::eof(), _Sbuf->sbumpc()))
  262. _Sbuf = 0, _Got = true;
  263. else
  264. _Got = false; }
  265. _E _Peek()
  266. {int_type _C;
  267. if (_Sbuf == 0
  268. || _Tr::eq_int_type(_Tr::eof(), _C = _Sbuf->sgetc()))
  269. _Sbuf = 0;
  270. else
  271. _Val = _Tr::to_char_type(_C);
  272. _Got = true;
  273. return (_Val); }
  274. streambuf_type *_Sbuf;
  275. bool _Got;
  276. _E _Val;
  277. };
  278. template<class _E, class _Tr> inline
  279. bool __cdecl operator==(const istreambuf_iterator<_E, _Tr>& _X,
  280. const istreambuf_iterator<_E, _Tr>& _Y)
  281. {return (_X.equal(_Y)); }
  282. template<class _E, class _Tr> inline
  283. bool __cdecl operator!=(const istreambuf_iterator<_E, _Tr>& _X,
  284. const istreambuf_iterator<_E, _Tr>& _Y)
  285. {return (!(_X == _Y)); }
  286. // TEMPLATE CLASS ostreambuf_iterator (from <iterator>)
  287. template<class _E, class _Tr /* = char_traits<_E> */ >
  288. class ostreambuf_iterator
  289. : public iterator<output_iterator_tag, void, void> {
  290. typedef ostreambuf_iterator<_E, _Tr> _Myt;
  291. public:
  292. typedef _E char_type;
  293. typedef _Tr traits_type;
  294. typedef basic_streambuf<_E, _Tr> streambuf_type;
  295. typedef basic_ostream<_E, _Tr> ostream_type;
  296. ostreambuf_iterator(streambuf_type *_Sb) _THROW0()
  297. : _Failed(false), _Sbuf(_Sb) {}
  298. ostreambuf_iterator(ostream_type& _O) _THROW0()
  299. : _Failed(false), _Sbuf(_O.rdbuf()) {}
  300. _Myt& operator=(_E _X)
  301. {if (_Sbuf == 0
  302. || _Tr::eq_int_type(_Tr::eof(), _Sbuf->sputc(_X)))
  303. _Failed = true;
  304. return (*this); }
  305. _Myt& operator*()
  306. {return (*this); }
  307. _Myt& operator++()
  308. {return (*this); }
  309. _Myt& operator++(int)
  310. {return (*this); }
  311. bool failed() const _THROW0()
  312. {return (_Failed); }
  313. private:
  314. bool _Failed;
  315. streambuf_type *_Sbuf;
  316. };
  317. // TEMPLATE OPERATORS
  318. namespace rel_ops {
  319. template<class _Ty> inline
  320. bool __cdecl operator!=(const _Ty& _X, const _Ty& _Y)
  321. {return (!(_X == _Y)); }
  322. template<class _Ty> inline
  323. bool __cdecl operator>(const _Ty& _X, const _Ty& _Y)
  324. {return (_Y < _X); }
  325. template<class _Ty> inline
  326. bool __cdecl operator<=(const _Ty& _X, const _Ty& _Y)
  327. {return (!(_Y < _X)); }
  328. template<class _Ty> inline
  329. bool __cdecl operator>=(const _Ty& _X, const _Ty& _Y)
  330. {return (!(_X < _Y)); }
  331. _STD_END
  332. _STD_END
  333. #ifdef _MSC_VER
  334. #if 1200 <= _MSC_VER
  335. #pragma warning(pop)
  336. #endif
  337. #pragma pack(pop)
  338. #endif /* _MSC_VER */
  339. #endif /* _UTILITY_ */
  340. /*
  341. * Copyright (c) 1995 by P.J. Plauger. ALL RIGHTS RESERVED.
  342. * Consult your license regarding permissions and restrictions.
  343. */
  344. /*
  345. * This file is derived from software bearing the following
  346. * restrictions:
  347. *
  348. * Copyright (c) 1994
  349. * Hewlett-Packard Company
  350. *
  351. * Permission to use, copy, modify, distribute and sell this
  352. * software and its documentation for any purpose is hereby
  353. * granted without fee, provided that the above copyright notice
  354. * appear in all copies and that both that copyright notice and
  355. * this permission notice appear in supporting documentation.
  356. * Hewlett-Packard Company makes no representations about the
  357. * suitability of this software for any purpose. It is provided
  358. * "as is" without express or implied warranty.
  359. */