Graphviz 13.0.0~dev.20241220.2304
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
26static int PQbucket(pq_t *pq, Halfedge *he) {
27 int bucket;
28 double b;
29
30 const double deltay = ymax - ymin;
31 b = (he->ystar - ymin) / deltay * pq->hashsize;
32 if (b < 0)
33 bucket = 0;
34 else if (b >= pq->hashsize)
35 bucket = pq->hashsize - 1;
36 else
37 bucket = b;
38 if (bucket < pq->min)
39 pq->min = bucket;
40 return bucket;
41}
42
44static bool gt(double a_y, double a_x, double b_y, double b_x) {
45 if (a_y > b_y) {
46 return true;
47 }
48 if (a_y < b_y) {
49 return false;
50 }
51 return a_x > b_x;
52}
53
54void PQinsert(pq_t *pq, Halfedge *he, Site *v, double offset) {
55 Halfedge *last, *next;
56
57 he->vertex = v;
58 ref(v);
59 he->ystar = v->coord.y + offset;
60 last = &pq->hash[PQbucket(pq, he)];
61 while ((next = last->PQnext) != NULL &&
62 gt(he->ystar, v->coord.x, next->ystar, next->vertex->coord.x)) {
63 last = next;
64 }
65 he->PQnext = last->PQnext;
66 last->PQnext = he;
67 ++pq->count;
68}
69
70void PQdelete(pq_t *pq, Halfedge *he) {
72
73 if (he->vertex != NULL) {
74 last = &pq->hash[PQbucket(pq, he)];
75 while (last->PQnext != he)
76 last = last->PQnext;
77 last->PQnext = he->PQnext;
78 --pq->count;
79 deref(he->vertex);
80 he->vertex = NULL;
81 }
82}
83
84bool PQempty(const pq_t *pq) {
85 return pq->count == 0;
86}
87
89 Point answer;
90
91 while (pq->hash[pq->min].PQnext == NULL) {
92 ++pq->min;
93 }
94 answer.x = pq->hash[pq->min].PQnext->vertex->coord.x;
95 answer.y = pq->hash[pq->min].PQnext->ystar;
96 return answer;
97}
98
100 Halfedge *curr = pq->hash[pq->min].PQnext;
101 pq->hash[pq->min].PQnext = curr->PQnext;
102 --pq->count;
103 return curr;
104}
105
107 if (pq != NULL) {
108 free(pq->hash);
109 }
110 free(pq);
111}
112
114 pq_t *pq = gv_alloc(sizeof(pq_t));
115 pq->hashsize = 4 * sqrt_nsites;
116 pq->hash = gv_calloc(pq->hashsize, sizeof(Halfedge));
117 return pq;
118}
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:258
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:163
swig_ptr_object_handlers offset
Definition gv_php.cpp:5907
Point PQ_min(pq_t *pq)
Definition heap.c:88
static bool gt(double a_y, double a_x, double b_y, double b_x)
a > b?
Definition heap.c:44
void PQcleanup(pq_t *pq)
Definition heap.c:106
static int PQbucket(pq_t *pq, Halfedge *he)
Definition heap.c:26
bool PQempty(const pq_t *pq)
Definition heap.c:84
void PQdelete(pq_t *pq, Halfedge *he)
Definition heap.c:70
pq_t * PQinitialize(void)
Definition heap.c:113
Halfedge * PQextractmin(pq_t *pq)
Definition heap.c:99
void PQinsert(pq_t *pq, Halfedge *he, Site *v, double offset)
Definition heap.c:54
void ref(Site *v)
Definition site.c:59
double ystar
Definition hedges.h:26
struct Halfedge * PQnext
Definition hedges.h:27
Site * vertex
Definition hedges.h:25
double x
Definition geometry.h:23
double y
Definition geometry.h:23
Definition site.h:22
Point coord
Definition site.h:23
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