| 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 |
|
| 20 |
struct pq { |
| 21 |
Halfedge *hash; ///< backing storage |
| 22 |
int hashsize; ///< total allocated backing storage elements |
| 23 |
int count; ///< occupancy |
| 24 |
int min; ///< index of minimum element |
| 25 |
}; |
| 26 |
|
| 27 |
/// convert a `double` to an `int`, between bounds |
| 28 |
/// |
| 29 |
/// @param lower Lower bound to limit to |
| 30 |
/// @param v Value to convert |
| 31 |
/// @param upper Upper bound to limit to |
| 32 |
/// @return A converted value in the range [lower, upper] |
| 33 |
static 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 |
|
| 44 |
static 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 |
|
| 53 |
/// `a > b`? |
| 54 |
static 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 |
|
| 64 |
void 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 |
|
| 79 |
void PQdelete(pq_t *pq, Halfedge *he) { |
| 80 |
Halfedge *last; |
| 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 |
|
| 92 |
bool PQempty(const pq_t *pq) { |
| 93 |
return pq->count == 0; |
| 94 |
} |
| 95 |
|
| 96 |
Point PQ_min(pq_t *pq) { |
| 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 |
|
| 107 |
Halfedge *PQextractmin(pq_t *pq) { |
| 108 |
Halfedge *curr = pq->hash[pq->min].PQnext; |
| 109 |
pq->hash[pq->min].PQnext = curr->PQnext; |
| 110 |
--pq->count; |
| 111 |
return curr; |
| 112 |
} |
| 113 |
|
| 114 |
void PQcleanup(pq_t *pq) { |
| 115 |
if (pq != NULL) { |
| 116 |
free(pq->hash); |
| 117 |
} |
| 118 |
free(pq); |
| 119 |
} |
| 120 |
|
| 121 |
pq_t *PQinitialize(void) { |
| 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 |
} |