typedef a vector and boost::numeric::ublas::vector of fixed size

531 Views Asked by At

I mean to typedef a name for a vector / boost vector with fixed size, and then for corresponding iterators. What I could do is (see below)

typedef std::array<double, 3> point_3d_array;
typedef point_3d_array::iterator point_3d_iterator;
typedef point_3d_array::const_iterator point_3d_const_iterator;

The idea is to use later in my code something like

point_3d_array p;
for ( point_3d_const_iterator it = p.begin() ; it != p.end() ; it++ ) {
   my code
}

Question 1: Is this possible with

  1. std::vector<double>

  2. boost::numeric::ublas::vector<double>?

If it is not possible:

  1. Question 2: What is an alternative implementation? (other than that below).

  2. Question 3: How would I typedef the iterators?

As of now, since I couldn't find a way to do it, I defined my own class (see below). But this carries the burden of (at least) having to redefine my own begin, end and iterators (e.g., this). I mean to avoid this.

Question 4: I put together two alternative lines in the definition of operator+= (see below). One of them is not working. What is the problem?

typedef std::array<double, 3> point_3d_array;
typedef point_3d_array::iterator point_3d_iterator;
typedef point_3d_array::const_iterator point_3d_const_iterator;

class point_3d {
public:
    /*
     * Default constructor
     */
    point_3d() : _point_3d({ 0, 0, 0 }) { };
    /*
     * Initialization constructor
     * Is copy constructor automatically generated?
     */
    point_3d(const double x1, const double x2, const double x3) : _point_3d({x1, x2, x3}) {};

    /*
     * Iterator members
     */
    point_3d_iterator begin() { return _point_3d.begin(); }
    point_3d_iterator end() { return _point_3d.end(); }
    point_3d_const_iterator begin() const { return _point_3d.begin(); }
    point_3d_const_iterator end() const { return _point_3d.end(); }
    /*
     * Array subscript operators
     */
    double & operator[](size_t i) {
        return this->_point_3d[ i ];
    }
    const double & operator[](size_t i) const {
        return this->_point_3d[ i ];
    }

    /*
     * Basic operation members
     */
    point_3d & operator+=(const point_3d &rhs) {
        for ( size_t i = 0 ; i < this->_point_3d.size() ; i++ ) {
            //this[ i ] += rhs[ i ];  // Why are Array subscript operators not working in the lhs?
            this->_point_3d[ i ] += rhs[ i ];
        }
        return *this;
    }

private:
    point_3d_array _point_3d;
};
2

There are 2 best solutions below

1
Bathsheba On

Neither std::vector nor (at the time of writing) boost::numeric::ublas::vector are designed to be a fixed size. There's no template parameter that specifies the size of the container.

So no, a fixed sized typedef makes no sense for these containers.

If you want to constrain the size of a std::vector, then one approach would be to write your own template class with a std::vector to model the payload.

0
Cem Bassoy On

boost::numeric::ublas has a fixed size storage container and a fixed size vector. I am not sure if you have to usetypedef for iterator types and vector types. Here is a small example how to use both types and mix them even with std::array or std::vector.

#include <boost/numeric/ublas/storage.hpp>
#include <boost/numeric/ublas/vector.hpp>
#include <array>
#include <numeric>

using namespace boost::numeric;
using value_t = float;

int main () {
    constexpr auto N = 5ul;
    auto a = ublas::bounded_array<value_t,N>{N};
    auto b = ublas::fixed_vector<value_t,N>{};
    auto c = std::array<value_t,N>{};

    auto print = [](auto n, auto v){
        std::cout << n << "= "; 
        std::copy(v.begin(), v.end(), std::ostream_iterator<value_t>(std::cout, " ")); 
        std::cout << std::endl;
    };

    std::iota(a.begin(), a.end(), value_t(1));
    std::iota(b.begin(), b.end(), value_t(1));
    std::transform(a.begin(), a.end(), b.begin(), c.begin(), [](auto const& aa, auto const& bb){return aa+bb;});

    auto d = 2*b + b;

    print("a",a);
    print("b",b);
    print("c",c);
    print("d",d);
    return 0;
}