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/.
38 typedef struct _GRealTuples GRealTuples;
45 GHashTable *all_tuples;
46 GHashTable **hashed_tuple_tables;
47 GMemChunk *tuple_chunk;
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->tuple_chunk = g_mem_chunk_new ("Relation Chunk",
112 fields * sizeof (gpointer),
113 fields * sizeof (gpointer) * 128,
115 rel->all_tuples = g_hash_table_new (tuple_hash (fields), tuple_equal (fields));
116 rel->hashed_tuple_tables = g_new0 (GHashTable*, fields);
122 g_relation_free_array (gpointer key, gpointer value, gpointer user_data)
124 g_hash_table_destroy ((GHashTable*) value);
128 g_relation_destroy (GRelation *relation)
134 g_hash_table_destroy (relation->all_tuples);
135 g_mem_chunk_destroy (relation->tuple_chunk);
137 for (i = 0; i < relation->fields; i += 1)
139 if (relation->hashed_tuple_tables[i])
141 g_hash_table_foreach (relation->hashed_tuple_tables[i], g_relation_free_array, NULL);
142 g_hash_table_destroy (relation->hashed_tuple_tables[i]);
146 g_free (relation->hashed_tuple_tables);
152 g_relation_index (GRelation *relation,
155 GEqualFunc key_equal_func)
157 g_return_if_fail (relation != NULL);
159 g_return_if_fail (relation->count == 0 && relation->hashed_tuple_tables[field] == NULL);
161 relation->hashed_tuple_tables[field] = g_hash_table_new (hash_func, key_equal_func);
165 g_relation_insert (GRelation *relation,
168 gpointer* tuple = g_chunk_new (gpointer, relation->tuple_chunk);
172 va_start(args, relation);
174 for (i = 0; i < relation->fields; i += 1)
175 tuple[i] = va_arg(args, gpointer);
179 g_hash_table_insert (relation->all_tuples, tuple, tuple);
181 relation->count += 1;
183 for (i = 0; i < relation->fields; i += 1)
187 GHashTable *per_key_table;
189 table = relation->hashed_tuple_tables[i];
195 per_key_table = g_hash_table_lookup (table, key);
197 if (per_key_table == NULL)
199 per_key_table = g_hash_table_new (tuple_hash (relation->fields), tuple_equal (relation->fields));
200 g_hash_table_insert (table, key, per_key_table);
203 g_hash_table_insert (per_key_table, tuple, tuple);
208 g_relation_delete_tuple (gpointer tuple_key,
209 gpointer tuple_value,
212 gpointer *tuple = (gpointer*) tuple_value;
213 GRelation *rel = (GRelation *) user_data;
216 g_assert (tuple_key == tuple_value);
218 for (j = 0; j < rel->fields; j += 1)
220 GHashTable *one_table = rel->hashed_tuple_tables[j];
222 GHashTable *per_key_table;
224 if (one_table == NULL)
227 if (j == rel->current_field)
228 /* can't delete from the table we're foreaching in */
233 per_key_table = g_hash_table_lookup (one_table, one_key);
235 g_hash_table_remove (per_key_table, tuple);
238 g_hash_table_remove (rel->all_tuples, tuple);
244 g_relation_delete (GRelation *relation,
248 GHashTable *table = relation->hashed_tuple_tables[field];
249 GHashTable *key_table;
250 gint count = relation->count;
252 g_return_val_if_fail (relation != NULL, 0);
253 g_return_val_if_fail (table != NULL, 0);
255 key_table = g_hash_table_lookup (table, key);
260 relation->current_field = field;
262 g_hash_table_foreach (key_table, g_relation_delete_tuple, relation);
264 g_hash_table_remove (table, key);
266 g_hash_table_destroy (key_table);
268 /* @@@ FIXME: Remove empty hash tables. */
270 return count - relation->count;
274 g_relation_select_tuple (gpointer tuple_key,
275 gpointer tuple_value,
278 gpointer *tuple = (gpointer*) tuple_value;
279 GRealTuples *tuples = (GRealTuples*) user_data;
280 gint stride = sizeof (gpointer) * tuples->width;
282 g_assert (tuple_key == tuple_value);
284 memcpy (tuples->data + (tuples->len * tuples->width),
292 g_relation_select (GRelation *relation,
296 GHashTable *table = relation->hashed_tuple_tables[field];
297 GHashTable *key_table;
298 GRealTuples *tuples = g_new0 (GRealTuples, 1);
301 g_return_val_if_fail (relation != NULL, NULL);
302 g_return_val_if_fail (table != NULL, NULL);
304 key_table = g_hash_table_lookup (table, key);
307 return (GTuples*)tuples;
309 count = g_relation_count (relation, key, field);
311 tuples->data = g_malloc (sizeof (gpointer) * relation->fields * count);
312 tuples->width = relation->fields;
314 g_hash_table_foreach (key_table, g_relation_select_tuple, tuples);
316 g_assert (count == tuples->len);
318 return (GTuples*)tuples;
322 g_relation_count (GRelation *relation,
326 GHashTable *table = relation->hashed_tuple_tables[field];
327 GHashTable *key_table;
329 g_return_val_if_fail (relation != NULL, 0);
330 g_return_val_if_fail (table != NULL, 0);
332 key_table = g_hash_table_lookup (table, key);
337 return g_hash_table_size (key_table);
341 g_relation_exists (GRelation *relation, ...)
343 gpointer* tuple = g_chunk_new (gpointer, relation->tuple_chunk);
348 va_start(args, relation);
350 for (i = 0; i < relation->fields; i += 1)
351 tuple[i] = va_arg(args, gpointer);
355 result = g_hash_table_lookup (relation->all_tuples, tuple) != NULL;
357 g_mem_chunk_free (relation->tuple_chunk, tuple);
363 g_tuples_destroy (GTuples *tuples0)
365 GRealTuples *tuples = (GRealTuples*) tuples0;
369 g_free (tuples->data);
375 g_tuples_index (GTuples *tuples0,
379 GRealTuples *tuples = (GRealTuples*) tuples0;
381 g_return_val_if_fail (tuples0 != NULL, NULL);
382 g_return_val_if_fail (field < tuples->width, NULL);
384 return tuples->data[index * tuples->width + field];
391 g_relation_print_one (gpointer tuple_key,
392 gpointer tuple_value,
397 GRelation* rel = (GRelation*) user_data;
398 gpointer* tuples = (gpointer*) tuple_value;
400 gstring = g_string_new ("[");
402 for (i = 0; i < rel->fields; i += 1)
404 g_string_append_printf (gstring, "%p", tuples[i]);
406 if (i < (rel->fields - 1))
407 g_string_append (gstring, ",");
410 g_string_append (gstring, "]");
411 g_log (G_LOG_DOMAIN, G_LOG_LEVEL_INFO, gstring->str);
412 g_string_free (gstring, TRUE);
416 g_relation_print_index (gpointer tuple_key,
417 gpointer tuple_value,
420 GRelation* rel = (GRelation*) user_data;
421 GHashTable* table = (GHashTable*) tuple_value;
423 g_log (G_LOG_DOMAIN, G_LOG_LEVEL_INFO, "*** key %p", tuple_key);
425 g_hash_table_foreach (table,
426 g_relation_print_one,
431 g_relation_print (GRelation *relation)
435 g_log (G_LOG_DOMAIN, G_LOG_LEVEL_INFO, "*** all tuples (%d)", relation->count);
437 g_hash_table_foreach (relation->all_tuples,
438 g_relation_print_one,
441 for (i = 0; i < relation->fields; i += 1)
443 if (relation->hashed_tuple_tables[i] == NULL)
446 g_log (G_LOG_DOMAIN, G_LOG_LEVEL_INFO, "*** index %d", i);
448 g_hash_table_foreach (relation->hashed_tuple_tables[i],
449 g_relation_print_index,