More work on multidimensional Array class.

This commit is contained in:
David Williams
2010-03-28 23:18:50 +00:00
parent 834b747171
commit 3e1c07550b
3 changed files with 223 additions and 146 deletions

View File

@ -39,12 +39,58 @@ namespace PolyVox
class Array
{
public:
Array<noOfDims, ElementType>()
:m_pElements(0)
,m_pDimensions(0)
,m_pOffsets(0)
,m_uNoOfElements(0)
{
}
Array<noOfDims, ElementType>(const uint32_t (&pDimensions)[noOfDims])
:m_pElements(0)
,m_pDimensions(0)
,m_pOffsets(0)
,m_uNoOfElements(0)
{
resize(pDimensions);
}
~Array<noOfDims, ElementType>()
{
deallocate();
}
SubArray<noOfDims-1, ElementType> operator[](uint32_t uIndex)
{
assert(uIndex<m_pDimensions[0]);
return
SubArray<noOfDims-1, ElementType>(&m_pElements[uIndex*m_pOffsets[0]],
m_pDimensions+1, m_pOffsets+1);
}
const SubArray<noOfDims-1, ElementType> operator[](uint32_t uIndex) const
{
assert(uIndex<m_pDimensions[0]);
return
SubArray<noOfDims-1, ElementType>(&m_pElements[uIndex*m_pOffsets[0]],
m_pDimensions+1, m_pOffsets+1);
}
uint32_t getNoOfElements(void) const
{
return m_uNoOfElements;
}
ElementType* getRawData(void) const
{
return m_pElements;
}
void resize(const uint32_t (&pDimensions)[noOfDims])
{
deallocate();
m_pDimensions = new uint32_t[noOfDims];
m_pOffsets = new uint32_t[noOfDims];
@ -64,42 +110,59 @@ namespace PolyVox
}
// Allocate new elements, let exception propagate
m_pElements = new ElementType[m_uNoOfElements];
}
~Array<noOfDims, ElementType>()
{
delete[] m_pDimensions;
delete[] m_pOffsets;
delete[] m_pElements;
}
SubArray<noOfDims-1, ElementType> operator [](uint32_t uIndex)
void swap(Array<noOfDims, ElementType>& rhs)
{
assert(uIndex<m_pDimensions[0]);
return
SubArray<noOfDims-1, ElementType>(&m_pElements[uIndex*m_pOffsets[0]],
m_pDimensions+1, m_pOffsets+1);
}
//Implement this function without temporary 'Array'
//objects, as the destructors will free the memory...
uint32_t* m_pTempDimensions = m_pDimensions;
uint32_t* m_pTempOffsets = m_pOffsets;
uint32_t m_uTempNoOfElements = m_uNoOfElements;
ElementType* m_pTempElements = m_pElements;
const SubArray<noOfDims-1, ElementType> operator [](uint32_t uIndex) const
{
assert(uIndex<m_pDimensions[0]);
return
SubArray<noOfDims-1, ElementType>(&m_pElements[uIndex*m_pOffsets[0]],
m_pDimensions+1, m_pOffsets+1);
}
m_pDimensions = rhs.m_pDimensions;
m_pOffsets = rhs.m_pOffsets;
m_uNoOfElements = rhs.m_uNoOfElements;
m_pElements = rhs.m_pElements;
ElementType* getRawData(void)
{
return m_pElements;
}
uint32_t getNoOfElements(void)
{
return m_uNoOfElements;
rhs.m_pDimensions = m_pTempDimensions;
rhs.m_pOffsets = m_pTempOffsets;
rhs.m_uNoOfElements = m_uTempNoOfElements;
rhs.m_pElements = m_pTempElements;
}
private:
Array<noOfDims, ElementType>(const Array<noOfDims, ElementType>& rhs)
:m_pElements(0)
,m_pDimensions(0)
,m_pOffsets(0)
,m_uNoOfElements(0)
{
//Not implemented
assert(false);
}
Array<noOfDims, ElementType>& Array<noOfDims, ElementType>::operator=(const Array<noOfDims, ElementType>& rhs)
{
//Not implemented
assert(false);
return *this;
}
void deallocate(void)
{
delete[] m_pDimensions;
m_pDimensions = 0;
delete[] m_pOffsets;
m_pOffsets = 0;
delete[] m_pElements;
m_pElements = 0;
m_uNoOfElements = 0;
}
uint32_t * m_pDimensions;
uint32_t * m_pOffsets;
uint32_t m_uNoOfElements;
@ -110,40 +173,96 @@ namespace PolyVox
class Array<1, ElementType>
{
public:
Array<1, ElementType>()
: m_pElements(0)
,m_pDimensions(0)
{
}
Array<1, ElementType>(const uint32_t (&pDimensions)[1])
: m_pElements(0)
,m_pDimensions(0)
{
resize(pDimensions);
}
~Array<1, ElementType>()
{
deallocate();
}
ElementType& operator[] (uint32_t uIndex)
{
assert(uIndex<m_pDimensions[0]);
return m_pElements[uIndex];
}
const ElementType& operator[] (uint32_t uIndex) const
{
assert(uIndex<m_pDimensions[0]);
return m_pElements[uIndex];
}
uint32_t getNoOfElements(void) const
{
return m_pDimensions[0];
}
ElementType* getRawData(void) const
{
return m_pElements;
}
void resize(const uint32_t (&pDimensions)[1])
{
deallocate();
m_pDimensions = new uint32_t[1];
m_pDimensions[0] = pDimensions[0];
// Allocate new elements, let exception propagate
m_pElements = new ElementType[m_pDimensions[0]];
}
ElementType & operator [] (uint32_t uIndex)
{
assert(uIndex<m_pDimensions[0]);
return m_pElements[uIndex];
}
const ElementType & operator [] (uint32_t uIndex) const
void swap(Array<1, ElementType>& rhs)
{
assert(uIndex<m_pDimensions[0]);
return m_pElements[uIndex];
}
//Implement this function without temporary 'Array'
//objects, as the destructors will free the memory...
uint32_t* m_pTempDimensions = m_pDimensions;
ElementType* m_pTempElements = m_pElements;
ElementType* getRawData(void)
{
return m_pElements;
}
m_pDimensions = rhs.m_pDimensions;
m_pElements = rhs.m_pElements;
uint32_t getNoOfElements(void)
{
return m_pDimensions[0];
rhs.m_pDimensions = m_pTempDimensions;
rhs.m_pElements = m_pTempElements;
}
private:
Array<1, ElementType>(const Array<1, ElementType>& rhs)
: m_pElements(0)
,m_pDimensions(0)
{
//Not implemented
assert(false);
}
Array<1, ElementType>& Array<1, ElementType>::operator=(const Array<1, ElementType>& rhs)
{
//Not implemented
assert(false);
return *this;
}
void deallocate(void)
{
delete[] m_pDimensions;
m_pDimensions = 0;
delete[] m_pElements;
m_pElements = 0;
}
uint32_t * m_pDimensions;
ElementType * m_pElements;
};