VGraph VGraphHemi(VGraph src,VLong hemi) { VString str; VGraph dest=NULL; int i,j,n,ncols,half=0; float x,y,z,val=0; VNode node,node1; int *table; half = 80; /* standard coordinate for inter-hemisperic cleft */ if (VGetAttr (VGraphAttrList (src), "ca", NULL,VStringRepn, (VPointer) & str) == VAttrFound) { sscanf(str,"%f %f %f",&x,&y,&z); ncols = (int) x; half = ncols/2; } table = (int *) VMalloc(sizeof(int) * (src)->lastUsed + 1); for (i=0; i<= src->lastUsed; i++) table[i] = 0; n = (src)->lastUsed/2; dest = VCreateGraph(n,VGraphNFields(src),VNodeRepn(src),src->useWeights); for (i=1; i<=(src)->lastUsed; i++) { node = (VNode) VGraphGetNode (src,i); if (node == 0) continue; VReadNode((src),node,&x,&y,&z,&val); if ((hemi == 0 && x <= half) || (hemi == 1 && x > half)) { n = VGraphAddNode(dest,(VNode) node); table[i] = n; } } for (i=1; i<=(src)->lastUsed; i++) { node = (VNode) VGraphGetNode (src,i); if (node == 0) continue; for (j=1; j<=(src)->lastUsed; j++) { node1 = (VNode) VGraphGetNode (src,j); if (node1 == 0) continue; if (table[i] == 0 || table[j] == 0) continue; if (VGraphIsAdjacent(src,i,j) == TRUE) { VGraphLinkNodes(dest,table[i],table[j]); } } } VFree(table); return dest; }
VGraph VCopyGraph (VGraph src) { VGraph dst; int i; dst = VCreateGraph (src->size, src->nfields, src->node_repn, src->useWeights); /* copy each used node in table */ for (i = 1; i <= src->size; i++) dst->table[i-1] = VCopyNodeDeep(src, VGraphGetNode(src, i)); dst->nnodes = src->nnodes; dst->lastUsed = src->lastUsed; if (VGraphAttrList (dst)) VDestroyAttrList (VGraphAttrList (dst)); if (VGraphAttrList (src)) VGraphAttrList (dst) = VCopyAttrList (VGraphAttrList (src)); return dst; }
VGraph VGraphExtractNodes (VGraph src) { VGraph dst; VAdjacency adj; VNode n; int i, j; /* create a destination graph much like src */ dst = VCreateGraph(src->size, src->nfields, src->node_repn, src->useWeights); /* copy selected nodes from src */ for (i = j = 1; i <= src->lastUsed; i++) { n = VGraphGetNode(src, i); if (n && n->base.hops) dst->table[j] = VCopyNodeShallow(src, n); }; /* set number of nodes used */ dst->nnodes = j-1; dst->lastUsed = j; /* now link nodes in new graph */ for (i = 1; i <= dst->lastUsed; i++) { n = VGraphGetNode(dst, i); if (n == 0) continue; j = VGraphLookupNode(src, n); if (j == 0) continue; n = VGraphGetNode(src, j); for (adj = n->base.head; adj; adj = adj->next) { n = VGraphGetNode(src, adj->id); j = VGraphLookupNode(dst, n); if (j) VGraphLinkNodes(dst, i, j); }; }; if (VGraphAttrList (dst)) VDestroyAttrList (VGraphAttrList (dst)); if (VGraphAttrList (src)) VGraphAttrList (dst) = VCopyAttrList (VGraphAttrList (src)); return dst; }
static VPointer VGraphDecodeMethod (VStringConst name, VBundle b) { VGraph graph; VLong size, nfields, node_repn, useWeights; VAttrList list; VLong idx, nadj; int length; size_t len; VNode n; VPointer p, ptr; VAdjacency adj; #define Extract(name, dict, locn, required) \ VExtractAttr (b->list, name, dict, VLongRepn, & locn, required) /* Extract the required attribute values for Graph. */ if (!Extract (VRepnAttr, VNumericRepnDict, node_repn, TRUE) || !Extract (VNNodeFieldsAttr, NULL, nfields, TRUE) || !Extract (VNNodeWeightsAttr, NULL, useWeights, TRUE)) return NULL; /* Look for size attribute, if not present, look for nnodes (for backward compatibility */ if (Extract (VNGraphSizeAttr, NULL, size, TRUE) == FALSE && Extract (VNGraphNodesAttr, NULL, size, TRUE) == FALSE) return NULL; if (size <= 0 || nfields <= 0) { VWarning ("VGraphReadDataMethod: Bad Graph file attributes"); return NULL; } /* Create the Graph data structure. */ graph = VCreateGraph ((int)size, (int) nfields, (VRepnKind) node_repn, (int) useWeights); if (! graph) return NULL; /* Give it whatever attributes remain: */ list = VGraphAttrList (graph); VGraphAttrList (graph) = b->list; b->list = list; length = b->length; if (length == 0) return graph; p = b->data; #define unpack(repn, cnt, dest) \ ptr = dest; \ if (VUnpackData(repn, cnt, p, VMsbFirst, & len, & ptr, 0) == 0) return 0; \ p = (char *) p + len; length -= len; len = length; \ if (length < 0) goto Fail; len = length; while (length > 0) { /* Get the index : */ unpack(VLongRepn, 1, &idx); graph->table[idx-1] = n = VCalloc(1, VNodeSize(graph)); if (idx > graph->lastUsed) graph->lastUsed = idx; graph->nnodes++; /* Get the number of adjacencies : */ unpack(VLongRepn, 1, &nadj); /* Unpack the adjacencies : */ while (nadj--) { adj = VMalloc(sizeof(VAdjRec)); unpack(VLongRepn, 1, &adj->id); if (graph->useWeights) { unpack(VFloatRepn, 1, &adj->weight); } else adj->weight = 0.0; adj->next = n->base.head; n->base.head = adj; }; /* Unpack the node itself: */ if (graph->useWeights) { unpack(VFloatRepn, 1, &(n->base.weight)); } else n->base.weight = 0.0; unpack(graph->node_repn, graph->nfields, n->data); } return graph; Fail: VWarning ("VGraphDecodeMethod: %s graph has wrong data length", name); VDestroyGraph (graph); return NULL; #undef Extract }