]> git.pond.sub.org Git - empserver/blob - src/lib/as/as_merge.c
Import of Empire 4.2.12
[empserver] / src / lib / as / as_merge.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_merge.c      1.2     11/13/90";
26 #endif /* not lint */
27
28 /*
29  * Merge neighbors into queue, keeping it sorted.  "neighbors" is sorted,
30  * both by lower bound cost and then by secondary cost.
31  */
32 struct as_queue *
33 as_merge(struct as_data *adp, struct as_queue *head, struct as_node **neighbors)
34 {
35         struct as_queue *qp;
36         struct as_queue *pp;            /* previous pointer */
37         struct as_queue *ip;            /* insert pointer */
38         struct as_node  *np;
39         int             i;
40
41         qp = head;
42         pp = NULL;
43         for (i = 0; neighbors[i]; i++) {
44                 np = neighbors[i];
45                 /* scan until qp points to a node we should go in front of */
46                 while (qp && (qp->np->lbcost < np->lbcost)) {
47                         pp = qp;
48                         qp = qp->next;
49                 }
50                 /* check for equal lower bounds, and use secondary cost if = */
51                 if (qp && qp->np->lbcost == np->lbcost) {
52                         while (qp && (qp->np->lbcost == np->lbcost) &&
53                                 (qp->np->seccost < np->seccost)) {
54                                 pp = qp;
55                                 qp = qp->next;
56                         }
57                 }
58                 AS_NEW_MALLOC(ip, struct as_queue, NULL);
59                 /* if there was such a node, insert us in front of it */
60                 if (qp) {
61                         ip->prev = qp->prev;
62                         if (ip->prev)
63                                 ip->prev->next = ip;
64                         ip->next = qp;
65                         qp->prev = ip;
66                         if (qp == head)
67                                 head = ip;
68                 } else {        /* otherwise add us to end of queue */
69                         ip->next = NULL;
70                         ip->prev = pp;
71                         if (ip->prev)
72                                 ip->prev->next = ip;
73                         else {
74                                 head = ip;
75                         }
76                         pp = ip;
77                 }
78                 ip->np = np;
79                 as_setcinq(adp, np->c, ip);
80                 np->step++;
81         }
82
83         return (head);
84 }