/* --------------------------------------------------- purpose -- allocate a int array with size entries and fill with value ival return value -- a pointer to the start of the array created : 95sep22, cca --------------------------------------------------- */ int * IVinit ( int size, int ival ) { int *y = NULL ; if ( size > 0 ) { y = IVinit2(size) ; IVfill(size, y, ival) ; } return(y) ; }
/* ------------------------------ purpose -- to permute a vector y[*] := y[index[*]] created -- 95sep22, cca ------------------------------ */ void IVperm ( int size, int y[], int index[] ) { if ( size > 0 ) { if ( y == NULL || index == NULL ) { fprintf(stderr, "\n fatal error in IVperm, invalid data" "\n size = %d, y = %p, index = %p\n", size, y, index) ; exit(-1) ; } else { int *x ; int i ; x = IVinit2(size) ; IVcopy(size, x, y) ; for ( i = 0 ; i < size ; i++ ) { y[i] = x[index[i]] ; } IVfree(x) ; } } return ; }
/* ---------------------------------------------------- purpose -- to read a Graph object from a binary file return value -- 1 if success, 0 if failure created -- 95sep29, cca ---------------------------------------------------- */ int Graph_readFromBinaryFile ( Graph *graph, FILE *fp ) { int nedges, nvbnd, nvtx, rc, totewght, totvwght, type ; int itemp[6] ; int *vwghts ; IVL *adjIVL, *ewghtIVL ; /* --------------- check the input --------------- */ if ( graph == NULL || fp == NULL ) { fprintf(stderr, "\n fatal error in Graph_readFromBinaryFile(%p,%p)" "\n bad input\n", graph, fp) ; return(0) ; } /* --------------------- clear the data fields --------------------- */ Graph_clearData(graph) ; /* --------------------------------------------- read in the six scalar parameters type, nvtx, nvbnd, nedges, totvwght, totewght --------------------------------------------- */ if ( (rc = fread((void *) itemp, sizeof(int), 6, fp)) != 6 ) { fprintf(stderr, "\n error in Graph_readFromBinaryFile(%p,%p)" "\n %d items of %d read\n", graph, fp, rc, 6) ; return(0) ; } type = itemp[0] ; nvtx = itemp[1] ; nvbnd = itemp[2] ; nedges = itemp[3] ; totvwght = itemp[4] ; totewght = itemp[5] ; /* -------------------------------------------------- create the adjIVL IVL object, set its type to IVL_CHUNKED, then read in its data -------------------------------------------------- */ adjIVL = IVL_new() ; IVL_setDefaultFields(adjIVL) ; adjIVL->type = IVL_CHUNKED ; rc = IVL_readFromBinaryFile(adjIVL, fp) ; if ( rc != 1 ) { fprintf(stderr, "\n error in Graph_readFromBinaryFile(%p,%p)" "\n trying to read in adjIVL" "\n return code %d from IVL_readBinaryFile(%p,%p)", graph, fp, rc, adjIVL, fp) ; return(0) ; } if ( type % 2 == 1 ) { int nvtot, wght ; /* -------------------------- vertex weights are present -------------------------- */ nvtot = nvtx + nvbnd ; vwghts = IVinit2(nvtot) ; if ( (rc = fread((void *) vwghts, sizeof(int), nvtot, fp)) != nvtot){ fprintf(stderr, "\n error in Graph_readFromBinaryFile(%p,%p)" "\n %d items of %d read\n", graph, fp, rc, nvtx+nvbnd) ; return(0) ; } wght = IVsum(nvtot, vwghts) ; if ( wght != totvwght ) { fprintf(stderr, "\n error in Graph_readFromBinaryFile(%p,%p)" "\n totvwght = %d, IVsum(vwghts) = %d\n", graph, fp, totvwght, wght) ; return(0) ; } } else { vwghts = NULL ; } if ( type > 2 ) { int wght ; /* ----------------------------------------------------- edge weights are present, create the ewghtIVL object, set its type to IVL_CHUNKED, then read in its data ----------------------------------------------------- */ ewghtIVL = IVL_new() ; IVL_setDefaultFields(ewghtIVL) ; ewghtIVL->type = IVL_CHUNKED ; rc = IVL_readFromBinaryFile(ewghtIVL, fp) ; if ( rc != 1 ) { fprintf(stderr, "\n error in Graph_readFromBinaryFile(%p,%p)" "\n trying to read in ewghtIVL" "\n return code %d from IVL_readBinaryFile(%p,%p)", graph, fp, rc, ewghtIVL, fp) ; return(0) ; } wght = IVL_sum(ewghtIVL) ; if ( wght != totewght ) { fprintf(stderr, "\n error in Graph_readFromBinaryFile(%p,%p)" "\n totewght = %d, IVL_sum(ewghtIVL) = %d\n", graph, fp, totewght, wght) ; return(0) ; } } else { ewghtIVL = NULL ; } /* --------------------- initialize the object --------------------- */ Graph_init2(graph, type, nvtx, nvbnd, nedges, totvwght, totewght, adjIVL, vwghts, ewghtIVL) ; return(1) ; }