00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
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, class T> 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