summaryrefslogtreecommitdiffstats
path: root/flow/gsl/gslglibhash.cc
blob: c933929177b89f939929af13b19f3e5d3596025f (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
/* GSL Glib Hashtable implementation
 * Copyright (C) 2001 Stefan Westerfeld
 *
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
 * version 2 of the License, or (at your option) any later version.
 *
 * This library is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General
 * Public License along with this library; if not, write to the
 * Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
 * Boston, MA 02110-1301, USA.
 */
#include "gslglib.h"
#include <list>
#include <map>

using std::list;
using std::pair;
using std::map;

/*
 * this uses a map of list to emulate somewhat hashtable like behaviour - note
 * that insert and remove are O(log N) due to the use of a map, instead of
 * ~ O(1) which they would be for a "real" hashtable
 */
struct _GHashTable
{
  GHashFunc hashFunc;
  GEqualFunc equalFunc;
  map<guint /*hashvalue for key*/, list< pair<gpointer,gpointer> > /*(key,value) pairs*/> nodes;

  /*Con*/ _GHashTable (GHashFunc hf, GEqualFunc ef) :
    hashFunc(hf), equalFunc(ef) { }
};


GHashTable* g_hash_table_new               (GHashFunc       hash_func,
					    GEqualFunc      key_equal_func)
{
  return new GHashTable (hash_func?hash_func:g_direct_hash, key_equal_func?key_equal_func:g_direct_equal);
}
void        g_hash_table_destroy           (GHashTable     *hash_table)
{
  g_return_if_fail (hash_table != NULL);

  delete hash_table;
}
void        g_hash_table_insert            (GHashTable     *hash_table,
					    gpointer        key,
					    gpointer        value)
{
  g_return_if_fail (hash_table != NULL);
  guint hashvalue = hash_table->hashFunc (key);

  list< pair<gpointer,gpointer> >& bucket = hash_table->nodes[hashvalue];
  list< pair<gpointer,gpointer> >::iterator i;
  
  for (i = bucket.begin(); i != bucket.end(); i++)
    {
      if (hash_table->equalFunc(i->first, key))
      	{
	  if (value || TRUE)
	    {
	      i->second = value; /* overwrite old hash value */
	      return;
	    }
	  else
	    {
	      bucket.erase(i); /* remove value */

	      if (bucket.empty()) /* remove bucket if this was the only value */
	        hash_table->nodes.erase (hashvalue);
	      return;
	    }
	}
    }

  if (value)
    hash_table->nodes[hashvalue].push_back(std::make_pair (key, value));
}

gpointer    g_hash_table_lookup            (GHashTable     *hash_table,
					    gconstpointer   key)
{
  g_return_val_if_fail (hash_table != NULL, NULL);

  guint hashvalue = hash_table->hashFunc (key);

  list< pair<gpointer,gpointer> >& bucket = hash_table->nodes[hashvalue];
  list< pair<gpointer,gpointer> >::iterator i;
  
  for (i = bucket.begin(); i != bucket.end(); i++)
    {
      if (hash_table->equalFunc(i->first, key))
      	return i->second;
    }

  return 0;
}
gboolean    g_hash_table_remove            (GHashTable     *hash_table,
					    gconstpointer   key)
{
  g_return_val_if_fail (hash_table != NULL, FALSE);

  guint hashvalue = hash_table->hashFunc (key);

  list< pair<gpointer,gpointer> >& bucket = hash_table->nodes[hashvalue];
  list< pair<gpointer,gpointer> >::iterator i;
  
  for (i = bucket.begin(); i != bucket.end(); i++)
    {
      if (hash_table->equalFunc(i->first, key))
      	{
      	  bucket.erase (i);

	  if (bucket.empty()) /* remove bucket if this was the only value */
	    hash_table->nodes.erase (hashvalue);
	  return true;
	}
    }

  return false;
}

void        g_hash_table_foreach           (GHashTable     *hash_table,
					    GHFunc          func,
					    gpointer        user_data)
{
  map<guint, list< pair<gpointer,gpointer> > >::iterator bi;

  g_return_if_fail (hash_table != NULL);

  /* for all buckets */
  for (bi = hash_table->nodes.begin (); bi != hash_table->nodes.end (); bi++)
    {
      list< pair<gpointer,gpointer> >& bucket = bi->second;
      list< pair<gpointer,gpointer> >::iterator i;
 
      /* for each element in the current bucket */
      for (i = bucket.begin(); i != bucket.end(); i++)
        func ((void*) i->first, (void*) i->second, user_data);
   }
}