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/.
36 #include "gmessages.h"
37 #include "gtestutils.h"
40 #undef G_DISABLE_DEPRECATED
46 * @title: Relations and Tuples
47 * @short_description: tables of data which can be indexed on any
50 * A #GRelation is a table of data which can be indexed on any number
51 * of fields, rather like simple database tables. A #GRelation contains
52 * a number of records, called tuples. Each record contains a number of
53 * fields. Records are not ordered, so it is not possible to find the
54 * record at a particular index.
56 * Note that #GRelation tables are currently limited to 2 fields.
58 * To create a GRelation, use g_relation_new().
60 * To specify which fields should be indexed, use g_relation_index().
61 * Note that this must be called before any tuples are added to the
64 * To add records to a #GRelation use g_relation_insert().
66 * To determine if a given record appears in a #GRelation, use
67 * g_relation_exists(). Note that fields are compared directly, so
68 * pointers must point to the exact same position (i.e. different
69 * copies of the same string will not match.)
71 * To count the number of records which have a particular value in a
72 * given field, use g_relation_count().
74 * To get all the records which have a particular value in a given
75 * field, use g_relation_select(). To access fields of the resulting
76 * records, use g_tuples_index(). To free the resulting records use
79 * To delete all records which have a particular value in a given
80 * field, use g_relation_delete().
82 * To destroy the #GRelation, use g_relation_destroy().
84 * To help debug #GRelation objects, use g_relation_print().
86 * GRelation has been marked as deprecated, since this API has never
87 * been fully implemented, is not very actively maintained and rarely
91 typedef struct _GRealTuples GRealTuples;
96 * The #GRelation struct is an opaque data structure to represent a
97 * <link linkend="glib-Relations-and-Tuples">Relation</link>. It should
98 * only be accessed via the following functions.
105 GHashTable *all_tuples;
106 GHashTable **hashed_tuple_tables;
113 * @len: the number of records that matched.
115 * The #GTuples struct is used to return records (or tuples) from the
116 * #GRelation by g_relation_select(). It only contains one public
117 * member - the number of records that matched. To access the matched
118 * records, you must use g_tuples_index().
128 tuple_equal_2 (gconstpointer v_a,
131 gpointer* a = (gpointer*) v_a;
132 gpointer* b = (gpointer*) v_b;
134 return a[0] == b[0] && a[1] == b[1];
138 tuple_hash_2 (gconstpointer v_a)
140 #if GLIB_SIZEOF_VOID_P > GLIB_SIZEOF_LONG
141 /* In practise this snippet has been written for 64-bit Windows
142 * where ints are 32 bits, pointers 64 bits. More exotic platforms
145 guint* a = (guint*) v_a;
147 return (a[0] ^ a[1] ^ a[2] ^ a[3]);
149 gpointer* a = (gpointer*) v_a;
151 return (gulong)a[0] ^ (gulong)a[1];
156 tuple_hash (gint fields)
163 g_error ("no tuple hash for %d", fields);
170 tuple_equal (gint fields)
175 return tuple_equal_2;
177 g_error ("no tuple equal for %d", fields);
185 * @fields: the number of fields.
186 * @Returns: a new #GRelation.
188 * Creates a new #GRelation with the given number of fields. Note that
189 * currently the number of fields must be 2.
191 * Deprecated: 2.26: Rarely used API
194 g_relation_new (gint fields)
196 GRelation* rel = g_new0 (GRelation, 1);
198 rel->fields = fields;
199 rel->all_tuples = g_hash_table_new (tuple_hash (fields), tuple_equal (fields));
200 rel->hashed_tuple_tables = g_new0 (GHashTable*, fields);
206 relation_delete_value_tuple (gpointer tuple_key,
207 gpointer tuple_value,
210 GRelation *relation = user_data;
211 gpointer *tuple = tuple_value;
212 g_slice_free1 (relation->fields * sizeof (gpointer), tuple);
216 g_relation_free_array (gpointer key, gpointer value, gpointer user_data)
218 g_hash_table_destroy ((GHashTable*) value);
222 * g_relation_destroy:
223 * @relation: a #GRelation.
225 * Destroys the #GRelation, freeing all memory allocated. However, it
226 * does not free memory allocated for the tuple data, so you should
227 * free that first if appropriate.
229 * Deprecated: 2.26: Rarely used API
232 g_relation_destroy (GRelation *relation)
238 for (i = 0; i < relation->fields; i += 1)
240 if (relation->hashed_tuple_tables[i])
242 g_hash_table_foreach (relation->hashed_tuple_tables[i], g_relation_free_array, NULL);
243 g_hash_table_destroy (relation->hashed_tuple_tables[i]);
247 g_hash_table_foreach (relation->all_tuples, relation_delete_value_tuple, relation);
248 g_hash_table_destroy (relation->all_tuples);
250 g_free (relation->hashed_tuple_tables);
257 * @relation: a #GRelation.
258 * @field: the field to index, counting from 0.
259 * @hash_func: a function to produce a hash value from the field data.
260 * @key_equal_func: a function to compare two values of the given field.
262 * Creates an index on the given field. Note that this must be called
263 * before any records are added to the #GRelation.
265 * Deprecated: 2.26: Rarely used API
268 g_relation_index (GRelation *relation,
271 GEqualFunc key_equal_func)
273 g_return_if_fail (relation != NULL);
275 g_return_if_fail (relation->count == 0 && relation->hashed_tuple_tables[field] == NULL);
277 relation->hashed_tuple_tables[field] = g_hash_table_new (hash_func, key_equal_func);
282 * @relation: a #GRelation.
283 * @...: the fields of the record to add. These must match the
284 * number of fields in the #GRelation, and of type #gpointer
287 * Inserts a record into a #GRelation.
289 * Deprecated: 2.26: Rarely used API
292 g_relation_insert (GRelation *relation,
295 gpointer* tuple = g_slice_alloc (relation->fields * sizeof (gpointer));
299 va_start (args, relation);
301 for (i = 0; i < relation->fields; i += 1)
302 tuple[i] = va_arg (args, gpointer);
306 g_hash_table_insert (relation->all_tuples, tuple, tuple);
308 relation->count += 1;
310 for (i = 0; i < relation->fields; i += 1)
314 GHashTable *per_key_table;
316 table = relation->hashed_tuple_tables[i];
322 per_key_table = g_hash_table_lookup (table, key);
324 if (per_key_table == NULL)
326 per_key_table = g_hash_table_new (tuple_hash (relation->fields), tuple_equal (relation->fields));
327 g_hash_table_insert (table, key, per_key_table);
330 g_hash_table_insert (per_key_table, tuple, tuple);
335 g_relation_delete_tuple (gpointer tuple_key,
336 gpointer tuple_value,
339 gpointer *tuple = (gpointer*) tuple_value;
340 GRelation *relation = (GRelation *) user_data;
343 g_assert (tuple_key == tuple_value);
345 for (j = 0; j < relation->fields; j += 1)
347 GHashTable *one_table = relation->hashed_tuple_tables[j];
349 GHashTable *per_key_table;
351 if (one_table == NULL)
354 if (j == relation->current_field)
355 /* can't delete from the table we're foreaching in */
360 per_key_table = g_hash_table_lookup (one_table, one_key);
362 g_hash_table_remove (per_key_table, tuple);
365 if (g_hash_table_remove (relation->all_tuples, tuple))
366 g_slice_free1 (relation->fields * sizeof (gpointer), tuple);
368 relation->count -= 1;
373 * @relation: a #GRelation.
374 * @key: the value to compare with.
375 * @field: the field of each record to match.
376 * @Returns: the number of records deleted.
378 * Deletes any records from a #GRelation that have the given key value
379 * in the given field.
381 * Deprecated: 2.26: Rarely used API
384 g_relation_delete (GRelation *relation,
389 GHashTable *key_table;
392 g_return_val_if_fail (relation != NULL, 0);
394 table = relation->hashed_tuple_tables[field];
395 count = relation->count;
397 g_return_val_if_fail (table != NULL, 0);
399 key_table = g_hash_table_lookup (table, key);
404 relation->current_field = field;
406 g_hash_table_foreach (key_table, g_relation_delete_tuple, relation);
408 g_hash_table_remove (table, key);
410 g_hash_table_destroy (key_table);
412 /* @@@ FIXME: Remove empty hash tables. */
414 return count - relation->count;
418 g_relation_select_tuple (gpointer tuple_key,
419 gpointer tuple_value,
422 gpointer *tuple = (gpointer*) tuple_value;
423 GRealTuples *tuples = (GRealTuples*) user_data;
424 gint stride = sizeof (gpointer) * tuples->width;
426 g_assert (tuple_key == tuple_value);
428 memcpy (tuples->data + (tuples->len * tuples->width),
437 * @relation: a #GRelation.
438 * @key: the value to compare with.
439 * @field: the field of each record to match.
440 * @Returns: the records (tuples) that matched.
442 * Returns all of the tuples which have the given key in the given
443 * field. Use g_tuples_index() to access the returned records. The
444 * returned records should be freed with g_tuples_destroy().
446 * Deprecated: 2.26: Rarely used API
449 g_relation_select (GRelation *relation,
454 GHashTable *key_table;
458 g_return_val_if_fail (relation != NULL, NULL);
460 table = relation->hashed_tuple_tables[field];
462 g_return_val_if_fail (table != NULL, NULL);
464 tuples = g_new0 (GRealTuples, 1);
465 key_table = g_hash_table_lookup (table, key);
468 return (GTuples*)tuples;
470 count = g_relation_count (relation, key, field);
472 tuples->data = g_malloc (sizeof (gpointer) * relation->fields * count);
473 tuples->width = relation->fields;
475 g_hash_table_foreach (key_table, g_relation_select_tuple, tuples);
477 g_assert (count == tuples->len);
479 return (GTuples*)tuples;
484 * @relation: a #GRelation.
485 * @key: the value to compare with.
486 * @field: the field of each record to match.
487 * @Returns: the number of matches.
489 * Returns the number of tuples in a #GRelation that have the given
490 * value in the given field.
492 * Deprecated: 2.26: Rarely used API
495 g_relation_count (GRelation *relation,
500 GHashTable *key_table;
502 g_return_val_if_fail (relation != NULL, 0);
504 table = relation->hashed_tuple_tables[field];
506 g_return_val_if_fail (table != NULL, 0);
508 key_table = g_hash_table_lookup (table, key);
513 return g_hash_table_size (key_table);
518 * @relation: a #GRelation.
519 * @...: the fields of the record to compare. The number must match
520 * the number of fields in the #GRelation.
521 * @Returns: %TRUE if a record matches.
523 * Returns %TRUE if a record with the given values exists in a
524 * #GRelation. Note that the values are compared directly, so that, for
525 * example, two copies of the same string will not match.
527 * Deprecated: 2.26: Rarely used API
530 g_relation_exists (GRelation *relation, ...)
532 gpointer *tuple = g_slice_alloc (relation->fields * sizeof (gpointer));
537 va_start(args, relation);
539 for (i = 0; i < relation->fields; i += 1)
540 tuple[i] = va_arg(args, gpointer);
544 result = g_hash_table_lookup (relation->all_tuples, tuple) != NULL;
546 g_slice_free1 (relation->fields * sizeof (gpointer), tuple);
553 * @tuples: the tuple data to free.
555 * Frees the records which were returned by g_relation_select(). This
556 * should always be called after g_relation_select() when you are
557 * finished with the records. The records are not removed from the
560 * Deprecated: 2.26: Rarely used API
563 g_tuples_destroy (GTuples *tuples0)
565 GRealTuples *tuples = (GRealTuples*) tuples0;
569 g_free (tuples->data);
576 * @tuples: the tuple data, returned by g_relation_select().
577 * @index_: the index of the record.
578 * @field: the field to return.
579 * @Returns: the field of the record.
581 * Gets a field from the records returned by g_relation_select(). It
582 * returns the given field of the record at the given index. The
583 * returned value should not be changed.
585 * Deprecated: 2.26: Rarely used API
588 g_tuples_index (GTuples *tuples0,
592 GRealTuples *tuples = (GRealTuples*) tuples0;
594 g_return_val_if_fail (tuples0 != NULL, NULL);
595 g_return_val_if_fail (field < tuples->width, NULL);
597 return tuples->data[index * tuples->width + field];
604 g_relation_print_one (gpointer tuple_key,
605 gpointer tuple_value,
610 GRelation* rel = (GRelation*) user_data;
611 gpointer* tuples = (gpointer*) tuple_value;
613 gstring = g_string_new ("[");
615 for (i = 0; i < rel->fields; i += 1)
617 g_string_append_printf (gstring, "%p", tuples[i]);
619 if (i < (rel->fields - 1))
620 g_string_append (gstring, ",");
623 g_string_append (gstring, "]");
624 g_log (G_LOG_DOMAIN, G_LOG_LEVEL_INFO, "%s", gstring->str);
625 g_string_free (gstring, TRUE);
629 g_relation_print_index (gpointer tuple_key,
630 gpointer tuple_value,
633 GRelation* rel = (GRelation*) user_data;
634 GHashTable* table = (GHashTable*) tuple_value;
636 g_log (G_LOG_DOMAIN, G_LOG_LEVEL_INFO, "*** key %p", tuple_key);
638 g_hash_table_foreach (table,
639 g_relation_print_one,
645 * @relation: a #GRelation.
647 * Outputs information about all records in a #GRelation, as well as
648 * the indexes. It is for debugging.
650 * Deprecated: 2.26: Rarely used API
653 g_relation_print (GRelation *relation)
657 g_log (G_LOG_DOMAIN, G_LOG_LEVEL_INFO, "*** all tuples (%d)", relation->count);
659 g_hash_table_foreach (relation->all_tuples,
660 g_relation_print_one,
663 for (i = 0; i < relation->fields; i += 1)
665 if (relation->hashed_tuple_tables[i] == NULL)
668 g_log (G_LOG_DOMAIN, G_LOG_LEVEL_INFO, "*** index %d", i);
670 g_hash_table_foreach (relation->hashed_tuple_tables[i],
671 g_relation_print_index,