PixelLightAPI  .
FastPool.h
Go to the documentation of this file.
00001 /*********************************************************\
00002  *  File: FastPool.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_FASTPOOL_H__
00024 #define __PLCORE_CONTAINER_FASTPOOL_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 //[ Forward declarations                                  ]
00042 //[-------------------------------------------------------]
00043 template<class ValueType> class FastPool;
00044 template<class ValueType> class FastPoolIterator;
00045 
00046 
00047 //[-------------------------------------------------------]
00048 //[ Classes                                               ]
00049 //[-------------------------------------------------------]
00050 /**
00051 *  @brief
00052 *    Linked fast pool element you have to derive your elements from
00053 */
00054 template <class ValueType>
00055 class FastPoolElement {
00056 
00057 
00058     //[-------------------------------------------------------]
00059     //[ Friends                                               ]
00060     //[-------------------------------------------------------]
00061     friend class FastPool<ValueType>;
00062     friend class FastPoolIterator<ValueType>;
00063 
00064 
00065     //[-------------------------------------------------------]
00066     //[ Public functions                                      ]
00067     //[-------------------------------------------------------]
00068     public:
00069         /**
00070         *  @brief
00071         *    Constructor
00072         */
00073         FastPoolElement();
00074 
00075         /**
00076         *  @brief
00077         *    Destructor
00078         */
00079         virtual ~FastPoolElement();
00080 
00081         /**
00082         *  @brief
00083         *    Copy operator
00084         *
00085         *  @param[in] lstSource
00086         *    Fast pool element to copy from
00087         *
00088         *  @return
00089         *    Reference to this instance
00090         */
00091         virtual FastPoolElement<ValueType> &operator =(const FastPoolElement<ValueType> &lstSource);
00092 
00093 
00094     //[-------------------------------------------------------]
00095     //[ Private data                                          ]
00096     //[-------------------------------------------------------]
00097     private:
00098         ValueType *m_pNextElement;      /**< Pointer to the next element in the pool, can be a null pointer */
00099         ValueType *m_pPreviousElement;  /**< Pointer to the previous element in the pool, can be a null pointer */
00100 
00101 
00102 };
00103 
00104 /**
00105 *  @brief
00106 *    Fast pool class
00107 *
00108 *  @note
00109 *    - Your elements MUST be derived from FastPoolElement
00110 *    - Use RemoveElement() to remove an element in a quite fast way
00111 *    - Do NOT use your element within more than one fast pool at once, if this is required,
00112 *      use the pool class instead
00113 *
00114 *  @see
00115 *    - Pool class
00116 */
00117 template <class ValueType>
00118 class FastPool : public Container<ValueType> {
00119 
00120 
00121     //[-------------------------------------------------------]
00122     //[ Friends                                               ]
00123     //[-------------------------------------------------------]
00124     friend class FastPoolIterator<ValueType>;
00125 
00126 
00127     //[-------------------------------------------------------]
00128     //[ Public functions                                      ]
00129     //[-------------------------------------------------------]
00130     public:
00131         /**
00132         *  @brief
00133         *    Constructor
00134         */
00135         FastPool();
00136 
00137         /**
00138         *  @brief
00139         *    Copy constructor
00140         *
00141         *  @param[in] lstSource
00142         *    Fast pool to copy from
00143         *  @param[in] nStart
00144         *    Index the copy operation should start
00145         *  @param[in] nCount
00146         *    Number of elements to copy, if 0 copy all elements of lstSource behind nStart
00147         */
00148         FastPool(const FastPool<ValueType> &lstSource, uint32 nStart = 0, uint32 nCount = 0);
00149 
00150         /**
00151         *  @brief
00152         *    Copy constructor
00153         *
00154         *  @param[in] lstSource
00155         *    Container to copy from
00156         *  @param[in] nStart
00157         *    Index the copy operation should start
00158         *  @param[in] nCount
00159         *    Number of elements to copy, if 0 copy all elements of lstSource behind nStart
00160         */
00161         FastPool(const Container<ValueType> &lstSource, uint32 nStart = 0, uint32 nCount = 0);
00162 
00163         /**
00164         *  @brief
00165         *    Destructor
00166         */
00167         virtual ~FastPool();
00168 
00169         /**
00170         *  @brief
00171         *    Copy operator
00172         *
00173         *  @param[in] lstSource
00174         *    Fast pool to copy from
00175         *
00176         *  @return
00177         *    Reference to this instance
00178         */
00179         Container<ValueType> &operator =(const FastPool<ValueType> &lstSource);
00180 
00181         /**
00182         *  @brief
00183         *    Returns the number of currently free pool elements
00184         *
00185         *  @return
00186         *    Number of currently free pool elements
00187         */
00188         uint32 GetNumOfFreeElements() const;
00189 
00190         /**
00191         *  @brief
00192         *    Returns the total size of all free container elements (in bytes)
00193         *
00194         *  @return
00195         *    Total size of all free container elements (in bytes)
00196         */
00197         uint32 GetFreeSize() const;
00198 
00199         /**
00200         *  @brief
00201         *    Marks all elements as free
00202         *
00203         *  @note
00204         *    - Quite fast
00205         */
00206         void FreeElements();
00207 
00208         /**
00209         *  @brief
00210         *    Removes all currently free elements
00211         */
00212         void RemoveAllFreeElements();
00213 
00214         /**
00215         *  @brief
00216         *    Removes an element (added to the list of free elements)
00217         *
00218         *  @param[in] cElement
00219         *    Element to remove, MUST be an element from THIS pool!
00220         *
00221         *  @note
00222         *    - If you have the reference to your element, use this function to remove
00223         *      it because it's quite faster than the default Remove() function
00224         */
00225         void RemoveElement(ValueType &cElement);
00226 
00227         /**
00228         *  @brief
00229         *    Returns the index of an element
00230         *
00231         *  @param[in] cElement
00232         *    Element to get the index from
00233         *
00234         *  @return
00235         *    Index of the given element, < 0 if it's not in the container
00236         */
00237         int GetElementIndex(ValueType &cElement) const;
00238 
00239 
00240     //[-------------------------------------------------------]
00241     //[ Private functions                                     ]
00242     //[-------------------------------------------------------]
00243     private:
00244         /**
00245         *  @brief
00246         *    Adds a free element, if there's no free element a new one is created
00247         *
00248         *  @return
00249         *    Added element
00250         */
00251         ValueType &AddElement();
00252 
00253 
00254     //[-------------------------------------------------------]
00255     //[ Private data                                          ]
00256     //[-------------------------------------------------------]
00257     private:
00258         // Standard list data
00259         uint32     m_nNumOfElements;        /**< Current number of elements */
00260         ValueType *m_pFirstElement;         /**< Pointer to first pool element, can be a null pointer */
00261         ValueType *m_pLastElement;          /**< Pointer to last pool element, can be a null pointer */
00262         // Additional pool data
00263         uint32     m_nNumOfFreeElements;    /**< Current number of free elements */
00264         ValueType *m_pFirstFreeElement;     /**< Pointer to first free pool element, can be a null pointer */
00265 
00266 
00267     //[-------------------------------------------------------]
00268     //[ Public virtual Iterable functions                     ]
00269     //[-------------------------------------------------------]
00270     public:
00271         virtual Iterator<ValueType> GetIterator(uint32 nIndex = 0) const override;
00272         virtual ConstIterator<ValueType> GetConstIterator(uint32 nIndex = 0) const override;
00273         virtual Iterator<ValueType> GetEndIterator() const override;
00274         virtual ConstIterator<ValueType> GetConstEndIterator() const override;
00275 
00276 
00277     //[-------------------------------------------------------]
00278     //[ Public virtual Container functions                    ]
00279     //[-------------------------------------------------------]
00280     public:
00281         virtual bool IsEmpty() const override;
00282         virtual uint32 GetNumOfElements() const override;
00283         virtual uint32 GetElementSize() const override;
00284         virtual uint32 GetSize() const override;
00285         virtual void Clear() override;
00286         virtual bool IsElement(const ValueType &Element) const override;
00287         virtual int GetIndex(const ValueType &Element) const override;
00288         virtual ValueType &Get(uint32 nIndex) const override;
00289         virtual ValueType &operator [](uint32 nIndex) const override;
00290         virtual bool Replace(const ValueType &Element1, const ValueType &Element2) override;
00291         virtual bool ReplaceAtIndex(uint32 nIndex, const ValueType &Element) override;
00292         virtual ValueType &Add() override;
00293         virtual ValueType &Add(const ValueType &Element) override;
00294         virtual uint32 Add(const ValueType *pElements, uint32 nCount) override;
00295         virtual Container<ValueType> &operator +=(const ValueType &Element) override;
00296         virtual bool Add(const Container<ValueType> &lstContainer, uint32 nStart = 0, uint32 nCount = 0) override;
00297         virtual Container<ValueType> &operator +=(const Container<ValueType> &lstContainer) override;
00298         virtual ValueType &AddAtIndex(int nIndex) override;
00299         virtual ValueType &AddAtIndex(const ValueType &Element, int nIndex) override;
00300         virtual bool Remove(const ValueType &Element) override;
00301         virtual bool RemoveAtIndex(uint32 nElement) override;
00302         virtual Container<ValueType> &operator -=(const ValueType &Element) override;
00303         virtual bool Remove(const Container<ValueType> &lstContainer, uint32 nStart = 0, uint32 nCount = 0) override;
00304         virtual Container<ValueType> &operator -=(const Container<ValueType> &lstContainer) override;
00305         virtual bool Copy(const Container<ValueType> &lstContainer, uint32 nStart = 0, uint32 nCount = 0) override;
00306         virtual Container<ValueType> &operator =(const Container<ValueType> &lstContainer) override;
00307         virtual bool Compare(const Container<ValueType> &lstContainer, uint32 nStart = 0, uint32 nCount = 0) const override;
00308         virtual bool operator ==(const Container<ValueType> &lstContainer) const override;
00309         virtual bool operator !=(const Container<ValueType> &lstContainer) const override;
00310 
00311 
00312 };
00313 
00314 
00315 //[-------------------------------------------------------]
00316 //[ Namespace                                             ]
00317 //[-------------------------------------------------------]
00318 } // PLCore
00319 
00320 
00321 //[-------------------------------------------------------]
00322 //[ Implementation                                        ]
00323 //[-------------------------------------------------------]
00324 #include "PLCore/Container/FastPool.inl"
00325 
00326 
00327 #endif // __PLCORE_CONTAINER_FASTPOOL_H__


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