Graphviz  2.41.20171026.1811
nodeset.c
Go to the documentation of this file.
1 /* $Id$ $Revision$ */
2 /* vim:set shiftwidth=4 ts=8: */
3 
4 /*************************************************************************
5  * Copyright (c) 2011 AT&T Intellectual Property
6  * All rights reserved. This program and the accompanying materials
7  * are made available under the terms of the Eclipse Public License v1.0
8  * which accompanies this distribution, and is available at
9  * http://www.eclipse.org/legal/epl-v10.html
10  *
11  * Contributors: See CVS logs. Details at http://www.graphviz.org/
12  *************************************************************************/
13 
14 
15 #include <nodeset.h>
16 
17 
18 static nsitem_t *mkItem(Dt_t * d, nsitem_t * obj, Dtdisc_t * disc)
19 {
20  nsitem_t *ap = GNEW(nsitem_t);
21 
22  ap->np = obj->np;
23  return ap;
24 }
25 
26 static void freeItem(Dt_t * d, nsitem_t * obj, Dtdisc_t * disc)
27 {
28  free(obj);
29 }
30 
31 static int
32 cmpItem(Dt_t * d, Agnode_t ** key1, Agnode_t ** key2, Dtdisc_t * disc)
33 {
34  if (*key1 > *key2)
35  return 1;
36  else if (*key1 < *key2)
37  return -1;
38  else
39  return 0;
40 }
41 
42 static Dtdisc_t nodeDisc = {
43  offsetof(nsitem_t, np), /* key */
44  sizeof(Agnode_t *), /* size */
45  offsetof(nsitem_t, link), /* link */
46  (Dtmake_f) mkItem,
47  (Dtfree_f) freeItem,
48  (Dtcompar_f) cmpItem,
49  (Dthash_f) 0,
50  (Dtmemory_f) 0,
51  (Dtevent_f) 0
52 };
53 
54 /* mkNodeset:
55  * Creates an empty node set.
56  */
58 {
59  nodeset_t *s = dtopen(&nodeDisc, Dtoset);
60  return s;
61 }
62 
63 /* freeNodeset:
64  * Deletes a node set, deleting all items as well.
65  * It does not delete the nodes.
66  */
68 {
69  if (s != NULL)
70  dtclose(s);
71 }
72 
73 /* clearNodeset:
74  * Remove all items from a node set.
75  */
77 {
78  dtclear(s);
79 }
80 
81 /* insertNodeset:
82  * Add a node into the nodeset.
83  */
85 {
86  nsitem_t key;
87 
88  key.np = n;
89  dtinsert(ns, &key);
90 }
91 
93 {
94  nsitem_t key;
95 
96  key.np = n;
97  dtdelete(ns, &key);
98 }
99 
100 /* sizeNodeset:
101  * Report on the nodeset size.
102  */
104 {
105  return dtsize(ns);
106 }
107 
108 /* As the node set is a Dt_t, traversal is done using standard
109  * functions from libcdt.
110  */
111 
113 {
114  nsitem_t *ip;
115  for (ip = (nsitem_t *) dtfirst(ns); ip;
116  ip = (nsitem_t *) dtnext(ns, ip)) {
117  fprintf(stderr, "%s", agnameof(ip->np));
118  }
119  fputs("\n", stderr);
120 }
void printNodeset(nodeset_t *ns)
Definition: nodeset.c:112
int(* Dtcompar_f)(Dt_t *, void *, void *, Dtdisc_t *)
Definition: cdt.h:40
unsigned int(* Dthash_f)(Dt_t *, void *, Dtdisc_t *)
Definition: cdt.h:41
CDT_API int dtclose(Dt_t *)
void *(* Dtmake_f)(Dt_t *, void *, Dtdisc_t *)
Definition: cdt.h:38
int sizeNodeset(nodeset_t *ns)
Definition: nodeset.c:103
CDT_API Dtmethod_t * Dtoset
Definition: cdt.h:166
#define dtdelete(d, o)
Definition: cdt.h:264
Definition: cdt.h:80
#define dtfirst(d)
Definition: cdt.h:254
CDT_API Dt_t * dtopen(Dtdisc_t *, Dtmethod_t *)
Definition: dtopen.c:9
void freeNodeset(nodeset_t *s)
Definition: nodeset.c:67
void clearNodeset(nodeset_t *s)
Definition: nodeset.c:76
CGRAPH_API char * agnameof(void *)
Definition: id.c:143
#define dtnext(d, o)
Definition: cdt.h:255
void *(* Dtmemory_f)(Dt_t *, void *, size_t, Dtdisc_t *)
Definition: cdt.h:36
Agnode_t * np
Definition: nodeset.h:25
CDT_API int dtsize(Dt_t *)
Definition: dtsize.c:12
Definition: grammar.c:79
#define dtinsert(d, o)
Definition: cdt.h:262
#define NULL
Definition: logic.h:39
#define GNEW(t)
Definition: memory.h:37
#define dtclear(d)
Definition: cdt.h:267
nodeset_t * mkNodeset()
Definition: nodeset.c:57
int(* Dtevent_f)(Dt_t *, int, void *, Dtdisc_t *)
Definition: cdt.h:42
void insertNodeset(nodeset_t *ns, Agnode_t *n)
Definition: nodeset.c:84
void(* Dtfree_f)(Dt_t *, void *, Dtdisc_t *)
Definition: cdt.h:39
Definition: cdt.h:99
void removeNodeset(nodeset_t *ns, Agnode_t *n)
Definition: nodeset.c:92