473,396 Members | 2,081 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 software developers and data experts.

Non-container Iterators

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
efficiency.

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:
phaseAccumulator++;

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.
Jul 24 '08 #1
7 1625
First, my apologies for the double post. Second, my apologies for replying
to my own post. :-)

After further thought, I don't think iterators are the way to model my phase
accumulator. Rather I think the concept of Generator, a nullary function
that returns a value, is a better model. So I came up with this:
class PhaseAccumulator
{
public:
typedef float result_type;

private:
float *phase;
float increment;

public:
PhaseAccumulator(float *phase, float increment)
{
this->phase = phase;
this->increment = increment;
}

result_type operator()()
{
*phase += increment;

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

return *phase;
}
};
I use unary functions for my waveforms; they argument is the phase. I need
to fed the output of my PhaseAccumulator into a Waveform object. In looking
at the STL, I haven't found an adapter to do this; I could be missing
something. So I wrote my own:
template<typename Generator, typename UnaryFunction>
class NullaryCompose
{
public:
typedef typename Generator::result_type result_type;

private:
Generator myG;
UnaryFunction myF;

public:
NullaryCompose(Generator g, UnaryFunction f) :
myG(g),
myF(f)
{
}

result_type operator()()
{
return myF(myG());
}
};
Which allows me to do this:
PhaseAccumulator phaseAccumulator(&phase, increment);
NullaryCompose<PhaseAccumulator, WaveShapes::Sinenc(phaseAccumulator,
WaveShapes::Sine());

std::generate(&buffer[0], &buffer[100], nc);
I think modeling these sorts of components as function objects makes more
sense than modeling them as iterators.
Jul 24 '08 #2
Good grief this is frustrating. Viewing this group through Google
Groups, I have apparently triple posted. The third post was *suppose*
to be 1) an apology for double posting, and 2) further thoughts on how
my phase accumulator would be better modeled using STL's idea of a
Generator instead of an Iterator. But I'm wary of reposting that at
this point.

Sorry folks.
Jul 24 '08 #3
"Leslie Sanford" <ja**********@bitemehotmail.comwrote:
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
efficiency.
Then you might find this interesting:

class fibonacci: public std::iterator< std::forward_iterator_tag, int >
{
int prev_value, value, max;
public:
fibonacci(): prev_value(0), value(0), max(0) { }
explicit fibonacci(int m): prev_value(0), value(1), max(m) { }
const int operator*() const { return value; }
fibonacci& operator++() {
int tmp = value;
value += prev_value;
prev_value = tmp;
return *this;
}
fibonacci operator++(int) {
fibonacci tmp(*this);
++(*this);
return tmp;
}
friend bool operator==(const fibonacci& lhs, const fibonacci& rhs) {
bool result = false;
if ( lhs.value == 0 && rhs.value == 0 ) result = true;
else if ( rhs.value == 0 && !( lhs.value < lhs.max ) )
result = true;
else if ( lhs.value == 0 && !( rhs.value < rhs.max ) )
result = true;
else if ( lhs.prev_value == rhs.prev_value &&
lhs.value == rhs.value &&
lhs.max == rhs.max )
result = true;
return result;
}
};

bool operator!=(const fibonacci& lhs, const fibonacci& rhs) {
return !(lhs == rhs);
}
int main() {
copy( fibonacci( 20 ), fibonacci(),
ostream_iterator<int>( cout, " " ) );
cout << '\n';
}
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:
phaseAccumulator++;

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.
Not if you put 'phase' inside the iterator. Then you can give two
iterators the same phase and increment and advance each of them a
different amount, and they will each be at a different spot in the
"container".

The key is to provide a sentinel iterator. In your case, the sentinel
can be a PhaseIterator that has an increment of 0.

class PhaseIterator : public std::iterator< std::forward_iterator_tag,
int >
{
float phase;
float increment;
int step;
int max;
public:
PhaseIterator(): phase(0), increment(0), step(0), max(0) { }
explicit PhaseIterator(float i, int m):
phase(0), increment(i), step(0), max(m) { }
const float operator*() const { return phase; }
PhaseIterator& operator++() {
phase += increment;
if ( phase >= 1.0f )
phase -= 1.0f;
++step;
return *this;
}
PhaseIterator operator++(int) {
PhaseIterator tmp(*this);
++(*this);
return tmp;
}
friend bool operator==(const PhaseIterator& lhs,
const PhaseIterator& rhs) {
bool result = false;
if ( lhs.phase == rhs.phase && lhs.increment == rhs.increment )
result = true;
else if ( rhs.increment == 0 && !( lhs.step < lhs.max ) )
result = true;
else if ( lhs.increment == 0 && !( rhs.step < rhs.max ) )
result = true;
return result;
}
};

bool operator!=(const PhaseIterator& lhs, const PhaseIterator& rhs) {
return !(lhs == rhs);
}
int main() {
copy( PhaseIterator( 0.07, 20 ), PhaseIterator(),
ostream_iterator<float>( cout, " " ) );
cout << '\n';
}
Jul 25 '08 #4

"Daniel T." wrote:
"Leslie Sanford" wrote:
>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 efficiency.

Then you might find this interesting:

class fibonacci: public std::iterator< std::forward_iterator_tag, int >
<snip>

Yes, indeed. That is very interesting.

<snip>
>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.

Not if you put 'phase' inside the iterator. Then you can give two
iterators the same phase and increment and advance each of them a
different amount, and they will each be at a different spot in the
"container".
Understood.
The key is to provide a sentinel iterator. In your case, the sentinel
can be a PhaseIterator that has an increment of 0.
In this case, there is no sentinel iterator as an oscillator, which a phase
accumulator drives, can cycle indefinitely, a kind of circular buffer, I
suppose.

Is it acceptable for an iterator to never reach an "end"? I would have
another way for testing for the end of the loop, specifically the end of the
buffer that I'm filling. I should be able to increment the phase iterator
indefinitely.

while(first != last)
{
*first = *phase;

phase++;
first++;
}

Since (triple) posting, I've been giving this approach some thought, and I
was wondering if a Generator would be a more appropriate model than an
Iterator to represent a phase accumulator.

http://www.sgi.com/tech/stl/Generator.html

class PhaseAccumulator
{
public:
typedef float result_type;

private:
float phase;
float increment;

public:
PhaseAccumulator(float phase, float increment)
{
this->phase = phase;
this->increment = increment;
}

result_type operator()()
{
phase += increment;

if(phase >= 1.0f)
{
phase -= 1.0f;
}

return phase;
}
};

I can have a Square waveform represented as a unary function:

typedef std::unary_function<float, floatWaveShapeBase;

struct Square : public WaveShapeBase
{
result_type operator()(argument_type phase) const
{
assert(phase >= 0.0f && phase < 1.0f);

return phase < 0.5f ? -1.0f : 1.0f;
}
};

And use both in a loop to fill a buffer:

class Oscillator
{
PhaseAccumulator phase;
Square wave;

public:
// Stuff...

void Process(float *first, float *last)
{
while(first != last)
{
*first = wave(phase());

first++;
}
}
}

Maybe this is a more appropriate approach given the concepts involved?


Jul 25 '08 #5
On Jul 24, 11:18*pm, "Leslie Sanford" <jabberdab...@bitemehotmail.com>
wrote:
In this case, there is no sentinel iterator as an oscillator, which a phase
accumulator drives, can cycle indefinitely, a kind of circular buffer, I
suppose.

Is it acceptable for an iterator to never reach an "end"? I would have
another way for testing for the end of the loop, specifically the end of the
buffer that I'm filling. I should be able to increment the phase iterator
indefinitely.

while(first != last)
{
* * *first = *phase;

* * phase++;
* * first++;

}

Since (triple) posting, I've been giving this approach some thought, and I
was wondering if a Generator would be a more appropriate model than an
Iterator to represent a phase accumulator.

http://www.sgi.com/tech/stl/Generator.html

class PhaseAccumulator
{
public:
* * typedef float result_type;

private:
* * float phase;
* * float increment;

public:
* * PhaseAccumulator(float phase, float increment)
* * {
* * * * this->phase = phase;
* * * * this->increment = increment;
* * }

* * result_type operator()()
* * {
* * * * phase += increment;

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

* * * * return phase;
* * }

};

I can have a Square waveform represented as a unary function:

typedef std::unary_function<float, floatWaveShapeBase;

struct Square : public WaveShapeBase
{
* * result_type operator()(argument_type phase) const
* * {
* * * * assert(phase >= 0.0f && phase < 1.0f);

* * * * return phase < 0.5f ? -1.0f : 1.0f;
* * }

};

And use both in a loop to fill a buffer:

class Oscillator
{
* * PhaseAccumulator phase;
* * Square wave;

* * public:
* * * * // Stuff...

* * void Process(float *first, float *last)
* * {
* * * * while(first != last)
* * * * {
* * * * * * *first = wave(phase());

* * * * * * first++;
* * * * }
* * }

}

Maybe this is a more appropriate approach given the concepts involved?
Since the phase can't "end" in any meaningful sense, a generator may
be a more appropriate design pattern. Pseudo-random number generators
(eventually) loop, too, and they're commonly used as generator
functors. That being said, there certainly are generator-style
iterators out there, e.g. boost::counting_iterator:

http://www.boost.org/doc/libs/1_35_0..._iterator.html

Using something like that approach, your oscillator class might look
like:

void Oscillator::Process(float *begin, float *end)
{
// Generate
std::transform(
phase.begin(),
phase.end( std::distance(begin,end) ),
begin,
Square );
}

I assume the phase has been given counting_iterator-like support,
where the end() function takes a parameter telling it how many times
to increment. Note also that you could convert your square wave
functor to an ordinary function with the signature float Square(float)
since it doesn't take any constructor parameters or maintain mutable
state. The member variable for that functor is overkill.

The down-side to this approach is that the use of std::transform()
with a generator-style iterator to generate is takes some thinking,
and one of our goals for code is (or, should be) to make it readable.
The names of the standard functions are intended to communicate your
meaning rather than obscure it (cf. http://www.ddj.com/cpp/184401446),
but generator-style iterators cut against the grain in a circumstance
like this.

A generator approach: Using functional composition [let's call ours
my_compose(f,g) = f(g())], whose implementation is relatively
straightforward and similar to the common extension to the STL (e.g.,
http://www.sgi.com/tech/stl/unary_compose.html), one can make this
clearer on first reading:

std::generate( begin, end, my_compose( Square, phase ) );

Now fewer tricks are being played, and its meaning should be clearer
to someone reading your code down the line (which could be you).

Cheers! --M
Jul 25 '08 #6
On Jul 24, 11:18*pm, "Leslie Sanford" <jabberdab...@bitemehotmail.com>
wrote:
"Daniel T." wrote:
"Leslie Sanford" wrote:
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 efficiency.
Then you might find this interesting:
class fibonacci: public std::iterator< std::forward_iterator_tag, int >

<snip>

Yes, indeed. That is very interesting.

<snip>


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 iteratora
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.
Not if you put 'phase' inside the iterator. Then you can give two
iterators the same phase and increment and advance each of them a
different amount, and they will each be at a different spot in the
"container".

Understood.
The key is to provide a sentinel iterator. In your case, the sentinel
can be a PhaseIterator that has an increment of 0.

In this case, there is no sentinel iterator as an oscillator, which a phase
accumulator drives, can cycle indefinitely, a kind of circular buffer, I
suppose.

Is it acceptable for an iterator to never reach an "end"? I would have
another way for testing for the end of the loop, specifically the end of the
buffer that I'm filling. I should be able to increment the phase iterator
indefinitely.

while(first != last)
{
* * *first = *phase;

* * phase++;
* * first++;

}

Since (triple) posting, I've been giving this approach some thought, and I
was wondering if a Generator would be a more appropriate model than an
Iterator to represent a phase accumulator.

http://www.sgi.com/tech/stl/Generator.html

class PhaseAccumulator
{
public:
* * typedef float result_type;

private:
* * float phase;
* * float increment;

public:
* * PhaseAccumulator(float phase, float increment)
* * {
* * * * this->phase = phase;
* * * * this->increment = increment;
* * }

* * result_type operator()()
* * {
* * * * phase += increment;

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

* * * * return phase;
* * }

};

I can have a Square waveform represented as a unary function:

typedef std::unary_function<float, floatWaveShapeBase;

struct Square : public WaveShapeBase
{
* * result_type operator()(argument_type phase) const
* * {
* * * * assert(phase >= 0.0f && phase < 1.0f);

* * * * return phase < 0.5f ? -1.0f : 1.0f;
* * }

};

And use both in a loop to fill a buffer:

class Oscillator
{
* * PhaseAccumulator phase;
* * Square wave;

* * public:
* * * * // Stuff...

* * void Process(float *first, float *last)
* * {
* * * * while(first != last)
* * * * {
* * * * * * *first = wave(phase());

* * * * * * first++;
* * * * }
* * }

}

Maybe this is a more appropriate approach given the concepts involved?
That sounds like it would work. Then your process function can be
replaced by the generate algorithm.

float arr[20];
generate( arr, arr + 20, PhaseAccumulator(0, 0.3) );

or better:

vector< float arr;
generate_n( back_inserter( arr ), 20, PhaseAccumulator(0, 0.3) );

But if you make a powerful enough PhaseAccumulator iterator, you won't
*need* to fill the array with values, you can use the iterator
directly over the calculated container.
Jul 25 '08 #7

"mlimber" wrote:
>
A generator approach: Using functional composition [let's call ours
my_compose(f,g) = f(g())], whose implementation is relatively
straightforward and similar to the common extension to the STL (e.g.,
http://www.sgi.com/tech/stl/unary_compose.html), one can make this
clearer on first reading:

std::generate( begin, end, my_compose( Square, phase ) );
I like this approach; the idea of function composition is appealing. The
problem is handling state. The waveform function object is stateless, a true
function. However, the phase accumulator has state, specifically the value
of its phase. Say I have the following:

void Oscillator::Process(float *begin, float *end)
{
// Assumes a phase member variable.
std::generate(begin, end, my_compose(Square(), phase);
}

Each time Process is called the phase needs to pick up from where it left
off with the previous call. However, 'phase' is passed in by value, so the
phase accumulator that's actually getting advanced by generate is a copy of
the original. The end result is that each time Process is called, phase
starts at the same place.

I see a couple of ways around this. Design the phase accumulator so that it
takes a pointer to the actual phase variable:

// Inside the Oscillator class..

// Member variable.
float phase;

//...

void Oscillator::Process(float *begin, float *end)
{
std::generate(begin, end, my_compose(Square(),
PhaseAccumulator(&phase));
}

This ensures that the phase value that's getting incremented persists
between calls since the PhaseAccumulator is incrementing the pointer
variable.

I suppose this solution is ok.

Another approach is to somehow update the phase accumulator after generate
has done its job:

void Oscillator::Process(float *begin, float *end)
{
MyCompose<Square, PhaseAccumulatormc(Square(), phase);

std::generate(begin, end, mc);

// Assumes our custom compose template exposes the generator function
object.
phase = mc.generator;
}

This works as well. Another way is to explicitely tell the generate function
to treat the phase accumulator object as a reference, but then I'm pretty
sure I'd lose inlining in that case. Things would start to look ugly at that
point anyway.

Also, we can forego the convenience of using generate and simply write the
loop ourselves using the same member variables:

void Oscillator::Process(float *first, float *last)
{
while(first != last)
{
// Assumes phase and wave are member variables.
*first = wave(phase());

first++;
}
}


Jul 25 '08 #8

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

5
by: klaus triendl | last post by:
hi, recently i discovered a memory leak in our code; after some investigation i could reduce it to the following problem: return objects of functions are handled as temporary objects, hence...
3
by: Mario | last post by:
Hello, I couldn't find a solution to the following problem (tried google and dejanews), maybe I'm using the wrong keywords? Is there a way to open a file (a linux fifo pipe actually) in...
25
by: Yves Glodt | last post by:
Hello, if I do this: for row in sqlsth: ________pkcolumns.append(row.strip()) ________etc without a prior:
32
by: Adrian Herscu | last post by:
Hi all, In which circumstances it is appropriate to declare methods as non-virtual? Thanx, Adrian.
8
by: Bern McCarty | last post by:
Is it at all possible to leverage mixed-mode assemblies from AppDomains other than the default AppDomain? Is there any means at all of doing this? Mixed-mode is incredibly convenient, but if I...
14
by: Patrick Kowalzick | last post by:
Dear all, I have an existing piece of code with a struct with some PODs. struct A { int x; int y; };
11
by: ypjofficial | last post by:
Hello All, So far I have been reading that in case of a polymorphic class ( having at least one virtual function in it), the virtual function call get resolved at run time and during that the...
2
by: Ian825 | last post by:
I need help writing a function for a program that is based upon the various operations of a matrix and I keep getting a "non-aggregate type" error. My guess is that I need to dereference my...
399
by: =?UTF-8?B?Ik1hcnRpbiB2LiBMw7Z3aXMi?= | last post by:
PEP 1 specifies that PEP authors need to collect feedback from the community. As the author of PEP 3131, I'd like to encourage comments to the PEP included below, either here (comp.lang.python), or...
12
by: puzzlecracker | last post by:
is it even possible or/and there is a better alternative to accept input in a nonblocking manner?
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.