1
0
Fork 0
mirror of https://github.com/pygos/pkg-utils.git synced 2024-11-25 14:00:43 +01:00

Cleanup: split sort_by_dependencies function up

Signed-off-by: David Oberhollenzer <goliath@infraroot.at>
This commit is contained in:
David Oberhollenzer 2019-02-21 15:04:27 +01:00
parent f2627b8933
commit 98b655f133

View file

@ -1,48 +1,15 @@
#include "install.h"
int sort_by_dependencies(struct pkg_dep_list *list)
static void remove_dependency(struct pkg_dep_list *list,
struct pkg_dep_node *pkg)
{
struct pkg_dep_node *it, *prev, *pkg;
struct pkg_dep_list result;
struct pkg_dep_node *it;
size_t i;
result.head = NULL;
result.tail = NULL;
while (list->head != NULL) {
/* find node with no outgoing edges */
prev = NULL;
it = list->head;
while (it != NULL && it->num_deps != 0) {
prev = it;
it = it->next;
}
if (it == NULL) {
fputs("cycle detected in dependency graph\n", stderr);
pkg_list_cleanup(&result);
return -1;
}
/* remove from graph */
if (prev == NULL) {
list->head = it->next;
} else {
prev->next = it->next;
}
if (it == list->tail)
list->tail = prev;
/* remove edges pointing to the package */
pkg = it;
for (it = list->head; it != NULL; it = it->next) {
for (i = 0; i < it->num_deps; ++i) {
if (it->deps[i] == pkg) {
it->deps[i] =
it->deps[it->num_deps - 1];
it->deps[i] = it->deps[it->num_deps - 1];
it->num_deps -= 1;
--i;
}
@ -53,17 +20,59 @@ int sort_by_dependencies(struct pkg_dep_list *list)
it->deps = NULL;
}
}
}
/* append to list */
if (result.tail == NULL) {
result.head = result.tail = pkg;
static void append(struct pkg_dep_list *out, struct pkg_dep_node *pkg)
{
if (out->tail == NULL) {
out->head = out->tail = pkg;
} else {
result.tail->next = pkg;
result.tail = pkg;
out->tail->next = pkg;
out->tail = pkg;
}
}
list->head = result.head;
list->tail = result.tail;
static struct pkg_dep_node *remove_no_deps(struct pkg_dep_list *list)
{
struct pkg_dep_node *it = list->head, *prev = NULL;
while (it != NULL && it->num_deps != 0) {
prev = it;
it = it->next;
}
if (it != NULL) {
if (prev == NULL) {
list->head = it->next;
} else {
prev->next = it->next;
}
if (it == list->tail)
list->tail = prev;
}
return it;
}
int sort_by_dependencies(struct pkg_dep_list *list)
{
struct pkg_dep_list result = { NULL, NULL };
struct pkg_dep_node *pkg;
while (list->head != NULL) {
pkg = remove_no_deps(list);
if (pkg == NULL) {
fputs("cycle detected in dependency graph\n", stderr);
pkg_list_cleanup(&result);
return -1;
}
remove_dependency(list, pkg);
append(&result, pkg);
}
*list = result;
return 0;
}