Vector Data Structure Interface

Numeric vectors provide two kinds of type specific operations. On the one hand there are methods to manage the vectors, such as setting the length and the default value and on the other hand there are methods to use vectors as values i.e., to set and get the vector elements.

You can manage the vector’s length with these methods:

void setLength (int pLength);

sets the length of a vector for the first time before initialization.

int getLength () const;

returns the vector’s length.

void changeLength (int pLength);

with this method you can change the size of an already initialized vector during a simulation.

Other methods for vector management are:

void setDefault (T pDefault);
T    getDefault ();

methods with whom you can set a default value for vector elements. This makes sense on an uninitialized vector. When you create a new vector, all its elements are set with this default value.

To operate on vector elements use the following methods:

Type&  operator []     (int index) const;
Type&  getElement      (unsigned int index);
int    getIntElement   (unsigned int index);
double getFloatElement (unsigned int index);
void   setElement      (unsigned int index, T pValue);

With these methods it is possible to access vector elements. Return methods are also defined as const methods that return a const reference. In case of a generic vector, operator [] returns a copy of the value inside the vector, therefore an expression like tVector[i] = tElement does not work and will cause a memory leak. All these methods throw an IndexOutOfRangeException in case the specified index does not exist.

Example 1

// create a vector of ten integers, all elements set to one.
TypeRef tVector = DsHandler::newIntVector(10,1);

// set the first element to 99
tVector.setElement(0,99);

// read the value of the first element.
int tValue = tIntVector.getIntElement(0);

To call operator [] on a vector it is necessary to have an object of IntVector or FloatVector, due to the operator [] that is not defined in the base class Type. For this it is necessary to use static_cast operator.

Example 2

// create a generic vector
TypeRef tVector = DsHandler::newValue("Root.Vector");

// set the default value to a defined data structure
tVector.setDefault(DsHandler::findClass("Root.NetworkProtocol.TCPProtocol"));

// change the vector length to 25.
// The vector is initialized with default elements.
tVector.changeLength(25);