1 /* GLIB - Library of useful routines for C programming
2 * Copyright (C) 1995-1997 Peter Mattis, Spencer Kimball and Josh MacDonald
4 * This library is free software; you can redistribute it and/or
5 * modify it under the terms of the GNU Lesser General Public
6 * License as published by the Free Software Foundation; either
7 * version 2 of the License, or (at your option) any later version.
9 * This library is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 * Lesser General Public License for more details.
14 * You should have received a copy of the GNU Lesser General Public
15 * License along with this library; if not, write to the Free
16 * Software Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
20 * Modified by the GLib Team and others 1997-2000. See the AUTHORS
21 * file for a list of people on the GLib Team. See the ChangeLog
22 * files for a list of changes. These files are distributed with
23 * GLib at ftp://ftp.gtk.org/pub/gtk/.
39 typedef struct _GRealTuples GRealTuples;
46 GHashTable *all_tuples;
47 GHashTable **hashed_tuple_tables;
60 tuple_equal_2 (gconstpointer v_a,
63 gpointer* a = (gpointer*) v_a;
64 gpointer* b = (gpointer*) v_b;
66 return a[0] == b[0] && a[1] == b[1];
70 tuple_hash_2 (gconstpointer v_a)
72 gpointer* a = (gpointer*) v_a;
74 return (gulong)a[0] ^ (gulong)a[1];
78 tuple_hash (gint fields)
85 g_error ("no tuple hash for %d", fields);
92 tuple_equal (gint fields)
99 g_error ("no tuple equal for %d", fields);
106 g_relation_new (gint fields)
108 GRelation* rel = g_new0 (GRelation, 1);
110 rel->fields = fields;
111 rel->all_tuples = g_hash_table_new (tuple_hash (fields), tuple_equal (fields));
112 rel->hashed_tuple_tables = g_new0 (GHashTable*, fields);
118 relation_delete_value_tuple (gpointer tuple_key,
119 gpointer tuple_value,
122 GRelation *relation = user_data;
123 gpointer *tuple = tuple_value;
124 g_slice_free1 (relation->fields * sizeof (gpointer), tuple);
128 g_relation_free_array (gpointer key, gpointer value, gpointer user_data)
130 g_hash_table_destroy ((GHashTable*) value);
134 g_relation_destroy (GRelation *relation)
140 for (i = 0; i < relation->fields; i += 1)
142 if (relation->hashed_tuple_tables[i])
144 g_hash_table_foreach (relation->hashed_tuple_tables[i], g_relation_free_array, NULL);
145 g_hash_table_destroy (relation->hashed_tuple_tables[i]);
149 g_hash_table_foreach (relation->all_tuples, relation_delete_value_tuple, relation);
150 g_hash_table_destroy (relation->all_tuples);
152 g_free (relation->hashed_tuple_tables);
158 g_relation_index (GRelation *relation,
161 GEqualFunc key_equal_func)
163 g_return_if_fail (relation != NULL);
165 g_return_if_fail (relation->count == 0 && relation->hashed_tuple_tables[field] == NULL);
167 relation->hashed_tuple_tables[field] = g_hash_table_new (hash_func, key_equal_func);
171 g_relation_insert (GRelation *relation,
174 gpointer* tuple = g_slice_alloc (relation->fields * sizeof (gpointer));
178 va_start (args, relation);
180 for (i = 0; i < relation->fields; i += 1)
181 tuple[i] = va_arg (args, gpointer);
185 g_hash_table_insert (relation->all_tuples, tuple, tuple);
187 relation->count += 1;
189 for (i = 0; i < relation->fields; i += 1)
193 GHashTable *per_key_table;
195 table = relation->hashed_tuple_tables[i];
201 per_key_table = g_hash_table_lookup (table, key);
203 if (per_key_table == NULL)
205 per_key_table = g_hash_table_new (tuple_hash (relation->fields), tuple_equal (relation->fields));
206 g_hash_table_insert (table, key, per_key_table);
209 g_hash_table_insert (per_key_table, tuple, tuple);
214 g_relation_delete_tuple (gpointer tuple_key,
215 gpointer tuple_value,
218 gpointer *tuple = (gpointer*) tuple_value;
219 GRelation *relation = (GRelation *) user_data;
222 g_assert (tuple_key == tuple_value);
224 for (j = 0; j < relation->fields; j += 1)
226 GHashTable *one_table = relation->hashed_tuple_tables[j];
228 GHashTable *per_key_table;
230 if (one_table == NULL)
233 if (j == relation->current_field)
234 /* can't delete from the table we're foreaching in */
239 per_key_table = g_hash_table_lookup (one_table, one_key);
241 g_hash_table_remove (per_key_table, tuple);
244 if (g_hash_table_remove (relation->all_tuples, tuple))
245 g_slice_free1 (relation->fields * sizeof (gpointer), tuple);
247 relation->count -= 1;
251 g_relation_delete (GRelation *relation,
256 GHashTable *key_table;
259 g_return_val_if_fail (relation != NULL, 0);
261 table = relation->hashed_tuple_tables[field];
262 count = relation->count;
264 g_return_val_if_fail (table != NULL, 0);
266 key_table = g_hash_table_lookup (table, key);
271 relation->current_field = field;
273 g_hash_table_foreach (key_table, g_relation_delete_tuple, relation);
275 g_hash_table_remove (table, key);
277 g_hash_table_destroy (key_table);
279 /* @@@ FIXME: Remove empty hash tables. */
281 return count - relation->count;
285 g_relation_select_tuple (gpointer tuple_key,
286 gpointer tuple_value,
289 gpointer *tuple = (gpointer*) tuple_value;
290 GRealTuples *tuples = (GRealTuples*) user_data;
291 gint stride = sizeof (gpointer) * tuples->width;
293 g_assert (tuple_key == tuple_value);
295 memcpy (tuples->data + (tuples->len * tuples->width),
303 g_relation_select (GRelation *relation,
308 GHashTable *key_table;
312 g_return_val_if_fail (relation != NULL, NULL);
314 table = relation->hashed_tuple_tables[field];
316 g_return_val_if_fail (table != NULL, NULL);
318 tuples = g_new0 (GRealTuples, 1);
319 key_table = g_hash_table_lookup (table, key);
322 return (GTuples*)tuples;
324 count = g_relation_count (relation, key, field);
326 tuples->data = g_malloc (sizeof (gpointer) * relation->fields * count);
327 tuples->width = relation->fields;
329 g_hash_table_foreach (key_table, g_relation_select_tuple, tuples);
331 g_assert (count == tuples->len);
333 return (GTuples*)tuples;
337 g_relation_count (GRelation *relation,
342 GHashTable *key_table;
344 g_return_val_if_fail (relation != NULL, 0);
346 table = relation->hashed_tuple_tables[field];
348 g_return_val_if_fail (table != NULL, 0);
350 key_table = g_hash_table_lookup (table, key);
355 return g_hash_table_size (key_table);
359 g_relation_exists (GRelation *relation, ...)
361 gpointer *tuple = g_slice_alloc (relation->fields * sizeof (gpointer));
366 va_start(args, relation);
368 for (i = 0; i < relation->fields; i += 1)
369 tuple[i] = va_arg(args, gpointer);
373 result = g_hash_table_lookup (relation->all_tuples, tuple) != NULL;
375 g_slice_free1 (relation->fields * sizeof (gpointer), tuple);
381 g_tuples_destroy (GTuples *tuples0)
383 GRealTuples *tuples = (GRealTuples*) tuples0;
387 g_free (tuples->data);
393 g_tuples_index (GTuples *tuples0,
397 GRealTuples *tuples = (GRealTuples*) tuples0;
399 g_return_val_if_fail (tuples0 != NULL, NULL);
400 g_return_val_if_fail (field < tuples->width, NULL);
402 return tuples->data[index * tuples->width + field];
409 g_relation_print_one (gpointer tuple_key,
410 gpointer tuple_value,
415 GRelation* rel = (GRelation*) user_data;
416 gpointer* tuples = (gpointer*) tuple_value;
418 gstring = g_string_new ("[");
420 for (i = 0; i < rel->fields; i += 1)
422 g_string_append_printf (gstring, "%p", tuples[i]);
424 if (i < (rel->fields - 1))
425 g_string_append (gstring, ",");
428 g_string_append (gstring, "]");
429 g_log (G_LOG_DOMAIN, G_LOG_LEVEL_INFO, gstring->str);
430 g_string_free (gstring, TRUE);
434 g_relation_print_index (gpointer tuple_key,
435 gpointer tuple_value,
438 GRelation* rel = (GRelation*) user_data;
439 GHashTable* table = (GHashTable*) tuple_value;
441 g_log (G_LOG_DOMAIN, G_LOG_LEVEL_INFO, "*** key %p", tuple_key);
443 g_hash_table_foreach (table,
444 g_relation_print_one,
449 g_relation_print (GRelation *relation)
453 g_log (G_LOG_DOMAIN, G_LOG_LEVEL_INFO, "*** all tuples (%d)", relation->count);
455 g_hash_table_foreach (relation->all_tuples,
456 g_relation_print_one,
459 for (i = 0; i < relation->fields; i += 1)
461 if (relation->hashed_tuple_tables[i] == NULL)
464 g_log (G_LOG_DOMAIN, G_LOG_LEVEL_INFO, "*** index %d", i);
466 g_hash_table_foreach (relation->hashed_tuple_tables[i],
467 g_relation_print_index,
474 #include "galiasdef.c"