Provides an efficient implementation of a multidimensional array. More...
#include <Array.h>
Public Member Functions | |
Array () | |
Constructor. | |
Array (const uint32_t(&pDimensions)[noOfDims]) | |
Constructor. | |
~Array () | |
Destructor. | |
SubArray< noOfDims-1, ElementType > | operator[] (uint32_t uIndex) |
Subarray access. | |
const SubArray< noOfDims-1, ElementType > | operator[] (uint32_t uIndex) const |
Subarray access. | |
uint32_t | getNoOfElements (void) const |
Gets the total number of elements in this array. | |
ElementType * | getRawData (void) const |
Gets a pointer to the first element of the array. | |
void | resize (const uint32_t(&pDimensions)[noOfDims]) |
Resize the array to the specified dimensions. | |
void | swap (Array< noOfDims, ElementType > &rhs) |
Swaps the contents of this array with the one specified. | |
uint32_t | getDimension (uint32_t uDimension) |
Get the size of the Array along the specified dimension. |
Provides an efficient implementation of a multidimensional array.
While C++ provides one-dimensional arrays as a language feature, it does not provide a simple and intuitive way of working with multidimensional arrays whose sizes are specified at runtime. Such a construct is very useful within the context of PolyVox, and this Array class provides such functionality implemented via templates and partial specialisation.
The following code snippet illustrates the basic usage of the class by writing a different value into each element:
int width = 5; int height = 10; int depth = 20; //Creates a 3D array of integers with dimensions 5x10x20 Array<3, int> myArray(ArraySizes(width)(height)(depth)); int ct = 1; for(int z = 0; z < depth; z++) { for(int y = 0; y < height; y++) { for(int x = 0; x < width; x++) { myArray[x][y][z] = ct; ct++; } } }
Although the constructor and resize() functions both take the required dimensions as an array of ints, note that the ArraySizes class can be used to build this inline. This is a more convienient way of specifying these dimensions.
Note also that this class has a private assignment operator and copy constructor in order to prevent copying. This is because a deep copy is a potentially slow operation and can often be performed inadvertently by functions such as std::swap, while a shallow copy introduces confusion over memory ownership.
Definition at line 76 of file Array.h.
PolyVox::Array< noOfDims, ElementType >::Array | ( | ) |
PolyVox::Array< noOfDims, ElementType >::Array | ( | const uint32_t(&) | pDimensions[noOfDims] | ) |
Constructor.
Creates an array with the specified dimensions.
pDimensions | The dimensions of the array. You can also use the ArraySizes class to construct this more easily. |
Definition at line 46 of file Array.inl.
PolyVox::Array< noOfDims, ElementType >::~Array | ( | ) |
uint32_t PolyVox::Array< noOfDims, ElementType >::getDimension | ( | uint32_t | uDimension | ) |
Get the size of the Array along the specified dimension.
uDimension | The dimension to get the size of. |
Definition at line 187 of file Array.inl.
Referenced by PolyVox::AmbientOcclusionCalculator< VolumeType, VoxelType >::AmbientOcclusionCalculator(), and PolyVox::AmbientOcclusionCalculator< VolumeType, VoxelType >::execute().
uint32_t PolyVox::Array< noOfDims, ElementType >::getNoOfElements | ( | void | ) | const |
Gets the total number of elements in this array.
Definition at line 105 of file Array.inl.
Referenced by PolyVox::SurfaceExtractor< VolumeType, VoxelType >::execute(), and PolyVox::CubicSurfaceExtractor< VolumeType, VoxelType >::execute().
ElementType * PolyVox::Array< noOfDims, ElementType >::getRawData | ( | void | ) | const |
Gets a pointer to the first element of the array.
Sometimes it is useful to directly manipulate the underlying array without going through this classes interface.
Although this does not honour the principle of encapsulation it can be done safely if you are careful and can sometimes be useful. Use getNoOfElements() to determine how far you can safely write.
Definition at line 119 of file Array.inl.
Referenced by PolyVox::SurfaceExtractor< VolumeType, VoxelType >::execute(), and PolyVox::CubicSurfaceExtractor< VolumeType, VoxelType >::execute().
const SubArray< noOfDims-1, ElementType > PolyVox::Array< noOfDims, ElementType >::operator[] | ( | uint32_t | uIndex | ) | const |
Subarray access.
An N-dimensional array can be conceptually consists of N subarrays each of which has N-1 dimensions.
For example, a 3D array conceptually consists of three 2D arrays. This operator is used to access the subarray at the specified index. Crucially, the subarray defines a similar operator allowing them to be chained together to convieniently access a particular element.
uIndex | The zero-based index of the subarray to retrieve. |
SubArray< noOfDims-1, ElementType > PolyVox::Array< noOfDims, ElementType >::operator[] | ( | uint32_t | uIndex | ) |
Subarray access.
An N-dimensional array can be conceptually consists of N subarrays each of which has N-1 dimensions.
For example, a 3D array conceptually consists of three 2D arrays. This operator is used to access the subarray at the specified index. Crucially, the subarray defines a similar operator allowing them to be chained together to convieniently access a particular element.
uIndex | The zero-based index of the subarray to retrieve. |
void PolyVox::Array< noOfDims, ElementType >::resize | ( | const uint32_t(&) | pDimensions[noOfDims] | ) |
Resize the array to the specified dimensions.
Please note that the existing contents of the array will be lost.
pDimensions | The new dimensions of the array. You can also use the ArraySizes class to specify this more easily. |
Definition at line 131 of file Array.inl.
Referenced by PolyVox::Array< 1, ElementType >::Array(), PolyVox::Array< noOfDims, ElementType >::Array(), and PolyVox::CubicSurfaceExtractor< VolumeType, VoxelType >::execute().
void PolyVox::Array< noOfDims, ElementType >::swap | ( | Array< noOfDims, ElementType > & | rhs | ) |
Swaps the contents of this array with the one specified.
Because this class does not have a public assignment operator or copy constructor it cannot be used with the STL swap() function.
This function provides an efficient implementation of that feature.
rhs | The array to swap this object with. |
Definition at line 163 of file Array.inl.
Referenced by PolyVox::SurfaceExtractor< VolumeType, VoxelType >::execute(), and PolyVox::CubicSurfaceExtractor< VolumeType, VoxelType >::execute().