Velocity Reviews

Velocity Reviews (http://www.velocityreviews.com/forums/index.php)
-   C++ (http://www.velocityreviews.com/forums/f39-c.html)
-   -   Constraining the length of std::vector<double> (http://www.velocityreviews.com/forums/t665852-constraining-the-length-of-std-vector-double.html)

Rune Allnor 01-16-2009 02:19 PM

Constraining the length of std::vector<double>
 
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



sean_in_raleigh@yahoo.com 01-16-2009 02:49 PM

Re: Constraining the length of std::vector<double>
 
On Jan 16, 9:19 am, Rune Allnor <all...@tele.ntnu.no> 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[i] *= vec[i];
}

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

Kai-Uwe Bux 01-16-2009 03:14 PM

Re: Constraining the length of std::vector<double>
 
sean_in_raleigh@yahoo.com wrote:

> On Jan 16, 9:19 am, Rune Allnor <all...@tele.ntnu.no> 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

Vidar Hasfjord 01-16-2009 09:46 PM

Re: Constraining the length of std::vector<double>
 
On Jan 16, 2:19*pm, Rune Allnor <all...@tele.ntnu.no> 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


All times are GMT. The time now is 02:36 AM.

Powered by vBulletin®. Copyright ©2000 - 2014, vBulletin Solutions, Inc.
SEO by vBSEO ©2010, Crawlability, Inc.