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