Graphviz 13.1.3~dev.20250829.0113
Loading...
Searching...
No Matches
heap.c
Go to the documentation of this file.
1/*************************************************************************
2 * Copyright (c) 2011 AT&T Intellectual Property
3 * All rights reserved. This program and the accompanying materials
4 * are made available under the terms of the Eclipse Public License v1.0
5 * which accompanies this distribution, and is available at
6 * https://www.eclipse.org/legal/epl-v10.html
7 *
8 * Contributors: Details at https://graphviz.org
9 *************************************************************************/
10
11#include <common/render.h>
12#include <stdbool.h>
13
14#include <neatogen/mem.h>
15#include <neatogen/hedges.h>
16#include <neatogen/heap.h>
17#include <util/alloc.h>
18
19struct pq {
22 int count;
23 int min;
24};
25
32static int clamp(int lower, double v, int upper) {
33 assert(upper >= lower);
34 if (v < lower) {
35 return lower;
36 }
37 if (v > upper) {
38 return upper;
39 }
40 return (int)v;
41}
42
43static int PQbucket(pq_t *pq, Halfedge *he) {
44 const double deltay = ymax - ymin;
45 const int bucket =
46 clamp(0, (he->ystar - ymin) / deltay * pq->hashsize, pq->hashsize - 1);
47 if (bucket < pq->min)
48 pq->min = bucket;
49 return bucket;
50}
51
53static bool gt(double a_y, double a_x, double b_y, double b_x) {
54 if (a_y > b_y) {
55 return true;
56 }
57 if (a_y < b_y) {
58 return false;
59 }
60 return a_x > b_x;
61}
62
63void PQinsert(pq_t *pq, Halfedge *he, Site *v, double offset) {
64 Halfedge *last, *next;
65
66 he->vertex = v;
67 ref(v);
68 he->ystar = v->coord.y + offset;
69 last = &pq->hash[PQbucket(pq, he)];
70 while ((next = last->PQnext) != NULL &&
71 gt(he->ystar, v->coord.x, next->ystar, next->vertex->coord.x)) {
72 last = next;
73 }
74 he->PQnext = last->PQnext;
75 last->PQnext = he;
76 ++pq->count;
77}
78
79void PQdelete(pq_t *pq, Halfedge *he) {
81
82 if (he->vertex != NULL) {
83 last = &pq->hash[PQbucket(pq, he)];
84 while (last->PQnext != he)
85 last = last->PQnext;
86 last->PQnext = he->PQnext;
87 --pq->count;
88 deref(he->vertex);
89 he->vertex = NULL;
90 }
91}
92
93bool PQempty(const pq_t *pq) {
94 return pq->count == 0;
95}
96
98 Point answer;
99
100 while (pq->hash[pq->min].PQnext == NULL) {
101 ++pq->min;
102 }
103 answer.x = pq->hash[pq->min].PQnext->vertex->coord.x;
104 answer.y = pq->hash[pq->min].PQnext->ystar;
105 return answer;
106}
107
109 Halfedge *curr = pq->hash[pq->min].PQnext;
110 pq->hash[pq->min].PQnext = curr->PQnext;
111 --pq->count;
112 return curr;
113}
114
116 if (pq != NULL) {
117 free(pq->hash);
118 }
119 free(pq);
120}
121
123 pq_t *pq = gv_alloc(sizeof(pq_t));
124 pq->hashsize = 4 * sqrt_nsites;
125 pq->hash = gv_calloc(pq->hashsize, sizeof(Halfedge));
126 return pq;
127}
static agxbuf last
last message
Definition agerror.c:29
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
static Agobj_t * deref(Expr_t *pgm, Exnode_t *x, Exref_t *ref, Agobj_t *objp, Gpr_t *state)
Definition compile.c:204
double ymin
Definition geometry.c:15
int sqrt_nsites
Definition geometry.c:19
double ymax
Definition geometry.c:15
void free(void *)
node NULL
Definition grammar.y:181
Point PQ_min(pq_t *pq)
Definition heap.c:97
static bool gt(double a_y, double a_x, double b_y, double b_x)
a > b?
Definition heap.c:53
void PQcleanup(pq_t *pq)
Definition heap.c:115
static int PQbucket(pq_t *pq, Halfedge *he)
Definition heap.c:43
bool PQempty(const pq_t *pq)
Definition heap.c:93
void PQdelete(pq_t *pq, Halfedge *he)
Definition heap.c:79
static int clamp(int lower, double v, int upper)
Definition heap.c:32
pq_t * PQinitialize(void)
Definition heap.c:122
Halfedge * PQextractmin(pq_t *pq)
Definition heap.c:108
void PQinsert(pq_t *pq, Halfedge *he, Site *v, double offset)
Definition heap.c:63
void ref(Site *v)
Definition site.c:59
double ystar
Definition hedges.h:33
struct Halfedge * PQnext
Definition hedges.h:34
Site * vertex
Definition hedges.h:32
double x
Definition geometry.h:29
double y
Definition geometry.h:29
Definition site.h:28
Point coord
Definition site.h:29
Definition heap.c:19
int hashsize
total allocated backing storage elements
Definition heap.c:21
int count
occupancy
Definition heap.c:22
Halfedge * hash
backing storage
Definition heap.c:20
int min
index of minimum element
Definition heap.c:23