]> git.pond.sub.org Git - empserver/blob - src/lib/as/as_merge.c
Indented with src/scripts/indent-emp.
[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,
34          struct as_node **neighbors)
35 {
36     struct as_queue *qp;
37     struct as_queue *pp;        /* previous pointer */
38     struct as_queue *ip;        /* insert pointer */
39     struct as_node *np;
40     int i;
41
42     qp = head;
43     pp = NULL;
44     for (i = 0; neighbors[i]; i++) {
45         np = neighbors[i];
46         /* scan until qp points to a node we should go in front of */
47         while (qp && (qp->np->lbcost < np->lbcost)) {
48             pp = qp;
49             qp = qp->next;
50         }
51         /* check for equal lower bounds, and use secondary cost if = */
52         if (qp && qp->np->lbcost == np->lbcost) {
53             while (qp && (qp->np->lbcost == np->lbcost) &&
54                    (qp->np->seccost < np->seccost)) {
55                 pp = qp;
56                 qp = qp->next;
57             }
58         }
59         AS_NEW_MALLOC(ip, struct as_queue, NULL);
60         /* if there was such a node, insert us in front of it */
61         if (qp) {
62             ip->prev = qp->prev;
63             if (ip->prev)
64                 ip->prev->next = ip;
65             ip->next = qp;
66             qp->prev = ip;
67             if (qp == head)
68                 head = ip;
69         } else {                /* otherwise add us to end of queue */
70             ip->next = NULL;
71             ip->prev = pp;
72             if (ip->prev)
73                 ip->prev->next = ip;
74             else {
75                 head = ip;
76             }
77             pp = ip;
78         }
79         ip->np = np;
80         as_setcinq(adp, np->c, ip);
81         np->step++;
82     }
83
84     return (head);
85 }