Constraining the length of std::vector<double>

Discussion in 'C++' started by Rune Allnor, Jan 16, 2009.

  1. Rune Allnor

    Rune Allnor Guest

    Hi all.

    I have an application where I need to do some computations on
    vectors of numerical data. Specifically, the routine requires an
    N-dimensional point and an N-dimensional vector as input.

    The naive approach is to implement this as two instances
    of std::vector<double>. However, I would like to include some
    error-checking on the lengths of the vectors. I would at least
    detect if the vectors are of different length (which would be
    an error) and preferably specify by means of template
    parameters exactly what the correct length should be.

    It is no problem to do this at run-time and control parameters,
    but this would imply a run-time overhead I would prefer
    to avoid, if possible, and do all this book-keeping at
    compile time instead.

    Now, I would be surprised if there is a way to constrain the
    sizes of the vectors at compile time, but I have to ask.

    Assuming the answer is 'no', are there alternative approches
    that achieve the goal?

    Rune
     
    Rune Allnor, Jan 16, 2009
    #1
    1. Advertising

  2. Rune Allnor

    Guest

    On Jan 16, 9:19 am, Rune Allnor <> wrote:
    > I have an application where I need to do some computations on
    > vectors of numerical data. Specifically, the routine requires an
    > N-dimensional point and an N-dimensional vector as input.
    > [...]
    > It is no problem to do this at run-time and control parameters,
    > but this would imply a run-time overhead I would prefer
    > to avoid, if possible, and do all this book-keeping at
    > compile time instead.


    How about something like this?

    #include <vector>

    using namespace std;

    template<unsigned VectorSize>
    class SizedDoubleVector : public vector<double>
    {
    public:
    SizedDoubleVector() : vector<double>(VectorSize) {}
    enum { size = VectorSize };
    };

    template<unsigned VectorSize>
    void do_something(SizedDoubleVector<VectorSize>& point,
    SizedDoubleVector<VectorSize>& vec)
    {
    for (int i = 0; i < SizedDoubleVector<VectorSize>::size; i++)
    point *= vec;
    }

    int
    main(int argc, char **argv)
    {
    SizedDoubleVector<3> three_point;
    SizedDoubleVector<3> three_vec;

    do_something(three_point, three_vec); // fine

    SizedDoubleVector<4> four_vec;
    do_something(three_point, four_vec); // compile error
    }

    Sean
     
    , Jan 16, 2009
    #2
    1. Advertising

  3. Rune Allnor

    Kai-Uwe Bux Guest

    wrote:

    > On Jan 16, 9:19 am, Rune Allnor <> wrote:
    >> I have an application where I need to do some computations on
    >> vectors of numerical data. Specifically, the routine requires an
    >> N-dimensional point and an N-dimensional vector as input.
    >> [...]
    >> It is no problem to do this at run-time and control parameters,
    >> but this would imply a run-time overhead I would prefer
    >> to avoid, if possible, and do all this book-keeping at
    >> compile time instead.

    >
    > How about something like this?
    >
    > #include <vector>
    >
    > using namespace std;
    >
    > template<unsigned VectorSize>
    > class SizedDoubleVector : public vector<double>
    > {
    > public:
    > SizedDoubleVector() : vector<double>(VectorSize) {}
    > enum { size = VectorSize };
    > };

    [snip]

    Since you inherit publicly, methods like push_back() are still available.
    Thus, you can change the length of the vector. Basically, the class is not
    enforcing its invariant but requires flawless cooperation from the
    programmer.

    One could use private inheritance and make only those methods available that
    do not change the length of the vector.


    Best

    Kai-Uwe Bux
     
    Kai-Uwe Bux, Jan 16, 2009
    #3
  4. On Jan 16, 2:19 pm, Rune Allnor <> wrote:
    > Hi all.
    >
    > I have an application where I need to do some computations on
    > vectors of numerical data. Specifically, the routine requires an
    > N-dimensional point and an N-dimensional vector as input.


    Check out std::valarray or a linear algebra library such as
    Boost.uBLAS. The latter has vector types for both static vectors (size
    known at compile-time) as well as dynamic vectors (size determined and
    variable at run-time). Only dynamic vectors are supported by
    std::valarray, and checks that operand sizes match are done at
    runtime.

    Regards,
    Vidar Hasfjord
     
    Vidar Hasfjord, Jan 16, 2009
    #4
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Kwaj
    Replies:
    1
    Views:
    645
    David Bishop
    Apr 25, 2004
  2. Sydex
    Replies:
    12
    Views:
    6,649
    Victor Bazarov
    Feb 17, 2005
  3. Anonymous
    Replies:
    20
    Views:
    4,428
    Pete Becker
    Mar 30, 2005
  4. Ingo Nolden
    Replies:
    15
    Views:
    1,548
    Jerry Coffin
    Apr 30, 2005
  5. Anonymous
    Replies:
    4
    Views:
    409
    Klaas Vantournhout
    Oct 17, 2007
Loading...

Share This Page