Skip to content

Instantly share code, notes, and snippets.

@jefftrull
Created April 25, 2017 17:25
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jefftrull/f5d8ecb218815523f5d7af836eea8b9a to your computer and use it in GitHub Desktop.
Save jefftrull/f5d8ecb218815523f5d7af836eea8b9a to your computer and use it in GitHub Desktop.
Cannot sort with boost::iterator::zip_iterator
// experimenting with zip iterator and sort
#include <vector>
#include <iostream>
#include <boost/iterator/zip_iterator.hpp>
#include <boost/fusion/adapted/std_tuple.hpp>
using zip_it = boost::zip_iterator<std::tuple<std::vector<std::size_t>::iterator,
std::vector<std::size_t>::iterator,
std::vector<double>::iterator>>;
namespace std {
template<typename IndexT, typename NumericT>
void swap(typename std::tuple<IndexT&, IndexT&, NumericT&> a,
typename std::tuple<IndexT&, IndexT&, NumericT&> b)
{
std::cerr << "initial values: A={" << std::get<0>(a) << ", " << std::get<1>(a) << ", " << std::get<2>(a) << "} ";
std::cerr << "B={" << std::get<0>(b) << ", " << std::get<1>(b) << ", " << std::get<2>(b) << "}\n";
using std::swap;
swap(std::get<0>(a), std::get<0>(b));
swap(std::get<1>(a), std::get<1>(b));
swap(std::get<2>(a), std::get<2>(b));
std::cerr << "final values: A={" << std::get<0>(a) << ", " << std::get<1>(a) << ", " << std::get<2>(a) << "} ";
std::cerr << "B={" << std::get<0>(b) << ", " << std::get<1>(b) << ", " << std::get<2>(b) << "}" << std::endl;
}
}
int main() {
std::vector<std::size_t> rows{3, 2, 1};
std::vector<std::size_t> cols{1, 2, 3};
std::vector<double> values{4.0, 5.0, 6.0};
auto start = boost::make_zip_iterator(std::make_tuple(rows.begin(), cols.begin(), values.begin()));
auto stop = boost::make_zip_iterator(std::make_tuple(rows.end(), cols.end(), values.end()));
std::sort(start, stop);
for ( int i = 0; i < 3; ++i ) {
std::cerr << rows[i] << ", " << cols[i] << ", " << values[i] << "\n";
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment