14 .TH assoc 3 "23 January 2001" "Straylight/Edgeware" "mLib utilities library"
16 assoc \- tables indexed by atoms
28 .B "#include <mLib/assoc.h>"
30 .BI "void assoc_create(assoc_table *" t );
31 .BI "void assoc_destroy(assoc_table *" t );
33 .BI "void *assoc_find(assoc_table *" t ", atom *" a ", size_t " sz ", unsigned *" f );
34 .BI "void assoc_remove(assoc_table *" t ", void *" b );
36 .BI "atom *ASSOC_ATOM(const void *" p );
38 .BI "void assoc_mkiter(assoc_iter *" i ", assoc_table *" t );
39 .BI "void *assoc_next(assoc_iter *" i );
43 .I "association table"
44 is a data structure which maps atoms (see
46 to arbitrary values. It works in a similar way to the symbol tables
49 except that it uses atoms rather than blocks of data as keys.
55 table: the value structures must include an
59 There are three main data structures:
62 Keeps track of the information associated with a particular table.
65 The header which must be attached to the front of all the value objects.
68 An iterator object, used for enumerating all of the associations stored
71 All of the above structures should be considered
73 don't try looking inside.
74 .SS "Creation and destruction"
77 object itself needs to be allocated by the caller. It is initialized by
78 passing it to the function
80 After initialization, the table contains no entries.
82 Initializing an association table involves allocating some memory. If
83 this allocation fails, an
87 When an association table is no longer needed, the memory occupied by
88 the values and other maintenance structures can be reclaimed by calling
90 Any bits of user data attached to values should previously have been
92 .SS "Adding, searching and removing"
93 Most of the actual work is done by the function
95 It does both lookup and creation, depending on its arguments. To do its
96 job, it needs to know the following bits of information:
99 A pointer to an association table to manipulate.
102 The address of the atom to use as a key.
105 The size of the value block to allocate if the key could not be found.
106 If this is zero, no value is allocated, and a null pointer is returned
107 to indicate an unsuccessful lookup.
110 The address of a `found' flag to set. This is an output parameter. On
113 will set the value of
115 to zero if the key could not be found, or nonzero if it was found. This
116 can be used to tell whether the value returned has been newly allocated,
117 or whether it was already in the table.
119 A symbol can be removed from the table by calling
121 passing the association table itself, and the value block that needs
123 .SS "Enquiries about associations"
126 to an association, the expression
128 has as its value a poinetr to the atom which is that association's key.
129 .SS "Enumerating associations"
130 Enumerating the values in an association table is fairly simple.
133 object from somewhere. Attach it to an association table by calling
135 and passing in the addresses of the iterator and the symbol table.
138 will return a different value from the association table, until all of
139 them have been enumerated, at which point,
141 returns a null pointer.
143 It's safe to remove the symbol you've just been returned by
145 However, it's not safe to remove any other symbol. So don't do that.
147 When you've finished with an iterator, it's safe to just throw it away.
148 You don't need to call any functions beforehand.
155 Mark Wooding, <mdw@distorted.org.uk>