diff options
-rw-r--r-- | tests/test-graph.c | 22 | ||||
-rw-r--r-- | ufo/ufo-graph.c | 63 | ||||
-rw-r--r-- | ufo/ufo-graph.h | 4 |
3 files changed, 81 insertions, 8 deletions
diff --git a/tests/test-graph.c b/tests/test-graph.c index 970801a..ab04e1c 100644 --- a/tests/test-graph.c +++ b/tests/test-graph.c @@ -152,6 +152,20 @@ test_get_num_edges (Fixture *fixture, gconstpointer data) } static void +test_get_num_successors (Fixture *fixture, gconstpointer data) +{ + g_assert (ufo_graph_get_num_successors (fixture->sequence, fixture->root) == 1); + g_assert (ufo_graph_get_num_successors (fixture->diamond, fixture->root) == 2); +} + +static void +test_get_num_predecessors (Fixture *fixture, gconstpointer data) +{ + g_assert (ufo_graph_get_num_predecessors (fixture->sequence, fixture->target1) == 1); + g_assert (ufo_graph_get_num_predecessors (fixture->diamond, fixture->target3) == 2); +} + +static void test_get_edges (Fixture *fixture, gconstpointer data) { GList *edges; @@ -335,10 +349,18 @@ test_add_graph (void) Fixture, NULL, fixture_setup, test_get_successors, fixture_teardown); + g_test_add ("/graph/nodes/successors/num", + Fixture, NULL, + fixture_setup, test_get_num_successors, fixture_teardown); + g_test_add ("/graph/nodes/predecessors", Fixture, NULL, fixture_setup, test_get_predecessors, fixture_teardown); + g_test_add ("/graph/nodes/predecessors/num", + Fixture, NULL, + fixture_setup, test_get_num_predecessors, fixture_teardown); + g_test_add ("/graph/nodes/filtered", Fixture, NULL, fixture_setup, test_get_nodes_filtered, fixture_teardown); diff --git a/ufo/ufo-graph.c b/ufo/ufo-graph.c index 6a8955f..fe38f4d 100644 --- a/ufo/ufo-graph.c +++ b/ufo/ufo-graph.c @@ -344,6 +344,26 @@ ufo_graph_get_leaves (UfoGraph *graph) return ufo_graph_get_nodes_filtered (graph, (UfoFilterPredicate) has_no_successor, graph); } +static GList * +get_target_edges (GList *edges, + UfoNode *target) +{ + UfoEdge match; + + match.target = target; + return g_list_find_all_data (edges, &match, cmp_edge_target); +} + +static GList * +get_source_edges (GList *edges, + UfoNode *source) +{ + UfoEdge match; + + match.source = source; + return g_list_find_all_data (edges, &match, cmp_edge_source); +} + /** * ufo_graph_get_predecessors: * @graph: A #UfoGraph @@ -359,15 +379,12 @@ ufo_graph_get_predecessors (UfoGraph *graph, UfoNode *node) { UfoGraphPrivate *priv; - UfoEdge match; GList *edges; GList *result; g_return_val_if_fail (UFO_IS_GRAPH (graph), NULL); priv = graph->priv; - - match.target = node; - edges = g_list_find_all_data (priv->edges, &match, cmp_edge_target); + edges = get_target_edges (priv->edges, node); result = NULL; for (GList *it = g_list_first (edges); it != NULL; it = g_list_next (it)) { @@ -379,6 +396,22 @@ ufo_graph_get_predecessors (UfoGraph *graph, return result; } +guint +ufo_graph_get_num_predecessors (UfoGraph *graph, + UfoNode *node) +{ + UfoGraphPrivate *priv; + GList *edges; + guint n_predecessors; + + g_return_val_if_fail (UFO_IS_GRAPH (graph), 0); + priv = graph->priv; + edges = get_target_edges (priv->edges, node); + n_predecessors = g_list_length (edges); + g_list_free (edges); + return n_predecessors; +} + /** * ufo_graph_get_successors: * @graph: A #UfoGraph @@ -394,15 +427,12 @@ ufo_graph_get_successors (UfoGraph *graph, UfoNode *node) { UfoGraphPrivate *priv; - UfoEdge match; GList *edges; GList *result; g_return_val_if_fail (UFO_IS_GRAPH (graph), NULL); priv = graph->priv; - - match.source = node; - edges = g_list_find_all_data (priv->edges, &match, cmp_edge_source); + edges = get_source_edges (priv->edges, node); result = NULL; for (GList *it = g_list_first (edges); it != NULL; it = g_list_next (it)) { @@ -414,6 +444,21 @@ ufo_graph_get_successors (UfoGraph *graph, return result; } +guint +ufo_graph_get_num_successors (UfoGraph *graph, + UfoNode *node) +{ + UfoGraphPrivate *priv; + GList *edges; + guint n_successors; + + g_return_val_if_fail (UFO_IS_GRAPH (graph), 0); + priv = graph->priv; + edges = get_source_edges (priv->edges, node); + n_successors = g_list_length (edges); + g_list_free (edges); + return n_successors; +} static void copy_and_connect_successors (UfoGraph *graph, @@ -543,6 +588,8 @@ ufo_graph_expand (UfoGraph *graph, gpointer label; next = UFO_NODE (it->data); + + copy = ufo_node_copy (next, &error); label = ufo_graph_get_edge_label (graph, orig, next); ufo_graph_connect_nodes (graph, current, copy, label); diff --git a/ufo/ufo-graph.h b/ufo/ufo-graph.h index 5ecf32e..e50ddf8 100644 --- a/ufo/ufo-graph.h +++ b/ufo/ufo-graph.h @@ -102,8 +102,12 @@ guint ufo_graph_get_num_edges (UfoGraph *graph); GList *ufo_graph_get_edges (UfoGraph *graph); GList *ufo_graph_get_roots (UfoGraph *graph); GList *ufo_graph_get_leaves (UfoGraph *graph); +guint ufo_graph_get_num_predecessors (UfoGraph *graph, + UfoNode *node); GList *ufo_graph_get_predecessors (UfoGraph *graph, UfoNode *node); +guint ufo_graph_get_num_successors (UfoGraph *graph, + UfoNode *node); GList *ufo_graph_get_successors (UfoGraph *graph, UfoNode *node); GList *ufo_graph_get_paths (UfoGraph *graph, |