Graphviz 12.0.1~dev.20240715.2254
Loading...
Searching...
No Matches
cdt.h
Go to the documentation of this file.
1
13#pragma once
14
15#ifdef __cplusplus
16extern "C" {
17#endif
18
19/* Public interface for the dictionary library
20**
21** Written by Kiem-Phong Vo
22*/
23
24#define CDT_VERSION 20050420L
25
26#include <stddef.h> /* size_t */
27#include <string.h>
28
29#ifdef GVDLL
30#ifdef EXPORT_CDT
31#define CDT_API __declspec(dllexport)
32#else
33#define CDT_API __declspec(dllimport)
34#endif
35#endif
36
37#ifndef CDT_API
38#define CDT_API /* nothing */
39#endif
40
41typedef struct _dtlink_s Dtlink_t;
42typedef struct _dthold_s Dthold_t;
43typedef struct _dtdisc_s Dtdisc_t;
44typedef struct _dtmethod_s Dtmethod_t;
45typedef struct _dtdata_s Dtdata_t;
46typedef struct _dt_s Dt_t;
47typedef struct _dt_s Dict_t; /* for libdict compatibility */
48typedef struct _dtstat_s Dtstat_t;
49typedef void* (*Dtsearch_f)(Dt_t*,void*,int);
50typedef void* (*Dtmake_f)(void*,Dtdisc_t*);
51typedef void (*Dtfree_f)(void *);
52typedef int (*Dtcompar_f)(void *,void *);
53
55{ Dtlink_t* right; /* right child */
56 union
57 { unsigned int _hash; /* hash value */
58 Dtlink_t* _left; /* left child */
59 } hl;
60};
61
62/* private structure to hold an object */
64{ Dtlink_t hdr; /* header */
65 void* obj; /* user object */
66};
67
68/* method to manipulate dictionary structure */
70{ Dtsearch_f searchf; /* search function */
71 int type; /* type of operation */
72};
73
74/* stuff that may be in shared memory */
76{ int type; /* type of dictionary */
77 Dtlink_t* here; /* finger to last search element */
78 union
79 { Dtlink_t** _htab; /* hash table */
80 Dtlink_t* _head; /* linked list */
81 } hh;
82 int ntab; /* number of hash slots */
83 int size; /* number of objects */
84 int loop; /* number of nested loops */
85};
86
87/* structure to hold methods that manipulate an object */
89{ int key; /* where the key begins in an object */
90 int size; /* key size and type */
91 int link; /* offset to Dtlink_t field */
92 Dtmake_f makef; /* object constructor */
93 Dtfree_f freef; /* object destructor */
94 Dtcompar_f comparf;/* to compare two objects */
95};
96
97#define DTDISC(dc, ky, sz, lk, mkf, frf, cmpf) \
98 ( (dc)->key = (ky), (dc)->size = (sz), (dc)->link = (lk), \
99 (dc)->makef = (mkf), (dc)->freef = (frf), \
100 (dc)->comparf = (cmpf) )
101
102/* the dictionary structure itself */
103struct _dt_s
104{ Dtsearch_f searchf;/* search function */
105 Dtdisc_t* disc; /* method to manipulate objs */
106 Dtdata_t* data; /* sharable data */
107 Dtmethod_t* meth; /* dictionary method */
108 int nview; /* number of parent view dictionaries */
109 Dt_t* view; /* next on viewpath */
110 Dt_t* walk; /* dictionary being walked */
111 void* user; /* for user's usage */
112};
113
114/* structure to get status of a dictionary */
116{ int dt_meth; /* method type */
117 int dt_size; /* number of elements */
118 size_t dt_n; // number of chains or levels
119 size_t dt_max; // max size of a chain or a level
120 size_t* dt_count; // counts of chains or levels by size
121};
122
123/* supported storage methods */
124#define DT_SET 0000001 /* set with unique elements */
125#define DT_OSET 0000004 /* ordered set (self-adjusting tree) */
126#define DT_OBAG 0000010 /* ordered multiset */
127#define DT_QUEUE 0000100 /* queue: insert at top, delete at tail */
128#define DT_METHODS 0000377 /* all currently supported methods */
129
130/* types of search */
131#define DT_INSERT 0000001 /* insert object if not found */
132#define DT_DELETE 0000002 /* delete object if found */
133#define DT_SEARCH 0000004 /* look for an object */
134#define DT_NEXT 0000010 /* look for next element */
135#define DT_PREV 0000020 /* find previous element */
136#define DT_RENEW 0000040 /* renewing an object */
137#define DT_CLEAR 0000100 /* clearing all objects */
138#define DT_FIRST 0000200 /* get first object */
139#define DT_LAST 0000400 /* get last object */
140#define DT_MATCH 0001000 /* find object matching key */
141#define DT_VSEARCH 0002000 /* search using internal representation */
142#define DT_DETACH 0010000 /* detach an object from the dictionary */
143
144CDT_API extern Dtmethod_t* Dtset;
145CDT_API extern Dtmethod_t* Dtoset;
146CDT_API extern Dtmethod_t* Dtobag;
147CDT_API extern Dtmethod_t* Dtqueue;
148
152
154CDT_API int dtclose(Dt_t*);
158
162
163CDT_API int dtwalk(Dt_t*, int(*)(void*,void*), void*);
164
165CDT_API void* dtrenew(Dt_t*, void*);
166
167CDT_API int dtsize(Dt_t*);
168CDT_API int dtstat(Dt_t*, Dtstat_t*, int);
169CDT_API unsigned int dtstrhash(void*, int);
170
171/* internal functions for translating among holder, object and key */
172#define _DT(dt) ((Dt_t*)(dt))
173#define _DTDSC(dc,ky,sz,lk,cmpf) \
174 (ky = dc->key, sz = dc->size, lk = dc->link, cmpf = dc->comparf)
175#define _DTLNK(o,lk) ((Dtlink_t*)((char*)(o) + lk) )
176#define _DTOBJ(e,lk) (lk < 0 ? ((Dthold_t*)(e))->obj : (void*)((char*)(e) - lk) )
177#define _DTKEY(o,ky,sz) (void*)(sz < 0 ? *((char**)((char*)(o)+ky)) : ((char*)(o)+ky))
178
179#define _DTCMP(k1, k2, cmpf, sz) \
180 (cmpf ? (cmpf)(k1, k2) : \
181 (sz <= 0 ? strcmp(k1,k2) : memcmp(k1,k2,(size_t)sz)) )
182
183#define dtlink(d,e) (((Dtlink_t*)(e))->right)
184#define dtobj(d,e) _DTOBJ((e), _DT(d)->disc->link)
185#define dtfinger(d) (_DT(d)->data->here ? dtobj((d),_DT(d)->data->here):(void*)(0))
186
187#define dtfirst(d) (*(_DT(d)->searchf))((d),(void*)(0),DT_FIRST)
188#define dtnext(d,o) (*(_DT(d)->searchf))((d),(void*)(o),DT_NEXT)
189#define dtlast(d) (*(_DT(d)->searchf))((d),(void*)(0),DT_LAST)
190#define dtprev(d,o) (*(_DT(d)->searchf))((d),(void*)(o),DT_PREV)
191#define dtsearch(d,o) (*(_DT(d)->searchf))((d),(void*)(o),DT_SEARCH)
192#define dtmatch(d,o) (*(_DT(d)->searchf))((d),(void*)(o),DT_MATCH)
193#define dtinsert(d,o) (*(_DT(d)->searchf))((d),(void*)(o),DT_INSERT)
194#define dtdelete(d,o) (*(_DT(d)->searchf))((d),(void*)(o),DT_DELETE)
195#define dtdetach(d,o) (*(_DT(d)->searchf))((d),(void*)(o),DT_DETACH)
196#define dtclear(d) (*(_DT(d)->searchf))((d),(void*)(0),DT_CLEAR)
197
198#define DT_PRIME 17109811 /* 2#00000001 00000101 00010011 00110011 */
199
208#ifdef __cplusplus
209}
210#endif
void *(* Dtmake_f)(void *, Dtdisc_t *)
Definition cdt.h:50
CDT_API int dtwalk(Dt_t *, int(*)(void *, void *), void *)
Definition dtwalk.c:9
CDT_API Dtmethod_t * dtmethod(Dt_t *, Dtmethod_t *)
Definition dtmethod.c:9
int(* Dtcompar_f)(void *, void *)
Definition cdt.h:52
void(* Dtfree_f)(void *)
Definition cdt.h:51
CDT_API Dtmethod_t * Dtset
set with unique elements
Definition dthash.c:277
CDT_API Dtlink_t * dtflatten(Dt_t *)
Definition dtflatten.c:10
CDT_API Dtmethod_t * Dtobag
ordered multiset
Definition dttree.c:305
CDT_API Dtmethod_t _Dtqueue
Definition dtlist.c:132
CDT_API Dtlink_t * dtextract(Dt_t *)
Definition dtextract.c:9
CDT_API int dtsize(Dt_t *)
Definition dtsize.c:12
CDT_API unsigned int dtstrhash(void *, int)
Definition dtstrhash.c:19
#define CDT_API
Definition cdt.h:38
CDT_API Dt_t * dtview(Dt_t *, Dt_t *)
Definition dtview.c:91
CDT_API Dtmethod_t _Dttree
Definition dttree.c:307
CDT_API Dtmethod_t * Dttree
Definition dttree.c:308
CDT_API int dtclose(Dt_t *)
Definition dtclose.c:8
CDT_API void * dtrenew(Dt_t *, void *)
Definition dtrenew.c:9
CDT_API Dtmethod_t * Dtoset
ordered set (self-adjusting tree)
Definition dttree.c:304
CDT_API Dtmethod_t * Dtqueue
queue: insert at top, delete at tail
Definition dtlist.c:134
void *(* Dtsearch_f)(Dt_t *, void *, int)
Definition cdt.h:49
CDT_API int dtstat(Dt_t *, Dtstat_t *, int)
Definition dtstat.c:38
CDT_API int dtrestore(Dt_t *, Dtlink_t *)
Definition dtrestore.c:11
CDT_API Dtdisc_t * dtdisc(Dt_t *dt, Dtdisc_t *)
Definition dtdisc.c:11
CDT_API Dt_t * dtopen(Dtdisc_t *, Dtmethod_t *)
Definition dtopen.c:9
Definition cdt.h:104
Dtsearch_f searchf
Definition cdt.h:104
Dt_t * view
Definition cdt.h:109
Dt_t * walk
Definition cdt.h:110
int nview
Definition cdt.h:108
Dtdata_t * data
Definition cdt.h:106
Dtdisc_t * disc
Definition cdt.h:105
Dtmethod_t * meth
Definition cdt.h:107
void * user
Definition cdt.h:111
int ntab
Definition cdt.h:82
Dtlink_t * _head
Definition cdt.h:80
Dtlink_t * here
Definition cdt.h:77
int loop
Definition cdt.h:84
int type
Definition cdt.h:76
int size
Definition cdt.h:83
union _dtdata_s::@58 hh
Dtlink_t ** _htab
Definition cdt.h:79
Dtfree_f freef
Definition cdt.h:93
Dtmake_f makef
Definition cdt.h:92
int size
Definition cdt.h:90
int key
Definition cdt.h:89
int link
Definition cdt.h:91
Dtcompar_f comparf
Definition cdt.h:94
Dtlink_t hdr
Definition cdt.h:64
void * obj
Definition cdt.h:65
int type
Definition cdt.h:71
Dtsearch_f searchf
Definition cdt.h:70
int dt_size
Definition cdt.h:117
size_t dt_n
Definition cdt.h:118
size_t dt_max
Definition cdt.h:119
int dt_meth
Definition cdt.h:116
size_t * dt_count
Definition cdt.h:120