File: fo-hash-table.c

package info (click to toggle)
xmlroff 0.6.2-1
  • links: PTS, VCS
  • area: main
  • in suites: squeeze
  • size: 36,456 kB
  • ctags: 25,964
  • sloc: ansic: 178,200; xml: 109,155; sh: 8,973; makefile: 1,331; perl: 30
file content (321 lines) | stat: -rw-r--r-- 7,932 bytes parent folder | download | duplicates (5)
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
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
/* Fo
 * fo-hashtable.c: HashTable hashtable
 *
 * Copyright (C) 2001 Sun Microsystems
 * Copyright (C) 2007 Menteith Consulting Ltd
 *
 * See COPYING for the status of this software.
 */

#include <math.h>
#include "fo-utils.h"
#include "fo-hash-table.h"

struct _FoHashTable
{
  FoObject parent_instance;

  GHashTable *hash_table;
};

struct _FoHashTableClass
{
  FoObjectClass parent_class;
};

static void     fo_hash_table_class_init         (FoHashTableClass *klass);
static void     fo_hash_table_finalize           (GObject          *object);

static guint    fo_hash_table_hash_func          (gconstpointer     key);
static gboolean fo_hash_table_key_equal_func     (gconstpointer     a,
						  gconstpointer     b);
static void     fo_hash_table_destroy_func   (gpointer data);

static gpointer parent_class;

/**
 * fo_hash_table_get_type:
 * 
 * Register the #FoHashTable object type.
 * 
 * Return value: #GType value of the #FoHashTable object type.
 **/
GType
fo_hash_table_get_type (void)
{
  static GType object_type = 0;

  if (!object_type)
    {
      static const GTypeInfo object_info =
      {
        sizeof (FoHashTableClass),
        (GBaseInitFunc) NULL,
        (GBaseFinalizeFunc) NULL,
        (GClassInitFunc) fo_hash_table_class_init,
        NULL,           /* class_finalize */
        NULL,           /* class_data */
        sizeof (FoHashTable),
        0,              /* n_preallocs */
        NULL,		/* instance_init */
	NULL		/* value_table */
      };
      
      object_type = g_type_register_static (FO_TYPE_OBJECT,
                                            "FoHashTable",
                                            &object_info,
					    0);
    }
  
  return object_type;
}

/**
 * fo_hash_table_class_init:
 * @klass: #FoHashTableClass object to initialise.
 * 
 * Implements #GClassInitFunc for #FoHashTableClass.
 **/
void
fo_hash_table_class_init (FoHashTableClass *klass)
{
  GObjectClass *object_class = G_OBJECT_CLASS (klass);
  
  parent_class = g_type_class_peek_parent (klass);
  
  object_class->finalize = fo_hash_table_finalize;
}

/**
 * fo_hash_table_finalize:
 * @object: #FoHashTable object to finalize.
 * 
 * Implements #GObjectFinalizeFunc for #FoHashTable.
 **/
void
fo_hash_table_finalize (GObject *object)
{
  FoHashTable *fo_hash_table;

  fo_hash_table = FO_HASH_TABLE (object);

  g_hash_table_destroy(fo_hash_table->hash_table);

  G_OBJECT_CLASS (parent_class)->finalize (object);
}

/**
 * fo_hash_table_new:
 * 
 * Creates a new #FoHashTable initialized to default value.
 * 
 * Return value: the new #FoHashTable
 **/
FoHashTable *
fo_hash_table_new (void)
{
  FoHashTable *fo_hash_table;

  fo_hash_table = FO_HASH_TABLE (g_object_new (fo_hash_table_get_type (), NULL));
  
  fo_hash_table->hash_table = g_hash_table_new_full(fo_hash_table_hash_func,
						    fo_hash_table_key_equal_func,
						    fo_hash_table_destroy_func,
						    fo_hash_table_destroy_func);

  return fo_hash_table;
}

/**
 * fo_hash_table_destroy_func:
 * @data: Data to be destroyed.
 * 
 * #GDestroyNotify function called when a key or value is removed from
 * a #FoHashTable.
 **/
void
fo_hash_table_destroy_func (gpointer data)
{
  if ((data != NULL) &&
      FO_IS_OBJECT (data))
      {
	  g_object_unref(data);
      }
}

/**
 * fo_hash_table_hash_func:
 * @key: Key to hash.
 * 
 * Creates the hash code for @key using the 'hash' function for the
 * #FoObject class type of @key.
 * 
 * Return value: Hash code for @key.
 **/
guint
fo_hash_table_hash_func   (gconstpointer     key)
{
    guint result = 0;

  if ((key != NULL) &&
      (FO_IS_OBJECT (key)))
  {
      result = FO_OBJECT_GET_CLASS (key)->hash_func (key);
  }

      return result;
}

/**
 * fo_hash_table_key_equal_func:
 * @a: First key to compare.
 * @b: Second key to compare.
 * 
 * Compares @a and @b using the 'equal' function of the #FoObject
 * class type of @a.
 * 
 * Return value: %TRUE if @a and @b are equal.
 **/
gboolean
fo_hash_table_key_equal_func  (gconstpointer     a,
			       gconstpointer     b)
{
    return FO_OBJECT_GET_CLASS (a)->equal_func (a, b);
}

/**
 * fo_hash_table_insert:
 * @fo_hash_table: #FoHashTable in which to insert.
 * @key:           Key at which to insert.
 * @value:         Value to insert.
 * @error:         #GError with information about error that occurred.
 * 
 * Inserts @value as the value corresponding to @key in
 * @fo_hash_table.
 **/
void
fo_hash_table_insert (FoHashTable *fo_hash_table,
		      FoObject    *key,
		      FoObject    *value,
		      GError     **error G_GNUC_UNUSED)
{
  g_return_if_fail (fo_hash_table != NULL);
  g_return_if_fail (FO_IS_HASH_TABLE (fo_hash_table));
  g_return_if_fail (key != NULL);
  g_return_if_fail (FO_IS_OBJECT (value));

  g_hash_table_insert (fo_hash_table->hash_table,
		       g_object_ref(key),
		       g_object_ref(value));
}

/**
 * fo_hash_table_replace:
 * @fo_hash_table: #FoHashTable in which to replace.
 * @key:           Key of key-value pair to replace.
 * @value:         New value.
 * @error:         #GError with information about error that occurred.
 * 
 * Replaces the key-value pair in @fo_hash_table that has a key
 * matching @key.
 **/
void
fo_hash_table_replace (FoHashTable *fo_hash_table,
		       FoObject    *key,
		       FoObject    *value,
		       GError     **error G_GNUC_UNUSED)
{
  g_return_if_fail (fo_hash_table != NULL);
  g_return_if_fail (FO_IS_HASH_TABLE (fo_hash_table));
  g_return_if_fail (key != NULL);
  g_return_if_fail (FO_IS_OBJECT (key));

  g_hash_table_replace (fo_hash_table->hash_table,
			g_object_ref(key),
			g_object_ref(value));
}

/**
 * fo_hash_table_remove:
 * @fo_hash_table: #FoHashTable from which to remove a key-value pair.
 * @key:           Key of pair to remove.
 * @error:         #GError with information about error that occurred.
 * 
 * Removes the key-value pair with key matching @key from
 * @fo_hash_table.
 * 
 * Return value: %TRUE is successful.
 **/
gboolean
fo_hash_table_remove (FoHashTable *fo_hash_table,
		      FoObject   *key,
		      GError   **error G_GNUC_UNUSED)
{
  g_return_val_if_fail (fo_hash_table != NULL, FALSE);
  g_return_val_if_fail (FO_IS_HASH_TABLE (fo_hash_table), FALSE);
  g_return_val_if_fail (key != NULL, FALSE);
  g_return_val_if_fail (FO_IS_OBJECT (key), FALSE);

  return g_hash_table_remove (fo_hash_table->hash_table,
			      g_object_ref(key));
}

/**
 * fo_hash_table_lookup:
 * @fo_hash_table: #FoHashTable in which to look.
 * @key:           Key value against which to compare.
 * @error:         #GError with information about error that occurred.
 * 
 * Gets the value associated with @key in @fo_hash_table.
 * 
 * Return value: The value, or %NULL if no matching key.
 **/
FoObject *
fo_hash_table_lookup (FoHashTable *fo_hash_table,
		      FoObject    *key,
		      GError     **error G_GNUC_UNUSED)
{
  g_return_val_if_fail (fo_hash_table != NULL, NULL);
  g_return_val_if_fail (FO_IS_HASH_TABLE (fo_hash_table), NULL);
  g_return_val_if_fail (key != NULL, NULL);
  g_return_val_if_fail (FO_IS_OBJECT (key), NULL);

  return g_hash_table_lookup (fo_hash_table->hash_table,
			      key);
}

/**
 * fo_hash_table_size:
 * @fo_hash_table: #FoHashTable for which to get size.
 * 
 * Gets the number of key-value pairs in @fo_hash_table.
 * 
 * Return value: Number of key-value pairs.
 **/
guint
fo_hash_table_size (FoHashTable *fo_hash_table)
{
    return g_hash_table_size(fo_hash_table->hash_table);
}

/**
 * fo_hash_table_error_quark:
 * 
 * Get the error quark for #FoHashTable.
 *
 * If the quark does not yet exist, create it.
 * 
 * Return value: Quark associated with #FoHashTable errors.
 **/
GQuark
fo_hash_table_error_quark (void)
{
  static GQuark quark = 0;
  if (quark == 0)
    quark = g_quark_from_static_string ("FoHashTable error");
  return quark;
}

const char *fo_hash_table_error_messages [] = {
  N_("Invalid hash_table for context."),
};