Non-container Iterators

Discussion in 'C++' started by Leslie Sanford, Jul 24, 2008.

  1. My area of programming is DSP. I write things like filters, oscillators,
    envelopes, etc. I've been looking at STL iterators, and what's struck me is
    that if I can find ways to model my code using STL's iterator conventions, I
    could possibly make my code more economic while probably losing little to no

    As an example, an oscillator will have a "phase accumulator." So typically
    in a loop I will have something like this:

    phaseAccumulator += phaseIncrement;

    if(phaseAccumulator >= 1.0f)
    phaseAccumulator -= 1.0f;

    output = waveform(phaseAccumulator);

    It occurred to me that I could have a phase accumulator iterator. My code
    would turn into this:


    output = waveform(*phaseAccumulator);

    Much more compact. Or even better:

    std::transform(phaseFirst, phaseLast, outputBuffer, SineWaveform());

    Down to just one line of code. Ok, so far, so good. But there is a gray area
    I'm concerned about.

    I need to keep track of the phase. The phase is state that needs to persist
    across iterations. This means that I need to give the iterator a pointer to
    the phase variable when I create it so that as it's iterating, it's also
    modifying the phase variable. Something like:

    // Inside my Oscillator class somewhere:
    it = PhaseIterator it(&phase, increment);

    // Inside the PhaseIterator class:
    PhaseIterator &operator++()
    *phase += increment;

    if(*phase >= 1.0f)
    *phase -= 1.0f;

    return *this;

    This works, but it means that I can only use one phase iterator at a time.
    That's actually not a problem for me since the iterator is only being used
    internally by the Oscillator class, but my question is whether this violates
    the spirit of STL iterators. Is there a category of iterators (Input
    Iterators, perhaps?) that restrict you to using only one iterator at a time
    over a collection?

    Advancing an iterator doesn't usually involve changing the collection the
    iterator belongs to. However, in this case, the collection is the phase
    values belonging to the oscillator; the iterator is actually generating the
    phase values that drive oscillation. I like this approach, but was wanting
    to get some thoughts on this from others.
    Leslie Sanford, Jul 24, 2008
    1. Advertisements

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. moggous phar
    Gianni Mariani
    Oct 15, 2004
  2. Marcin Kaliciñski

    Iterators and reverse iterators

    Marcin Kaliciñski, May 8, 2005, in forum: C++
    Kai-Uwe Bux
    May 8, 2005
  3. Patrick Kowalzick

    Iterators over nested STL container

    Patrick Kowalzick, Aug 3, 2005, in forum: C++
    Chris Theis
    Aug 3, 2005
  4. Leslie Sanford

    Re: Non-container Iterators

    Leslie Sanford, Jul 25, 2008, in forum: C++
    Leslie Sanford
    Jul 25, 2008
  5. , India
    James Kanze
    Aug 8, 2009

Share This Page