Boost logo

Boost Users :

Subject: Re: [Boost-users] Ranges to Collections
From: Neal Becker (ndbecker2_at_[hidden])
Date: 2011-06-07 11:57:25


Not all containers I use have a (begin, end) constructor:

#include <boost/range.hpp>
#include <algorithm>
#include <boost/numeric/ublas/vector.hpp>
#include <boost/multi_array.hpp>
#include <pyublas/numpy.hpp>

namespace detail {
  template<typename cont_t, typename range>
  struct construct_from_range_impl {
    cont_t operator() (range const& r) {
      return cont_t (boost::begin (r), boost::end (r));
    }
  };

  template<typename T, typename range>
  struct construct_from_range_impl<boost::numeric::ublas::vector<T>, range> {
    typedef typename boost::numeric::ublas::vector<T> ret_t;
    ret_t operator() (range const& r) {
      ret_t v (boost::size (r));
      std::copy (boost::begin (r), boost::end (r), v.begin());
      return v;
    }
  };

  template<typename T, typename range>
  struct construct_from_range_impl<boost::multi_array<T,1>, range> {
    typedef typename boost::multi_array<T,1> ret_t;
    ret_t operator() (range const& r) {
      ret_t v (r);
      return v;
    }
  };

  template<typename T, typename range>
  struct construct_from_range_impl<pyublas::numpy_vector<T>, range> {
    typedef typename pyublas::numpy_vector<T> ret_t;
    ret_t operator() (range const& r) {
      ret_t v (r);
      return v;
    }
  };
}

template<typename cont_t, typename range>
cont_t inline construct_from_range (range const& r) {
  return detail::construct_from_range_impl<cont_t,range>() (r);
}


Boost-users list run by williamkempf at hotmail.com, kalb at libertysoft.com, bjorn.karlsson at readsoft.com, gregod at cs.rpi.edu, wekempf at cox.net