Debian Codegraph @

src/common/tsort.c

raw symbols SCIP
1 /*
2 * Copyright 2010 Red Hat Inc., Durham, North Carolina.
3 * All Rights Reserved.
4 *
5 * This library is free software; you can redistribute it and/or
6 * modify it under the terms of the GNU Lesser General Public
7 * License as published by the Free Software Foundation; either
8 * version 2.1 of the License, or (at your option) any later version.
9 *
10 * This library is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 * Lesser General Public License for more details.
14 *
15 * You should have received a copy of the GNU Lesser General Public
16 * License along with this library; if not, write to the Free Software
17 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
18 *
19 * Authors:
20 * Lukas Kuklinek <[email protected]>
21 */
22
23
24 // topological sort
25
26 #ifdef HAVE_CONFIG_H
27 #include <config.h>
28 #endif
29
30 #include "tsort.h"
31
32 struct oscap_tsort_context {
33 struct oscap_list *visited;
34 struct oscap_list *cur_stack;
35 struct oscap_list *result;
36 oscap_tsort_edge_func edge_func;
37 oscap_cmp_func cmp_func;
38 void *userdata;
39 };
40
41 static struct oscap_tsort_context *oscap_tsort_context_new(oscap_tsort_edge_func edge_func, oscap_cmp_func cmp_func, void *userdata)
42 {
43 struct oscap_tsort_context *ctx = calloc(1, sizeof(struct oscap_tsort_context));
44 ctx->visited = oscap_list_new();
45 ctx->cur_stack = oscap_list_new();
46 ctx->result = oscap_list_new();
47 ctx->edge_func = edge_func;
48 ctx->cmp_func = cmp_func;
49 ctx->userdata = userdata;
50 return ctx;
51 }
52
53 static void oscap_tsort_context_free(struct oscap_tsort_context *ctx)
54 {
55 if (ctx != NULL) {
56 oscap_list_free(ctx->visited, NULL);
57 oscap_list_free(ctx->cur_stack, NULL);
58 oscap_list_free(ctx->result, NULL);
59 free(ctx);
60 }
61 }
62
63 static bool oscap_tsort_visit(void *node, struct oscap_tsort_context* ctx);
64
65 inline static bool oscap_tsort_visit_all(struct oscap_list *nodelist, struct oscap_tsort_context* ctx)
66 {
67 bool ret = true;
68 struct oscap_iterator *it = oscap_iterator_new(nodelist);
69
70 while (oscap_iterator_has_more(it)) {
71 if (!oscap_tsort_visit(oscap_iterator_next(it), ctx)) {
72 ret = false;
73 break;
74 }
75 }
76
77 oscap_iterator_free(it);
78 return ret;
79 }
80
81 static bool oscap_tsort_visit(void *node, struct oscap_tsort_context* ctx)
82 {
83 // loop detection
84 if (oscap_list_contains(ctx->cur_stack, node, ctx->cmp_func)) return false;
85
86 // skip already visited node
87 if (oscap_list_contains(ctx->visited, node, ctx->cmp_func)) return true;
88
89 // mark as visited & update stack
90 oscap_list_add(ctx->visited, node);
91 oscap_list_push(ctx->cur_stack, node);
92
93 // visit all next nodes (dependencies)
94 struct oscap_list *next = ctx->edge_func(node, ctx->userdata);
95 bool ret = oscap_tsort_visit_all(next, ctx);
96 oscap_list_free(next, NULL);
97
98 // update stack & add node to result
99 if (ret) oscap_list_pop(ctx->cur_stack, NULL);
100 oscap_list_add(ctx->result, node);
101
102 return ret;
103 }
104
105 bool oscap_tsort(struct oscap_list *input, struct oscap_list **output, oscap_tsort_edge_func edge_func, oscap_cmp_func cmp_func, void *userdata)
106 {
107 assert(edge_func != NULL);
108 assert(input != NULL);
109
110 if (output != NULL) *output = NULL;
111 if (cmp_func == NULL) cmp_func = oscap_ptr_cmp;
112
113 struct oscap_tsort_context *ctx = oscap_tsort_context_new(edge_func, cmp_func, userdata);
114 bool ret = oscap_tsort_visit_all(input, ctx);
115
116 if (output != NULL) {
117 if (ret) {
118 // the graph has been topologically sorted, return result
119 *output = ctx->result;
120 ctx->result = NULL;
121 }
122 else {
123 // a loop has been ecountered in the graph, return the loop
124 *output = ctx->cur_stack;
125 ctx->cur_stack = NULL;
126 }
127 }
128
129 oscap_tsort_context_free(ctx);
130
131 return ret;
132 }
133
134
135