Graphviz 14.1.2~dev.20260119.0928
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 "config.h"
12
13#include <common/render.h>
14#include <stdbool.h>
15
16#include <neatogen/hedges.h>
17#include <neatogen/heap.h>
18#include <util/alloc.h>
19
20struct pq {
23 int count;
24 int min;
25};
26
33static int clamp(int lower, double v, int upper) {
34 assert(upper >= lower);
35 if (v < lower) {
36 return lower;
37 }
38 if (v > upper) {
39 return upper;
40 }
41 return (int)v;
42}
43
44static int PQbucket(pq_t *pq, Halfedge *he) {
45 const double deltay = ymax - ymin;
46 const int bucket =
47 clamp(0, (he->ystar - ymin) / deltay * pq->hashsize, pq->hashsize - 1);
48 if (bucket < pq->min)
49 pq->min = bucket;
50 return bucket;
51}
52
54static bool gt(double a_y, double a_x, double b_y, double b_x) {
55 if (a_y > b_y) {
56 return true;
57 }
58 if (a_y < b_y) {
59 return false;
60 }
61 return a_x > b_x;
62}
63
64void PQinsert(pq_t *pq, Halfedge *he, Site *v, double offset) {
65 Halfedge *last, *next;
66
67 he->vertex = 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 he->vertex = NULL;
89 }
90}
91
92bool PQempty(const pq_t *pq) {
93 return pq->count == 0;
94}
95
97 Point answer;
98
99 while (pq->hash[pq->min].PQnext == NULL) {
100 ++pq->min;
101 }
102 answer.x = pq->hash[pq->min].PQnext->vertex->coord.x;
103 answer.y = pq->hash[pq->min].PQnext->ystar;
104 return answer;
105}
106
108 Halfedge *curr = pq->hash[pq->min].PQnext;
109 pq->hash[pq->min].PQnext = curr->PQnext;
110 --pq->count;
111 return curr;
112}
113
115 if (pq != NULL) {
116 free(pq->hash);
117 }
118 free(pq);
119}
120
122 pq_t *pq = gv_alloc(sizeof(pq_t));
123 pq->hashsize = 4 * sqrt_nsites;
124 pq->hash = gv_calloc(pq->hashsize, sizeof(Halfedge));
125 return pq;
126}
static agxbuf last
last message
Definition agerror.c:31
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
double ymin
Definition geometry.c:17
int sqrt_nsites
Definition geometry.c:21
double ymax
Definition geometry.c:17
void free(void *)
node NULL
Definition grammar.y:181
Point PQ_min(pq_t *pq)
Definition heap.c:96
static bool gt(double a_y, double a_x, double b_y, double b_x)
a > b?
Definition heap.c:54
void PQcleanup(pq_t *pq)
Definition heap.c:114
static int PQbucket(pq_t *pq, Halfedge *he)
Definition heap.c:44
bool PQempty(const pq_t *pq)
Definition heap.c:92
void PQdelete(pq_t *pq, Halfedge *he)
Definition heap.c:79
static int clamp(int lower, double v, int upper)
Definition heap.c:33
pq_t * PQinitialize(void)
Definition heap.c:121
Halfedge * PQextractmin(pq_t *pq)
Definition heap.c:107
void PQinsert(pq_t *pq, Halfedge *he, Site *v, double offset)
Definition heap.c:64
double ystar
Definition hedges.h:28
struct Halfedge * PQnext
Definition hedges.h:29
Site * vertex
Definition hedges.h:27
double x
Definition geometry.h:24
double y
Definition geometry.h:24
Definition site.h:23
Point coord
Definition site.h:24
Definition heap.c:20
int hashsize
total allocated backing storage elements
Definition heap.c:22
int count
occupancy
Definition heap.c:23
Halfedge * hash
backing storage
Definition heap.c:21
int min
index of minimum element
Definition heap.c:24