Graphviz  2.29.20120524.0446
lib/neatogen/heap.c
Go to the documentation of this file.
00001 /* $Id$ $Revision$ */
00002 /* vim:set shiftwidth=4 ts=8: */
00003 
00004 /*************************************************************************
00005  * Copyright (c) 2011 AT&T Intellectual Property 
00006  * All rights reserved. This program and the accompanying materials
00007  * are made available under the terms of the Eclipse Public License v1.0
00008  * which accompanies this distribution, and is available at
00009  * http://www.eclipse.org/legal/epl-v10.html
00010  *
00011  * Contributors: See CVS logs. Details at http://www.graphviz.org/
00012  *************************************************************************/
00013 
00014 
00015 #include "render.h"
00016 #include <stdio.h>
00017 
00018 #include "mem.h"
00019 #include "hedges.h"
00020 #include "heap.h"
00021 
00022 
00023 static Halfedge *PQhash;
00024 static int PQhashsize;
00025 static int PQcount;
00026 static int PQmin;
00027 
00028 static int PQbucket(Halfedge * he)
00029 {
00030     int bucket;
00031     double b;
00032 
00033     b = (he->ystar - ymin) / deltay * PQhashsize;
00034     if (b < 0)
00035         bucket = 0;
00036     else if (b >= PQhashsize)
00037         bucket = PQhashsize - 1;
00038     else
00039         bucket = b;
00040     if (bucket < PQmin)
00041         PQmin = bucket;
00042     return (bucket);
00043 }
00044 
00045 void PQinsert(Halfedge * he, Site * v, double offset)
00046 {
00047     Halfedge *last, *next;
00048 
00049     he->vertex = v;
00050     ref(v);
00051     he->ystar = v->coord.y + offset;
00052     last = &PQhash[PQbucket(he)];
00053     while ((next = last->PQnext) != (struct Halfedge *) NULL &&
00054            (he->ystar > next->ystar ||
00055             (he->ystar == next->ystar
00056              && v->coord.x > next->vertex->coord.x))) {
00057         last = next;
00058     }
00059     he->PQnext = last->PQnext;
00060     last->PQnext = he;
00061     PQcount += 1;
00062 }
00063 
00064 void PQdelete(Halfedge * he)
00065 {
00066     Halfedge *last;
00067 
00068     if (he->vertex != (Site *) NULL) {
00069         last = &PQhash[PQbucket(he)];
00070         while (last->PQnext != he)
00071             last = last->PQnext;
00072         last->PQnext = he->PQnext;
00073         PQcount -= 1;
00074         deref(he->vertex);
00075         he->vertex = (Site *) NULL;
00076     }
00077 }
00078 
00079 
00080 int PQempty(void)
00081 {
00082     return (PQcount == 0);
00083 }
00084 
00085 
00086 Point PQ_min(void)
00087 {
00088     Point answer;
00089 
00090     while (PQhash[PQmin].PQnext == (struct Halfedge *) NULL) {
00091         PQmin += 1;
00092     }
00093     answer.x = PQhash[PQmin].PQnext->vertex->coord.x;
00094     answer.y = PQhash[PQmin].PQnext->ystar;
00095     return (answer);
00096 }
00097 
00098 Halfedge *PQextractmin(void)
00099 {
00100     Halfedge *curr;
00101 
00102     curr = PQhash[PQmin].PQnext;
00103     PQhash[PQmin].PQnext = curr->PQnext;
00104     PQcount -= 1;
00105     return (curr);
00106 }
00107 
00108 void PQcleanup(void)
00109 {
00110     free(PQhash);
00111     PQhash = NULL;
00112 }
00113 
00114 void PQinitialize(void)
00115 {
00116     int i;
00117 
00118     PQcount = 0;
00119     PQmin = 0;
00120     PQhashsize = 4 * sqrt_nsites;
00121     if (PQhash == NULL)
00122         PQhash = N_GNEW(PQhashsize, Halfedge);
00123     for (i = 0; i < PQhashsize; i += 1)
00124         PQhash[i].PQnext = (Halfedge *) NULL;
00125 }
00126 
00127 static void PQdumphe(Halfedge * p)
00128 {
00129     printf("  [%p] %p %p %d %d %d %d %f\n",
00130            p, p->ELleft, p->ELright, p->ELedge->edgenbr,
00131            p->ELrefcnt, p->ELpm, (p->vertex ? p->vertex->sitenbr : -1),
00132            p->ystar);
00133 }
00134 
00135 void PQdump(void)
00136 {
00137     int i;
00138     Halfedge *p;
00139 
00140     for (i = 0; i < PQhashsize; i += 1) {
00141         printf("[%d]\n", i);
00142         p = PQhash[i].PQnext;
00143         while (p != NULL) {
00144             PQdumphe(p);
00145             p = p->PQnext;
00146         }
00147     }
00148 
00149 }