GenericHashTable.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 // GenericHashTable.H
00012 //
00013 /* 
00014  * \class GenericHashTable
00015  *
00016  * A hash table class that uses a general set of classes for keys.
00017  * The \c GenericHashTable class is derived from \c AbstractHashTable,
00018  * which defines the basic operations of the hash table. The keys
00019  * are assumed to be classes for which the following operations are
00020  * defined:
00021  *      size_type hash(size_type tablesize) const
00022  *      int compare(KEY& key) const
00023  *      int write(ostream& os) const
00024  *      int read(istream& is)
00025  *
00026  * \sa SimpleHashTable
00027  */
00028 
00029 
00030 #ifndef __GenericHashTable_h
00031 #define __GenericHashTable_h
00032 
00033 #ifdef __GNUC__
00034 #pragma interface
00035 #endif
00036 
00037 #include "AbstractHashTable.h"
00038  
00039 template <class T>
00040 class UTILIB_API GenericHashTable;
00041 
00042 
00043 
00047 template <class T>
00048 class GenericHashTableItem 
00049 {
00050   friend class AbstractHashTable<GenericHashTableItem<T>,T>;
00051 
00052 public:
00053 
00055   void write(ostream& os) {Keyptr->write(os);}
00057   T& key() {return *Keyptr;}
00059   int compare(T& key)
00060                 {return Keyptr->compare(key);}
00061 
00062 private:
00063 
00065   T* Keyptr;
00067   GenericHashTableItem(T* Key_) : Keyptr(Key_) {}
00068 };
00069 
00070 
00071 
00072 template <class T>
00073 class GenericHashTable : public AbstractHashTable<GenericHashTableItem<T>,T>
00074 {
00075 public:
00076 
00078   explicit GenericHashTable(const char* nameBuff = "Unnamed")
00079         : AbstractHashTable<GenericHashTableItem<T>,T>(nameBuff) {}
00080 
00082   explicit GenericHashTable(size_type init_size, 
00083                                                 const char* nameBuff = "Unnamed")
00084         : AbstractHashTable<GenericHashTableItem<T>,T>(init_size, nameBuff) {}
00085 };
00086 
00087 #endif