Debian Codegraph @

lib/common/pointset.c

raw
1 /// @file
2 /// @ingroup common_utils
3 /*************************************************************************
4 * Copyright (c) 2011 AT&T Intellectual Property
5 * All rights reserved. This program and the accompanying materials
6 * are made available under the terms of the Eclipse Public License v1.0
7 * which accompanies this distribution, and is available at
8 * https://www.eclipse.org/legal/epl-v10.html
9 *
10 * Contributors: Details at https://graphviz.org
11 *************************************************************************/
12
13 #include "config.h"
14
15 #include <common/render.h>
16 #include <common/pointset.h>
17 #include <stddef.h>
18 #include <util/alloc.h>
19
20 typedef struct {
21 Dtlink_t link;
22 pointf id;
23 } pair;
24
25 static pair *mkPair(pointf p) {
26 pair *pp = gv_alloc(sizeof(pair));
27 pp->id = p;
28 return pp;
29 }
30
31 static int cmppair(void *k1, void *k2) {
32 const pointf *key1 = k1;
33 const pointf *key2 = k2;
34 if (key1->x > key2->x)
35 return 1;
36 else if (key1->x < key2->x)
37 return -1;
38 else if (key1->y > key2->y)
39 return 1;
40 else if (key1->y < key2->y)
41 return -1;
42 else
43 return 0;
44 }
45
46 static int cmpmpair(void *k1, void *k2) {
47 const point *key1 = k1;
48 const point *key2 = k2;
49 if (key1->x > key2->x)
50 return 1;
51 else if (key1->x < key2->x)
52 return -1;
53 else if (key1->y > key2->y)
54 return 1;
55 else if (key1->y < key2->y)
56 return -1;
57 else
58 return 0;
59 }
60
61 static Dtdisc_t intPairDisc = {
62 offsetof(pair, id),
63 sizeof(pointf),
64 offsetof(pair, link),
65 0,
66 free,
67 cmppair,
68 };
69
70 PointSet *newPS(void)
71 {
72 return (dtopen(&intPairDisc, Dtoset));
73 }
74
75 void freePS(PointSet * ps)
76 {
77 dtclose(ps);
78 }
79
80 void insertPS(PointSet *ps, pointf pt) {
81 pair *pp;
82
83 pp = mkPair(pt);
84 if (dtinsert(ps, pp) != pp)
85 free(pp);
86 }
87
88 void addPS(PointSet *ps, double x, double y) {
89 const pointf pt = {.x = x, .y = y};
90 pair *pp = mkPair(pt);
91 if (dtinsert(ps, pp) != pp)
92 free(pp);
93 }
94
95 int inPS(PointSet *ps, pointf pt) {
96 pair p;
97 p.id = pt;
98 return dtsearch(ps, &p) ? 1 : 0;
99 }
100
101 int isInPS(PointSet *ps, double x, double y) {
102 return inPS(ps, (pointf){.x = x, .y = y});
103 }
104
105 int sizeOf(PointSet * ps)
106 {
107 return dtsize(ps);
108 }
109
110 pointf *pointsOf(PointSet *ps) {
111 const size_t n = (size_t)dtsize(ps);
112 pointf *pts = gv_calloc(n, sizeof(pointf));
113 pair *p;
114 pointf *pp = pts;
115
116 for (p = (pair *) dtflatten(ps); p;
117 p = (pair *)dtlink(ps, p)) {
118 *pp++ = p->id;
119 }
120
121 return pts;
122 }
123
124 typedef struct {
125 Dtlink_t link;
126 point id;
127 int v;
128 } mpair;
129
130 static void *mkMPair(void *p, Dtdisc_t *disc) {
131 (void)disc;
132 mpair *obj = p;
133 mpair *ap = gv_alloc(sizeof(mpair));
134 ap->id = obj->id;
135 ap->v = obj->v;
136 return ap;
137 }
138
139 static Dtdisc_t intMPairDisc = {
140 offsetof(mpair, id),
141 sizeof(point),
142 offsetof(mpair, link),
143 mkMPair,
144 free,
145 cmpmpair,
146 };
147
148 PointMap *newPM(void)
149 {
150 return dtopen(&intMPairDisc, Dtoset);
151 }
152
153 void clearPM(PointMap * ps)
154 {
155 dtclear(ps);
156 }
157
158 void freePM(PointMap * ps)
159 {
160 dtclose(ps);
161 }
162
163 int insertPM(PointMap * pm, int x, int y, int value)
164 {
165 mpair *p;
166 mpair dummy;
167
168 dummy.id.x = x;
169 dummy.id.y = y;
170 dummy.v = value;
171 p = dtinsert(pm, &dummy);
172 return p->v;
173 }