I feel like this question must have been asked and solved many times, because this seems to me a quite generic scenario, but I could not find anything that pointed me in the direction of a solution.
I'm trying to implement a generic iterable Generator
object that produces a sequence of numbers up until a certain termination condition is met, signaling that such condition has been reached in order to stop the iteration.
The basic idea is, essentially, to have something similar to Python's generators where an object yields values until it has no more to yield and then a StopIteration
exception is raised to inform the outside loop that the sequence is finished.
From what I understand, the problem splits into creating the sequence-generating object an then obtaining an iterator over it.
For the sequence-generating object, I thought I'd define a base Generator
class which is then extended to provide specific behaviours (e.g., get values from a set of ranges, or from a list of fixed values, etc). All Generaor
s produce a new value at each call of operator()
or throw a ValuesFinishedException
if the generator ran to the end of the sequence.
I implemented this as such (I show the single-range subclass as example, but I need to be able to model more types of sequences):
struct ValuesFinishedException : public std::exception { };
template <typename T>
class Generator
{
public:
Generator() { };
~Generator() { };
virtual T operator()() = 0; // return the new number or raise a ValuesFinishedException
};
template <typename T>
class RangeGenerator : public Generator<T>
{
private:
T m_start;
T m_stop;
T m_step;
T m_next_val;
public:
RangeGenerator(T start, T stop, T step) :
m_start(start),
m_stop(stop),
m_step(step),
m_next_val(start)
{ }
T operator()() override
{
if (m_next_val >= m_stop)
throw ValuesFinishedException();
T retval = m_next_val;
m_next_val += m_step;
return retval;
}
void setStep(T step) { m_step = step; }
T step() { return m_step; }
};
For the iterator part, though, I'm stuck.
I have researched any combination I could think of of "Iterator", "Generator" and synonyms, but all I find only considers the case where a generator function has an unlimited number of values (see for example boost's generator_iterator). I thought about writing a Generator::iterator
class myself, but I only found examples of trivial iterators (linked lists, array reimplementations) where end
is well-defined. I don't know in advance when the end will be reached, I only know that if the generator I'm iterating over raises the exception, I need to set the iterator's current value to "end()", but I don't know how to represent it.
Edit: Adding the intended use-case
The reason for this class is to have a flexible sequence object I can loop over:
RangeGenerator gen(0.25f, 95.3f, 1.2f);
for(auto v : gen)
{
// do something with v
}
The example of the range is just the simplest one. I will have at least three actual use-cases:
- simple range (with variable step)
- concatenation of multiple ranges
- sequence of constant values stored in a vector
For each of these I'm planning on having a Generator
subclass, with the iterator defined for the abstract Generator
.