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
30Void_t*;
31Dt_t;
32Dtdisc_t;
33Dtmethod_t;
34Dtlink_t;
35Dtstat_t;
36.Ce
37.Ss "DICTIONARY CONTROL"
38.Cs
39Dt_t*       dtopen(const Dtdisc_t* disc, const Dtmethod_t* meth);
40int         dtclose(Dt_t* dt);
41void        dtclear(dt);
42Dtmethod_t* dtmethod(Dt_t* dt, const Dtmethod_t* meth);
43Dtdisc_t*   dtdisc(Dt_t* dt, const Dtdisc_t* disc, int type);
44Dt_t*       dtview(Dt_t* dt, Dt_t* view);
45int         dttreeset(Dt_t* dt, int minp, int balance);
46.Ce
47.Ss "STORAGE METHODS"
48.Cs
49Dtmethod_t* Dtset;
50Dtmethod_t* Dtbag;
51Dtmethod_t* Dtoset;
52Dtmethod_t* Dtobag;
53Dtmethod_t* Dtlist;
54Dtmethod_t* Dtstack;
55Dtmethod_t* Dtqueue;
56.Ce
57.Ss "DISCIPLINE"
58.Cs
59#define DTOFFSET(struct_s,member)
60#define DTDISC(disc,key,size,link,makef,freef,comparf,hashf,memoryf,eventf)
61typedef Void_t*      (*Dtmake_f)(Dt_t*, Void_t*, Dtdisc_t*);
62typedef void         (*Dtfree_f)(Dt_t*, Void_t*, Dtdisc_t*);
63typedef int          (*Dtcompar_f)(Dt_t*, Void_t*, Void_t*, Dtdisc_t*);
64typedef unsigned int (*Dthash_f)(Dt_t*, Void_t*, Dtdisc_t*);
65typedef Void_t*      (*Dtmemory_f)(Dt_t*, Void_t*, size_t, Dtdisc_t*);
66typedef int          (*Dtevent_f)(Dt_t*, int, Void_t*, Dtdisc_t*);
67.Ce
68.Ss "OBJECT OPERATIONS"
69.Cs
70Void_t*   dtinsert(Dt_t* dt, Void_t* obj);
71Void_t*   dtdelete(Dt_t* dt, Void_t* obj);
72Void_t*   dtattach(Dt_t* dt, Void_t* obj);
73Void_t*   dtdetach(Dt_t* dt, Void_t* obj);
74Void_t*   dtsearch(Dt_t* dt, Void_t* obj);
75Void_t*   dtmatch(Dt_t* dt, Void_t* key);
76Void_t*   dtfirst(Dt_t* dt);
77Void_t*   dtnext(Dt_t* dt, Void_t* obj);
78Void_t*   dtlast(Dt_t* dt);
79Void_t*   dtprev(Dt_t* dt, Void_t* obj);
80Void_t*   dtfinger(Dt_t* dt);
81Void_t*   dtrenew(Dt_t* dt, Void_t* obj);
82int       dtwalk(Dt_t* dt, int (*userf)(Dt_t*, Void_t*, Void_t*), Void_t*);
83Dtlink_t* dtflatten(Dt_t* dt);
84Dtlink_t* dtlink(Dt_t*, Dtlink_t* link);
85Void_t*   dtobj(Dt_t* dt, Dtlink_t* link);
86Dtlink_t* dtextract(Dt_t* dt);
87int       dtrestore(Dt_t* dt, Dtlink_t* link);
88
89#define   DTTREESEARCH(Dt_t* dt, Void_t* obj, action)
90#define   DTTREEMATCH(Dt_t* dt, Void_t* key, action)
91.Ce
92.Ss "DICTIONARY STATUS"
93.Cs
94Dt_t*     dtvnext(Dt_t* dt);
95int       dtvcount(Dt_t* dt);
96Dt_t*     dtvhere(Dt_t* dt);
97int       dtsize(Dt_t* dt);
98int       dtstat(Dt_t* dt, Dtstat_t*, int all);
99.Ce
100.Ss "HASH FUNCTIONS"
101.Cs
102unsigned int dtstrhash(unsigned int h, char* str, int n);
103unsigned int dtcharhash(unsigned int h, unsigned char c);
104.Ce
105.SH DESCRIPTION
106.PP
107\fICdt\fP manages run-time dictionaries using standard container data types:
108unordered set/multiset, ordered set/multiset, list, stack, and queue.
109.PP
110.Ss "DICTIONARY TYPES"
111.PP
112.Ss "  Void_t*"
113This type is used to pass objects between \fICdt\fP and application code.
114\f5Void_t\fP is defined as \f5void\fP for ANSI-C and C++
115and \f5char\fP for other compilation environments.
116.PP
117.Ss "  Dt_t"
118This is the type of a dictionary handle.
119.PP
120.Ss "  Dtdisc_t"
121This defines the type of a discipline structure which describes
122object lay-out and manipulation functions.
123.PP
124.Ss "  Dtmethod_t"
125This defines the type of a container method.
126.PP
127.Ss "  Dtlink_t"
128This is the type of a dictionary object holder (see \f5dtdisc()\fP.)
129.PP
130.Ss "  Dtstat_t"
131This is the type of a structure to return dictionary statistics (see \f5dtstat()\fP.)
132.PP
133.Ss "DICTIONARY CONTROL"
134.PP
135.Ss "  Dt_t* dtopen(const Dtdisc_t* disc, const Dtmethod_t* meth)"
136This creates a new dictionary.
137\f5disc\fP is a discipline structure to describe object format.
138\f5meth\fP specifies a manipulation method.
139\f5dtopen()\fP returns the new dictionary or \f5NULL\fP on error.
140See also the events \f5DT_OPEN\fP and \f5DT_ENDOPEN\fP below.
141.PP
142.Ss "  int dtclose(Dt_t* dt)"
143This deletes \f5dt\fP and its objects.
144Note that \f5dtclose()\fP fails if \f5dt\fP is being viewed by
145some other dictionaries (see \f5dtview()\fP).
146\f5dtclose()\fP returns \f50\fP on success and \f5-1\fP on error.
147See also the events \f5DT_CLOSE\fP and \f5DT_ENDCLOSE\fP below.
148.PP
149.Ss "  void dtclear(Dt_t* dt)"
150This deletes all objects in \f5dt\fP without closing \f5dt\fP.
151.PP
152.Ss "  Dtmethod_t dtmethod(Dt_t* dt, const Dtmethod_t* meth)"
153If \f5meth\fP is \f5NULL\fP, \f5dtmethod()\fP returns the current method.
154Otherwise, it changes the storage method of \f5dt\fP to \f5meth\fP.
155Object order remains the same during a
156method switch among \f5Dtlist\fP, \f5Dtstack\fP and \f5Dtqueue\fP.
157Switching to and from \f5Dtset/Dtbag\fP and \f5Dtoset/Dtobag\fP may cause
158objects to be rehashed, reordered, or removed as the case requires.
159\f5dtmethod()\fP returns the previous method or \f5NULL\fP on error.
160.PP
161.Ss "  Dtdisc_t* dtdisc(Dt_t* dt, const Dtdisc_t* disc, int type)"
162If \f5disc\fP is \f5NULL\fP, \f5dtdisc()\fP returns the current discipline.
163Otherwise, it changes the discipline of \f5dt\fP to \f5disc\fP.
164Objects may be rehashed, reordered, or removed as appropriate.
165\f5type\fP can be any bit combination of \f5DT_SAMECMP\fP and \f5DT_SAMEHASH\fP.
166\f5DT_SAMECMP\fP means that objects will compare exactly the same as before
167thus obviating the need for reordering or removing new duplicates.
168\f5DT_SAMEHASH\fP means that hash values of objects remain the same
169thus obviating the need to rehash.
170\f5dtdisc()\fP returns the previous discipline on success
171and \f5NULL\fP on error.
172.PP
173.Ss "  Dt_t* dtview(Dt_t* dt, Dt_t* view)"
174A viewpath allows a search or walk starting from a dictionary to continue to another.
175\f5dtview()\fP first terminates any current view from \f5dt\fP to another dictionary.
176Then, if \f5view\fP is \f5NULL\fP, \f5dtview\fP returns the terminated view dictionary.
177If \f5view\fP is not \f5NULL\fP, a viewpath from \f5dt\fP to \f5view\fP is established.
178\f5dtview()\fP returns \f5dt\fP on success and \f5NULL\fP on error.
179.PP
180If two dictionaries on the same viewpath have the same values for the discipline fields
181\f5Dtdisc_t.link\fP, \f5Dtdisc_t.key\fP, \f5Dtdisc_t.size\fP, and \f5Dtdisc_t.hashf\fP,
182it is expected that key hashing will be the same.
183If not, undefined behaviors may result during a search or a walk.
184.PP
185.Ss "  int dttreeset(Dt_t* dt, int minp, int balance)"
186This function only applies to dictionaries operated under the method \f5Dtoset\fP
187which uses top-down splay trees (see below). It returns 0 on success and -1 on error.
188.Tp
189\f5minp\fP:
190This parameter defines the minimum path length before a search path is adjusted.
191For example, \f5minp\fP equal 0 would mean that search paths are always adjusted.
192If \f5minp\fP is negative, the minimum search path is internally computed based
193on a function of the current dictionary size. This computed value is such that
194if the tree is balanced, it will never require adjusting.
195.Tp
196\f5balance\fP:
197If this is non-zero, the tree will be made balanced.
198.PP
199.Ss "STORAGE METHODS"
200.PP
201Storage methods are of type \f5Dtmethod_t*\fP.
202\fICdt\fP supports the following methods:
203.PP
204.Ss "  Dtoset"
205.Ss "  Dtobag"
206Objects are ordered by comparisons.
207\f5Dtoset\fP keeps unique objects.
208\f5Dtobag\fP allows repeatable objects.
209.PP
210.Ss "  Dtset"
211.Ss "  Dtbag"
212Objects are unordered.
213\f5Dtset\fP keeps unique objects.
214\f5Dtbag\fP allows repeatable objects and always keeps them together
215(note the effect on dictionary walking.)
216These methods use a hash table with chaining to manage the objects.
217See also the event \f5DT_HASHSIZE\fP below on how to manage hash table
218resizing when objects are inserted.
219.PP
220.Ss "  Dtlist"
221Objects are kept in a list.
222New objects are inserted either
223in front of \fIcurrent object\fP (see \f5dtfinger()\fP) if this is defined
224or at list front if there is no current object.
225.PP
226.Ss "  Dtstack"
227Objects are kept in a stack, i.e., in reverse order of insertion.
228Thus, the last object inserted is at stack top
229and will be the first to be deleted.
230.PP
231.Ss "  Dtqueue"
232Objects are kept in a queue, i.e., in order of insertion.
233Thus, the first object inserted is at queue head
234and will be the first to be deleted.
235.PP
236.Ss "DISCIPLINE"
237.PP
238Object format and associated management functions are
239defined in the type \f5Dtdisc_t\fP:
240.Cs
241    typedef struct
242    { int        key, size;
243      int        link;
244      Dtmake_f   makef;
245      Dtfree_f   freef;
246      Dtcompar_f comparf;
247      Dthash_f   hashf;
248      Dtmemory_f memoryf;
249      Dtevent_f  eventf;
250    } Dtdisc_t;
251.Ce
252.Ss "  int key, size"
253Each object \f5obj\fP is identified by a key used for object comparison or hashing.
254\f5key\fP should be non-negative and defines an offset into \f5obj\fP.
255If \f5size\fP is negative, the key is a null-terminated
256string with starting address \f5*(Void_t**)((char*)obj+key)\fP.
257If \f5size\fP is zero, the key is a null-terminated string with starting address
258\f5(Void_t*)((char*)obj+key)\fP.
259Finally, if \f5size\fP is positive, the key is a byte array of length \f5size\fP
260starting at \f5(Void_t*)((char*)obj+key)\fP.
261.PP
262.Ss "  int link"
263Let \f5obj\fP be an object to be inserted into \f5dt\fP as discussed below.
264If \f5link\fP is negative, an internally allocated object holder is used
265to hold \f5obj\fP. Otherwise, \f5obj\fP should have
266a \f5Dtlink_t\fP structure embedded \f5link\fP bytes into it,
267i.e., at address \f5(Dtlink_t*)((char*)obj+link)\fP.
268.PP
269.Ss "  Void_t* (*makef)(Dt_t* dt, Void_t* obj, Dtdisc_t* disc)"
270If \f5makef\fP is not \f5NULL\fP,
271\f5dtinsert(dt,obj)\fP will call it
272to make a copy of \f5obj\fP suitable for insertion into \f5dt\fP.
273If \f5makef\fP is \f5NULL\fP, \f5obj\fP itself will be inserted into \f5dt\fP.
274.PP
275.Ss "  void (*freef)(Dt_t* dt, Void_t* obj, Dtdisc_t* disc)"
276If not \f5NULL\fP,
277\f5freef\fP is used to destroy data associated with \f5obj\fP.
278.PP
279.Ss "int (*comparf)(Dt_t* dt, Void_t* key1, Void_t* key2, Dtdisc_t* disc)"
280If not \f5NULL\fP, \f5comparf\fP is used to compare two keys.
281Its return value should be \f5<0\fP, \f5=0\fP, or \f5>0\fP to indicate
282whether \f5key1\fP is smaller, equal to, or larger than \f5key2\fP.
283All three values are significant for method \f5Dtoset\fP and \f5Dtobag\fP.
284For other methods, a zero value
285indicates equality and a non-zero value indicates inequality.
286If \f5(*comparf)()\fP is \f5NULL\fP, an internal function is used
287to compare the keys as defined by the \f5Dtdisc_t.size\fP field.
288.PP
289.Ss "  unsigned int (*hashf)(Dt_t* dt, Void_t* key, Dtdisc_t* disc)"
290If not \f5NULL\fP,
291\f5hashf\fP is used to compute the hash value of \f5key\fP.
292It is required that keys compared equal will also have same hash values.
293If \f5hashf\fP is \f5NULL\fP, an internal function is used to hash
294the key as defined by the \f5Dtdisc_t.size\fP field.
295.PP
296.Ss "  Void_t* (*memoryf)(Dt_t* dt, Void_t* addr, size_t size, Dtdisc_t* disc)"
297If not \f5NULL\fP, \f5memoryf\fP is used to allocate and free memory.
298When \f5addr\fP is \f5NULL\fP, a memory segment of size \f5size\fP is requested. 
299If \f5addr\fP is not \f5NULL\fP and \f5size\fP is zero, \f5addr\fP is to be freed.
300If \f5addr\fP is not \f5NULL\fP and \f5size\fP is positive,
301\f5addr\fP is to be resized to the given size.
302If \f5memoryf\fP is \f5NULL\fP, \fImalloc(3)\fP is used.
303When dictionaries share memory,
304a record of the first allocated memory segment should be kept
305so that it can be used to initialize other dictionaries (see below.)
306.PP
307.Ss "  int (*eventf)(Dt_t* dt, int type, Void_t* data, Dtdisc_t* disc)"
308If not \f5NULL\fP, \f5eventf\fP announces various events.
309Each event may have particular handling of the return values from \f5eventf\fP.
310But a negative return value typically means failure.
311Following are the events:
312.Tp
313\f5DT_OPEN\fP:
314\f5dt\fP is being opened.
315If \f5eventf\fP returns negative, the opening process terminates with failure.
316If \f5eventf\fP returns zero, the opening process proceeds normally.
317A positive return value indicates special treatment of memory as follows.
318If \f5*(Void_t**)data\fP is set to point to some memory segment
319as discussed in \f5memoryf\fP, that segment of memory is used to start
320the dictionary. If \f5*(Void_t**)data\fP is not set, this indicates that
321\f5dtopen()\fP should allocate the dictionary handle itself with
322\f5memoryf\fP so that all memories pertained to the dictionary are allocated
323via this function.
324.Tp
325\f5DT_ENDOPEN\fP:
326This event announces that \f5dtopen()\fP has successfully opened
327a dictionary and is about to return. The \f5data\fP argument of
328\f5eventf\fP should be the new dictionary handle itself.
329.Tp
330\f5DT_CLOSE\fP:
331\f5dt\fP is about to be closed. If \f5eventf\fP returns zero,
332the closing process proceeds normally. This means that all objects
333in the dictionary will be deleted and all associated memories freed.
334If \f5eventf\fP returns a positive value, objects will not be deleted.
335However, the dictionary handle itself will still be deallocated
336unless it was allocated via \f5memoryf\fP during \f5dtopen()\fP.
337This allows shared/persistent memory dictionaries to retain
338the relevant memories across dictionary openings and closings.
339.Tp
340\f5DT_ENDCLOSE\fP:
341This event announces that \f5dtclose()\fP has successfully closed
342a dictionary and is about to return.
343.Tp
344\f5DT_DISC\fP:
345The discipline of \f5dt\fP is being changed to a new one given in
346\f5(Dtdisc_t*)data\fP.
347.Tp
348\f5DT_METH\fP:
349The method of \f5dt\fP is being changed to a new one given in
350\f5(Dtmethod_t*)data\fP.
351.Tp
352\f5DT_HASHSIZE\fP:
353The hash table (for \f5Dtset\fP and \f5Dtbag\fP) is being resized.
354In this case, \f5*(int*)data\fP has the current size of the table.
355The application can set the new table size by first changing
356\f5*(int*)data\fP to the desired size, then return a positive value.
357The application can also fix the table size at the current value
358forever by setting \f5*(int*)data\fP to a negative value, then
359again return a positive value. A non-positive return value from
360the event handling function means that Cdt will be responsible
361for choosing the hash table size.
362.PP
363.Ss "#define DTOFFSET(struct_s,member)"
364This macro function computes the offset of \f5member\fP from the start
365of structure \f5struct_s\fP. It is useful for getting the offset of
366a \f5Dtlink_t\fP embedded inside an object.
367.PP
368.Ss "#define DTDISC(disc,key,size,link,makef,freef,comparf,hashf,memoryf,eventf)"
369This macro function initializes the discipline pointed to by \f5disc\fP
370with the given values.
371.PP
372.Ss "OBJECT OPERATIONS"
373.PP
374.Ss "  Void_t* dtinsert(Dt_t* dt, Void_t* obj)"
375This inserts an object prototyped by \f5obj\fP into \f5dt\fP.
376If there is an existing object in \f5dt\fP matching \f5obj\fP
377and the storage method is \f5Dtset\fP or \f5Dtoset\fP,
378\f5dtinsert()\fP will simply return the matching object.
379Otherwise, a new object is inserted according to the method in use.
380See \f5Dtdisc_t.makef\fP for object construction.
381\f5dtinsert()\fP returns the new object, a matching object as noted,
382or \f5NULL\fP on error.
383.PP
384.Ss "  Void_t* dtdelete(Dt_t* dt, Void_t* obj)"
385If \f5obj\fP is \f5NULL\fP, methods \f5Dtstack\fP and \f5Dtqueue\fP
386delete respectively stack top or queue head while other methods do nothing.
387If \f5obj\fP is not \f5NULL\fP, there are two cases.
388If the method in use is not \f5Dtbag\fP or \f5Dtobag\fP,
389the first object matching \f5obj\fP is deleted.
390On the other hand, if the method in use is \f5Dtbag\fP or \f5Dtobag\fP,
391the library check to see if \f5obj\fP is in the dictionary and delete it.
392If \f5obj\fP is not in the dictionary, some object matching it will be deleted.
393See \f5Dtdisc_t.freef\fP for object destruction.
394\f5dtdelete()\fP returns the deleted object (even if it was deallocated)
395or \f5NULL\fP on error.
396.PP
397.Ss "  Void_t* dtattach(Dt_t* dt, Void_t* obj)"
398This function is similar to \f5dtinsert()\fP but \f5obj\fP itself
399will be inserted into \f5dt\fP even if a discipline
400function \f5makef\fP is defined.
401.PP
402.Ss "  Void_t* dtdetach(Dt_t* dt, Void_t* obj)"
403This function is similar to \f5dtdelete()\fP but the object to be deleted
404from \f5dt\fP will not be freed (via the discipline \f5freef\fP function).
405.PP
406.Ss "  Void_t* dtsearch(Dt_t* dt, Void_t* obj)"
407.Ss "  Void_t* dtmatch(Dt_t* dt, Void_t* key)"
408These functions find an object matching \f5obj\fP or \f5key\fP either from \f5dt\fP or
409from some dictionary accessible from \f5dt\fP via a viewpath (see \f5dtview()\fP.)
410\f5dtsearch()\fP and \f5dtmatch()\fP return the matching object or
411\f5NULL\fP on failure.
412.PP
413.Ss "  Void_t* dtfirst(Dt_t* dt)"
414.Ss "  Void_t* dtnext(Dt_t* dt, Void_t* obj)"
415\f5dtfirst()\fP returns the first object in \f5dt\fP.
416\f5dtnext()\fP returns the object following \f5obj\fP.
417Objects are ordered based on the storage method in use.
418For \f5Dtoset\fP and \f5Dtobag\fP, objects are ordered by object comparisons.
419For \f5Dtstack\fP, objects are ordered in reverse order of insertion.
420For \f5Dtqueue\fP, objects are ordered in order of insertion.
421For \f5Dtlist\fP, objects are ordered by list position.
422For \f5Dtset\fP and \f5Dtbag\fP,
423objects are ordered by some internal order (more below).
424Thus, objects in a dictionary or a viewpath can be walked using 
425a \f5for(;;)\fP loop as below.
426.Cs
427    for(obj = dtfirst(dt); obj; obj = dtnext(dt,obj))
428.Ce
429When a dictionary uses \f5Dtset\fP or \f5Dtbag\fP,
430the object order is determined upon a call to \f5dtfirst()\fP/\f5dtlast()\fP.
431This order is frozen until a call \f5dtnext()\fP/\f5dtprev()\fP returns \f5NULL\fP
432or when these same functions are called with a \f5NULL\fP object argument.
433It is important that a \f5dtfirst()/dtlast()\fP call be
434balanced by a \f5dtnext()/dtprev()\fP call as described.
435Nested loops will require multiple balancing, once per loop.
436If loop balancing is not done carefully, either performance is degraded
437or unexpected behaviors may result.
438.Ss "  Void_t* dtlast(Dt_t* dt)"
439.Ss "  Void_t* dtprev(Dt_t* dt, Void_t* obj)"
440\f5dtlast()\fP and \f5dtprev()\fP are like \f5dtfirst()\fP and \f5dtnext()\fP
441but work in reverse order.
442Note that dictionaries on a viewpath are still walked in order
443but objects in each dictionary are walked in reverse order.
444.PP
445.Ss "  Void_t* dtfinger(Dt_t* dt)"
446This function returns the \fIcurrent object\fP of \f5dt\fP, if any.
447The current object is defined after a successful call to one of
448\f5dtsearch()\fP, \f5dtmatch()\fP, \f5dtinsert()\fP,
449\f5dtfirst()\fP, \f5dtnext()\fP, \f5dtlast()\fP, or \f5dtprev()\fP.
450As a side effect of this implementation of \fICdt\fP,
451when a dictionary is based on \f5Dtoset\fP and \f5Dtobag\fP,
452the current object is always defined and is the root of the tree.
453.PP
454.Ss "  Void_t* dtrenew(Dt_t* dt, Void_t* obj)"
455This function repositions and perhaps rehashes
456an object \f5obj\fP after its key has been changed.
457\f5dtrenew()\fP only works if \f5obj\fP is the current object (see \f5dtfinger()\fP).
458.PP
459.Ss "  dtwalk(Dt_t* dt, int (*userf)(Dt_t*, Void_t*, Void_t*), Void_t* data)"
460This function calls \f5(*userf)(walk,obj,data)\fP on each object in \f5dt\fP and
461other dictionaries viewable from it.
462\f5walk\fP is the dictionary containing \f5obj\fP.
463If \f5userf()\fP returns a \f5<0\fP value,
464\f5dtwalk()\fP terminates and returns the same value.
465\f5dtwalk()\fP returns \f50\fP on completion.
466.PP
467.Ss "  Dtlink_t* dtflatten(Dt_t* dt)"
468.Ss "  Dtlink_t* dtlink(Dt_t* dt, Dtlink_t* link)"
469.Ss "  Void_t* dtobj(Dt_t* dt, Dtlink_t* link)"
470Using \f5dtfirst()/dtnext()\fP or \f5dtlast()/dtprev()\fP
471to walk a single dictionary can incur significant cost due to function calls.
472For efficient walking of a single directory (i.e., no viewpathing),
473\f5dtflatten()\fP and \f5dtlink()\fP can be used.
474Objects in \f5dt\fP are made into a linked list and walked as follows:
475.Cs
476    for(link = dtflatten(dt); link; link = dtlink(dt,link) )
477.Ce
478.PP
479Note that \f5dtflatten()\fP returns a list of type \f5Dtlink_t*\fP,
480not \f5Void_t*\fP. That is, it returns a dictionary holder pointer,
481not a user object pointer
482(although both are the same if the discipline field \f5link\fP is zero.)
483The macro function \f5dtlink()\fP
484returns the dictionary holder object following \f5link\fP.
485The macro function \f5dtobj(dt,link)\fP
486returns the user object associated with \f5link\fP,
487Beware that the flattened object list is unflattened on any
488dictionary operations other than \f5dtlink()\fP.
489.PP
490.Ss "  Dtlink_t* dtextract(Dt_t* dt)"
491.Ss "  int dtrestore(Dt_t* dt, Dtlink_t* link)"
492\f5dtextract()\fP extracts all objects from \f5dt\fP and makes it appear empty.
493\f5dtrestore()\fP repopulates \f5dt\fP with
494objects previously obtained via \f5dtextract()\fP.
495\f5dtrestore()\fP will fail if \f5dt\fP is not empty.
496These functions can be used
497to share a same \f5dt\fP handle among many sets of objects.
498They are useful to reduce dictionary overhead
499in an application that creates many concurrent dictionaries.
500It is important that the same discipline and method are in use at both
501extraction and restoration. Otherwise, undefined behaviors may result.
502.PP
503.Ss "  #define   DTTREESEARCH(Dt_t* dt, Void_t* obj, action)"
504.Ss "  #define   DTTREEMATCH(Dt_t* dt, Void_t* key, action)"
505These macro functions are analogues of \f5dtsearch()\fP and \f5dtmatch()\fP
506but they can only be used on a dictionary based on a binary
507search tree, i.e., \f5Dtoset\fP or \f5Dtobag\fP.
508.Tp
509\f5obj\fP or \f5key\fP:
510These are used to find a matching object. If there is no match,
511the result is \f5NULL\fP.
512.Tp
513\f5action\fP:
514The matching object \f5o\fP (which may be \f5NULL\fP) will be processed as follow:
515
516.Cs
517    action (o);
518.Ce
519
520Since \f5action\fP is used verbatim, it can be any C code
521fragment combinable with \f5(o)\fP to form a syntactically correct C statement.
522For example, suppose that the matching object is an integer, the below code
523accumulates the integer value in a variable \f5total\fP:
524
525.Cs
526    DTTREEMATCH(dt, key, total += (int));
527.Ce
528
529.PP
530.Ss "DICTIONARY INFORMATION"
531.PP
532.Ss "  Dt_t* dtvnext(Dt_t* dt)"
533This returns the dictionary that \f5dt\fP is viewing, if any.
534.Ss "  int dtvcount(Dt_t* dt)"
535This returns the number of dictionaries that view \f5dt\fP.
536.Ss "  Dt_t* dtvhere(Dt_t* dt)"
537This returns the dictionary \f5v\fP viewable from \f5dt\fP
538where an object was found from the most recent search or walk operation.
539.Ss "  int dtsize(Dt_t* dt)"
540This function returns the number of objects stored in \f5dt\fP.
541.PP
542.Ss "  int dtstat(Dt_t *dt, Dtstat_t* st, int all)"
543This function reports dictionary statistics.
544If \f5all\fP is non-zero, all fields of \f5st\fP are filled.
545Otherwise, only the \f5dt_type\fP and \f5dt_size\fP fields are filled.
546It returns \f50\fP on success and \f5-1\fP on error.
547.PP
548\f5Dtstat_t\fP contains the below fields:
549.Tp
550\f5int dt_type\fP:
551This is one of \f5DT_SET\fP, \f5DT_BAG\fP, \f5DT_OSET\fP, \f5DT_OBAG\fP,
552\f5DT_LIST\fP, \f5DT_STACK\fP, and \f5DT_QUEUE\fP.
553.Tp
554\f5int dt_size\fP:
555This contains the number of objects in the dictionary.
556.Tp
557\f5int dt_n\fP:
558For \f5Dtset\fP and \f5Dtbag\fP,
559this is the number of non-empty chains in the hash table.
560For \f5Dtoset\fP and \f5Dtobag\fP,
561this is the deepest level in the tree (counting from zero.)
562Each level in the tree contains all nodes of equal distance from the root node.
563\f5dt_n\fP and the below two fields are undefined for other methods.
564.Tp
565\f5int dt_max\fP:
566For \f5Dtbag\fP and \f5Dtset\fP, this is the size of a largest chain.
567For \f5Dtoset\fP and \f5Dtobag\fP, this is the size of a largest level.
568.Tp
569\f5int* dt_count\fP:
570For \f5Dtset\fP and \f5Dtbag\fP,
571this is the list of counts for chains of particular sizes.
572For example, \f5dt_count[1]\fP is the number of chains of size \f51\fP.
573For \f5Dtoset\fP and \f5Dtobag\fP, this is the list of sizes of the levels.
574For example, \f5dt_count[1]\fP is the size of level \f51\fP.
575.PP
576.Ss "HASH FUNCTIONS"
577.PP
578.Ss "  unsigned int dtcharhash(unsigned int h, char c)"
579.Ss "  unsigned int dtstrhash(unsigned int h, char* str, int n)"
580These functions compute hash values from bytes or strings.
581\f5dtcharhash()\fP computes a new hash value from byte \f5c\fP and seed value \f5h\fP.
582\f5dtstrhash()\fP computes a new hash value from string \f5str\fP and seed value \f5h\fP.
583If \f5n\fP is positive, \f5str\fP is a byte array of length \f5n\fP;
584otherwise, \f5str\fP is a null-terminated string.
585.PP
586.SH IMPLEMENTATION NOTES
587\f5Dtset\fP and \f5Dtbag\fP are based on hash tables with
588move-to-front collision chains.
589\f5Dtoset\fP and \f5Dtobag\fP are based on top-down splay trees.
590\f5Dtlist\fP, \f5Dtstack\fP and \f5Dtqueue\fP are based on doubly linked list.
591.PP
592.SH AUTHOR
593Kiem-Phong Vo, kpv@research.att.com
594