/* --- Function: int read_netconnections(Graph gr, int netconn[][NR_OF_NETNODES], int nr_of_nodes) --- */ int read_netconnections(Graph gr, int netconn[][NR_OF_NETNODES], int nr_of_nodes) { int i, j, tmp, retval; struct BfsVertexdata_ bfstmp; BfsVertexdata bfs; for (i = 0; i < nr_of_nodes; ++i) { for (j = 0; j < nr_of_nodes; ++j) { if (netconn[i][j] != 0) { bfs = (BfsVertexdata)malloc(sizeof(struct BfsVertexdata_)); MALCHK(bfs); bfs->data = (int *)malloc(sizeof(int)); MALCHK(bfs->data); *(int *)bfs->data = j+1; /* Temporary vertex (search) data.. */ tmp = i+1; bfstmp.data = &tmp; if ((retval = GRAPHinsedge(gr, &bfstmp, bfs)) != OK) { bfs_destroy(bfs); return retval; } } } } /* Everything OK */ return OK; }
/* --- Function: int read_netnodes(Graph gr, int *pnodes, int nr_of_nodes) --- */ int read_netnodes(Graph gr, int *pnodes, int nr_of_nodes) { int i, retval; BfsVertexdata bfs; for (i = 0; i < nr_of_nodes; ++i) { bfs = (BfsVertexdata)malloc(sizeof(struct BfsVertexdata_)); MALCHK(bfs); bfs->data = (int *)malloc(sizeof(int)); MALCHK(bfs->data); /* Copy data to vertex.. */ *((int *)bfs->data) = *(pnodes+i); if ((retval = GRAPHinsvertex(gr, bfs)) != OK) { bfs_destroy(bfs); return retval; } } /* Everything OK */ return OK; }
bfs_t* bfs_init(const linear_hashing_t* hashing, unsigned int source_id) { assert(hashing != NULL); bfs_t* bfs = NULL; bool status = true; /* Check if source node exists */ if(!linear_hashing_contains(hashing, source_id)) status = false; /* Allocate memory for the bfs struct */ if(status && (bfs = malloc(sizeof(struct bfs_struct))) == NULL) status = false; /* Initialize all fields */ if(status) { bfs->queue = NULL; bfs->visited = NULL; bfs->hashing = hashing; } /* Initialize the first set and queue */ if(status) status = bfs_data_structure_init(&bfs->visited, &bfs->queue, source_id); /* On error, free everything */ if(!status) bfs_destroy(&bfs); return bfs; }
/* * Destroys this struct and frees all memory associated with it */ void bfg_destroy(struct befunge_program * bf) { bfs_destroy(&bf->stack); }