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
47 * @title: Relations and Tuples
48 * @short_description: tables of data which can be indexed on any
51 * A #GRelation is a table of data which can be indexed on any number
52 * of fields, rather like simple database tables. A #GRelation contains
53 * a number of records, called tuples. Each record contains a number of
54 * fields. Records are not ordered, so it is not possible to find the
55 * record at a particular index.
57 * Note that #GRelation tables are currently limited to 2 fields.
59 * To create a GRelation, use g_relation_new().
61 * To specify which fields should be indexed, use g_relation_index().
62 * Note that this must be called before any tuples are added to the
65 * To add records to a #GRelation use g_relation_insert().
67 * To determine if a given record appears in a #GRelation, use
68 * g_relation_exists(). Note that fields are compared directly, so
69 * pointers must point to the exact same position (i.e. different
70 * copies of the same string will not match.)
72 * To count the number of records which have a particular value in a
73 * given field, use g_relation_count().
75 * To get all the records which have a particular value in a given
76 * field, use g_relation_select(). To access fields of the resulting
77 * records, use g_tuples_index(). To free the resulting records use
80 * To delete all records which have a particular value in a given
81 * field, use g_relation_delete().
83 * To destroy the #GRelation, use g_relation_destroy().
85 * To help debug #GRelation objects, use g_relation_print().
87 * GRelation has been marked as deprecated, since this API has never
88 * been fully implemented, is not very actively maintained and rarely
92 typedef struct _GRealTuples GRealTuples;
97 * The #GRelation struct is an opaque data structure to represent a
98 * <link linkend="glib-Relations-and-Tuples">Relation</link>. It should
99 * only be accessed via the following functions.
106 GHashTable *all_tuples;
107 GHashTable **hashed_tuple_tables;
114 * @len: the number of records that matched.
116 * The #GTuples struct is used to return records (or tuples) from the
117 * #GRelation by g_relation_select(). It only contains one public
118 * member - the number of records that matched. To access the matched
119 * records, you must use g_tuples_index().
129 tuple_equal_2 (gconstpointer v_a,
132 gpointer* a = (gpointer*) v_a;
133 gpointer* b = (gpointer*) v_b;
135 return a[0] == b[0] && a[1] == b[1];
139 tuple_hash_2 (gconstpointer v_a)
141 #if GLIB_SIZEOF_VOID_P > GLIB_SIZEOF_LONG
142 /* In practise this snippet has been written for 64-bit Windows
143 * where ints are 32 bits, pointers 64 bits. More exotic platforms
146 guint* a = (guint*) v_a;
148 return (a[0] ^ a[1] ^ a[2] ^ a[3]);
150 gpointer* a = (gpointer*) v_a;
152 return (gulong)a[0] ^ (gulong)a[1];
157 tuple_hash (gint fields)
164 g_error ("no tuple hash for %d", fields);
171 tuple_equal (gint fields)
176 return tuple_equal_2;
178 g_error ("no tuple equal for %d", fields);
186 * @fields: the number of fields.
187 * @Returns: a new #GRelation.
189 * Creates a new #GRelation with the given number of fields. Note that
190 * currently the number of fields must be 2.
192 * Deprecated: 2.26: Rarely used API
195 g_relation_new (gint fields)
197 GRelation* rel = g_new0 (GRelation, 1);
199 rel->fields = fields;
200 rel->all_tuples = g_hash_table_new (tuple_hash (fields), tuple_equal (fields));
201 rel->hashed_tuple_tables = g_new0 (GHashTable*, fields);
207 relation_delete_value_tuple (gpointer tuple_key,
208 gpointer tuple_value,
211 GRelation *relation = user_data;
212 gpointer *tuple = tuple_value;
213 g_slice_free1 (relation->fields * sizeof (gpointer), tuple);
217 g_relation_free_array (gpointer key, gpointer value, gpointer user_data)
219 g_hash_table_destroy ((GHashTable*) value);
223 * g_relation_destroy:
224 * @relation: a #GRelation.
226 * Destroys the #GRelation, freeing all memory allocated. However, it
227 * does not free memory allocated for the tuple data, so you should
228 * free that first if appropriate.
230 * Deprecated: 2.26: Rarely used API
233 g_relation_destroy (GRelation *relation)
239 for (i = 0; i < relation->fields; i += 1)
241 if (relation->hashed_tuple_tables[i])
243 g_hash_table_foreach (relation->hashed_tuple_tables[i], g_relation_free_array, NULL);
244 g_hash_table_destroy (relation->hashed_tuple_tables[i]);
248 g_hash_table_foreach (relation->all_tuples, relation_delete_value_tuple, relation);
249 g_hash_table_destroy (relation->all_tuples);
251 g_free (relation->hashed_tuple_tables);
258 * @relation: a #GRelation.
259 * @field: the field to index, counting from 0.
260 * @hash_func: a function to produce a hash value from the field data.
261 * @key_equal_func: a function to compare two values of the given field.
263 * Creates an index on the given field. Note that this must be called
264 * before any records are added to the #GRelation.
266 * Deprecated: 2.26: Rarely used API
269 g_relation_index (GRelation *relation,
272 GEqualFunc key_equal_func)
274 g_return_if_fail (relation != NULL);
276 g_return_if_fail (relation->count == 0 && relation->hashed_tuple_tables[field] == NULL);
278 relation->hashed_tuple_tables[field] = g_hash_table_new (hash_func, key_equal_func);
283 * @relation: a #GRelation.
284 * @Varargs: the fields of the record to add. These must match the
285 * number of fields in the #GRelation, and of type #gpointer
288 * Inserts a record into a #GRelation.
290 * Deprecated: 2.26: Rarely used API
293 g_relation_insert (GRelation *relation,
296 gpointer* tuple = g_slice_alloc (relation->fields * sizeof (gpointer));
300 va_start (args, relation);
302 for (i = 0; i < relation->fields; i += 1)
303 tuple[i] = va_arg (args, gpointer);
307 g_hash_table_insert (relation->all_tuples, tuple, tuple);
309 relation->count += 1;
311 for (i = 0; i < relation->fields; i += 1)
315 GHashTable *per_key_table;
317 table = relation->hashed_tuple_tables[i];
323 per_key_table = g_hash_table_lookup (table, key);
325 if (per_key_table == NULL)
327 per_key_table = g_hash_table_new (tuple_hash (relation->fields), tuple_equal (relation->fields));
328 g_hash_table_insert (table, key, per_key_table);
331 g_hash_table_insert (per_key_table, tuple, tuple);
336 g_relation_delete_tuple (gpointer tuple_key,
337 gpointer tuple_value,
340 gpointer *tuple = (gpointer*) tuple_value;
341 GRelation *relation = (GRelation *) user_data;
344 g_assert (tuple_key == tuple_value);
346 for (j = 0; j < relation->fields; j += 1)
348 GHashTable *one_table = relation->hashed_tuple_tables[j];
350 GHashTable *per_key_table;
352 if (one_table == NULL)
355 if (j == relation->current_field)
356 /* can't delete from the table we're foreaching in */
361 per_key_table = g_hash_table_lookup (one_table, one_key);
363 g_hash_table_remove (per_key_table, tuple);
366 if (g_hash_table_remove (relation->all_tuples, tuple))
367 g_slice_free1 (relation->fields * sizeof (gpointer), tuple);
369 relation->count -= 1;
374 * @relation: a #GRelation.
375 * @key: the value to compare with.
376 * @field: the field of each record to match.
377 * @Returns: the number of records deleted.
379 * Deletes any records from a #GRelation that have the given key value
380 * in the given field.
382 * Deprecated: 2.26: Rarely used API
385 g_relation_delete (GRelation *relation,
390 GHashTable *key_table;
393 g_return_val_if_fail (relation != NULL, 0);
395 table = relation->hashed_tuple_tables[field];
396 count = relation->count;
398 g_return_val_if_fail (table != NULL, 0);
400 key_table = g_hash_table_lookup (table, key);
405 relation->current_field = field;
407 g_hash_table_foreach (key_table, g_relation_delete_tuple, relation);
409 g_hash_table_remove (table, key);
411 g_hash_table_destroy (key_table);
413 /* @@@ FIXME: Remove empty hash tables. */
415 return count - relation->count;
419 g_relation_select_tuple (gpointer tuple_key,
420 gpointer tuple_value,
423 gpointer *tuple = (gpointer*) tuple_value;
424 GRealTuples *tuples = (GRealTuples*) user_data;
425 gint stride = sizeof (gpointer) * tuples->width;
427 g_assert (tuple_key == tuple_value);
429 memcpy (tuples->data + (tuples->len * tuples->width),
438 * @relation: a #GRelation.
439 * @key: the value to compare with.
440 * @field: the field of each record to match.
441 * @Returns: the records (tuples) that matched.
443 * Returns all of the tuples which have the given key in the given
444 * field. Use g_tuples_index() to access the returned records. The
445 * returned records should be freed with g_tuples_destroy().
447 * Deprecated: 2.26: Rarely used API
450 g_relation_select (GRelation *relation,
455 GHashTable *key_table;
459 g_return_val_if_fail (relation != NULL, NULL);
461 table = relation->hashed_tuple_tables[field];
463 g_return_val_if_fail (table != NULL, NULL);
465 tuples = g_new0 (GRealTuples, 1);
466 key_table = g_hash_table_lookup (table, key);
469 return (GTuples*)tuples;
471 count = g_relation_count (relation, key, field);
473 tuples->data = g_malloc (sizeof (gpointer) * relation->fields * count);
474 tuples->width = relation->fields;
476 g_hash_table_foreach (key_table, g_relation_select_tuple, tuples);
478 g_assert (count == tuples->len);
480 return (GTuples*)tuples;
485 * @relation: a #GRelation.
486 * @key: the value to compare with.
487 * @field: the field of each record to match.
488 * @Returns: the number of matches.
490 * Returns the number of tuples in a #GRelation that have the given
491 * value in the given field.
493 * Deprecated: 2.26: Rarely used API
496 g_relation_count (GRelation *relation,
501 GHashTable *key_table;
503 g_return_val_if_fail (relation != NULL, 0);
505 table = relation->hashed_tuple_tables[field];
507 g_return_val_if_fail (table != NULL, 0);
509 key_table = g_hash_table_lookup (table, key);
514 return g_hash_table_size (key_table);
519 * @relation: a #GRelation.
520 * @Varargs: the fields of the record to compare. The number must match
521 * the number of fields in the #GRelation.
522 * @Returns: %TRUE if a record matches.
524 * Returns %TRUE if a record with the given values exists in a
525 * #GRelation. Note that the values are compared directly, so that, for
526 * example, two copies of the same string will not match.
528 * Deprecated: 2.26: Rarely used API
531 g_relation_exists (GRelation *relation, ...)
533 gpointer *tuple = g_slice_alloc (relation->fields * sizeof (gpointer));
538 va_start(args, relation);
540 for (i = 0; i < relation->fields; i += 1)
541 tuple[i] = va_arg(args, gpointer);
545 result = g_hash_table_lookup (relation->all_tuples, tuple) != NULL;
547 g_slice_free1 (relation->fields * sizeof (gpointer), tuple);
554 * @tuples: the tuple data to free.
556 * Frees the records which were returned by g_relation_select(). This
557 * should always be called after g_relation_select() when you are
558 * finished with the records. The records are not removed from the
561 * Deprecated: 2.26: Rarely used API
564 g_tuples_destroy (GTuples *tuples0)
566 GRealTuples *tuples = (GRealTuples*) tuples0;
570 g_free (tuples->data);
577 * @tuples: the tuple data, returned by g_relation_select().
578 * @index_: the index of the record.
579 * @field: the field to return.
580 * @Returns: the field of the record.
582 * Gets a field from the records returned by g_relation_select(). It
583 * returns the given field of the record at the given index. The
584 * returned value should not be changed.
586 * Deprecated: 2.26: Rarely used API
589 g_tuples_index (GTuples *tuples0,
593 GRealTuples *tuples = (GRealTuples*) tuples0;
595 g_return_val_if_fail (tuples0 != NULL, NULL);
596 g_return_val_if_fail (field < tuples->width, NULL);
598 return tuples->data[index * tuples->width + field];
605 g_relation_print_one (gpointer tuple_key,
606 gpointer tuple_value,
611 GRelation* rel = (GRelation*) user_data;
612 gpointer* tuples = (gpointer*) tuple_value;
614 gstring = g_string_new ("[");
616 for (i = 0; i < rel->fields; i += 1)
618 g_string_append_printf (gstring, "%p", tuples[i]);
620 if (i < (rel->fields - 1))
621 g_string_append (gstring, ",");
624 g_string_append (gstring, "]");
625 g_log (G_LOG_DOMAIN, G_LOG_LEVEL_INFO, "%s", gstring->str);
626 g_string_free (gstring, TRUE);
630 g_relation_print_index (gpointer tuple_key,
631 gpointer tuple_value,
634 GRelation* rel = (GRelation*) user_data;
635 GHashTable* table = (GHashTable*) tuple_value;
637 g_log (G_LOG_DOMAIN, G_LOG_LEVEL_INFO, "*** key %p", tuple_key);
639 g_hash_table_foreach (table,
640 g_relation_print_one,
646 * @relation: a #GRelation.
648 * Outputs information about all records in a #GRelation, as well as
649 * the indexes. It is for debugging.
651 * Deprecated: 2.26: Rarely used API
654 g_relation_print (GRelation *relation)
658 g_log (G_LOG_DOMAIN, G_LOG_LEVEL_INFO, "*** all tuples (%d)", relation->count);
660 g_hash_table_foreach (relation->all_tuples,
661 g_relation_print_one,
664 for (i = 0; i < relation->fields; i += 1)
666 if (relation->hashed_tuple_tables[i] == NULL)
669 g_log (G_LOG_DOMAIN, G_LOG_LEVEL_INFO, "*** index %d", i);
671 g_hash_table_foreach (relation->hashed_tuple_tables[i],
672 g_relation_print_index,
679 #include "galiasdef.c"