// iterator standard header
#pragma once
#ifndef _ITERATOR_
#define _ITERATOR_
#ifndef RC_INVOKED
#include <istream>
#include <xutility>
#pragma pack(push,_CRT_PACKING)
#pragma warning(push,3)
_STD_BEGIN
// TEMPLATE CLASS back_insert_iterator
template<class _Container>
class back_insert_iterator
: public _Outit
{ // wrap pushes to back of container as output iterator
public:
typedef back_insert_iterator<_Container> _Myt;
typedef _Container container_type;
typedef typename _Container::const_reference const_reference;
typedef typename _Container::value_type _Valty;
explicit back_insert_iterator(_Container& _Cont)
: container(&_Cont)
{ // construct with container
}
_Myt& operator=(const _Valty& _Val)
{ // push value into container
container->push_back(_Val);
return (*this);
}
_Myt& operator=(_Valty&& _Val)
{ // push value into container
container->push_back(_STD forward<_Valty>(_Val));
return (*this);
}
_Myt& operator*()
{ // pretend to return designated value
return (*this);
}
_Myt& operator++()
{ // pretend to preincrement
return (*this);
}
_Myt operator++(int)
{ // pretend to postincrement
return (*this);
}
protected:
_Container *container; // pointer to container
};
template<class _Container>
struct _Is_checked_helper<back_insert_iterator<_Container> >
: public _STD tr1::true_type
{ // mark back_insert_iterator as checked
};
// TEMPLATE FUNCTION back_inserter
template<class _Container> inline
back_insert_iterator<_Container> back_inserter(_Container& _Cont)
{ // return a back_insert_iterator
return (_STD back_insert_iterator<_Container>(_Cont));
}
// TEMPLATE CLASS front_insert_iterator
template<class _Container>
class front_insert_iterator
: public _Outit
{ // wrap pushes to front of container as output iterator
public:
typedef front_insert_iterator<_Container> _Myt;
typedef _Container container_type;
typedef typename _Container::const_reference const_reference;
typedef typename _Container::value_type _Valty;
explicit front_insert_iterator(_Container& _Cont)
: container(&_Cont)
{ // construct with container
}
_Myt& operator=(const _Valty& _Val)
{ // push value into container
container->push_front(_Val);
return (*this);
}
_Myt& operator=(_Valty&& _Val)
{ // push value into container
container->push_front(_STD forward<_Valty>(_Val));
return (*this);
}
_Myt& operator*()
{ // pretend to return designated value
return (*this);
}
_Myt& operator++()
{ // pretend to preincrement
return (*this);
}
_Myt operator++(int)
{ // pretend to postincrement
return (*this);
}
protected:
_Container *container; // pointer to container
};
template<class _Container>
struct _Is_checked_helper<front_insert_iterator<_Container> >
: public _STD tr1::true_type
{ // mark front_insert_iterator as checked
};
// TEMPLATE FUNCTION front_inserter
template<class _Container> inline
front_insert_iterator<_Container> front_inserter(_Container& _Cont)
{ // return front_insert_iterator
return (_STD front_insert_iterator<_Container>(_Cont));
}
// TEMPLATE CLASS insert_iterator
template<class _Container>
class insert_iterator
: public _Outit
{ // wrap inserts into container as output iterator
public:
typedef insert_iterator<_Container> _Myt;
typedef _Container container_type;
typedef typename _Container::const_reference const_reference;
typedef typename _Container::value_type _Valty;
insert_iterator(_Container& _Cont, typename _Container::iterator _Where)
: container(&_Cont), iter(_Where)
{ // construct with container and iterator
}
_Myt& operator=(const _Valty& _Val)
{ // insert into container and increment stored iterator
iter = container->insert(iter, _Val);
++iter;
return (*this);
}
_Myt& operator=(_Valty&& _Val)
{ // push value into container
iter = container->insert(iter, _STD forward<_Valty>(_Val));
++iter;
return (*this);
}
_Myt& operator*()
{ // pretend to return designated value
return (*this);
}
_Myt& operator++()
{ // pretend to preincrement
return (*this);
}
_Myt& operator++(int)
{ // pretend to postincrement
return (*this);
}
protected:
_Container *container; // pointer to container
typename _Container::iterator iter; // iterator into container
};
template<class _Container>
struct _Is_checked_helper<insert_iterator<_Container> >
: public _STD tr1::true_type
{ // mark insert_iterator as checked
};
// TEMPLATE FUNCTION inserter
template<class _Container> inline
insert_iterator<_Container> inserter(_Container& _Cont,
typename _Container::iterator _Where)
{ // return insert_iterator
return (_STD insert_iterator<_Container>(_Cont, _Where));
}
// TEMPLATE CLASS move_iterator
template<class _RanIt>
class move_iterator
{ // wrap iterator to move rvalues
public:
typedef move_iterator<_RanIt> _Myt;
typedef typename iterator_traits<_RanIt>::iterator_category
iterator_category;
typedef typename iterator_traits<_RanIt>::value_type
value_type;
typedef typename iterator_traits<_RanIt>::difference_type
difference_type;
typedef _RanIt pointer;
typedef value_type&& reference;
typedef _RanIt iterator_type;
move_iterator()
{ // construct with default wrapped iterator
}
explicit move_iterator(iterator_type _Right)
: current(_Right)
{ // construct wrapped iterator from _Right
}
template<class _RanIt2>
move_iterator(const move_iterator<_RanIt2>& _Right)
: current(_Right.base())
{ // initialize with compatible base
}
template<class _RanIt2>
_Myt& operator=(const move_iterator<_RanIt2>& _Right)
{ // assign with compatible base
current = _Right.base();
return (*this);
}
_RanIt base() const
{ // return wrapped iterator
return (current);
}
reference operator*() const
{ // return designated value
return (move(*current));
}
pointer operator->() const
{ // return pointer to class object
return (&**this);
}
_Myt& operator++()
{ // preincrement
++current;
return (*this);
}
_Myt operator++(int)
{ // postincrement
_Myt _Tmp = *this;
++current;
return (_Tmp);
}
_Myt& operator--()
{ // predecrement
--current;
return (*this);
}
_Myt operator--(int)
{ // postdecrement
_Myt _Tmp = *this;
--current;
return (_Tmp);
}
template<class _RanIt2>
bool _Equal(const move_iterator<_RanIt2>& _Right) const
{ // test for iterator equality
return (current == _Right.base());
}
// N.B. functions valid for random-access iterators only beyond this point
_Myt& operator+=(difference_type _Off)
{ // increment by integer
current += _Off;
return (*this);
}
_Myt operator+(difference_type _Off) const
{ // return this + integer
return (_Myt(current + _Off));
}
_Myt& operator-=(difference_type _Off)
{ // decrement by integer
current -= _Off;
return (*this);
}
_Myt operator-(difference_type _Off) const
{ // return this - integer
return (_Myt(current - _Off));
}
reference operator[](difference_type _Off) const
{ // subscript
return (move(current[_Off]));
}
template<class _RanIt2>
bool _Less(const move_iterator<_RanIt2>& _Right) const
{ // test if this < _Right
return (current < _Right.base());
}
difference_type operator-(const _Myt& _Right) const
{ // return difference of iterators
return (current - _Right.base());
}
protected:
iterator_type current; // the wrapped iterator
};
template<class _RanIt>
struct _Is_checked_helper<move_iterator<_RanIt> >
: public _Is_checked_helper<_RanIt>
{ // mark move_iterator as checked if its underlying iterator is checked
};
// move_iterator TEMPLATE OPERATORS
template<class _RanIt,
class _Diff> inline
move_iterator<_RanIt>
operator+(_Diff _Off,
const move_iterator<_RanIt>& _Right)
{ // return move_iterator + integer
return (_Right + _Off);
}
template<class _RanIt1,
class _RanIt2> inline
typename _RanIt1::difference_type operator-(
move_iterator<_RanIt1>& _Left,
const move_iterator<_RanIt2>& _Right)
{ // test for move_iterator equality
return (_Left.base() - _Right.base());
}
template<class _RanIt1,
class _RanIt2> inline
bool operator==(
const move_iterator<_RanIt1>& _Left,
const move_iterator<_RanIt2>& _Right)
{ // test for move_iterator equality
return (_Left._Equal(_Right));
}
template<class _RanIt1,
class _RanIt2> inline
bool operator!=(
const move_iterator<_RanIt1>& _Left,
const move_iterator<_RanIt2>& _Right)
{ // test for move_iterator inequality
return (!(_Left == _Right));
}
template<class _RanIt1,
class _RanIt2> inline
bool operator<(
const move_iterator<_RanIt1>& _Left,
const move_iterator<_RanIt2>& _Right)
{ // test for move_iterator < move_iterator
return (_Left._Less(_Right));
}
template<class _RanIt1,
class _RanIt2> inline
bool operator>(
const move_iterator<_RanIt1>& _Left,
const move_iterator<_RanIt2>& _Right)
{ // test for move_iterator > move_iterator
return (_Right < _Left);
}
template<class _RanIt1,
class _RanIt2> inline
bool operator<=(
const move_iterator<_RanIt1>& _Left,
const move_iterator<_RanIt2>& _Right)
{ // test for move_iterator <= move_iterator
return (!(_Right < _Left));
}
template<class _RanIt1,
class _RanIt2> inline
bool operator>=(
const move_iterator<_RanIt1>& _Left,
const move_iterator<_RanIt2>& _Right)
{ // test for move_iterator >= move_iterator
return (!(_Left < _Right));
}
// TEMPLATE FUNCTION make_move_iterator
template<class _RanIt> inline
move_iterator<_RanIt> make_move_iterator(const _RanIt _Iter)
{ // make move_iterator from iterator
return (move_iterator<_RanIt>(_Iter));
}
// TEMPLATE CLASS istream_iterator
template<class _Ty,
class _Elem = char,
class _Traits = char_traits<_Elem>,
class _Diff = ptrdiff_t>
class istream_iterator
: public iterator<input_iterator_tag, _Ty, _Diff,
const _Ty *, const _Ty&>
{ // wrap _Ty extracts from input stream as input iterator
typedef istream_iterator<_Ty, _Elem, _Traits, _Diff> _Myt;
public:
typedef _Elem char_type;
typedef _Traits traits_type;
typedef basic_istream<_Elem, _Traits> istream_type;
istream_iterator()
: _Myistr(0)
{ // construct singular iterator
}
istream_iterator(istream_type& _Istr)
: _Myistr(&_Istr)
{ // construct with input stream
_Getval();
}
const _Ty& operator*() const
{ // return designated value
return (_Myval);
}
const _Ty *operator->() const
{ // return pointer to class object
return (&**this);
}
_Myt& operator++()
{ // preincrement
_Getval();
return (*this);
}
_Myt operator++(int)
{ // postincrement
_Myt _Tmp = *this;
++*this;
return (_Tmp);
}
bool _Equal(const _Myt& _Right) const
{ // test for iterator equality
return (_Myistr == _Right._Myistr);
}
protected:
void _Getval()
{ // get a _Ty value if possible
if (_Myistr != 0 && !(*_Myistr >> _Myval))
_Myistr = 0;
}
istream_type *_Myistr; // pointer to input stream
_Ty _Myval; // lookahead value (valid if _Myistr is not null)
};
template<class _Ty,
class _Elem,
class _Traits,
class _Diff>
struct _Is_checked_helper<istream_iterator<_Ty, _Elem, _Traits, _Diff> >
: public _STD tr1::true_type
{ // mark istream_iterator as checked
};
// istream_iterator TEMPLATE OPERATORS
template<class _Ty,
class _Elem,
class _Traits,
class _Diff> inline
bool operator==(
const istream_iterator<_Ty, _Elem, _Traits, _Diff>& _Left,
const istream_iterator<_Ty, _Elem, _Traits, _Diff>& _Right)
{ // test for istream_iterator equality
return (_Left._Equal(_Right));
}
template<class _Ty,
class _Elem,
class _Traits,
class _Diff> inline
bool operator!=(
const istream_iterator<_Ty, _Elem, _Traits, _Diff>& _Left,
const istream_iterator<_Ty, _Elem, _Traits, _Diff>& _Right)
{ // test for istream_iterator inequality
return (!(_Left == _Right));
}
// TEMPLATE CLASS ostream_iterator
template<class _Ty,
class _Elem = char,
class _Traits = char_traits<_Elem> >
class ostream_iterator
: public _Outit
{ // wrap _Ty inserts to output stream as output iterator
public:
typedef _Elem char_type;
typedef _Traits traits_type;
typedef basic_ostream<_Elem, _Traits> ostream_type;
ostream_iterator(ostream_type& _Ostr,
const _Elem *_Delim = 0)
: _Myostr(&_Ostr), _Mydelim(_Delim)
{ // construct from output stream and delimiter
}
ostream_iterator<_Ty, _Elem, _Traits>& operator=(const _Ty& _Val)
{ // insert value into output stream, followed by delimiter
*_Myostr << _Val;
if (_Mydelim != 0)
*_Myostr << _Mydelim;
return (*this);
}
ostream_iterator<_Ty, _Elem, _Traits>& operator*()
{ // pretend to return designated value
return (*this);
}
ostream_iterator<_Ty, _Elem, _Traits>& operator++()
{ // pretend to preincrement
return (*this);
}
ostream_iterator<_Ty, _Elem, _Traits> operator++(int)
{ // pretend to postincrement
return (*this);
}
protected:
const _Elem *_Mydelim; // pointer to delimiter string (NB: not freed)
ostream_type *_Myostr; // pointer to output stream
};
template<class _Ty,
class _Elem,
class _Traits>
struct _Is_checked_helper<ostream_iterator<_Ty, _Elem, _Traits> >
: public _STD tr1::true_type
{ // mark ostream_iterator as checked
};
_STD_END
_STDEXT_BEGIN
// TEMPLATE CLASS checked_array_iterator
template<class _Iterator>
class checked_array_iterator
{ // wrap an iterator (actually, a pointer) with checking
public:
typedef checked_array_iterator<_Iterator> _Myt;
typedef typename _STD iterator_traits<_Iterator>::iterator_category iterator_category;
typedef typename _STD iterator_traits<_Iterator>::value_type value_type;
typedef typename _STD iterator_traits<_Iterator>::difference_type difference_type;
typedef typename _STD iterator_traits<_Iterator>::difference_type distance_type; // retained
typedef typename _STD iterator_traits<_Iterator>::pointer pointer;
typedef typename _STD iterator_traits<_Iterator>::reference reference;
checked_array_iterator()
: _Myarray(), _Mysize(0), _Myindex(0)
{ // default construct
}
checked_array_iterator(_Iterator _Array, _STD size_t _Size, _STD size_t _Index = 0)
: _Myarray(_Array), _Mysize(_Size), _Myindex(_Index)
{ // construct with array, size, and optional index
_SCL_SECURE_ALWAYS_VALIDATE(_Index <= _Size);
}
_Iterator base() const
{ // return unwrapped iterator
return (_Myarray + _Myindex);
}
typedef _Iterator _Unchecked_type;
_Myt& _Rechecked(_Unchecked_type _Right)
{ // reset from unchecked iterator
_Myindex = _Right - _Myarray;
return (*this);
}
_Unchecked_type _Unchecked() const
{ // make an unchecked iterator
return (base());
}
reference operator*() const
{ // return designated object
_SCL_SECURE_ALWAYS_VALIDATE_RANGE(_Myarray != 0 && _Myindex < _Mysize);
return (_Myarray[_Myindex]);
}
pointer operator->() const
{ // return pointer to class object
return (&**this);
}
_Myt& operator++()
{ // preincrement
_SCL_SECURE_ALWAYS_VALIDATE_RANGE(_Myarray != 0 && _Myindex < _Mysize);
++_Myindex;
return (*this);
}
_Myt operator++(int)
{ // postincrement
_Myt _Tmp = *this;
++*this;
return (_Tmp);
}
_Myt& operator--()
{ // predecrement
_SCL_SECURE_ALWAYS_VALIDATE_RANGE(_Myarray != 0 && _Myindex > 0);
--_Myindex;
return (*this);
}
_Myt operator--(int)
{ // postdecrement
_Myt _Tmp = *this;
--*this;
return (_Tmp);
}
_Myt& operator+=(difference_type _Off)
{ // increment by integer
_SCL_SECURE_ALWAYS_VALIDATE_RANGE(_Myarray != 0 && _Myindex + _Off <= _Mysize);
_Myindex += _Off;
return (*this);
}
_Myt operator+(difference_type _Off) const
{ // return this + integer
_Myt _Tmp = *this;
return (_Tmp += _Off);
}
_Myt& operator-=(difference_type _Off)
{ // decrement by integer
return (*this += -_Off);
}
_Myt operator-(difference_type _Off) const
{ // return this - integer
_Myt _Tmp = *this;
return (_Tmp -= _Off);
}
difference_type operator-(const _Myt& _Right) const
{ // return difference of iterators
_SCL_SECURE_ALWAYS_VALIDATE(_Myarray == _Right._Myarray);
return (_Myindex - _Right._Myindex);
}
reference operator[](difference_type _Off) const
{ // subscript
return (*(*this + _Off));
}
bool operator==(const _Myt& _Right) const
{ // test for iterator equality
_SCL_SECURE_ALWAYS_VALIDATE(_Myarray == _Right._Myarray);
return (_Myindex == _Right._Myindex);
}
bool operator!=(const _Myt& _Right) const
{ // test for iterator inequality
return (!(*this == _Right));
}
bool operator<(const _Myt& _Right) const
{ // test if this < _Right
_SCL_SECURE_ALWAYS_VALIDATE(_Myarray == _Right._Myarray);
return (_Myindex < _Right._Myindex);
}
bool operator>(const _Myt& _Right) const
{ // test if this > _Right
return (_Right < *this);
}
bool operator<=(const _Myt& _Right) const
{ // test if this <= _Right
return (!(_Right < *this));
}
bool operator>=(const _Myt& _Right) const
{ // test if this >= _Right
return (!(*this < _Right));
}
private:
_Iterator _Myarray; // beginning of array
_STD size_t _Mysize; // size of array
_STD size_t _Myindex; // offset into array
};
template<class _Iterator> inline
typename checked_array_iterator<_Iterator>::_Unchecked_type
_Unchecked(checked_array_iterator<_Iterator> _Iter)
{ // convert to unchecked
return (_Iter._Unchecked());
}
template<class _Iterator> inline
checked_array_iterator<_Iterator>&
_Rechecked(checked_array_iterator<_Iterator>& _Iter,
typename checked_array_iterator<_Iterator>
::_Unchecked_type _Right)
{ // convert to checked
return (_Iter._Rechecked(_Right));
}
template<class _Iterator> inline
checked_array_iterator<_Iterator> operator+(
typename checked_array_iterator<_Iterator>::difference_type _Off,
checked_array_iterator<_Iterator> _Next)
{ // add offset to iterator
return (_Next += _Off);
}
template<class _Iterator> inline
checked_array_iterator<_Iterator> make_checked_array_iterator(
_Iterator _Array, _STD size_t _Size, _STD size_t _Index = 0)
{ // construct with array, size, and optional index
return (checked_array_iterator<_Iterator>(_Array, _Size, _Index));
}
// TEMPLATE CLASS unchecked_array_iterator
template<class _Iterator>
class unchecked_array_iterator
{ // wrap an iterator (actually, a pointer) without checking, in order to silence warnings
public:
typedef unchecked_array_iterator<_Iterator> _Myt;
typedef typename _STD iterator_traits<_Iterator>::iterator_category iterator_category;
typedef typename _STD iterator_traits<_Iterator>::value_type value_type;
typedef typename _STD iterator_traits<_Iterator>::difference_type difference_type;
typedef typename _STD iterator_traits<_Iterator>::difference_type distance_type; // retained
typedef typename _STD iterator_traits<_Iterator>::pointer pointer;
typedef typename _STD iterator_traits<_Iterator>::reference reference;
unchecked_array_iterator()
: _Myptr()
{ // default construct
}
explicit unchecked_array_iterator(_Iterator _Ptr)
: _Myptr(_Ptr)
{ // construct with pointer
}
_Iterator base() const
{ // return unwrapped iterator
return (_Myptr);
}
typedef _Iterator _Unchecked_type;
_Myt& _Rechecked(_Unchecked_type _Right)
{ // reset from unchecked iterator
_Myptr = _Right;
return (*this);
}
_Unchecked_type _Unchecked() const
{ // make an unchecked iterator
return (base());
}
reference operator*() const
{ // return designated object
return (*_Myptr);
}
pointer operator->() const
{ // return pointer to class object
return (&**this);
}
_Myt& operator++()
{ // preincrement
++_Myptr;
return (*this);
}
_Myt operator++(int)
{ // postincrement
_Myt _Tmp = *this;
++*this;
return (_Tmp);
}
_Myt& operator--()
{ // predecrement
--_Myptr;
return (*this);
}
_Myt operator--(int)
{ // postdecrement
_Myt _Tmp = *this;
--*this;
return (_Tmp);
}
_Myt& operator+=(difference_type _Off)
{ // increment by integer
_Myptr += _Off;
return (*this);
}
_Myt operator+(difference_type _Off) const
{ // return this + integer
_Myt _Tmp = *this;
return (_Tmp += _Off);
}
_Myt& operator-=(difference_type _Off)
{ // decrement by integer
return (*this += -_Off);
}
_Myt operator-(difference_type _Off) const
{ // return this - integer
_Myt _Tmp = *this;
return (_Tmp -= _Off);
}
difference_type operator-(const _Myt& _Right) const
{ // return difference of iterators
return (_Myptr - _Right._Myptr);
}
reference operator[](difference_type _Off) const
{ // subscript
return (*(*this + _Off));
}
bool operator==(const _Myt& _Right) const
{ // test for iterator equality
return (_Myptr == _Right._Myptr);
}
bool operator!=(const _Myt& _Right) const
{ // test for iterator inequality
return (!(*this == _Right));
}
bool operator<(const _Myt& _Right) const
{ // test if this < _Right
return (_Myptr < _Right._Myptr);
}
bool operator>(const _Myt& _Right) const
{ // test if this > _Right
return (_Right < *this);
}
bool operator<=(const _Myt& _Right) const
{ // test if this <= _Right
return (!(_Right < *this));
}
bool operator>=(const _Myt& _Right) const
{ // test if this >= _Right
return (!(*this < _Right));
}
private:
_Iterator _Myptr; // underlying pointer
};
template<class _Iterator> inline
typename unchecked_array_iterator<_Iterator>::_Unchecked_type
_Unchecked(unchecked_array_iterator<_Iterator> _Iter)
{ // convert to unchecked
return (_Iter._Unchecked());
}
template<class _Iterator> inline
unchecked_array_iterator<_Iterator>&
_Rechecked(unchecked_array_iterator<_Iterator>& _Iter,
typename unchecked_array_iterator<_Iterator>
::_Unchecked_type _Right)
{ // convert to checked
return (_Iter._Rechecked(_Right));
}
template<class _Iterator> inline
unchecked_array_iterator<_Iterator> operator+(
typename unchecked_array_iterator<_Iterator>::difference_type _Off,
unchecked_array_iterator<_Iterator> _Next)
{ // add offset to iterator
return (_Next += _Off);
}
template<class _Iterator> inline
unchecked_array_iterator<_Iterator> make_unchecked_array_iterator(
_Iterator _Ptr)
{ // construct with pointer
return (unchecked_array_iterator<_Iterator>(_Ptr));
}
_STDEXT_END
#pragma warning(pop)
#pragma pack(pop)
#endif /* RC_INVOKED */
#endif /* _ITERATOR_ */
/*
* This file is derived from software bearing the following
* restrictions:
*
* Copyright (c) 1994
* Hewlett-Packard Company
*
* Permission to use, copy, modify, distribute and sell this
* software and its documentation for any purpose is hereby
* granted without fee, provided that the above copyright notice
* appear in all copies and that both that copyright notice and
* this permission notice appear in supporting documentation.
* Hewlett-Packard Company makes no representations about the
* suitability of this software for any purpose. It is provided
* "as is" without express or implied warranty.
*/
/*
* Copyright (c) 1992-2009 by P.J. Plauger. ALL RIGHTS RESERVED.
* Consult your license regarding permissions and restrictions.
V5.20:0009 */