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/.
34 typedef struct _GRealTuples GRealTuples;
41 GHashTable *all_tuples;
42 GHashTable **hashed_tuple_tables;
43 GMemChunk *tuple_chunk;
56 tuple_equal_2 (gconstpointer v_a,
59 gpointer* a = (gpointer*) v_a;
60 gpointer* b = (gpointer*) v_b;
62 return a[0] == b[0] && a[1] == b[1];
66 tuple_hash_2 (gconstpointer v_a)
68 gpointer* a = (gpointer*) v_a;
70 return (gulong)a[0] ^ (gulong)a[1];
74 tuple_hash (gint fields)
81 g_error ("no tuple hash for %d", fields);
88 tuple_equal (gint fields)
95 g_error ("no tuple equal for %d", fields);
102 g_relation_new (gint fields)
104 GRelation* rel = g_new0 (GRelation, 1);
106 rel->fields = fields;
107 rel->tuple_chunk = g_mem_chunk_new ("Relation Chunk",
108 fields * sizeof (gpointer),
109 fields * sizeof (gpointer) * 128,
111 rel->all_tuples = g_hash_table_new (tuple_hash (fields), tuple_equal (fields));
112 rel->hashed_tuple_tables = g_new0 (GHashTable*, fields);
118 g_relation_free_array (gpointer key, gpointer value, gpointer user_data)
120 g_hash_table_destroy ((GHashTable*) value);
124 g_relation_destroy (GRelation *relation)
130 g_hash_table_destroy (relation->all_tuples);
131 g_mem_chunk_destroy (relation->tuple_chunk);
133 for (i = 0; i < relation->fields; i += 1)
135 if (relation->hashed_tuple_tables[i])
137 g_hash_table_foreach (relation->hashed_tuple_tables[i], g_relation_free_array, NULL);
138 g_hash_table_destroy (relation->hashed_tuple_tables[i]);
142 g_free (relation->hashed_tuple_tables);
148 g_relation_index (GRelation *relation,
151 GEqualFunc key_equal_func)
153 g_return_if_fail (relation != NULL);
155 g_return_if_fail (relation->count == 0 && relation->hashed_tuple_tables[field] == NULL);
157 relation->hashed_tuple_tables[field] = g_hash_table_new (hash_func, key_equal_func);
161 g_relation_insert (GRelation *relation,
164 gpointer* tuple = g_chunk_new (gpointer, relation->tuple_chunk);
168 va_start(args, relation);
170 for (i = 0; i < relation->fields; i += 1)
171 tuple[i] = va_arg(args, gpointer);
175 g_hash_table_insert (relation->all_tuples, tuple, tuple);
177 relation->count += 1;
179 for (i = 0; i < relation->fields; i += 1)
183 GHashTable *per_key_table;
185 table = relation->hashed_tuple_tables[i];
191 per_key_table = g_hash_table_lookup (table, key);
193 if (per_key_table == NULL)
195 per_key_table = g_hash_table_new (tuple_hash (relation->fields), tuple_equal (relation->fields));
196 g_hash_table_insert (table, key, per_key_table);
199 g_hash_table_insert (per_key_table, tuple, tuple);
204 g_relation_delete_tuple (gpointer tuple_key,
205 gpointer tuple_value,
208 gpointer *tuple = (gpointer*) tuple_value;
209 GRelation *rel = (GRelation *) user_data;
212 g_assert (tuple_key == tuple_value);
214 for (j = 0; j < rel->fields; j += 1)
216 GHashTable *one_table = rel->hashed_tuple_tables[j];
218 GHashTable *per_key_table;
220 if (one_table == NULL)
223 if (j == rel->current_field)
224 /* can't delete from the table we're foreaching in */
229 per_key_table = g_hash_table_lookup (one_table, one_key);
231 g_hash_table_remove (per_key_table, tuple);
234 g_hash_table_remove (rel->all_tuples, tuple);
240 g_relation_delete (GRelation *relation,
244 GHashTable *table = relation->hashed_tuple_tables[field];
245 GHashTable *key_table;
246 gint count = relation->count;
248 g_return_val_if_fail (relation != NULL, 0);
249 g_return_val_if_fail (table != NULL, 0);
251 key_table = g_hash_table_lookup (table, key);
256 relation->current_field = field;
258 g_hash_table_foreach (key_table, g_relation_delete_tuple, relation);
260 g_hash_table_remove (table, key);
262 g_hash_table_destroy (key_table);
264 /* @@@ FIXME: Remove empty hash tables. */
266 return count - relation->count;
270 g_relation_select_tuple (gpointer tuple_key,
271 gpointer tuple_value,
274 gpointer *tuple = (gpointer*) tuple_value;
275 GRealTuples *tuples = (GRealTuples*) user_data;
276 gint stride = sizeof (gpointer) * tuples->width;
278 g_assert (tuple_key == tuple_value);
280 memcpy (tuples->data + (tuples->len * tuples->width),
288 g_relation_select (GRelation *relation,
292 GHashTable *table = relation->hashed_tuple_tables[field];
293 GHashTable *key_table;
294 GRealTuples *tuples = g_new0 (GRealTuples, 1);
297 g_return_val_if_fail (relation != NULL, NULL);
298 g_return_val_if_fail (table != NULL, NULL);
300 key_table = g_hash_table_lookup (table, key);
303 return (GTuples*)tuples;
305 count = g_relation_count (relation, key, field);
307 tuples->data = g_malloc (sizeof (gpointer) * relation->fields * count);
308 tuples->width = relation->fields;
310 g_hash_table_foreach (key_table, g_relation_select_tuple, tuples);
312 g_assert (count == tuples->len);
314 return (GTuples*)tuples;
318 g_relation_count (GRelation *relation,
322 GHashTable *table = relation->hashed_tuple_tables[field];
323 GHashTable *key_table;
325 g_return_val_if_fail (relation != NULL, 0);
326 g_return_val_if_fail (table != NULL, 0);
328 key_table = g_hash_table_lookup (table, key);
333 return g_hash_table_size (key_table);
337 g_relation_exists (GRelation *relation, ...)
339 gpointer* tuple = g_chunk_new (gpointer, relation->tuple_chunk);
344 va_start(args, relation);
346 for (i = 0; i < relation->fields; i += 1)
347 tuple[i] = va_arg(args, gpointer);
351 result = g_hash_table_lookup (relation->all_tuples, tuple) != NULL;
353 g_mem_chunk_free (relation->tuple_chunk, tuple);
359 g_tuples_destroy (GTuples *tuples0)
361 GRealTuples *tuples = (GRealTuples*) tuples0;
365 g_free (tuples->data);
371 g_tuples_index (GTuples *tuples0,
375 GRealTuples *tuples = (GRealTuples*) tuples0;
377 g_return_val_if_fail (tuples0 != NULL, NULL);
378 g_return_val_if_fail (field < tuples->width, NULL);
380 return tuples->data[index * tuples->width + field];
387 g_relation_print_one (gpointer tuple_key,
388 gpointer tuple_value,
393 GRelation* rel = (GRelation*) user_data;
394 gpointer* tuples = (gpointer*) tuple_value;
396 gstring = g_string_new ("[");
398 for (i = 0; i < rel->fields; i += 1)
400 g_string_append_printf (gstring, "%p", tuples[i]);
402 if (i < (rel->fields - 1))
403 g_string_append (gstring, ",");
406 g_string_append (gstring, "]");
407 g_log (G_LOG_DOMAIN, G_LOG_LEVEL_INFO, gstring->str);
408 g_string_free (gstring, TRUE);
412 g_relation_print_index (gpointer tuple_key,
413 gpointer tuple_value,
416 GRelation* rel = (GRelation*) user_data;
417 GHashTable* table = (GHashTable*) tuple_value;
419 g_log (G_LOG_DOMAIN, G_LOG_LEVEL_INFO, "*** key %p", tuple_key);
421 g_hash_table_foreach (table,
422 g_relation_print_one,
427 g_relation_print (GRelation *relation)
431 g_log (G_LOG_DOMAIN, G_LOG_LEVEL_INFO, "*** all tuples (%d)", relation->count);
433 g_hash_table_foreach (relation->all_tuples,
434 g_relation_print_one,
437 for (i = 0; i < relation->fields; i += 1)
439 if (relation->hashed_tuple_tables[i] == NULL)
442 g_log (G_LOG_DOMAIN, G_LOG_LEVEL_INFO, "*** index %d", i);
444 g_hash_table_foreach (relation->hashed_tuple_tables[i],
445 g_relation_print_index,