/misc/src/release/graphviz-2.18-1/src/graphviz-2.18/lib/common/pointset.c

Go to the documentation of this file.
00001 /* $Id: pointset.c,v 1.6 2008/01/03 23:24:28 ellson Exp $ $Revision: 1.6 $ */
00002 /* vim:set shiftwidth=4 ts=8: */
00003 
00004 /**********************************************************
00005 *      This software is part of the graphviz package      *
00006 *                http://www.graphviz.org/                 *
00007 *                                                         *
00008 *            Copyright (c) 1994-2004 AT&T Corp.           *
00009 *                and is licensed under the                *
00010 *            Common Public License, Version 1.0           *
00011 *                      by AT&T Corp.                      *
00012 *                                                         *
00013 *        Information and Software Systems Research        *
00014 *              AT&T Research, Florham Park NJ             *
00015 **********************************************************/
00016 
00017 
00018 #include "pointset.h"
00019 
00020 typedef struct {
00021     Dtlink_t link;
00022     point id;
00023 } pair;
00024 
00025 static pair *mkPair(point p)
00026 {
00027     pair *pp;
00028 
00029     pp = NEW(pair);
00030     pp->id = p;
00031     return pp;
00032 }
00033 
00034 static void freePair(Dt_t * d, pair* pp, Dtdisc_t * disc)
00035 {
00036     free (pp);
00037 }
00038 
00039 static int cmppair(Dt_t * d, point * key1, point * key2, Dtdisc_t * disc)
00040 {
00041     if (key1->x > key2->x)
00042         return 1;
00043     else if (key1->x < key2->x)
00044         return -1;
00045     else if (key1->y > key2->y)
00046         return 1;
00047     else if (key1->y < key2->y)
00048         return -1;
00049     else
00050         return 0;
00051 }
00052 
00053 static Dtdisc_t intPairDisc = {
00054     offsetof(pair, id),
00055     sizeof(point),
00056     offsetof(pair, link),
00057     0,
00058     (Dtfree_f) freePair,
00059     (Dtcompar_f) cmppair,
00060     0,
00061     0,
00062     0
00063 };
00064 
00065 PointSet *newPS(void)
00066 {
00067     return (dtopen(&intPairDisc, Dtoset));
00068 }
00069 
00070 void freePS(PointSet * ps)
00071 {
00072     dtclose(ps);
00073 }
00074 
00075 void insertPS(PointSet * ps, point pt)
00076 {
00077     dtinsert(ps, mkPair(pt));
00078 }
00079 
00080 void addPS(PointSet * ps, int x, int y)
00081 {
00082     point pt;
00083 
00084     pt.x = x;
00085     pt.y = y;
00086     dtinsert(ps, mkPair(pt));
00087 }
00088 
00089 int inPS(PointSet * ps, point pt)
00090 {
00091     pair p;
00092     p.id = pt;
00093     return ((dtsearch(ps, &p)) ? 1 : 0);
00094 }
00095 
00096 int isInPS(PointSet * ps, int x, int y)
00097 {
00098     pair p;
00099     p.id.x = x;
00100     p.id.y = y;
00101     return ((dtsearch(ps, &p)) ? 1 : 0);
00102 }
00103 
00104 int sizeOf(PointSet * ps)
00105 {
00106     return dtsize(ps);
00107 }
00108 
00109 point *pointsOf(PointSet * ps)
00110 {
00111     int n = dtsize(ps);
00112     point *pts = N_NEW(n, point);
00113     pair *p;
00114     point *pp = pts;
00115 
00116     for (p = (pair *) dtflatten(ps); p;
00117          p = (pair *) dtlink(ps, (Dtlink_t *) p)) {
00118         *pp++ = p->id;
00119     }
00120 
00121     return pts;
00122 }
00123 
00124 typedef struct {
00125     Dtlink_t link;
00126     point id;
00127     int v;
00128 } mpair;
00129 
00130 typedef struct {
00131     Dtdisc_t disc;
00132     mpair *flist;
00133 } MPairDisc;
00134 
00135 static mpair *mkMPair(Dt_t * d, mpair * obj, MPairDisc * disc)
00136 {
00137     mpair *ap;
00138 
00139     if (disc->flist) {
00140         ap = disc->flist;
00141         disc->flist = (mpair *) (ap->link.right);
00142     } else
00143         ap = GNEW(mpair);
00144     ap->id = obj->id;
00145     ap->v = obj->v;
00146     return ap;
00147 }
00148 
00149 static void freeMPair(Dt_t * d, mpair * ap, MPairDisc * disc)
00150 {
00151     ap->link.right = (Dtlink_t *) (disc->flist);
00152     disc->flist = ap;
00153 }
00154 
00155 static Dtdisc_t intMPairDisc = {
00156     offsetof(mpair, id),
00157     sizeof(point),
00158     offsetof(mpair, link),
00159     (Dtmake_f) mkMPair,
00160     (Dtfree_f) freeMPair,
00161     (Dtcompar_f) cmppair,
00162     0,
00163     0,
00164     0
00165 };
00166 
00167 PointMap *newPM(void)
00168 {
00169     MPairDisc *dp = GNEW(MPairDisc);
00170 
00171     dp->disc = intMPairDisc;
00172     dp->flist = 0;
00173 
00174     return (dtopen(&(dp->disc), Dtoset));
00175 }
00176 
00177 void clearPM(PointMap * ps)
00178 {
00179     dtclear(ps);
00180 }
00181 
00182 void freePM(PointMap * ps)
00183 {
00184     MPairDisc *dp = (MPairDisc *) (ps->disc);
00185     mpair *p;
00186     mpair *next;
00187 
00188     dtclose(ps);
00189     for (p = dp->flist; p; p = next) {
00190         next = (mpair *) (p->link.right);
00191         free(p);
00192     }
00193     free(dp);
00194 }
00195 
00196 int insertPM(PointMap * pm, int x, int y, int v)
00197 {
00198     mpair *p;
00199     mpair dummy;
00200 
00201     dummy.id.x = x;
00202     dummy.id.y = y;
00203     dummy.v = v;
00204     p = dtinsert(pm, &dummy);
00205     return p->v;
00206 }

Generated on Mon Mar 31 19:03:24 2008 for Graphviz by  doxygen 1.5.1