PixelLightAPI  .
Bitset.h
Go to the documentation of this file.
00001 /*********************************************************\
00002  *  File: Bitset.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_BITSET_H__
00024 #define __PLCORE_CONTAINER_BITSET_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 *    Bit set class
00046 *
00047 *  @remarks
00048 *    The bit set class which is useful when dealing with many binary states because
00049 *    many bits will be put together in variables to save memory. The bit set is
00050 *    resized automatically if required.
00051 */
00052 class PLCORE_API Bitset : public Container<bool> {
00053 
00054 
00055     //[-------------------------------------------------------]
00056     //[ Public functions                                      ]
00057     //[-------------------------------------------------------]
00058     public:
00059         /**
00060         *  @brief
00061         *    Constructor
00062         *
00063         *  @param[in] nMaxNumOfElements
00064         *    Maximum number of elements within the bit set
00065         *  @param[in] bAdded
00066         *    Are all elements added? (GetNumOfElements() = GetMaxNumOfElements())
00067         *  @param[in] bInit
00068         *    Initialize new elements by setting them to null?
00069         */
00070         Bitset(uint32 nMaxNumOfElements = 0, bool bAdded = true, bool bInit = false);
00071 
00072         /**
00073         *  @brief
00074         *    Copy constructor
00075         *
00076         *  @param[in] lstSource
00077         *    Bitset to copy from
00078         *  @param[in] nStart
00079         *    Index the copy operation should start
00080         *  @param[in] nCount
00081         *    Number of elements to copy, if 0 copy all elements of lstSource behind nStart
00082         */
00083         Bitset(const Bitset &lstSource, uint32 nStart = 0, uint32 nCount = 0);
00084 
00085         /**
00086         *  @brief
00087         *    Copy constructor
00088         *
00089         *  @param[in] lstSource
00090         *    Container to copy from
00091         *  @param[in] nStart
00092         *    Index the copy operation should start
00093         *  @param[in] nCount
00094         *    Number of elements to copy, if 0 copy all elements of lstSource behind nStart
00095         */
00096         Bitset(const Container<bool> &lstSource, uint32 nStart = 0, uint32 nCount = 0);
00097 
00098         /**
00099         *  @brief
00100         *    Destructor
00101         */
00102         virtual ~Bitset();
00103 
00104         /**
00105         *  @brief
00106         *    Copy operator
00107         *
00108         *  @param[in] lstSource
00109         *    Bitset to copy from
00110         *
00111         *  @return
00112         *    Reference to this instance
00113         */
00114         inline Container<bool> &operator =(const Bitset &lstSource);
00115 
00116         /**
00117         *  @brief
00118         *    Returns the maximum number of elements in the bit set
00119         *
00120         *  @return
00121         *    Maximum number of element in the bit set
00122         */
00123         inline uint32 GetMaxNumOfElements() const;
00124 
00125         /**
00126         *  @brief
00127         *    Sets the maximum number of elements in the bit set
00128         *
00129         *  @param[in] nMaxNumOfElements
00130         *    New maximum number of elements (0 = clear bit set)
00131         *  @param[in] bAdded
00132         *    Are all elements added? (GetNumOfElements() = GetMaxNumOfElements())
00133         *  @param[in] bInit
00134         *    Initialize new elements by setting them to null?
00135         *
00136         *  @return
00137         *    'true' if all went fine, else 'false'
00138         */
00139         bool Resize(uint32 nMaxNumOfElements, bool bAdded = true, bool bInit = false);
00140 
00141         /**
00142         *  @brief
00143         *    Returns the number of elements automatically added if the bit set
00144         *    size is to small
00145         *
00146         *  @return
00147         *    Number of elements automatically added if the bit set size is to small
00148         */
00149         inline uint32 GetResizeCount() const;
00150 
00151         /**
00152         *  @brief
00153         *    Sets the number of elements automatically added if the bit set
00154         *    size is to small
00155         *
00156         *  @param[in] nCount
00157         *    Number of elements automatically added if the bit set size is to small
00158         *
00159         *  @return
00160         *    'true' if all went fine, else 'false'
00161         *
00162         *  @note
00163         *    - If nCount is 0, the array size isn't changed automatically
00164         */
00165         inline bool SetResizeCount(uint32 nCount = 64);
00166 
00167         /**
00168         *  @brief
00169         *    Clears a bit to 0
00170         *
00171         *  @param[in] nIndex
00172         *    Index of the bit to clear
00173         *
00174         *  @return
00175         *    'true' if all went fine, else 'false'
00176         */
00177         inline bool Clear(uint32 nIndex);
00178 
00179         /**
00180         *  @brief
00181         *    Clears all bits to 0
00182         */
00183         void ClearAll();
00184 
00185         /**
00186         *  @brief
00187         *    Sets the desired bit
00188         *
00189         *  @param[in] nIndex
00190         *    Index of the bit to set
00191         *
00192         *  @return
00193         *    'true' if all went fine, else 'false'
00194         */
00195         inline bool Set(uint32 nIndex);
00196 
00197         /**
00198         *  @brief
00199         *    Sets all bits to 1
00200         */
00201         void SetAll();
00202 
00203         /**
00204         *  @brief
00205         *    Inverts the desired bit
00206         *
00207         *  @param[in] nIndex
00208         *    Index of the bit to invert
00209         *
00210         *  @return
00211         *    'true' if all went fine, else 'false'
00212         */
00213         inline bool Invert(uint32 nIndex);
00214 
00215         /**
00216         *  @brief
00217         *    Inverts all bits
00218         */
00219         inline void InvertAll();
00220 
00221         /**
00222         *  @brief
00223         *    Returns if the desired bit slot is a 1 or a 0
00224         *
00225         *  @param[in] nIndex
00226         *    Index of the bit to check
00227         *
00228         *  @return
00229         *    'true' if the bit is set, else 'false'
00230         */
00231         inline bool IsSet(uint32 nIndex) const;
00232 
00233         /**
00234         *  @brief
00235         *    Returns the number of set bits (value = 'true')
00236         *
00237         *  @return
00238         *    The number of set bits (value = 'true')
00239         */
00240         uint32 GetNumOfSetBits() const;
00241 
00242         /**
00243         *  @brief
00244         *    Returns the internal number of integers (32 bit) used to store the bits
00245         *
00246         *  @return
00247         *    The internal number of integers used to store the bits
00248         */
00249         inline uint32 GetNumOfIntegers() const;
00250 
00251         /**
00252         *  @brief
00253         *    Returns the internal integers (32 bit) used to store the bits
00254         *
00255         *  @return
00256         *    The internal integers used to store the bits, can be a null pointer, DON'T delete the memory!
00257         */
00258         inline const uint32 *GetIntegers() const;
00259 
00260         /**
00261         *  @brief
00262         *    Resets the bitset
00263         *
00264         *  @remarks
00265         *    While the Clear() function destroys also the data, this function will only
00266         *    reset the current number of elements within the array to 0.
00267         */
00268         inline void Reset();
00269 
00270 
00271     //[-------------------------------------------------------]
00272     //[ Private data                                          ]
00273     //[-------------------------------------------------------]
00274     private:
00275         uint32  m_nMaxNumOfElements;    /**< Maximum number of elements */
00276         uint32  m_nNumOfElements;       /**< Current number of elements */
00277         uint32  m_nNumOfIntegers;       /**< Number of integers */
00278         uint32 *m_pnIntegers;           /**< Bits data, can be a null pointer */
00279         uint32  m_nResizeCount;         /**< Automatic resize count */
00280         // Static data
00281         static bool m_bBit; /**< Last returned bit */
00282 
00283 
00284     //[-------------------------------------------------------]
00285     //[ Public virtual Iterable functions                     ]
00286     //[-------------------------------------------------------]
00287     public:
00288         virtual Iterator<bool> GetIterator(uint32 nIndex = 0) const override;
00289         virtual ConstIterator<bool> GetConstIterator(uint32 nIndex = 0) const override;
00290         virtual Iterator<bool> GetEndIterator() const override;
00291         virtual ConstIterator<bool> GetConstEndIterator() const override;
00292 
00293 
00294     //[-------------------------------------------------------]
00295     //[ Public virtual Container functions                    ]
00296     //[-------------------------------------------------------]
00297     public:
00298         inline virtual bool IsEmpty() const;
00299         inline virtual uint32 GetNumOfElements() const;
00300         inline virtual uint32 GetElementSize() const;
00301         inline virtual uint32 GetSize() const;
00302         virtual void Clear() override;
00303         inline virtual bool IsElement(const bool &Element) const;
00304         inline virtual int GetIndex(const bool &Element) const;
00305         inline virtual bool &Get(uint32 nIndex) const;
00306         inline virtual bool &operator [](uint32 nIndex) const;
00307         inline virtual bool Replace(const bool &Element1, const bool &Element2);
00308         inline virtual bool ReplaceAtIndex(uint32 nIndex, const bool &Element);
00309         virtual bool &Add() override;
00310         virtual bool &Add(const bool &Element) override;
00311         virtual uint32 Add(const bool *pElements, uint32 nCount) override;
00312         inline virtual Container<bool> &operator +=(const bool &Element);
00313         virtual bool Add(const Container<bool> &lstContainer, uint32 nStart = 0, uint32 nCount = 0) override;
00314         inline virtual Container<bool> &operator +=(const Container<bool> &lstContainer);
00315         virtual bool &AddAtIndex(int nIndex) override;
00316         virtual bool &AddAtIndex(const bool &Element, int nIndex) override;
00317         inline virtual bool Remove(const bool &Element);
00318         virtual bool RemoveAtIndex(uint32 nElement) override;
00319         inline virtual Container<bool> &operator -=(const bool &Element);
00320         virtual bool Remove(const Container<bool> &lstContainer, uint32 nStart = 0, uint32 nCount = 0) override;
00321         inline virtual Container<bool> &operator -=(const Container<bool> &lstContainer);
00322         virtual bool Copy(const Container<bool> &lstContainer, uint32 nStart = 0, uint32 nCount = 0) override;
00323         inline virtual Container<bool> &operator =(const Container<bool> &lstContainer);
00324         virtual bool Compare(const Container<bool> &lstContainer, uint32 nStart = 0, uint32 nCount = 0) const override;
00325         inline virtual bool operator ==(const Container<bool> &lstContainer) const;
00326         inline virtual bool operator !=(const Container<bool> &lstContainer) const;
00327 
00328 
00329 };
00330 
00331 
00332 //[-------------------------------------------------------]
00333 //[ Namespace                                             ]
00334 //[-------------------------------------------------------]
00335 } // PLCore
00336 
00337 
00338 //[-------------------------------------------------------]
00339 //[ Implementation                                        ]
00340 //[-------------------------------------------------------]
00341 #include "PLCore/Container/Bitset.inl"
00342 
00343 
00344 #endif // __PLCORE_CONTAINER_BITSET_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