GenericHeap.h

00001 /*  _________________________________________________________________________
00002  *
00003  *  UTILIB: A utility library for developing portable C++ codes.
00004  *  Copyright (c) 2001, Sandia National Laboratories.
00005  *  This software is distributed under the GNU Lesser General Public License.
00006  *  For more information, see the README file in the top UTILIB directory.
00007  *  _________________________________________________________________________
00008  */
00009 
00010 // 
00011 // GenericHeap.H
00012 //
00013 /* 
00014  * \class GenericHeap
00015  *
00016  * A heap class that uses a general set of classes for keys.
00017  * The \c GenericHeap class is derived from \c AbstractHeap,
00018  * which defines the basic operations of the heap. The keys
00019  * are assumed to be classes for which the following operations are
00020  * defined:
00021  *      int compare(KEY& key) const
00022  *      int write(ostream& os) const
00023  *      int read(istream& is)
00024  *
00025  * \sa SimpleHeap
00026  */
00027 
00028 
00029 #ifndef __GenericHeap_h
00030 #define __GenericHeap_h
00031 
00032 #ifdef __GNUC__
00033 #pragma interface
00034 #endif
00035 
00036 #include "_generic.h"
00037 #include "AbstractHeap.h"
00038  
00039 template <class T>
00040 class UTILIB_API GenericHeap;
00041 
00045 template <class T>
00046 class GenericHeapItem 
00047 {
00048   friend class AbstractHeap<GenericHeapItem<T>,T>;
00049 
00050 public:
00051 
00053   void write(ostream& os) {Keyptr->write(os);}
00055   T& key() {return *Keyptr;}
00057   int compare(T& key)
00058                 {return Keyptr->compare(key);}
00059 
00060 private:
00061 
00063   T* Keyptr;
00065   int element;
00067   GenericHeapItem(T* Key_) : Keyptr(Key_), element(0) {}
00068 };
00069 
00070 
00071 
00072 template <class T>
00073 class GenericHeap : public AbstractHeap<GenericHeapItem<T>,T>
00074 {
00075 public:
00076 
00078   explicit GenericHeap(const char* nameBuff = "Unnamed", 
00079                 int initSize=defaultSize, int quantumChoice = defaultQuantum)
00080         : AbstractHeap<GenericHeapItem<T>,T>(nameBuff,initSize,quantumChoice) {}
00081 
00082 };
00083 
00084 #endif