tpl_dynLinHash.H

00001 
00002 /*
00003   This file is part of Aleph system
00004 
00005   Copyright (c) 2002, 2003, 2004, 2005, 2006
00006   UNIVERSITY LOS ANDES (ULA) Merida - REPÚBLICA BOLIVARIANA DE VENEZUELA  
00007 
00008   - Center of Studies in Microelectronics & Distributed Systems (CEMISID) 
00009   - ULA Computer Science Department
00010   - FUNDACITE Mérida - Ministerio de Ciencia y Tecnología
00011 
00012   PERMISSION TO USE, COPY, MODIFY AND DISTRIBUTE THIS SOFTWARE AND ITS
00013   DOCUMENTATION IS HEREBY GRANTED, PROVIDED THAT BOTH THE COPYRIGHT
00014   NOTICE AND THIS PERMISSION NOTICE APPEAR IN ALL COPIES OF THE
00015   SOFTWARE, DERIVATIVE WORKS OR MODIFIED VERSIONS, AND ANY PORTIONS
00016   THEREOF, AND THAT BOTH NOTICES APPEAR IN SUPPORTING DOCUMENTATION.
00017 
00018   Aleph is distributed in the hope that it will be useful, but WITHOUT
00019   ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
00020   or FITNESS FOR A PARTICULAR PURPOSE.
00021 
00022   UNIVERSIDAD DE LOS ANDES requests users of this software to return to 
00023 
00024   Leandro Leon
00025   CEMISID 
00026   Ed La Hechicera 
00027   3er piso, ala sur
00028   Facultad de Ingenieria 
00029   Universidad de Los Andes 
00030   Merida - REPÚBLICA BOLIVARIANA DE VENEZUELA    or
00031 
00032   lrleon@ula.ve
00033 
00034   any improvements or extensions that they make and grant Universidad 
00035   de Los Andes (ULA) the rights to redistribute these changes. 
00036 
00037   Aleph is (or was) granted by: 
00038   - Consejo de Desarrollo Cientifico, Humanistico, Tecnico de la ULA 
00039     (CDCHT)
00040   - Fundacite Mérida
00041 */
00042 
00043 
00044 # ifndef TPL_DYNLINHASH_H
00045 # define TPL_DYNLINHASH_H
00046 
00047 
00048 # include <tpl_linHash.H>
00049 
00050 namespace Aleph 
00051 {
00052 
00068     template <typename Key, typename Data>
00069 class DynLinearHashTable : public LinearHashTable<Key>
00070 {
00071   struct Bucket : public LinearHashTable<Key>::Bucket
00072   {
00073     Data data;
00074 
00075     Bucket() {}
00076 
00077     Bucket(const Key & key, const Data & d) 
00078       :  LinearHashTable<Key>::Bucket(key), data(d) { /* empty */ }
00079   };
00080 
00081 public:
00082 
00084   typedef typename LinearHashTable<Key>::Hash_Fct Hash_Fct;
00085 
00096   DynLinearHashTable(Hash_Fct       hash_fct, 
00097                const size_t & len = DefaultPrime,
00098                const float &  lower_alpha = 0.5,
00099                const float &  upper_alpha = 0.8) 
00100     : LinearHashTable<Key>(hash_fct, len, lower_alpha, upper_alpha)
00101   {
00102     // empty
00103   }
00104 
00108   Data * insert(const Key & key, const Data & record)
00109     throw (std::exception, std::bad_alloc)
00110   {
00111     Bucket * bucket = new Bucket (key, record);
00112 
00113     LinearHashTable<Key>::insert(bucket);
00114 
00115     return &bucket->data;
00116   }
00117 
00121   Data * search(const Key & key)
00122   {
00123     Bucket * bucket = static_cast<Bucket*>(LinearHashTable<Key>::search(key));
00124 
00125     return bucket != NULL ? &bucket->data : NULL;
00126   }
00127 
00128 private:
00129 
00130   static Bucket * record_to_bucket(Data * rec)
00131   {
00132     Bucket * ret_val = 0;
00133     size_t offset = reinterpret_cast<size_t>(&ret_val->data);
00134     
00135     return reinterpret_cast<Bucket*>(reinterpret_cast<size_t>(rec) - offset);
00136   }
00137 
00138 public:
00139 
00142   void remove(Data * record)
00143   {
00144     Bucket * bucket = record_to_bucket(record);
00145 
00146     LinearHashTable<Key>::remove(bucket);
00147 
00148     delete bucket;
00149   }
00150 };
00151 
00152 } // end namespace Aleph
00153 
00154 # endif // TPL_DYNLINHASH_H

Leandro R. León