int_iterator.hpp 2.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182
  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. #if !defined BOOST_MSVC
  8. #include <boost/operators.hpp>
  9. #endif
  10. #include <iostream>
  11. #include <iterator>
  12. #include <cstddef>
  13. //using namespace std;
  14. #ifndef BOOST_NO_OPERATORS_IN_NAMESPACE
  15. namespace boost {
  16. namespace iterators {
  17. #endif
  18. // this should use random_access_iterator_helper but I've had
  19. // VC++ portablility problems with that. -JGS
  20. template <class IntT>
  21. class int_iterator
  22. {
  23. typedef int_iterator self;
  24. public:
  25. typedef std::random_access_iterator_tag iterator_category;
  26. typedef IntT value_type;
  27. typedef IntT& reference;
  28. typedef IntT* pointer;
  29. typedef std::ptrdiff_t difference_type;
  30. inline int_iterator() : _i(0) { }
  31. inline int_iterator(IntT i) : _i(i) { }
  32. inline int_iterator(const self& x) : _i(x._i) { }
  33. inline self& operator=(const self& x) { _i = x._i; return *this; }
  34. inline IntT operator*() { return _i; }
  35. inline IntT operator[](IntT n) { return _i + n; }
  36. inline self& operator++() { ++_i; return *this; }
  37. inline self operator++(int) { self t = *this; ++_i; return t; }
  38. inline self& operator+=(IntT n) { _i += n; return *this; }
  39. inline self operator+(IntT n) { self t = *this; t += n; return t; }
  40. inline self& operator--() { --_i; return *this; }
  41. inline self operator--(int) { self t = *this; --_i; return t; }
  42. inline self& operator-=(IntT n) { _i -= n; return *this; }
  43. inline IntT operator-(const self& x) const { return _i - x._i; }
  44. inline bool operator==(const self& x) const { return _i == x._i; }
  45. // vc++ had a problem finding != in random_access_iterator_helper
  46. // need to look into this... for now implementing everything here -JGS
  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. inline bool operator>(const self& x) const { return _i > x._i; }
  51. inline bool operator>=(const self& x) const { return _i >= x._i; }
  52. protected:
  53. IntT _i;
  54. };
  55. template <class IntT>
  56. inline int_iterator<IntT>
  57. operator+(IntT n, int_iterator<IntT> t) { t += n; return t; }
  58. #ifndef BOOST_NO_OPERATORS_IN_NAMESPACE
  59. } /* namespace iterators */
  60. using iterators::int_iterator;
  61. } /* namespace boost */
  62. #endif
  63. #ifdef BOOST_NO_OPERATORS_IN_NAMESPACE
  64. namespace boost {
  65. using ::int_iterator;
  66. namespace iterators {
  67. using ::int_iterator;
  68. }}
  69. #endif
  70. #endif /* BOOST_INT_ITERATOR_H */