Graphviz 13.0.0~dev.20241220.2304
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 dtdisc_s_ Dtdisc_t;
43typedef struct dt_s_ Dt_t;
44typedef struct dt_s_ Dict_t; /* for libdict compatibility */
45typedef void* (*Dtsearch_f)(Dt_t*,void*,int);
46typedef void* (*Dtmake_f)(void*,Dtdisc_t*);
47typedef void (*Dtfree_f)(void *);
48typedef int (*Dtcompar_f)(void *,void *);
49
51{ Dtlink_t* right; /* right child */
52 union
53 { unsigned int _hash; /* hash value */
54 Dtlink_t* _left; /* left child */
55 } hl;
56};
57
58/* private structure to hold an object */
59typedef struct
60{ Dtlink_t hdr; /* header */
61 void* obj; /* user object */
62} Dthold_t;
63
64/* method to manipulate dictionary structure */
65typedef struct
66{ Dtsearch_f searchf; /* search function */
67 int type; /* type of operation */
69
70/* stuff that may be in shared memory */
71typedef struct
72{ int type; /* type of dictionary */
73 Dtlink_t* here; /* finger to last search element */
74 union
75 { Dtlink_t** _htab; /* hash table */
76 Dtlink_t* _head; /* linked list */
77 } hh;
78 int ntab; /* number of hash slots */
79 int size; /* number of objects */
80 int loop; /* number of nested loops */
81} Dtdata_t;
82
83/* structure to hold methods that manipulate an object */
85{ int key; /* where the key begins in an object */
86 int size; /* key size and type */
87 int link; /* offset to Dtlink_t field */
88 Dtmake_f makef; /* object constructor */
89 Dtfree_f freef; /* object destructor */
90 Dtcompar_f comparf;/* to compare two objects */
91};
92
93#define DTDISC(dc, ky, sz, lk, mkf, frf, cmpf) \
94 ( (dc)->key = (ky), (dc)->size = (sz), (dc)->link = (lk), \
95 (dc)->makef = (mkf), (dc)->freef = (frf), \
96 (dc)->comparf = (cmpf) )
97
98/* the dictionary structure itself */
99struct dt_s_
100{ Dtsearch_f searchf;/* search function */
101 Dtdisc_t* disc; /* method to manipulate objs */
103 Dtmethod_t* meth; /* dictionary method */
104 int nview; /* number of parent view dictionaries */
105 Dt_t* view; /* next on viewpath */
106 Dt_t* walk; /* dictionary being walked */
107 void* user; /* for user's usage */
108};
109
110/* structure to get status of a dictionary */
111typedef struct
112{ int dt_meth; /* method type */
113 int dt_size; /* number of elements */
114 size_t dt_n; // number of chains or levels
115 size_t dt_max; // max size of a chain or a level
116 size_t* dt_count; // counts of chains or levels by size
117} Dtstat_t;
118
119/* supported storage methods */
120#define DT_SET 0000001 /* set with unique elements */
121#define DT_OSET 0000004 /* ordered set (self-adjusting tree) */
122#define DT_OBAG 0000010 /* ordered multiset */
123#define DT_METHODS 0000377 /* all currently supported methods */
124
125/* types of search */
126#define DT_INSERT 0000001 /* insert object if not found */
127#define DT_DELETE 0000002 /* delete object if found */
128#define DT_SEARCH 0000004 /* look for an object */
129#define DT_NEXT 0000010 /* look for next element */
130#define DT_PREV 0000020 /* find previous element */
131#define DT_RENEW 0000040 /* renewing an object */
132#define DT_CLEAR 0000100 /* clearing all objects */
133#define DT_FIRST 0000200 /* get first object */
134#define DT_LAST 0000400 /* get last object */
135#define DT_MATCH 0001000 /* find object matching key */
136#define DT_VSEARCH 0002000 /* search using internal representation */
137#define DT_DETACH 0010000 /* detach an object from the dictionary */
138
139CDT_API extern Dtmethod_t* Dtset;
140CDT_API extern Dtmethod_t* Dtoset;
141CDT_API extern Dtmethod_t* Dtobag;
142
144
146CDT_API int dtclose(Dt_t*);
150
154
155CDT_API int dtwalk(Dt_t*, int(*)(void*,void*), void*);
156
157CDT_API void* dtrenew(Dt_t*, void*);
158
159CDT_API int dtsize(Dt_t*);
160CDT_API int dtstat(Dt_t*, Dtstat_t*, int);
161CDT_API unsigned int dtstrhash(void*, int);
162
163/* internal functions for translating among holder, object and key */
164#define _DT(dt) ((Dt_t*)(dt))
165#define _DTDSC(dc,ky,sz,lk,cmpf) \
166 (ky = dc->key, sz = dc->size, lk = dc->link, cmpf = dc->comparf)
167#define _DTLNK(o,lk) ((Dtlink_t*)((char*)(o) + lk) )
168#define _DTOBJ(e,lk) (lk < 0 ? ((Dthold_t*)(e))->obj : (void*)((char*)(e) - lk) )
169#define _DTKEY(o,ky,sz) (void*)(sz < 0 ? *((char**)((char*)(o)+ky)) : ((char*)(o)+ky))
170
171#define _DTCMP(k1, k2, cmpf, sz) \
172 (cmpf ? (cmpf)(k1, k2) : \
173 (sz <= 0 ? strcmp(k1,k2) : memcmp(k1,k2,(size_t)sz)) )
174
175#define dtlink(d,e) (((Dtlink_t*)(e))->right)
176#define dtobj(d,e) _DTOBJ((e), _DT(d)->disc->link)
177#define dtfinger(d) (_DT(d)->data.here ? dtobj((d), _DT(d)->data.here) : NULL)
178
179#define dtfirst(d) (*(_DT(d)->searchf))((d),(void*)(0),DT_FIRST)
180#define dtnext(d,o) (*(_DT(d)->searchf))((d),(void*)(o),DT_NEXT)
181#define dtlast(d) (*(_DT(d)->searchf))((d),(void*)(0),DT_LAST)
182#define dtprev(d,o) (*(_DT(d)->searchf))((d),(void*)(o),DT_PREV)
183#define dtsearch(d,o) (*(_DT(d)->searchf))((d),(void*)(o),DT_SEARCH)
184#define dtmatch(d,o) (*(_DT(d)->searchf))((d),(void*)(o),DT_MATCH)
185#define dtinsert(d,o) (*(_DT(d)->searchf))((d),(void*)(o),DT_INSERT)
186#define dtdelete(d,o) (*(_DT(d)->searchf))((d),(void*)(o),DT_DELETE)
187#define dtdetach(d,o) (*(_DT(d)->searchf))((d),(void*)(o),DT_DETACH)
188#define dtclear(d) (*(_DT(d)->searchf))((d),(void*)(0),DT_CLEAR)
189
198#ifdef __cplusplus
199}
200#endif
void *(* Dtmake_f)(void *, Dtdisc_t *)
Definition cdt.h:46
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:48
void(* Dtfree_f)(void *)
Definition cdt.h:47
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 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:21
#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: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
void *(* Dtsearch_f)(Dt_t *, void *, int)
Definition cdt.h:45
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:72
int size
Definition cdt.h:79
Dtlink_t ** _htab
Definition cdt.h:75
Dtlink_t * here
Definition cdt.h:73
int type
Definition cdt.h:72
int ntab
Definition cdt.h:78
Dtlink_t * _head
Definition cdt.h:76
int loop
Definition cdt.h:80
Definition cdt.h:60
Dtlink_t hdr
Definition cdt.h:60
void * obj
Definition cdt.h:61
Dtsearch_f searchf
Definition cdt.h:66
int type
Definition cdt.h:67
size_t dt_n
Definition cdt.h:114
int dt_size
Definition cdt.h:113
size_t dt_max
Definition cdt.h:115
int dt_meth
Definition cdt.h:112
size_t * dt_count
Definition cdt.h:116
Definition cdt.h:100
Dtmethod_t * meth
Definition cdt.h:103
Dt_t * walk
Definition cdt.h:106
Dtsearch_f searchf
Definition cdt.h:100
Dtdata_t data
sharable data
Definition cdt.h:102
void * user
Definition cdt.h:107
int nview
Definition cdt.h:104
Dtdisc_t * disc
Definition cdt.h:101
Dt_t * view
Definition cdt.h:105
Dtfree_f freef
Definition cdt.h:89
int key
Definition cdt.h:85
int size
Definition cdt.h:86
int link
Definition cdt.h:87
Dtcompar_f comparf
Definition cdt.h:90
Dtmake_f makef
Definition cdt.h:88