How to compose generators with STL algorithms - c++

I have an algorithm which generates combinations from entries of a container and I want to find the combination which minimizes a cost function:
struct Vec { double x; double y; };
double cost( Vec a, Vec b ) {
double dx = a.x - b.x;
double dy = a.y - b.y;
return dx*dx + dy*dy;
}
pair<Vec,Vec> get_pair_with_minimum_cost ( vector<Vec> inp, double (*cost_fun)(Vec,Vec) )
{
pair<Vec,Vec> result;
double min_cost = FLT_MAX;
size_t sz = inp.size();
for(size_t i=0; i<sz; i++) {
for (size_t j=i; j<sz; j++) {
double cost = cost_fun(inp[i], inp[j]);
if (cost < min_cost) {
min_cost = cost;
result = make_pair(inp[i], inp[j]);
}
}
}
return result;
}
vector <Vec> inp = {....};
auto best_pair = get_pair_with_minimum_cost ( inp, cost );
Unfortunately, get_pair_with_minimum_cost() does 2 jobs:
generates the combinations
gets the minimum element
I could break them in two functions, like:
the generator:
template <class Func>
void generate_all_combinations_of( vector<Vec> inp, Func fun )
{
size_t sz = inp.size();
for(size_t i=0; i<sz; i++) {
for (size_t j=i; j<sz; j++) {
fun(make_pair(inp[i], inp[j]));
}
}
}
and then use std::min_element on the output of the generator, i.e.
vector<Vec> inp = {....};
vector<pair<Vec,Vec>> all_combinations;
generate_all_combinations_of(inp, [&](vector<pair<Vec,Vec>> o){all_combinations.push_back(o); } );
auto best_pair = *min_element(all_combinations.begin(), all_combinations.end(), cost);
but I do not want the pay the cost of creating and extra container with temporary data (all_combinations).
Questions:
Can I rewrite the generate_all_combinations_of() such that it uses yield or the new std::ranges in such a way that I can combine it with STL algorithms such as find_if, any_of, min_element or even adjacent_pair ?
The great thing about this 'generator' function is that it is easy to read, so I would like to keep it as readable as possible.
NB: some of these algorithms need to break the loop.
What is the official name of combining entries this way?
It this the combinations used in 'bubble-sort'.

Here's how I would write the function in c++20, using range views and algorithms so that there isn't a separate container that stores the intermediate results:
double get_minimum_cost(auto const & inp)
{
namespace rs = std::ranges;
namespace rv = std::ranges::views;
// for each i compute the minimum cost for all j's
auto min_cost_from_i = [&](auto i)
{
auto costs_from_i = rv::iota(i + 1, inp.size())
| rv::transform([&](auto j)
{
return cost(inp[i], inp[j]);
});
return *rs::min_element(costs_from_i);
};
// compute min costs for all i's
auto all_costs = rv::iota(0u, inp.size())
| rv::transform(min_cost_from_i);
return *rs::min_element(all_costs);
}
Here's a demo.
Note that the solution doesn't compare the cost between same elements, since the cost function example you showed would have a trivial result of 0. For a cost function that doesn't return 0, you can adapt the solution to generate a range from i instead of i + 1. Also, if the cost function is not symmetric, make that range start from 0 instead of i.
Also, this function has UB if you call it with an empty range, so you should check for that as well.

There is http://www.open-std.org/jtc1/sc22/wg21/docs/papers/2020/p2168r0.pdf who's development I would follow
If you are using MSVC, and can use their experimental/generator (not sure if others support it yet), you can use
std::experimental::generator<std::size_t> Generate(std::size_t const end){
for(std::size_t i = 0; i < end; ++i)
co_yield i;
}
int main(){
auto vals = Generate(22);
auto const result = *std::min_element(std::begin(vals),std::end(vals));
std::cout <<'\n' << " " << result;
}
Here you would need to modify the Generate function to Yield a pair/or to yield cost
(My recommendation would be to Keep things simple and yield the cost)
Then use vals to find min_cost
Ranges
Based on what I can find about the Ranges Proposal, it works on the basis of std::begin and std::end both of which experimental::generator provides
So it should probably work

Here's how I would write the function in c++17, using algorithms' min_element function, with no need for a separate container that stores the intermediate results. I know you were looking for a c++20 solution, but this code does work fine under c++20, and perhaps it gives you some ideas about adapting functions to ranges when the range isn't just one of the ranges supplied by c++20's ranges library.
// TwoContainerRanger is an iterable container where the iterator consists
// of two indices that match the given filter, and whose iterators, when
// dereferenced, return the result of calling func with the
// elements of the two containers, at those two indices.
// filter can be nullptr.
template <typename Container1, typename Container2, typename Func>
struct TwoContainerRanger {
Container1 &c1;
Container2 &c2;
const Func &fun;
bool (*restriction)(size_t i1, size_t i2);
TwoContainerRanger(Container1 &container1, Container2 &container2,
bool (*filter)(size_t i1, size_t i2), const Func &func)
: c1(container1), c2(container2), fun(func), restriction(filter) {}
struct Iterator {
const TwoContainerRanger *gen;
size_t index1, index2;
auto &operator++() {
do {
if (++index1 == gen->c1.size()) {
if (++index2 == gen->c2.size()) {
// we leave both indices pointing to the end
// to indicate that we have reached the end.
return *this;
} else {
index1 = 0u;
}
}
} while (gen->restriction && gen->restriction(index1, index2) == false);
return *this;
}
bool operator==(const Iterator &other) const = default;
bool operator!=(const Iterator &other) const = default;
auto operator*() const {
return gen->fun(gen->c1[index1], gen->c2[index2]);
}
};
Iterator begin() {
Iterator b{this, size_t(0) - 1, 0u};
return ++b; // automatically applies the restriction
}
Iterator end() { return Iterator{this, c1.size(), c2.size()}; }
};
Calling it looks like this:
int main() {
std::array<Vec, 5> ar = {Vec{0, 0}, Vec{1, 1}, Vec{3, 3}, Vec{7, 7},
Vec{3.1, 3.1}};
TwoContainerRanger tcr{ar, ar, Triangle, cost};
auto result = std::min_element(tcr.begin(), tcr.end());
std::cout << "Min was at (" << result.index1 << "," << result.index2
<< "); cost was " << *result << '\n';
}
Here's a demo.

Related

Parallel for loop over range of array indices in C++17

I need to update a 100M-element array and would like to do it in parallel. std::for_each(std::execution::par, ...) seems great for this, except that the update needs to access elements of other arrays depending on the index that I am updating. A minimal serial working example of the kind of thing I'm trying to parallelize might look like this:
for (size_t i = 0; i < 100'000'000; i++)
d[i] = combine(d[i], s[2*i], s[2*i+1]);
I could of course manually spawn threads, but that is a lot more code than std::for_each, so it would be great to find an elegant way to do this with the standard library. So far I have found some not very elegant ways of using for_each, for instance:
Compute the index by using pointer arithmetic on the address of the array element.
Implement my own bogus iterator in the spirit of boost's counting_range.
Is there a better way to do this?
std::ranges should be able to help if you have access to c++20, you can iterate over the indexes rather than your data:
#include <ranges>
#include <vector>
#include <algorithm>
#include <iostream>
int main() {
std::vector<int> d(100);
std::ranges::iota_view indexes((size_t)0, d.size());
std::for_each(std::execution::par, indexes.begin(), indexes.end(), [&d](size_t i)
{
std::cout << i << "," << d[i] << "\n";
});
return 0;
}
You should be able to iterate over the indexes rather than the items. I think C++20 std::ranges gives you an easy way to do this, or you can use one of the Boost range methods. I'm not sure why you would consider rolling your own in the spirit of Boost counting_range when you could just, well, use Boost :-)
Having said that, I've actually opted for that roll-your-own approach, simply to make the code self-contained with neither C++20 nor Boost: feel free to replace paxrange with one of the other methods depending on your needs:
#include <iostream>
#include <algorithm>
// Seriously, just use Boost :-)
class paxrange {
public:
class iterator {
friend class paxrange;
public:
long int operator *() const { return value; }
const iterator &operator ++() { ++value; return *this; }
iterator operator ++(int) { iterator copy(*this); ++value; return copy; }
bool operator ==(const iterator &other) const { return value == other.value; }
bool operator !=(const iterator &other) const { return value != other.value; }
protected:
iterator(long int start) : value (start) { }
private:
unsigned long value;
};
iterator begin() const { return beginVal; }
iterator end() const { return endVal; }
paxrange(long int begin, long int end) : beginVal(begin), endVal(end) {}
private:
iterator beginVal;
iterator endVal;
};
int main() {
// Create a source and destination collection.
std::vector<int> s;
s.push_back(42); s.push_back(77); s.push_back(144);
s.push_back(12); s.push_back(6);
std::vector<int> d(5);
// Shows how to use indexes with multiple collections sharing index.
auto process = [s, &d](const int idx) { d[idx] = s[idx] + idx; };
paxrange x(0, d.size());
std::for_each(x.begin(), x.end(), process); // add parallelism later.
// Debug output.
for (const auto &item: s) std::cout << "< " << item << '\n';
std::cout << "=====\n";
for (const auto &item: d) std::cout << "> " << item << '\n';
}
The "meat" of the solution is the three lines in the middle of main(), where you set up a function for call-backs, one that takes the index rather than the item itself.
Inside that function, you use that index plus as many collections as needed, to set up the destination collection, very similar to what you desire.
In my case, I simply wanted the output vector to be the input vector but with the index added to each element, as per the output:
< 42
< 77
< 144
< 12
< 6
=====
> 42
> 78
> 146
> 15
> 10
There is a simple header-only library in Github which might help you.
Your minimal example can be parallelized like this. However, presumably due to cache cooling, the runtime will not scale down linearly with the number of cores.
#include "Lazy.h"
double combine(double a, double b, double c)
{
if (b > 0.5 && c < 0.4)
return a + std::exp(b * c + 1);
else if (b*c < 0.2)
return a * 0.8 + (1-c) * (1-b);
else
return std::exp(1.0 / a) + b + c;
}
// Generate index split for parallel tasks
auto getIndexPairs(std::size_t N, std::size_t numSplits)
{
std::vector<std::pair<std::size_t, std::size_t>> vecPairs(numSplits);
double dFrom = 0, dTo = 0;
for (auto i = 0; i < numSplits; ++i) {
dFrom = dTo;
dTo += N / double(numSplits);
vecPairs[i] = {std::size_t(dFrom), std::min(std::size_t(dTo), N)};
}
vecPairs[numSplits-1].second = N;
return vecPairs;
}
int main(int argc, char** argv) {
const std::size_t N = 100000000;
const std::size_t C = std::thread::hardware_concurrency(); // Number of parallel finder threads
std::vector<double> d(N);
std::vector<double> s(2*N);
// Fill d and s with some values
for (std::size_t i = 0; i < N; ++i) {
s[i] = double(i) / N;
s[i + N] = double(i + N) / N;
d[i] = N - i;
}
// Run combine(...) in parallel in C threads
Lazy::runForAll(getIndexPairs(N, C), [&](auto pr) {
for (int i=pr.first; i<pr.second; ++i)
d[i] = combine(d[i], s[2*i], s[2*i+1]);
return nullptr; // Dummy return value
});
}
#Alan Birtles answer does not work with a parallel execution policy, as it errors out to "static_assert failed: 'Parallel algorithms require forward iterators or stronger.'".
A potential alternative is to make an iterator vector, but it won't be as space-efficient.
std::vector<std::size_t> indexes(d.size());
std::iota(indexes.begin(), indexes.end(), 0);
std::for_each(std::execution::par, indexes.begin(), indexes.end(), [&](size_t i) {
std::cout << i << ',' << d[i] << '\n';
}

How can I generate the cartesian product of some vectors, whose number is given at runtime in C++? [duplicate]

I've a vector of vectors say vector<vector<int> > items of different sizes like as follows
1,2,3
4,5
6,7,8
I want to create combinations in terms of Cartesian product of these vectors like
1,4,6
1,4,7
1,4,8
and so on till
3,5,8
How can I do that ? I've looked up several links and I've also listed them at the end of this post but I'm not able to interpret that as I'm not that familiar with the language. Could some body help me with this.
#include <iostream>
#include <iomanip>
#include <vector>
using namespace std;
int main()
{
vector<vector<int> > items;
int k = 0;
for ( int i = 0; i < 5; i++ ) {
items.push_back ( vector<int>() );
for ( int j = 0; j < 5; j++ )
items[i].push_back ( k++ );
}
cartesian ( items ); // I want some function here to do this.
}
This program has equal length vectors and I put this so that it will be easier to understand my data structure. It will be very helpful even if somebody uses others answers from other links and integrate with this to get the result. Thank you very much
Couple of links I looked at
one
Two
Program from : program
First, I'll show you a recursive version.
// Cartesion product of vector of vectors
#include <vector>
#include <iostream>
#include <iterator>
// Types to hold vector-of-ints (Vi) and vector-of-vector-of-ints (Vvi)
typedef std::vector<int> Vi;
typedef std::vector<Vi> Vvi;
// Just for the sample -- populate the intput data set
Vvi build_input() {
Vvi vvi;
for(int i = 0; i < 3; i++) {
Vi vi;
for(int j = 0; j < 3; j++) {
vi.push_back(i*10+j);
}
vvi.push_back(vi);
}
return vvi;
}
// just for the sample -- print the data sets
std::ostream&
operator<<(std::ostream& os, const Vi& vi)
{
os << "(";
std::copy(vi.begin(), vi.end(), std::ostream_iterator<int>(os, ", "));
os << ")";
return os;
}
std::ostream&
operator<<(std::ostream& os, const Vvi& vvi)
{
os << "(\n";
for(Vvi::const_iterator it = vvi.begin();
it != vvi.end();
it++) {
os << " " << *it << "\n";
}
os << ")";
return os;
}
// recursive algorithm to to produce cart. prod.
// At any given moment, "me" points to some Vi in the middle of the
// input data set.
// for int i in *me:
// add i to current result
// recurse on next "me"
//
void cart_product(
Vvi& rvvi, // final result
Vi& rvi, // current result
Vvi::const_iterator me, // current input
Vvi::const_iterator end) // final input
{
if(me == end) {
// terminal condition of the recursion. We no longer have
// any input vectors to manipulate. Add the current result (rvi)
// to the total set of results (rvvvi).
rvvi.push_back(rvi);
return;
}
// need an easy name for my vector-of-ints
const Vi& mevi = *me;
for(Vi::const_iterator it = mevi.begin();
it != mevi.end();
it++) {
// final rvi will look like "a, b, c, ME, d, e, f"
// At the moment, rvi already has "a, b, c"
rvi.push_back(*it); // add ME
cart_product(rvvi, rvi, me+1, end); add "d, e, f"
rvi.pop_back(); // clean ME off for next round
}
}
// sample only, to drive the cart_product routine.
int main() {
Vvi input(build_input());
std::cout << input << "\n";
Vvi output;
Vi outputTemp;
cart_product(output, outputTemp, input.begin(), input.end());
std::cout << output << "\n";
}
Now, I'll show you the recursive iterative version that I shamelessly stole from #John :
The rest of the program is pretty much the same, only showing the cart_product function.
// Seems like you'd want a vector of iterators
// which iterate over your individual vector<int>s.
struct Digits {
Vi::const_iterator begin;
Vi::const_iterator end;
Vi::const_iterator me;
};
typedef std::vector<Digits> Vd;
void cart_product(
Vvi& out, // final result
Vvi& in) // final result
{
Vd vd;
// Start all of the iterators at the beginning.
for(Vvi::const_iterator it = in.begin();
it != in.end();
++it) {
Digits d = {(*it).begin(), (*it).end(), (*it).begin()};
vd.push_back(d);
}
while(1) {
// Construct your first product vector by pulling
// out the element of each vector via the iterator.
Vi result;
for(Vd::const_iterator it = vd.begin();
it != vd.end();
it++) {
result.push_back(*(it->me));
}
out.push_back(result);
// Increment the rightmost one, and repeat.
// When you reach the end, reset that one to the beginning and
// increment the next-to-last one. You can get the "next-to-last"
// iterator by pulling it out of the neighboring element in your
// vector of iterators.
for(Vd::iterator it = vd.begin(); ; ) {
// okay, I started at the left instead. sue me
++(it->me);
if(it->me == it->end) {
if(it+1 == vd.end()) {
// I'm the last digit, and I'm about to roll
return;
} else {
// cascade
it->me = it->begin;
++it;
}
} else {
// normal
break;
}
}
}
}
Here is a solution in C++11.
The indexing of the variable-sized arrays can be done eloquently with modular arithmetic.
The total number of lines in the output is the product of the sizes of the input vectors. That is:
N = v[0].size() * v[1].size() * v[2].size()
Therefore the main loop has n as the iteration variable, from 0 to N-1. In principle, each value of n encodes enough information to extract each of the indices of v for that iteration. This is done in a subloop using repeated modular arithmetic:
#include <cstdlib>
#include <iostream>
#include <numeric>
#include <vector>
using namespace std;
void cartesian( vector<vector<int> >& v ) {
auto product = []( long long a, vector<int>& b ) { return a*b.size(); };
const long long N = accumulate( v.begin(), v.end(), 1LL, product );
vector<int> u(v.size());
for( long long n=0 ; n<N ; ++n ) {
lldiv_t q { n, 0 };
for( long long i=v.size()-1 ; 0<=i ; --i ) {
q = div( q.quot, v[i].size() );
u[i] = v[i][q.rem];
}
// Do what you want here with u.
for( int x : u ) cout << x << ' ';
cout << '\n';
}
}
int main() {
vector<vector<int> > v { { 1, 2, 3 },
{ 4, 5 },
{ 6, 7, 8 } };
cartesian(v);
return 0;
}
Output:
1 4 6
1 4 7
1 4 8
...
3 5 8
Shorter code:
vector<vector<int>> cart_product (const vector<vector<int>>& v) {
vector<vector<int>> s = {{}};
for (const auto& u : v) {
vector<vector<int>> r;
for (const auto& x : s) {
for (const auto y : u) {
r.push_back(x);
r.back().push_back(y);
}
}
s = move(r);
}
return s;
}
Seems like you'd want a vector of iterators which iterate over your individual vector<int>s.
Start all of the iterators at the beginning. Construct your first product vector by pulling out the element of each vector via the iterator.
Increment the rightmost one, and repeat.
When you reach the end, reset that one to the beginning and increment the next-to-last one. You can get the "next-to-last" iterator by pulling it out of the neighboring element in your vector of iterators.
Continue cycling through until both the last and next-to-last iterators are at the end. Then, reset them both, increment the third-from-last iterator. In general, this can be cascaded.
It's like an odometer, but with each different digit being in a different base.
Here's my solution. Also iterative, but a little shorter than the above...
void xp(const vector<vector<int>*>& vecs, vector<vector<int>*> *result) {
vector<vector<int>*>* rslts;
for (int ii = 0; ii < vecs.size(); ++ii) {
const vector<int>& vec = *vecs[ii];
if (ii == 0) {
// vecs=[[1,2],...] ==> rslts=[[1],[2]]
rslts = new vector<vector<int>*>;
for (int jj = 0; jj < vec.size(); ++jj) {
vector<int>* v = new vector<int>;
v->push_back(vec[jj]);
rslts->push_back(v);
}
} else {
// vecs=[[1,2],[3,4],...] ==> rslts=[[1,3],[1,4],[2,3],[2,4]]
vector<vector<int>*>* tmp = new vector<vector<int>*>;
for (int jj = 0; jj < vec.size(); ++jj) { // vec[jj]=3 (first iter jj=0)
for (vector<vector<int>*>::const_iterator it = rslts->begin();
it != rslts->end(); ++it) {
vector<int>* v = new vector<int>(**it); // v=[1]
v->push_back(vec[jj]); // v=[1,3]
tmp->push_back(v); // tmp=[[1,3]]
}
}
for (int kk = 0; kk < rslts->size(); ++kk) {
delete (*rslts)[kk];
}
delete rslts;
rslts = tmp;
}
}
result->insert(result->end(), rslts->begin(), rslts->end());
delete rslts;
}
I derived it with some pain from a haskell version I wrote:
xp :: [[a]] -> [[a]]
xp [] = []
xp [l] = map (:[]) l
xp (h:t) = foldr (\x acc -> foldr (\l acc -> (x:l):acc) acc (xp t)) [] h
Since I needed the same functionality, I implemented an iterator which computes the Cartesian product on the fly, as needed, and iterates over it.
It can be used as follows.
#include <forward_list>
#include <iostream>
#include <vector>
#include "cartesian.hpp"
int main()
{
// Works with a vector of vectors
std::vector<std::vector<int>> test{{1,2,3}, {4,5,6}, {8,9}};
CartesianProduct<decltype(test)> cp(test);
for(auto const& val: cp) {
std::cout << val.at(0) << ", " << val.at(1) << ", " << val.at(2) << "\n";
}
// Also works with something much less, like a forward_list of forward_lists
std::forward_list<std::forward_list<std::string>> foo{{"boo", "far", "zab"}, {"zoo", "moo"}, {"yohoo", "bohoo", "whoot", "noo"}};
CartesianProduct<decltype(foo)> bar(foo);
for(auto const& val: bar) {
std::cout << val.at(0) << ", " << val.at(1) << ", " << val.at(2) << "\n";
}
}
The file cartesian.hpp looks like this.
#include <cassert>
#include <limits>
#include <stdexcept>
#include <vector>
#include <boost/iterator/iterator_facade.hpp>
//! Class iterating over the Cartesian product of a forward iterable container of forward iterable containers
template<typename T>
class CartesianProductIterator : public boost::iterator_facade<CartesianProductIterator<T>, std::vector<typename T::value_type::value_type> const, boost::forward_traversal_tag>
{
public:
//! Delete default constructor
CartesianProductIterator() = delete;
//! Constructor setting the underlying iterator and position
/*!
* \param[in] structure The underlying structure
* \param[in] pos The position the iterator should be initialized to. std::numeric_limits<std::size_t>::max()stands for the end, the position after the last element.
*/
explicit CartesianProductIterator(T const& structure, std::size_t pos);
private:
//! Give types more descriptive names
// \{
typedef T OuterContainer;
typedef typename T::value_type Container;
typedef typename T::value_type::value_type Content;
// \}
//! Grant access to boost::iterator_facade
friend class boost::iterator_core_access;
//! Increment iterator
void increment();
//! Check for equality
bool equal(CartesianProductIterator<T> const& other) const;
//! Dereference iterator
std::vector<Content> const& dereference() const;
//! The part we are iterating over
OuterContainer const& structure_;
//! The position in the Cartesian product
/*!
* For each element of structure_, give the position in it.
* The empty vector represents the end position.
* Note that this vector has a size equal to structure->size(), or is empty.
*/
std::vector<typename Container::const_iterator> position_;
//! The position just indexed by an integer
std::size_t absolutePosition_ = 0;
//! The begin iterators, saved for convenience and performance
std::vector<typename Container::const_iterator> cbegins_;
//! The end iterators, saved for convenience and performance
std::vector<typename Container::const_iterator> cends_;
//! Used for returning references
/*!
* We initialize with one empty element, so that we only need to add more elements in increment().
*/
mutable std::vector<std::vector<Content>> result_{std::vector<Content>()};
//! The size of the instance of OuterContainer
std::size_t size_ = 0;
};
template<typename T>
CartesianProductIterator<T>::CartesianProductIterator(OuterContainer const& structure, std::size_t pos) : structure_(structure)
{
for(auto & entry: structure_) {
cbegins_.push_back(entry.cbegin());
cends_.push_back(entry.cend());
++size_;
}
if(pos == std::numeric_limits<std::size_t>::max() || size_ == 0) {
absolutePosition_ = std::numeric_limits<std::size_t>::max();
return;
}
// Initialize with all cbegin() position
position_.reserve(size_);
for(std::size_t i = 0; i != size_; ++i) {
position_.push_back(cbegins_[i]);
if(cbegins_[i] == cends_[i]) {
// Empty member, so Cartesian product is empty
absolutePosition_ = std::numeric_limits<std::size_t>::max();
return;
}
}
// Increment to wanted position
for(std::size_t i = 0; i < pos; ++i) {
increment();
}
}
template<typename T>
void CartesianProductIterator<T>::increment()
{
if(absolutePosition_ == std::numeric_limits<std::size_t>::max()) {
return;
}
std::size_t pos = size_ - 1;
// Descend as far as necessary
while(++(position_[pos]) == cends_[pos] && pos != 0) {
--pos;
}
if(position_[pos] == cends_[pos]) {
assert(pos == 0);
absolutePosition_ = std::numeric_limits<std::size_t>::max();
return;
}
// Set all to begin behind pos
for(++pos; pos != size_; ++pos) {
position_[pos] = cbegins_[pos];
}
++absolutePosition_;
result_.emplace_back();
}
template<typename T>
std::vector<typename T::value_type::value_type> const& CartesianProductIterator<T>::dereference() const
{
if(absolutePosition_ == std::numeric_limits<std::size_t>::max()) {
throw new std::out_of_range("Out of bound dereference in CartesianProductIterator\n");
}
auto & result = result_[absolutePosition_];
if(result.empty()) {
result.reserve(size_);
for(auto & iterator: position_) {
result.push_back(*iterator);
}
}
return result;
}
template<typename T>
bool CartesianProductIterator<T>::equal(CartesianProductIterator<T> const& other) const
{
return absolutePosition_ == other.absolutePosition_ && structure_ == other.structure_;
}
//! Class that turns a forward iterable container of forward iterable containers into a forward iterable container which iterates over the Cartesian product of the forward iterable containers
template<typename T>
class CartesianProduct
{
public:
//! Constructor from type T
explicit CartesianProduct(T const& t) : t_(t) {}
//! Iterator to beginning of Cartesian product
CartesianProductIterator<T> begin() const { return CartesianProductIterator<T>(t_, 0); }
//! Iterator behind the last element of the Cartesian product
CartesianProductIterator<T> end() const { return CartesianProductIterator<T>(t_, std::numeric_limits<std::size_t>::max()); }
private:
T const& t_;
};
If someone has comments how to make it faster or better, I'd highly appreciate them.
I was just forced to implement this for a project I was working on and I came up with the code below. It can be stuck in a header and it's use is very simple but it returns all of the combinations you can get from a vector of vectors. The array that it returns only holds integers. This was a conscious decision because I just wanted the indices. In this way, I could index into each of the vector's vector and then perform the calculations I/anyone would need... best to avoid letting CartesianProduct hold "stuff" itself, it is a mathematical concept based around counting not a data structure. I'm fairly new to c++ but this was tested in a decryption algorithm pretty thoroughly. There is some light recursion but overall this is a simple implementation of a simple counting concept.
// Use of the CartesianProduct class is as follows. Give it the number
// of rows and the sizes of each of the rows. It will output all of the
// permutations of these numbers in their respective rows.
// 1. call cp.permutation() // need to check all 0s.
// 2. while cp.HasNext() // it knows the exit condition form its inputs.
// 3. cp.Increment() // Make the next permutation
// 4. cp.permutation() // get the next permutation
class CartesianProduct{
public:
CartesianProduct(int num_rows, vector<int> sizes_of_rows){
permutation_ = new int[num_rows];
num_rows_ = num_rows;
ZeroOutPermutation();
sizes_of_rows_ = sizes_of_rows;
num_max_permutations_ = 1;
for (int i = 0; i < num_rows; ++i){
num_max_permutations_ *= sizes_of_rows_[i];
}
}
~CartesianProduct(){
delete permutation_;
}
bool HasNext(){
if(num_permutations_processed_ != num_max_permutations_) {
return true;
} else {
return false;
}
}
void Increment(){
int row_to_increment = 0;
++num_permutations_processed_;
IncrementAndTest(row_to_increment);
}
int* permutation(){
return permutation_;
}
int num_permutations_processed(){
return num_permutations_processed_;
}
void PrintPermutation(){
cout << "( ";
for (int i = 0; i < num_rows_; ++i){
cout << permutation_[i] << ", ";
}
cout << " )" << endl;
}
private:
int num_permutations_processed_;
int *permutation_;
int num_rows_;
int num_max_permutations_;
vector<int> sizes_of_rows_;
// Because CartesianProduct is called first initially with it's values
// of 0 and because those values are valid and important output
// of the CartesianProduct we increment the number of permutations
// processed here when we populate the permutation_ array with 0s.
void ZeroOutPermutation(){
for (int i = 0; i < num_rows_; ++i){
permutation_[i] = 0;
}
num_permutations_processed_ = 1;
}
void IncrementAndTest(int row_to_increment){
permutation_[row_to_increment] += 1;
int max_index_of_row = sizes_of_rows_[row_to_increment] - 1;
if (permutation_[row_to_increment] > max_index_of_row){
permutation_[row_to_increment] = 0;
IncrementAndTest(row_to_increment + 1);
}
}
};
#include <iostream>
#include <vector>
void cartesian (std::vector<std::vector<int>> const& items) {
auto n = items.size();
auto next = [&](std::vector<int> & x) {
for ( int i = 0; i < n; ++ i )
if ( ++x[i] == items[i].size() ) x[i] = 0;
else return true;
return false;
};
auto print = [&](std::vector<int> const& x) {
for ( int i = 0; i < n; ++ i )
std::cout << items[i][x[i]] << ",";
std::cout << "\b \n";
};
std::vector<int> x(n);
do print(x); while (next(x)); // Shazam!
}
int main () {
std::vector<std::vector<int>>
items { { 1, 2, 3 }, { 4, 5 }, { 6, 7, 8 } };
cartesian(items);
return 0;
}
The idea behind this is as follows.
Let n := items.size().
Let m_i := items[i].size(), for all i in {0,1,...,n-1}.
Let M := {0,1,...,m_0-1} x {0,1,...,m_1-1} x ... x {0,1,...,m_{n-1}-1}.
We first solve the simpler problem of iterating through M. This is accomplished by the next lambda. The algorithm is simply the "carrying" routine grade schoolers use to add 1, albeit with a mixed radix number system.
We use this to solve the more general problem by transforming a tuple x in M to one of the desired tuples via the formula items[i][x[i]] for all i in {0,1,...,n-1}. We perform this transformation in the print lambda.
We then perform the iteration with do print(x); while (next(x));.
Now some comments on complexity, under the assumption that m_i > 1 for all i:
This algorithm requires O(n) space. Note that explicit construction of the Cartesian product takes O(m_0 m_1 m_2 ... m_{n-1}) >= O(2^n) space. So this is exponentially better on space than any algorithm which requires all tuples to be stored simultaneously in memory.
The next function takes amortized O(1) time (by a geometric series argument).
The print function takes O(n) time.
Hence, altogether, the algorithm has time complexity O(n|M|) and space complexity O(n) (not counting the cost of storing items).
An interesting thing to note is that if print is replaced with a function which inspects on average only O(1) coordinates per tuple rather than all of them, then time complexity falls to O(|M|), that is, it becomes linear time with respect to the size of the Cartesian product. In other words, avoiding the copy of the tuple each iterate can be meaningful in some situations.
This version supports no iterators or ranges, but it is a simple direct implementation that uses the multiplication operator to represent the Cartesian product, and a lambda to perform the action.
The interface is designed with the particular functionality I needed. I needed the flexibility to choose vectors over which to apply the Cartesian product in a way that did not obscure the code.
int main()
{
vector< vector<long> > v{ { 1, 2, 3 }, { 4, 5 }, { 6, 7, 8 } };
(Cartesian<long>(v[0]) * v[1] * v[2]).ForEach(
[](long p_Depth, long *p_LongList)
{
std::cout << p_LongList[0] << " " << p_LongList[1] << " " << p_LongList[2] << std::endl;
}
);
}
The implementation uses recursion up the class structure to implement the embedded for loops over each vector. The algorithm works directly on the input vectors, requiring no large temporary arrays. It is simple to understand and debug.
The use of std::function p_Action instead of void p_Action(long p_Depth, T *p_ParamList) for the lambda parameter would allow me to capture local variables, if I wanted to. In the above call, I don't.
But you knew that, didn't you. "function" is a template class which takes the type parameter of a function and makes it callable.
#include <vector>
#include <iostream>
#include <functional>
#include <string>
using namespace std;
template <class T>
class Cartesian
{
private:
vector<T> &m_Vector;
Cartesian<T> *m_Cartesian;
public:
Cartesian(vector<T> &p_Vector, Cartesian<T> *p_Cartesian=NULL)
: m_Vector(p_Vector), m_Cartesian(p_Cartesian)
{};
virtual ~Cartesian() {};
Cartesian<T> *Clone()
{
return new Cartesian<T>(m_Vector, m_Cartesian ? m_Cartesian->Clone() : NULL);
};
Cartesian<T> &operator *=(vector<T> &p_Vector)
{
if (m_Cartesian)
(*m_Cartesian) *= p_Vector;
else
m_Cartesian = new Cartesian(p_Vector);
return *this;
};
Cartesian<T> operator *(vector<T> &p_Vector)
{
return (*Clone()) *= p_Vector;
};
long Depth()
{
return m_Cartesian ? 1 + m_Cartesian->Depth() : 1;
};
void ForEach(function<void (long p_Depth, T *p_ParamList)> p_Action)
{
Loop(0, new T[Depth()], p_Action);
};
private:
void Loop(long p_Depth, T *p_ParamList, function<void (long p_Depth, T *p_ParamList)> p_Action)
{
for (T &element : m_Vector)
{
p_ParamList[p_Depth] = element;
if (m_Cartesian)
m_Cartesian->Loop(p_Depth + 1, p_ParamList, p_Action);
else
p_Action(Depth(), p_ParamList);
}
};
};

Find max/min of vector of vectors

What is the most efficient and standard (C++11/14) way to find the max/min item of vector of vectors?
std::vector<std::vector<double>> some_values{{5,0,8},{3,1,9}};
the wanted max element is 9
the wanted min element is 0
Here's a multi-threaded solution that returns an iterator (or throws) to the maximum for general type T (assuming operator< is defined for T). Note the most important optimisation is to perform the inner max operations on the 'columns' to exploit C++'s column-major ordering.
#include <vector>
#include <algorithm>
template <typename T>
typename std::vector<T>::const_iterator max_element(const std::vector<std::vector<T>>& values)
{
if (values.empty()) throw std::runtime_error {"values cannot be empty"};
std::vector<std::pair<typename std::vector<T>::const_iterator, bool>> maxes(values.size());
threaded_transform(values.cbegin(), values.cend(), maxes.begin(),
[] (const auto& v) {
return std::make_pair(std::max_element(v.cbegin(), v.cend()), v.empty());
});
auto it = std::remove_if(maxes.begin(), maxes.end(), [] (auto p) { return p.second; });
if (it == maxes.begin()) throw std::runtime_error {"values cannot be empty"};
return std::max_element(maxes.begin(), it,
[] (auto lhs, auto rhs) {
return *lhs.first < *rhs.first;
})->first;
}
threaded_transform is not part of the standard library (yet), but here's an implementation you could use.
#include <vector>
#include <thread>
#include <algorithm>
#include <cstddef>
template <typename InputIterator, typename OutputIterator, typename UnaryOperation>
OutputIterator threaded_transform(InputIterator first, InputIterator last, OutputIterator result, UnaryOperation op, unsigned num_threads)
{
std::size_t num_values_per_threads = std::distance(first, last) / num_threads;
std::vector<std::thread> threads;
threads.reserve(num_threads);
for (int i = 1; i <= num_threads; ++i) {
if (i == num_threads) {
threads.push_back(std::thread(std::transform<InputIterator,
OutputIterator, UnaryOperation>,
first, last, result, op));
} else {
threads.push_back(std::thread(std::transform<InputIterator,
OutputIterator, UnaryOperation>,
first, first + num_values_per_threads,
result, op));
}
first += num_values_per_threads;
result += num_values_per_threads;
}
for (auto& thread : threads) thread.join();
return result;
}
template <typename InputIterator, typename OutputIterator, typename UnaryOperation>
OutputIterator threaded_transform(InputIterator first, InputIterator last, OutputIterator result, UnaryOperation op)
{
return threaded_transform<InputIterator, OutputIterator, UnaryOperation>(first, last, result, op, std::thread::hardware_concurrency());
}
If you used a boost::multi_array<double, 2> instead of a std::vector<std::vector<double>> it would be as simple as:
auto minmax = std::minmax_element(values.data(), values.data() + values.num_elements());
Live demo.
The plain for loop way:
T max_e = std::numeric_limits<T>::min();
for(const auto& v: vv) {
for(const auto& e: v) {
max_e = std::max(max_e, e);
}
}
You must at least look at every element, so, as Anony-mouse mentioned, complexity will be at least O(n^2).
#include <vector>
#include <limits>
#include <algorithm>
int main() {
std::vector<std::vector<double>> some_values;
double max = std::numeric_limits<double>::lowest();
for (const auto& v : some_values)
{
double current_max = *std::max_element(v.cbegin(), v.cend());
max = max < current_max ? current_max : max; // max = std::max(current_max, max);
}
}
You can do it pretty easily with Eric Niebler's range-v3 library (which obviously isn't standard yet, but hopefully will be in the not-too-distant future):
vector<vector<double>> some_values{{5,0,8},{3,1,9}};
auto joined = some_values | ranges::view::join;
auto p = std::minmax_element(joined.begin(), joined.end());
p.first is an iterator to the min element; p.second to the max.
(range-v3 does have an implementation of minmax_element, but unfortunately, it requires a ForwardRange and view::join only gives me an InputRange, so I can't use it.)
Any efficient way to calculate the maximum element in a 2-D array(or vector in your case) involves a complexity of O(n^2) irrespective of what you do, as the calculation involves a comparison between n*n elements.Best way in terms of ease of use is to use std::max_element on the vector of vectors.I will not delve into details.Here is the reference.
If you create a custom iterator to iterate over all double of your vector of vector, a simple std::minmax_element do the job
iterator is something like:
class MyIterator : public std::iterator<std::random_access_iterator_tag, double>
{
public:
MyIterator() : container(nullptr), i(0), j(0) {}
MyIterator(const std::vector<std::vector<double>>& container,
std::size_t i,
std::size_t j) : container(&container), i(i), j(j)
{
// Skip empty container
if (i < container.size() && container[i].empty())
{
j = 0;
++(*this);
}
}
MyIterator(const MyIterator& rhs) = default;
MyIterator& operator = (const MyIterator& rhs) = default;
MyIterator& operator ++() {
if (++j >= (*container)[i].size()) {
do {++i;} while (i < (*container).size() && (*container)[i].empty());
j = 0;
}
return *this;
}
MyIterator operator ++(int) { auto it = *this; ++(*this); return it; }
MyIterator& operator --() {
if (j-- == 0) {
do { --i; } while (i != 0 && (*container)[i].empty());
j = (*container)[i].size();
}
return *this;
}
MyIterator operator --(int) { auto it = *this; --(*this); return it; }
double operator *() const { return (*container)[i][j]; }
bool operator == (const MyIterator& rhs) const {
return container == rhs.container && i == rhs.i && j == rhs.j;
}
bool operator != (const MyIterator& rhs) const { return !(*this == rhs); }
private:
const std::vector<std::vector<double>>* container;
std::size_t i;
std::size_t j;
};
And usage may be
// Helper functions for begin/end
MyIterator MyIteratorBegin(const std::vector<std::vector<double>>& container)
{
return MyIterator(container, 0, 0);
}
MyIterator MyIteratorEnd(const std::vector<std::vector<double>>& container)
{
return MyIterator(container, container.size(), 0);
}
int main() {
std::vector<std::vector<double>> values = {{5,0,8}, {}, {3,1,9}};
auto b = MyIteratorBegin(values);
auto e = MyIteratorEnd(values);
auto p = std::minmax_element(b, e);
if (p.first != e) {
std::cout << "min is " << *p.first << " and max is " << *p.second << std::endl;
}
}
Live example
Using the accumulate function you could write:
#include <iostream>
#include <numeric>
#include <vector>
int main()
{
std::vector<std::vector<double>> m{ {5, 0, 8}, {3, 1, 9} };
double x = std::accumulate(m.begin(), m.end(), m[0][0],
[](double max, const std::vector<double> &v)
{
return std::max(max,
*std::max_element(v.begin(),
v.end()));
});
std::cout << x << '\n';
return 0;
}
but I'd prefer the good, old for-loop.
The example can be extended to find both the min and max values:
std::accumulate(m.begin(), m.end(),
std::make_pair(m[0][0], m[0][0]),
[](std::pair<double, double> minmax, const std::vector<double> &v)
{
auto tmp(std::minmax_element(v.begin(), v.end()));
return std::make_pair(
std::min(minmax.first, *tmp.first),
std::max(minmax.second, *tmp.second));
});
(in real code you have to handle the empty-vector case)
Unfortunately a vector of vector isn't stored contiguously in memory, so you haven't a single block containing all the values (this is one of the reasons why a vector of vector isn't a good model for a matrix).
You can take advantage of a vector of vector if it contains a lot of elements.
Since each sub-vector is autonomous, you could use std::async to fill asynchronously a vector of futures containing the max value of each sub-vector.
The simplest method would be to first have a function to determine the max/min elements of one vector, say a function called:
double getMaxInVector(const vector<double>& someVec){}
Passing by reference (for reading purposes only) in this case will be a lot more time and space efficient (you don't want your function copying an entire vector). Thus in your function to determine max/min element of a vector of vectors, you would have a nested loop, such as:
for(size_t x= 0; x < some_values.size(); x++){
for(size_t y = 0; y < x.size(); y++){
// y represents the vectors inside the vector of course
// current max/min = getMax(y)
// update max/min after inner loop finishes and x increments
// by comparing it with previous max/min
The problem with the above solution is its inefficiency. From my knowledge, this algorithm will generally run on O(n^2log(n)) efficiency, which is quite unimpressive. But of course, it is still a solution. Although there might be standard algorithms that can find the max/min of a vector for you, it's always more accomplishing to write your own, and using the given will usually do nothing in terms of improving efficiency because the algorithm will generally be the same (for small functions that determine max/min). In fact, theoretically, standard functions would run marginally slower since those functions are templates which have to determine the type it is dealing with at run-time.
Lets say we have a vector named some_values, as shown below
7 4 2 0
4 8 10 8
3 6 7 6
3 9 19* 14
define a one-dimensional vector as shown below
vector<int> oneDimVector;
for(int i = 0; i < 4; i++){
for(int j = 0; j < 4; j++){
oneDimVector.push_back(some_values[i][j]);
}
}
Then find out a maximum/minimum element in that one-dimensional vector as shown below
vector<int>::iterator maxElement = max_element(oneDimVector.begin(),oneDimVector.end());
vector<int>::iterator minElement = min_element(oneDimVector.begin(),oneDimVector.end());
Now you get the max/min elements as below
cout << "Max element is " << *maxElement << endl;
cout << "Min element is " << *minElement << endl;
vector<vector<int>> vv = { vector<int>{10,12,43,58}, vector<int>{10,14,23,18}, vector<int>{28,47,12,90} };
vector<vector<int>> vv1 = { vector<int>{22,24,43,58}, vector<int>{56,17,23,18}, vector<int>{11,12,12,90} };
int matrix1_elem_sum=0;
int matrix2_elem_sum = 0;
for (size_t i = 0; i < vv.size(); i++)
{
matrix1_elem_sum += std::accumulate(vv[i].begin(), vv[i].end(), 0);
matrix2_elem_sum += std::accumulate(vv1[i].begin(), vv1[i].end(), 0);
}
cout << matrix1_elem_sum <<endl;
cout << matrix2_elem_sum << endl;
int summ = matrix1_elem_sum + matrix2_elem_sum;
cout << summ << endl;
or optimazed variant:
vector<vector<int>> vv = { vector<int>{10,12,43,58}, vector<int>{10,14,23,18}, vector<int>{28,47,12,90} };
vector<vector<int>> vv1 = { vector<int>{22,24,43,58}, vector<int>{56,17,23,18}, vector<int>{11,12,12,90} };
int summ=0;
int matrix2_elem_sum = 0;
for (size_t i = 0; i < vv.size(); i++)
{
summ += std::accumulate(vv[i].begin(), vv[i].end(), 0)+ std::accumulate(vv1[i].begin(), vv1[i].end(), 0);
}
cout << summ << endl;
}

Cartesian product from a vector in C++11? [duplicate]

I've a vector of vectors say vector<vector<int> > items of different sizes like as follows
1,2,3
4,5
6,7,8
I want to create combinations in terms of Cartesian product of these vectors like
1,4,6
1,4,7
1,4,8
and so on till
3,5,8
How can I do that ? I've looked up several links and I've also listed them at the end of this post but I'm not able to interpret that as I'm not that familiar with the language. Could some body help me with this.
#include <iostream>
#include <iomanip>
#include <vector>
using namespace std;
int main()
{
vector<vector<int> > items;
int k = 0;
for ( int i = 0; i < 5; i++ ) {
items.push_back ( vector<int>() );
for ( int j = 0; j < 5; j++ )
items[i].push_back ( k++ );
}
cartesian ( items ); // I want some function here to do this.
}
This program has equal length vectors and I put this so that it will be easier to understand my data structure. It will be very helpful even if somebody uses others answers from other links and integrate with this to get the result. Thank you very much
Couple of links I looked at
one
Two
Program from : program
First, I'll show you a recursive version.
// Cartesion product of vector of vectors
#include <vector>
#include <iostream>
#include <iterator>
// Types to hold vector-of-ints (Vi) and vector-of-vector-of-ints (Vvi)
typedef std::vector<int> Vi;
typedef std::vector<Vi> Vvi;
// Just for the sample -- populate the intput data set
Vvi build_input() {
Vvi vvi;
for(int i = 0; i < 3; i++) {
Vi vi;
for(int j = 0; j < 3; j++) {
vi.push_back(i*10+j);
}
vvi.push_back(vi);
}
return vvi;
}
// just for the sample -- print the data sets
std::ostream&
operator<<(std::ostream& os, const Vi& vi)
{
os << "(";
std::copy(vi.begin(), vi.end(), std::ostream_iterator<int>(os, ", "));
os << ")";
return os;
}
std::ostream&
operator<<(std::ostream& os, const Vvi& vvi)
{
os << "(\n";
for(Vvi::const_iterator it = vvi.begin();
it != vvi.end();
it++) {
os << " " << *it << "\n";
}
os << ")";
return os;
}
// recursive algorithm to to produce cart. prod.
// At any given moment, "me" points to some Vi in the middle of the
// input data set.
// for int i in *me:
// add i to current result
// recurse on next "me"
//
void cart_product(
Vvi& rvvi, // final result
Vi& rvi, // current result
Vvi::const_iterator me, // current input
Vvi::const_iterator end) // final input
{
if(me == end) {
// terminal condition of the recursion. We no longer have
// any input vectors to manipulate. Add the current result (rvi)
// to the total set of results (rvvvi).
rvvi.push_back(rvi);
return;
}
// need an easy name for my vector-of-ints
const Vi& mevi = *me;
for(Vi::const_iterator it = mevi.begin();
it != mevi.end();
it++) {
// final rvi will look like "a, b, c, ME, d, e, f"
// At the moment, rvi already has "a, b, c"
rvi.push_back(*it); // add ME
cart_product(rvvi, rvi, me+1, end); add "d, e, f"
rvi.pop_back(); // clean ME off for next round
}
}
// sample only, to drive the cart_product routine.
int main() {
Vvi input(build_input());
std::cout << input << "\n";
Vvi output;
Vi outputTemp;
cart_product(output, outputTemp, input.begin(), input.end());
std::cout << output << "\n";
}
Now, I'll show you the recursive iterative version that I shamelessly stole from #John :
The rest of the program is pretty much the same, only showing the cart_product function.
// Seems like you'd want a vector of iterators
// which iterate over your individual vector<int>s.
struct Digits {
Vi::const_iterator begin;
Vi::const_iterator end;
Vi::const_iterator me;
};
typedef std::vector<Digits> Vd;
void cart_product(
Vvi& out, // final result
Vvi& in) // final result
{
Vd vd;
// Start all of the iterators at the beginning.
for(Vvi::const_iterator it = in.begin();
it != in.end();
++it) {
Digits d = {(*it).begin(), (*it).end(), (*it).begin()};
vd.push_back(d);
}
while(1) {
// Construct your first product vector by pulling
// out the element of each vector via the iterator.
Vi result;
for(Vd::const_iterator it = vd.begin();
it != vd.end();
it++) {
result.push_back(*(it->me));
}
out.push_back(result);
// Increment the rightmost one, and repeat.
// When you reach the end, reset that one to the beginning and
// increment the next-to-last one. You can get the "next-to-last"
// iterator by pulling it out of the neighboring element in your
// vector of iterators.
for(Vd::iterator it = vd.begin(); ; ) {
// okay, I started at the left instead. sue me
++(it->me);
if(it->me == it->end) {
if(it+1 == vd.end()) {
// I'm the last digit, and I'm about to roll
return;
} else {
// cascade
it->me = it->begin;
++it;
}
} else {
// normal
break;
}
}
}
}
Here is a solution in C++11.
The indexing of the variable-sized arrays can be done eloquently with modular arithmetic.
The total number of lines in the output is the product of the sizes of the input vectors. That is:
N = v[0].size() * v[1].size() * v[2].size()
Therefore the main loop has n as the iteration variable, from 0 to N-1. In principle, each value of n encodes enough information to extract each of the indices of v for that iteration. This is done in a subloop using repeated modular arithmetic:
#include <cstdlib>
#include <iostream>
#include <numeric>
#include <vector>
using namespace std;
void cartesian( vector<vector<int> >& v ) {
auto product = []( long long a, vector<int>& b ) { return a*b.size(); };
const long long N = accumulate( v.begin(), v.end(), 1LL, product );
vector<int> u(v.size());
for( long long n=0 ; n<N ; ++n ) {
lldiv_t q { n, 0 };
for( long long i=v.size()-1 ; 0<=i ; --i ) {
q = div( q.quot, v[i].size() );
u[i] = v[i][q.rem];
}
// Do what you want here with u.
for( int x : u ) cout << x << ' ';
cout << '\n';
}
}
int main() {
vector<vector<int> > v { { 1, 2, 3 },
{ 4, 5 },
{ 6, 7, 8 } };
cartesian(v);
return 0;
}
Output:
1 4 6
1 4 7
1 4 8
...
3 5 8
Shorter code:
vector<vector<int>> cart_product (const vector<vector<int>>& v) {
vector<vector<int>> s = {{}};
for (const auto& u : v) {
vector<vector<int>> r;
for (const auto& x : s) {
for (const auto y : u) {
r.push_back(x);
r.back().push_back(y);
}
}
s = move(r);
}
return s;
}
Seems like you'd want a vector of iterators which iterate over your individual vector<int>s.
Start all of the iterators at the beginning. Construct your first product vector by pulling out the element of each vector via the iterator.
Increment the rightmost one, and repeat.
When you reach the end, reset that one to the beginning and increment the next-to-last one. You can get the "next-to-last" iterator by pulling it out of the neighboring element in your vector of iterators.
Continue cycling through until both the last and next-to-last iterators are at the end. Then, reset them both, increment the third-from-last iterator. In general, this can be cascaded.
It's like an odometer, but with each different digit being in a different base.
Here's my solution. Also iterative, but a little shorter than the above...
void xp(const vector<vector<int>*>& vecs, vector<vector<int>*> *result) {
vector<vector<int>*>* rslts;
for (int ii = 0; ii < vecs.size(); ++ii) {
const vector<int>& vec = *vecs[ii];
if (ii == 0) {
// vecs=[[1,2],...] ==> rslts=[[1],[2]]
rslts = new vector<vector<int>*>;
for (int jj = 0; jj < vec.size(); ++jj) {
vector<int>* v = new vector<int>;
v->push_back(vec[jj]);
rslts->push_back(v);
}
} else {
// vecs=[[1,2],[3,4],...] ==> rslts=[[1,3],[1,4],[2,3],[2,4]]
vector<vector<int>*>* tmp = new vector<vector<int>*>;
for (int jj = 0; jj < vec.size(); ++jj) { // vec[jj]=3 (first iter jj=0)
for (vector<vector<int>*>::const_iterator it = rslts->begin();
it != rslts->end(); ++it) {
vector<int>* v = new vector<int>(**it); // v=[1]
v->push_back(vec[jj]); // v=[1,3]
tmp->push_back(v); // tmp=[[1,3]]
}
}
for (int kk = 0; kk < rslts->size(); ++kk) {
delete (*rslts)[kk];
}
delete rslts;
rslts = tmp;
}
}
result->insert(result->end(), rslts->begin(), rslts->end());
delete rslts;
}
I derived it with some pain from a haskell version I wrote:
xp :: [[a]] -> [[a]]
xp [] = []
xp [l] = map (:[]) l
xp (h:t) = foldr (\x acc -> foldr (\l acc -> (x:l):acc) acc (xp t)) [] h
Since I needed the same functionality, I implemented an iterator which computes the Cartesian product on the fly, as needed, and iterates over it.
It can be used as follows.
#include <forward_list>
#include <iostream>
#include <vector>
#include "cartesian.hpp"
int main()
{
// Works with a vector of vectors
std::vector<std::vector<int>> test{{1,2,3}, {4,5,6}, {8,9}};
CartesianProduct<decltype(test)> cp(test);
for(auto const& val: cp) {
std::cout << val.at(0) << ", " << val.at(1) << ", " << val.at(2) << "\n";
}
// Also works with something much less, like a forward_list of forward_lists
std::forward_list<std::forward_list<std::string>> foo{{"boo", "far", "zab"}, {"zoo", "moo"}, {"yohoo", "bohoo", "whoot", "noo"}};
CartesianProduct<decltype(foo)> bar(foo);
for(auto const& val: bar) {
std::cout << val.at(0) << ", " << val.at(1) << ", " << val.at(2) << "\n";
}
}
The file cartesian.hpp looks like this.
#include <cassert>
#include <limits>
#include <stdexcept>
#include <vector>
#include <boost/iterator/iterator_facade.hpp>
//! Class iterating over the Cartesian product of a forward iterable container of forward iterable containers
template<typename T>
class CartesianProductIterator : public boost::iterator_facade<CartesianProductIterator<T>, std::vector<typename T::value_type::value_type> const, boost::forward_traversal_tag>
{
public:
//! Delete default constructor
CartesianProductIterator() = delete;
//! Constructor setting the underlying iterator and position
/*!
* \param[in] structure The underlying structure
* \param[in] pos The position the iterator should be initialized to. std::numeric_limits<std::size_t>::max()stands for the end, the position after the last element.
*/
explicit CartesianProductIterator(T const& structure, std::size_t pos);
private:
//! Give types more descriptive names
// \{
typedef T OuterContainer;
typedef typename T::value_type Container;
typedef typename T::value_type::value_type Content;
// \}
//! Grant access to boost::iterator_facade
friend class boost::iterator_core_access;
//! Increment iterator
void increment();
//! Check for equality
bool equal(CartesianProductIterator<T> const& other) const;
//! Dereference iterator
std::vector<Content> const& dereference() const;
//! The part we are iterating over
OuterContainer const& structure_;
//! The position in the Cartesian product
/*!
* For each element of structure_, give the position in it.
* The empty vector represents the end position.
* Note that this vector has a size equal to structure->size(), or is empty.
*/
std::vector<typename Container::const_iterator> position_;
//! The position just indexed by an integer
std::size_t absolutePosition_ = 0;
//! The begin iterators, saved for convenience and performance
std::vector<typename Container::const_iterator> cbegins_;
//! The end iterators, saved for convenience and performance
std::vector<typename Container::const_iterator> cends_;
//! Used for returning references
/*!
* We initialize with one empty element, so that we only need to add more elements in increment().
*/
mutable std::vector<std::vector<Content>> result_{std::vector<Content>()};
//! The size of the instance of OuterContainer
std::size_t size_ = 0;
};
template<typename T>
CartesianProductIterator<T>::CartesianProductIterator(OuterContainer const& structure, std::size_t pos) : structure_(structure)
{
for(auto & entry: structure_) {
cbegins_.push_back(entry.cbegin());
cends_.push_back(entry.cend());
++size_;
}
if(pos == std::numeric_limits<std::size_t>::max() || size_ == 0) {
absolutePosition_ = std::numeric_limits<std::size_t>::max();
return;
}
// Initialize with all cbegin() position
position_.reserve(size_);
for(std::size_t i = 0; i != size_; ++i) {
position_.push_back(cbegins_[i]);
if(cbegins_[i] == cends_[i]) {
// Empty member, so Cartesian product is empty
absolutePosition_ = std::numeric_limits<std::size_t>::max();
return;
}
}
// Increment to wanted position
for(std::size_t i = 0; i < pos; ++i) {
increment();
}
}
template<typename T>
void CartesianProductIterator<T>::increment()
{
if(absolutePosition_ == std::numeric_limits<std::size_t>::max()) {
return;
}
std::size_t pos = size_ - 1;
// Descend as far as necessary
while(++(position_[pos]) == cends_[pos] && pos != 0) {
--pos;
}
if(position_[pos] == cends_[pos]) {
assert(pos == 0);
absolutePosition_ = std::numeric_limits<std::size_t>::max();
return;
}
// Set all to begin behind pos
for(++pos; pos != size_; ++pos) {
position_[pos] = cbegins_[pos];
}
++absolutePosition_;
result_.emplace_back();
}
template<typename T>
std::vector<typename T::value_type::value_type> const& CartesianProductIterator<T>::dereference() const
{
if(absolutePosition_ == std::numeric_limits<std::size_t>::max()) {
throw new std::out_of_range("Out of bound dereference in CartesianProductIterator\n");
}
auto & result = result_[absolutePosition_];
if(result.empty()) {
result.reserve(size_);
for(auto & iterator: position_) {
result.push_back(*iterator);
}
}
return result;
}
template<typename T>
bool CartesianProductIterator<T>::equal(CartesianProductIterator<T> const& other) const
{
return absolutePosition_ == other.absolutePosition_ && structure_ == other.structure_;
}
//! Class that turns a forward iterable container of forward iterable containers into a forward iterable container which iterates over the Cartesian product of the forward iterable containers
template<typename T>
class CartesianProduct
{
public:
//! Constructor from type T
explicit CartesianProduct(T const& t) : t_(t) {}
//! Iterator to beginning of Cartesian product
CartesianProductIterator<T> begin() const { return CartesianProductIterator<T>(t_, 0); }
//! Iterator behind the last element of the Cartesian product
CartesianProductIterator<T> end() const { return CartesianProductIterator<T>(t_, std::numeric_limits<std::size_t>::max()); }
private:
T const& t_;
};
If someone has comments how to make it faster or better, I'd highly appreciate them.
I was just forced to implement this for a project I was working on and I came up with the code below. It can be stuck in a header and it's use is very simple but it returns all of the combinations you can get from a vector of vectors. The array that it returns only holds integers. This was a conscious decision because I just wanted the indices. In this way, I could index into each of the vector's vector and then perform the calculations I/anyone would need... best to avoid letting CartesianProduct hold "stuff" itself, it is a mathematical concept based around counting not a data structure. I'm fairly new to c++ but this was tested in a decryption algorithm pretty thoroughly. There is some light recursion but overall this is a simple implementation of a simple counting concept.
// Use of the CartesianProduct class is as follows. Give it the number
// of rows and the sizes of each of the rows. It will output all of the
// permutations of these numbers in their respective rows.
// 1. call cp.permutation() // need to check all 0s.
// 2. while cp.HasNext() // it knows the exit condition form its inputs.
// 3. cp.Increment() // Make the next permutation
// 4. cp.permutation() // get the next permutation
class CartesianProduct{
public:
CartesianProduct(int num_rows, vector<int> sizes_of_rows){
permutation_ = new int[num_rows];
num_rows_ = num_rows;
ZeroOutPermutation();
sizes_of_rows_ = sizes_of_rows;
num_max_permutations_ = 1;
for (int i = 0; i < num_rows; ++i){
num_max_permutations_ *= sizes_of_rows_[i];
}
}
~CartesianProduct(){
delete permutation_;
}
bool HasNext(){
if(num_permutations_processed_ != num_max_permutations_) {
return true;
} else {
return false;
}
}
void Increment(){
int row_to_increment = 0;
++num_permutations_processed_;
IncrementAndTest(row_to_increment);
}
int* permutation(){
return permutation_;
}
int num_permutations_processed(){
return num_permutations_processed_;
}
void PrintPermutation(){
cout << "( ";
for (int i = 0; i < num_rows_; ++i){
cout << permutation_[i] << ", ";
}
cout << " )" << endl;
}
private:
int num_permutations_processed_;
int *permutation_;
int num_rows_;
int num_max_permutations_;
vector<int> sizes_of_rows_;
// Because CartesianProduct is called first initially with it's values
// of 0 and because those values are valid and important output
// of the CartesianProduct we increment the number of permutations
// processed here when we populate the permutation_ array with 0s.
void ZeroOutPermutation(){
for (int i = 0; i < num_rows_; ++i){
permutation_[i] = 0;
}
num_permutations_processed_ = 1;
}
void IncrementAndTest(int row_to_increment){
permutation_[row_to_increment] += 1;
int max_index_of_row = sizes_of_rows_[row_to_increment] - 1;
if (permutation_[row_to_increment] > max_index_of_row){
permutation_[row_to_increment] = 0;
IncrementAndTest(row_to_increment + 1);
}
}
};
#include <iostream>
#include <vector>
void cartesian (std::vector<std::vector<int>> const& items) {
auto n = items.size();
auto next = [&](std::vector<int> & x) {
for ( int i = 0; i < n; ++ i )
if ( ++x[i] == items[i].size() ) x[i] = 0;
else return true;
return false;
};
auto print = [&](std::vector<int> const& x) {
for ( int i = 0; i < n; ++ i )
std::cout << items[i][x[i]] << ",";
std::cout << "\b \n";
};
std::vector<int> x(n);
do print(x); while (next(x)); // Shazam!
}
int main () {
std::vector<std::vector<int>>
items { { 1, 2, 3 }, { 4, 5 }, { 6, 7, 8 } };
cartesian(items);
return 0;
}
The idea behind this is as follows.
Let n := items.size().
Let m_i := items[i].size(), for all i in {0,1,...,n-1}.
Let M := {0,1,...,m_0-1} x {0,1,...,m_1-1} x ... x {0,1,...,m_{n-1}-1}.
We first solve the simpler problem of iterating through M. This is accomplished by the next lambda. The algorithm is simply the "carrying" routine grade schoolers use to add 1, albeit with a mixed radix number system.
We use this to solve the more general problem by transforming a tuple x in M to one of the desired tuples via the formula items[i][x[i]] for all i in {0,1,...,n-1}. We perform this transformation in the print lambda.
We then perform the iteration with do print(x); while (next(x));.
Now some comments on complexity, under the assumption that m_i > 1 for all i:
This algorithm requires O(n) space. Note that explicit construction of the Cartesian product takes O(m_0 m_1 m_2 ... m_{n-1}) >= O(2^n) space. So this is exponentially better on space than any algorithm which requires all tuples to be stored simultaneously in memory.
The next function takes amortized O(1) time (by a geometric series argument).
The print function takes O(n) time.
Hence, altogether, the algorithm has time complexity O(n|M|) and space complexity O(n) (not counting the cost of storing items).
An interesting thing to note is that if print is replaced with a function which inspects on average only O(1) coordinates per tuple rather than all of them, then time complexity falls to O(|M|), that is, it becomes linear time with respect to the size of the Cartesian product. In other words, avoiding the copy of the tuple each iterate can be meaningful in some situations.
This version supports no iterators or ranges, but it is a simple direct implementation that uses the multiplication operator to represent the Cartesian product, and a lambda to perform the action.
The interface is designed with the particular functionality I needed. I needed the flexibility to choose vectors over which to apply the Cartesian product in a way that did not obscure the code.
int main()
{
vector< vector<long> > v{ { 1, 2, 3 }, { 4, 5 }, { 6, 7, 8 } };
(Cartesian<long>(v[0]) * v[1] * v[2]).ForEach(
[](long p_Depth, long *p_LongList)
{
std::cout << p_LongList[0] << " " << p_LongList[1] << " " << p_LongList[2] << std::endl;
}
);
}
The implementation uses recursion up the class structure to implement the embedded for loops over each vector. The algorithm works directly on the input vectors, requiring no large temporary arrays. It is simple to understand and debug.
The use of std::function p_Action instead of void p_Action(long p_Depth, T *p_ParamList) for the lambda parameter would allow me to capture local variables, if I wanted to. In the above call, I don't.
But you knew that, didn't you. "function" is a template class which takes the type parameter of a function and makes it callable.
#include <vector>
#include <iostream>
#include <functional>
#include <string>
using namespace std;
template <class T>
class Cartesian
{
private:
vector<T> &m_Vector;
Cartesian<T> *m_Cartesian;
public:
Cartesian(vector<T> &p_Vector, Cartesian<T> *p_Cartesian=NULL)
: m_Vector(p_Vector), m_Cartesian(p_Cartesian)
{};
virtual ~Cartesian() {};
Cartesian<T> *Clone()
{
return new Cartesian<T>(m_Vector, m_Cartesian ? m_Cartesian->Clone() : NULL);
};
Cartesian<T> &operator *=(vector<T> &p_Vector)
{
if (m_Cartesian)
(*m_Cartesian) *= p_Vector;
else
m_Cartesian = new Cartesian(p_Vector);
return *this;
};
Cartesian<T> operator *(vector<T> &p_Vector)
{
return (*Clone()) *= p_Vector;
};
long Depth()
{
return m_Cartesian ? 1 + m_Cartesian->Depth() : 1;
};
void ForEach(function<void (long p_Depth, T *p_ParamList)> p_Action)
{
Loop(0, new T[Depth()], p_Action);
};
private:
void Loop(long p_Depth, T *p_ParamList, function<void (long p_Depth, T *p_ParamList)> p_Action)
{
for (T &element : m_Vector)
{
p_ParamList[p_Depth] = element;
if (m_Cartesian)
m_Cartesian->Loop(p_Depth + 1, p_ParamList, p_Action);
else
p_Action(Depth(), p_ParamList);
}
};
};

How can I create cartesian product of vector of vectors?

I've a vector of vectors say vector<vector<int> > items of different sizes like as follows
1,2,3
4,5
6,7,8
I want to create combinations in terms of Cartesian product of these vectors like
1,4,6
1,4,7
1,4,8
and so on till
3,5,8
How can I do that ? I've looked up several links and I've also listed them at the end of this post but I'm not able to interpret that as I'm not that familiar with the language. Could some body help me with this.
#include <iostream>
#include <iomanip>
#include <vector>
using namespace std;
int main()
{
vector<vector<int> > items;
int k = 0;
for ( int i = 0; i < 5; i++ ) {
items.push_back ( vector<int>() );
for ( int j = 0; j < 5; j++ )
items[i].push_back ( k++ );
}
cartesian ( items ); // I want some function here to do this.
}
This program has equal length vectors and I put this so that it will be easier to understand my data structure. It will be very helpful even if somebody uses others answers from other links and integrate with this to get the result. Thank you very much
Couple of links I looked at
one
Two
Program from : program
First, I'll show you a recursive version.
// Cartesion product of vector of vectors
#include <vector>
#include <iostream>
#include <iterator>
// Types to hold vector-of-ints (Vi) and vector-of-vector-of-ints (Vvi)
typedef std::vector<int> Vi;
typedef std::vector<Vi> Vvi;
// Just for the sample -- populate the intput data set
Vvi build_input() {
Vvi vvi;
for(int i = 0; i < 3; i++) {
Vi vi;
for(int j = 0; j < 3; j++) {
vi.push_back(i*10+j);
}
vvi.push_back(vi);
}
return vvi;
}
// just for the sample -- print the data sets
std::ostream&
operator<<(std::ostream& os, const Vi& vi)
{
os << "(";
std::copy(vi.begin(), vi.end(), std::ostream_iterator<int>(os, ", "));
os << ")";
return os;
}
std::ostream&
operator<<(std::ostream& os, const Vvi& vvi)
{
os << "(\n";
for(Vvi::const_iterator it = vvi.begin();
it != vvi.end();
it++) {
os << " " << *it << "\n";
}
os << ")";
return os;
}
// recursive algorithm to to produce cart. prod.
// At any given moment, "me" points to some Vi in the middle of the
// input data set.
// for int i in *me:
// add i to current result
// recurse on next "me"
//
void cart_product(
Vvi& rvvi, // final result
Vi& rvi, // current result
Vvi::const_iterator me, // current input
Vvi::const_iterator end) // final input
{
if(me == end) {
// terminal condition of the recursion. We no longer have
// any input vectors to manipulate. Add the current result (rvi)
// to the total set of results (rvvvi).
rvvi.push_back(rvi);
return;
}
// need an easy name for my vector-of-ints
const Vi& mevi = *me;
for(Vi::const_iterator it = mevi.begin();
it != mevi.end();
it++) {
// final rvi will look like "a, b, c, ME, d, e, f"
// At the moment, rvi already has "a, b, c"
rvi.push_back(*it); // add ME
cart_product(rvvi, rvi, me+1, end); add "d, e, f"
rvi.pop_back(); // clean ME off for next round
}
}
// sample only, to drive the cart_product routine.
int main() {
Vvi input(build_input());
std::cout << input << "\n";
Vvi output;
Vi outputTemp;
cart_product(output, outputTemp, input.begin(), input.end());
std::cout << output << "\n";
}
Now, I'll show you the recursive iterative version that I shamelessly stole from #John :
The rest of the program is pretty much the same, only showing the cart_product function.
// Seems like you'd want a vector of iterators
// which iterate over your individual vector<int>s.
struct Digits {
Vi::const_iterator begin;
Vi::const_iterator end;
Vi::const_iterator me;
};
typedef std::vector<Digits> Vd;
void cart_product(
Vvi& out, // final result
Vvi& in) // final result
{
Vd vd;
// Start all of the iterators at the beginning.
for(Vvi::const_iterator it = in.begin();
it != in.end();
++it) {
Digits d = {(*it).begin(), (*it).end(), (*it).begin()};
vd.push_back(d);
}
while(1) {
// Construct your first product vector by pulling
// out the element of each vector via the iterator.
Vi result;
for(Vd::const_iterator it = vd.begin();
it != vd.end();
it++) {
result.push_back(*(it->me));
}
out.push_back(result);
// Increment the rightmost one, and repeat.
// When you reach the end, reset that one to the beginning and
// increment the next-to-last one. You can get the "next-to-last"
// iterator by pulling it out of the neighboring element in your
// vector of iterators.
for(Vd::iterator it = vd.begin(); ; ) {
// okay, I started at the left instead. sue me
++(it->me);
if(it->me == it->end) {
if(it+1 == vd.end()) {
// I'm the last digit, and I'm about to roll
return;
} else {
// cascade
it->me = it->begin;
++it;
}
} else {
// normal
break;
}
}
}
}
Here is a solution in C++11.
The indexing of the variable-sized arrays can be done eloquently with modular arithmetic.
The total number of lines in the output is the product of the sizes of the input vectors. That is:
N = v[0].size() * v[1].size() * v[2].size()
Therefore the main loop has n as the iteration variable, from 0 to N-1. In principle, each value of n encodes enough information to extract each of the indices of v for that iteration. This is done in a subloop using repeated modular arithmetic:
#include <cstdlib>
#include <iostream>
#include <numeric>
#include <vector>
using namespace std;
void cartesian( vector<vector<int> >& v ) {
auto product = []( long long a, vector<int>& b ) { return a*b.size(); };
const long long N = accumulate( v.begin(), v.end(), 1LL, product );
vector<int> u(v.size());
for( long long n=0 ; n<N ; ++n ) {
lldiv_t q { n, 0 };
for( long long i=v.size()-1 ; 0<=i ; --i ) {
q = div( q.quot, v[i].size() );
u[i] = v[i][q.rem];
}
// Do what you want here with u.
for( int x : u ) cout << x << ' ';
cout << '\n';
}
}
int main() {
vector<vector<int> > v { { 1, 2, 3 },
{ 4, 5 },
{ 6, 7, 8 } };
cartesian(v);
return 0;
}
Output:
1 4 6
1 4 7
1 4 8
...
3 5 8
Shorter code:
vector<vector<int>> cart_product (const vector<vector<int>>& v) {
vector<vector<int>> s = {{}};
for (const auto& u : v) {
vector<vector<int>> r;
for (const auto& x : s) {
for (const auto y : u) {
r.push_back(x);
r.back().push_back(y);
}
}
s = move(r);
}
return s;
}
Seems like you'd want a vector of iterators which iterate over your individual vector<int>s.
Start all of the iterators at the beginning. Construct your first product vector by pulling out the element of each vector via the iterator.
Increment the rightmost one, and repeat.
When you reach the end, reset that one to the beginning and increment the next-to-last one. You can get the "next-to-last" iterator by pulling it out of the neighboring element in your vector of iterators.
Continue cycling through until both the last and next-to-last iterators are at the end. Then, reset them both, increment the third-from-last iterator. In general, this can be cascaded.
It's like an odometer, but with each different digit being in a different base.
Here's my solution. Also iterative, but a little shorter than the above...
void xp(const vector<vector<int>*>& vecs, vector<vector<int>*> *result) {
vector<vector<int>*>* rslts;
for (int ii = 0; ii < vecs.size(); ++ii) {
const vector<int>& vec = *vecs[ii];
if (ii == 0) {
// vecs=[[1,2],...] ==> rslts=[[1],[2]]
rslts = new vector<vector<int>*>;
for (int jj = 0; jj < vec.size(); ++jj) {
vector<int>* v = new vector<int>;
v->push_back(vec[jj]);
rslts->push_back(v);
}
} else {
// vecs=[[1,2],[3,4],...] ==> rslts=[[1,3],[1,4],[2,3],[2,4]]
vector<vector<int>*>* tmp = new vector<vector<int>*>;
for (int jj = 0; jj < vec.size(); ++jj) { // vec[jj]=3 (first iter jj=0)
for (vector<vector<int>*>::const_iterator it = rslts->begin();
it != rslts->end(); ++it) {
vector<int>* v = new vector<int>(**it); // v=[1]
v->push_back(vec[jj]); // v=[1,3]
tmp->push_back(v); // tmp=[[1,3]]
}
}
for (int kk = 0; kk < rslts->size(); ++kk) {
delete (*rslts)[kk];
}
delete rslts;
rslts = tmp;
}
}
result->insert(result->end(), rslts->begin(), rslts->end());
delete rslts;
}
I derived it with some pain from a haskell version I wrote:
xp :: [[a]] -> [[a]]
xp [] = []
xp [l] = map (:[]) l
xp (h:t) = foldr (\x acc -> foldr (\l acc -> (x:l):acc) acc (xp t)) [] h
Since I needed the same functionality, I implemented an iterator which computes the Cartesian product on the fly, as needed, and iterates over it.
It can be used as follows.
#include <forward_list>
#include <iostream>
#include <vector>
#include "cartesian.hpp"
int main()
{
// Works with a vector of vectors
std::vector<std::vector<int>> test{{1,2,3}, {4,5,6}, {8,9}};
CartesianProduct<decltype(test)> cp(test);
for(auto const& val: cp) {
std::cout << val.at(0) << ", " << val.at(1) << ", " << val.at(2) << "\n";
}
// Also works with something much less, like a forward_list of forward_lists
std::forward_list<std::forward_list<std::string>> foo{{"boo", "far", "zab"}, {"zoo", "moo"}, {"yohoo", "bohoo", "whoot", "noo"}};
CartesianProduct<decltype(foo)> bar(foo);
for(auto const& val: bar) {
std::cout << val.at(0) << ", " << val.at(1) << ", " << val.at(2) << "\n";
}
}
The file cartesian.hpp looks like this.
#include <cassert>
#include <limits>
#include <stdexcept>
#include <vector>
#include <boost/iterator/iterator_facade.hpp>
//! Class iterating over the Cartesian product of a forward iterable container of forward iterable containers
template<typename T>
class CartesianProductIterator : public boost::iterator_facade<CartesianProductIterator<T>, std::vector<typename T::value_type::value_type> const, boost::forward_traversal_tag>
{
public:
//! Delete default constructor
CartesianProductIterator() = delete;
//! Constructor setting the underlying iterator and position
/*!
* \param[in] structure The underlying structure
* \param[in] pos The position the iterator should be initialized to. std::numeric_limits<std::size_t>::max()stands for the end, the position after the last element.
*/
explicit CartesianProductIterator(T const& structure, std::size_t pos);
private:
//! Give types more descriptive names
// \{
typedef T OuterContainer;
typedef typename T::value_type Container;
typedef typename T::value_type::value_type Content;
// \}
//! Grant access to boost::iterator_facade
friend class boost::iterator_core_access;
//! Increment iterator
void increment();
//! Check for equality
bool equal(CartesianProductIterator<T> const& other) const;
//! Dereference iterator
std::vector<Content> const& dereference() const;
//! The part we are iterating over
OuterContainer const& structure_;
//! The position in the Cartesian product
/*!
* For each element of structure_, give the position in it.
* The empty vector represents the end position.
* Note that this vector has a size equal to structure->size(), or is empty.
*/
std::vector<typename Container::const_iterator> position_;
//! The position just indexed by an integer
std::size_t absolutePosition_ = 0;
//! The begin iterators, saved for convenience and performance
std::vector<typename Container::const_iterator> cbegins_;
//! The end iterators, saved for convenience and performance
std::vector<typename Container::const_iterator> cends_;
//! Used for returning references
/*!
* We initialize with one empty element, so that we only need to add more elements in increment().
*/
mutable std::vector<std::vector<Content>> result_{std::vector<Content>()};
//! The size of the instance of OuterContainer
std::size_t size_ = 0;
};
template<typename T>
CartesianProductIterator<T>::CartesianProductIterator(OuterContainer const& structure, std::size_t pos) : structure_(structure)
{
for(auto & entry: structure_) {
cbegins_.push_back(entry.cbegin());
cends_.push_back(entry.cend());
++size_;
}
if(pos == std::numeric_limits<std::size_t>::max() || size_ == 0) {
absolutePosition_ = std::numeric_limits<std::size_t>::max();
return;
}
// Initialize with all cbegin() position
position_.reserve(size_);
for(std::size_t i = 0; i != size_; ++i) {
position_.push_back(cbegins_[i]);
if(cbegins_[i] == cends_[i]) {
// Empty member, so Cartesian product is empty
absolutePosition_ = std::numeric_limits<std::size_t>::max();
return;
}
}
// Increment to wanted position
for(std::size_t i = 0; i < pos; ++i) {
increment();
}
}
template<typename T>
void CartesianProductIterator<T>::increment()
{
if(absolutePosition_ == std::numeric_limits<std::size_t>::max()) {
return;
}
std::size_t pos = size_ - 1;
// Descend as far as necessary
while(++(position_[pos]) == cends_[pos] && pos != 0) {
--pos;
}
if(position_[pos] == cends_[pos]) {
assert(pos == 0);
absolutePosition_ = std::numeric_limits<std::size_t>::max();
return;
}
// Set all to begin behind pos
for(++pos; pos != size_; ++pos) {
position_[pos] = cbegins_[pos];
}
++absolutePosition_;
result_.emplace_back();
}
template<typename T>
std::vector<typename T::value_type::value_type> const& CartesianProductIterator<T>::dereference() const
{
if(absolutePosition_ == std::numeric_limits<std::size_t>::max()) {
throw new std::out_of_range("Out of bound dereference in CartesianProductIterator\n");
}
auto & result = result_[absolutePosition_];
if(result.empty()) {
result.reserve(size_);
for(auto & iterator: position_) {
result.push_back(*iterator);
}
}
return result;
}
template<typename T>
bool CartesianProductIterator<T>::equal(CartesianProductIterator<T> const& other) const
{
return absolutePosition_ == other.absolutePosition_ && structure_ == other.structure_;
}
//! Class that turns a forward iterable container of forward iterable containers into a forward iterable container which iterates over the Cartesian product of the forward iterable containers
template<typename T>
class CartesianProduct
{
public:
//! Constructor from type T
explicit CartesianProduct(T const& t) : t_(t) {}
//! Iterator to beginning of Cartesian product
CartesianProductIterator<T> begin() const { return CartesianProductIterator<T>(t_, 0); }
//! Iterator behind the last element of the Cartesian product
CartesianProductIterator<T> end() const { return CartesianProductIterator<T>(t_, std::numeric_limits<std::size_t>::max()); }
private:
T const& t_;
};
If someone has comments how to make it faster or better, I'd highly appreciate them.
I was just forced to implement this for a project I was working on and I came up with the code below. It can be stuck in a header and it's use is very simple but it returns all of the combinations you can get from a vector of vectors. The array that it returns only holds integers. This was a conscious decision because I just wanted the indices. In this way, I could index into each of the vector's vector and then perform the calculations I/anyone would need... best to avoid letting CartesianProduct hold "stuff" itself, it is a mathematical concept based around counting not a data structure. I'm fairly new to c++ but this was tested in a decryption algorithm pretty thoroughly. There is some light recursion but overall this is a simple implementation of a simple counting concept.
// Use of the CartesianProduct class is as follows. Give it the number
// of rows and the sizes of each of the rows. It will output all of the
// permutations of these numbers in their respective rows.
// 1. call cp.permutation() // need to check all 0s.
// 2. while cp.HasNext() // it knows the exit condition form its inputs.
// 3. cp.Increment() // Make the next permutation
// 4. cp.permutation() // get the next permutation
class CartesianProduct{
public:
CartesianProduct(int num_rows, vector<int> sizes_of_rows){
permutation_ = new int[num_rows];
num_rows_ = num_rows;
ZeroOutPermutation();
sizes_of_rows_ = sizes_of_rows;
num_max_permutations_ = 1;
for (int i = 0; i < num_rows; ++i){
num_max_permutations_ *= sizes_of_rows_[i];
}
}
~CartesianProduct(){
delete permutation_;
}
bool HasNext(){
if(num_permutations_processed_ != num_max_permutations_) {
return true;
} else {
return false;
}
}
void Increment(){
int row_to_increment = 0;
++num_permutations_processed_;
IncrementAndTest(row_to_increment);
}
int* permutation(){
return permutation_;
}
int num_permutations_processed(){
return num_permutations_processed_;
}
void PrintPermutation(){
cout << "( ";
for (int i = 0; i < num_rows_; ++i){
cout << permutation_[i] << ", ";
}
cout << " )" << endl;
}
private:
int num_permutations_processed_;
int *permutation_;
int num_rows_;
int num_max_permutations_;
vector<int> sizes_of_rows_;
// Because CartesianProduct is called first initially with it's values
// of 0 and because those values are valid and important output
// of the CartesianProduct we increment the number of permutations
// processed here when we populate the permutation_ array with 0s.
void ZeroOutPermutation(){
for (int i = 0; i < num_rows_; ++i){
permutation_[i] = 0;
}
num_permutations_processed_ = 1;
}
void IncrementAndTest(int row_to_increment){
permutation_[row_to_increment] += 1;
int max_index_of_row = sizes_of_rows_[row_to_increment] - 1;
if (permutation_[row_to_increment] > max_index_of_row){
permutation_[row_to_increment] = 0;
IncrementAndTest(row_to_increment + 1);
}
}
};
#include <iostream>
#include <vector>
void cartesian (std::vector<std::vector<int>> const& items) {
auto n = items.size();
auto next = [&](std::vector<int> & x) {
for ( int i = 0; i < n; ++ i )
if ( ++x[i] == items[i].size() ) x[i] = 0;
else return true;
return false;
};
auto print = [&](std::vector<int> const& x) {
for ( int i = 0; i < n; ++ i )
std::cout << items[i][x[i]] << ",";
std::cout << "\b \n";
};
std::vector<int> x(n);
do print(x); while (next(x)); // Shazam!
}
int main () {
std::vector<std::vector<int>>
items { { 1, 2, 3 }, { 4, 5 }, { 6, 7, 8 } };
cartesian(items);
return 0;
}
The idea behind this is as follows.
Let n := items.size().
Let m_i := items[i].size(), for all i in {0,1,...,n-1}.
Let M := {0,1,...,m_0-1} x {0,1,...,m_1-1} x ... x {0,1,...,m_{n-1}-1}.
We first solve the simpler problem of iterating through M. This is accomplished by the next lambda. The algorithm is simply the "carrying" routine grade schoolers use to add 1, albeit with a mixed radix number system.
We use this to solve the more general problem by transforming a tuple x in M to one of the desired tuples via the formula items[i][x[i]] for all i in {0,1,...,n-1}. We perform this transformation in the print lambda.
We then perform the iteration with do print(x); while (next(x));.
Now some comments on complexity, under the assumption that m_i > 1 for all i:
This algorithm requires O(n) space. Note that explicit construction of the Cartesian product takes O(m_0 m_1 m_2 ... m_{n-1}) >= O(2^n) space. So this is exponentially better on space than any algorithm which requires all tuples to be stored simultaneously in memory.
The next function takes amortized O(1) time (by a geometric series argument).
The print function takes O(n) time.
Hence, altogether, the algorithm has time complexity O(n|M|) and space complexity O(n) (not counting the cost of storing items).
An interesting thing to note is that if print is replaced with a function which inspects on average only O(1) coordinates per tuple rather than all of them, then time complexity falls to O(|M|), that is, it becomes linear time with respect to the size of the Cartesian product. In other words, avoiding the copy of the tuple each iterate can be meaningful in some situations.
This version supports no iterators or ranges, but it is a simple direct implementation that uses the multiplication operator to represent the Cartesian product, and a lambda to perform the action.
The interface is designed with the particular functionality I needed. I needed the flexibility to choose vectors over which to apply the Cartesian product in a way that did not obscure the code.
int main()
{
vector< vector<long> > v{ { 1, 2, 3 }, { 4, 5 }, { 6, 7, 8 } };
(Cartesian<long>(v[0]) * v[1] * v[2]).ForEach(
[](long p_Depth, long *p_LongList)
{
std::cout << p_LongList[0] << " " << p_LongList[1] << " " << p_LongList[2] << std::endl;
}
);
}
The implementation uses recursion up the class structure to implement the embedded for loops over each vector. The algorithm works directly on the input vectors, requiring no large temporary arrays. It is simple to understand and debug.
The use of std::function p_Action instead of void p_Action(long p_Depth, T *p_ParamList) for the lambda parameter would allow me to capture local variables, if I wanted to. In the above call, I don't.
But you knew that, didn't you. "function" is a template class which takes the type parameter of a function and makes it callable.
#include <vector>
#include <iostream>
#include <functional>
#include <string>
using namespace std;
template <class T>
class Cartesian
{
private:
vector<T> &m_Vector;
Cartesian<T> *m_Cartesian;
public:
Cartesian(vector<T> &p_Vector, Cartesian<T> *p_Cartesian=NULL)
: m_Vector(p_Vector), m_Cartesian(p_Cartesian)
{};
virtual ~Cartesian() {};
Cartesian<T> *Clone()
{
return new Cartesian<T>(m_Vector, m_Cartesian ? m_Cartesian->Clone() : NULL);
};
Cartesian<T> &operator *=(vector<T> &p_Vector)
{
if (m_Cartesian)
(*m_Cartesian) *= p_Vector;
else
m_Cartesian = new Cartesian(p_Vector);
return *this;
};
Cartesian<T> operator *(vector<T> &p_Vector)
{
return (*Clone()) *= p_Vector;
};
long Depth()
{
return m_Cartesian ? 1 + m_Cartesian->Depth() : 1;
};
void ForEach(function<void (long p_Depth, T *p_ParamList)> p_Action)
{
Loop(0, new T[Depth()], p_Action);
};
private:
void Loop(long p_Depth, T *p_ParamList, function<void (long p_Depth, T *p_ParamList)> p_Action)
{
for (T &element : m_Vector)
{
p_ParamList[p_Depth] = element;
if (m_Cartesian)
m_Cartesian->Loop(p_Depth + 1, p_ParamList, p_Action);
else
p_Action(Depth(), p_ParamList);
}
};
};