Graphviz 12.0.1~dev.20240715.2254
Loading...
Searching...
No Matches
imap.c
Go to the documentation of this file.
1
5/*************************************************************************
6 * Copyright (c) 2011 AT&T Intellectual Property
7 * All rights reserved. This program and the accompanying materials
8 * are made available under the terms of the Eclipse Public License v1.0
9 * which accompanies this distribution, and is available at
10 * https://www.eclipse.org/legal/epl-v10.html
11 *
12 * Contributors: Details at https://graphviz.org
13 *************************************************************************/
14
15#include <cgraph/cghdr.h>
16#include <stdbool.h>
17#include <stddef.h>
18
25
26static int idcmpf(void *arg_p0, void *arg_p1) {
27 IMapEntry_t *p0 = arg_p0;
28 IMapEntry_t *p1 = arg_p1;
29 if (p0->id > p1->id)
30 {
31 return 1;
32 }
33 else if (p0->id < p1->id)
34 {
35 return -1;
36 }
37 else
38 {
39 return 0;
40 }
41}
42
43/* note, OK to compare pointers into shared string pool
44 * but can't probe with an arbitrary string pointer
45 */
46static int namecmpf(void *arg_p0, void *arg_p1) {
47 IMapEntry_t *p0 = arg_p0;
48 IMapEntry_t *p1 = arg_p1;
49 if (p0->str > p1->str)
50 {
51 return 1;
52 }
53 else if (p0->str < p1->str)
54 {
55 return -1;
56 }
57 else
58 {
59 return 0;
60 }
61}
62
64 0, /* object ptr is passed as key */
65 0, /* size (ignored) */
66 offsetof(IMapEntry_t, namedict_link),
67 NULL,
68 NULL,
70};
71
73 0, /* object ptr is passed as key */
74 0, /* size (ignored) */
75 offsetof(IMapEntry_t, iddict_link),
76 NULL,
77 NULL,
78 idcmpf,
79};
80
81int aginternalmaplookup(Agraph_t * g, int objtype, char *str,
82 IDTYPE *result)
83{
84 Dict_t *d;
85 IMapEntry_t *sym, template;
86 char *search_str;
87
88 if (objtype == AGINEDGE)
89 objtype = AGEDGE;
90 if ((d = g->clos->lookup_by_name[objtype])) {
91 if ((search_str = agstrbind(g, str))) {
92 template.str = search_str;
93 sym = dtsearch(d, &template);
94 if (sym) {
95 *result = sym->id;
96 return true;
97 }
98 }
99 }
100 return false;
101}
102
103/* caller GUARANTEES that this is a new entry */
104void aginternalmapinsert(Agraph_t * g, int objtype, char *str,
105 IDTYPE id)
106{
107 IMapEntry_t *ent;
108 Dict_t *d_name_to_id, *d_id_to_name;
109
110 ent = AGNEW(g, IMapEntry_t);
111 ent->id = id;
112 ent->str = agstrdup(g, str);
113
114 if (objtype == AGINEDGE)
115 objtype = AGEDGE;
116 if ((d_name_to_id = g->clos->lookup_by_name[objtype]) == NULL)
117 d_name_to_id = g->clos->lookup_by_name[objtype] =
119 if ((d_id_to_name = g->clos->lookup_by_id[objtype]) == NULL)
120 d_id_to_name = g->clos->lookup_by_id[objtype] =
122 dtinsert(d_name_to_id, ent);
123 dtinsert(d_id_to_name, ent);
124}
125
126static IMapEntry_t *find_isym(Agraph_t * g, int objtype, IDTYPE id)
127{
128 Dict_t *d;
129 IMapEntry_t *isym, itemplate;
130
131 if (objtype == AGINEDGE)
132 objtype = AGEDGE;
133 if ((d = g->clos->lookup_by_id[objtype])) {
134 itemplate.id = id;
135 isym = dtsearch(d, &itemplate);
136 } else
137 isym = NULL;
138 return isym;
139}
140
141char *aginternalmapprint(Agraph_t * g, int objtype, IDTYPE id)
142{
143 IMapEntry_t *isym;
144
145 if ((isym = find_isym(g, objtype, id)))
146 return isym->str;
147 return NULL;
148}
149
150
151int aginternalmapdelete(Agraph_t * g, int objtype, IDTYPE id)
152{
153 IMapEntry_t *isym;
154
155 if (objtype == AGINEDGE)
156 objtype = AGEDGE;
157 if ((isym = find_isym(g, objtype, id))) {
158 dtdelete(g->clos->lookup_by_name[objtype], isym);
159 dtdelete(g->clos->lookup_by_id[objtype], isym);
160 agstrfree(g, isym->str);
161 agfree(g, isym);
162 return true;
163 }
164 return false;
165}
166
168{
169 int i;
170 IMapEntry_t *sym, *nxt;
171 Dict_t **d_name;
172
173 Ag_G_global = g;
174 d_name = g->clos->lookup_by_name;
175 for (i = 0; i < 3; i++) {
176 if (d_name[i]) {
177 for (sym = dtfirst(d_name[i]); sym; sym = nxt) {
178 nxt = dtnext(d_name[i], sym);
179 if (sym->str[0] == LOCALNAMEPREFIX)
180 aginternalmapdelete(g, i, sym->id);
181 }
182 }
183 }
184}
185
186static void closeit(Dict_t ** d)
187{
188 int i;
189
190 for (i = 0; i < 3; i++) {
191 if (d[i]) {
192 dtclose(d[i]);
193 d[i] = NULL;
194 }
195 }
196}
197
#define dtsearch(d, o)
Definition cdt.h:191
#define dtinsert(d, o)
Definition cdt.h:193
CDT_API Dtmethod_t * Dttree
Definition dttree.c:308
CDT_API int dtclose(Dt_t *)
Definition dtclose.c:8
#define dtdelete(d, o)
Definition cdt.h:194
#define dtnext(d, o)
Definition cdt.h:188
#define dtfirst(d)
Definition cdt.h:187
cgraph.h additions
Dict_t * agdtopen(Agraph_t *g, Dtdisc_t *disc, Dtmethod_t *method)
Definition utils.c:29
Agraph_t * Ag_G_global
Definition graph.c:24
#define LOCALNAMEPREFIX
Definition cghdr.h:45
#define AGNEW(g, t)
Definition cghdr.h:49
node NULL
Definition grammar.y:149
void agfree(Agraph_t *g, void *ptr)
Definition mem.c:49
void aginternalmapclearlocalnames(Agraph_t *g)
Definition imap.c:167
uint64_t IDTYPE
unique per main graph ID
Definition cgraph.h:73
@ AGEDGE
Definition cgraph.h:207
@ AGINEDGE
Definition cgraph.h:207
int agstrfree(Agraph_t *, const char *)
Definition refstr.c:138
char * agstrdup(Agraph_t *, const char *)
returns a pointer to a reference-counted copy of the argument string, creating one if necessary
Definition refstr.c:130
char * agstrbind(Agraph_t *g, const char *)
Definition refstr.c:95
static uint64_t id
Definition gv2gml.c:42
agxbuf * str
Definition htmlparse.c:97
void aginternalmapclose(Agraph_t *g)
Definition imap.c:198
int aginternalmapdelete(Agraph_t *g, int objtype, IDTYPE id)
Definition imap.c:151
void aginternalmapinsert(Agraph_t *g, int objtype, char *str, IDTYPE id)
Definition imap.c:104
static int namecmpf(void *arg_p0, void *arg_p1)
Definition imap.c:46
static Dtdisc_t LookupById
Definition imap.c:72
int aginternalmaplookup(Agraph_t *g, int objtype, char *str, IDTYPE *result)
Definition imap.c:81
char * aginternalmapprint(Agraph_t *g, int objtype, IDTYPE id)
Definition imap.c:141
struct IMapEntry_s IMapEntry_t
static IMapEntry_t * find_isym(Agraph_t *g, int objtype, IDTYPE id)
Definition imap.c:126
static Dtdisc_t LookupByName
Definition imap.c:63
static void closeit(Dict_t **d)
Definition imap.c:186
static int idcmpf(void *arg_p0, void *arg_p1)
Definition imap.c:26
Dict_t * lookup_by_id[3]
Definition cgraph.h:418
Dict_t * lookup_by_name[3]
Definition cgraph.h:417
graph or subgraph
Definition cgraph.h:425
Agclos_t * clos
shared resources
Definition cgraph.h:435
Dtlink_t iddict_link
Definition imap.c:21
IDTYPE id
Definition imap.c:22
Dtlink_t namedict_link
Definition imap.c:20
char * str
Definition imap.c:23
Definition cdt.h:104