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