Main Page   Class Hierarchy   Compound List   File List   Compound Members  

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 #include "gldebug.h"
00033 
00034 
00037 template <class T>
00038 struct GlSListNode
00039 {
00040         GlSListNode* next;      
00041         T data;                         
00042 };
00043 
00044 
00049 template <class T>
00050 class GlSList
00051 {
00052   public:
00053         GlSList()               { root = 0; }
00054         ~GlSList()              { Clear(); }
00055 
00057         int  Size() const               { return Count(); }
00059         int  Count() const              {       GlSListNode<T>* node;
00060                                                                 int count = 0;
00061                                                                 for( node = root; node; node = node->next )
00062                                                                         ++count;
00063                                                                 return count;
00064                                                         }
00066         bool Empty() const              { return root == 0; }
00068         T&       Front() const          { return root->data; }
00070         GlSListNode<T>*  FrontNode() const              { return root; }
00071 
00073         void Clear()    {       GlSListNode<T>* temp;
00074 
00075                                                 while( root )
00076                                                 {
00077                                                         temp = root;
00078                                                         root = root->next;
00079                                                         delete temp;
00080                                                 }
00081                                         }
00082 
00084         void PushFront( const T& insert )
00085                                         {
00086                                                 GlSListNode<T>* node = new GlSListNode<T>;
00087                                                 node->data = insert;
00088                                                 node->next = root;
00089                                                 root = node;
00090                                         }
00091 
00093         void PushBack( const T& insert )
00094         {
00095                 GlSListNode<T>* end;
00096                 for ( end=root; end && end->next; end = end->next )
00097                 {}
00098 
00099                 if ( !end )
00100                 {
00101                         PushFront( insert );
00102                 }
00103                 else
00104                 {
00105                         GlSListNode<T>* node = new GlSListNode<T>;
00106                         node->data = insert;
00107                         node->next = 0;
00108                         end->next = node;
00109                 }
00110         }
00111 
00114         void PopFront()
00115         {
00116                 if ( root )
00117                 {
00118                         GlSListNode<T>* temp = root->next;
00119                         delete root;
00120                         root = temp;
00121                 }
00122         }
00123 
00127         void Pop( const T& thisone )
00128         {
00129                 GlSListNode<T>* node;
00130                 GlSListNode<T>* prev;
00131 
00132                 for( node = root, prev = 0;
00133                          node;
00134                          prev = node, node = node->next )
00135                 {
00136                         if ( node->data == thisone )
00137                         {
00138                                 if ( prev )
00139                                 {
00140                                         prev->next = node->next;
00141                                 }
00142                                 else
00143                                 {
00144                                         root = node->next;
00145                                 }
00146                                 delete node;
00147                                 break;
00148                         }
00149                 }
00150         }
00151 
00153         GlSListNode<T>* Find( const T& findthis )
00154         {
00155                 GlSListNode<T>* node;
00156                 for( node=root; node; node=node->next )
00157                 {
00158                         if ( node->data == findthis )
00159                                 return node;
00160                 }       
00161                 return 0;
00162         }
00163                 
00165         bool FindAndDelete( const T& findthis )
00166         {
00167                 GlSListNode<T>* node;
00168                 GlSListNode<T>* prev;
00169 
00170                 for(    node=root, prev = 0; 
00171                                 node; 
00172                                 prev = node, node=node->next )
00173                 {
00174                         if ( node->data == findthis )
00175                         {
00176                                 if ( prev )
00177                                         prev->next = node->next;
00178                                 else
00179                                         root = node->next;
00180                                 delete node;
00181                                 return true;
00182                         }
00183                 }       
00184                 return false;
00185         }
00186 
00187 
00188   private:
00189         GlSListNode<T>* root;
00190         GlSList( const GlSList& that );
00191         GlSList operator=( const GlSList& that );
00192 };
00193 
00194 
00197 template <class T>
00198 class GlSListIterator
00199 {
00200   public:
00201         GlSListIterator( const GlSList<T>& _list ) : current( 0 ), list( &_list )       {}
00202 
00203         void Begin()            { current = list->FrontNode(); }
00204         void Next()                     { current = current->next; }
00205         bool Done()                     { return ( current == 0 ); }
00206 
00208         T& Current()                                    { return (current->data); }
00209 
00210         GlSListNode<T>* CurrentNode()   { return current; }
00211 
00212   private:
00213         GlSListNode<T>* current;        
00214         const GlSList<T>* list;
00215 };
00216 
00217 
00218 #endif

Generated on Mon Sep 15 12:01:10 2003 for Kyra by doxygen1.2.11.1 written by Dimitri van Heesch, © 1997-2001