PixelLightAPI  .
Array.h
Go to the documentation of this file.
00001 /*********************************************************\
00002  *  File: Array.h                                        *
00003  *
00004  *  Copyright (C) 2002-2011 The PixelLight Team (http://www.pixellight.org/)
00005  *
00006  *  This file is part of PixelLight.
00007  *
00008  *  PixelLight is free software: you can redistribute it and/or modify
00009  *  it under the terms of the GNU Lesser General Public License as published by
00010  *  the Free Software Foundation, either version 3 of the License, or
00011  *  (at your option) any later version.
00012  *
00013  *  PixelLight is distributed in the hope that it will be useful,
00014  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
00015  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
00016  *  GNU Lesser General Public License for more details.
00017  *
00018  *  You should have received a copy of the GNU Lesser General Public License
00019  *  along with PixelLight. If not, see <http://www.gnu.org/licenses/>.
00020 \*********************************************************/
00021 
00022 
00023 #ifndef __PLCORE_CONTAINER_ARRAY_H__
00024 #define __PLCORE_CONTAINER_ARRAY_H__
00025 #pragma once
00026 
00027 
00028 //[-------------------------------------------------------]
00029 //[ Includes                                              ]
00030 //[-------------------------------------------------------]
00031 #include "PLCore/Container/Container.h"
00032 
00033 
00034 //[-------------------------------------------------------]
00035 //[ Namespace                                             ]
00036 //[-------------------------------------------------------]
00037 namespace PLCore {
00038 
00039 
00040 //[-------------------------------------------------------]
00041 //[ Classes                                               ]
00042 //[-------------------------------------------------------]
00043 /**
00044 *  @brief
00045 *    Array class
00046 *
00047 *  @remarks
00048 *    Within the array, all elements will be arranged sequentially in memory. The array is
00049 *    resized automatically if required, in this case a new array is created internally,
00050 *    old data is copied and the old array is destroyed. Therefore, avoid array resizing
00051 *    if you have a lot of big elements. Using 'GetData()' you can receive a pointer to the
00052 *    first element, now you can iterate through the array using 'GetData()+1' etc. for more
00053 *    performance.
00054 *
00055 *  @note
00056 *    - Do NOT backup pointer to array elements, if the array is resized they will become
00057 *      invalid
00058 */
00059 template <class ValueType>
00060 class PLCORE_TMPL Array : public Container<ValueType> {
00061 
00062 
00063     //[-------------------------------------------------------]
00064     //[ Public functions                                      ]
00065     //[-------------------------------------------------------]
00066     public:
00067         /**
00068         *  @brief
00069         *    Constructor
00070         *
00071         *  @param[in] nMaxNumOfElements
00072         *    Maximum number of elements within the array
00073         *  @param[in] bAdded
00074         *    Are all elements added? (GetNumOfElements() = GetMaxNumOfElements())
00075         *  @param[in] bInit
00076         *    Initialize new elements by setting them to null?
00077         *
00078         *  @see
00079         *    - Resize()
00080         */
00081         Array(uint32 nMaxNumOfElements = 0, bool bAdded = true, bool bInit = false);
00082 
00083         /**
00084         *  @brief
00085         *    Copy constructor
00086         *
00087         *  @param[in] lstSource
00088         *    Array to copy from
00089         *  @param[in] nStart
00090         *    Index the copy operation should start
00091         *  @param[in] nCount
00092         *    Number of elements to copy, if 0 copy all elements of lstSource behind nStart
00093         */
00094         Array(const Array<ValueType> &lstSource, uint32 nStart = 0, uint32 nCount = 0);
00095 
00096         /**
00097         *  @brief
00098         *    Copy constructor
00099         *
00100         *  @param[in] lstSource
00101         *    Container to copy from
00102         *  @param[in] nStart
00103         *    Index the copy operation should start
00104         *  @param[in] nCount
00105         *    Number of elements to copy, if 0 copy all elements of lstSource behind nStart
00106         */
00107         Array(const Container<ValueType> &lstSource, uint32 nStart = 0, uint32 nCount = 0);
00108 
00109         /**
00110         *  @brief
00111         *    Destructor
00112         */
00113         virtual ~Array();
00114 
00115         /**
00116         *  @brief
00117         *    Copy operator
00118         *
00119         *  @param[in] lstSource
00120         *    Array to copy from
00121         *
00122         *  @return
00123         *    Reference to this instance
00124         */
00125         Container<ValueType> &operator =(const Array<ValueType> &lstSource);
00126 
00127         /**
00128         *  @brief
00129         *    Returns the maximum number of elements in the array
00130         *
00131         *  @return
00132         *    Maximum number of element in the array
00133         */
00134         uint32 GetMaxNumOfElements() const;
00135 
00136         /**
00137         *  @brief
00138         *    Sets the maximum number of elements in the array
00139         *
00140         *  @param[in] nMaxNumOfElements
00141         *    New maximum number of elements (0 = clear array)
00142         *  @param[in] bAdded
00143         *    Are all elements added? (GetNumOfElements() = GetMaxNumOfElements())
00144         *  @param[in] bInit
00145         *    Initialize new elements by setting them to null?
00146         *
00147         *  @return
00148         *    'true' if all went fine, else 'false'
00149         *
00150         *  @note
00151         *    - In arrays with direct objects like Array<MyClass> which have some internal
00152         *      classes and especial virtual functions it's NOT recommended to set 'bInit' to true!
00153         */
00154         bool Resize(uint32 nMaxNumOfElements, bool bAdded = true, bool bInit = false);
00155 
00156         /**
00157         *  @brief
00158         *    Returns the number of elements automatically added if the array
00159         *    size is to small
00160         *
00161         *  @return
00162         *    Number of elements automatically added if the array size is to small
00163         */
00164         uint32 GetResizeCount() const;
00165 
00166         /**
00167         *  @brief
00168         *    Sets the number of elements automatically added if the array
00169         *    size is to small
00170         *
00171         *  @param[in] nCount
00172         *    Number of elements automatically added if the array size is to small
00173         *
00174         *  @return
00175         *    'true' if all went fine, else 'false'
00176         *
00177         *  @note
00178         *    - If nCount is 0, the array size isn't changed automatically
00179         */
00180         bool SetResizeCount(uint32 nCount = 10);
00181 
00182         /**
00183         *  @brief
00184         *    Returns the array data
00185         *
00186         *  @return
00187         *    The array data, can be a null pointer
00188         *
00189         *  @note
00190         *    - Do NOT delete this data by yourself!
00191         *    - Do NOT mess up the memory by writing outside the given buffer...
00192         */
00193         ValueType *GetData() const;
00194 
00195         /**
00196         *  @brief
00197         *    Resets the array
00198         *
00199         *  @remarks
00200         *    While the Clear() function destroys also the data, this function will only
00201         *    reset the current number of elements within the array to 0.
00202         */
00203         void Reset();
00204 
00205 
00206     //[-------------------------------------------------------]
00207     //[ Private data                                          ]
00208     //[-------------------------------------------------------]
00209     private:
00210         uint32     m_nMaxNumOfElements; /**< Maximum number of elements */
00211         uint32     m_nNumOfElements;    /**< Current number of elements */
00212         ValueType *m_pData;             /**< Elements, can be a null pointer */
00213         uint32     m_nResizeCount;      /**< Automatic resize count */
00214 
00215 
00216     //[-------------------------------------------------------]
00217     //[ Public virtual Iterable functions                     ]
00218     //[-------------------------------------------------------]
00219     public:
00220         virtual Iterator<ValueType> GetIterator(uint32 nIndex = 0) const override;
00221         virtual ConstIterator<ValueType> GetConstIterator(uint32 nIndex = 0) const override;
00222         virtual Iterator<ValueType> GetEndIterator() const override;
00223         virtual ConstIterator<ValueType> GetConstEndIterator() const override;
00224 
00225 
00226     //[-------------------------------------------------------]
00227     //[ Public virtual Container functions                    ]
00228     //[-------------------------------------------------------]
00229     public:
00230         virtual bool IsEmpty() const override;
00231         virtual uint32 GetNumOfElements() const override;
00232         virtual uint32 GetElementSize() const override;
00233         virtual uint32 GetSize() const override;
00234         virtual void Clear() override;
00235         virtual bool IsElement(const ValueType &Element) const override;
00236         virtual int GetIndex(const ValueType &Element) const override;
00237         virtual ValueType &Get(uint32 nIndex) const override;
00238         virtual ValueType &operator [](uint32 nIndex) const override;
00239         virtual bool Replace(const ValueType &Element1, const ValueType &Element2) override;
00240         virtual bool ReplaceAtIndex(uint32 nIndex, const ValueType &Element) override;
00241         virtual ValueType &Add() override;
00242         virtual ValueType &Add(const ValueType &Element) override;
00243         virtual uint32 Add(const ValueType *pElements, uint32 nCount) override;
00244         virtual Container<ValueType> &operator +=(const ValueType &Element) override;
00245         virtual bool Add(const Container<ValueType> &lstContainer, uint32 nStart = 0, uint32 nCount = 0) override;
00246         virtual Container<ValueType> &operator +=(const Container<ValueType> &lstContainer) override;
00247         virtual ValueType &AddAtIndex(int nIndex) override;
00248         virtual ValueType &AddAtIndex(const ValueType &Element, int nIndex) override;
00249         virtual bool Remove(const ValueType &Element) override;
00250         virtual bool RemoveAtIndex(uint32 nElement) override;
00251         virtual Container<ValueType> &operator -=(const ValueType &Element) override;
00252         virtual bool Remove(const Container<ValueType> &lstContainer, uint32 nStart = 0, uint32 nCount = 0) override;
00253         virtual Container<ValueType> &operator -=(const Container<ValueType> &lstContainer) override;
00254         virtual bool Copy(const Container<ValueType> &lstContainer, uint32 nStart = 0, uint32 nCount = 0) override;
00255         virtual Container<ValueType> &operator =(const Container<ValueType> &lstContainer) override;
00256         virtual bool Compare(const Container<ValueType> &lstContainer, uint32 nStart = 0, uint32 nCount = 0) const override;
00257         virtual bool operator ==(const Container<ValueType> &lstContainer) const override;
00258         virtual bool operator !=(const Container<ValueType> &lstContainer) const override;
00259 
00260 
00261 };
00262 
00263 
00264 //[-------------------------------------------------------]
00265 //[ Namespace                                             ]
00266 //[-------------------------------------------------------]
00267 } // PLCore
00268 
00269 
00270 //[-------------------------------------------------------]
00271 //[ Implementation                                        ]
00272 //[-------------------------------------------------------]
00273 #include "PLCore/Container/Array.inl"
00274 
00275 
00276 #endif // __PLCORE_CONTAINER_ARRAY_H__


PixelLight PixelLight 0.9.10-R1
Copyright (C) 2002-2011 by The PixelLight Team
Last modified Fri Dec 23 2011 15:50:51
The content of this PixelLight document is published under the
Creative Commons Attribution-NonCommercial-ShareAlike 3.0 Unported