P
P Kenter
Dear all.
I'm currently writing unit tests in C++ and am checking 2D or 3D
'arrays' for their dimension. These 'arrays' are implemented as
vectors of vectors. The problem is, the vectors of a vector can differ
in size if they are initialized incorrectly.
I've written a routine to check for this and would like your view on
style, etc. If I've implemented something that is part of the
language, please let me know.I couldn't find it.
This is the first time I've written a routine that uses templates so
I'd especially appreciate comments on that aspect.
The code is appended below. I hope the commments make it clear how it
works.
The 2nd and 3rd parameter together make up the dimensions that the
first parameter must have. I would like to combine parameter 2 and 3
into one parameter that is a vector of integers. However I would also
like to have brief init statements like this:
uint aDim_2_3_4[] = {2, 3, 4};
Any tips on that?
Regards, Pepijn Kenter
#include <assert.h>
#include <vector>
#include <iostream>
using namespace std;
// Returns true if vVector is a multi dimensional array of size
// (aDim[0] x aDim[1] x ...)
// param vVector the Vector to be checked
// param nDims the number of dimensions vVector should have
// param aDim array containing the dimensions vVector should have.
// aDim should be array of size(nDims), this is not checked.
template<class T> bool CorrectDimensions(const vector<T>& vVector,
const uint nDims,
const uint aDim[])
{
if (nDims == 0) {
return false;
} else {
// check if current dimension is correct
if (vVector.size() == aDim[0]) {
// check the next dimension for incorrect values
// if this is the last dimension (i.e. nDims-1==0) vVector
// must have no trailing dimensions. In that case the
// recursion shall return true because the non-vector
// routine is called.
for (uint nIdx = 0; nIdx < vVector.size(); nIdx++) {
if (!CorrectDimensions(vVector[nIdx], nDims-1,
aDim+1)) {
return false;
}
}
return true;
} else {
return false; // current dimension not correct size
}
}
assert(false); // should not come here
}
// Every type that is not a vector must have 0 dimensions.
template<class T> bool CorrectDimensions(const T& vVector,
const uint nDims,
const uint aDim[])
{
if (nDims == 0) {
return true;
} else {
return false;
}
}
int main(void)
{
uint aDim_2[] = {2};
uint aDim_2_3[] = {2, 3};
uint aDim_2_3_4[] = {2, 3, 4};
vector< vector< double > > vVector;
vVector.resize(2);
vVector[0].resize(3); // vVector[0] and vVector[1] differ in size!
vVector[1].resize(2);
cout << "Initialising vVector" << endl;
cout << "Check 2x3: " <<
CorrectDimensions(vVector, 2, aDim_2_3) << endl;
cout << "Check 2: " <<
CorrectDimensions(vVector, 1, aDim_2) << endl;
cout << "Check 2x3x4: " <<
CorrectDimensions(vVector, 3, aDim_2_3_4) << endl;
cout << endl;
// make vVector now a 2x3 array.
cout << "Resizing vVector" << endl;
vVector[1].resize(3);
cout << "Check 2x3: " <<
CorrectDimensions(vVector, 2, aDim_2_3) << endl;
cout << "Check 2: " <<
CorrectDimensions(vVector, 1, aDim_2) << endl;
cout << "Check 2x3x4: " <<
CorrectDimensions(vVector, 3, aDim_2_3_4) << endl;
cout << endl;
return 0;
}
I'm currently writing unit tests in C++ and am checking 2D or 3D
'arrays' for their dimension. These 'arrays' are implemented as
vectors of vectors. The problem is, the vectors of a vector can differ
in size if they are initialized incorrectly.
I've written a routine to check for this and would like your view on
style, etc. If I've implemented something that is part of the
language, please let me know.I couldn't find it.
This is the first time I've written a routine that uses templates so
I'd especially appreciate comments on that aspect.
The code is appended below. I hope the commments make it clear how it
works.
The 2nd and 3rd parameter together make up the dimensions that the
first parameter must have. I would like to combine parameter 2 and 3
into one parameter that is a vector of integers. However I would also
like to have brief init statements like this:
uint aDim_2_3_4[] = {2, 3, 4};
Any tips on that?
Regards, Pepijn Kenter
#include <assert.h>
#include <vector>
#include <iostream>
using namespace std;
// Returns true if vVector is a multi dimensional array of size
// (aDim[0] x aDim[1] x ...)
// param vVector the Vector to be checked
// param nDims the number of dimensions vVector should have
// param aDim array containing the dimensions vVector should have.
// aDim should be array of size(nDims), this is not checked.
template<class T> bool CorrectDimensions(const vector<T>& vVector,
const uint nDims,
const uint aDim[])
{
if (nDims == 0) {
return false;
} else {
// check if current dimension is correct
if (vVector.size() == aDim[0]) {
// check the next dimension for incorrect values
// if this is the last dimension (i.e. nDims-1==0) vVector
// must have no trailing dimensions. In that case the
// recursion shall return true because the non-vector
// routine is called.
for (uint nIdx = 0; nIdx < vVector.size(); nIdx++) {
if (!CorrectDimensions(vVector[nIdx], nDims-1,
aDim+1)) {
return false;
}
}
return true;
} else {
return false; // current dimension not correct size
}
}
assert(false); // should not come here
}
// Every type that is not a vector must have 0 dimensions.
template<class T> bool CorrectDimensions(const T& vVector,
const uint nDims,
const uint aDim[])
{
if (nDims == 0) {
return true;
} else {
return false;
}
}
int main(void)
{
uint aDim_2[] = {2};
uint aDim_2_3[] = {2, 3};
uint aDim_2_3_4[] = {2, 3, 4};
vector< vector< double > > vVector;
vVector.resize(2);
vVector[0].resize(3); // vVector[0] and vVector[1] differ in size!
vVector[1].resize(2);
cout << "Initialising vVector" << endl;
cout << "Check 2x3: " <<
CorrectDimensions(vVector, 2, aDim_2_3) << endl;
cout << "Check 2: " <<
CorrectDimensions(vVector, 1, aDim_2) << endl;
cout << "Check 2x3x4: " <<
CorrectDimensions(vVector, 3, aDim_2_3_4) << endl;
cout << endl;
// make vVector now a 2x3 array.
cout << "Resizing vVector" << endl;
vVector[1].resize(3);
cout << "Check 2x3: " <<
CorrectDimensions(vVector, 2, aDim_2_3) << endl;
cout << "Check 2: " <<
CorrectDimensions(vVector, 1, aDim_2) << endl;
cout << "Check 2x3x4: " <<
CorrectDimensions(vVector, 3, aDim_2_3_4) << endl;
cout << endl;
return 0;
}