general/hashtable.h

Go to the documentation of this file.
00001 /* $Id:hashtable.h jjs $ */
00002 /*
00003  * ggcov - A GTK frontend for exploring gcov coverage data
00004  * Copyright (c) 2003-2004 Greg Banks <gnb@alphalink.com.au>
00005  *
00006  * This program is free software; you can redistribute it and/or modify
00007  * it under the terms of the GNU General Public License as published by
00008  * the Free Software Foundation; either version 2 of the License, or
00009  * (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
00018  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
00019  */
00020 
00021 #ifndef __HASHTABLE_H
00022 #define __HASHTABLE_H
00023 
00024 #include <glib.h>
00025 
00027 
00028 template<class K> class hashtable_ops_t {
00029 public:
00030     static GHashFunc hash;      
00031     static GCompareFunc compare;
00032 };
00033 
00034 template<> GHashFunc hashtable_ops_t<char>::hash;
00035 template<> GCompareFunc hashtable_ops_t<char>::compare;
00036 
00037 template<> GHashFunc hashtable_ops_t<const char>::hash;
00038 template<> GCompareFunc hashtable_ops_t<const char>::compare;
00039 
00040 template<> GHashFunc hashtable_ops_t<void>::hash;
00041 template<> GCompareFunc hashtable_ops_t<void>::compare;
00042 
00044 
00045 template<class K/*key*/, class T/*item*/> class hashtable {
00046   private:
00047     GHashTable* table;  
00048 
00049   public:
00051     hashtable(int size) {
00052         table = g_hash_table_new(
00053             hashtable_ops_t<K>::hash,
00054             hashtable_ops_t<K>::compare);
00055     }
00056 
00058     ~hashtable() {
00059         g_hash_table_destroy(table);
00060     }
00062 
00065     void insert(K* key, T* value) {
00066         g_hash_table_insert(table, (gpointer)key, (gpointer)value);
00067     }
00068 
00070     void remove(K* key) {
00071         g_hash_table_remove(table, (gconstpointer)key);
00072     }
00073 
00075     T *operator[](K* key) {
00076         return (T*)g_hash_table_lookup(table, (gconstpointer)key);
00077     }
00078 
00080     bool lookup_extended(K* key, K** orig_key_ret, T **value_ret) {
00081         return g_hash_table_lookup_extended(table, (gconstpointer)key,
00082                     (gpointer *)orig_key_ret, (gpointer *)value_ret);
00083     }
00084 
00086     void foreach(void (*func)(K*, T*, void *closure), void *closure) {
00087         g_hash_table_foreach(table, (GHFunc)*func, closure);
00088     }
00089 
00091     void foreach_remove(gboolean (*func)(K*, T*, void *closure), void *closure) {
00092         g_hash_table_foreach_remove(table, (GHRFunc)func, closure);
00093     }
00094 
00096     int size() const {
00097         return g_hash_table_size((GHashTable*)this);
00098     }
00099 };
00100 
00101 #endif // __HASHTABLE_H


Generated on Tue Jan 1 17:30:00 2008 for general/hashtable.h Source File by  doxygen   Visit the project page on SourceForge.net Logo