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.

375 lines
9.5 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. template<class _Category, class _Ty, class _Diff,
  239. class _Pointer, class _Reference> inline
  240. _Ty *_Val_type(const iterator<_Category, _Ty, _Diff,
  241. _Pointer, _Reference>&)
  242. { // return value type from iterator argument
  243. return ((_Ty *)0);
  244. }
  245. template<class _Ty> inline
  246. _Ty *_Val_type(const _Ty *)
  247. { // return value type from pointer argument
  248. return ((_Ty *)0);
  249. }
  250. // TEMPLATE FUNCTION advance
  251. template<class _InIt, class _Diff> inline
  252. void advance(_InIt& _Where, _Diff _Off)
  253. { // increment iterator by offset, arbitrary iterators
  254. _Advance(_Where, _Off, _Iter_cat(_Where));
  255. }
  256. template<class _InIt, class _Diff> inline
  257. void _Advance(_InIt& _Where, _Diff _Off, input_iterator_tag)
  258. { // increment iterator by offset, input iterators
  259. for (; 0 < _Off; --_Off)
  260. ++_Where;
  261. }
  262. template<class _FI, class _Diff> inline
  263. void _Advance(_FI& _Where, _Diff _Off, forward_iterator_tag)
  264. { // increment iterator by offset, forward iterators
  265. for (; 0 < _Off; --_Off)
  266. ++_Where;
  267. }
  268. template<class _BI, class _Diff> inline
  269. void _Advance(_BI& _Where, _Diff _Off, bidirectional_iterator_tag)
  270. { // increment iterator by offset, bidirectional iterators
  271. for (; 0 < _Off; --_Off)
  272. ++_Where;
  273. for (; _Off < 0; ++_Off)
  274. --_Where;
  275. }
  276. template<class _RI, class _Diff> inline
  277. void _Advance(_RI& _Where, _Diff _Off, random_access_iterator_tag)
  278. { // increment iterator by offset, random-access iterators
  279. _Where += _Off;
  280. }
  281. // TEMPLATE FUNCTION _Dist_type
  282. template<class _Category, class _Ty, class _Diff,
  283. class _Pointer, class _Reference> inline
  284. _Diff *_Dist_type(const iterator<_Category, _Ty, _Diff,
  285. _Pointer, _Reference>&)
  286. { // return distance type from iterator argument
  287. return ((_Diff *)0);
  288. }
  289. template<class _Ty> inline
  290. ptrdiff_t *_Dist_type(const _Ty *)
  291. { // return distance type from pointer argument
  292. return ((ptrdiff_t *)0);
  293. }
  294. _STD_END
  295. #pragma warning(pop)
  296. #pragma pack(pop)
  297. #endif /* _ITERATOR_ */
  298. /*
  299. * Copyright (c) 1992-2001 by P.J. Plauger. ALL RIGHTS RESERVED.
  300. * Consult your license regarding permissions and restrictions.
  301. */
  302. /*
  303. * This file is derived from software bearing the following
  304. * restrictions:
  305. *
  306. * Copyright (c) 1994
  307. * Hewlett-Packard Company
  308. *
  309. * Permission to use, copy, modify, distribute and sell this
  310. * software and its documentation for any purpose is hereby
  311. * granted without fee, provided that the above copyright notice
  312. * appear in all copies and that both that copyright notice and
  313. * this permission notice appear in supporting documentation.
  314. * Hewlett-Packard Company makes no representations about the
  315. * suitability of this software for any purpose. It is provided
  316. * "as is" without express or implied warranty.
  317. V3.10:0009 */