bnmf-algs
generator.hpp
Go to the documentation of this file.
1 #pragma once
2 
3 #include <functional>
4 #include <memory>
5 
6 namespace bnmf_algs {
7 namespace util {
8 
105 template <typename T, typename Computer>
106 class ComputationIterator : public std::iterator<std::forward_iterator_tag, T> {
107  public:
127  ComputationIterator(T* init_val_ptr, size_t* step_count_ptr,
128  Computer* computer_ptr)
129  : curr_val_ptr(init_val_ptr), step_count_ptr(step_count_ptr),
130  computer_ptr(computer_ptr){};
131 
145  explicit ComputationIterator(size_t* step_count_ptr)
146  : curr_val_ptr(nullptr), step_count_ptr(step_count_ptr),
147  computer_ptr(nullptr) {}
148 
176  (*computer_ptr)(*step_count_ptr, *curr_val_ptr);
177  ++(*step_count_ptr);
178  return *this;
179  }
180 
187  const T& operator*() const { return *curr_val_ptr; }
188 
195  const T* operator->() const { return curr_val_ptr; }
196 
207  bool operator==(const ComputationIterator& other) const {
208  return *(this->step_count_ptr) == *(other.step_count_ptr);
209  }
210 
221  bool operator!=(const ComputationIterator& other) const {
222  return !(*this == other);
223  }
224 
225  private:
226  T* curr_val_ptr;
227  size_t* step_count_ptr;
228  Computer* computer_ptr;
229 };
230 
279 template <typename T, typename Computer> class Generator {
280  // todo: Can we use the return type of Computer to get rid of T?
281  public:
287 
288  public:
308  Generator(const T& init_val, size_t iter_count, Computer&& computer)
309  : init_val(init_val), curr_step_count(0), total_iter_count(iter_count),
310  computer(std::move(computer)),
311  begin_it(&(this->init_val), &(this->curr_step_count),
312  &(this->computer)),
313  end_it(&(this->total_iter_count)){};
314 
324  Generator(const Generator& other)
325  : init_val(other.init_val), curr_step_count(other.curr_step_count),
326  total_iter_count(other.total_iter_count), computer(other.computer),
327  begin_it(iter_type(&(this->init_val), &(this->curr_step_count),
328  &(this->computer))),
329  end_it(iter_type(&(this->total_iter_count))) {}
330 
342  Generator& operator=(const Generator& other) {
343  this->init_val = other.init_val;
344  this->curr_step_count = other.curr_step_count;
345  this->total_iter_count = other.total_iter_count;
346  this->computer = other.computer;
347 
348  this->begin_it = iter_type(&(this->init_val), &(this->curr_step_count),
349  &(this->total_iter_count));
350  this->end_it = iter_type(&(this->total_iter_count));
351 
352  return *this;
353  }
354 
365  : init_val(std::move(other.init_val)),
366  curr_step_count(std::move(other.curr_step_count)),
367  total_iter_count(std::move(other.total_iter_count)),
368  computer(std::move(other.computer)),
369  begin_it(iter_type(&(this->init_val), &(this->curr_step_count),
370  &(this->computer))),
371  end_it(iter_type(&(this->total_iter_count))) {}
372 
385  this->init_val = std::move(other.init_val);
386  this->curr_step_count = std::move(other.curr_step_count);
387  this->total_iter_count = std::move(other.total_iter_count);
388  this->computer = std::move(other.computer);
389 
390  this->begin_it = iter_type(&(this->init_val), &(this->curr_step_count),
391  &(this->computer));
392  this->end_it = iter_type(&(this->total_iter_count));
393 
394  return *this;
395  }
421  iter_type begin() { return begin_it; }
422 
433  iter_type end() { return end_it; }
434 
435  private:
436  T init_val;
437  size_t curr_step_count;
438  size_t total_iter_count;
439  Computer computer;
440  iter_type begin_it;
441  iter_type end_it;
442 };
443 } // namespace util
444 } // namespace bnmf_algs
bool operator!=(const ComputationIterator &other) const
Inquality operator.
Definition: generator.hpp:221
A template generator that generates values from an initial value by applying a computation to the pre...
Definition: generator.hpp:279
const T * operator->() const
Member access operator to access the members of the most recently computed value. ...
Definition: generator.hpp:195
ComputationIterator(size_t *step_count_ptr)
ComputationIterator constructor that takes a pointer for the current step only.
Definition: generator.hpp:145
iter_type begin()
Return the iterator pointing to the previously computed value.
Definition: generator.hpp:421
Generator(const Generator &other)
Copy constructor.
Definition: generator.hpp:324
const T & operator*() const
Dereference operator to the get a const reference to the most recently computed value.
Definition: generator.hpp:187
Generator & operator=(Generator &&other)
Move assignment operator.
Definition: generator.hpp:384
T move(T...args)
Generator(Generator &&other)
Move constructor.
Definition: generator.hpp:364
iter_type end()
Return the end iterator.
Definition: generator.hpp:433
bool operator==(const ComputationIterator &other) const
Equality operator.
Definition: generator.hpp:207
ComputationIterator(T *init_val_ptr, size_t *step_count_ptr, Computer *computer_ptr)
ComputationIterator constructor that takes pointers for initial value, current step count and the com...
Definition: generator.hpp:127
ComputationIterator & operator++()
Pre-increment operator that computes the next value of the computation and updates step_count and cur...
Definition: generator.hpp:175
Main namespace for bnmf-algs library.
Definition: alloc_model_funcs.hpp:12
Generator(const T &init_val, size_t iter_count, Computer &&computer)
Generator constructor.
Definition: generator.hpp:308
Generator & operator=(const Generator &other)
Copy assignment operator.
Definition: generator.hpp:342
A template iterator that generates values via computation using the previous value.
Definition: generator.hpp:106