bnmf-algs
Public Types | Public Member Functions | List of all members
bnmf_algs::util::Generator< T, Computer > Class Template Reference

A template generator that generates values from an initial value by applying a computation to the previous value a given number of times. More...

#include <generator.hpp>

Public Types

using iter_type = ComputationIterator< T, Computer >
 Iterator type that will be used to compute values repeatedly without explicitly storing all the values. More...
 

Public Member Functions

 Generator (const T &init_val, size_t iter_count, Computer &&computer)
 Generator constructor. More...
 
 Generator (const Generator &other)
 Copy constructor. More...
 
Generatoroperator= (const Generator &other)
 Copy assignment operator. More...
 
 Generator (Generator &&other)
 Move constructor. More...
 
Generatoroperator= (Generator &&other)
 Move assignment operator. More...
 
iter_type begin ()
 Return the iterator pointing to the previously computed value. More...
 
iter_type end ()
 Return the end iterator. More...
 

Detailed Description

template<typename T, typename Computer>
class bnmf_algs::util::Generator< T, Computer >

A template generator that generates values from an initial value by applying a computation to the previous value a given number of times.

A generator object is very similar to python generator expressions in that it generates values from an initial value by applying a function repeatedly. Only the most recently computed value is stored and returned.

After the specified number of values are generated by repeatedly applying the computer function/functor, Generator object gets consumed and cannot generate any more values. Hence, to generate more values, a new Generator is required. For example,

void increment(size_t step, int& prev) {
++prev;
}
Generator<int, decltype(&increment)> gen(0, 50, increment);
for (auto num : gen) {
numbers.push_back(num);
}
// numbers.size() == 50
// gen.begin() == gen.end()
std::vector<int> new_numbers(numbers);
for (auto num : gen) {
new_numbers.push_back(num);
}
// new_numbers.size() == 0

Since Generator object provides begin() and end() methods, it can be used with range-based for loops as seen in the previous example. Regular iterator-based for loops are also supported:

Op increment;
Generator<int, Op> gen(0, 50, increment);
for (auto it = gen.begin(); it != gen.end(); ++it) {
numbers.push_back(num);
}
Template Parameters
TType of the values to generate.
ComputerType of the computer functor/function/etc. to use.

Member Typedef Documentation

template<typename T, typename Computer>
using bnmf_algs::util::Generator< T, Computer >::iter_type = ComputationIterator<T, Computer>

Iterator type that will be used to compute values repeatedly without explicitly storing all the values.

Constructor & Destructor Documentation

template<typename T, typename Computer>
bnmf_algs::util::Generator< T, Computer >::Generator ( const T &  init_val,
size_t  iter_count,
Computer &&  computer 
)
inline

Generator constructor.

Generator constructor takes an initial value and an iteration count. init_val is the first value that will get generated. iter_count is the number of values that will get generated. For example,

Generator<int, Op> gen(0, 5, increment); // 0, 1, 2, 3, 4
gen = Generator<int, Op>(20, 4, decrement); // 20, 19, 18, 17
Parameters
init_valInitial value of the sequence to generate. This is the first value that will get generated.
iter_countNumber of values to generate.
computerComputer function/functor that will compute the next value from the previous value. Computer is taken as an rvalue reference and is moved into this Generator object.
template<typename T, typename Computer>
bnmf_algs::util::Generator< T, Computer >::Generator ( const Generator< T, Computer > &  other)
inline

Copy constructor.

Generator class manually implements its copy constructor to correctly copy the begin and end iterators which should point to the copy constructed object's member variables.

Parameters
otherOther Generator object to copy construct from.
template<typename T, typename Computer>
bnmf_algs::util::Generator< T, Computer >::Generator ( Generator< T, Computer > &&  other)
inline

Move constructor.

Generator class manually implements its move constructor to correctly construct begin and end iterators which should point to move constructed object's member variables.

Parameters
otherOther Generator object to move from.

Member Function Documentation

template<typename T, typename Computer>
iter_type bnmf_algs::util::Generator< T, Computer >::begin ( )
inline

Return the iterator pointing to the previously computed value.

Note that since only the most recently computed value is stored, the semantics for begin() is not the same STL container::begin(). After an iterator is incremented, the next value is computed and the previous value is forgotten.

All the returned begin iterators point to the same values. Hence, once a copy of a begin iterator is incremented and hence the pointed values are updated, all the subsequent calls to begin() will return iterators pointing to these updated values.

An increment to any of the iterators update the values pointed by all the iterators. Hence, once the total number of calls to increment operator on iterators is equal to iter_count, all the iterators are equal to Generator::end().

Note that incrementing any of the iterators beyond Generator::end() still produces new values. However, after that point, it is no longer possible to make equivalence checks against Generator::end(). Hence, in such a case iterators must be handled manually.

Returns
Iterator pointing to the most recently computed value.
template<typename T, typename Computer>
iter_type bnmf_algs::util::Generator< T, Computer >::end ( )
inline

Return the end iterator.

End iterator is a sentinel iterator. If an iterator returned by Generator::begin() is equal to Generator::end(), then this Generator object is consumed and doesn't produce any values when used with range-based for loops.

Returns
End iterator.
template<typename T, typename Computer>
Generator& bnmf_algs::util::Generator< T, Computer >::operator= ( const Generator< T, Computer > &  other)
inline

Copy assignment operator.

Generator class manually implements its copy assignment operator to correctly copy the begin and end iterators which should point to the copy assigned object's member variables.

Parameters
otherOther Generator object to copy assign from.
Returns
Reference to the assigned Generator object.
template<typename T, typename Computer>
Generator& bnmf_algs::util::Generator< T, Computer >::operator= ( Generator< T, Computer > &&  other)
inline

Move assignment operator.

Generator class manually implements its move assignment operator to correctly construct begin and end iterators which should point to move assigned object's member variables.

Parameters
otherOther Generator object to move from.
Returns
Reference to move assigned Generator object.

The documentation for this class was generated from the following file: