Array.h

Go to the documentation of this file.
00001 //*****************************************************************************/
00002 // Copyright (c) 1998-2006 Autodesk, Inc.
00003 // All rights reserved.
00004 // 
00005 // These coded instructions, statements, and computer programs contain
00006 // unpublished proprietary information written by Autodesk, Inc., and are
00007 // protected by Federal copyright law. They may not be disclosed to third
00008 // parties or copied or duplicated in any form, in whole or in part, without
00009 // the prior written consent of Autodesk, Inc.
00010 //*****************************************************************************/
00011 /*==============================================================================
00012 
00013     file:     Array.h
00014     author:   Daniel Levesque
00015     created:  27 March 2006
00016     description:
00017         Array container.
00018 
00019 ==============================================================================*/
00020 #pragma once
00021 
00022 #include "..\maxheap.h"
00023 #include <memory.h>     
00024 #include <stdlib.h>
00025 #include <new>
00026 #include "../assert1.h"
00027 #include "../utilexp.h"
00028 #include "../coreexp.h"
00029 
00030 UtilExport void UtilOutOfMemoryException();
00031 UtilExport void* UtilAllocateMemory(size_t);
00032 UtilExport void UtilDeallocateMemory(void*);
00033 
00034 namespace MaxSDK {
00035 
00036 //==============================================================================
00037 // class MaxSDK::Array
00038 //
00040 
00048 template <class T> class Array
00049 : public MaxHeapOperators {
00050 public:
00051 
00053 
00058     typedef int( __cdecl *CompareFnc) ( const void *elem1, const void *elem2 );
00059 
00061     Array();
00068     Array(size_t initUsedLength, const T& defaultVal = T(), size_t initGrowLength = kDefaultGrowthLength);
00072     Array(const Array<T>& src);
00074     ~Array();
00075 
00080     Array<T>& operator=(const Array<T>& src);
00087     bool operator==(const Array<T>& op) const;
00088 
00090 
00096     T& operator[](size_t i);
00097     const T& operator[](size_t i) const;
00099 
00101 
00107     const T& at(size_t index) const;
00108     T& at(size_t index);
00110 
00118     Array<T>& setAt(size_t index, const T& value);
00123     Array<T>& setAll(const T& value);
00124 
00126 
00130     T& first();
00131     const T& first() const;
00133 
00135 
00139     T& last();
00140     const T& last() const;
00142 
00147     size_t append(const T& value);
00154     Array<T>& append(const T* values, size_t count);
00159     Array<T>& append(const Array<T>& array);
00160 
00167     Array<T>& insertAt(size_t index, const T& value);
00176     Array<T>& insertAt(size_t index, const T* values, size_t count);
00177 
00183     Array<T>& removeAt(size_t index);
00192     bool remove(const T& value, size_t start = 0);
00197     Array<T>& removeFirst();
00202     Array<T>& removeLast();
00206     Array<T>& removeAll();
00215     Array<T>& removeSubArray(size_t startIndex, size_t endIndex);
00216 
00223     bool contains(const T& value, size_t start = 0) const;
00232     bool find(const T& value, size_t& foundAt, size_t start = 0) const;
00239     size_t find(const T& value) const;
00247     size_t findFrom(const T& value, size_t start) const;
00248 
00250     size_t length() const; // Used length.
00252     bool isEmpty() const;
00254     size_t lengthUsed() const;
00261     Array<T>& setLengthUsed(size_t length, const T& defaultVal = T());
00263     size_t lengthReserved() const;
00268     Array<T>& setLengthReserved(size_t length);
00269 
00272     size_t growLength() const;
00274 
00277     Array<T>& setGrowLength(size_t);
00278 
00283     Array<T>& reverse();
00288     Array<T>& swap(size_t i1, size_t i2);
00294     void sort(CompareFnc cmp);
00295 
00297 
00301     const T* asArrayPtr() const;
00302     T* asArrayPtr();
00304 
00308     bool isValidIndex(size_t) const;
00309 
00310 protected:
00311 
00312     enum {
00318         kArrayGrowthThreshold = 0x10000,
00320         kDefaultGrowthLength = 8
00321     };
00322 
00324     T* mpArray;
00326     size_t mReservedLen;
00328     size_t mUsedLen;
00330     size_t mGrowLen;
00331 
00333     static size_t quickSortPartition(T* data, size_t first, size_t last, CompareFnc cmp);
00335     static void quickSortRecursive(T* data, size_t first, size_t last, CompareFnc cmp);
00337     static void handleOutOfMemory();
00338 
00340     static T* ArrayAllocate(size_t len);
00342     static void ArrayConstruct(T* arrayBegin, size_t len, const T& defaultVal);
00344     static void ArrayDeAllocate(T* arrayBegin);
00346     static void ArrayDestruct(T* arrayBegin, size_t len);
00349     static void ArrayCopy(T* pCopy, const T * pSource, size_t nCount);
00351     static void ArrayCopyOverlap(T* pCopy, const T * pSource, size_t nCount);
00354     static void ArrayCopyConstruct(T* pCopy, const T * pSource, size_t nCount);
00355 };
00356 
00357 #include "Array.inline.h"
00358 #include "Array.imp.h"
00359 
00360 } // namespace MaxSDK
00361