Vita
i_ga_iterator.tcc
1/**
2 * \file
3 * \remark This file is part of VITA.
4 *
5 * \copyright Copyright (C) 2014-2016 EOS di Manlio Morini.
6 *
7 * \license
8 * This Source Code Form is subject to the terms of the Mozilla Public
9 * License, v. 2.0. If a copy of the MPL was not distributed with this file,
10 * You can obtain one at http://mozilla.org/MPL/2.0/
11 */
12
13#if !defined(VITA_GA_I_GA_H)
14# error "Don't include this file directly, include the specific .h instead"
15#endif
16
17#if !defined(VITA_INDIVIDUAL_GA_ITERATOR_TCC)
18#define VITA_INDIVIDUAL_GA_ITERATOR_TCC
19
20///
21/// \brief Iterator to scan the active genes of an individual
22///
23class i_ga::const_iterator
24{
25public:
26 using iterator_category = std::forward_iterator_tag;
27 using difference_type = std::ptrdiff_t ;
28 using value_type = locus;
29 using pointer = const value_type *;
30 using reference = const value_type &;
31
32 ///
33 /// \brief Builds an empty iterator.
34 ///
35 /// Empty iterator is used as sentry (it is the value returned by
36 /// i_ga::end()).
37 ///
38 const_iterator() : sup_(0), i_(std::numeric_limits<decltype(i_)>::max()) {}
39
40 ///
41 /// \param[in] id an individual.
42 ///
43 explicit const_iterator(const i_ga &id) : sup_(id.parameters()), i_(0)
44 {
45 }
46
47 ///
48 /// \return locus of the next active symbol.
49 ///
50 const_iterator &operator++()
51 {
52 ++i_;
53
54 if (i_ >= sup_)
55 i_ = std::numeric_limits<decltype(i_)>::max();
56
57 return *this;;
58 }
59
60 ///
61 /// \param[in] rhs second term of comparison.
62 ///
63 /// Returns `true` if iterators point to the same locus.
64 ///
65 bool operator==(const const_iterator &rhs) const
66 {
67 return i_ == rhs.i_;
68 }
69
70 bool operator!=(const const_iterator &rhs) const
71 {
72 return !(*this == rhs);
73 }
74
75 ///
76 /// \return the current locus of the individual.
77 ///
78 value_type operator*() const
79 {
80 return {0, i_};
81 }
82
83private:
84 const category_t sup_;
85 category_t i_;
86}; // class i_ga::const_iterator
87
88#endif // Include guard