void graph_remove_vertex(graph *g, int vertex) { int i; if(g == NULL) { return; } /* is vertex in scope? */ if(vertex >= g->max_verticies) { return; } if(g->verticies[vertex] == NULL) { return; } for(i = 0; i < g->max_verticies; i++) { /* remove the outgoing, then the incoming */ graph_remove_edge(g, row_col_to_1d(vertex, i, g->max_verticies)); graph_remove_edge(g, row_col_to_1d(i, vertex, g->max_verticies)); } /* did we manage vertex memory? */ if(g->freevertex_fn != NULL) { /* free the vertex itself */ g->freevertex_fn(graph_get_vertex(g, vertex)); } g->verticies[vertex] = NULL; g->nverticies--; }
void check_node(avl_tree_node_t *atn, multigrid_t *mg, multigrid_graph_t *mg_g) { struct T *t = atn->data; avl_tree_node_t *atn_from, *atn_to; graph_vertex_idx_t from, to; atn_from = avl_tree_get( &mg_g->grid_to_graph_vertex, (avl_tree_key_t)multigrid_get_grid(mg, t->from) ); atn_to = avl_tree_get( &mg_g->grid_to_graph_vertex, (avl_tree_key_t)multigrid_get_grid(mg, t->to) ); from = *(graph_vertex_idx_t *)atn_from->data; to = *(graph_vertex_idx_t *)atn_to->data; graph_edge_found_t gef = graph_remove_edge(&mg_g->graph, from, to); ck_assert_int_eq(gef.found, true); free(gef.data); if (atn->left) check_node(atn->left, mg, mg_g); if (atn->right) check_node(atn->right, mg, mg_g); }
int graph_add_edge(graph *g, void *edge_data, int vertex_from, int vertex_to) { int edge_index; void *edge_data_copy; if(g == NULL) { return -1; } /* check to make sure there is a vertex at vertex_from, vertex_to */ if((graph_get_vertex(g, vertex_from) == NULL) || (graph_get_vertex(g, vertex_to) == NULL)) { return -1; } edge_index = row_col_to_1d(vertex_from, vertex_to, g->max_verticies); if(g->dupedge_fn != NULL) { edge_data_copy = g->dupedge_fn(edge_data); } else { edge_data_copy = edge_data; } if(g->edges[edge_index] != NULL) { /* if there was an edge there, remove it */ graph_remove_edge(g, edge_index); } g->edges[edge_index] = edge_data_copy; g->nedges++; return edge_index; }
int graph_remove_vertex_deep(struct graph *g, struct vertex *v) { struct vertex *t; for (t = g->head; t != NULL; t = t->next) { graph_remove_edge(t, v); } return graph_remove_vertex(g, v); }
void remove_edge(char *s1, char *s2) { int i, v1=-1, v2=-1; for(i=0; i<ARRAY_SIZE; i++) { if(!vertex[i]) continue; if(!strcmp(s2, vertex[i])) v1=i; if(!strcmp(s1, vertex[i])) v2=i; } if(!(v1==-1||v2==-1)) { graph_remove_edge(v1, v2, DIRECTION_SINGLE); } }
gboolean graph_try_remove_edge (Graph *self, GraphNode *from_node, GraphNode *to_node) { guint ii; for ( ii = 0 ; ii < from_node->out_edges->len ; ii++ ) { GraphEdge *current_edge = g_ptr_array_index (from_node->out_edges, ii); if ( current_edge->to_node == to_node ) { graph_remove_edge (self, current_edge); return TRUE; } } return FALSE; }
// Free self, regardless of reference count. void graph_free (Graph *self) { // Remove all the edges. gint ii; for ( ii = 0 ; ii < self->edges->len ; ii++ ) { graph_remove_edge (self, g_ptr_array_index (self->edges, ii)); } // Remove all the nodes. for ( ii = 0 ; ii < self->nodes->len ; ii++ ) { graph_remove_node (self, g_ptr_array_index (self->nodes, ii)); } g_free (self); }
} END_TEST START_TEST(test_graph_remove_edge) { kld_graph_t * g = (kld_graph_t *) new_graph(); int data = 1; int * data_ref = &data; kld_graph_node_t * n1 = (kld_graph_node_t *) new_graph_node(g); kld_graph_node_t * n2 = (kld_graph_node_t *) new_graph_node(g); graph_insert_edge(g, n1, n2, data_ref); graph_remove_edge(g, n1, n2); kld_graph_edge_t * e = (kld_graph_edge_t *) graph_get_edge(g, n1, n2); fail_if(e != NULL, "Edge should be NULL after inserting and then removing it."); } END_TEST
int main(int argc, char** argv) { graph_p g = graph_init(8); edge_t e; e.v = 1; e.w = 3; printf("inserting edge\n"); graph_insert_edge(g, e); graph_print(g); /* edge_t edges[8 * 7 / 2]; printf("edges %d\n", graph_edges(g, edges)); */ printf("removing edge\n"); graph_remove_edge(g, e); graph_print(g); graph_destroy(g); return EXIT_SUCCESS; }
int rdis_user_function (struct _rdis * rdis, uint64_t address) { // get a tree of all functions reachable at this address struct _map * functions = loader_function_address(rdis->loader, rdis->memory, address); // add in this address as a new function as well struct _function * function = function_create(address); map_insert(functions, function->address, function); object_delete(function); // for each newly reachable function struct _map_it * mit; for (mit = map_iterator(functions); mit != NULL; mit = map_it_next(mit)) { struct _function * function = map_it_data(mit); uint64_t fitaddress = function->address; // if we already have this function, skip it if (map_fetch(rdis->functions, function->address) != NULL) continue; // add this function to the rdis->function_tree map_insert(rdis->functions, function->address, function); // add label struct _label * label = loader_label_address(rdis->loader, rdis->memory, fitaddress); map_insert(rdis->labels, fitaddress, label); object_delete(label); // if this function is already in our graph, all we need to do is make // sure its a separate node and then remove function predecessors struct _graph_node * node = graph_fetch_node_max(rdis->graph, fitaddress); if (node != NULL) { // already a node, remove function predecessors if (node->index == address) { remove_function_predecessors(rdis->graph, rdis->functions); continue; } // search for instruction with given address struct _list * ins_list = node->data; struct _list_it * it; for (it = list_iterator(ins_list); it != NULL; it = it->next) { struct _ins * ins = it->data; // found instruction if (ins->address == fitaddress) { // create a new instruction list. // add remaining instructions to new list while removing them from // the current list struct _list * new_ins_list = list_create(); while (1) { list_append(new_ins_list, it->data); it = list_remove(ins_list, it); if (it == NULL) break; } // create a new graph node for this new function graph_add_node(rdis->graph, fitaddress, new_ins_list); // all graph successors from old node are added to new node struct _queue * queue = queue_create(); struct _list * successors = graph_node_successors(node); struct _list_it * sit; for (sit = list_iterator(successors); sit != NULL; sit = sit->next) { struct _graph_edge * edge = sit->data; graph_add_edge(rdis->graph, fitaddress, edge->tail, edge->data); queue_push(queue, edge); } object_delete(successors); // and removed from old node while (queue->size > 0) { struct _graph_edge * edge = queue_peek(queue); graph_remove_edge(rdis->graph, edge->head, edge->tail); queue_pop(queue); } object_delete(queue); // that was easy break; } } if (it != NULL) continue; } // we need to create a new graph for this node struct _graph * graph = loader_graph_address(rdis->loader, rdis->memory, fitaddress); graph_merge(rdis->graph, graph); object_delete(graph); } object_delete(functions); rdis_callback(rdis, RDIS_CALLBACK_ALL); return 0; }
static void handle_graph_message(UiConnection *self, const gchar *command, JsonObject *payload, SoupWebsocketConnection *ws) { g_return_if_fail(payload); Graph *graph = NULL; if (g_strcmp0(command, "clear") != 0) { // All other commands must have graph // TODO: change FBP protocol to use 'graph' instead of 'id'? const gchar *graph_id = json_object_get_string_member(payload, "graph"); Network *net = (graph_id) ? g_hash_table_lookup(self->network_map, graph_id) : NULL; graph = (net) ? net->graph : NULL; g_return_if_fail(graph); } if (g_strcmp0(command, "clear") == 0) { const gchar *graph_id = json_object_get_string_member(payload, "id"); Graph *graph = graph_new(graph_id, self->component_lib); Network *network = network_new(graph); ui_connection_add_network(self, graph_id, network); } else if (g_strcmp0(command, "addnode") == 0) { graph_add_node(graph, json_object_get_string_member(payload, "id"), json_object_get_string_member(payload, "component") ); } else if (g_strcmp0(command, "removenode") == 0) { graph_remove_node(graph, json_object_get_string_member(payload, "id") ); } else if (g_strcmp0(command, "changenode") == 0) { // Just metadata, ignored } else if (g_strcmp0(command, "addinitial") == 0) { JsonObject *tgt = json_object_get_object_member(payload, "tgt"); JsonObject *src = json_object_get_object_member(payload, "src"); GValue data = G_VALUE_INIT; json_node_get_value(json_object_get_member(src, "data"), &data); graph_add_iip(graph, json_object_get_string_member(tgt, "node"), json_object_get_string_member(tgt, "port"), &data ); g_value_unset(&data); } else if (g_strcmp0(command, "removeinitial") == 0) { JsonObject *tgt = json_object_get_object_member(payload, "tgt"); graph_remove_iip(graph, json_object_get_string_member(tgt, "node"), json_object_get_string_member(tgt, "port") ); } else if (g_strcmp0(command, "addedge") == 0) { JsonObject *src = json_object_get_object_member(payload, "src"); JsonObject *tgt = json_object_get_object_member(payload, "tgt"); graph_add_edge(graph, json_object_get_string_member(src, "node"), json_object_get_string_member(src, "port"), json_object_get_string_member(tgt, "node"), json_object_get_string_member(tgt, "port") ); } else if (g_strcmp0(command, "removeedge") == 0) { JsonObject *src = json_object_get_object_member(payload, "src"); JsonObject *tgt = json_object_get_object_member(payload, "tgt"); graph_remove_edge(graph, json_object_get_string_member(src, "node"), json_object_get_string_member(src, "port"), json_object_get_string_member(tgt, "node"), json_object_get_string_member(tgt, "port") ); } else if (g_strcmp0(command, "changeedge") == 0) { // Just metadata, ignored } else { imgflo_warning("Unhandled message on protocol 'graph', command='%s'", command); } }
int main(){ setup(); for(int i=0; i<10; i++){ graph_add_vertex(gp, u[i]); } graph_add_edge(gp, u[0], u[1]); graph_add_edge(gp, u[0], u[2]); graph_add_edge(gp, u[0], u[3]); graph_add_edge(gp, u[0], u[4]); graph_add_edge(gp, u[1], u[5]); graph_add_edge(gp, u[1], u[8]); graph_add_edge(gp, u[1], u[2]); graph_add_edge(gp, u[2], u[5]); graph_add_edge(gp, u[2], u[7]); graph_add_edge(gp, u[3], u[1]); graph_add_edge(gp, u[3], u[6]); graph_add_edge(gp, u[3], u[7]); graph_add_edge(gp, u[4], u[5]); graph_add_edge(gp, u[5], u[6]); graph_add_edge(gp, u[6], u[7]); graph_add_edge(gp, u[7], u[8]); graph_add_edge(gp, u[8], u[9]); graph_add_edge(gp, u[9], u[0]); graph_dump(gp, print); puts("Drop 0-item"); graph_drop_vertex(gp, u[0]); graph_dump(gp, print); puts(""); puts("Remove 9-item(will failure, because 8-item adjacent to 9-item)"); graph_remove_vertex(gp, u[9]); graph_dump(gp, print); puts(""); puts("Remove edge from 8-item to 9-item"); graph_remove_edge(gp, u[8], u[9]); graph_dump(gp, print); puts(""); puts("Remove 9-item(will success)"); graph_remove_vertex(gp, u[9]); graph_dump(gp, print); puts(""); puts("Remove edge from 1-item to 5-item"); graph_remove_edge(gp, u[1], u[5]); graph_dump(gp, print); puts(""); puts("Add edge from 1-item to 5-item"); graph_add_edge(gp, u[1], u[5]); graph_dump(gp, print); puts(""); graph_dump(gp, print); enddown(); return 0; }