PixelLightAPI  .
SimpleList.h
Go to the documentation of this file.
00001 /*********************************************************\
00002  *  File: SimpleList.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_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         *    Copy operator
00168         *
00169         *  @param[in] lstSource
00170         *    Simple list to copy from
00171         *
00172         *  @return
00173         *    Reference to this instance
00174         */
00175         SimpleList<ValueType> &operator =(const SimpleList<ValueType> &lstSource);
00176 
00177 
00178     //[-------------------------------------------------------]
00179     //[ Public Iterable functions                             ]
00180     //[-------------------------------------------------------]
00181     public:
00182         Iterator<ValueType> GetIterator(uint32 nIndex = 0) const;
00183         ConstIterator<ValueType> GetConstIterator(uint32 nIndex = 0) const;
00184         Iterator<ValueType> GetEndIterator() const;
00185         ConstIterator<ValueType> GetConstEndIterator() const;
00186 
00187 
00188     //[-------------------------------------------------------]
00189     //[ Public Container functions                            ]
00190     //[-------------------------------------------------------]
00191     public:
00192         bool IsEmpty() const;
00193         uint32 GetNumOfElements() const;
00194         uint32 GetElementSize() const;
00195         uint32 GetSize() const;
00196         void Clear();
00197         bool IsElement(const ValueType &Element) const;
00198         int GetIndex(const ValueType &Element) const;
00199         ValueType &Get(uint32 nIndex) const;
00200         ValueType &operator [](uint32 nIndex) const;
00201         bool Replace(const ValueType &Element1, const ValueType &Element2);
00202         bool ReplaceAtIndex(uint32 nIndex, const ValueType &Element);
00203         ValueType &Add();
00204         ValueType &Add(const ValueType &Element);
00205         uint32 Add(const ValueType *pElements, uint32 nCount);
00206         SimpleList<ValueType> &operator +=(const ValueType &Element);
00207         bool Add(const Container<ValueType> &lstContainer, uint32 nStart = 0, uint32 nCount = 0);
00208         SimpleList<ValueType> &operator +=(const Container<ValueType> &lstContainer);
00209         ValueType &AddAtIndex(int nIndex);
00210         ValueType &AddAtIndex(const ValueType &Element, int nIndex);
00211         bool Remove(const ValueType &Element);
00212         bool RemoveAtIndex(uint32 nElement);
00213         SimpleList<ValueType> &operator -=(const ValueType &Element);
00214         bool Remove(const Container<ValueType> &lstContainer, uint32 nStart = 0, uint32 nCount = 0);
00215         SimpleList<ValueType> &operator -=(const Container<ValueType> &lstContainer);
00216         bool Copy(const Container<ValueType> &lstContainer, uint32 nStart = 0, uint32 nCount = 0);
00217         SimpleList<ValueType> &operator =(const Container<ValueType> &lstContainer);
00218         bool Compare(const Container<ValueType> &lstContainer, uint32 nStart = 0, uint32 nCount = 0) const;
00219         bool operator ==(const Container<ValueType> &lstContainer) const;
00220         bool operator !=(const Container<ValueType> &lstContainer) const;
00221 
00222 
00223 };
00224 
00225 
00226 //[-------------------------------------------------------]
00227 //[ Public static variables                               ]
00228 //[-------------------------------------------------------]
00229 template <class ValueType> ValueType SimpleList<ValueType>::Null;
00230 
00231 
00232 //[-------------------------------------------------------]
00233 //[ Namespace                                             ]
00234 //[-------------------------------------------------------]
00235 } // PLCore
00236 
00237 
00238 //[-------------------------------------------------------]
00239 //[ Implementation                                        ]
00240 //[-------------------------------------------------------]
00241 #include "PLCore/Container/SimpleList.inl"
00242 
00243 
00244 #endif // __PLCORE_CONTAINER_SIMPLELIST_H__


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