PixelLightAPI  .
Array.h
Go to the documentation of this file.
00001 /*********************************************************\
00002  *  File: Array.h                                        *
00003  *
00004  *  Copyright (C) 2002-2012 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         *  @brief
00207         *    Moves an element within the array
00208         *
00209         *  @param[in] nFromIndex
00210         *    The index of the element which should be moved
00211         *  @param[in] nToIndex
00212         *    The index to which the element should be moved
00213         *
00214         *  @note
00215         *    - This methods assumes, that both index values are within 0 and GetNumOfElements()-1
00216         */
00217         void MoveElement(uint32 nFromIndex, uint32 nToIndex);
00218 
00219 
00220     //[-------------------------------------------------------]
00221     //[ Private data                                          ]
00222     //[-------------------------------------------------------]
00223     private:
00224         uint32     m_nMaxNumOfElements; /**< Maximum number of elements */
00225         uint32     m_nNumOfElements;    /**< Current number of elements */
00226         ValueType *m_pData;             /**< Elements, can be a null pointer */
00227         uint32     m_nResizeCount;      /**< Automatic resize count */
00228 
00229 
00230     //[-------------------------------------------------------]
00231     //[ Public virtual Iterable functions                     ]
00232     //[-------------------------------------------------------]
00233     public:
00234         virtual Iterator<ValueType> GetIterator(uint32 nIndex = 0) const override;
00235         virtual ConstIterator<ValueType> GetConstIterator(uint32 nIndex = 0) const override;
00236         virtual Iterator<ValueType> GetEndIterator() const override;
00237         virtual ConstIterator<ValueType> GetConstEndIterator() const override;
00238 
00239 
00240     //[-------------------------------------------------------]
00241     //[ Public virtual Container functions                    ]
00242     //[-------------------------------------------------------]
00243     public:
00244         virtual bool IsEmpty() const override;
00245         virtual uint32 GetNumOfElements() const override;
00246         virtual uint32 GetElementSize() const override;
00247         virtual uint32 GetSize() const override;
00248         virtual void Clear() override;
00249         virtual bool IsElement(const ValueType &Element) const override;
00250         virtual int GetIndex(const ValueType &Element) const override;
00251         virtual ValueType &Get(uint32 nIndex) const override;
00252         virtual ValueType &operator [](uint32 nIndex) const override;
00253         virtual bool Replace(const ValueType &Element1, const ValueType &Element2) override;
00254         virtual bool ReplaceAtIndex(uint32 nIndex, const ValueType &Element) override;
00255         virtual ValueType &Add() override;
00256         virtual ValueType &Add(const ValueType &Element) override;
00257         virtual uint32 Add(const ValueType *pElements, uint32 nCount) override;
00258         virtual Container<ValueType> &operator +=(const ValueType &Element) override;
00259         virtual bool Add(const Container<ValueType> &lstContainer, uint32 nStart = 0, uint32 nCount = 0) override;
00260         virtual Container<ValueType> &operator +=(const Container<ValueType> &lstContainer) override;
00261         virtual ValueType &AddAtIndex(int nIndex) override;
00262         virtual ValueType &AddAtIndex(const ValueType &Element, int nIndex) override;
00263         virtual bool Remove(const ValueType &Element) override;
00264         virtual bool RemoveAtIndex(uint32 nElement) override;
00265         virtual Container<ValueType> &operator -=(const ValueType &Element) override;
00266         virtual bool Remove(const Container<ValueType> &lstContainer, uint32 nStart = 0, uint32 nCount = 0) override;
00267         virtual Container<ValueType> &operator -=(const Container<ValueType> &lstContainer) override;
00268         virtual bool Copy(const Container<ValueType> &lstContainer, uint32 nStart = 0, uint32 nCount = 0) override;
00269         virtual Container<ValueType> &operator =(const Container<ValueType> &lstContainer) override;
00270         virtual bool Compare(const Container<ValueType> &lstContainer, uint32 nStart = 0, uint32 nCount = 0) const override;
00271         virtual bool operator ==(const Container<ValueType> &lstContainer) const override;
00272         virtual bool operator !=(const Container<ValueType> &lstContainer) const override;
00273 
00274 
00275 };
00276 
00277 
00278 //[-------------------------------------------------------]
00279 //[ Namespace                                             ]
00280 //[-------------------------------------------------------]
00281 } // PLCore
00282 
00283 
00284 //[-------------------------------------------------------]
00285 //[ Implementation                                        ]
00286 //[-------------------------------------------------------]
00287 #include "PLCore/Container/Array.inl"
00288 
00289 
00290 #endif // __PLCORE_CONTAINER_ARRAY_H__


PixelLight PixelLight 0.9.11-R1
Copyright (C) 2002-2012 by The PixelLight Team
Last modified Thu Feb 23 2012 14:08:52
The content of this PixelLight document is published under the
Creative Commons Attribution-NonCommercial-ShareAlike 3.0 Unported