Debian Codegraph @

lib/cdt/cdt.3

raw
1 .fp 5 CW
2 .TH LIBCDT 3
3 .SH NAME
4 \fBCdt\fR \- container data types
5 .SH SYNOPSIS
6 .de Tp
7 .fl
8 .ne 2
9 .TP
10 ..
11 .de Ss
12 .fl
13 .ne 2
14 .SS "\\$1"
15 ..
16 .de Cs
17 .nf
18 .ft 5
19 ..
20 .de Ce
21 .ft 1
22 .fi
23 ..
24 .ta 1.0i 2.0i 3.0i 4.0i 5.0i
25 .Cs
26 #include <cdt.h>
27 .Ce
28 .Ss "DICTIONARY TYPES"
29 .Cs
30 Dt_t;
31 Dtdisc_t;
32 Dtmethod_t;
33 Dtlink_t;
34 Dtstat_t;
35 .Ce
36 .Ss "DICTIONARY CONTROL"
37 .Cs
38 Dt_t* dtopen(const Dtdisc_t* disc, const Dtmethod_t* meth);
39 int dtclose(Dt_t* dt);
40 void dtclear(dt);
41 Dtmethod_t* dtmethod(Dt_t* dt, const Dtmethod_t* meth);
42 Dtdisc_t* dtdisc(Dt_t* dt, const Dtdisc_t* disc);
43 Dt_t* dtview(Dt_t* dt, Dt_t* view);
44 .Ce
45 .Ss "STORAGE METHODS"
46 .Cs
47 Dtmethod_t* Dtset;
48 Dtmethod_t* Dtoset;
49 Dtmethod_t* Dtobag;
50 .Ce
51 .Ss "DISCIPLINE"
52 .Cs
53 #define DTDISC(disc,key,size,link,makef,freef,comparf)
54 typedef void* (*Dtmake_f)(void*, Dtdisc_t*);
55 typedef void (*Dtfree_f)(void*, Dtdisc_t*);
56 typedef int (*Dtcompar_f)(Dt_t*, void*, void*, Dtdisc_t*);
57 .Ce
58 .Ss "OBJECT OPERATIONS"
59 .Cs
60 void* dtinsert(Dt_t* dt, void* obj);
61 void* dtdelete(Dt_t* dt, void* obj);
62 void* dtdetach(Dt_t* dt, void* obj);
63 void* dtsearch(Dt_t* dt, void* obj);
64 void* dtmatch(Dt_t* dt, void* key);
65 void* dtfirst(Dt_t* dt);
66 void* dtnext(Dt_t* dt, void* obj);
67 void* dtlast(Dt_t* dt);
68 void* dtprev(Dt_t* dt, void* obj);
69 void* dtfinger(Dt_t* dt);
70 void* dtrenew(Dt_t* dt, void* obj);
71 int dtwalk(Dt_t* dt, int (*userf)(void*, void*), void*);
72 Dtlink_t* dtflatten(Dt_t* dt);
73 Dtlink_t* dtlink(Dt_t*, Dtlink_t* link);
74 void* dtobj(Dt_t* dt, Dtlink_t* link);
75 Dtlink_t* dtextract(Dt_t* dt);
76 int dtrestore(Dt_t* dt, Dtlink_t* link);
77 .Ce
78 .Ss "DICTIONARY STATUS"
79 .Cs
80 int dtsize(Dt_t* dt);
81 int dtstat(Dt_t* dt, Dtstat_t*, int all);
82 .Ce
83 .Ss "HASH FUNCTIONS"
84 .Cs
85 unsigned int dtstrhash(void *str, int n);
86 .Ce
87 .SH DESCRIPTION
88 .PP
89 \fICdt\fP manages run-time dictionaries using standard container data types:
90 unordered set/multiset, ordered set/multiset, list, and stack.
91 .PP
92 .Ss "DICTIONARY TYPES"
93 .PP
94 .Ss " Dt_t"
95 This is the type of a dictionary handle.
96 .PP
97 .Ss " Dtdisc_t"
98 This defines the type of a discipline structure which describes
99 object lay-out and manipulation functions.
100 .PP
101 .Ss " Dtmethod_t"
102 This defines the type of a container method.
103 .PP
104 .Ss " Dtlink_t"
105 This is the type of a dictionary object holder (see \f5dtdisc()\fP.)
106 .PP
107 .Ss " Dtstat_t"
108 This is the type of a structure to return dictionary statistics (see \f5dtstat()\fP.)
109 .PP
110 .Ss "DICTIONARY CONTROL"
111 .PP
112 .Ss " Dt_t* dtopen(const Dtdisc_t* disc, const Dtmethod_t* meth)"
113 This creates a new dictionary.
114 \f5disc\fP is a discipline structure to describe object format.
115 \f5meth\fP specifies a manipulation method.
116 \f5dtopen()\fP returns the new dictionary or \f5NULL\fP on error.
117 .PP
118 .Ss " int dtclose(Dt_t* dt)"
119 This deletes \f5dt\fP and its objects.
120 Note that \f5dtclose()\fP fails if \f5dt\fP is being viewed by
121 some other dictionaries (see \f5dtview()\fP).
122 \f5dtclose()\fP returns \f50\fP on success and \f5-1\fP on error.
123 .PP
124 .Ss " void dtclear(Dt_t* dt)"
125 This deletes all objects in \f5dt\fP without closing \f5dt\fP.
126 .PP
127 .Ss " Dtmethod_t dtmethod(Dt_t* dt, const Dtmethod_t* meth)"
128 If \f5meth\fP is \f5NULL\fP, \f5dtmethod()\fP returns the current method.
129 Otherwise, it changes the storage method of \f5dt\fP to \f5meth\fP.
130 Switching to and from \f5Dtset\fP and \f5Dtoset/Dtobag\fP may cause
131 objects to be rehashed, reordered, or removed as the case requires.
132 \f5dtmethod()\fP returns the previous method or \f5NULL\fP on error.
133 .PP
134 .Ss " Dtdisc_t* dtdisc(Dt_t* dt, const Dtdisc_t* disc)"
135 If \f5disc\fP is \f5NULL\fP, \f5dtdisc()\fP returns the current discipline.
136 Otherwise, it changes the discipline of \f5dt\fP to \f5disc\fP.
137 Objects may be rehashed, reordered, or removed as appropriate.
138 \f5dtdisc()\fP returns the previous discipline on success
139 and \f5NULL\fP on error.
140 .PP
141 .Ss " Dt_t* dtview(Dt_t* dt, Dt_t* view)"
142 A viewpath allows a search or walk starting from a dictionary to continue to another.
143 \f5dtview()\fP first terminates any current view from \f5dt\fP to another dictionary.
144 Then, if \f5view\fP is \f5NULL\fP, \f5dtview\fP returns the terminated view dictionary.
145 If \f5view\fP is not \f5NULL\fP, a viewpath from \f5dt\fP to \f5view\fP is established.
146 \f5dtview()\fP returns \f5dt\fP on success and \f5NULL\fP on error.
147 .PP
148 It is an error to have dictionaries on a viewpath with different storage methods.
149 In addition, dictionaries on the same view path should
150 treat objects in a consistent manner with respect to comparison or hashing.
151 If not, undefined behaviors may result.
152 .PP
153 .Ss "STORAGE METHODS"
154 .PP
155 Storage methods are of type \f5Dtmethod_t*\fP.
156 \fICdt\fP supports the following methods:
157 .PP
158 .Ss " Dtoset"
159 .Ss " Dtobag"
160 Objects are ordered by comparisons.
161 \f5Dtoset\fP keeps unique objects.
162 \f5Dtobag\fP allows repeatable objects.
163 .PP
164 .Ss " Dtset"
165 Objects are unordered.
166 \f5Dtset\fP keeps unique objects.
167 This method uses a hash table with chaining to manage the objects.
168 .PP
169 .Ss "DISCIPLINE"
170 .PP
171 Object format and associated management functions are
172 defined in the type \f5Dtdisc_t\fP:
173 .Cs
174 typedef struct
175 { int key, size;
176 int link;
177 Dtmake_f makef;
178 Dtfree_f freef;
179 Dtcompar_f comparf;
180 } Dtdisc_t;
181 .Ce
182 .Ss " int key, size"
183 Each object \f5obj\fP is identified by a key used for object comparison or hashing.
184 \f5key\fP should be non-negative and defines an offset into \f5obj\fP.
185 If \f5size\fP is negative, the key is a null-terminated
186 string with starting address \f5*(void**)((char*)obj+key)\fP.
187 If \f5size\fP is zero, the key is a null-terminated string with starting address
188 \f5(void*)((char*)obj+key)\fP.
189 Finally, if \f5size\fP is positive, the key is a byte array of length \f5size\fP
190 starting at \f5(void*)((char*)obj+key)\fP.
191 .PP
192 .Ss " int link"
193 Let \f5obj\fP be an object to be inserted into \f5dt\fP as discussed below.
194 If \f5link\fP is negative, an internally allocated object holder is used
195 to hold \f5obj\fP. Otherwise, \f5obj\fP should have
196 a \f5Dtlink_t\fP structure embedded \f5link\fP bytes into it,
197 i.e., at address \f5(Dtlink_t*)((char*)obj+link)\fP.
198 .PP
199 .Ss " void* (*makef)(void* obj, Dtdisc_t* disc)"
200 If \f5makef\fP is not \f5NULL\fP,
201 \f5dtinsert(dt,obj)\fP will call it
202 to make a copy of \f5obj\fP suitable for insertion into \f5dt\fP.
203 If \f5makef\fP is \f5NULL\fP, \f5obj\fP itself will be inserted into \f5dt\fP.
204 .PP
205 .Ss " void (*freef)(void* obj, Dtdisc_t* disc)"
206 If not \f5NULL\fP,
207 \f5freef\fP is used to destroy data associated with \f5obj\fP.
208 .PP
209 .Ss "int (*comparf)(Dt_t* dt, void* key1, void* key2, Dtdisc_t* disc)"
210 If not \f5NULL\fP, \f5comparf\fP is used to compare two keys.
211 Its return value should be \f5<0\fP, \f5=0\fP, or \f5>0\fP to indicate
212 whether \f5key1\fP is smaller, equal to, or larger than \f5key2\fP.
213 All three values are significant for method \f5Dtoset\fP and \f5Dtobag\fP.
214 For other methods, a zero value
215 indicates equality and a non-zero value indicates inequality.
216 If \f5(*comparf)()\fP is \f5NULL\fP, an internal function is used
217 to compare the keys as defined by the \f5Dtdisc_t.size\fP field.
218 .PP
219 .Ss "#define DTDISC(disc,key,size,link,makef,freef,comparf)"
220 This macro function initializes the discipline pointed to by \f5disc\fP
221 with the given values.
222 .PP
223 .Ss "OBJECT OPERATIONS"
224 .PP
225 .Ss " void* dtinsert(Dt_t* dt, void* obj)"
226 This function adds an object prototyped by \f5obj\fP into \f5dt\fP.
227 \f5dtinsert()\fP performs the same function
228 for all methods.
229 If there is an existing object in \f5dt\fP matching \f5obj\fP
230 and the storage method is \f5Dtset\fP or \f5Dtoset\fP,
231 \f5dtinsert()\fP will simply return the matching object.
232 Otherwise, a new object is inserted according to the method in use.
233 See \f5Dtdisc_t.makef\fP for object construction.
234 The new object or a matching object as noted will be returned on success
235 while \f5NULL\fP is returned on error.
236 .PP
237 .Ss " void* dtdelete(Dt_t* dt, void* obj)"
238 If \f5obj\fP is not \f5NULL\fP, there are two cases.
239 If the method in use is not \f5Dtobag\fP,
240 the first object matching \f5obj\fP is deleted.
241 On the other hand, if the method in use is or \f5Dtobag\fP,
242 the library check to see if \f5obj\fP is in the dictionary and delete it.
243 If \f5obj\fP is not in the dictionary, some object matching it will be deleted.
244 See \f5Dtdisc_t.freef\fP for object destruction.
245 \f5dtdelete()\fP returns the deleted object (even if it was deallocated)
246 or \f5NULL\fP on error.
247 .PP
248 .Ss " void* dtdetach(Dt_t* dt, void* obj)"
249 This function is similar to \f5dtdelete()\fP but the object to be deleted
250 from \f5dt\fP will not be freed (via the discipline \f5freef\fP function).
251 .PP
252 .Ss " void* dtsearch(Dt_t* dt, void* obj)"
253 .Ss " void* dtmatch(Dt_t* dt, void* key)"
254 These functions find an object matching \f5obj\fP or \f5key\fP either from \f5dt\fP or
255 from some dictionary accessible from \f5dt\fP via a viewpath (see \f5dtview()\fP.)
256 \f5dtsearch()\fP and \f5dtmatch()\fP return the matching object or
257 \f5NULL\fP on failure.
258 .PP
259 .Ss " void* dtfirst(Dt_t* dt)"
260 .Ss " void* dtnext(Dt_t* dt, void* obj)"
261 \f5dtfirst()\fP returns the first object in \f5dt\fP.
262 \f5dtnext()\fP returns the object following \f5obj\fP.
263 Objects are ordered based on the storage method in use.
264 For \f5Dtoset\fP and \f5Dtobag\fP, objects are ordered by object comparisons.
265 For \f5Dtset\fP,
266 objects are ordered by some internal order (more below).
267 Thus, objects in a dictionary or a viewpath can be walked using
268 a \f5for(;;)\fP loop as below.
269 .Cs
270 for(obj = dtfirst(dt); obj; obj = dtnext(dt,obj))
271 .Ce
272 When a dictionary uses \f5Dtset\fP,
273 the object order is determined upon a call to \f5dtfirst()\fP/\f5dtlast()\fP.
274 This order is frozen until a call \f5dtnext()\fP/\f5dtprev()\fP returns \f5NULL\fP
275 or when these same functions are called with a \f5NULL\fP object argument.
276 It is important that a \f5dtfirst()/dtlast()\fP call be
277 balanced by a \f5dtnext()/dtprev()\fP call as described.
278 Nested loops will require multiple balancing, once per loop.
279 If loop balancing is not done carefully, either performance is degraded
280 or unexpected behaviors may result.
281 .Ss " void* dtlast(Dt_t* dt)"
282 .Ss " void* dtprev(Dt_t* dt, void* obj)"
283 \f5dtlast()\fP and \f5dtprev()\fP are like \f5dtfirst()\fP and \f5dtnext()\fP
284 but work in reverse order.
285 Note that dictionaries on a viewpath are still walked in order
286 but objects in each dictionary are walked in reverse order.
287 .PP
288 .Ss " void* dtfinger(Dt_t* dt)"
289 This function returns the \fIcurrent object\fP of \f5dt\fP, if any.
290 The current object is defined after a successful call to one of
291 \f5dtsearch()\fP, \f5dtmatch()\fP, \f5dtinsert()\fP,
292 \f5dtfirst()\fP, \f5dtnext()\fP, \f5dtlast()\fP, or \f5dtprev()\fP.
293 As a side effect of this implementation of \fICdt\fP,
294 when a dictionary is based on \f5Dtoset\fP and \f5Dtobag\fP,
295 the current object is always defined and is the root of the tree.
296 .PP
297 .Ss " void* dtrenew(Dt_t* dt, void* obj)"
298 This function repositions and perhaps rehashes
299 an object \f5obj\fP after its key has been changed.
300 \f5dtrenew()\fP only works if \f5obj\fP is the current object (see \f5dtfinger()\fP).
301 .PP
302 .Ss " dtwalk(Dt_t* dt, int (*userf)(void*, void*), void* data)"
303 This function calls \f5(*userf)(obj,data)\fP on each object in \f5dt\fP and
304 other dictionaries viewable from it.
305 If \f5userf()\fP returns a \f5<0\fP value,
306 \f5dtwalk()\fP terminates and returns the same value.
307 \f5dtwalk()\fP returns \f50\fP on completion.
308 .PP
309 .Ss " Dtlink_t* dtflatten(Dt_t* dt)"
310 .Ss " Dtlink_t* dtlink(Dt_t* dt, Dtlink_t* link)"
311 .Ss " void* dtobj(Dt_t* dt, Dtlink_t* link)"
312 Using \f5dtfirst()/dtnext()\fP or \f5dtlast()/dtprev()\fP
313 to walk a single dictionary can incur significant cost due to function calls.
314 For efficient walking of a single directory (i.e., no viewpathing),
315 \f5dtflatten()\fP and \f5dtlink()\fP can be used.
316 Objects in \f5dt\fP are made into a linked list and walked as follows:
317 .Cs
318 for(link = dtflatten(dt); link; link = dtlink(dt,link) )
319 .Ce
320 .PP
321 Note that \f5dtflatten()\fP returns a list of type \f5Dtlink_t*\fP,
322 not \f5void*\fP. That is, it returns a dictionary holder pointer,
323 not a user object pointer
324 (although both are the same if the discipline field \f5link\fP is zero.)
325 The macro function \f5dtlink()\fP
326 returns the dictionary holder object following \f5link\fP.
327 The macro function \f5dtobj(dt,link)\fP
328 returns the user object associated with \f5link\fP,
329 Beware that the flattened object list is unflattened on any
330 dictionary operations other than \f5dtlink()\fP.
331 .PP
332 .Ss " Dtlink_t* dtextract(Dt_t* dt)"
333 .Ss " int dtrestore(Dt_t* dt, Dtlink_t* link)"
334 \f5dtextract()\fP extracts all objects from \f5dt\fP and makes it appear empty.
335 \f5dtrestore()\fP repopulates \f5dt\fP with
336 objects previously obtained via \f5dtextract()\fP.
337 \f5dtrestore()\fP will fail if \f5dt\fP is not empty.
338 These functions can be used
339 to share a same \f5dt\fP handle among many sets of objects.
340 They are useful to reduce dictionary overhead
341 in an application that creates many concurrent dictionaries.
342 It is important that the same discipline and method are in use at both
343 extraction and restoration. Otherwise, undefined behaviors may result.
344 .PP
345 .Ss "DICTIONARY INFORMATION"
346 .PP
347 .Ss " int dtsize(Dt_t* dt)"
348 This function returns the number of objects stored in \f5dt\fP.
349 .PP
350 .Ss " int dtstat(Dt_t *dt, Dtstat_t* st, int all)"
351 This function reports dictionary statistics.
352 If \f5all\fP is non-zero, all fields of \f5st\fP are filled.
353 Otherwise, only the \f5dt_type\fP and \f5dt_size\fP fields are filled.
354 It returns \f50\fP on success and \f5-1\fP on error.
355 .PP
356 \f5Dtstat_t\fP contains the below fields:
357 .Tp
358 \f5int dt_type\fP:
359 This is one of \f5DT_SET\fP, \f5DT_OSET\fP, and \f5DT_OBAG\fP.
360 .Tp
361 \f5int dt_size\fP:
362 This contains the number of objects in the dictionary.
363 .Tp
364 \f5size_t dt_n\fP:
365 For \f5Dtset\fP,
366 this is the number of non-empty chains in the hash table.
367 For \f5Dtoset\fP and \f5Dtobag\fP,
368 this is the deepest level in the tree (counting from zero.)
369 Each level in the tree contains all nodes of equal distance from the root node.
370 \f5dt_n\fP and the below two fields are undefined for other methods.
371 .Tp
372 \f5size_t dt_max\fP:
373 For \f5Dtset\fP, this is the size of a largest chain.
374 For \f5Dtoset\fP and \f5Dtobag\fP, this is the size of a largest level.
375 .Tp
376 \f5size_t *dt_count\fP:
377 For \f5Dtset\fP,
378 this is the list of counts for chains of particular sizes.
379 For example, \f5dt_count[1]\fP is the number of chains of size \f51\fP.
380 For \f5Dtoset\fP and \f5Dtobag\fP, this is the list of sizes of the levels.
381 For example, \f5dt_count[1]\fP is the size of level \f51\fP.
382 .PP
383 .Ss "HASH FUNCTIONS"
384 .PP
385 .Ss " unsigned int dtstrhash(void *str, int n)"
386 This function computes hash values from bytes or strings.
387 \f5dtstrhash()\fP computes a new hash value from string \f5str\fP.
388 If \f5n\fP is positive, \f5str\fP is a byte array of length \f5n\fP;
389 otherwise, \f5str\fP is a null-terminated string.
390 .PP
391 .SH IMPLEMENTATION NOTES
392 \f5Dtset\fP are based on hash tables with
393 move-to-front collision chains.
394 \f5Dtoset\fP and \f5Dtobag\fP are based on top-down splay trees.
395 .PP
396 .SH AUTHOR
397 Kiem-Phong Vo, [email protected]