Steps towards making new array class be multidimensional.

This commit is contained in:
David Williams 2014-08-24 12:26:36 +02:00
parent 083c65ecd5
commit 9a1c6784df

View File

@ -26,22 +26,23 @@ distribution.
namespace PolyVox namespace PolyVox
{ {
template <typename ElementType> template <uint32_t noOfDims, typename ElementType>
class Array2D class Array2D
{ {
public: public:
Array2D(uint32_t width, uint32_t height) Array2D(uint32_t width, uint32_t height)
:m_uWidth(width) :m_pData(0)
, m_uHeight(height)
, m_pData(0)
{ {
m_pData = new ElementType[m_uWidth * m_uHeight]; m_uDimensions[0] = width;
m_uDimensions[1] = height;
initialize();
} }
// These are deleted to avoid accidental copying. // These are deleted to avoid accidental copying.
Array2D<ElementType>(const Array2D<ElementType>&) = delete; Array2D<noOfDims, ElementType>(const Array2D<noOfDims, ElementType>&) = delete;
Array2D<ElementType>& operator=(const Array2D<ElementType>&) = delete; Array2D<noOfDims, ElementType>& operator=(const Array2D<noOfDims, ElementType>&) = delete;
~Array2D() ~Array2D()
{ {
@ -50,14 +51,14 @@ namespace PolyVox
ElementType operator()(uint32_t x, uint32_t y) const ElementType operator()(uint32_t x, uint32_t y) const
{ {
POLYVOX_ASSERT((x < m_uWidth && y < m_uHeight), "Array access is out-of-range."); POLYVOX_ASSERT((x < m_uDimensions[0] && y < m_uDimensions[1]), "Array access is out-of-range.");
return m_pData[y * m_uWidth + x]; return m_pData[y * m_uDimensions[0] + x];
} }
ElementType& operator()(uint32_t x, uint32_t y) ElementType& operator()(uint32_t x, uint32_t y)
{ {
POLYVOX_ASSERT((x < m_uWidth && y < m_uHeight), "Array access is out-of-range."); POLYVOX_ASSERT((x < m_uDimensions[0] && y < m_uDimensions[1]), "Array access is out-of-range.");
return m_pData[y * m_uWidth + x]; return m_pData[y * m_uDimensions[0] + x];
} }
ElementType* getRawData() ElementType* getRawData()
@ -67,7 +68,7 @@ namespace PolyVox
size_t getNoOfElements() size_t getNoOfElements()
{ {
return m_uWidth * m_uHeight; return m_uNoOfElements;
} }
void swap(Array2D& other) void swap(Array2D& other)
@ -79,27 +80,38 @@ namespace PolyVox
private: private:
uint32_t m_uWidth; void initialize(void)
uint32_t m_uHeight; {
// Calculate the total number of elements in the array.
m_uNoOfElements = 1;
for (uint32_t i = 0; i < noOfDims; i++)
{
m_uNoOfElements *= m_uDimensions[i];
}
m_pData = new ElementType[m_uNoOfElements];
}
uint32_t m_uDimensions[noOfDims];
uint32_t m_uNoOfElements;
ElementType* m_pData; ElementType* m_pData;
}; };
///A 2D Array of floats. ///A 2D Array of floats.
typedef Array2D<float> Array2DFloat; typedef Array2D<2, float> Array2DFloat;
///A 2D Array of doubles. ///A 2D Array of doubles.
typedef Array2D<double> Array2DDouble; typedef Array2D<2, double> Array2DDouble;
///A 2D Array of signed 8-bit values. ///A 2D Array of signed 8-bit values.
typedef Array2D<int8_t> Array2DInt8; typedef Array2D<2, int8_t> Array2DInt8;
///A 2D Array of unsigned 8-bit values. ///A 2D Array of unsigned 8-bit values.
typedef Array2D<uint8_t> Array2DUint8; typedef Array2D<2, uint8_t> Array2DUint8;
///A 2D Array of signed 16-bit values. ///A 2D Array of signed 16-bit values.
typedef Array2D<int16_t> Array2DInt16; typedef Array2D<2, int16_t> Array2DInt16;
///A 2D Array of unsigned 16-bit values. ///A 2D Array of unsigned 16-bit values.
typedef Array2D<uint16_t> Array2DUint16; typedef Array2D<2, uint16_t> Array2DUint16;
///A 2D Array of signed 32-bit values. ///A 2D Array of signed 32-bit values.
typedef Array2D<int32_t> Array2DInt32; typedef Array2D<2, int32_t> Array2DInt32;
///A 2D Array of unsigned 32-bit values. ///A 2D Array of unsigned 32-bit values.
typedef Array2D<uint32_t> Array2DUint32; typedef Array2D<2, uint32_t> Array2DUint32;
} }
#endif //__PolyVox_Array2D_H__ #endif //__PolyVox_Array2D_H__