]> git.pond.sub.org Git - empserver/blob - src/lib/as/as_hash.c
Import of Empire 4.2.12
[empserver] / src / lib / as / as_hash.c
1 /*
2  *  A* Search - A search library used in Empire to determine paths between
3  *              objects.
4  *  Copyright (C) 1990-1998 Phil Lapsley
5  *
6  *  This program is free software; you can redistribute it and/or modify
7  *  it under the terms of the GNU General Public License as published by
8  *  the Free Software Foundation; either version 2 of the License, or
9  *  (at your option) any later version.
10  *
11  *  This program is distributed in the hope that it will be useful,
12  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
13  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14  *  GNU General Public License for more details.
15  *
16  *  You should have received a copy of the GNU General Public License
17  *  along with this program; if not, write to the Free Software
18  *  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
19  */
20 #include <stdio.h>
21 #include <stdlib.h>
22 #include "as.h"
23
24 #if !defined(lint) && !defined(SABER)
25 static  char    sccsid[] = "@(#)as_hash.c       1.2     11/13/90";
26 #endif /* not lint */
27
28 /*
29  * Return a pointer to the as_queue structure associated with
30  * this coordinate if the coordinate is in the queue.
31  */
32 struct as_queue *
33 as_iscinq(struct as_data *adp, struct as_coord c)
34 {
35         int             hashval;
36         struct as_hash  *hp;
37
38         hashval = (*adp->hash)(c) % adp->hashsize;
39
40         for (hp = adp->hashtab[hashval]; hp; hp = hp->next)
41                 if (hp->c.x == c.x && hp->c.y == c.y)
42                                 return (hp->qp);
43
44         return (NULL);
45 }
46
47 /*
48  * Set the queue structure associated with this coordinate.
49  */
50 void
51 as_setcinq(struct as_data *adp, struct as_coord c, struct as_queue *qp)
52 {
53         int             hashval;
54         struct as_hash  *hp;
55         struct as_hash  *new;
56
57         new = (struct as_hash *)malloc(sizeof(struct as_hash));
58         new->c = c;
59         new->qp = qp;
60
61         hashval = (*adp->hash)(c) % adp->hashsize;
62         hp = adp->hashtab[hashval];
63
64         new->next = (hp) ? hp : NULL;
65         adp->hashtab[hashval] = new;
66 }
67
68 /*
69  * Walk down the hash table array, freeing the chains and zeroing
70  * the chain pointers.
71  */
72 void
73 as_free_hashtab(struct as_data *adp)
74 {
75         int             i;
76         struct as_hash  *hp, *hp2;
77
78         for (i = 0; i < adp->hashsize; i++) {
79                 for (hp = adp->hashtab[i]; hp; hp = hp2) {
80                         hp2 = hp->next;
81                         free((char *)hp);
82                 }
83                 adp->hashtab[i] = NULL;
84         }
85 }