3 .\" Manual for association tables based on atoms
5 .\" (c) 2001, 2005, 2009, 2023, 2024 Straylight/Edgeware
8 .\"----- Licensing notice ---------------------------------------------------
10 .\" This file is part of the mLib utilities library.
12 .\" mLib is free software: you can redistribute it and/or modify it under
13 .\" the terms of the GNU Library General Public License as published by
14 .\" the Free Software Foundation; either version 2 of the License, or (at
15 .\" your option) any later version.
17 .\" mLib is distributed in the hope that it will be useful, but WITHOUT
18 .\" ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
19 .\" FITNESS FOR A PARTICULAR PURPOSE. See the GNU Library General Public
20 .\" License for more details.
22 .\" You should have received a copy of the GNU Library General Public
23 .\" License along with mLib. If not, write to the Free Software
24 .\" Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307,
27 .\"--------------------------------------------------------------------------
28 .so ../defs.man \" @@@PRE@@@
30 .\"--------------------------------------------------------------------------
31 .TH assoc 3mLib "23 January 2001" "Straylight/Edgeware" "mLib utilities library"
41 .\"--------------------------------------------------------------------------
43 assoc \- tables indexed by atoms
45 .\"--------------------------------------------------------------------------
49 .B "#include <mLib/assoc.h>"
51 .B "typedef struct { ...\& } assoc_table;"
52 .B "typedef struct { ...\& } assoc_base;"
54 .BI "void assoc_create(assoc_table *" t );
55 .BI "void assoc_destroy(assoc_table *" t );
57 .BI "void *assoc_find(assoc_table *" t ", atom *" a ", size_t " sz ", unsigned *" f );
58 .BI "void assoc_remove(assoc_table *" t ", void *" b );
60 .BI "atom *ASSOC_ATOM(const void *" p );
62 .BI "void assoc_mkiter(assoc_iter *" i ", assoc_table *" t );
63 .BI "void *assoc_next(assoc_iter *" i );
66 .\"--------------------------------------------------------------------------
70 .I "association table"
71 is a data structure which maps atoms (see
73 to arbitrary values. It works in a similar way to the symbol tables
76 except that it uses atoms rather than blocks of data as keys.
82 table: the value structures must include an
86 There are three main data structures:
89 Keeps track of the information associated with a particular table.
92 The header which must be attached to the front of all the value objects.
95 An iterator object, used for enumerating all of the associations stored
98 All of the above structures should be considered
100 don't try looking inside.
102 .SS "Creation and destruction"
105 object itself needs to be allocated by the caller. It is initialized by
106 passing it to the function
108 After initialization, the table contains no entries.
110 Initializing an association table involves allocating some memory. If
111 this allocation fails, an
115 When an association table is no longer needed, the memory occupied by
116 the values and other maintenance structures can be reclaimed by calling
118 Any bits of user data attached to values should previously have been
121 .SS "Adding, searching and removing"
122 Most of the actual work is done by the function
124 It does both lookup and creation, depending on its arguments. To do its
125 job, it needs to know the following bits of information:
127 .BI "assoc_table *" t
128 A pointer to an association table to manipulate.
131 The address of the atom to use as a key.
134 The size of the value block to allocate if the key could not be found.
135 If this is zero, no value is allocated, and a null pointer is returned
136 to indicate an unsuccessful lookup.
139 The address of a `found' flag to set. This is an output parameter. On
142 will set the value of
144 to zero if the key could not be found, or nonzero if it was found. This
145 can be used to tell whether the value returned has been newly allocated,
146 or whether it was already in the table.
148 A symbol can be removed from the table by calling
150 passing the association table itself, and the value block that needs
153 .SS "Enquiries about associations"
156 to an association, the expression
158 has as its value a poinetr to the atom which is that association's key.
160 .SS "Enumerating associations"
161 Enumerating the values in an association table is fairly simple.
164 object from somewhere. Attach it to an association table by calling
166 and passing in the addresses of the iterator and the symbol table.
169 will return a different value from the association table, until all of
170 them have been enumerated, at which point,
172 returns a null pointer.
174 It's safe to remove the symbol you've just been returned by
176 However, it's not safe to remove any other symbol. So don't do that.
178 When you've finished with an iterator, it's safe to just throw it away.
179 You don't need to call any functions beforehand.
181 .\"--------------------------------------------------------------------------
189 .\"--------------------------------------------------------------------------
192 Mark Wooding, <mdw@distorted.org.uk>
194 .\"----- That's all, folks --------------------------------------------------