PixelLightAPI  .
SimpleList.h
Go to the documentation of this file.
00001 /*********************************************************\
00002  *  File: SimpleList.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_SIMPLELIST_H__
00024 #define __PLCORE_CONTAINER_SIMPLELIST_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 SimpleListIterator;
00044 
00045 
00046 //[-------------------------------------------------------]
00047 //[ Classes                                               ]
00048 //[-------------------------------------------------------]
00049 /**
00050 *  @brief
00051 *    Linked simple list class
00052 *
00053 *  @remarks
00054 *    Purpose of this class is a 'as memory compact as possible'-list, so we also needed to
00055 *    get rid of the virtual-table. As result you can't cast this list type down to "Container"
00056 *    or "Iterable", but get a most memory compact list. We recommend to use this list only
00057 *    in memory-critical implementations were each byte counts.
00058 *    Within the single linked list each node knows it's next element. If you
00059 *    want to receive element x, internally the process begins with the first element and
00060 *    goes to the next until element x was found. As you see the following example isn't
00061 *    that performant:\n
00062 *  @verbatim
00063 *    for (uint32 i=0; i<lstSimpleList.GetNumOfElements(); i++) {
00064 *      ListElement &cElement = lstSimpleList[i];
00065 *    }
00066 *  @endverbatim
00067 *    Instead use the provided iterator whenever possible:\
00068 *  @verbatim
00069 *    SimpleListIterator<ListElement*> cIterator = m_lstSimpleList.GetIterator();
00070 *    while (cIterator.HasNext()) {
00071 *        ListElement *pElement = cIterator.Next();
00072 *    }
00073 *  @endverbatim
00074 *
00075 *  @note
00076 *    - IsEmpty(), GetNumOfElements(), GetSize() linear, not constant
00077 *    - Iterator from end to start is quite slow
00078 *    - For maximum performance options the access to the internal data is public, we strongly recommend
00079 *      to use this ONLY when really required and only to read, not to manipulate!
00080 *
00081 *  @see
00082 *    - 'Iterable' for the documentation of the 'Public Iterable functions'
00083 *    - 'Container' for the documentation of the 'Public Container functions'
00084 */
00085 template <class ValueType>
00086 class SimpleList {
00087 
00088 
00089     //[-------------------------------------------------------]
00090     //[ Friends                                               ]
00091     //[-------------------------------------------------------]
00092     friend class SimpleListIterator<ValueType>;
00093 
00094 
00095     //[-------------------------------------------------------]
00096     //[ Public static variables                               ]
00097     //[-------------------------------------------------------]
00098     public:
00099         static ValueType Null;  /**< 'Null'-object, do NEVER EVER manipulate this object! */
00100 
00101 
00102     //[-------------------------------------------------------]
00103     //[ Public structures                                     ]
00104     //[-------------------------------------------------------]
00105     public:
00106         /**
00107         *  @brief
00108         *    Internal linked simple list element
00109         */
00110         struct ListElement {
00111             ListElement *pNextElement;  /**< Pointer to the next element in the list, can be a null pointer */
00112             ValueType    Data;          /**< The stored data */
00113         };
00114 
00115 
00116     //[-------------------------------------------------------]
00117     //[ Public data                                           ]
00118     //[-------------------------------------------------------]
00119     public:
00120         ListElement *pFirstElement; /**< Pointer to first list element, can be a null pointer */
00121 
00122 
00123     //[-------------------------------------------------------]
00124     //[ Public functions                                      ]
00125     //[-------------------------------------------------------]
00126     public:
00127         /**
00128         *  @brief
00129         *    Constructor
00130         */
00131         SimpleList();
00132 
00133         /**
00134         *  @brief
00135         *    Copy constructor
00136         *
00137         *  @param[in] lstSource
00138         *    Simple list to copy from
00139         *  @param[in] nStart
00140         *    Index the copy operation should start
00141         *  @param[in] nCount
00142         *    Number of elements to copy, if 0 copy all elements of lstSource behind nStart
00143         */
00144         SimpleList(const SimpleList<ValueType> &lstSource, uint32 nStart = 0, uint32 nCount = 0);
00145 
00146         /**
00147         *  @brief
00148         *    Copy constructor
00149         *
00150         *  @param[in] lstSource
00151         *    Container to copy from
00152         *  @param[in] nStart
00153         *    Index the copy operation should start
00154         *  @param[in] nCount
00155         *    Number of elements to copy, if 0 copy all elements of lstSource behind nStart
00156         */
00157         SimpleList(const Container<ValueType> &lstSource, uint32 nStart = 0, uint32 nCount = 0);
00158 
00159         /**
00160         *  @brief
00161         *    Destructor
00162         */
00163         ~SimpleList();
00164 
00165         /**
00166         *  @brief
00167         *    Makes this container to a copy of another container
00168         *
00169         *  @param[in] lstContainer
00170         *    Container to copy from
00171         *  @param[in] nStart
00172         *    Index the copy operation should start
00173         *  @param[in] nCount
00174         *    Number of elements to copy, if 0 copy all elements of lstContainer behind nStart
00175         *
00176         *  @return
00177         *    'true' if all went fine, else 'false'
00178         */
00179         bool Copy(const SimpleList<ValueType> &lstContainer, uint32 nStart = 0, uint32 nCount = 0);
00180 
00181         /**
00182         *  @brief
00183         *    Copy operator
00184         *
00185         *  @param[in] lstSource
00186         *    Simple list to copy from
00187         *
00188         *  @return
00189         *    Reference to this instance
00190         */
00191         SimpleList<ValueType> &operator =(const SimpleList<ValueType> &lstSource);
00192 
00193 
00194     //[-------------------------------------------------------]
00195     //[ Public Iterable functions                             ]
00196     //[-------------------------------------------------------]
00197     public:
00198         Iterator<ValueType> GetIterator(uint32 nIndex = 0) const;
00199         ConstIterator<ValueType> GetConstIterator(uint32 nIndex = 0) const;
00200         Iterator<ValueType> GetEndIterator() const;
00201         ConstIterator<ValueType> GetConstEndIterator() const;
00202 
00203 
00204     //[-------------------------------------------------------]
00205     //[ Public Container functions                            ]
00206     //[-------------------------------------------------------]
00207     public:
00208         bool IsEmpty() const;
00209         uint32 GetNumOfElements() const;
00210         uint32 GetElementSize() const;
00211         uint32 GetSize() const;
00212         void Clear();
00213         bool IsElement(const ValueType &Element) const;
00214         int GetIndex(const ValueType &Element) const;
00215         ValueType &Get(uint32 nIndex) const;
00216         ValueType &operator [](uint32 nIndex) const;
00217         bool Replace(const ValueType &Element1, const ValueType &Element2);
00218         bool ReplaceAtIndex(uint32 nIndex, const ValueType &Element);
00219         ValueType &Add();
00220         ValueType &Add(const ValueType &Element);
00221         uint32 Add(const ValueType *pElements, uint32 nCount);
00222         SimpleList<ValueType> &operator +=(const ValueType &Element);
00223         bool Add(const Container<ValueType> &lstContainer, uint32 nStart = 0, uint32 nCount = 0);
00224         SimpleList<ValueType> &operator +=(const Container<ValueType> &lstContainer);
00225         ValueType &AddAtIndex(int nIndex);
00226         ValueType &AddAtIndex(const ValueType &Element, int nIndex);
00227         bool Remove(const ValueType &Element);
00228         bool RemoveAtIndex(uint32 nElement);
00229         SimpleList<ValueType> &operator -=(const ValueType &Element);
00230         bool Remove(const Container<ValueType> &lstContainer, uint32 nStart = 0, uint32 nCount = 0);
00231         SimpleList<ValueType> &operator -=(const Container<ValueType> &lstContainer);
00232         bool Copy(const Container<ValueType> &lstContainer, uint32 nStart = 0, uint32 nCount = 0);
00233         SimpleList<ValueType> &operator =(const Container<ValueType> &lstContainer);
00234         bool Compare(const Container<ValueType> &lstContainer, uint32 nStart = 0, uint32 nCount = 0) const;
00235         bool operator ==(const Container<ValueType> &lstContainer) const;
00236         bool operator !=(const Container<ValueType> &lstContainer) const;
00237 
00238 
00239 };
00240 
00241 
00242 //[-------------------------------------------------------]
00243 //[ Public static variables                               ]
00244 //[-------------------------------------------------------]
00245 template <class ValueType> ValueType SimpleList<ValueType>::Null;
00246 
00247 
00248 //[-------------------------------------------------------]
00249 //[ Namespace                                             ]
00250 //[-------------------------------------------------------]
00251 } // PLCore
00252 
00253 
00254 //[-------------------------------------------------------]
00255 //[ Implementation                                        ]
00256 //[-------------------------------------------------------]
00257 #include "PLCore/Container/SimpleList.inl"
00258 
00259 
00260 #endif // __PLCORE_CONTAINER_SIMPLELIST_H__


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