kdynamicarray.h

Go to the documentation of this file.
00001 
00004 #ifndef FBXFILESDK_COMPONENTS_KBASELIB_KLIB_KDYNAMICARRAY_H
00005 #define FBXFILESDK_COMPONENTS_KBASELIB_KLIB_KDYNAMICARRAY_H
00006 
00007 /**************************************************************************************
00008 
00009  Copyright (C) 2001 - 2009 Autodesk, Inc. and/or its licensors.
00010  All Rights Reserved.
00011 
00012  The coded instructions, statements, computer programs, and/or related material 
00013  (collectively the "Data") in these files contain unpublished information 
00014  proprietary to Autodesk, Inc. and/or its licensors, which is protected by 
00015  Canada and United States of America federal copyright law and by international 
00016  treaties. 
00017  
00018  The Data may not be disclosed or distributed to third parties, in whole or in
00019  part, without the prior written consent of Autodesk, Inc. ("Autodesk").
00020 
00021  THE DATA IS PROVIDED "AS IS" AND WITHOUT WARRANTY.
00022  ALL WARRANTIES ARE EXPRESSLY EXCLUDED AND DISCLAIMED. AUTODESK MAKES NO
00023  WARRANTY OF ANY KIND WITH RESPECT TO THE DATA, EXPRESS, IMPLIED OR ARISING
00024  BY CUSTOM OR TRADE USAGE, AND DISCLAIMS ANY IMPLIED WARRANTIES OF TITLE, 
00025  NON-INFRINGEMENT, MERCHANTABILITY OR FITNESS FOR A PARTICULAR PURPOSE OR USE. 
00026  WITHOUT LIMITING THE FOREGOING, AUTODESK DOES NOT WARRANT THAT THE OPERATION
00027  OF THE DATA WILL BE UNINTERRUPTED OR ERROR FREE. 
00028  
00029  IN NO EVENT SHALL AUTODESK, ITS AFFILIATES, PARENT COMPANIES, LICENSORS
00030  OR SUPPLIERS ("AUTODESK GROUP") BE LIABLE FOR ANY LOSSES, DAMAGES OR EXPENSES
00031  OF ANY KIND (INCLUDING WITHOUT LIMITATION PUNITIVE OR MULTIPLE DAMAGES OR OTHER
00032  SPECIAL, DIRECT, INDIRECT, EXEMPLARY, INCIDENTAL, LOSS OF PROFITS, REVENUE
00033  OR DATA, COST OF COVER OR CONSEQUENTIAL LOSSES OR DAMAGES OF ANY KIND),
00034  HOWEVER CAUSED, AND REGARDLESS OF THE THEORY OF LIABILITY, WHETHER DERIVED
00035  FROM CONTRACT, TORT (INCLUDING, BUT NOT LIMITED TO, NEGLIGENCE), OR OTHERWISE,
00036  ARISING OUT OF OR RELATING TO THE DATA OR ITS USE OR ANY OTHER PERFORMANCE,
00037  WHETHER OR NOT AUTODESK HAS BEEN ADVISED OF THE POSSIBILITY OF SUCH LOSS
00038  OR DAMAGE. 
00039 
00040 **************************************************************************************/
00041 
00042 #include <fbxfilesdk/components/kbaselib/kbaselib_h.h>
00043 
00044 #include <fbxfilesdk/components/kbaselib/klib/kcontainerallocators.h>
00045 #include <fbxfilesdk/components/kbaselib/klib/kdebug.h>
00046 
00047 #include <new>
00048 
00049 #include <fbxfilesdk/fbxfilesdk_nsbegin.h>
00050 
00051 template <typename VALUE_TYPE, typename ALLOCATOR = KBaseAllocator>
00052 class KDynamicArray
00053 {
00054 public:
00055     typedef VALUE_TYPE ValueType;
00056     typedef ALLOCATOR AllocatorType;
00057 
00058     KDynamicArray()
00059         : mArray(NULL)
00060         , mArrayCapacity(0)
00061         , mValueCount(0)
00062         , mAllocator(sizeof(ValueType))
00063     {
00064     }
00065 
00066     KDynamicArray(size_t const pInitialSize)
00067         : mArray(NULL)
00068         , mArrayCapacity(0)
00069         , mValueCount(0)
00070         , mAllocator(sizeof(ValueType))
00071     {
00072         Reserve(pInitialSize);
00073     }
00074 
00075     KDynamicArray(KDynamicArray const& pArray)
00076         : mArray(NULL)
00077         , mArrayCapacity(0)
00078         , mValueCount(0)
00079         , mAllocator(sizeof(ValueType))
00080     {
00081         Reserve(pArray.mArrayCapacity);
00082         CopyArray(mArray, pArray.mArray, pArray.mValueCount);
00083         mValueCount = pArray.mValueCount;
00084     }
00085 
00086     ~KDynamicArray()
00087     {
00088         size_t i;
00089         for (i = 0; i < mValueCount; i++)
00090         {
00091             mArray[i].~VALUE_TYPE();
00092         }
00093 
00094         mAllocator.FreeMemory(mArray);
00095     }
00096 
00097     size_t GetCapacity() const
00098     {
00099         return mArrayCapacity;
00100     }
00101 
00102     size_t GetSize() const
00103     {
00104         return mValueCount;
00105     }
00106 
00107     void Reserve(size_t const pCount)
00108     {
00109         if (pCount > mArrayCapacity)
00110         {
00111             // We don't use mAllocator.PreAllocate, because we want our array
00112             // to be continuous in memory.
00113             void* lBuffer = mAllocator.AllocateRecords(pCount);
00114             ValueType* lNewArray = reinterpret_cast<ValueType*>(lBuffer);
00115 
00116             MoveArray(lNewArray, mArray, mValueCount);
00117 
00118             mAllocator.FreeMemory(mArray);
00119             mArray = lNewArray;
00120             mArrayCapacity = pCount;
00121         }
00122     }
00123 
00124     void PushBack(ValueType const& pValue, size_t const pNCopies = 1)
00125     {
00126         if (mValueCount + pNCopies > mArrayCapacity)
00127         {
00128             // grow by 50%
00129             size_t lNewSize = mArrayCapacity + mArrayCapacity / 2;
00130 
00131             if (mValueCount + pNCopies > lNewSize)
00132             {
00133                 lNewSize = mValueCount + pNCopies;
00134             }
00135 
00136             Reserve(lNewSize);
00137         }
00138 
00139         K_ASSERT(mValueCount + pNCopies <= mArrayCapacity);
00140 
00141         Fill(mArray + mValueCount, pValue, pNCopies);
00142 
00143         mValueCount += pNCopies;
00144     }
00145 
00146     void Insert(size_t const pIndex, ValueType const& pValue, size_t const pNCopies = 1)
00147     {
00148         K_ASSERT(pIndex >= 0);
00149         K_ASSERT(pIndex <= mValueCount);
00150 
00151         ValueType lValue = pValue; // in case pValue is in array
00152 
00153         if (pNCopies == 0)
00154         {
00155         }
00156         else if (pIndex >= mValueCount)
00157         {
00158             PushBack(pValue, pNCopies);
00159         }
00160         else if (mValueCount + pNCopies > mArrayCapacity)
00161         {
00162             // not enough room
00163             // grow by 50%
00164             size_t lNewSize = mArrayCapacity + mArrayCapacity / 2;
00165 
00166             if (mValueCount + pNCopies > lNewSize)
00167             {
00168                 lNewSize = mValueCount + pNCopies;
00169             }
00170 
00171             void* lBuffer = mAllocator.AllocateRecords(lNewSize);
00172             ValueType* lNewArray = reinterpret_cast<ValueType*>(lBuffer);
00173 
00174             MoveArray(lNewArray, mArray, pIndex); // copy prefix
00175             Fill(lNewArray + pIndex, pValue, pNCopies); // copy values
00176             MoveArray(lNewArray + pIndex + pNCopies, mArray + pIndex, mValueCount - pIndex); // copy suffix
00177 
00178             mAllocator.FreeMemory(mArray);
00179             mArray = lNewArray;
00180             mValueCount += pNCopies;
00181             mArrayCapacity = lNewSize;
00182         }
00183         else
00184         {
00185             // copy suffix backwards
00186             MoveArrayBackwards(mArray + pIndex + pNCopies, mArray + pIndex, mValueCount - pIndex);
00187             Fill(mArray + pIndex, pValue, pNCopies); // copy values
00188             mValueCount += pNCopies;
00189         }
00190     }
00191 
00192     void PopBack(size_t pNElements = 1)
00193     {
00194         K_ASSERT(pNElements <= mValueCount);
00195 
00196         size_t i;
00197         for (i = mValueCount - pNElements; i < mValueCount; i++)
00198         {
00199             mArray[i].~VALUE_TYPE();
00200         }
00201 
00202         mValueCount -= pNElements;
00203     }
00204 
00205     void Remove(size_t const pIndex, size_t pNElements = 1)
00206     {
00207         K_ASSERT(pIndex >= 0);
00208         K_ASSERT(pIndex <= mValueCount);
00209         K_ASSERT(pIndex + pNElements <= mValueCount);
00210 
00211         if (pIndex + pNElements >= mValueCount)
00212         {
00213             PopBack(pNElements);
00214         }
00215         else
00216         {
00217             size_t i;
00218             for (i = pIndex; i < pIndex + pNElements; i++)
00219             {
00220                 mArray[i].~VALUE_TYPE();
00221             }
00222 
00223             MoveOverlappingArray(mArray + pIndex, mArray + pIndex + pNElements, mValueCount - pNElements);
00224 
00225             mValueCount -= pNElements;
00226         }
00227     }
00228 
00229     ValueType& operator[](size_t const pIndex)
00230     {
00231         return *(mArray + pIndex);
00232     }
00233 
00234     ValueType const& operator[](size_t const pIndex) const
00235     {
00236         return *(mArray + pIndex);
00237     }
00238 
00239     KDynamicArray& operator=(KDynamicArray const& pArray)
00240     {
00241         Reserve(pArray.mArrayCapacity);
00242         CopyArray(mArray, pArray.mArray, pArray.mValueCount);
00243         mValueCount = pArray.mValueCount;
00244 
00245         return *this;
00246     }
00247 
00248 private:
00249     static void CopyArray(ValueType* pDest, ValueType const* pSrc, size_t pCount)
00250     {
00251         int i;
00252         for (i = 0; i < pCount; i++)
00253         {
00254 #if ( defined(MEMORY_DEBUG) && defined(_DEBUG) && defined(KARCH_ENV_WIN32))
00255 #pragma push_macro("new")
00256 #undef new
00257 #endif
00258             new(&(pDest[i])) ValueType(pSrc[i]);
00259 #if ( defined(MEMORY_DEBUG) && defined(_DEBUG) && defined(KARCH_ENV_WIN32))
00260 #pragma pop_macro("new")
00261 #endif
00262         }
00263     }
00264 
00265     static void MoveArray(ValueType* pDest, ValueType const* pSrc, size_t pCount)
00266     {
00267         int i;
00268         for (i = 0; i < pCount; i++)
00269         {
00270 #if ( defined(MEMORY_DEBUG) && defined(_DEBUG) && defined(KARCH_ENV_WIN32))
00271 #pragma push_macro("new")
00272 #undef new
00273 #endif
00274             new(&(pDest[i])) ValueType(pSrc[i]);
00275 #if ( defined(MEMORY_DEBUG) && defined(_DEBUG) && defined(KARCH_ENV_WIN32))
00276 #pragma pop_macro("new")
00277 #endif
00278         }
00279 
00280         for (i = 0; i < pCount; i++)
00281         {
00282             pSrc[i].~VALUE_TYPE();
00283         }
00284     }
00285 
00286     static void MoveOverlappingArray(ValueType* pDest, ValueType const* pSrc, size_t pCount)
00287     {
00288         int i;
00289         for (i = 0; i < pCount; i++)
00290         {
00291 #if ( defined(MEMORY_DEBUG) && defined(_DEBUG) && defined(KARCH_ENV_WIN32))
00292 #pragma push_macro("new")
00293 #undef new
00294 #endif
00295             new(&(pDest[i])) ValueType(pSrc[i]);
00296 #if ( defined(MEMORY_DEBUG) && defined(_DEBUG) && defined(KARCH_ENV_WIN32))
00297 #pragma pop_macro("new")
00298 #endif
00299 
00300             pSrc[i].~VALUE_TYPE();
00301         }
00302     }
00303 
00304     static void MoveArrayBackwards(ValueType* pDest, ValueType const* pSrc, size_t pCount)
00305     {
00306         size_t i;
00307         for (i = pCount - 1; i >= 0; i--)
00308         {
00309 #if ( defined(MEMORY_DEBUG) && defined(_DEBUG) && defined(KARCH_ENV_WIN32))
00310 #pragma push_macro("new")
00311 #undef new
00312 #endif
00313             new(&(pDest[i])) ValueType(pSrc[i]);
00314 #if ( defined(MEMORY_DEBUG) && defined(_DEBUG) && defined(KARCH_ENV_WIN32))
00315 #pragma pop_macro("new")
00316 #endif
00317 
00318             pSrc[i].~VALUE_TYPE();
00319         }
00320     }
00321 
00322     static void Fill(ValueType* pDest, ValueType const& pValue, size_t pCount)
00323     {
00324         size_t i;
00325         for (i = 0; i < pCount; i++)
00326         {
00327 #if ( defined(MEMORY_DEBUG) && defined(_DEBUG) && defined(KARCH_ENV_WIN32))
00328 #pragma push_macro("new")
00329 #undef new
00330 #endif
00331             new(&(pDest[i])) ValueType(pValue);
00332 #if ( defined(MEMORY_DEBUG) && defined(_DEBUG) && defined(KARCH_ENV_WIN32))
00333 #pragma pop_macro("new")
00334 #endif
00335         }
00336     }
00337 
00338 
00339     ValueType* mArray;
00340     size_t mArrayCapacity;
00341     size_t mValueCount;
00342 
00343     AllocatorType mAllocator;
00344 };
00345 
00346 #include <fbxfilesdk/fbxfilesdk_nsend.h>
00347 
00348 #endif // FBXFILESDK_COMPONENTS_KBASELIB_KLIB_KDYNAMICARRAY_H
00349