src/lib/as/as_merge.c

Go to the documentation of this file.
00001 /*
00002  *  A* Search - A search library used in Empire to determine paths between
00003  *              objects.
00004  *  Copyright (C) 1990-1998 Phil Lapsley
00005  *
00006  *  This program is free software; you can redistribute it and/or modify
00007  *  it under the terms of the GNU General Public License as published by
00008  *  the Free Software Foundation; either version 2 of the License, or
00009  *  (at your option) any later version.
00010  *
00011  *  This program is distributed in the hope that it will be useful,
00012  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
00013  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00014  *  GNU General Public License for more details.
00015  *
00016  *  You should have received a copy of the GNU General Public License
00017  *  along with this program; if not, write to the Free Software
00018  *  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
00019  */
00020 
00021 #include <config.h>
00022 
00023 #include <stdlib.h>
00024 #include "as.h"
00025 
00026 /*
00027  * Merge neighbors into queue, keeping it sorted.  "neighbors" is sorted,
00028  * both by lower bound cost and then by secondary cost.
00029  */
00030 struct as_queue *
00031 as_merge(struct as_data *adp, struct as_queue *head,
00032          struct as_node **neighbors)
00033 {
00034     struct as_queue *qp;
00035     struct as_queue *pp;        /* previous pointer */
00036     struct as_queue *ip;        /* insert pointer */
00037     struct as_node *np;
00038     int i;
00039 
00040     qp = head;
00041     pp = NULL;
00042     for (i = 0; neighbors[i]; i++) {
00043         np = neighbors[i];
00044         /* scan until qp points to a node we should go in front of */
00045         while (qp && (qp->np->lbcost < np->lbcost)) {
00046             pp = qp;
00047             qp = qp->next;
00048         }
00049         /* check for equal lower bounds, and use secondary cost if = */
00050         if (qp && qp->np->lbcost == np->lbcost) {
00051             while (qp && (qp->np->lbcost == np->lbcost) &&
00052                    (qp->np->seccost < np->seccost)) {
00053                 pp = qp;
00054                 qp = qp->next;
00055             }
00056         }
00057         AS_NEW_MALLOC(ip, struct as_queue, NULL);
00058         /* if there was such a node, insert us in front of it */
00059         if (qp) {
00060             ip->prev = qp->prev;
00061             if (ip->prev)
00062                 ip->prev->next = ip;
00063             ip->next = qp;
00064             qp->prev = ip;
00065             if (qp == head)
00066                 head = ip;
00067         } else {                /* otherwise add us to end of queue */
00068             ip->next = NULL;
00069             ip->prev = pp;
00070             if (ip->prev)
00071                 ip->prev->next = ip;
00072             else {
00073                 head = ip;
00074             }
00075             pp = ip;
00076         }
00077         ip->np = np;
00078         as_setcinq(adp, np->c, ip);
00079         np->step++;
00080     }
00081 
00082     return head;
00083 }

Generated on Fri Mar 28 11:01:11 2008 for empserver by  doxygen 1.5.2