Institut de Robòtica i Informàtica Industrial
KRD Group

The CuikSuite Project

vector.c File Reference

Implementation of the functions operating on Tvector. More...

#include "vector.h"
#include "defines.h"
#include <stdlib.h>
#include <string.h>

Go to the source code of this file.

Functions

boolean CmpID (void *a, void *b)
 Comparison operator for identifiers.
void CopyID (void *a, void *b)
 Copy constructor for identifiers.
void DeleteID (void *a)
 Destructor for identifiers.
boolean CmpDouble (void *a, void *b)
 Comparison operator for doubles.
void CopyDouble (void *a, void *b)
 Copy constructor for doubles.
void DeleteDouble (void *a)
 Destructor for doubles.
boolean CmpVoidPtr (void *a, void *b)
 Comparison operator for void pointers.
void CopyVoidPtr (void *a, void *b)
 Copy constructor for void pointers.
void DeleteVoidPtr (void *a)
 Destructor for void pointers.
void InitVector (unsigned int ele_size, void(*Copy)(void *, void *), void(*Delete)(void *), unsigned int max_ele, Tvector *vector)
 Constructor.
void ResetVector (Tvector *vector)
 Resets a vector.
void CopyVector (Tvector *v_dst, Tvector *v_src)
 Copy constructor.
unsigned int VectorFirstFreePos (Tvector *vector)
 Finds a free position in the vector.
unsigned int VectorSize (Tvector *vector)
 Gets the number of elements in a vector.
unsigned int VectorMaxSize (Tvector *vector)
 Gets the maximum number of elements in a vector.
boolean VectorEmpty (Tvector *vector)
 Checks if a vector is empty.
boolean ElementInVector (void *e, boolean(*cmp)(void *, void *), Tvector *vector)
 Search for an element in a vector.
unsigned int FindPos (void *e, boolean(*cmp)(void *, void *), Tvector *vector)
 Search for an element in a vector.
unsigned int NewVectorElement (void *e, Tvector *vector)
 Adds an element to the vector.
unsigned int NewFreeVectorElement (void *e, Tvector *vector)
 Adds an element to the vector.
void SetVectorElement (unsigned int i, void *e, Tvector *vector)
 Adds an element to the vector in a given position.
void * GetVectorElement (unsigned int i, Tvector *vector)
 Returns a pointer to a vector element.
void * GetLastElement (Tvector *vector)
 Returns a pointer to the last element of a vector.
void RemoveVectorElement (unsigned int i, Tvector *vector)
 Removes an element from the vector.
void SwapVectorElements (unsigned int i, unsigned int j, Tvector *vector)
 Swaps two elements in a vector.
void ExtractVectorElement (unsigned int i, void *e, Tvector *vector)
 Extracts an element from a vector.
void MoveVectorElement (unsigned int i, unsigned int ni, Tvector *vector)
 Moves a vector element from one position to another.
void DeleteVector (void *vector)
 Destructor.

Detailed Description

Implementation of the functions operating on Tvector.

See also:
Tvector,vector.h,

Definition in file vector.c.


Function Documentation

boolean CmpID ( void *  a,
void *  b 
)

Comparison operator for identifiers. To be used when defining a vector of identifiers (i.e., unsigned int).

Parameters:
a Pointer to the first identifier to compare.
b Pointer to the second identifier to compare.
Returns:
TRUE if the two inputs are the same.

Definition at line 19 of file vector.c.

void CopyID ( void *  a,
void *  b 
)

Copy constructor for identifiers. To be used when defining a vector of identifiers (i.e., unsigned int). This is a direct assignation.

Parameters:
a Pointer to the identifier to initialize.
b Pointer to the identifier from where to copy.

Definition at line 24 of file vector.c.

Referenced by InitHeap().

Here is the caller graph for this function:

void DeleteID ( void *  a  ) 

Destructor for identifiers. To be used when defining a vector of identifiers (i.e., unsigned int). This basically frees the space taken by the identifier.

Parameters:
a Pointer to the identifier to delete.

Definition at line 29 of file vector.c.

Referenced by InitHeap().

Here is the caller graph for this function:

boolean CmpDouble ( void *  a,
void *  b 
)

Comparison operator for doubles. To be used when defining a vector of doubles.

Parameters:
a Pointer to the first double to compare.
b Pointer to the second double to compare.
Returns:
TRUE if the two inputs are the same.

Definition at line 34 of file vector.c.

void CopyDouble ( void *  a,
void *  b 
)

Copy constructor for doubles. To be used when defining a vector of doubles. This is a direct assignation.

Parameters:
a Pointer to the double to initialize.
b Pointer to the double from where to copy.

Definition at line 39 of file vector.c.

void DeleteDouble ( void *  a  ) 

Destructor for doubles. To be used when defining a vector of doubles. This basically frees the space taken by the double.

Parameters:
a Pointer to the double to delete.

Definition at line 44 of file vector.c.

boolean CmpVoidPtr ( void *  a,
void *  b 
)

Comparison operator for doubles. To be used when defining a vector of pointers.

Parameters:
a Pointer to the first pointer to compare.
b Pointer to the second pointer to compare.
Returns:
TRUE if the two inputs are the same.

Definition at line 49 of file vector.c.

void CopyVoidPtr ( void *  a,
void *  b 
)

Copy constructor for doubles. To be used when defining a vector of pointers. This is a direct assignation.

Parameters:
a Pointer to the pointer to initialize.
b Pointer to the pointer from where to copy.

Definition at line 54 of file vector.c.

Referenced by CopyLink(), CreateBranch(), GenerateKinTree(), InitLink(), and InitMechanism().

Here is the caller graph for this function:

void DeleteVoidPtr ( void *  a  ) 

Destructor for void pointers. To be used when defining a vector of pointers. This basically frees the space taken by the pointer.

Parameters:
a Pointer to the pointer to delete.

Definition at line 59 of file vector.c.

Referenced by CopyLink(), CreateBranch(), GenerateKinTree(), InitLink(), and InitMechanism().

Here is the caller graph for this function:

void InitVector ( unsigned int  ele_size,
void(*)(void *, void *)  Copy,
void(*)(void *)  Delete,
unsigned int  max_ele,
Tvector vector 
)

Initializes an empty vector Examples of use:

  • InitVector(sizeof(unsigned int),CopyID,DeleteID,MaxNumEle,&vector);
  • InitVector(sizeof(double),CopyDouble,DeleteDouble,MaxNumEle,&vector);
  • InitVector(sizeof(void *),CopyPtr,DeletePtr,MaxNumEle,&vector);
Parameters:
ele_size Number of bytes taken by each element in the list.
Copy Copy constructor for the elements in the list.
Delete Destructor for the elements in the list.
max_ele Initial number of elements in the vector. This can be extended on-line but the execution it is more efficient if we have a good guess for this value.
vector The vector to initialize.

Definition at line 67 of file vector.c.

References Tvector::Copy, Tvector::Delete, Tvector::ele_size, Tvector::elements, Tvector::max_ele, NEW, and Tvector::num_ele.

Referenced by CopyLink(), CreateBranch(), GenerateKinTree(), InitHeap(), InitLink(), and InitMechanism().

Here is the caller graph for this function:

void ResetVector ( Tvector vector  ) 

Removes all the elements from a vector but it does not frees the allocated memory nor resets the maximum number of elements. If you plan to re-use a vector it is more efficient to reset it instead of deleting it and re-initializing it again.

Parameters:
vector The vector to reset.

Definition at line 83 of file vector.c.

References Tvector::Delete, Tvector::elements, and Tvector::num_ele.

Referenced by DeleteVector(), and ResetHeap().

Here is the caller graph for this function:

void CopyVector ( Tvector v_dst,
Tvector v_src 
)

Initializes a vector from another vector.

Parameters:
v_dst The vector to initialize.
v_src The vector from where to copy.

Definition at line 101 of file vector.c.

References Tvector::Copy, Tvector::Delete, Tvector::ele_size, Tvector::elements, Tvector::max_ele, NEW, and Tvector::num_ele.

Referenced by CopyHeap().

Here is the caller graph for this function:

unsigned int VectorFirstFreePos ( Tvector vector  ) 

Returns the first (i.e., with lower identifier) free position in a vector.

Parameters:
vector The vector to query.
Returns:
The index of the free position.

Definition at line 125 of file vector.c.

References Tvector::elements.

Referenced by NewFreeVectorElement().

Here is the caller graph for this function:

unsigned int VectorSize ( Tvector vector  ) 

Returns the number of elements in a vector. Note that the vector can have "holes", i.e., empty slots in position explicitly deleted using RemoveVectorElement.

Parameters:
vector The vector to query.
Returns:
The number of elements in the vector.
See also:
RemoveVectorElement

Definition at line 136 of file vector.c.

References Tvector::num_ele.

Referenced by GetMechanismNJoints(), GetMechanismNLinks(), InitWorldKinCS(), LinkNBodies(), and nStepsBranch().

Here is the caller graph for this function:

unsigned int VectorMaxSize ( Tvector vector  ) 

Returns the maximum number of elements in a vector. Note that the vector can have "holes", i.e., empty slots in position explicitly deleted using RemoveVectorElement.

Parameters:
vector The vector to query.
Returns:
The maximum number of elements in the vector.

Definition at line 141 of file vector.c.

References Tvector::max_ele.

boolean VectorEmpty ( Tvector vector  ) 

Checks if a vector is empty

Parameters:
vector The vector to query.
Returns:
TRUE if there are no elements in the vector.

Definition at line 146 of file vector.c.

References Tvector::num_ele.

boolean ElementInVector ( void *  e,
boolean(*)(void *, void *)  cmp,
Tvector vector 
)

Looks for an element in a vector.

Parameters:
e The element we are looking for.
cmp The comparison operator to use to determine if two elements are the same. Note that for structured types the comparison can be based on a particular field and that only those field need to be initialized in parameter e.
vector The vector to query.
Returns:
TRUE if the vector includes the requested element.

Definition at line 151 of file vector.c.

References FindPos().

Here is the call graph for this function:

unsigned int FindPos ( void *  e,
boolean(*)(void *, void *)  cmp,
Tvector vector 
)

Looks for an element in a vector.

Parameters:
e The element we are looking for.
cmp The comparison operator to use to determine if two elements are the same. Note that for structured types the comparison can be based on a particular field and that only those field need to be initialized in parameter e.
vector The vector to query.
Returns:
The position in the vector holding the requested element or NO_UINT if the vector does not include the element.

Definition at line 156 of file vector.c.

References Tvector::elements, FALSE, and TRUE.

Referenced by ElementInVector().

Here is the caller graph for this function:

unsigned int NewVectorElement ( void *  e,
Tvector vector 
)

Adds an element at the end of the list.

Parameters:
e The element add to the vector.
vector The vector to update.
Returns:
The position in the vector where the element has been stored.

Definition at line 179 of file vector.c.

References Tvector::num_ele, and SetVectorElement().

Referenced by AddBody2Link(), AddStepToBranch(), and GenerateKinTree().

Here is the call graph for this function:

Here is the caller graph for this function:

unsigned int NewFreeVectorElement ( void *  e,
Tvector vector 
)

Looks for a free vector position and stores the given element.

Parameters:
e The element add to the vector.
vector The vector to update.
Returns:
The position in the vector where the element has been stored.

Definition at line 190 of file vector.c.

References SetVectorElement(), and VectorFirstFreePos().

Here is the call graph for this function:

void SetVectorElement ( unsigned int  i,
void *  e,
Tvector vector 
)

Adds an element to the vector in a given position. If the vector position was already occupied, the stored element is deleted before copying the new element.

Parameters:
i The vector position where to store the element.
e The element add to the vector.
vector The vector to update.

Definition at line 201 of file vector.c.

References Tvector::Copy, Tvector::Delete, Tvector::ele_size, Tvector::elements, Tvector::max_ele, MEM_EXPAND, NEW, and Tvector::num_ele.

Referenced by AddElement2Heap(), AddJoint2Mechanism(), AddLink2Mechanism(), NewFreeVectorElement(), and NewVectorElement().

Here is the caller graph for this function:

void* GetVectorElement ( unsigned int  i,
Tvector vector 
)

Returns a pointer to a given vector element.

Parameters:
i The vector position to retrive.
vector The vector to query.
Returns:
A pointer to the requested vector element or NULL if the vector position i is free.

Definition at line 236 of file vector.c.

References Tvector::elements.

Referenced by DeleteBranch(), DistanceToSubBoxCenter(), ExtractMinElement(), GetBranchStep(), GetHeapElement(), GetHeapPosition(), GetLinkBody(), GetMechanismJoint(), GetMechanismLink(), HeapDown(), HeapUp(), InitWorldKinCS(), PointInSubBox(), and PointInSystemBox().

Here is the caller graph for this function:

void* GetLastElement ( Tvector vector  ) 

Returns a pointer to a the last element of a vector.

Parameters:
vector The vector to query.
Returns:
A pointer to the last element of the vector.

Definition at line 244 of file vector.c.

References Tvector::elements, and Tvector::num_ele.

void RemoveVectorElement ( unsigned int  i,
Tvector vector 
)

Removes an element from the vector. Note that elements in the vector are not compacted after the deletion. In this way indexes in other data structured refered to this vector still hold after the deletion. The drawnback is that we can end up with a vector with "holes" and we have to be carefull when retriving elements from the vector.

Parameters:
i The vector position to delete.
vector The vector to query.

Definition at line 252 of file vector.c.

References Tvector::Delete, Tvector::elements, and Tvector::num_ele.

Referenced by ExtractMinElement(), ExtractVectorElement(), and MoveVectorElement().

Here is the caller graph for this function:

void SwapVectorElements ( unsigned int  i,
unsigned int  j,
Tvector vector 
)

Swaps two elements in a vector. This operation is provided since it is more efficient than extracting the two elements and then inserting them again in the vector in their new positions.

If one of the two elements to swap is empty, this function triggers an error.

Parameters:
i Index of the first element to swap.
j Index of the second element to swap.
vector The vector to update.

Definition at line 265 of file vector.c.

References Tvector::elements, and Error().

Referenced by ExtractMinElement(), HeapDown(), and HeapUp().

Here is the call graph for this function:

Here is the caller graph for this function:

void ExtractVectorElement ( unsigned int  i,
void *  e,
Tvector vector 
)

Extracts an element from a vector.

The element is copied to the given buffer and removed from the vector.

If the given element of the vector is empty, this function triggers an error.

Parameters:
i Index of the element to be extracted.
e Buffer where to copy the extracted element.
vector The vector to update.

Definition at line 285 of file vector.c.

References Tvector::Copy, Tvector::elements, Error(), and RemoveVectorElement().

Referenced by ExtractMinElement().

Here is the call graph for this function:

Here is the caller graph for this function:

void MoveVectorElement ( unsigned int  i,
unsigned int  ni,
Tvector vector 
)

Moves a vector element from one position to another.

If the element to move is empty, this function triggers an error.

If the element where to store the moved element is full, it is removed before the moving.

Parameters:
i The index of the element to move.
ni The desired new index for this element.
vector The vector to update.

Definition at line 306 of file vector.c.

References Tvector::elements, Error(), and RemoveVectorElement().

Here is the call graph for this function:

void DeleteVector ( void *  vector  ) 

Deletes the information stored in the vector and frees the allocated memory space.

Parameters:
vector The vector to be deleted.

Definition at line 332 of file vector.c.

References ResetVector().

Referenced by DeleteBranch(), DeleteHeap(), DeleteLink(), DeleteMechanism(), and InitWorldKinCS().

Here is the call graph for this function:

Here is the caller graph for this function: