Skip to content

Instantly share code, notes, and snippets.

@Psirus
Created February 14, 2018 16:17
Show Gist options
  • Save Psirus/6474433fe0a08f969d624b937f5f4fc4 to your computer and use it in GitHub Desktop.
Save Psirus/6474433fe0a08f969d624b937f5f4fc4 to your computer and use it in GitHub Desktop.
Entry Generator
#include <boost/iterator/iterator_facade.hpp>
#include <eigen3/Eigen/Core>
#include <iostream>
#include <tuple>
#include <vector>
using Entry = std::pair<Eigen::VectorXd, Eigen::VectorXi>;
class EntryIterator;
class EntryGenerator
{
public:
virtual EntryIterator begin() = 0;
virtual EntryIterator end() = 0;
virtual bool valid() = 0;
virtual void next() = 0;
virtual Entry& get() = 0;
};
class EntryIterator : public boost::iterator_facade<EntryIterator, Entry, boost::single_pass_traversal_tag>
{
public:
EntryIterator()
: g()
{
}
EntryIterator(EntryGenerator* g)
: g(g)
{
}
private:
friend class boost::iterator_core_access;
Entry& dereference() const
{
if (!g || !g->valid())
throw std::runtime_error("...");
return g->get();
}
bool equal(EntryIterator const& l) const
{
return g == l.g || ((!g || !g->valid()) && (!l.g || !l.g->valid()));
}
void increment()
{
if (g)
g->next();
}
EntryGenerator* g;
};
class CellEntryGenerator : public EntryGenerator
{
public:
CellEntryGenerator()
{
Eigen::Vector3d values;
values << 4.0, 2.7, 4.2;
Eigen::Vector3i numbers;
numbers << 1, 2, 3;
entries.push_back(std::make_pair(values, numbers));
entries.push_back(std::make_pair(values, numbers));
entries.push_back(std::make_pair(values, numbers));
entries.push_back(std::make_pair(values, numbers));
}
bool valid() override
{
return current_entry < entries.size();
}
void next() override
{
current_entry++;
}
Entry& get() override
{
return entries[current_entry];
}
EntryIterator begin() override
{
return EntryIterator(this);
}
EntryIterator end() override
{
return EntryIterator();
}
private:
std::vector<Entry> entries;
int current_entry = 0;
};
int main()
{
CellEntryGenerator generator;
for (Entry& vecPlusNumbering : generator)
{
std::cout << "Values of this cell:\n" << vecPlusNumbering.first << std::endl;
std::cout << "Numbering of these values:\n" << vecPlusNumbering.second << std::endl;
}
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment