| 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 | #include "list.h" |
| 27 | |
| 28 | #ifndef OSCAP_TSORT_H_ |
| 29 | #define OSCAP_TSORT_H_ |
| 30 | |
| 31 | |
| 32 | // returns nodest with an edge from given node |
| 33 | typedef struct oscap_list* (*oscap_tsort_edge_func)(void *node, void *userdata); |
| 34 | |
| 35 | /* |
| 36 | * Topological sort. |
| 37 | * |
| 38 | * Performs a topological sort on a directed graph. |
| 39 | * |
| 40 | * @a edge_func is supposed to return an oscap_list of nodes edges coming from the current node |
| 41 | * (passed to it as an argument) are pointing to. It takes the node as a first argument |
| 42 | * and a pointer passed as @a userdata as a second argument. |
| 43 | * |
| 44 | * The purpose of @a cmp_func is to compare nodes. If it is NULL raw pointer comparison |
| 45 | * is used, which should be good enough in most cases. |
| 46 | * |
| 47 | * Pointer @a output will be set to a list with result. If you want to just check whether |
| 48 | * there is a topological order defined on the graph (i.e. it is an acyclic graph), pass NULL. |
| 49 | * If the function manages to find a topological order of the nodes (returns true), |
| 50 | * it is returned in this variable. Otherwise, it will contain an encountered loop. |
| 51 | * You are responsible to free this list. |
| 52 | * |
| 53 | * @param input set of nodes to sort |
| 54 | * @param output this pointer will be set to the result of the algorithm |
| 55 | * @param edge_func function to return target nodes of outgoing edges from the current one |
| 56 | * @param cmp_func node compasrison function |
| 57 | * @param userdata arbitrary data pointer to be forwarded to the edge_func |
| 58 | * @returns whether the algorithm managed to topologically sort the graph |
| 59 | * @retval true input was sorted, result is in the *output pointer |
| 60 | * @retval false a loop was detected, *output points to the encountered loop |
| 61 | */ |
| 62 | bool oscap_tsort(struct oscap_list *input, struct oscap_list **output, oscap_tsort_edge_func edge_func, oscap_cmp_func cmp_func, void *userdata); |
| 63 | |
| 64 | |
| 65 | #endif // OSCAP_TSORT_H_ |
| 66 |