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 Library 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 * Library General Public License for more details.
14 * You should have received a copy of the GNU Library 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.
27 typedef struct _GRealRelation GRealRelation;
28 typedef struct _GRealTuples GRealTuples;
35 GHashTable *all_tuples;
36 GHashTable **hashed_tuple_tables;
37 GMemChunk *tuple_chunk;
50 tuple_equal_2 (gconstpointer v_a,
53 gpointer* a = (gpointer*) v_a;
54 gpointer* b = (gpointer*) v_b;
56 return a[0] == b[0] && a[1] == b[1];
60 tuple_hash_2 (gconstpointer v_a)
62 gpointer* a = (gpointer*) v_a;
64 return (gulong)a[0] ^ (gulong)a[1];
68 tuple_hash (gint fields)
75 g_error ("no tuple hash for %d", fields);
82 tuple_equal (gint fields)
89 g_error ("no tuple equal for %d", fields);
96 g_relation_new (gint fields)
98 GRealRelation* rel = g_new0 (GRealRelation, 1);
100 rel->fields = fields;
101 rel->tuple_chunk = g_mem_chunk_new ("Relation Chunk",
102 fields * sizeof (gpointer),
103 fields * sizeof (gpointer) * 128,
105 rel->all_tuples = g_hash_table_new (tuple_hash (fields), tuple_equal (fields));
106 rel->hashed_tuple_tables = g_new0 (GHashTable*, fields);
108 return (GRelation*) rel;
112 g_relation_free_array (gpointer key, gpointer value, gpointer user_data)
114 g_hash_table_destroy ((GHashTable*) value);
118 g_relation_destroy (GRelation *relation)
120 GRealRelation *rel = (GRealRelation *) relation;
125 g_hash_table_destroy (rel->all_tuples);
126 g_mem_chunk_destroy (rel->tuple_chunk);
128 for (i = 0; i < rel->fields; i += 1)
130 if (rel->hashed_tuple_tables[i])
132 g_hash_table_foreach (rel->hashed_tuple_tables[i], g_relation_free_array, NULL);
133 g_hash_table_destroy (rel->hashed_tuple_tables[i]);
137 g_free (rel->hashed_tuple_tables);
143 g_relation_index (GRelation *relation,
146 GCompareFunc key_compare_func)
148 GRealRelation *rel = (GRealRelation *) relation;
150 g_assert (rel->count == 0 && rel->hashed_tuple_tables[field] == NULL);
152 rel->hashed_tuple_tables[field] = g_hash_table_new (hash_func, key_compare_func);
156 g_relation_insert (GRelation *relation,
159 GRealRelation *rel = (GRealRelation *) relation;
160 gpointer* tuple = g_chunk_new (gpointer, rel->tuple_chunk);
164 va_start(args, relation);
166 for (i = 0; i < rel->fields; i += 1)
167 tuple[i] = va_arg(args, gpointer);
171 g_hash_table_insert (rel->all_tuples, tuple, tuple);
175 for (i = 0; i < rel->fields; i += 1)
179 GHashTable *per_key_table;
181 table = rel->hashed_tuple_tables[i];
187 per_key_table = g_hash_table_lookup (table, key);
189 if (per_key_table == NULL)
191 per_key_table = g_hash_table_new (tuple_hash (rel->fields), tuple_equal (rel->fields));
192 g_hash_table_insert (table, key, per_key_table);
195 g_hash_table_insert (per_key_table, tuple, tuple);
200 g_relation_delete_tuple (gpointer tuple_key, gpointer tuple_value, gpointer user_data)
202 gpointer *tuple = (gpointer*) tuple_value;
203 GRealRelation *rel = (GRealRelation *) user_data;
206 g_assert (tuple_key == tuple_value);
208 for (j = 0; j < rel->fields; j += 1)
210 GHashTable *one_table = rel->hashed_tuple_tables[j];
212 GHashTable *per_key_table;
214 if (one_table == NULL)
217 if (j == rel->current_field)
218 /* can't delete from the table we're foreaching in */
223 per_key_table = g_hash_table_lookup (one_table, one_key);
225 g_hash_table_remove (per_key_table, tuple);
228 g_hash_table_remove (rel->all_tuples, tuple);
234 g_relation_delete (GRelation *relation,
238 GRealRelation *rel = (GRealRelation *) relation;
239 GHashTable *table = rel->hashed_tuple_tables[field];
240 GHashTable *key_table;
241 gint count = rel->count;
245 key_table = g_hash_table_lookup (table, key);
250 rel->current_field = field;
252 g_hash_table_foreach (key_table, g_relation_delete_tuple, rel);
254 g_hash_table_remove (table, key);
256 g_hash_table_destroy (key_table);
258 /* @@@ FIXME: Remove empty hash tables. */
260 return count - rel->count;
264 g_relation_select_tuple (gpointer tuple_key, gpointer tuple_value, gpointer user_data)
266 gpointer *tuple = (gpointer*) tuple_value;
267 GRealTuples *tuples = (GRealTuples*) user_data;
268 gint stride = sizeof (gpointer) * tuples->width;
270 g_assert (tuple_key == tuple_value);
272 memcpy (tuples->data + (tuples->len * tuples->width),
280 g_relation_select (GRelation *relation,
284 GRealRelation *rel = (GRealRelation *) relation;
285 GHashTable *table = rel->hashed_tuple_tables[field];
286 GHashTable *key_table;
287 GRealTuples *tuples = g_new0 (GRealTuples, 1);
292 key_table = g_hash_table_lookup (table, key);
295 return (GTuples*)tuples;
297 count = g_relation_count (relation, key, field);
299 tuples->data = g_malloc (sizeof (gpointer) * rel->fields * count);
300 tuples->width = rel->fields;
302 g_hash_table_foreach (key_table, g_relation_select_tuple, tuples);
304 g_assert (count == tuples->len);
306 return (GTuples*)tuples;
310 g_relation_count (GRelation *relation,
314 GRealRelation *rel = (GRealRelation *) relation;
315 GHashTable *table = rel->hashed_tuple_tables[field];
316 GHashTable *key_table;
320 key_table = g_hash_table_lookup (table, key);
325 return g_hash_table_size (key_table);
329 g_relation_exists (GRelation *relation, ...)
331 GRealRelation *rel = (GRealRelation *) relation;
332 gpointer* tuple = g_chunk_new (gpointer, rel->tuple_chunk);
337 va_start(args, relation);
339 for (i = 0; i < rel->fields; i += 1)
340 tuple[i] = va_arg(args, gpointer);
344 result = g_hash_table_lookup (rel->all_tuples, tuple) != NULL;
346 g_mem_chunk_free (rel->tuple_chunk, tuple);
352 g_tuples_destroy (GTuples *tuples0)
354 GRealTuples *tuples = (GRealTuples*) tuples0;
358 g_free (tuples->data);
364 g_tuples_index (GTuples *tuples0,
368 GRealTuples *tuples = (GRealTuples*) tuples0;
370 g_assert (field < tuples->width);
372 return tuples->data[index * tuples->width + field];
379 g_relation_print_one (gpointer tuple_key,
380 gpointer tuple_value,
385 GRealRelation* rel = (GRealRelation*) user_data;
386 gpointer* tuples = (gpointer*) tuple_value;
388 gstring = g_string_new ("[");
390 for (i = 0; i < rel->fields; i += 1)
392 g_string_sprintfa (gstring, "%p", tuples[i]);
394 if (i < (rel->fields - 1))
395 g_string_append (gstring, ",");
398 g_string_append (gstring, "]");
399 g_log (g_log_domain_glib, G_LOG_LEVEL_INFO, gstring->str);
400 g_string_free (gstring, TRUE);
404 g_relation_print_index (gpointer tuple_key,
405 gpointer tuple_value,
408 GRealRelation* rel = (GRealRelation*) user_data;
409 GHashTable* table = (GHashTable*) tuple_value;
411 g_log (g_log_domain_glib, G_LOG_LEVEL_INFO, "*** key %p", tuple_key);
413 g_hash_table_foreach (table,
414 g_relation_print_one,
419 g_relation_print (GRelation *relation)
422 GRealRelation* rel = (GRealRelation*) relation;
424 g_log (g_log_domain_glib, G_LOG_LEVEL_INFO, "*** all tuples (%d)", rel->count);
426 g_hash_table_foreach (rel->all_tuples,
427 g_relation_print_one,
430 for (i = 0; i < rel->fields; i += 1)
432 if (rel->hashed_tuple_tables[i] == NULL)
435 g_log (g_log_domain_glib, G_LOG_LEVEL_INFO, "*** index %d", i);
437 g_hash_table_foreach (rel->hashed_tuple_tables[i],
438 g_relation_print_index,