/** * Add a node to carto graph. * * @param graph the carto graph to add the node to. * @param node the node to add. */ void opal_carto_base_graph_add_node_fn(opal_carto_graph_t *graph, opal_carto_base_node_t *node) { /* construct new vertex */ node->vertex = OBJ_NEW(opal_graph_vertex_t); /* assign the node as the vertex data */ node->vertex->vertex_data = (void *)node; /* assign the vertex function pointers */ node->vertex->free_vertex_data = opal_carto_base_free_node; node->vertex->copy_vertex_data = opal_carto_base_copy_nodes; node->vertex->alloc_vertex_data = opal_carto_base_alloc_node; node->vertex->compare_vertex = opal_carto_compare_nodes; node->vertex->print_vertex = opal_carto_print_node; /* add the new node to the carto graph by adding the nodes vertex to the graph */ opal_graph_add_vertex((opal_graph_t *)graph, node->vertex); }
/** * This graph API duplicates a graph. Note that this API does * not copy the graph but builds a new graph while coping just * the vertex data. * * @param dest The new created graph. * @param src The graph we want to duplicate. */ void opal_graph_duplicate(opal_graph_t **dest, opal_graph_t *src) { opal_adjacency_list_t *aj_list; opal_list_item_t *aj_list_item, *edg_item; opal_graph_vertex_t *vertex; opal_graph_edge_t *edge, *new_edge; /* construct a new graph */ *dest = OBJ_NEW(opal_graph_t); /* Run on all the vertices of the src graph */ for (aj_list_item = opal_list_get_first(src->adjacency_list); aj_list_item != opal_list_get_end(src->adjacency_list); aj_list_item = opal_list_get_next(aj_list_item)) { aj_list = (opal_adjacency_list_t *) aj_list_item; /* for each vertex in the src graph, construct a new vertex */ vertex = OBJ_NEW(opal_graph_vertex_t); /* associate the new vertex to a vertex from the original graph */ vertex->sibling = aj_list->vertex; /* associate the original vertex to the new constructed vertex */ aj_list->vertex->sibling = vertex; /* allocate space to vertex data in the new vertex */ if (NULL != aj_list->vertex->alloc_vertex_data) { vertex->vertex_data = aj_list->vertex->alloc_vertex_data(); vertex->alloc_vertex_data = aj_list->vertex->alloc_vertex_data; } /* copy the vertex data from the original vertex to the new vertex */ if (NULL != aj_list->vertex->copy_vertex_data) { aj_list->vertex->copy_vertex_data(&(vertex->vertex_data), aj_list->vertex->vertex_data); vertex->copy_vertex_data = aj_list->vertex->copy_vertex_data; } /* copy all the fields of the original vertex to the new vertex. */ vertex->free_vertex_data = aj_list->vertex->free_vertex_data; vertex->print_vertex = aj_list->vertex->print_vertex; vertex->compare_vertex = aj_list->vertex->compare_vertex; vertex->in_graph = *dest; /* add the new vertex to the new graph */ opal_graph_add_vertex(*dest, vertex); } /** * Now, copy all the edges from the source graph */ /* Run on all the adjscency lists in the graph */ for (aj_list_item = opal_list_get_first(src->adjacency_list); aj_list_item != opal_list_get_end(src->adjacency_list); aj_list_item = opal_list_get_next(aj_list_item)) { aj_list = (opal_adjacency_list_t *) aj_list_item; /* for all the edges in the adjscency list */ for (edg_item = opal_list_get_first(aj_list->edges); edg_item != opal_list_get_end(aj_list->edges); edg_item = opal_list_get_next(edg_item)) { edge = (opal_graph_edge_t *)edg_item; /* construct new edge for the new graph */ new_edge = OBJ_NEW(opal_graph_edge_t); /* copy the edge weight from the original edge */ new_edge->weight = edge->weight; /* connect the new edge according to start and end associations to the vertices of the src graph */ new_edge->start = edge->start->sibling; new_edge->end = edge->end->sibling; /* add the new edge to the new graph */ opal_graph_add_edge(*dest, new_edge); } } }