2014-06-08 10:51:01 +04:00
|
|
|
/* vim: tabstop=4 shiftwidth=4 noexpandtab
|
2014-06-08 10:58:31 +04:00
|
|
|
* This file is part of ToaruOS and is released under the terms
|
2014-06-08 10:13:29 +04:00
|
|
|
* of the NCSA / University of Illinois License - see LICENSE.md
|
|
|
|
* Copyright (C) 2011-2014 Kevin Lange
|
2014-06-08 10:43:21 +04:00
|
|
|
*
|
2011-11-29 12:49:19 +04:00
|
|
|
* General-purpose tree implementation
|
|
|
|
*/
|
2014-03-25 07:18:40 +04:00
|
|
|
|
|
|
|
#include "tree.h"
|
|
|
|
|
|
|
|
#ifdef _KERNEL_
|
|
|
|
# include <system.h>
|
|
|
|
#else
|
|
|
|
# include <stddef.h>
|
|
|
|
# include <stdlib.h>
|
|
|
|
#endif
|
2011-11-29 12:49:19 +04:00
|
|
|
|
2013-06-06 10:10:36 +04:00
|
|
|
tree_t * tree_create(void) {
|
2011-11-29 17:10:35 +04:00
|
|
|
/* Create a new tree */
|
2011-11-29 12:49:19 +04:00
|
|
|
tree_t * out = malloc(sizeof(tree_t));
|
|
|
|
out->nodes = 0;
|
|
|
|
out->root = NULL;
|
|
|
|
return out;
|
|
|
|
}
|
|
|
|
|
|
|
|
void tree_set_root(tree_t * tree, void * value) {
|
2012-03-15 22:15:57 +04:00
|
|
|
/* Set the root node for a new tree. */
|
2011-11-29 12:49:19 +04:00
|
|
|
tree_node_t * root = tree_node_create(value);
|
|
|
|
tree->root = root;
|
|
|
|
tree->nodes = 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
void tree_node_destroy(tree_node_t * node) {
|
2011-11-29 17:10:35 +04:00
|
|
|
/* Free the contents of a node and its children, but not the nodes themselves */
|
2011-11-29 12:49:19 +04:00
|
|
|
foreach(child, node->children) {
|
|
|
|
tree_node_destroy((tree_node_t *)child->value);
|
|
|
|
}
|
|
|
|
free(node->value);
|
|
|
|
}
|
|
|
|
|
|
|
|
void tree_destroy(tree_t * tree) {
|
2011-11-29 17:10:35 +04:00
|
|
|
/* Free the contents of a tree, but not the nodes */
|
2011-11-29 12:49:19 +04:00
|
|
|
if (tree->root) {
|
|
|
|
tree_node_destroy(tree->root);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void tree_node_free(tree_node_t * node) {
|
2011-11-29 17:10:35 +04:00
|
|
|
/* Free a node and its children, but not their contents */
|
2011-11-29 12:49:19 +04:00
|
|
|
if (!node) return;
|
|
|
|
foreach(child, node->children) {
|
|
|
|
tree_node_free(child->value);
|
|
|
|
}
|
|
|
|
free(node);
|
|
|
|
}
|
|
|
|
|
|
|
|
void tree_free(tree_t * tree) {
|
2011-11-29 17:10:35 +04:00
|
|
|
/* Free all of the nodes in a tree, but not their contents */
|
2011-11-29 12:49:19 +04:00
|
|
|
tree_node_free(tree->root);
|
|
|
|
}
|
|
|
|
|
|
|
|
tree_node_t * tree_node_create(void * value) {
|
2011-11-29 17:10:35 +04:00
|
|
|
/* Create a new tree node pointing to the given value */
|
2011-11-29 12:49:19 +04:00
|
|
|
tree_node_t * out = malloc(sizeof(tree_node_t));
|
|
|
|
out->value = value;
|
|
|
|
out->children = list_create();
|
|
|
|
out->parent = NULL;
|
|
|
|
return out;
|
|
|
|
}
|
|
|
|
|
|
|
|
void tree_node_insert_child_node(tree_t * tree, tree_node_t * parent, tree_node_t * node) {
|
2011-11-29 17:10:35 +04:00
|
|
|
/* Insert a node as a child of parent */
|
2011-11-29 12:49:19 +04:00
|
|
|
list_insert(parent->children, node);
|
|
|
|
node->parent = parent;
|
|
|
|
tree->nodes++;
|
|
|
|
}
|
|
|
|
|
|
|
|
tree_node_t * tree_node_insert_child(tree_t * tree, tree_node_t * parent, void * value) {
|
2011-11-29 17:10:35 +04:00
|
|
|
/* Insert a (fresh) node as a child of parent */
|
2011-11-29 12:49:19 +04:00
|
|
|
tree_node_t * out = tree_node_create(value);
|
|
|
|
tree_node_insert_child_node(tree, parent, out);
|
|
|
|
return out;
|
|
|
|
}
|
|
|
|
|
|
|
|
tree_node_t * tree_node_find_parent(tree_node_t * haystack, tree_node_t * needle) {
|
2011-11-29 17:10:35 +04:00
|
|
|
/* Recursive node part of tree_find_parent */
|
2011-11-29 12:49:19 +04:00
|
|
|
tree_node_t * found = NULL;
|
|
|
|
foreach(child, haystack->children) {
|
|
|
|
if (child->value == needle) {
|
|
|
|
return haystack;
|
|
|
|
}
|
|
|
|
found = tree_node_find_parent((tree_node_t *)child->value, needle);
|
|
|
|
if (found) {
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return found;
|
|
|
|
}
|
|
|
|
|
|
|
|
tree_node_t * tree_find_parent(tree_t * tree, tree_node_t * node) {
|
2011-11-29 17:10:35 +04:00
|
|
|
/* Return the parent of a node, inefficiently. */
|
2011-11-29 12:49:19 +04:00
|
|
|
if (!tree->root) return NULL;
|
|
|
|
return tree_node_find_parent(tree->root, node);
|
|
|
|
}
|
|
|
|
|
2011-11-29 16:52:26 +04:00
|
|
|
size_t tree_count_children(tree_node_t * node) {
|
2011-11-29 17:10:35 +04:00
|
|
|
/* return the number of children this node has */
|
2011-11-29 16:52:26 +04:00
|
|
|
if (!node) return 0;
|
2011-11-29 17:01:36 +04:00
|
|
|
if (!node->children) return 0;
|
2011-11-29 16:52:26 +04:00
|
|
|
size_t out = node->children->length;
|
|
|
|
foreach(child, node->children) {
|
2011-11-29 17:01:36 +04:00
|
|
|
out += tree_count_children((tree_node_t *)child->value);
|
2011-11-29 16:52:26 +04:00
|
|
|
}
|
|
|
|
return out;
|
|
|
|
}
|
|
|
|
|
2011-11-29 12:49:19 +04:00
|
|
|
void tree_node_parent_remove(tree_t * tree, tree_node_t * parent, tree_node_t * node) {
|
2011-11-29 17:10:35 +04:00
|
|
|
/* remove a node when we know its parent; update node counts for the tree */
|
2011-11-29 16:52:26 +04:00
|
|
|
tree->nodes -= tree_count_children(node) + 1;
|
2011-11-29 12:49:19 +04:00
|
|
|
list_delete(parent->children, list_find(parent->children, node));
|
|
|
|
tree_node_free(node);
|
|
|
|
}
|
|
|
|
|
|
|
|
void tree_node_remove(tree_t * tree, tree_node_t * node) {
|
2011-11-29 17:10:35 +04:00
|
|
|
/* remove an entire branch given its root */
|
2011-11-29 12:49:19 +04:00
|
|
|
tree_node_t * parent = node->parent;
|
2011-11-29 17:10:35 +04:00
|
|
|
if (!parent) {
|
|
|
|
if (node == tree->root) {
|
|
|
|
tree->nodes = 0;
|
|
|
|
tree->root = NULL;
|
|
|
|
tree_node_free(node);
|
|
|
|
}
|
|
|
|
}
|
2011-11-29 12:49:19 +04:00
|
|
|
tree_node_parent_remove(tree, parent, node);
|
|
|
|
}
|
2011-11-29 16:52:26 +04:00
|
|
|
|
|
|
|
void tree_remove(tree_t * tree, tree_node_t * node) {
|
2011-11-29 17:10:35 +04:00
|
|
|
/* Remove this node and move its children into its parent's list of children */
|
2011-11-29 16:52:26 +04:00
|
|
|
tree_node_t * parent = node->parent;
|
2011-11-29 17:10:35 +04:00
|
|
|
/* This is something we just can't do. We don't know how to merge our
|
|
|
|
* children into our "parent" because then we'd have more than one root node.
|
|
|
|
* A good way to think about this is actually what this tree struct
|
|
|
|
* primarily exists for: processes. Trying to remove the root is equivalent
|
|
|
|
* to trying to kill init! Which is bad. We immediately fault on such
|
|
|
|
* a case anyway ("Tried to kill init, shutting down!").
|
|
|
|
*/
|
2011-11-29 16:52:26 +04:00
|
|
|
if (!parent) return;
|
|
|
|
tree->nodes--;
|
|
|
|
list_delete(parent->children, list_find(parent->children, node));
|
2011-12-16 07:08:48 +04:00
|
|
|
foreach(child, node->children) {
|
|
|
|
/* Reassign the parents */
|
|
|
|
((tree_node_t *)child->value)->parent = parent;
|
|
|
|
}
|
2011-11-29 16:52:26 +04:00
|
|
|
list_merge(parent->children, node->children);
|
|
|
|
free(node);
|
|
|
|
}
|
2011-12-08 06:58:47 +04:00
|
|
|
|
2014-04-27 12:37:33 +04:00
|
|
|
void tree_remove_reparent_root(tree_t * tree, tree_node_t * node) {
|
|
|
|
/* Remove this node and move its children into the root children */
|
|
|
|
tree_node_t * parent = node->parent;
|
|
|
|
if (!parent) return;
|
|
|
|
tree->nodes--;
|
|
|
|
list_delete(parent->children, list_find(parent->children, node));
|
|
|
|
foreach(child, node->children) {
|
|
|
|
/* Reassign the parents */
|
|
|
|
((tree_node_t *)child->value)->parent = tree->root;
|
|
|
|
}
|
|
|
|
list_merge(tree->root->children, node->children);
|
|
|
|
free(node);
|
|
|
|
}
|
|
|
|
|
2011-12-08 06:58:47 +04:00
|
|
|
void tree_break_off(tree_t * tree, tree_node_t * node) {
|
|
|
|
tree_node_t * parent = node->parent;
|
|
|
|
if (!parent) return;
|
|
|
|
list_delete(parent->children, list_find(parent->children, node));
|
|
|
|
}
|
|
|
|
|
|
|
|
tree_node_t * tree_node_find(tree_node_t * node, void * search, tree_comparator_t comparator) {
|
|
|
|
if (comparator(node->value,search)) {
|
|
|
|
return node;
|
|
|
|
}
|
|
|
|
tree_node_t * found;
|
|
|
|
foreach(child, node->children) {
|
|
|
|
found = tree_node_find((tree_node_t *)child->value, search, comparator);
|
|
|
|
if (found) return found;
|
|
|
|
}
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
tree_node_t * tree_find(tree_t * tree, void * value, tree_comparator_t comparator) {
|
|
|
|
return tree_node_find(tree->root, value, comparator);
|
|
|
|
}
|