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