Re: [Boost-bugs] [Boost C++ Libraries] #7824: "++--rng.end() == rng.end()" can fail for strided ranges

Subject: Re: [Boost-bugs] [Boost C++ Libraries] #7824: "++--rng.end() == rng.end()" can fail for strided ranges
From: Boost C++ Libraries (noreply_at_[hidden])
Date: 2012-12-22 08:49:51


#7824: "++--rng.end() == rng.end()" can fail for strided ranges
--------------------------------------+-------------------------------------
  Reporter: michel | Owner: neilgroves
      Type: Bugs | Status: new
 Milestone: To Be Determined | Component: range
   Version: Boost Development Trunk | Severity: Problem
Resolution: | Keywords:
--------------------------------------+-------------------------------------

Comment (by michel):

 Just for reference, here is a sample implementation of `strided_iterator`
 for `Random Access Range` (without zero-stride support),
 which can pass `strided.cpp` test (other than `strided_test_zero_stride`).
 {{{
 // for Random Access Range (without zero-stride support)
 template <typename BaseIterator>
 class strided_iterator<BaseIterator, boost::random_access_traversal_tag>
   : public iterator_facade<
         strided_iterator<BaseIterator, boost::random_access_traversal_tag>
       , typename std::iterator_traits<BaseIterator>::value_type
       , boost::random_access_traversal_tag
       , typename std::iterator_traits<BaseIterator>::reference
       , typename std::iterator_traits<BaseIterator>::difference_type
>
 {
 public:
     // types
     typedef typename std::iterator_traits<BaseIterator>::reference
 reference;
     typedef typename std::iterator_traits<BaseIterator>::difference_type
 difference_type;

     // ctors
     strided_iterator() {}
     strided_iterator(
         BaseIterator first
       , BaseIterator it
       , BaseIterator last
       , difference_type stride
     )
       : m_begin(first)
       , m_offset((it - first) % stride == 0 ? (it - first) / stride : (it
 - first) / stride + 1)
       , m_stride(stride)
     {}

 private:
     // iterator_facade core operations
     friend class boost::iterator_core_access;

     reference dereference() const
     { return *(m_begin + m_stride * m_offset); }

     bool equal(strided_iterator const& other) const
     { return m_begin == other.m_begin && m_offset == other.m_offset; }

     void increment() { ++m_offset; }
     void decrement() { --m_offset; }
     void advance(difference_type n) { m_offset += n; }

     difference_type distance_to(strided_iterator const& other) const
     { return other.m_offset - m_offset; }

     // data
     BaseIterator m_begin; // the begin iterator of an input range
     difference_type m_offset; // the number of strides taken by this
 iterator
     difference_type m_stride;
 };
 }}}

-- 
Ticket URL: <https://svn.boost.org/trac/boost/ticket/7824#comment:1>
Boost C++ Libraries <http://www.boost.org/>
Boost provides free peer-reviewed portable C++ source libraries.

This archive was generated by hypermail 2.1.7 : 2017-02-16 18:50:11 UTC