iterator.hpp 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172
  1. // Copyright 2002 The Trustees of Indiana University.
  2. // Use, modification and distribution is subject to the Boost Software
  3. // License, Version 1.0. (See accompanying file LICENSE_1_0.txt or copy at
  4. // http://www.boost.org/LICENSE_1_0.txt)
  5. // Boost.MultiArray Library
  6. // Authors: Ronald Garcia
  7. // Jeremy Siek
  8. // Andrew Lumsdaine
  9. // See http://www.boost.org/libs/multi_array for documentation.
  10. #ifndef ITERATOR_RG071801_HPP
  11. #define ITERATOR_RG071801_HPP
  12. //
  13. // iterator.hpp - implementation of iterators for the
  14. // multi-dimensional array class
  15. //
  16. #include "boost/multi_array/base.hpp"
  17. #include "boost/iterator/iterator_facade.hpp"
  18. #include "boost/mpl/aux_/msvc_eti_base.hpp"
  19. #include <algorithm>
  20. #include <cstddef>
  21. #include <iterator>
  22. namespace boost {
  23. namespace detail {
  24. namespace multi_array {
  25. /////////////////////////////////////////////////////////////////////////
  26. // iterator components
  27. /////////////////////////////////////////////////////////////////////////
  28. template <class T>
  29. struct operator_arrow_proxy
  30. {
  31. operator_arrow_proxy(T const& px) : value_(px) {}
  32. T* operator->() const { return &value_; }
  33. // This function is needed for MWCW and BCC, which won't call operator->
  34. // again automatically per 13.3.1.2 para 8
  35. operator T*() const { return &value_; }
  36. mutable T value_;
  37. };
  38. template <typename T, typename TPtr, typename NumDims, typename Reference,
  39. typename IteratorCategory>
  40. class array_iterator;
  41. template <typename T, typename TPtr, typename NumDims, typename Reference,
  42. typename IteratorCategory>
  43. class array_iterator
  44. : public
  45. iterator_facade<
  46. array_iterator<T,TPtr,NumDims,Reference,IteratorCategory>
  47. , typename associated_types<T,NumDims>::value_type
  48. , IteratorCategory
  49. , Reference
  50. >
  51. , private
  52. #if BOOST_WORKAROUND(BOOST_MSVC, < 1300)
  53. mpl::aux::msvc_eti_base<typename
  54. #endif
  55. value_accessor_generator<T,NumDims>::type
  56. #if BOOST_WORKAROUND(BOOST_MSVC, < 1300)
  57. >::type
  58. #endif
  59. {
  60. friend class iterator_core_access;
  61. typedef detail::multi_array::associated_types<T,NumDims> access_t;
  62. typedef iterator_facade<
  63. array_iterator<T,TPtr,NumDims,Reference,IteratorCategory>
  64. , typename detail::multi_array::associated_types<T,NumDims>::value_type
  65. , boost::random_access_traversal_tag
  66. , Reference
  67. > facade_type;
  68. typedef typename access_t::index index;
  69. typedef typename access_t::size_type size_type;
  70. #ifndef BOOST_NO_MEMBER_TEMPLATE_FRIENDS
  71. template <typename, typename, typename, typename, typename>
  72. friend class array_iterator;
  73. #else
  74. public:
  75. #endif
  76. index idx_;
  77. TPtr base_;
  78. const size_type* extents_;
  79. const index* strides_;
  80. const index* index_base_;
  81. public:
  82. // Typedefs to circumvent ambiguities between parent classes
  83. typedef typename facade_type::reference reference;
  84. typedef typename facade_type::value_type value_type;
  85. typedef typename facade_type::difference_type difference_type;
  86. array_iterator() {}
  87. array_iterator(index idx, TPtr base, const size_type* extents,
  88. const index* strides,
  89. const index* index_base) :
  90. idx_(idx), base_(base), extents_(extents),
  91. strides_(strides), index_base_(index_base) { }
  92. template <typename OPtr, typename ORef, typename Cat>
  93. array_iterator(
  94. const array_iterator<T,OPtr,NumDims,ORef,Cat>& rhs
  95. , typename boost::enable_if_convertible<OPtr,TPtr>::type* = 0
  96. )
  97. : idx_(rhs.idx_), base_(rhs.base_), extents_(rhs.extents_),
  98. strides_(rhs.strides_), index_base_(rhs.index_base_) { }
  99. // RG - we make our own operator->
  100. operator_arrow_proxy<reference>
  101. operator->() const
  102. {
  103. return operator_arrow_proxy<reference>(this->dereference());
  104. }
  105. reference dereference() const
  106. {
  107. typedef typename value_accessor_generator<T,NumDims>::type accessor;
  108. return accessor::access(boost::type<reference>(),
  109. idx_,
  110. base_,
  111. extents_,
  112. strides_,
  113. index_base_);
  114. }
  115. void increment() { ++idx_; }
  116. void decrement() { --idx_; }
  117. template <class IteratorAdaptor>
  118. bool equal(IteratorAdaptor& rhs) const {
  119. const std::size_t N = NumDims::value;
  120. return (idx_ == rhs.idx_) &&
  121. (base_ == rhs.base_) &&
  122. ( (extents_ == rhs.extents_) ||
  123. std::equal(extents_,extents_+N,rhs.extents_) ) &&
  124. ( (strides_ == rhs.strides_) ||
  125. std::equal(strides_,strides_+N,rhs.strides_) ) &&
  126. ( (index_base_ == rhs.index_base_) ||
  127. std::equal(index_base_,index_base_+N,rhs.index_base_) );
  128. }
  129. template <class DifferenceType>
  130. void advance(DifferenceType n) {
  131. idx_ += n;
  132. }
  133. template <class IteratorAdaptor>
  134. typename facade_type::difference_type
  135. distance_to(IteratorAdaptor& rhs) const {
  136. return rhs.idx_ - idx_;
  137. }
  138. };
  139. } // namespace multi_array
  140. } // namespace detail
  141. } // namespace boost
  142. #endif // ITERATOR_RG071801_HPP