Public Member Functions

PolyVox::Array< noOfDims, ElementType > Class Template Reference

Provides an efficient implementation of a multidimensional array. More...

#include <Array.h>

Inheritance diagram for PolyVox::Array< noOfDims, ElementType >:
Collaboration diagram for PolyVox::Array< noOfDims, ElementType >:

List of all members.

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.

Detailed Description

template<uint32_t noOfDims, typename ElementType>
class PolyVox::Array< noOfDims, ElementType >

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.


Constructor & Destructor Documentation

template<uint32_t noOfDims, typename ElementType >
PolyVox::Array< noOfDims, ElementType >::Array (  ) 

Constructor.

Creates an empty array with no elements.

You will have to call resize() on this array before it can be used.

Definition at line 31 of file Array.inl.

template<uint32_t noOfDims, typename ElementType >
PolyVox::Array< noOfDims, ElementType >::Array ( const uint32_t(&)  pDimensions[noOfDims]  ) 

Constructor.

Creates an array with the specified dimensions.

Parameters:
pDimensions The dimensions of the array. You can also use the ArraySizes class to construct this more easily.
See also:
ArraySizes

Definition at line 46 of file Array.inl.

Here is the call graph for this function:

template<uint32_t noOfDims, typename ElementType >
PolyVox::Array< noOfDims, ElementType >::~Array (  ) 

Destructor.

Destroys the array and releases all owned memory.

Definition at line 59 of file Array.inl.


Member Function Documentation

template<uint32_t noOfDims, typename ElementType >
uint32_t PolyVox::Array< noOfDims, ElementType >::getDimension ( uint32_t  uDimension  ) 

Get the size of the Array along the specified dimension.

Parameters:
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().

template<uint32_t noOfDims, typename ElementType >
uint32_t PolyVox::Array< noOfDims, ElementType >::getNoOfElements ( void   )  const

Gets the total number of elements in this array.

Returns:
The number of elements in the array.
See also:
getRawData()

Definition at line 105 of file Array.inl.

Referenced by PolyVox::SurfaceExtractor< VolumeType, VoxelType >::execute(), and PolyVox::CubicSurfaceExtractor< VolumeType, VoxelType >::execute().

template<uint32_t noOfDims, typename ElementType >
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.

Returns:
A pointer to the first element of the array
See also:
getNoOfElements()

Definition at line 119 of file Array.inl.

Referenced by PolyVox::SurfaceExtractor< VolumeType, VoxelType >::execute(), and PolyVox::CubicSurfaceExtractor< VolumeType, VoxelType >::execute().

template<uint32_t noOfDims, typename ElementType >
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.

Parameters:
uIndex The zero-based index of the subarray to retrieve.
Returns:
The requested SubArray

Definition at line 92 of file Array.inl.

template<uint32_t noOfDims, typename ElementType >
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.

Parameters:
uIndex The zero-based index of the subarray to retrieve.
Returns:
The requested SubArray

Definition at line 74 of file Array.inl.

template<uint32_t noOfDims, typename ElementType >
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.

Parameters:
pDimensions The new dimensions of the array. You can also use the ArraySizes class to specify this more easily.
See also:
ArraySizes

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().

template<uint32_t noOfDims, typename ElementType>
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.

Parameters:
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().


The documentation for this class was generated from the following files: