Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
378 views
in Technique[技术] by (71.8m points)

c++ - Easiest way to make a cyclic iterator (circulator)?

I have an object that I want to travel in a continuous loop in a game. I have a series of coordinates in a std::vector that I want to use as waypoints.

Is there any way to make an std::vector<T>::iterator cyclic (also known as a circulator)?

The best I can come up with is to have two iterators and then whenever the first iterator is exhausted assign to it the value of the second (which would not be used to do anything else) but I am not even sure it will work - will the assignment operator copy whatever the iterator is using to hold the index or will it merely be referenced (and therefore will be useless after the second round)?

I want the object to travel the waypoint forever (unless it is destroyed but that doesn't happen in that method), but the iterator will only be called once for each frame and must return so that I can update the other objects in the game.

The solution must work on gcc and microsoft compiler (if it isn't possible to write it in standard C++).

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

Ok, now your problem is clearer :-)

Take a look at boost::iterator_facade and boost::iterator adaptor. They implement the full iterator interface and your cycle_iterator only as to implement a few methods like increment(), decrement():

template<class IteratorBase>
class cycle_iterator 
     : public boost::iterator_adaptor< 
          cycle_iterator,     // the derived class overriding iterator behavior
          IteratorBase,       // the base class providing default behavior
          boost::use_default, // iterator value type, will be IteratorBase::value_type
          std::forward_iterator_tag, // iterator category
          boost::use_default  // iterator reference type
       > 
{
  private:
     IteratorBase m_itBegin;
     IteratorBase m_itEnd;

  public:
     cycle_iterator( IteratorBase itBegin, IteratorBase itEnd ) 
       : iterator_adaptor_(itBegin), m_itBegin(itBegin), m_itEnd(itEnd)
     {}

     void increment() {
        /* Increment the base reference pointer. */
        ++base_reference();

        /* Check if past-the-end element is reached and bring back the base reference to the beginning. */
        if(base_reference() == m_itEnd)
            base_reference() = m_itBegin;
     }

     // implement decrement() and advance() if necessary
  };

This probably doesn't compile but should get you started.

Edit:

boost::iterator_adaptor implements the full iterator interface in terms of few functions. It provides default implementations for increment(), decrement(), advance(), distance_to(), equal_to() and dereference() using the base iterator passed down to the iterator_adaptor base class.

If all you need is a forward iterator, only the increment() method must be implemented to wrap around once you reach the end iterator. A cyclical iterator can be bidirectional if you implement decrement() in a similar fashion. If IteratorBase is itself a random access iterator, the cycle iterator can also be random access and method advance and distance_to must be implemented using modulo operations.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...