Blender  V2.59
SG_QList.h
Go to the documentation of this file.
00001 /*
00002  * $Id: SG_QList.h 35082 2011-02-22 19:30:37Z jesterking $
00003  *
00004  * ***** BEGIN GPL LICENSE BLOCK *****
00005  *
00006  * This program is free software; you can redistribute it and/or
00007  * modify it under the terms of the GNU General Public License
00008  * as published by the Free Software Foundation; either version 2
00009  * of the License, or (at your option) any later version.
00010  *
00011  * This program is distributed in the hope that it will be useful,
00012  * but WITHOUT ANY WARRANTY; without even the implied warranty of
00013  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00014  * GNU General Public License for more details.
00015  *
00016  * You should have received a copy of the GNU General Public License
00017  * along with this program; if not, write to the Free Software Foundation,
00018  * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
00019  *
00020  * The Original Code is Copyright (C) 2001-2002 by NaN Holding BV.
00021  * All rights reserved.
00022  *
00023  * The Original Code is: all of this file.
00024  *
00025  * Contributor(s): none yet.
00026  *
00027  * ***** END GPL LICENSE BLOCK *****
00028  */
00029 
00034 #ifndef __SG_QLIST
00035 #define __SG_QLIST
00036 
00037 #include "SG_DList.h"
00038 
00043 class SG_QList : public SG_DList
00044 {
00045 protected :
00046         SG_QList* m_fqlink;
00047         SG_QList* m_bqlink;
00048 
00049 public:
00050         template<typename T> class iterator
00051         {
00052         private:
00053                 SG_QList&       m_head;
00054                 T*                      m_current;
00055         public:
00056                 typedef iterator<T> _myT;
00057                 iterator(SG_QList& head, SG_QList* current=NULL) : m_head(head) { m_current = (T*)current; }
00058                 ~iterator() {}
00059 
00060                 void begin()
00061                 {
00062                         m_current = (T*)m_head.QPeek();
00063                 }
00064                 void back()
00065                 {
00066                         m_current = (T*)m_head.QBack();
00067                 }
00068                 bool end()
00069                 {
00070                         return (m_current == (T*)m_head.Self());
00071                 }
00072                 bool add_back(T* item)
00073                 {
00074                         return m_current->QAddBack(item);
00075                 }
00076                 T* operator*()
00077                 {
00078                         return m_current;
00079                 }
00080                 _myT& operator++()
00081                 {
00082                         m_current = (T*)m_current->QPeek();
00083                         return *this;
00084                 }
00085                 _myT& operator--()
00086                 {
00087                         // no check on NULL! make sure you don't try to increment beyond end
00088                         m_current = (T*)m_current->QBack();
00089                         return *this;
00090                 }
00091         };
00092 
00093         SG_QList() : SG_DList()
00094     { 
00095         m_fqlink = m_bqlink = this; 
00096     }
00097         SG_QList(const SG_QList& other) : SG_DList()
00098         {
00099         m_fqlink = m_bqlink = this; 
00100         }
00101     virtual ~SG_QList() 
00102     {
00103                 QDelink();
00104     }
00105 
00106     inline bool QEmpty()               // Check for empty queue
00107     {     
00108         return ( m_fqlink == this ); 
00109     }
00110     bool QAddBack( SG_QList *item )  // Add to the back
00111     {
00112                 if (!item->QEmpty())
00113                         return false;
00114         item->m_bqlink = m_bqlink;
00115         item->m_fqlink = this;
00116         m_bqlink->m_fqlink = item;
00117         m_bqlink = item;
00118                 return true;
00119     }
00120     bool QAddFront( SG_QList *item )  // Add to the back
00121     {
00122                 if (!item->Empty())
00123                         return false;
00124         item->m_fqlink = m_fqlink;
00125         item->m_bqlink = this;
00126         m_fqlink->m_bqlink = item;
00127         m_fqlink = item;
00128                 return true;
00129     }
00130     SG_QList *QRemove()           // Remove from the front
00131     {
00132         if (QEmpty()) 
00133         {
00134             return NULL;
00135         }
00136         SG_QList* item = m_fqlink;
00137         m_fqlink = item->m_fqlink;
00138         m_fqlink->m_bqlink = this;
00139         item->m_fqlink = item->m_bqlink = item;
00140         return item;
00141     }
00142     bool QDelink()             // Remove from the middle
00143     {
00144                 if (QEmpty())
00145                         return false;
00146                 m_bqlink->m_fqlink = m_fqlink;
00147                 m_fqlink->m_bqlink = m_bqlink;
00148                 m_fqlink = m_bqlink = this;
00149                 return true;
00150     }
00151     inline SG_QList *QPeek()                    // Look at front without removing
00152     { 
00153         return m_fqlink; 
00154     }  
00155     inline SG_QList *QBack()                    // Look at front without removing
00156     { 
00157         return m_bqlink; 
00158     }  
00159         
00160         
00161 #ifdef WITH_CXX_GUARDEDALLOC
00162 public:
00163         void *operator new(size_t num_bytes) { return MEM_mallocN(num_bytes, "GE:SG_QList"); }
00164         void operator delete( void *mem ) { MEM_freeN(mem); }
00165 #endif
00166 };
00167 
00168 #endif //__SG_QLIST
00169