gllist.h

00001 /*
00002 Copyright (c) 2000-2003 Lee Thomason (www.grinninglizard.com)
00003 
00004 Grinning Lizard Utilities. Note that software that uses the 
00005 utility package (including Lilith3D and Kyra) have more restrictive
00006 licences which applies to code outside of the utility package.
00007 
00008 
00009 This software is provided 'as-is', without any express or implied 
00010 warranty. In no event will the authors be held liable for any 
00011 damages arising from the use of this software.
00012 
00013 Permission is granted to anyone to use this software for any 
00014 purpose, including commercial applications, and to alter it and 
00015 redistribute it freely, subject to the following restrictions:
00016 
00017 1. The origin of this software must not be misrepresented; you must 
00018 not claim that you wrote the original software. If you use this 
00019 software in a product, an acknowledgment in the product documentation 
00020 would be appreciated but is not required.
00021 
00022 2. Altered source versions must be plainly marked as such, and 
00023 must not be misrepresented as being the original software.
00024 
00025 3. This notice may not be removed or altered from any source 
00026 distribution.
00027 */
00028 
00029 #ifndef KYRA_LIST_INCLUDED
00030 #define KYRA_LIST_INCLUDED
00031 
00032 #ifdef _MSC_VER
00033 // Disable the no-exception handling warning.
00034 #pragma warning( disable : 4530 )
00035 #pragma warning( disable : 4786 )
00036 #endif
00037 
00038 #include "../../grinliz/gldebug.h"
00039 
00040 
00043 template <class T>
00044 struct GlSListNode
00045 {
00046         GlSListNode* next;      
00047         T data;                         
00048 };
00049 
00050 
00055 template <class T>
00056 class GlSList
00057 {
00058   public:
00059         GlSList()               { root = 0; }
00060         ~GlSList()              { Clear(); }
00061 
00063         int  Size() const               { return Count(); }
00065         int  Count() const              {       GlSListNode<T>* node;
00066                                                                 int count = 0;
00067                                                                 for( node = root; node; node = node->next )
00068                                                                         ++count;
00069                                                                 return count;
00070                                                         }
00072         bool Empty() const              { return root == 0; }
00074         T&       Front() const          { return root->data; }
00076         GlSListNode<T>*  FrontNode() const              { return root; }
00077 
00079         void Clear()    {       GlSListNode<T>* temp;
00080 
00081                                                 while( root )
00082                                                 {
00083                                                         temp = root;
00084                                                         root = root->next;
00085                                                         delete temp;
00086                                                 }
00087                                         }
00088 
00090         void PushFront( const T& insert )
00091                                         {
00092                                                 GlSListNode<T>* node = new GlSListNode<T>;
00093                                                 node->data = insert;
00094                                                 node->next = root;
00095                                                 root = node;
00096                                         }
00097 
00099         void PushBack( const T& insert )
00100         {
00101                 GlSListNode<T>* end;
00102                 for ( end=root; end && end->next; end = end->next )
00103                 {}
00104 
00105                 if ( !end )
00106                 {
00107                         PushFront( insert );
00108                 }
00109                 else
00110                 {
00111                         GlSListNode<T>* node = new GlSListNode<T>;
00112                         node->data = insert;
00113                         node->next = 0;
00114                         end->next = node;
00115                 }
00116         }
00117 
00120         void PopFront()
00121         {
00122                 if ( root )
00123                 {
00124                         GlSListNode<T>* temp = root->next;
00125                         delete root;
00126                         root = temp;
00127                 }
00128         }
00129 
00133         void Pop( const T& thisone )
00134         {
00135                 GlSListNode<T>* node;
00136                 GlSListNode<T>* prev;
00137 
00138                 for( node = root, prev = 0;
00139                          node;
00140                          prev = node, node = node->next )
00141                 {
00142                         if ( node->data == thisone )
00143                         {
00144                                 if ( prev )
00145                                 {
00146                                         prev->next = node->next;
00147                                 }
00148                                 else
00149                                 {
00150                                         root = node->next;
00151                                 }
00152                                 delete node;
00153                                 break;
00154                         }
00155                 }
00156         }
00157 
00159         GlSListNode<T>* Find( const T& findthis )
00160         {
00161                 GlSListNode<T>* node;
00162                 for( node=root; node; node=node->next )
00163                 {
00164                         if ( node->data == findthis )
00165                                 return node;
00166                 }       
00167                 return 0;
00168         }
00169                 
00171         bool FindAndDelete( const T& findthis )
00172         {
00173                 GlSListNode<T>* node;
00174                 GlSListNode<T>* prev;
00175 
00176                 for(    node=root, prev = 0; 
00177                                 node; 
00178                                 prev = node, node=node->next )
00179                 {
00180                         if ( node->data == findthis )
00181                         {
00182                                 if ( prev )
00183                                         prev->next = node->next;
00184                                 else
00185                                         root = node->next;
00186                                 delete node;
00187                                 return true;
00188                         }
00189                 }       
00190                 return false;
00191         }
00192 
00193 
00194   private:
00195         GlSListNode<T>* root;
00196         GlSList( const GlSList& that );
00197         GlSList operator=( const GlSList& that );
00198 };
00199 
00200 
00203 template <class T>
00204 class GlSListIterator
00205 {
00206   public:
00207         GlSListIterator( const GlSList<T>& _list ) : current( 0 ), list( &_list )       {}
00208 
00209         void Begin()            { current = list->FrontNode(); }
00210         void Next()                     { current = current->next; }
00211         bool Done()                     { return ( current == 0 ); }
00212 
00214         T& Current()                                    { return (current->data); }
00215 
00216         GlSListNode<T>* CurrentNode()   { return current; }
00217 
00218   private:
00219         GlSListNode<T>* current;        
00220         const GlSList<T>* list;
00221 };
00222 
00223 
00224 #endif

Generated on Thu Jul 20 20:45:31 2006 for Kyra by  doxygen 1.4.7