• Main Page
  • Related Pages
  • Namespaces
  • Classes
  • Files
  • File List
  • File Members

PolyVoxCore/include/PolyVoxCore/Array.inl

Go to the documentation of this file.
00001 /*******************************************************************************
00002 Copyright (c) 2005-2009 David Williams
00003 
00004 This software is provided 'as-is', without any express or implied
00005 warranty. In no event will the authors be held liable for any damages
00006 arising from the use of this software.
00007 
00008 Permission is granted to anyone to use this software for any purpose,
00009 including commercial applications, and to alter it and redistribute it
00010 freely, subject to the following restrictions:
00011 
00012 1. The origin of this software must not be misrepresented; you must not
00013 claim that you wrote the original software. If you use this software
00014 in a product, an acknowledgment in the product documentation would be
00015 appreciated but is not required.
00016 
00017 2. Altered source versions must be plainly marked as such, and must not be
00018 misrepresented as being the original software.
00019 
00020 3. This notice may not be removed or altered from any source
00021 distribution.
00022 *******************************************************************************/
00023 
00024 namespace PolyVox
00025 {
00030     template <uint32_t noOfDims, typename ElementType>
00031     Array<noOfDims, ElementType>::Array()
00032         :m_pDimensions(0)
00033         ,m_pOffsets(0)
00034         ,m_uNoOfElements(0)
00035         ,m_pElements(0)
00036     {
00037     }
00038 
00045     template <uint32_t noOfDims, typename ElementType>
00046     Array<noOfDims, ElementType>::Array(const uint32_t (&pDimensions)[noOfDims])
00047         :m_pDimensions(0)
00048         ,m_pOffsets(0)
00049         ,m_uNoOfElements(0)
00050         ,m_pElements(0)
00051     {
00052         resize(pDimensions);
00053     }
00054 
00058     template <uint32_t noOfDims, typename ElementType>
00059     Array<noOfDims, ElementType>::~Array()
00060     {
00061         deallocate();
00062     }
00063 
00073     template <uint32_t noOfDims, typename ElementType>
00074     SubArray<noOfDims-1, ElementType> Array<noOfDims, ElementType>::operator[](uint32_t uIndex)
00075     {
00076         assert(uIndex<m_pDimensions[0]);
00077         return
00078             SubArray<noOfDims-1, ElementType>(&m_pElements[uIndex*m_pOffsets[0]],
00079             m_pDimensions+1, m_pOffsets+1);
00080     }
00081 
00091     template <uint32_t noOfDims, typename ElementType>
00092     const SubArray<noOfDims-1, ElementType> Array<noOfDims, ElementType>::operator[](uint32_t uIndex) const
00093     {
00094         assert(uIndex<m_pDimensions[0]);
00095         return
00096             SubArray<noOfDims-1, ElementType>(&m_pElements[uIndex*m_pOffsets[0]],
00097             m_pDimensions+1, m_pOffsets+1);
00098     }
00099 
00104     template <uint32_t noOfDims, typename ElementType>
00105     uint32_t Array<noOfDims, ElementType>::getNoOfElements(void) const
00106     {
00107         return m_uNoOfElements;
00108     }
00109 
00118     template <uint32_t noOfDims, typename ElementType>
00119     ElementType* Array<noOfDims, ElementType>::getRawData(void) const
00120     {
00121         return m_pElements;
00122     }
00123 
00130     template <uint32_t noOfDims, typename ElementType>
00131     void Array<noOfDims, ElementType>::resize(const uint32_t (&pDimensions)[noOfDims])
00132     {
00133         deallocate();
00134 
00135         m_pDimensions = new uint32_t[noOfDims];
00136         m_pOffsets = new uint32_t[noOfDims];
00137 
00138         // Calculate all the information you need to use the array
00139         m_uNoOfElements = 1;
00140         for (uint32_t i = 0; i<noOfDims; i++)
00141         {
00142             assert(pDimensions[i] != 0);
00143 
00144             m_uNoOfElements *= pDimensions[i];
00145             m_pDimensions[i] = pDimensions[i];
00146             m_pOffsets[i] = 1;
00147             for (uint32_t k=noOfDims-1; k>i; k--)
00148             {
00149                 m_pOffsets[i] *= pDimensions[k];
00150             }
00151         }
00152         // Allocate new elements, let exception propagate
00153         m_pElements = new ElementType[m_uNoOfElements];
00154     }
00155 
00162     template <uint32_t noOfDims, typename ElementType>
00163     void Array<noOfDims, ElementType>::swap(Array<noOfDims, ElementType>& rhs)
00164     {
00165         //Implement this function without temporary 'Array'
00166         //objects, as the destructors will free the memory...
00167         uint32_t* m_pTempDimensions = m_pDimensions;
00168         uint32_t* m_pTempOffsets = m_pOffsets;
00169         uint32_t m_uTempNoOfElements = m_uNoOfElements;
00170         ElementType* m_pTempElements = m_pElements;
00171 
00172         m_pDimensions = rhs.m_pDimensions;
00173         m_pOffsets = rhs.m_pOffsets;
00174         m_uNoOfElements = rhs.m_uNoOfElements;
00175         m_pElements = rhs.m_pElements;
00176 
00177         rhs.m_pDimensions = m_pTempDimensions;
00178         rhs.m_pOffsets = m_pTempOffsets;
00179         rhs.m_uNoOfElements = m_uTempNoOfElements;
00180         rhs.m_pElements = m_pTempElements;
00181     }
00182 
00186     template <uint32_t noOfDims, typename ElementType>
00187     uint32_t Array<noOfDims, ElementType>::getDimension(uint32_t uDimension)
00188     {
00189         assert(uDimension < noOfDims);
00190         return m_pDimensions[uDimension];
00191     }
00192 
00193     template <uint32_t noOfDims, typename ElementType>
00194     Array<noOfDims, ElementType>::Array(const Array<noOfDims, ElementType>& rhs)
00195         :m_pElements(0)
00196         ,m_pDimensions(0)
00197         ,m_pOffsets(0)
00198         ,m_uNoOfElements(0)
00199     {
00200         //Not implemented
00201         assert(false);
00202     }
00203 
00204     template <uint32_t noOfDims, typename ElementType>
00205     Array<noOfDims, ElementType>& Array<noOfDims, ElementType>::operator=(const Array<noOfDims, ElementType>& rhs)
00206     {
00207         //Not implemented
00208         assert(false);
00209 
00210         return *this;
00211     }
00212 
00213     template <uint32_t noOfDims, typename ElementType>
00214     void Array<noOfDims, ElementType>::deallocate(void)
00215     {
00216         delete[] m_pDimensions;
00217         m_pDimensions = 0;
00218         delete[] m_pOffsets;
00219         m_pOffsets = 0;
00220         delete[] m_pElements;
00221         m_pElements = 0;
00222 
00223         m_uNoOfElements = 0;
00224     }
00225 
00226     //****************************************************************************//
00227     // One dimensional specialisation begins here                                 //
00228     //****************************************************************************//
00229 
00230     template <typename ElementType>
00231     Array<1, ElementType>::Array()
00232         : m_pElements(0)
00233         ,m_pDimensions(0)
00234     {
00235     }
00236 
00237     template <typename ElementType>
00238     Array<1, ElementType>::Array(const uint32_t (&pDimensions)[1])
00239         : m_pElements(0)
00240         ,m_pDimensions(0)
00241     {
00242         resize(pDimensions);
00243     }
00244 
00245     template <typename ElementType>
00246     Array<1, ElementType>::~Array()
00247     {
00248         deallocate();
00249     }
00250 
00251     template <typename ElementType>
00252     ElementType& Array<1, ElementType>::operator[] (uint32_t uIndex)
00253     {
00254         assert(uIndex<m_pDimensions[0]);
00255         return m_pElements[uIndex];
00256     }
00257 
00258     template <typename ElementType>
00259     const ElementType& Array<1, ElementType>::operator[] (uint32_t uIndex) const
00260     {
00261         assert(uIndex<m_pDimensions[0]);
00262         return m_pElements[uIndex];
00263     }
00264 
00265     template <typename ElementType>
00266     uint32_t Array<1, ElementType>::getNoOfElements(void) const
00267     {
00268         return m_pDimensions[0];
00269     }
00270 
00271     template <typename ElementType>
00272     ElementType* Array<1, ElementType>::getRawData(void) const
00273     {
00274         return m_pElements;
00275     }
00276 
00277     template <typename ElementType>
00278     void Array<1, ElementType>::resize(const uint32_t (&pDimensions)[1])
00279     {
00280         deallocate();
00281 
00282         m_pDimensions = new uint32_t[1];
00283         m_pDimensions[0] = pDimensions[0];
00284 
00285         // Allocate new elements, let exception propagate
00286         m_pElements = new ElementType[m_pDimensions[0]];
00287     }
00288 
00289     template <typename ElementType>
00290     void Array<1, ElementType>::swap(Array<1, ElementType>& rhs)
00291     {
00292         //Implement this function without temporary 'Array'
00293         //objects, as the destructors will free the memory...
00294         uint32_t* m_pTempDimensions = m_pDimensions;
00295         ElementType* m_pTempElements = m_pElements;
00296 
00297         m_pDimensions = rhs.m_pDimensions;
00298         m_pElements = rhs.m_pElements;
00299 
00300         rhs.m_pDimensions = m_pTempDimensions;
00301         rhs.m_pElements = m_pTempElements;
00302     }
00303 
00304     template <typename ElementType>
00305     Array<1, ElementType>::Array(const Array<1, ElementType>& rhs)
00306         : m_pElements(0)
00307         ,m_pDimensions(0)
00308     {
00309         //Not implemented
00310         assert(false);
00311     }
00312 
00313     template <typename ElementType>
00314     Array<1, ElementType>& Array<1, ElementType>::operator=(const Array<1, ElementType>& rhs)
00315     {
00316         //Not implemented
00317         assert(false);
00318 
00319         return *this;
00320     }
00321 
00322     template <typename ElementType>
00323     void Array<1, ElementType>::deallocate(void)
00324     {
00325         delete[] m_pDimensions;
00326         m_pDimensions = 0;
00327         delete[] m_pElements;
00328         m_pElements = 0;
00329     }
00330 }//namespace PolyVox

Generated on Sat Nov 19 2011 00:27:30 for PolyVox by  doxygen 1.7.1