/* ----------------------- simplest constructor created -- 95sep22, cca ----------------------- */ IVL * IVL_new ( void ) { IVL *ivl ; ALLOCATE(ivl, struct _IVL, 1) ; IVL_setDefaultFields(ivl) ; return(ivl) ; }
/* ---------------------------------------------------- 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) ; }
/* -------------------------------------------------- clear the data fields, releasing allocated storage created -- 95sep22, cca -------------------------------------------------- */ void IVL_clearData ( IVL *ivl ) { /* --------------- check the input --------------- */ if ( ivl == NULL ) { fprintf(stderr, "\n fatal error in IVL_clearData(%p)" "\n bad input\n", ivl) ; exit(-1) ; } /* ---------------------------------------------------- switch over the storage type to free list entries. action is taken when type is IVL_SOLO or IVL_CHUNKED ---------------------------------------------------- */ switch ( ivl->type ) { case IVL_SOLO : { int ilist ; for ( ilist = 0 ; ilist < ivl->nlist ; ilist++ ) { if ( ivl->p_vec[ilist] != NULL ) { IVfree(ivl->p_vec[ilist]) ; ivl->p_vec[ilist] = NULL ; ivl->tsize -= ivl->sizes[ilist] ; } } } break ; case IVL_CHUNKED : { Ichunk *chunk ; while ( (chunk = ivl->chunk) != NULL ) { ivl->chunk = chunk->next ; if ( chunk->base != NULL ) { IVfree(chunk->base) ; chunk->base = NULL ; } FREE(chunk) ; } } break ; case IVL_NOTYPE : case IVL_UNKNOWN : break ; default : fprintf(stderr, "\n fatal error in IVL_clearData(%p)" "\n invalid type = %d\n", ivl, ivl->type) ; exit(-1) ; } /* ----------------------------------------------- free storage for the sizes[] and p_vec[] arrays ----------------------------------------------- */ if ( ivl->sizes != NULL ) { IVfree(ivl->sizes) ; ivl->sizes = NULL ; } if ( ivl->p_vec != NULL ) { PIVfree(ivl->p_vec) ; ivl->p_vec = NULL ; } ivl->nlist = ivl->maxnlist = 0 ; /* ---------------------- set the default fields ---------------------- */ IVL_setDefaultFields(ivl) ; return ; }