Graphviz 13.0.0~dev.20241220.2304
Loading...
Searching...
No Matches
pointset.c
Go to the documentation of this file.
1
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 <common/render.h>
14#include <common/pointset.h>
15#include <stddef.h>
16#include <util/alloc.h>
17
18typedef struct {
21} pair;
22
23static pair *mkPair(pointf p) {
24 pair *pp = gv_alloc(sizeof(pair));
25 pp->id = p;
26 return pp;
27}
28
29static int cmppair(void *k1, void *k2) {
30 const pointf *key1 = k1;
31 const pointf *key2 = k2;
32 if (key1->x > key2->x)
33 return 1;
34 else if (key1->x < key2->x)
35 return -1;
36 else if (key1->y > key2->y)
37 return 1;
38 else if (key1->y < key2->y)
39 return -1;
40 else
41 return 0;
42}
43
45 offsetof(pair, id),
46 sizeof(pointf),
47 offsetof(pair, link),
48 0,
49 free,
50 cmppair,
51};
52
54{
55 return (dtopen(&intPairDisc, Dtoset));
56}
57
59{
60 dtclose(ps);
61}
62
64 pair *pp;
65
66 pp = mkPair(pt);
67 if (dtinsert(ps, pp) != pp)
68 free(pp);
69}
70
71void addPS(PointSet *ps, double x, double y) {
72 const pointf pt = {.x = x, .y = y};
73 pair *pp = mkPair(pt);
74 if (dtinsert(ps, pp) != pp)
75 free(pp);
76}
77
79 pair p;
80 p.id = pt;
81 return dtsearch(ps, &p) ? 1 : 0;
82}
83
84int isInPS(PointSet *ps, double x, double y) {
85 return inPS(ps, (pointf){.x = x, .y = y});
86}
87
89{
90 return dtsize(ps);
91}
92
94 const size_t n = (size_t)dtsize(ps);
95 pointf *pts = gv_calloc(n, sizeof(pointf));
96 pair *p;
97 pointf *pp = pts;
98
99 for (p = (pair *) dtflatten(ps); p;
100 p = (pair *)dtlink(ps, p)) {
101 *pp++ = p->id;
102 }
103
104 return pts;
105}
106
107typedef struct {
110 int v;
111} mpair;
112
113static void *mkMPair(void *p, Dtdisc_t *disc) {
114 (void)disc;
115 mpair *obj = p;
116 mpair *ap = gv_alloc(sizeof(mpair));
117 ap->id = obj->id;
118 ap->v = obj->v;
119 return ap;
120}
121
123 offsetof(mpair, id),
124 sizeof(point),
125 offsetof(mpair, link),
126 mkMPair,
127 free,
128 cmppair,
129};
130
132{
133 return dtopen(&intMPairDisc, Dtoset);
134}
135
137{
138 dtclear(ps);
139}
140
142{
143 dtclose(ps);
144}
145
146int insertPM(PointMap * pm, int x, int y, int value)
147{
148 mpair *p;
149 mpair dummy;
150
151 dummy.id.x = x;
152 dummy.id.y = y;
153 dummy.v = value;
154 p = dtinsert(pm, &dummy);
155 return p->v;
156}
Memory allocation wrappers that exit on failure.
static void * gv_calloc(size_t nmemb, size_t size)
Definition alloc.h:26
static void * gv_alloc(size_t size)
Definition alloc.h:47
CDT_API Dtlink_t * dtflatten(Dt_t *)
Definition dtflatten.c:10
#define dtclear(d)
Definition cdt.h:188
#define dtsearch(d, o)
Definition cdt.h:183
CDT_API int dtsize(Dt_t *)
Definition dtsize.c:12
#define dtlink(d, e)
Definition cdt.h:175
#define dtinsert(d, o)
Definition cdt.h:185
CDT_API int dtclose(Dt_t *)
Definition dtclose.c:8
CDT_API Dtmethod_t * Dtoset
ordered set (self-adjusting tree)
Definition dttree.c:304
CDT_API Dt_t * dtopen(Dtdisc_t *, Dtmethod_t *)
Definition dtopen.c:9
static Dtdisc_t disc
Definition exparse.y:209
struct pointf_s pointf
void free(void *)
static int * ps
Definition lu.c:51
std::unordered_map< std::pair< int, int >, int, PointHash > PointMap
void addPS(PointSet *ps, double x, double y)
Definition pointset.c:71
static pair * mkPair(pointf p)
Definition pointset.c:23
static Dtdisc_t intMPairDisc
Definition pointset.c:122
void insertPS(PointSet *ps, pointf pt)
Definition pointset.c:63
static void * mkMPair(void *p, Dtdisc_t *disc)
Definition pointset.c:113
PointSet * newPS(void)
Definition pointset.c:53
static Dtdisc_t intPairDisc
Definition pointset.c:44
void clearPM(PointMap *ps)
Definition pointset.c:136
int insertPM(PointMap *pm, int x, int y, int value)
Definition pointset.c:146
void freePS(PointSet *ps)
Definition pointset.c:58
int isInPS(PointSet *ps, double x, double y)
Definition pointset.c:84
pointf * pointsOf(PointSet *ps)
Definition pointset.c:93
PointMap * newPM(void)
Definition pointset.c:131
static int cmppair(void *k1, void *k2)
Definition pointset.c:29
int sizeOf(PointSet *ps)
Definition pointset.c:88
int inPS(PointSet *ps, pointf pt)
Definition pointset.c:78
void freePM(PointMap *ps)
Definition pointset.c:141
point containers PointSet and PointMap
Definition cdt.h:100
int v
Definition pointset.c:110
Dtlink_t link
Definition pointset.c:108
point id
Definition pointset.c:109
pointf id
Definition pointset.c:20
Dtlink_t link
Definition pointset.c:19
Definition geom.h:27
int y
Definition geom.h:27
int x
Definition geom.h:27
double x
Definition geom.h:29
double y
Definition geom.h:29