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.
22 typedef struct _GRealRelation GRealRelation;
23 typedef struct _GRealTuples GRealTuples;
30 GHashTable *all_tuples;
31 GHashTable **hashed_tuple_tables;
32 GMemChunk *tuple_chunk;
45 tuple_equal_2 (gconstpointer v_a,
48 gpointer* a = (gpointer*) v_a;
49 gpointer* b = (gpointer*) v_b;
51 return a[0] == b[0] && a[1] == b[1];
55 tuple_hash_2 (gconstpointer v_a)
57 gpointer* a = (gpointer*) v_a;
59 return (gulong)a[0] ^ (gulong)a[1];
63 tuple_hash (gint fields)
70 g_error ("no tuple hash for %d", fields);
77 tuple_equal (gint fields)
84 g_error ("no tuple equal for %d", fields);
91 g_relation_new (gint fields)
93 GRealRelation* rel = g_new0 (GRealRelation, 1);
96 rel->tuple_chunk = g_mem_chunk_new ("Relation Chunk",
97 fields * sizeof (gpointer),
98 fields * sizeof (gpointer) * 128,
100 rel->all_tuples = g_hash_table_new (tuple_hash (fields), tuple_equal (fields));
101 rel->hashed_tuple_tables = g_new0 (GHashTable*, fields);
103 return (GRelation*) rel;
107 g_relation_free_array (gpointer key, gpointer value, gpointer user_data)
109 g_hash_table_destroy ((GHashTable*) value);
113 g_relation_destroy (GRelation *relation)
115 GRealRelation *rel = (GRealRelation *) relation;
120 g_hash_table_destroy (rel->all_tuples);
121 g_mem_chunk_destroy (rel->tuple_chunk);
123 for (i = 0; i < rel->fields; i += 1)
125 if (rel->hashed_tuple_tables[i])
127 g_hash_table_foreach (rel->hashed_tuple_tables[i], g_relation_free_array, NULL);
128 g_hash_table_destroy (rel->hashed_tuple_tables[i]);
132 g_free (rel->hashed_tuple_tables);
138 g_relation_index (GRelation *relation,
141 GCompareFunc key_compare_func)
143 GRealRelation *rel = (GRealRelation *) relation;
145 g_assert (rel->count == 0 && rel->hashed_tuple_tables[field] == NULL);
147 rel->hashed_tuple_tables[field] = g_hash_table_new (hash_func, key_compare_func);
151 g_relation_insert (GRelation *relation,
154 GRealRelation *rel = (GRealRelation *) relation;
155 gpointer* tuple = g_chunk_new (gpointer, rel->tuple_chunk);
159 va_start(args, relation);
161 for (i = 0; i < rel->fields; i += 1)
162 tuple[i] = va_arg(args, gpointer);
166 g_hash_table_insert (rel->all_tuples, tuple, tuple);
170 for (i = 0; i < rel->fields; i += 1)
174 GHashTable *per_key_table;
176 table = rel->hashed_tuple_tables[i];
182 per_key_table = g_hash_table_lookup (table, key);
184 if (per_key_table == NULL)
186 per_key_table = g_hash_table_new (tuple_hash (rel->fields), tuple_equal (rel->fields));
187 g_hash_table_insert (table, key, per_key_table);
190 g_hash_table_insert (per_key_table, tuple, tuple);
195 g_relation_delete_tuple (gpointer tuple_key, gpointer tuple_value, gpointer user_data)
197 gpointer *tuple = (gpointer*) tuple_value;
198 GRealRelation *rel = (GRealRelation *) user_data;
201 g_assert (tuple_key == tuple_value);
203 for (j = 0; j < rel->fields; j += 1)
205 GHashTable *one_table = rel->hashed_tuple_tables[j];
207 GHashTable *per_key_table;
209 if (one_table == NULL)
212 if (j == rel->current_field)
213 /* can't delete from the table we're foreaching in */
218 per_key_table = g_hash_table_lookup (one_table, one_key);
220 g_hash_table_remove (per_key_table, tuple);
223 g_hash_table_remove (rel->all_tuples, tuple);
229 g_relation_delete (GRelation *relation,
233 GRealRelation *rel = (GRealRelation *) relation;
234 GHashTable *table = rel->hashed_tuple_tables[field];
235 GHashTable *key_table;
236 gint count = rel->count;
240 key_table = g_hash_table_lookup (table, key);
245 rel->current_field = field;
247 g_hash_table_foreach (key_table, g_relation_delete_tuple, rel);
249 g_hash_table_remove (table, key);
251 g_hash_table_destroy (key_table);
253 /* @@@ Remove empty hash tables. */
255 return count - rel->count;
259 g_relation_select_tuple (gpointer tuple_key, gpointer tuple_value, gpointer user_data)
261 gpointer *tuple = (gpointer*) tuple_value;
262 GRealTuples *tuples = (GRealTuples*) user_data;
263 gint stride = sizeof (gpointer) * tuples->width;
265 g_assert (tuple_key == tuple_value);
267 memcpy (tuples->data + (tuples->len * tuples->width),
275 g_relation_select (GRelation *relation,
279 GRealRelation *rel = (GRealRelation *) relation;
280 GHashTable *table = rel->hashed_tuple_tables[field];
281 GHashTable *key_table;
282 GRealTuples *tuples = g_new0 (GRealTuples, 1);
287 key_table = g_hash_table_lookup (table, key);
290 return (GTuples*)tuples;
292 count = g_relation_count (relation, key, field);
294 tuples->data = g_malloc (sizeof (gpointer) * rel->fields * count);
295 tuples->width = rel->fields;
297 g_hash_table_foreach (key_table, g_relation_select_tuple, tuples);
299 g_assert (count == tuples->len);
301 return (GTuples*)tuples;
305 g_relation_count (GRelation *relation,
309 GRealRelation *rel = (GRealRelation *) relation;
310 GHashTable *table = rel->hashed_tuple_tables[field];
311 GHashTable *key_table;
315 key_table = g_hash_table_lookup (table, key);
320 return g_hash_table_size (key_table);
324 g_relation_exists (GRelation *relation, ...)
326 GRealRelation *rel = (GRealRelation *) relation;
327 gpointer* tuple = g_chunk_new (gpointer, rel->tuple_chunk);
332 va_start(args, relation);
334 for (i = 0; i < rel->fields; i += 1)
335 tuple[i] = va_arg(args, gpointer);
339 result = g_hash_table_lookup (rel->all_tuples, tuple) != NULL;
341 g_mem_chunk_free (rel->tuple_chunk, tuple);
347 g_tuples_destroy (GTuples *tuples0)
349 GRealTuples *tuples = (GRealTuples*) tuples0;
353 g_free (tuples->data);
359 g_tuples_index (GTuples *tuples0,
363 GRealTuples *tuples = (GRealTuples*) tuples0;
365 g_assert (field < tuples->width);
367 return tuples->data[index * tuples->width + field];
374 g_relation_print_one (gpointer tuple_key,
375 gpointer tuple_value,
379 GRealRelation* rel = (GRealRelation*) user_data;
380 gpointer* tuples = (gpointer*) tuple_value;
384 for (i = 0; i < rel->fields; i += 1)
386 g_print ("%p", tuples[i]);
388 if (i < (rel->fields - 1))
396 g_relation_print_index (gpointer tuple_key,
397 gpointer tuple_value,
400 GRealRelation* rel = (GRealRelation*) user_data;
401 GHashTable* table = (GHashTable*) tuple_value;
403 g_print ("*** key %p\n", tuple_key);
405 g_hash_table_foreach (table,
406 g_relation_print_one,
411 g_relation_print (GRelation *relation)
414 GRealRelation* rel = (GRealRelation*) relation;
416 g_print ("*** all tuples (%d)\n", rel->count);
418 g_hash_table_foreach (rel->all_tuples,
419 g_relation_print_one,
422 for (i = 0; i < rel->fields; i += 1)
424 if (rel->hashed_tuple_tables[i] == NULL)
427 g_print ("*** index %d\n", i);
429 g_hash_table_foreach (rel->hashed_tuple_tables[i],
430 g_relation_print_index,