diff options
author | David Oberhollenzer <david.oberhollenzer@sigma-star.at> | 2019-06-22 20:02:10 +0200 |
---|---|---|
committer | David Oberhollenzer <david.oberhollenzer@sigma-star.at> | 2019-06-22 20:04:25 +0200 |
commit | 6cbc85c018187a2b28bf0607f52bc258cc273253 (patch) | |
tree | 1081a7da4afef0236e9ef963bf43cfd2f0d02fba /lib/fstree/fstree_sort.c | |
parent | 8edee9c4967ed9f3ce53cdc752cd2c02ca585bfe (diff) |
Cleanup: split fstree sort into 2 fstree independend functions
Make tree node list sort and recursive variant available and independend
of the fstree_t.
This is considered cleaner, since the fstree_t actually isn't needed for
any of this and we can just call the recusvie sort on the root instead,
and we can use the sort implementation directly for things like the
upcoming unit test.
Also this commit splits up the merge/sort implementation into a seperate
split and merge functions to make the code somewhat more readable.
Signed-off-by: David Oberhollenzer <david.oberhollenzer@sigma-star.at>
Diffstat (limited to 'lib/fstree/fstree_sort.c')
-rw-r--r-- | lib/fstree/fstree_sort.c | 60 |
1 files changed, 28 insertions, 32 deletions
diff --git a/lib/fstree/fstree_sort.c b/lib/fstree/fstree_sort.c index 9f07c92..cd01235 100644 --- a/lib/fstree/fstree_sort.c +++ b/lib/fstree/fstree_sort.c @@ -3,32 +3,9 @@ #include <string.h> -static tree_node_t *sort_list(tree_node_t *head) +static tree_node_t *merge(tree_node_t *lhs, tree_node_t *rhs) { - tree_node_t *it, *prev, *lhs, *rhs; - size_t i, count = 0; - - for (it = head; it != NULL; it = it->next) - ++count; - - if (count < 2) - return head; - - prev = NULL; - it = head; - - for (i = 0; i < count / 2; ++i) { - prev = it; - it = it->next; - } - - prev->next = NULL; - - lhs = sort_list(head); - rhs = sort_list(it); - - head = NULL; - prev = NULL; + tree_node_t *head = NULL, *prev = NULL, *it; while (lhs != NULL && rhs != NULL) { if (strcmp(lhs->name, rhs->name) <= 0) { @@ -60,17 +37,36 @@ static tree_node_t *sort_list(tree_node_t *head) return head; } -static void sort_directory(tree_node_t *n) +tree_node_t *tree_node_list_sort(tree_node_t *head) { - n->data.dir->children = sort_list(n->data.dir->children); + tree_node_t *it, *prev; + size_t i, count = 0; - for (n = n->data.dir->children; n != NULL; n = n->next) { - if (S_ISDIR(n->mode)) - sort_directory(n); + for (it = head; it != NULL; it = it->next) + ++count; + + if (count < 2) + return head; + + prev = NULL; + it = head; + + for (i = 0; i < count / 2; ++i) { + prev = it; + it = it->next; } + + prev->next = NULL; + + return merge(tree_node_list_sort(head), tree_node_list_sort(it)); } -void fstree_sort(fstree_t *fs) +void tree_node_sort_recursive(tree_node_t *n) { - sort_directory(fs->root); + n->data.dir->children = tree_node_list_sort(n->data.dir->children); + + for (n = n->data.dir->children; n != NULL; n = n->next) { + if (S_ISDIR(n->mode)) + tree_node_sort_recursive(n); + } } |