Recently, I need to implement small classes that generate a bunch of numbers. It would be very convenient if C ++ had generators like python, but unfortunately this is not the case.
So, I was thinking about how to best implement these types of objects, for easy iteration and composition. When we think of iterators over containers, they essentially only contain the index for the element, and most of the information is in the container itself. This allows multiple iterators to reference different elements in the collection at the same time.
When it comes to machine states, it becomes obvious that the iterator will have to hold the whole state, since several iterators must be independent. In this sense, the state class of a machine is rather the βbuilderβ of these iterators, which are actual state machines.
As an example of toys, I implemented a range generator (ala xrange in python) that can be used in loops:
// using range-for from c++11 for (auto i : range<int>(1, 30)) { cout << i << endl; }
The code can be found on my bitbucket .
However, it is inconvenient to store an integer state in an iterator, since the end() iterator is only created to compare the final state that allocates space if the state is a large collection of members.
Was something done using simple machines with a linear state and looping over them using iterators?
source share