PixelLightAPI  .
Pool.h
Go to the documentation of this file.
00001 /*********************************************************\
00002  *  File: Pool.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_POOL_H__
00024 #define __PLCORE_CONTAINER_POOL_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 PoolIterator;
00044 
00045 
00046 //[-------------------------------------------------------]
00047 //[ Classes                                               ]
00048 //[-------------------------------------------------------]
00049 /**
00050 *  @brief
00051 *    Pool class
00052 *
00053 *  @remarks
00054 *    This pool container is very similar to the list container. Within the double linked
00055 *    list each node knows it's previous and next element. If you want to receive element x,
00056 *    internally the process begins with the first element and goes to the next until
00057 *    element x was found. As you see the following example isn't that performant:\n
00058 *  @verbatim
00059 *    for (uint32 i=0; i<lstList.GetNumOfElements(); i++) {
00060 *        PoolElement &cElement = lstList[i];
00061 *    }
00062 *  @endverbatim
00063 *    Instead use the provided iterator whenever possible:\
00064 *  @verbatim
00065 *    Iterator<PoolElement*> cIterator = m_lstPool.GetIterator();
00066 *    while (cIterator.HasNext()) {
00067 *        PoolElement *pElement = cIterator.Next();
00068 *    }
00069 *  @endverbatim
00070 *   But unlike the list container, this pool container has a second internal list holding
00071 *   currently unused elements. If a new element is added, a free element is chosen - only
00072 *   if there is no free element a new one is created. If a element is removed, it's pushed
00073 *   back to the list of free elements. So, this container needs more memory, but if elements
00074 *   are added and removed frequently, this container is more performant as the standard list.\n
00075 *   If your pool has to manage A LOT OF frequently added and removed elements, you should use
00076 *   FastPool - it's remove function is faster because the pool element to remove must not
00077 *   be searched first. But if you use this faster pool, you are forced to derive your elements
00078 *   from the class FastPoolElement.
00079 */
00080 template <class ValueType>
00081 class Pool : public Container<ValueType> {
00082 
00083 
00084     //[-------------------------------------------------------]
00085     //[ Friends                                               ]
00086     //[-------------------------------------------------------]
00087     friend class PoolIterator<ValueType>;
00088 
00089 
00090     //[-------------------------------------------------------]
00091     //[ Public functions                                      ]
00092     //[-------------------------------------------------------]
00093     public:
00094         /**
00095         *  @brief
00096         *    Constructor
00097         */
00098         Pool();
00099 
00100         /**
00101         *  @brief
00102         *    Copy constructor
00103         *
00104         *  @param[in] lstSource
00105         *    Pool to copy from
00106         *  @param[in] nStart
00107         *    Index the copy operation should start
00108         *  @param[in] nCount
00109         *    Number of elements to copy, if 0 copy all elements of lstSource behind nStart
00110         */
00111         Pool(const Pool<ValueType> &lstSource, uint32 nStart = 0, uint32 nCount = 0);
00112 
00113         /**
00114         *  @brief
00115         *    Copy constructor
00116         *
00117         *  @param[in] lstSource
00118         *    Container to copy from
00119         *  @param[in] nStart
00120         *    Index the copy operation should start
00121         *  @param[in] nCount
00122         *    Number of elements to copy, if 0 copy all elements of lstSource behind nStart
00123         */
00124         Pool(const Container<ValueType> &lstSource, uint32 nStart = 0, uint32 nCount = 0);
00125 
00126         /**
00127         *  @brief
00128         *    Destructor
00129         */
00130         virtual ~Pool();
00131 
00132         /**
00133         *  @brief
00134         *    Copy operator
00135         *
00136         *  @param[in] lstSource
00137         *    Pool to copy from
00138         *
00139         *  @return
00140         *    Reference to this instance
00141         */
00142         Container<ValueType> &operator =(const Pool<ValueType> &lstSource);
00143 
00144         /**
00145         *  @brief
00146         *    Returns the number of currently free pool elements
00147         *
00148         *  @return
00149         *    Number of currently free pool elements
00150         */
00151         uint32 GetNumOfFreeElements() const;
00152 
00153         /**
00154         *  @brief
00155         *    Returns the total size of all free container elements (in bytes)
00156         *
00157         *  @return
00158         *    Total size of all free container elements (in bytes)
00159         */
00160         uint32 GetFreeSize() const;
00161 
00162         /**
00163         *  @brief
00164         *    Marks all elements as free
00165         *
00166         *  @note
00167         *    - Quite fast
00168         */
00169         void FreeElements();
00170 
00171         /**
00172         *  @brief
00173         *    Removes all currently free elements
00174         */
00175         void RemoveAllFreeElements();
00176 
00177 
00178     //[-------------------------------------------------------]
00179     //[ Private structures                                    ]
00180     //[-------------------------------------------------------]
00181     private:
00182         /**
00183         *  @brief
00184         *    Internal linked pool element
00185         */
00186         struct PoolElement {
00187             PoolElement *pNextElement;      /**< Pointer to the next element in the pool, can be a null pointer */
00188             PoolElement *pPreviousElement;  /**< Pointer to the previous element in the pool, can be a null pointer */
00189             ValueType    Data;              /**< The stored data */
00190         };
00191 
00192 
00193     //[-------------------------------------------------------]
00194     //[ Private functions                                     ]
00195     //[-------------------------------------------------------]
00196     private:
00197         /**
00198         *  @brief
00199         *    Adds a free element, if there's no free element a new one is created
00200         *
00201         *  @return
00202         *    Added element
00203         */
00204         PoolElement &AddElement();
00205 
00206         /**
00207         *  @brief
00208         *    Removes an element (added to the list of free elements)
00209         *
00210         *  @param[in] cElement
00211         *    Pool element to remove
00212         */
00213         void RemoveElement(PoolElement &cElement);
00214 
00215 
00216     //[-------------------------------------------------------]
00217     //[ Private data                                          ]
00218     //[-------------------------------------------------------]
00219     private:
00220         // Standard list data
00221         uint32       m_nNumOfElements;      /**< Current number of elements */
00222         PoolElement *m_pFirstElement;       /**< Pointer to first pool element, can be a null pointer */
00223         PoolElement *m_pLastElement;        /**< Pointer to last pool element, can be a null pointer */
00224         // Additional pool data
00225         uint32       m_nNumOfFreeElements;  /**< Current number of free elements */
00226         PoolElement *m_pFirstFreeElement;   /**< Pointer to first free pool element, can be a null pointer */
00227 
00228 
00229     //[-------------------------------------------------------]
00230     //[ Public virtual Iterable functions                     ]
00231     //[-------------------------------------------------------]
00232     public:
00233         virtual Iterator<ValueType> GetIterator(uint32 nIndex = 0) const override;
00234         virtual ConstIterator<ValueType> GetConstIterator(uint32 nIndex = 0) const override;
00235         virtual Iterator<ValueType> GetEndIterator() const override;
00236         virtual ConstIterator<ValueType> GetConstEndIterator() const override;
00237 
00238 
00239     //[-------------------------------------------------------]
00240     //[ Public virtual Container functions                    ]
00241     //[-------------------------------------------------------]
00242     public:
00243         virtual bool IsEmpty() const override;
00244         virtual uint32 GetNumOfElements() const override;
00245         virtual uint32 GetElementSize() const override;
00246         virtual uint32 GetSize() const override;
00247         virtual void Clear() override;
00248         virtual bool IsElement(const ValueType &Element) const override;
00249         virtual int GetIndex(const ValueType &Element) const override;
00250         virtual ValueType &Get(uint32 nIndex) const override;
00251         virtual ValueType &operator [](uint32 nIndex) const override;
00252         virtual bool Replace(const ValueType &Element1, const ValueType &Element2) override;
00253         virtual bool ReplaceAtIndex(uint32 nIndex, const ValueType &Element) override;
00254         virtual ValueType &Add() override;
00255         virtual ValueType &Add(const ValueType &Element) override;
00256         virtual uint32 Add(const ValueType *pElements, uint32 nCount) override;
00257         virtual Container<ValueType> &operator +=(const ValueType &Element) override;
00258         virtual bool Add(const Container<ValueType> &lstContainer, uint32 nStart = 0, uint32 nCount = 0) override;
00259         virtual Container<ValueType> &operator +=(const Container<ValueType> &lstContainer) override;
00260         virtual ValueType &AddAtIndex(int nIndex) override;
00261         virtual ValueType &AddAtIndex(const ValueType &Element, int nIndex) override;
00262         virtual bool Remove(const ValueType &Element) override;
00263         virtual bool RemoveAtIndex(uint32 nElement) override;
00264         virtual Container<ValueType> &operator -=(const ValueType &Element) override;
00265         virtual bool Remove(const Container<ValueType> &lstContainer, uint32 nStart = 0, uint32 nCount = 0) override;
00266         virtual Container<ValueType> &operator -=(const Container<ValueType> &lstContainer) override;
00267         virtual bool Copy(const Container<ValueType> &lstContainer, uint32 nStart = 0, uint32 nCount = 0) override;
00268         virtual Container<ValueType> &operator =(const Container<ValueType> &lstContainer) override;
00269         virtual bool Compare(const Container<ValueType> &lstContainer, uint32 nStart = 0, uint32 nCount = 0) const override;
00270         virtual bool operator ==(const Container<ValueType> &lstContainer) const override;
00271         virtual bool operator !=(const Container<ValueType> &lstContainer) const override;
00272 
00273 
00274 };
00275 
00276 
00277 //[-------------------------------------------------------]
00278 //[ Namespace                                             ]
00279 //[-------------------------------------------------------]
00280 } // PLCore
00281 
00282 
00283 //[-------------------------------------------------------]
00284 //[ Implementation                                        ]
00285 //[-------------------------------------------------------]
00286 #include "PLCore/Container/Pool.inl"
00287 
00288 
00289 #endif // __PLCORE_CONTAINER_POOL_H__


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