25 #include <boost/iterator/iterator_facade.hpp>
31 :
public boost::iterator_facade<CartesianProductIterator<T>,
32 std::vector<typename T::value_type::value_type> const,
33 boost::forward_traversal_tag> {
52 using Content =
typename T::value_type::value_type;
76 std::vector<typename Container::const_iterator>
position_;
82 std::vector<typename Container::const_iterator>
cbegins_;
85 std::vector<typename Container::const_iterator>
cends_;
92 mutable std::vector<std::vector<Content>>
result_{std::vector<Content>()};
104 cends_.push_back(entry.cend());
108 if (pos == std::numeric_limits<std::size_t>::max() ||
size_ == 0) {
115 for (std::size_t i = 0; i !=
size_; ++i) {
125 for (std::size_t i = 0; i < pos; ++i) {
130 template <
typename T>
132 if (absolutePosition_ == std::numeric_limits<std::size_t>::max()) {
136 std::size_t pos = size_ - 1;
139 while (++(position_[pos]) == cends_[pos] && pos != 0) {
142 if (position_[pos] == cends_[pos]) {
144 absolutePosition_ = std::numeric_limits<std::size_t>::max();
148 for (++pos; pos != size_; ++pos) {
149 position_[pos] = cbegins_[pos];
152 result_.emplace_back();
155 template <
typename T>
156 std::vector<typename T::value_type::value_type>
const&
158 if (absolutePosition_ == std::numeric_limits<std::size_t>::max()) {
159 throw new std::out_of_range(
"Out of bound dereference in CartesianProductIterator\n");
161 auto&
result = result_[absolutePosition_];
164 for (
auto& iterator : position_) {
165 result.push_back(*iterator);
172 template <
typename T>
180 template <
typename T>
CartesianProductIterator< T > begin() const
Iterator to beginning of Cartesian product.
CartesianProductIterator()=delete
Delete default constructor.
bool equal(CartesianProductIterator< T > const &other) const
Check for equality.
std::vector< Content > const & dereference() const
Dereference iterator.
OuterContainer const & structure_
The part we are iterating over.
std::size_t absolutePosition_
The position just indexed by an integer.
void increment()
Increment iterator.
std::vector< typename Container::const_iterator > position_
The position in the Cartesian product.
std::vector< typename Container::const_iterator > cends_
The end iterators, saved for convenience and performance.
typename T::value_type::value_type Content
CartesianProductIterator< T > end() const
Iterator behind the last element of the Cartesian product.
typename T::value_type Container
std::vector< std::vector< Content > > result_
Used for returning references.
std::vector< typename Container::const_iterator > cbegins_
The begin iterators, saved for convenience and performance.
std::size_t size_
The size of the instance of OuterContainer.
CartesianProduct(T const &t)
Constructor from type T.
friend class boost::iterator_core_access
Grant access to boost::iterator_facade.
T OuterContainer
Give types more descriptive names.