src/tools/tHeap.h

Go to the documentation of this file.
00001 /*
00002 
00003 *************************************************************************
00004 
00005 ArmageTron -- Just another Tron Lightcycle Game in 3D.
00006 Copyright (C) 2000  Manuel Moos (manuel@moosnet.de)
00007 
00008 **************************************************************************
00009 
00010 This program is free software; you can redistribute it and/or
00011 modify it under the terms of the GNU General Public License
00012 as published by the Free Software Foundation; either version 2
00013 of the License, or (at your option) any later version.
00014 
00015 This program is distributed in the hope that it will be useful,
00016 but WITHOUT ANY WARRANTY; without even the implied warranty of
00017 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00018 GNU General Public License for more details.
00019 
00020 You should have received a copy of the GNU General Public License
00021 along with this program; if not, write to the Free Software
00022 Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
00023   
00024 ***************************************************************************
00025 
00026 */
00027 
00028 #ifndef ArmageTron_HEAP_H
00029 #define ArmageTron_HEAP_H
00030 
00031 #include "tList.h"
00032 #include "defs.h"
00033 
00034 /*
00035   Heap data structure
00036 */
00037 
00038 #ifdef DEBUG_EXPENSIVE
00039 #define EVENT_DEB
00040 #endif
00041 
00042 // #define HEAP_DEB
00043 
00044 class tHeapElement;
00045 
00046 class tHeapBase:private tList<tHeapElement>{
00047     friend class tHeapElement;
00048 protected:
00049     int  Lower(int i){ // the element below i in the heap
00050         if(i%2==0)  // just to make sure what happens; you never know what 1/2 is..
00051             return i/2-1;
00052         else
00053             return (i-1)/2;
00054     }
00055 
00056 int Len()const { return tList<tHeapElement>::Len(); }
00057     tHeapElement* operator()( int i ) { return const_cast< tHeapElement* >( tList<tHeapElement>::operator()(i) ); }
00058     const tHeapElement* operator()( int i )const { return tList<tHeapElement>::operator()(i); }
00059 
00060     bool SwapIf(int i,int j); // swaps heap elements i and j if they are
00061     // in wrong order; only then, TRUE is returned.
00062     // i is assumed to lie lower in the heap than j.
00063 
00064     void SwapDown(int j); // swap element j as far down as it may go.
00065     void SwapUp(int i);   // swap element j as far up as it must.
00066 
00067     //#ifdef EVENT_DEB
00068     void CheckHeap(); // checks the heap structure
00069     //#endif
00070 
00071     void Insert(tHeapElement *e);  // starts to manage object e
00072     void Remove(tHeapElement *e);  // stops (does not delete e)
00073     tHeapElement * Remove(int i);     // stops to manage object i, wich is returned.
00074     void Replace(int i);  // puts element i to the right place (if the
00075     // rest of the heap has correct order)
00076     void Replace(tHeapElement *e);
00077 public:
00078     static int  UpperL(int i){return 2*i+1;} // the elements left and
00079     static int  UpperR(int i){return 2*i+2;} // right above i
00080 
00081     tHeapBase(){}
00082     ~tHeapBase();
00083 };
00084 
00085 
00086 // the heap elements.
00087 
00088 class tHeapElement{
00089     friend class tHeapBase;
00090 
00091 public:
00092     tHeapElement():hID(-1),value_(0){}
00093     virtual ~tHeapElement();
00094 
00095     REAL Val() const {return value_;}
00096     void SetVal( REAL value, tHeapBase& heap );
00097     void RemoveFromHeap();
00098 
00099 protected:
00100     virtual tHeapBase *Heap() const = 0; //{return NULL;} // in wich heap are we?
00101 
00102 protected:
00103 private:
00104     int  hID;         // the id in the heap
00105     REAL value_;      // our value. lower values are lower in the heap.
00106 };
00107 
00108 
00109 template<class T> class tHeap: public tHeapBase{
00110 public:
00111     tHeap(){};
00112     ~tHeap(){};
00113 
00114     void Insert(T *e){tHeapBase::Insert(e);}  // starts to manage object e
00115     void Remove(T *e){tHeapBase::Remove(e);}  // stops (does not delete e)
00116     void Replace(T *e){tHeapBase::Replace(e);}
00117     T * Remove(int i){return static_cast<T*> (tHeapBase::Remove(i));}
00118 
00119     T * operator()(int i){return static_cast< T *>(tHeapBase::operator()(i));}
00120     const T * operator()(int i) const {return static_cast<const T *>(tHeapBase::operator()(i));}
00121     T * Events(int i){return static_cast<T *>(tHeapBase::operator()(i));}
00122 
00123     int Len() const {return tHeapBase::Len();}
00124 
00125     tHeapBase * operator &(){return this;}
00126 };
00127 
00128 #endif
00129 

Generated on Sat Mar 15 22:56:00 2008 for Armagetron Advanced by  doxygen 1.5.4