helpers.c

Go to the documentation of this file.
00001 /* LIBDGL -- a Directed Graph Library implementation
00002  * Copyright (C) 2002 Roberto Micarelli
00003  *
00004  * This program is free software; you can redistribute it and/or modify
00005  * it under the terms of the GNU General Public License as published by
00006  * the Free Software Foundation; either version 2 of the License, or
00007  * (at your option) any later version.
00008  *
00009  * This program is distributed in the hope that it will be useful,
00010  * but WITHOUT ANY WARRANTY; without even the implied warranty of
00011  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00012  * GNU General Public License for more details.
00013  *
00014  * You should have received a copy of the GNU General Public License
00015  * along with this program; if not, write to the Free Software
00016  * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
00017  */
00018 
00019 /*
00020  * best view with tabstop=4
00021  */
00022 
00023 
00024 #include <stdlib.h>
00025 #include <string.h>
00026 
00027 #include "type.h"
00028 #include "tree.h"
00029 #include "graph.h"
00030 #include "helpers.h"
00031 
00032 /*
00033  * helpers for parametric stack
00034  */
00035 unsigned char * dgl_mempush( unsigned char * pstack , long * istack , long size , void * pv )
00036 {
00037         if ( *istack == 0 ) pstack = NULL;
00038         pstack = realloc( pstack , size * (1 + *istack) );
00039         if ( pstack == NULL ) return NULL;
00040         memcpy( & pstack[ (*istack) * size ] , pv , size );
00041         (*istack) ++;
00042         return pstack;
00043 }
00044 
00045 unsigned char * dgl_mempop( unsigned char * pstack , long * istack , long size )
00046 {
00047         if ( *istack == 0 ) return NULL;
00048         return & pstack[ size * (--(*istack)) ];
00049 }
00050 
00051 void dgl_swapInt32Bytes( dglInt32_t * pn ) {
00052         unsigned char * pb = (unsigned char *) pn;
00053         pb[0] ^= pb[3];
00054         pb[3] ^= pb[0];
00055         pb[0] ^= pb[3];
00056 
00057         pb[1] ^= pb[2];
00058         pb[2] ^= pb[1];
00059         pb[1] ^= pb[2];
00060 }       
00061 
00062 void dgl_swapInt64Bytes( dglInt64_t * pn ) {
00063         unsigned char * pb = (unsigned char *) pn;
00064         pb[0] ^= pb[7];
00065         pb[7] ^= pb[0];
00066         pb[0] ^= pb[7];
00067 
00068         pb[1] ^= pb[6];
00069         pb[6] ^= pb[1];
00070         pb[1] ^= pb[6];
00071 
00072         pb[2] ^= pb[5];
00073         pb[5] ^= pb[2];
00074         pb[2] ^= pb[5];
00075 
00076         pb[3] ^= pb[4];
00077         pb[4] ^= pb[3];
00078         pb[3] ^= pb[4];
00079 }       
00080 
00081 /*
00082  * Keep the edge cost prioritizer in sync
00083  */
00084 int dgl_edge_prioritizer_del(dglGraph_s * pG, dglInt32_t nId, dglInt32_t nPriId)
00085 {
00086         dglTreeEdgePri32_s findPriItem, * pPriItem;
00087         register int iEdge1, iEdge2;
00088         dglInt32_t * pnNew;
00089 
00090         if (pG->edgePrioritizer.pvAVL) {
00091 
00092                 findPriItem.nKey = nPriId;
00093                 pPriItem = avl_find(pG->edgePrioritizer.pvAVL, &findPriItem);
00094 
00095                 if ( pPriItem && pPriItem->pnData ) {
00096 
00097                         pnNew = malloc( sizeof(dglInt32_t) * pPriItem->cnData );
00098 
00099                         if ( pnNew == NULL ) {
00100                                 pG->iErrno = DGL_ERR_MemoryExhausted;
00101                                 return -pG->iErrno;
00102                         }
00103 
00104                         for (iEdge1 = 0, iEdge2 = 0 ; iEdge2 < pPriItem->cnData ; iEdge2 ++) {
00105                                 if (pPriItem->pnData[iEdge2] != nId) {
00106                                         pnNew[iEdge1++] = pPriItem->pnData[iEdge2];
00107                                 }
00108                         }
00109 
00110                         free(pPriItem->pnData);
00111                         if ( iEdge1 == 0) {
00112                                 free(pnNew);
00113                                 pPriItem->pnData = NULL;
00114                                 pPriItem->cnData = 0;
00115                         }
00116                         else {
00117                                 pPriItem->pnData = pnNew;
00118                                 pPriItem->cnData = iEdge1;
00119                         }
00120                 }
00121         }
00122         return 0;
00123 }
00124 
00125 int dgl_edge_prioritizer_add(dglGraph_s * pG, dglInt32_t nId, dglInt32_t nPriId)
00126 {
00127         dglTreeEdgePri32_s * pPriItem;
00128 
00129         if ( pG->edgePrioritizer.pvAVL == NULL ) {
00130                 pG->edgePrioritizer.pvAVL = avl_create( dglTreeEdgePri32Compare, NULL, dglTreeGetAllocator() );
00131                 if ( pG->edgePrioritizer.pvAVL == NULL ) {
00132                         pG->iErrno = DGL_ERR_MemoryExhausted;
00133                         return -pG->iErrno;
00134                 }
00135         }
00136         pPriItem = dglTreeEdgePri32Add(pG->edgePrioritizer.pvAVL, nPriId);
00137         if (pPriItem == NULL) {
00138                 pG->iErrno = DGL_ERR_MemoryExhausted;
00139                 return -pG->iErrno;
00140         }
00141         if (pPriItem->cnData == 0) {
00142                 pPriItem->pnData = (dglInt32_t*) malloc( sizeof(dglInt32_t) );
00143         }
00144         else {
00145                 pPriItem->pnData = (dglInt32_t*) realloc( pPriItem->pnData , sizeof(dglInt32_t) * (pPriItem->cnData + 1) );
00146         }
00147         if ( pPriItem->pnData == NULL ) {
00148                 pG->iErrno = DGL_ERR_MemoryExhausted;
00149                 return -pG->iErrno;
00150         }
00151         pPriItem->pnData[ pPriItem->cnData ] = nId;
00152         pPriItem->cnData++;
00153         return 0;
00154 }

Generated on Sun Apr 6 17:32:44 2008 for GRASS by  doxygen 1.5.5