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.

392 lines
10 KiB

  1. // iterator standard header
  2. #pragma once
  3. #ifndef _ITERATOR_
  4. #define _ITERATOR_
  5. #include <xutility>
  6. #pragma pack(push,8)
  7. #pragma warning(push,3)
  8. _STD_BEGIN
  9. // TEMPLATE CLASS back_insert_iterator
  10. template<class _Container>
  11. class back_insert_iterator
  12. : public _Outit
  13. { // wrap pushes to back of container as output iterator
  14. public:
  15. typedef _Container container_type;
  16. typedef typename _Container::reference reference;
  17. explicit back_insert_iterator(_Container& _Cont)
  18. : container(&_Cont)
  19. { // construct with container
  20. }
  21. back_insert_iterator<_Container>& operator=(
  22. typename _Container::const_reference _Val)
  23. { // push value into container
  24. container->push_back(_Val);
  25. return (*this);
  26. }
  27. back_insert_iterator<_Container>& operator*()
  28. { // pretend to return designated value
  29. return (*this);
  30. }
  31. back_insert_iterator<_Container>& operator++()
  32. { // pretend to preincrement
  33. return (*this);
  34. }
  35. back_insert_iterator<_Container> operator++(int)
  36. { // pretend to postincrement
  37. return (*this);
  38. }
  39. protected:
  40. _Container *container; // pointer to container
  41. };
  42. // TEMPLATE FUNCTION back_inserter
  43. template<class _Container> inline
  44. back_insert_iterator<_Container> back_inserter(_Container& _Cont)
  45. { // return a back_insert_iterator
  46. return (std::back_insert_iterator<_Container>(_Cont));
  47. }
  48. // TEMPLATE CLASS front_insert_iterator
  49. template<class _Container>
  50. class front_insert_iterator
  51. : public _Outit
  52. { // wrap pushes to front of container as output iterator
  53. public:
  54. typedef _Container container_type;
  55. typedef typename _Container::reference reference;
  56. explicit front_insert_iterator(_Container& _Cont)
  57. : container(&_Cont)
  58. { // construct with container
  59. }
  60. front_insert_iterator<_Container>& operator=(
  61. typename _Container::const_reference _Val)
  62. { // push value into container
  63. container->push_front(_Val);
  64. return (*this);
  65. }
  66. front_insert_iterator<_Container>& operator*()
  67. { // pretend to return designated value
  68. return (*this);
  69. }
  70. front_insert_iterator<_Container>& operator++()
  71. { // pretend to preincrement
  72. return (*this);
  73. }
  74. front_insert_iterator<_Container> operator++(int)
  75. { // pretend to postincrement
  76. return (*this);
  77. }
  78. protected:
  79. _Container *container; // pointer to container
  80. };
  81. // TEMPLATE FUNCTION front_inserter
  82. template<class _Container> inline
  83. front_insert_iterator<_Container> front_inserter(_Container& _Cont)
  84. { // return front_insert_iterator
  85. return (std::front_insert_iterator<_Container>(_Cont));
  86. }
  87. // TEMPLATE CLASS insert_iterator
  88. template<class _Container>
  89. class insert_iterator
  90. : public _Outit
  91. { // wrap inserts into container as output iterator
  92. public:
  93. typedef _Container container_type;
  94. typedef typename _Container::reference reference;
  95. insert_iterator(_Container& _Cont, typename _Container::iterator _Where)
  96. : container(&_Cont), iter(_Where)
  97. { // construct with container and iterator
  98. }
  99. insert_iterator<_Container>& operator=(
  100. typename _Container::const_reference _Val)
  101. { // insert into container and increment stored iterator
  102. iter = container->insert(iter, _Val);
  103. ++iter;
  104. return (*this);
  105. }
  106. insert_iterator<_Container>& operator*()
  107. { // pretend to return designated value
  108. return (*this);
  109. }
  110. insert_iterator<_Container>& operator++()
  111. { // pretend to preincrement
  112. return (*this);
  113. }
  114. insert_iterator<_Container>& operator++(int)
  115. { // pretend to postincrement
  116. return (*this);
  117. }
  118. protected:
  119. _Container *container; // pointer to container
  120. typename _Container::iterator iter; // iterator into container
  121. };
  122. // TEMPLATE FUNCTION inserter
  123. template<class _Container,
  124. class _Iter> inline
  125. insert_iterator<_Container> inserter(_Container& _Cont, _Iter _Where)
  126. { // return insert_iterator
  127. return (std::insert_iterator<_Container>(_Cont, _Where));
  128. }
  129. // TEMPLATE CLASS istream_iterator
  130. template<class _Ty,
  131. class _Elem = char,
  132. class _Traits = char_traits<_Elem>,
  133. class _Diff = ptrdiff_t>
  134. class istream_iterator
  135. : public iterator<input_iterator_tag, _Ty, _Diff,
  136. const _Ty *, const _Ty&>
  137. { // wrap _Ty extracts from input stream as input iterator
  138. public:
  139. typedef istream_iterator<_Ty, _Elem, _Traits, _Diff> _Myt;
  140. typedef _Elem char_type;
  141. typedef _Traits traits_type;
  142. typedef basic_istream<_Elem, _Traits> istream_type;
  143. istream_iterator()
  144. : _Myistr(0)
  145. { // construct singular iterator
  146. }
  147. istream_iterator(istream_type& _Istr)
  148. : _Myistr(&_Istr)
  149. { // construct with input stream
  150. _Getval();
  151. }
  152. const _Ty& operator*() const
  153. { // return designated value
  154. return (_Myval);
  155. }
  156. const _Ty *operator->() const
  157. { // return pointer to class object
  158. return (&**this);
  159. }
  160. _Myt& operator++()
  161. { // preincrement
  162. _Getval();
  163. return (*this);
  164. }
  165. _Myt operator++(int)
  166. { // postincrement
  167. _Myt _Tmp = *this;
  168. _Getval();
  169. return (_Tmp);
  170. }
  171. bool _Equal(const _Myt& _Right) const
  172. { // test for iterator equality
  173. return (_Myistr == _Right._Myistr);
  174. }
  175. protected:
  176. void _Getval()
  177. { // get a _Ty value if possible
  178. if (_Myistr != 0 && !(*_Myistr >> _Myval))
  179. _Myistr = 0;
  180. }
  181. istream_type *_Myistr; // pointer to input stream
  182. _Ty _Myval; // lookahead value (valid if _Myistr is not null)
  183. };
  184. // istream_iterator TEMPLATE OPERATORS
  185. template<class _Ty, class _Elem, class _Traits, class _Diff> inline
  186. bool operator==(
  187. const istream_iterator<_Ty, _Elem, _Traits, _Diff>& _Left,
  188. const istream_iterator<_Ty, _Elem, _Traits, _Diff>& _Right)
  189. { // test for istream_iterator equality
  190. return (_Left._Equal(_Right));
  191. }
  192. template<class _Ty, class _Elem, class _Traits, class _Diff> inline
  193. bool operator!=(
  194. const istream_iterator<_Ty, _Elem, _Traits, _Diff>& _Left,
  195. const istream_iterator<_Ty, _Elem, _Traits, _Diff>& _Right)
  196. { // test for istream_iterator inequality
  197. return (!(_Left == _Right));
  198. }
  199. // TEMPLATE CLASS ostream_iterator
  200. template<class _Ty, class _Elem = char,
  201. class _Traits = char_traits<_Elem> >
  202. class ostream_iterator
  203. : public _Outit
  204. { // wrap _Ty inserts to output stream as output iterator
  205. public:
  206. typedef _Elem char_type;
  207. typedef _Traits traits_type;
  208. typedef basic_ostream<_Elem, _Traits> ostream_type;
  209. ostream_iterator(ostream_type& _Ostr,
  210. const _Elem *_Delim = 0)
  211. : _Myostr(&_Ostr), _Mydelim(_Delim)
  212. { // construct from output stream and delimiter
  213. }
  214. ostream_iterator<_Ty, _Elem, _Traits>& operator=(const _Ty& _Val)
  215. { // insert value into output stream, followed by delimiter
  216. *_Myostr << _Val;
  217. if (_Mydelim != 0)
  218. *_Myostr << _Mydelim;
  219. return (*this);
  220. }
  221. ostream_iterator<_Ty, _Elem, _Traits>& operator*()
  222. { // pretend to return designated value
  223. return (*this);
  224. }
  225. ostream_iterator<_Ty, _Elem, _Traits>& operator++()
  226. { // pretend to preincrement
  227. return (*this);
  228. }
  229. ostream_iterator<_Ty, _Elem, _Traits> operator++(int)
  230. { // pretend to postincrement
  231. return (*this);
  232. }
  233. protected:
  234. const _Elem *_Mydelim; // pointer to delimiter string (NB: not freed)
  235. ostream_type *_Myostr; // pointer to output stream
  236. };
  237. // TEMPLATE FUNCTION _Val_type
  238. #if _HAS_PARTIAL_SPECIALIZATION
  239. template<class _Iter> inline
  240. typename iterator_traits<_Iter>::value_type *_Val_type(_Iter)
  241. { // return value type from arbitrary argument
  242. return (0);
  243. }
  244. #else /* _HAS_PARTIAL_SPECIALIZATION */
  245. template<class _Category, class _Ty, class _Diff,
  246. class _Pointer, class _Reference> inline
  247. _Ty *_Val_type(const iterator<_Category, _Ty, _Diff,
  248. _Pointer, _Reference>&)
  249. { // return value type from iterator argument
  250. return ((_Ty *)0);
  251. }
  252. template<class _Ty> inline
  253. _Ty *_Val_type(const _Ty *)
  254. { // return value type from pointer argument
  255. return ((_Ty *)0);
  256. }
  257. #endif /* _HAS_PARTIAL_SPECIALIZATION */
  258. // TEMPLATE FUNCTION advance
  259. template<class _InIt, class _Diff> inline
  260. void advance(_InIt& _Where, _Diff _Off)
  261. { // increment iterator by offset, arbitrary iterators
  262. _Advance(_Where, _Off, _Iter_cat(_Where));
  263. }
  264. template<class _InIt, class _Diff> inline
  265. void _Advance(_InIt& _Where, _Diff _Off, input_iterator_tag)
  266. { // increment iterator by offset, input iterators
  267. for (; 0 < _Off; --_Off)
  268. ++_Where;
  269. }
  270. template<class _FI, class _Diff> inline
  271. void _Advance(_FI& _Where, _Diff _Off, forward_iterator_tag)
  272. { // increment iterator by offset, forward iterators
  273. for (; 0 < _Off; --_Off)
  274. ++_Where;
  275. }
  276. template<class _BI, class _Diff> inline
  277. void _Advance(_BI& _Where, _Diff _Off, bidirectional_iterator_tag)
  278. { // increment iterator by offset, bidirectional iterators
  279. for (; 0 < _Off; --_Off)
  280. ++_Where;
  281. for (; _Off < 0; ++_Off)
  282. --_Where;
  283. }
  284. template<class _RI, class _Diff> inline
  285. void _Advance(_RI& _Where, _Diff _Off, random_access_iterator_tag)
  286. { // increment iterator by offset, random-access iterators
  287. _Where += _Off;
  288. }
  289. // TEMPLATE FUNCTION _Dist_type
  290. #if _HAS_PARTIAL_SPECIALIZATION
  291. template<class _Iter> inline
  292. typename iterator_traits<_Iter>::difference_type
  293. *_Dist_type(_Iter)
  294. { // return distance type from arbitrary argument
  295. return (0);
  296. }
  297. #else /* _HAS_PARTIAL_SPECIALIZATION */
  298. template<class _Category, class _Ty, class _Diff,
  299. class _Pointer, class _Reference> inline
  300. _Diff *_Dist_type(const iterator<_Category, _Ty, _Diff,
  301. _Pointer, _Reference>&)
  302. { // return distance type from iterator argument
  303. return ((_Diff *)0);
  304. }
  305. template<class _Ty> inline
  306. ptrdiff_t *_Dist_type(const _Ty *)
  307. { // return distance type from pointer argument
  308. return ((ptrdiff_t *)0);
  309. }
  310. #endif /* _HAS_PARTIAL_SPECIALIZATION */
  311. _STD_END
  312. #pragma warning(pop)
  313. #pragma pack(pop)
  314. #endif /* _ITERATOR_ */
  315. /*
  316. * Copyright (c) 1992-2001 by P.J. Plauger. ALL RIGHTS RESERVED.
  317. * Consult your license regarding permissions and restrictions.
  318. */
  319. /*
  320. * This file is derived from software bearing the following
  321. * restrictions:
  322. *
  323. * Copyright (c) 1994
  324. * Hewlett-Packard Company
  325. *
  326. * Permission to use, copy, modify, distribute and sell this
  327. * software and its documentation for any purpose is hereby
  328. * granted without fee, provided that the above copyright notice
  329. * appear in all copies and that both that copyright notice and
  330. * this permission notice appear in supporting documentation.
  331. * Hewlett-Packard Company makes no representations about the
  332. * suitability of this software for any purpose. It is provided
  333. * "as is" without express or implied warranty.
  334. V3.10:0009 */