int_iterator.hpp 2.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374
  1. // (C) Copyright Jeremy Siek 1999.
  2. // Distributed under the Boost Software License, Version 1.0. (See
  3. // accompanying file LICENSE_1_0.txt or copy at
  4. // http://www.boost.org/LICENSE_1_0.txt)
  5. #ifndef BOOST_INT_ITERATOR_H
  6. #define BOOST_INT_ITERATOR_H
  7. #include <boost/iterator.hpp>
  8. #if !defined BOOST_MSVC
  9. #include <boost/operators.hpp>
  10. #endif
  11. #include <iostream>
  12. //using namespace std;
  13. #ifndef BOOST_NO_OPERATORS_IN_NAMESPACE
  14. namespace boost {
  15. #endif
  16. // this should use random_access_iterator_helper but I've had
  17. // VC++ portablility problems with that. -JGS
  18. template <class IntT>
  19. class int_iterator
  20. {
  21. typedef int_iterator self;
  22. public:
  23. typedef std::random_access_iterator_tag iterator_category;
  24. typedef IntT value_type;
  25. typedef IntT& reference;
  26. typedef IntT* pointer;
  27. typedef std::ptrdiff_t difference_type;
  28. inline int_iterator() : _i(0) { }
  29. inline int_iterator(IntT i) : _i(i) { }
  30. inline int_iterator(const self& x) : _i(x._i) { }
  31. inline self& operator=(const self& x) { _i = x._i; return *this; }
  32. inline IntT operator*() { return _i; }
  33. inline IntT operator[](IntT n) { return _i + n; }
  34. inline self& operator++() { ++_i; return *this; }
  35. inline self operator++(int) { self t = *this; ++_i; return t; }
  36. inline self& operator+=(IntT n) { _i += n; return *this; }
  37. inline self operator+(IntT n) { self t = *this; t += n; return t; }
  38. inline self& operator--() { --_i; return *this; }
  39. inline self operator--(int) { self t = *this; --_i; return t; }
  40. inline self& operator-=(IntT n) { _i -= n; return *this; }
  41. inline IntT operator-(const self& x) const { return _i - x._i; }
  42. inline bool operator==(const self& x) const { return _i == x._i; }
  43. // vc++ had a problem finding != in random_access_iterator_helper
  44. // need to look into this... for now implementing everything here -JGS
  45. inline bool operator!=(const self& x) const { return _i != x._i; }
  46. inline bool operator<(const self& x) const { return _i < x._i; }
  47. inline bool operator<=(const self& x) const { return _i <= x._i; }
  48. inline bool operator>(const self& x) const { return _i > x._i; }
  49. inline bool operator>=(const self& x) const { return _i >= x._i; }
  50. protected:
  51. IntT _i;
  52. };
  53. template <class IntT>
  54. inline int_iterator<IntT>
  55. operator+(IntT n, int_iterator<IntT> t) { t += n; return t; }
  56. #ifndef BOOST_NO_OPERATORS_IN_NAMESPACE
  57. } /* namespace boost */
  58. #endif
  59. #ifdef BOOST_NO_OPERATORS_IN_NAMESPACE
  60. namespace boost {
  61. using ::int_iterator;
  62. }
  63. #endif
  64. #endif /* BOOST_INT_ITERATOR_H */