Indented with src/scripts/indent-emp.

This commit is contained in:
Markus Armbruster 2003-09-02 20:48:48 +00:00
parent 5f263a7753
commit 9b7adfbecc
437 changed files with 52211 additions and 51052 deletions

View file

@ -27,46 +27,46 @@
* Added path list caching structures
*/
#include <stdio.h> /* for FILE */
#include "misc.h" /* for s_char */
#include <stdio.h> /* for FILE */
#include "misc.h" /* for s_char */
/*
* Coordinate.
*/
struct as_coord {
int x, y;
int x, y;
};
/*
* Path, made up of a linked list of coordinates.
*/
struct as_path {
struct as_coord c;
struct as_path *next;
struct as_coord c;
struct as_path *next;
};
/*
* Basic node, used internally by A* algorithm.
*/
struct as_node {
struct as_coord c; /* our coordinate */
double knowncost; /* cost so far */
double lbcost; /* lower bound on cost to dest */
double inclbcost; /* incremental lower bound cost */
double seccost; /* used to break ties */
int step;
int flags;
struct as_node *back;
struct as_coord c; /* our coordinate */
double knowncost; /* cost so far */
double lbcost; /* lower bound on cost to dest */
double inclbcost; /* incremental lower bound cost */
double seccost; /* used to break ties */
int step;
int flags;
struct as_node *back;
};
#define AS_TRIED 1 /* we've tried this node before */
#define AS_TRIED 1 /* we've tried this node before */
/*
* Linked list of nodes, used internally by A* algorithm.
*/
struct as_queue {
struct as_node *np;
struct as_queue *next;
struct as_queue *prev;
struct as_node *np;
struct as_queue *next;
struct as_queue *prev;
};
/*
@ -74,9 +74,9 @@ struct as_queue {
* coordinate before.
*/
struct as_hash {
struct as_coord c;
struct as_queue *qp;
struct as_hash *next;
struct as_coord c;
struct as_queue *qp;
struct as_hash *next;
};
/*
@ -84,26 +84,26 @@ struct as_hash {
* used by A* internals.
*/
struct as_data {
int maxneighbors; /* max # of neighbors a cell can have */
int hashsize; /* size of internal hash table */
int (*hash)(struct as_coord); /* hash function (coord -> int) */
int (*neighbor)(struct as_coord, struct as_coord *, s_char *); /* function to find neighbors */
double (*lbcost)(struct as_coord, struct as_coord, s_char *); /* function to give lower bound cost */
double (*realcost)(struct as_coord, struct as_coord, s_char *); /* function to give real cost */
double (*seccost)(struct as_coord, struct as_coord, s_char *); /* function to secondary cost */
char *userdata; /* user's data, passed to callbacks */
struct as_coord from; /* from coordinate */
struct as_coord to; /* to coordinate */
struct as_path *path; /* solution */
int maxneighbors; /* max # of neighbors a cell can have */
int hashsize; /* size of internal hash table */
/* below are "private" to as_ routines */
struct as_queue *head;
struct as_queue *tried;
struct as_hash **hashtab;
struct as_queue *subsumed;
struct as_coord *neighbor_coords;
struct as_node **neighbor_nodes;
int (*hash) (struct as_coord); /* hash function (coord -> int) */
int (*neighbor) (struct as_coord, struct as_coord *, s_char *); /* function to find neighbors */
double (*lbcost) (struct as_coord, struct as_coord, s_char *); /* function to give lower bound cost */
double (*realcost) (struct as_coord, struct as_coord, s_char *); /* function to give real cost */
double (*seccost) (struct as_coord, struct as_coord, s_char *); /* function to secondary cost */
char *userdata; /* user's data, passed to callbacks */
struct as_coord from; /* from coordinate */
struct as_coord to; /* to coordinate */
struct as_path *path; /* solution */
/* below are "private" to as_ routines */
struct as_queue *head;
struct as_queue *tried;
struct as_hash **hashtab;
struct as_queue *subsumed;
struct as_coord *neighbor_coords;
struct as_node **neighbor_nodes;
};
/*
@ -112,14 +112,14 @@ struct as_data {
struct as_topath {
coord x;
struct as_path *path; /* Path from holder of this list to here */
struct as_path *path; /* Path from holder of this list to here */
struct as_topath *next;
};
struct as_frompath {
coord x;
struct as_topath **tolist; /* List of nodes we have a path to */
struct as_frompath *next;
struct as_topath **tolist; /* List of nodes we have a path to */
struct as_frompath *next;
};
/*
@ -140,22 +140,27 @@ struct as_frompath {
/* Functions that the user can call. */
extern struct as_data *
as_init(int maxneighbors, int hashsize,
int (*hashfunc) (struct as_coord),
int (*neighborfunc) (struct as_coord, struct as_coord *, s_char *),
double (*lbcostfunc) (struct as_coord, struct as_coord, s_char *),
double (*realcostfunc) (struct as_coord, struct as_coord, s_char *),
double (*seccostfunc) (struct as_coord, struct as_coord, s_char *),
s_char *userdata);
extern int as_search(struct as_data *adp);
extern void as_delete(struct as_data *adp);
extern void as_reset(struct as_data *adp);
extern void as_stats(struct as_data *adp, FILE *fp);
extern struct as_data *as_init(int maxneighbors, int hashsize,
int (*hashfunc) (struct as_coord),
int (*neighborfunc) (struct as_coord,
struct as_coord *,
s_char *),
double (*lbcostfunc) (struct as_coord,
struct as_coord,
s_char *),
double (*realcostfunc) (struct as_coord,
struct as_coord,
s_char *),
double (*seccostfunc) (struct as_coord,
struct as_coord,
s_char *),
s_char *userdata);
extern int as_search(struct as_data *adp);
extern void as_delete(struct as_data *adp);
extern void as_reset(struct as_data *adp);
extern void as_stats(struct as_data *adp, FILE * fp);
extern struct as_path *as_find_cachepath(coord fx,
coord fy,
coord tx,
coord ty);
coord fy, coord tx, coord ty);
/* Functions that are "private" to algorithm */
@ -164,17 +169,17 @@ extern void as_clear_cachepath();
extern void as_enable_cachepath();
extern void as_disable_cachepath();
extern void as_makepath(struct as_data *adp);
extern void as_free_path(struct as_path *pp);
extern void as_makepath(struct as_data *adp);
extern void as_free_path(struct as_path *pp);
extern int as_costcomp(struct as_node **n1, struct as_node **n2);
extern struct as_queue *as_extend(struct as_data *adp);
extern struct as_queue *as_merge(struct as_data *adp,
struct as_queue *head,
struct as_node **neighbors);
extern struct as_queue *as_iscinq(struct as_data *adp, struct as_coord c);
extern void as_setcinq(struct as_data *adp,
struct as_coord c, struct as_queue *qp);
extern void as_free_hashtab(struct as_data *adp);
extern int as_winnow(struct as_data *adp,
struct as_coord *coords, int ncoords);
extern int as_costcomp(struct as_node **n1, struct as_node **n2);
extern struct as_queue *as_extend(struct as_data *adp);
extern struct as_queue *as_merge(struct as_data *adp,
struct as_queue *head,
struct as_node **neighbors);
extern struct as_queue *as_iscinq(struct as_data *adp, struct as_coord c);
extern void as_setcinq(struct as_data *adp,
struct as_coord c, struct as_queue *qp);
extern void as_free_hashtab(struct as_data *adp);
extern int as_winnow(struct as_data *adp,
struct as_coord *coords, int ncoords);

View file

@ -52,7 +52,7 @@ static struct as_frompath **fromhead = (struct as_frompath **)0;
/* Note that we only want to cache during updates. Other times, it
* probably doesn't make much sense, but can be done. */
static int as_cachepath_on = 0; /* Default to off */
static int as_cachepath_on = 0; /* Default to off */
void
as_enable_cachepath()
@ -107,9 +107,9 @@ as_add_cachepath(struct as_data *adp)
/* And set some stuff */
from->x = adp->from.x;
/* Here we malloc a whole bunch of tolist pointers. */
from->tolist = (struct as_topath **)calloc(1,
sizeof(struct as_topath *) *
WORLD_Y);
from->tolist = (struct as_topath **)calloc(1,
sizeof(struct as_topath
*) * WORLD_Y);
/* Now, add from to the global list */
from->next = fromhead[adp->from.y];
fromhead[adp->from.y] = from;

View file

@ -20,8 +20,8 @@
#include "as.h"
#if !defined(lint) && !defined(SABER)
static char sccsid[] = "@(#)as_costcomp.c 1.4 11/13/90";
#endif /* not lint*/
static char sccsid[] = "@(#)as_costcomp.c 1.4 11/13/90";
#endif /* not lint */
/*
* Compare the lower bound costs of two nodes. If the two nodes have
@ -31,19 +31,19 @@ static char sccsid[] = "@(#)as_costcomp.c 1.4 11/13/90";
int
as_costcomp(struct as_node **n1, struct as_node **n2)
{
double diff;
double diff;
diff = (*n1)->lbcost - (*n2)->lbcost;
if (diff < -0.0001)
return (-1);
if (diff > 0.0001)
return (1);
diff = (*n1)->lbcost - (*n2)->lbcost;
if (diff < -0.0001)
return (-1);
if (diff > 0.0001)
return (1);
/* equal, check secondary cost */
diff = (*n1)->seccost - (*n2)->seccost;
if (diff < -0.0001)
return (-1);
if (diff > 0.0001)
return (1);
return (0);
/* equal, check secondary cost */
diff = (*n1)->seccost - (*n2)->seccost;
if (diff < -0.0001)
return (-1);
if (diff > 0.0001)
return (1);
return (0);
}

View file

@ -22,10 +22,10 @@
#include "as.h"
#if !defined(lint) && !defined(SABER)
static char sccsid[] = "@(#)as_delete.c 1.5 11/13/90";
static char sccsid[] = "@(#)as_delete.c 1.5 11/13/90";
#endif /* not lint */
static void as_free_queue(struct as_queue *queue);
static void as_free_queue(struct as_queue *queue);
/*
* Free any dynamically allocated data stored in the as_data structure.
@ -34,15 +34,15 @@ void
as_reset(struct as_data *adp)
{
as_free_queue(adp->head);
adp->head = NULL;
as_free_queue(adp->tried);
adp->tried = NULL;
as_free_queue(adp->subsumed);
adp->subsumed = NULL;
as_free_hashtab(adp);
as_free_path(adp->path);
adp->path = NULL;
as_free_queue(adp->head);
adp->head = NULL;
as_free_queue(adp->tried);
adp->tried = NULL;
as_free_queue(adp->subsumed);
adp->subsumed = NULL;
as_free_hashtab(adp);
as_free_path(adp->path);
adp->path = NULL;
}
/*
@ -51,13 +51,13 @@ as_reset(struct as_data *adp)
static void
as_free_queue(struct as_queue *queue)
{
struct as_queue *qp, *qp2;
struct as_queue *qp, *qp2;
for (qp = queue; qp; qp = qp2) {
free((s_char *)qp->np);
qp2 = qp->next;
free((s_char *)qp);
}
for (qp = queue; qp; qp = qp2) {
free((s_char *)qp->np);
qp2 = qp->next;
free((s_char *)qp);
}
}
/*
@ -66,12 +66,12 @@ as_free_queue(struct as_queue *queue)
void
as_free_path(struct as_path *pp)
{
struct as_path *pp2;
struct as_path *pp2;
for (; pp; pp = pp2) {
pp2 = pp->next;
free((s_char *)pp);
}
for (; pp; pp = pp2) {
pp2 = pp->next;
free((s_char *)pp);
}
}
/*
@ -80,9 +80,9 @@ as_free_path(struct as_path *pp)
void
as_delete(struct as_data *adp)
{
as_reset(adp);
free((s_char *)adp->neighbor_coords);
free((s_char *)adp->neighbor_nodes);
free((s_char *)adp->hashtab);
free((s_char *)adp);
as_reset(adp);
free((s_char *)adp->neighbor_coords);
free((s_char *)adp->neighbor_nodes);
free((s_char *)adp->hashtab);
free((s_char *)adp);
}

View file

@ -22,7 +22,7 @@
#include "as.h"
#if !defined(lint) && !defined(SABER)
static char sccsid[] = "@(#)as_extend.c 1.2 11/13/90";
static char sccsid[] = "@(#)as_extend.c 1.2 11/13/90";
#endif /* not lint */
/*
@ -34,41 +34,41 @@ static char sccsid[] = "@(#)as_extend.c 1.2 11/13/90";
struct as_queue *
as_extend(struct as_data *adp)
{
struct as_queue *qp;
int i;
struct as_queue *head;
struct as_queue *qp;
int i;
struct as_queue *head;
head = adp->head;
head = adp->head;
/* Find the neighboring coordinates. */
i = (*adp->neighbor)(head->np->c, adp->neighbor_coords, adp->userdata);
if (i == 0)
return (NULL);
/*
* Get rid of neighbors that are more costly than ones we already have,
* and sort the rest into an array of as_nodes.
*/
i = as_winnow(adp, adp->neighbor_coords, i);
if (i < 0)
return (NULL);
if (i > 1)
qsort(adp->neighbor_nodes, i,
sizeof (*adp->neighbor_nodes),
(qsort_func_t) as_costcomp);
/* Find the neighboring coordinates. */
i = (*adp->neighbor) (head->np->c, adp->neighbor_coords,
adp->userdata);
if (i == 0)
return (NULL);
/*
* Get rid of neighbors that are more costly than ones we already have,
* and sort the rest into an array of as_nodes.
*/
i = as_winnow(adp, adp->neighbor_coords, i);
if (i < 0)
return (NULL);
if (i > 1)
qsort(adp->neighbor_nodes, i,
sizeof(*adp->neighbor_nodes), (qsort_func_t)as_costcomp);
/* remove old coord from head of queue and add to list of tried */
qp = head;
head = head->next;
if (head)
head->prev = NULL;
if (adp->tried) {
adp->tried->prev = qp;
qp->next = adp->tried;
adp->tried = qp;
} else
adp->tried = qp;
adp->tried->np->flags |= AS_TRIED;
/* remove old coord from head of queue and add to list of tried */
qp = head;
head = head->next;
if (head)
head->prev = NULL;
if (adp->tried) {
adp->tried->prev = qp;
qp->next = adp->tried;
adp->tried = qp;
} else
adp->tried = qp;
adp->tried->np->flags |= AS_TRIED;
head = as_merge(adp, head, adp->neighbor_nodes);
return (head);
head = as_merge(adp, head, adp->neighbor_nodes);
return (head);
}

View file

@ -22,7 +22,7 @@
#include "as.h"
#if !defined(lint) && !defined(SABER)
static char sccsid[] = "@(#)as_hash.c 1.2 11/13/90";
static char sccsid[] = "@(#)as_hash.c 1.2 11/13/90";
#endif /* not lint */
/*
@ -32,16 +32,16 @@ static char sccsid[] = "@(#)as_hash.c 1.2 11/13/90";
struct as_queue *
as_iscinq(struct as_data *adp, struct as_coord c)
{
int hashval;
struct as_hash *hp;
int hashval;
struct as_hash *hp;
hashval = (*adp->hash)(c) % adp->hashsize;
hashval = (*adp->hash) (c) % adp->hashsize;
for (hp = adp->hashtab[hashval]; hp; hp = hp->next)
if (hp->c.x == c.x && hp->c.y == c.y)
return (hp->qp);
for (hp = adp->hashtab[hashval]; hp; hp = hp->next)
if (hp->c.x == c.x && hp->c.y == c.y)
return (hp->qp);
return (NULL);
return (NULL);
}
/*
@ -50,19 +50,19 @@ as_iscinq(struct as_data *adp, struct as_coord c)
void
as_setcinq(struct as_data *adp, struct as_coord c, struct as_queue *qp)
{
int hashval;
struct as_hash *hp;
struct as_hash *new;
int hashval;
struct as_hash *hp;
struct as_hash *new;
new = (struct as_hash *)malloc(sizeof(struct as_hash));
new->c = c;
new->qp = qp;
new = (struct as_hash *)malloc(sizeof(struct as_hash));
new->c = c;
new->qp = qp;
hashval = (*adp->hash)(c) % adp->hashsize;
hp = adp->hashtab[hashval];
hashval = (*adp->hash) (c) % adp->hashsize;
hp = adp->hashtab[hashval];
new->next = (hp) ? hp : NULL;
adp->hashtab[hashval] = new;
new->next = (hp) ? hp : NULL;
adp->hashtab[hashval] = new;
}
/*
@ -72,14 +72,14 @@ as_setcinq(struct as_data *adp, struct as_coord c, struct as_queue *qp)
void
as_free_hashtab(struct as_data *adp)
{
int i;
struct as_hash *hp, *hp2;
int i;
struct as_hash *hp, *hp2;
for (i = 0; i < adp->hashsize; i++) {
for (hp = adp->hashtab[i]; hp; hp = hp2) {
hp2 = hp->next;
free((char *)hp);
}
adp->hashtab[i] = NULL;
for (i = 0; i < adp->hashsize; i++) {
for (hp = adp->hashtab[i]; hp; hp = hp2) {
hp2 = hp->next;
free((char *)hp);
}
adp->hashtab[i] = NULL;
}
}

View file

@ -22,7 +22,7 @@
#include "as.h"
#if !defined(lint) && !defined(SABER)
static char sccsid[] = "@(#)as_init.c 1.4 11/13/90";
static char sccsid[] = "@(#)as_init.c 1.4 11/13/90";
#endif /* not lint */
/*
@ -32,31 +32,31 @@ static char sccsid[] = "@(#)as_init.c 1.4 11/13/90";
struct as_data *
as_init(int maxneighbors,
int hashsize,
int (*hashfunc) (struct as_coord),
int (*neighborfunc) (struct as_coord, struct as_coord *, s_char *),
double (*lbcostfunc) (struct as_coord, struct as_coord, s_char *),
double (*realcostfunc) (struct as_coord, struct as_coord, s_char *),
double (*seccostfunc) (struct as_coord, struct as_coord, s_char *),
int (*hashfunc) (struct as_coord),
int (*neighborfunc) (struct as_coord, struct as_coord *, s_char *),
double (*lbcostfunc) (struct as_coord, struct as_coord, s_char *),
double (*realcostfunc) (struct as_coord, struct as_coord,
s_char *),
double (*seccostfunc) (struct as_coord, struct as_coord, s_char *),
s_char *userdata)
{
struct as_data *adp;
struct as_data *adp;
AS_NEW(adp, struct as_data, NULL);
AS_NEW_ARRAY(adp->neighbor_coords, struct as_coord,
maxneighbors, NULL);
AS_NEW_ARRAY(adp->neighbor_nodes, struct as_node *,
maxneighbors + 1, NULL);
AS_NEW_ARRAY(adp->hashtab, struct as_hash *,
hashsize, NULL);
AS_NEW(adp, struct as_data, NULL);
AS_NEW_ARRAY(adp->neighbor_coords, struct as_coord,
maxneighbors, NULL);
AS_NEW_ARRAY(adp->neighbor_nodes, struct as_node *,
maxneighbors + 1, NULL);
AS_NEW_ARRAY(adp->hashtab, struct as_hash *, hashsize, NULL);
adp->maxneighbors = maxneighbors;
adp->hashsize = hashsize;
adp->hash = hashfunc;
adp->neighbor = neighborfunc;
adp->lbcost = lbcostfunc;
adp->realcost = realcostfunc;
adp->seccost = seccostfunc;
adp->userdata = userdata;
adp->maxneighbors = maxneighbors;
adp->hashsize = hashsize;
adp->hash = hashfunc;
adp->neighbor = neighborfunc;
adp->lbcost = lbcostfunc;
adp->realcost = realcostfunc;
adp->seccost = seccostfunc;
adp->userdata = userdata;
return (adp);
return (adp);
}

View file

@ -22,7 +22,7 @@
#include "as.h"
#if !defined(lint) && !defined(SABER)
static char sccsid[] = "@(#)as_merge.c 1.2 11/13/90";
static char sccsid[] = "@(#)as_merge.c 1.2 11/13/90";
#endif /* not lint */
/*
@ -30,55 +30,56 @@ static char sccsid[] = "@(#)as_merge.c 1.2 11/13/90";
* both by lower bound cost and then by secondary cost.
*/
struct as_queue *
as_merge(struct as_data *adp, struct as_queue *head, struct as_node **neighbors)
as_merge(struct as_data *adp, struct as_queue *head,
struct as_node **neighbors)
{
struct as_queue *qp;
struct as_queue *pp; /* previous pointer */
struct as_queue *ip; /* insert pointer */
struct as_node *np;
int i;
struct as_queue *qp;
struct as_queue *pp; /* previous pointer */
struct as_queue *ip; /* insert pointer */
struct as_node *np;
int i;
qp = head;
pp = NULL;
for (i = 0; neighbors[i]; i++) {
np = neighbors[i];
/* scan until qp points to a node we should go in front of */
while (qp && (qp->np->lbcost < np->lbcost)) {
pp = qp;
qp = qp->next;
}
/* check for equal lower bounds, and use secondary cost if = */
if (qp && qp->np->lbcost == np->lbcost) {
while (qp && (qp->np->lbcost == np->lbcost) &&
(qp->np->seccost < np->seccost)) {
pp = qp;
qp = qp->next;
}
}
AS_NEW_MALLOC(ip, struct as_queue, NULL);
/* if there was such a node, insert us in front of it */
if (qp) {
ip->prev = qp->prev;
if (ip->prev)
ip->prev->next = ip;
ip->next = qp;
qp->prev = ip;
if (qp == head)
head = ip;
} else { /* otherwise add us to end of queue */
ip->next = NULL;
ip->prev = pp;
if (ip->prev)
ip->prev->next = ip;
else {
head = ip;
}
pp = ip;
}
ip->np = np;
as_setcinq(adp, np->c, ip);
np->step++;
qp = head;
pp = NULL;
for (i = 0; neighbors[i]; i++) {
np = neighbors[i];
/* scan until qp points to a node we should go in front of */
while (qp && (qp->np->lbcost < np->lbcost)) {
pp = qp;
qp = qp->next;
}
/* check for equal lower bounds, and use secondary cost if = */
if (qp && qp->np->lbcost == np->lbcost) {
while (qp && (qp->np->lbcost == np->lbcost) &&
(qp->np->seccost < np->seccost)) {
pp = qp;
qp = qp->next;
}
}
AS_NEW_MALLOC(ip, struct as_queue, NULL);
/* if there was such a node, insert us in front of it */
if (qp) {
ip->prev = qp->prev;
if (ip->prev)
ip->prev->next = ip;
ip->next = qp;
qp->prev = ip;
if (qp == head)
head = ip;
} else { /* otherwise add us to end of queue */
ip->next = NULL;
ip->prev = pp;
if (ip->prev)
ip->prev->next = ip;
else {
head = ip;
}
pp = ip;
}
ip->np = np;
as_setcinq(adp, np->c, ip);
np->step++;
}
return (head);
return (head);
}

View file

@ -26,7 +26,7 @@
#include "as.h"
#if !defined(lint) && !defined(SABER)
static char sccsid[] = "@(#)as_search.c 1.2 11/13/90";
static char sccsid[] = "@(#)as_search.c 1.2 11/13/90";
#endif /* not lint */
/*
@ -39,95 +39,94 @@ static char sccsid[] = "@(#)as_search.c 1.2 11/13/90";
int
as_search(struct as_data *adp)
{
int iter = 0;
struct as_queue *head;
struct as_node *np;
int iter = 0;
struct as_queue *head;
struct as_node *np;
#ifdef DEBUG
int i;
struct as_queue *qp;
struct as_path *pp;
int i;
struct as_queue *qp;
struct as_path *pp;
#endif /* DEBUG */
struct as_queue *as_extend(struct as_data *adp);
struct as_queue *as_extend(struct as_data *adp);
as_reset(adp);
as_reset(adp);
/*
* Jump start the queue by making first element the zero-cost
* node where we start.
*/
AS_NEW_MALLOC(head, struct as_queue, -2);
adp->head = head;
head->next = head->prev = NULL;
AS_NEW(np, struct as_node, -2);
np->c = adp->from;
head->np = np;
as_setcinq(adp, head->np->c, adp->head);
/*
* Jump start the queue by making first element the zero-cost
* node where we start.
*/
AS_NEW_MALLOC(head, struct as_queue, -2);
adp->head = head;
head->next = head->prev = NULL;
AS_NEW(np, struct as_node, -2);
np->c = adp->from;
head->np = np;
as_setcinq(adp, head->np->c, adp->head);
for (;;) {
iter++;
for (;;) {
iter++;
#ifdef DEBUG
fprintf(stderr, "Iteration %d, head at %d, %d\n", iter,
head->np->c.x, head->np->c.y);
fprintf(stderr, "Iteration %d, head at %d, %d\n", iter,
head->np->c.x, head->np->c.y);
#endif /* DEBUG */
/* see if we're done, one way or another */
if (head == NULL)
break;
/* see if we're done, one way or another */
if (head == NULL)
break;
/* Add it to the cache */
as_add_cachepath(adp);
/* Add it to the cache */
as_add_cachepath(adp);
if (head->np->c.x == adp->to.x && head->np->c.y == adp->to.y)
break;
if (head->np->c.x == adp->to.x && head->np->c.y == adp->to.y)
break;
/* extend queue by neighbors */
/* extend queue by neighbors */
#ifdef DEBUG
fprintf(stderr, "\tExtending queue\n");
fprintf(stderr, "\tExtending queue\n");
#endif /* DEBUG */
adp->head = head = as_extend(adp);
adp->head = head = as_extend(adp);
#ifdef DEBUG
fprintf(stderr, "queue:\n");
i = 0;
for (qp = head; qp; qp = qp->next) {
fprintf(stderr, "\t%d, %d so far %f lb %f sec %f\n",
qp->np->c.x, qp->np->c.y,
qp->np->knowncost,
qp->np->lbcost,
qp->np->seccost);
i++;
}
fprintf(stderr, "\tqueue len %d\n", i);
#endif /* DEBUG */
fprintf(stderr, "queue:\n");
i = 0;
for (qp = head; qp; qp = qp->next) {
fprintf(stderr, "\t%d, %d so far %f lb %f sec %f\n",
qp->np->c.x, qp->np->c.y,
qp->np->knowncost, qp->np->lbcost, qp->np->seccost);
i++;
}
if (head == NULL) {
#ifdef DEBUG
fprintf(stderr, "Failed\n");
fprintf(stderr, "\tqueue len %d\n", i);
#endif /* DEBUG */
return (-1);
}
as_makepath(adp);
}
if (head == NULL) {
#ifdef DEBUG
fprintf(stderr, "Failed\n");
#endif /* DEBUG */
return (-1);
}
as_makepath(adp);
#ifdef DEBUG
fprintf(stderr, "Succeeded, iter %d, cost %f!\n", iter, head->np->knowncost);
fprintf(stderr, "Path:\n");
for (pp = adp->path; pp; pp = pp->next) {
fprintf(stderr, "\t%d, %d\n", pp->c.x, pp->c.y);
}
fprintf(stderr, "Tried queue:\n");
for (qp = adp->tried; qp; qp = qp->next) {
fprintf(stderr, "\t%d, %d\n", qp->np->c.x, qp->np->c.y);
}
fprintf(stderr, "Subsumed queue:\n");
for (qp = adp->subsumed; qp; qp = qp->next) {
fprintf(stderr, "\t%d, %d\n", qp->np->c.x, qp->np->c.y);
}
fprintf(stderr, "Succeeded, iter %d, cost %f!\n", iter,
head->np->knowncost);
fprintf(stderr, "Path:\n");
for (pp = adp->path; pp; pp = pp->next) {
fprintf(stderr, "\t%d, %d\n", pp->c.x, pp->c.y);
}
fprintf(stderr, "Tried queue:\n");
for (qp = adp->tried; qp; qp = qp->next) {
fprintf(stderr, "\t%d, %d\n", qp->np->c.x, qp->np->c.y);
}
fprintf(stderr, "Subsumed queue:\n");
for (qp = adp->subsumed; qp; qp = qp->next) {
fprintf(stderr, "\t%d, %d\n", qp->np->c.x, qp->np->c.y);
}
#endif /* DEBUG */
return (0);
return (0);
}
/*
@ -137,13 +136,13 @@ as_search(struct as_data *adp)
void
as_makepath(struct as_data *adp)
{
struct as_path *pp;
struct as_node *np;
struct as_path *pp;
struct as_node *np;
for (np = adp->head->np; np; np = np->back) {
pp = (struct as_path *)malloc(sizeof(struct as_path));
pp->c = np->c;
pp->next = adp->path;
adp->path = pp;
}
for (np = adp->head->np; np; np = np->back) {
pp = (struct as_path *)malloc(sizeof(struct as_path));
pp->c = np->c;
pp->next = adp->path;
adp->path = pp;
}
}

View file

@ -21,53 +21,55 @@
#include "as.h"
#if !defined(lint) && !defined(SABER)
static char sccsid[] = "@(#)as_stats.c 1.2 11/13/90";
static char sccsid[] = "@(#)as_stats.c 1.2 11/13/90";
#endif /* not lint */
/*
* Print statistics on algorithm performance to the file pointer "fp".
*/
void
as_stats(struct as_data *adp, FILE *fp)
as_stats(struct as_data *adp, FILE * fp)
{
int i;
int j;
int total_q;
int total_h;
struct as_queue *qp;
struct as_hash *hp;
int i;
int j;
int total_q;
int total_h;
struct as_queue *qp;
struct as_hash *hp;
fprintf(fp, "Statistics:\n");
fprintf(fp, "Statistics:\n");
fprintf(fp, "queue lengths:\n");
total_q = 0;
total_h = 0;
for (i = 0, qp = adp->head; qp; qp = qp->next)
i++;
fprintf(fp, "\tmain:\t%d\n", i);
total_q += i;
for (i = 0, qp = adp->tried; qp; qp = qp->next)
i++;
fprintf(fp, "\ttried:\t%d\n", i);
total_q += i;
for (i = 0, qp = adp->subsumed; qp; qp = qp->next)
i++;
fprintf(fp, "\tsubsumed:\t%d\n", i);
total_q += i;
fprintf(fp, "hash table statistics (size %d):\n", adp->hashsize);
for (i = 0; i < adp->hashsize; i++) {
for (j = 0, hp = adp->hashtab[i]; hp; hp = hp->next)
j++;
fprintf(fp, "\t%d\t%d\n", i, j);
total_h += j;
}
fprintf(fp, "\ttotal\t%d\n", total_h);
fprintf(fp, "approximate memory usage (bytes):\n");
fprintf(fp, "\tqueues\t%d\n", (int)(total_q * sizeof (struct as_queue)));
fprintf(fp, "\tnodes\t%d\n", (int)(total_q * sizeof (struct as_node)));
fprintf(fp, "\thash ents\t%d\n", (int)(total_h * sizeof (struct as_hash)));
fprintf(fp, "\ttotal\t%d\n",
(int)(total_q * sizeof (struct as_queue) +
total_q * sizeof (struct as_node) +
total_h * sizeof (struct as_hash)));
fprintf(fp, "queue lengths:\n");
total_q = 0;
total_h = 0;
for (i = 0, qp = adp->head; qp; qp = qp->next)
i++;
fprintf(fp, "\tmain:\t%d\n", i);
total_q += i;
for (i = 0, qp = adp->tried; qp; qp = qp->next)
i++;
fprintf(fp, "\ttried:\t%d\n", i);
total_q += i;
for (i = 0, qp = adp->subsumed; qp; qp = qp->next)
i++;
fprintf(fp, "\tsubsumed:\t%d\n", i);
total_q += i;
fprintf(fp, "hash table statistics (size %d):\n", adp->hashsize);
for (i = 0; i < adp->hashsize; i++) {
for (j = 0, hp = adp->hashtab[i]; hp; hp = hp->next)
j++;
fprintf(fp, "\t%d\t%d\n", i, j);
total_h += j;
}
fprintf(fp, "\ttotal\t%d\n", total_h);
fprintf(fp, "approximate memory usage (bytes):\n");
fprintf(fp, "\tqueues\t%d\n",
(int)(total_q * sizeof(struct as_queue)));
fprintf(fp, "\tnodes\t%d\n", (int)(total_q * sizeof(struct as_node)));
fprintf(fp, "\thash ents\t%d\n",
(int)(total_h * sizeof(struct as_hash)));
fprintf(fp, "\ttotal\t%d\n",
(int)(total_q * sizeof(struct as_queue) +
total_q * sizeof(struct as_node) +
total_h * sizeof(struct as_hash)));
}

View file

@ -22,12 +22,12 @@
#include "as.h"
#if !defined(lint) && !defined(SABER)
static char sccsid[] = "@(#)as_winnow.c 1.8 11/13/90";
static char sccsid[] = "@(#)as_winnow.c 1.8 11/13/90";
#endif /* not lint */
static struct as_node *
as_newnode(struct as_node *backp, struct as_coord c,
double inclbcost, double lbcost, double knowncost, double seccost);
static struct as_node *as_newnode(struct as_node *backp, struct as_coord c,
double inclbcost, double lbcost,
double knowncost, double seccost);
/*
* Take a list of neighbor coordinates and winnow them down into
@ -46,132 +46,133 @@ as_newnode(struct as_node *backp, struct as_coord c,
int
as_winnow(struct as_data *adp, struct as_coord *coords, int ncoords)
{
int i = 0;
int fix_pointer;
double knowncost;
double incknowncost;
double lbcost;
double inclbcost;
double seccost;
struct as_coord *cp;
struct as_coord *end;
struct as_queue *qp;
struct as_node *np;
int i = 0;
int fix_pointer;
double knowncost;
double incknowncost;
double lbcost;
double inclbcost;
double seccost;
struct as_coord *cp;
struct as_coord *end;
struct as_queue *qp;
struct as_node *np;
for (cp = coords, end = coords + ncoords; cp < end; cp++) {
fix_pointer = 0;
incknowncost = (*adp->realcost)(adp->head->np->c, *cp,
adp->userdata);
knowncost = adp->head->np->knowncost + incknowncost;
/*
* If this neighbor is already in the queue, we can
* save some time.
*/
qp = as_iscinq(adp, *cp);
inclbcost = qp ? qp->np->inclbcost :
(*adp->lbcost)(*cp, adp->to, adp->userdata);
if (inclbcost < 0.0) /* skip bad cases */
continue;
lbcost = knowncost + inclbcost;
for (cp = coords, end = coords + ncoords; cp < end; cp++) {
fix_pointer = 0;
incknowncost = (*adp->realcost) (adp->head->np->c, *cp,
adp->userdata);
knowncost = adp->head->np->knowncost + incknowncost;
/*
* If this neighbor is already in the queue, we can
* save some time.
*/
qp = as_iscinq(adp, *cp);
inclbcost = qp ? qp->np->inclbcost :
(*adp->lbcost) (*cp, adp->to, adp->userdata);
if (inclbcost < 0.0) /* skip bad cases */
continue;
lbcost = knowncost + inclbcost;
#ifdef DEBUG
fprintf(stderr, "\tneighbor %d, %d, lbcost %f ", cp->x, cp->y, lbcost);
fprintf(stderr, "\tneighbor %d, %d, lbcost %f ", cp->x, cp->y,
lbcost);
#endif /* DEBUG */
/*
* If this neighbor is already in the queue, check to
* see which has the lower cost. If the one already in
* the queue is cheaper, skip this neighbor as bad. If
* the neighbor does, delete the one in the queue.
*/
if (qp) {
if (qp->np->lbcost <= lbcost) {
/*
* If this neighbor is already in the queue, check to
* see which has the lower cost. If the one already in
* the queue is cheaper, skip this neighbor as bad. If
* the neighbor does, delete the one in the queue.
*/
if (qp) {
if (qp->np->lbcost <= lbcost) {
#ifdef DEBUG
fprintf(stderr, "old, loses to %f\n", qp->np->lbcost);
fprintf(stderr, "old, loses to %f\n", qp->np->lbcost);
#endif /* DEBUG */
continue;
} else {
continue;
} else {
#ifdef DEBUG
fprintf(stderr, "old, wins over %f\n", qp->np->lbcost);
fprintf(stderr, "old, wins over %f\n", qp->np->lbcost);
#endif /* DEBUG */
if (qp->np->flags & AS_TRIED) {
/* should "never happen" */
return (0);
}
if (qp->np->flags & AS_TRIED) {
/* should "never happen" */
return (0);
}
/*
* The neighbor is better than a previously visited coordinate;
* remove the old coordinate from the queue and add it to
* the subsumed nodes queue. To get here at
* all we can't be the head, thus qp->prev is defined.
*/
/* Delete from main queue */
qp->prev->next = qp->next;
if (qp->next)
qp->next->prev = qp->prev;
/* Delete from main queue */
qp->prev->next = qp->next;
if (qp->next)
qp->next->prev = qp->prev;
/* Add to subsumed queue */
if (adp->subsumed) {
adp->subsumed->prev = qp;
qp->next = adp->subsumed;
} else {
qp->next = NULL;
}
adp->subsumed = qp;
adp->subsumed->prev = NULL;
fix_pointer = 1;
/* Add to subsumed queue */
if (adp->subsumed) {
adp->subsumed->prev = qp;
qp->next = adp->subsumed;
} else {
qp->next = NULL;
}
adp->subsumed = qp;
adp->subsumed->prev = NULL;
fix_pointer = 1;
/*
* At this point, the as_iscinq code may contain bogus pointer
* refs. They'll be fixed when as_merge merges the new
* neighbors into the main queue.
*/
}
}
}
}
#ifdef DEBUG
else {
fprintf(stderr, "new\n");
}
else {
fprintf(stderr, "new\n");
}
#endif /* DEBUG */
if (qp)
seccost = qp->np->seccost;
else
seccost = (adp->seccost) ?
(*adp->seccost)(*cp, adp->to, adp->userdata) :
0.0;
np = as_newnode(adp->head->np, *cp, inclbcost, lbcost,
if (qp)
seccost = qp->np->seccost;
else
seccost = (adp->seccost) ?
(*adp->seccost) (*cp, adp->to, adp->userdata) : 0.0;
np = as_newnode(adp->head->np, *cp, inclbcost, lbcost,
knowncost, seccost);
if (np == NULL)
return (0);
if (fix_pointer) {
if (np == NULL)
return (0);
if (fix_pointer) {
#ifdef DEBUG
fprintf(stderr, "Fixing pointer for %d, %d\n", adp->subsumed->np->c.x,
adp->subsumed->np->c.y);
fprintf(stderr, "Fixing pointer for %d, %d\n",
adp->subsumed->np->c.x, adp->subsumed->np->c.y);
#endif
adp->subsumed->np->back = np;
}
adp->neighbor_nodes[i++] = np;
adp->subsumed->np->back = np;
}
adp->neighbor_nodes[i] = NULL;
adp->neighbor_nodes[i++] = np;
return (i);
}
adp->neighbor_nodes[i] = NULL;
return (i);
}
static struct as_node *
as_newnode(struct as_node *backp, struct as_coord c,
double inclbcost, double lbcost, double knowncost, double seccost)
double inclbcost, double lbcost, double knowncost,
double seccost)
{
struct as_node *np;
struct as_node *np;
/* Got an interesting coordinate; make a node for it. */
AS_NEW_MALLOC(np, struct as_node, NULL);
np->flags = 0;
np->c = c;
np->inclbcost = inclbcost;
np->lbcost = lbcost;
np->knowncost = knowncost;
np->seccost = seccost;
np->step = backp->step;
np->back = backp;
/* Got an interesting coordinate; make a node for it. */
AS_NEW_MALLOC(np, struct as_node, NULL);
np->flags = 0;
np->c = c;
np->inclbcost = inclbcost;
np->lbcost = lbcost;
np->knowncost = knowncost;
np->seccost = seccost;
np->step = backp->step;
np->back = backp;
return (np);
return (np);
}