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;
48 GMemChunk *tuple_chunk;
61 tuple_equal_2 (gconstpointer v_a,
64 gpointer* a = (gpointer*) v_a;
65 gpointer* b = (gpointer*) v_b;
67 return a[0] == b[0] && a[1] == b[1];
71 tuple_hash_2 (gconstpointer v_a)
73 gpointer* a = (gpointer*) v_a;
75 return (gulong)a[0] ^ (gulong)a[1];
79 tuple_hash (gint fields)
86 g_error ("no tuple hash for %d", fields);
93 tuple_equal (gint fields)
100 g_error ("no tuple equal for %d", fields);
107 g_relation_new (gint fields)
109 GRelation* rel = g_new0 (GRelation, 1);
111 rel->fields = fields;
112 rel->tuple_chunk = g_mem_chunk_new ("Relation Chunk",
113 fields * sizeof (gpointer),
114 fields * sizeof (gpointer) * 128,
116 rel->all_tuples = g_hash_table_new (tuple_hash (fields), tuple_equal (fields));
117 rel->hashed_tuple_tables = g_new0 (GHashTable*, fields);
123 g_relation_free_array (gpointer key, gpointer value, gpointer user_data)
125 g_hash_table_destroy ((GHashTable*) value);
129 g_relation_destroy (GRelation *relation)
135 g_hash_table_destroy (relation->all_tuples);
136 g_mem_chunk_destroy (relation->tuple_chunk);
138 for (i = 0; i < relation->fields; i += 1)
140 if (relation->hashed_tuple_tables[i])
142 g_hash_table_foreach (relation->hashed_tuple_tables[i], g_relation_free_array, NULL);
143 g_hash_table_destroy (relation->hashed_tuple_tables[i]);
147 g_free (relation->hashed_tuple_tables);
153 g_relation_index (GRelation *relation,
156 GEqualFunc key_equal_func)
158 g_return_if_fail (relation != NULL);
160 g_return_if_fail (relation->count == 0 && relation->hashed_tuple_tables[field] == NULL);
162 relation->hashed_tuple_tables[field] = g_hash_table_new (hash_func, key_equal_func);
166 g_relation_insert (GRelation *relation,
169 gpointer* tuple = g_chunk_new (gpointer, relation->tuple_chunk);
173 va_start(args, relation);
175 for (i = 0; i < relation->fields; i += 1)
176 tuple[i] = va_arg(args, gpointer);
180 g_hash_table_insert (relation->all_tuples, tuple, tuple);
182 relation->count += 1;
184 for (i = 0; i < relation->fields; i += 1)
188 GHashTable *per_key_table;
190 table = relation->hashed_tuple_tables[i];
196 per_key_table = g_hash_table_lookup (table, key);
198 if (per_key_table == NULL)
200 per_key_table = g_hash_table_new (tuple_hash (relation->fields), tuple_equal (relation->fields));
201 g_hash_table_insert (table, key, per_key_table);
204 g_hash_table_insert (per_key_table, tuple, tuple);
209 g_relation_delete_tuple (gpointer tuple_key,
210 gpointer tuple_value,
213 gpointer *tuple = (gpointer*) tuple_value;
214 GRelation *rel = (GRelation *) user_data;
217 g_assert (tuple_key == tuple_value);
219 for (j = 0; j < rel->fields; j += 1)
221 GHashTable *one_table = rel->hashed_tuple_tables[j];
223 GHashTable *per_key_table;
225 if (one_table == NULL)
228 if (j == rel->current_field)
229 /* can't delete from the table we're foreaching in */
234 per_key_table = g_hash_table_lookup (one_table, one_key);
236 g_hash_table_remove (per_key_table, tuple);
239 g_hash_table_remove (rel->all_tuples, tuple);
245 g_relation_delete (GRelation *relation,
249 GHashTable *table = relation->hashed_tuple_tables[field];
250 GHashTable *key_table;
251 gint count = relation->count;
253 g_return_val_if_fail (relation != NULL, 0);
254 g_return_val_if_fail (table != NULL, 0);
256 key_table = g_hash_table_lookup (table, key);
261 relation->current_field = field;
263 g_hash_table_foreach (key_table, g_relation_delete_tuple, relation);
265 g_hash_table_remove (table, key);
267 g_hash_table_destroy (key_table);
269 /* @@@ FIXME: Remove empty hash tables. */
271 return count - relation->count;
275 g_relation_select_tuple (gpointer tuple_key,
276 gpointer tuple_value,
279 gpointer *tuple = (gpointer*) tuple_value;
280 GRealTuples *tuples = (GRealTuples*) user_data;
281 gint stride = sizeof (gpointer) * tuples->width;
283 g_assert (tuple_key == tuple_value);
285 memcpy (tuples->data + (tuples->len * tuples->width),
293 g_relation_select (GRelation *relation,
297 GHashTable *table = relation->hashed_tuple_tables[field];
298 GHashTable *key_table;
299 GRealTuples *tuples = g_new0 (GRealTuples, 1);
302 g_return_val_if_fail (relation != NULL, NULL);
303 g_return_val_if_fail (table != NULL, NULL);
305 key_table = g_hash_table_lookup (table, key);
308 return (GTuples*)tuples;
310 count = g_relation_count (relation, key, field);
312 tuples->data = g_malloc (sizeof (gpointer) * relation->fields * count);
313 tuples->width = relation->fields;
315 g_hash_table_foreach (key_table, g_relation_select_tuple, tuples);
317 g_assert (count == tuples->len);
319 return (GTuples*)tuples;
323 g_relation_count (GRelation *relation,
327 GHashTable *table = relation->hashed_tuple_tables[field];
328 GHashTable *key_table;
330 g_return_val_if_fail (relation != NULL, 0);
331 g_return_val_if_fail (table != NULL, 0);
333 key_table = g_hash_table_lookup (table, key);
338 return g_hash_table_size (key_table);
342 g_relation_exists (GRelation *relation, ...)
344 gpointer* tuple = g_chunk_new (gpointer, relation->tuple_chunk);
349 va_start(args, relation);
351 for (i = 0; i < relation->fields; i += 1)
352 tuple[i] = va_arg(args, gpointer);
356 result = g_hash_table_lookup (relation->all_tuples, tuple) != NULL;
358 g_mem_chunk_free (relation->tuple_chunk, tuple);
364 g_tuples_destroy (GTuples *tuples0)
366 GRealTuples *tuples = (GRealTuples*) tuples0;
370 g_free (tuples->data);
376 g_tuples_index (GTuples *tuples0,
380 GRealTuples *tuples = (GRealTuples*) tuples0;
382 g_return_val_if_fail (tuples0 != NULL, NULL);
383 g_return_val_if_fail (field < tuples->width, NULL);
385 return tuples->data[index * tuples->width + field];
392 g_relation_print_one (gpointer tuple_key,
393 gpointer tuple_value,
398 GRelation* rel = (GRelation*) user_data;
399 gpointer* tuples = (gpointer*) tuple_value;
401 gstring = g_string_new ("[");
403 for (i = 0; i < rel->fields; i += 1)
405 g_string_append_printf (gstring, "%p", tuples[i]);
407 if (i < (rel->fields - 1))
408 g_string_append (gstring, ",");
411 g_string_append (gstring, "]");
412 g_log (G_LOG_DOMAIN, G_LOG_LEVEL_INFO, gstring->str);
413 g_string_free (gstring, TRUE);
417 g_relation_print_index (gpointer tuple_key,
418 gpointer tuple_value,
421 GRelation* rel = (GRelation*) user_data;
422 GHashTable* table = (GHashTable*) tuple_value;
424 g_log (G_LOG_DOMAIN, G_LOG_LEVEL_INFO, "*** key %p", tuple_key);
426 g_hash_table_foreach (table,
427 g_relation_print_one,
432 g_relation_print (GRelation *relation)
436 g_log (G_LOG_DOMAIN, G_LOG_LEVEL_INFO, "*** all tuples (%d)", relation->count);
438 g_hash_table_foreach (relation->all_tuples,
439 g_relation_print_one,
442 for (i = 0; i < relation->fields; i += 1)
444 if (relation->hashed_tuple_tables[i] == NULL)
447 g_log (G_LOG_DOMAIN, G_LOG_LEVEL_INFO, "*** index %d", i);
449 g_hash_table_foreach (relation->hashed_tuple_tables[i],
450 g_relation_print_index,