[Insight-users] vnl_vector class

Luis Ibanez luis . ibanez at kitware . com
Tue, 03 Sep 2002 13:48:30 -0400


Hi Bjorn,

You have several options depending on what is the expected
use for your class.

Is this expected to be dynamic ?
That is, do you plan to insert and remove elements from the
Main vector and/or the Subvectors ?

Or instead, your main concern is the performace for
getting access to the elements in the vectors ?

These factor may decide what is the best balace for your design.

vnl_vector are resizeable only by assignment from another vector.
for example:

      v = vnl_vector<double>( 10 );

But you cannot insert in a vnl_vector in the way you do with
a std::vector using push_back();

--

One first thing you may want to avoid it to declare a vector
of vectors. Since assignments in the main vector will be done
by copy an will result very costly. (this may not be an issue
if you plan to load the data structure with data only once and
then only perform read/write access on the elements)

A vector of pointers to vectors could be, in general, a better
combination.

Something like:

class myVectorOfVectors {
public:
    typedef vnl_vector<    double     > SubVectorType;
    typedef vnl_vector< SubVectorType * > MainVectorType;
    VectorX():m_V(3) {
     m_V[0] = new SubVectorType(2);
     m_V[1] = new SubVectorType(3);
     m_V[3] = new SubVectorType(4);
     }
   double GetElement( int x, int y ) { return   (*m_V[x])[y];  }
   private:
     MainVectorType   m_V;
};


This example creates the Vector of pointers to Vectors<double>.
and initialize it with three vectors of length 2,3 and 4 as
you suggested.

If you don't need the Vectors to be resizable, you may also
be interested in using the itk::FixedArray classs.

Also keep in mind that there is an itk::Array classs that
derives from vnl_vector.

---

Please let us know if that corresponds to what
you were looking for,

Thanks


    Luis



==========================================

Bjorn Hanch Sollie wrote:
 > Hi all!
 >
 > I'm currently trying to use the vnl_vector class to automatically
 > create a vector containing a variable number of vectors of variable
 > length (based on input data).  Since I'm having some problems with
 > this, I'd appreciate some advice on how to do it, if possible.
 >
 > For example, if I specify that I want a vector with 3 elements,
 > containing 3 vectors of length 2, 3 and 4 respectively, I would like
 > my program to initialize this automatically.  The problem seems to be
 > that all the vectors have names, and keeping track of all the names of
 > the "sub-vectors" seems to be a rather cumbersome task, plus my
 > program would have to generate those names in some way.
 >
 > While it is possible to use the vnl_matrix class to this end, I'd
 > rather not, because one of the vectors will often tend to be very
 > large, and there are often a lot of smaller vectors; thus such a
 > (sparse) matrix would waste a lot of memory (in the order of tens of
 > megabytes in my application).
 >
 > If doing this with the vnl_vector class is nontrivial, can anyone
 > please give me any hints about memory-preserving alternatives?
 >
 > Thanks in advance!
 >
 > -Bjorn
 >