BarryServer : Git

All the code for all my projects
// BarryServer : Git / libBLOC / commit / b3106d3ca5d418eb70a698902e557178416aba18 / tree

// Related

libBLOC

Barry Initial commit b3106d3 (2 years, 11 months ago)
diff --git a/tree/tree.c b/tree/tree.c
new file mode 100644
index 0000000..1bffd72
--- /dev/null
+++ b/tree/tree.c
@@ -0,0 +1,115 @@
+/*
+ * Copyright (C) 2023 Barry
+ *
+ * This file is part of Barry's Little Objects in C Library (libBLOC).
+ *
+ * libBLOC is free software: you can redistribute it and/or modify it under the
+ * terms of the GNU General Public License as published by the Free Software
+ * Foundation, either version 3 of the License, or (at your option) any later
+ * version.
+ *
+ * libBLOC is distributed in the hope that it will be useful, but WITHOUT ANY
+ * WARRANTY; without even the implied warranty of MERCHANTIBILITY or FITNESS
+ * FOR A PARTICULAR PURPOSE.  See the GNU General Public License for more
+ * details.
+ *
+ * You should have received a copy of the GNU General Public License along with
+ * libBLOC.  If not, see <https://www.gnu.org/licenses/>.
+ */
+
+/*
+ * This file implements object Trees.  Object Trees work similarly to lists
+ * except trees store ordered data and are organised such that inserting,
+ * removing, and searching are more efficient.  Trees can also be created with
+ * a specific Object Type, and will only accept objects of that type.  If no
+ * type is specified, the tree can accept any object.
+ */
+
+#include <BLOC/object.h>
+#include <BLOC/tree.h>
+#include "../assert.h"
+#include "../object.h"
+#include "tree.h"
+
+static void _tree_new(void *);
+static void _tree_delete(void *);
+
+/* Tree object type */
+struct ObjectType treeType = {
+	.name = "Tree",
+	.size = sizeof(Tree),
+	.new = _tree_new,
+	.delete = _tree_delete,
+};
+
+/* Create a Tree object */
+static void
+_tree_new(void *obj)
+{
+	Tree *tree = obj;
+	iterable_init(&tree->header);
+}
+
+/* Destroy a Tree object */
+static void
+_tree_delete(void *obj)
+{
+	Tree *tree = obj;
+	struct TreeNode *node, *next;
+	for (node = (void *) tree->header.start; node; node = next) {
+		next = (struct TreeNode *) node->header.next;
+		obj_put(node->header.obj);
+		free(node);
+	}
+}
+
+/* Count the number of nodes in a (sub-)tree recursively */
+unsigned int
+_tree_count(struct TreeNode *root)
+{
+	if (!root)
+		return 0;
+	unsigned int n = 0;
+	n += _tree_count(root->left);
+	n += _tree_count(root->right);
+	return n + 1;
+}
+
+/* Rotate an entire tree left */
+struct TreeNode *
+_tree_rotate_left(struct TreeNode *root)
+{
+	struct TreeNode *tmp = root->right;
+	root->right = tmp->left;
+	tmp->left = root;
+	return tmp;
+}
+
+/* Rotate an entire tree right */
+struct TreeNode *
+_tree_rotate_right(struct TreeNode *root)
+{
+	struct TreeNode *tmp = root->left;
+	root->left = tmp->right;
+	tmp->right = root;
+	return tmp;
+}
+
+/* Create an object tree */
+Tree *
+tree_create(struct ObjectType *type, int (*compare)(void *, void *))
+{
+	if (!compare)
+		return NULL;
+	Tree *tree = obj_new(&treeType);
+	tree->type = type;
+	tree->compare = compare;
+	return tree;
+}
+
+/* Count the nodes in a tree */
+unsigned int
+tree_count(Tree *tree)
+{
+	return tree->header.entries;
+}
diff --git a/tree/tree.h b/tree/tree.h
new file mode 100644
index 0000000..65daaa1
--- /dev/null
+++ b/tree/tree.h
@@ -0,0 +1,43 @@
+/*
+ * Copyright (C) 2023 Barry
+ *
+ * This file is part of Barry's Little Objects in C Library (libBLOC).
+ *
+ * libBLOC is free software: you can redistribute it and/or modify it under the
+ * terms of the GNU General Public License as published by the Free Software
+ * Foundation, either version 3 of the License, or (at your option) any later
+ * version.
+ *
+ * libBLOC is distributed in the hope that it will be useful, but WITHOUT ANY
+ * WARRANTY; without even the implied warranty of MERCHANTIBILITY or FITNESS
+ * FOR A PARTICULAR PURPOSE.  See the GNU General Public License for more
+ * details.
+ *
+ * You should have received a copy of the GNU General Public License along with
+ * libBLOC.  If not, see <https://www.gnu.org/licenses/>.
+ */
+
+#ifndef TREE_H
+#define TREE_H
+
+#include <BLOC/iterator.h>
+
+/* Structure for a Tree Node */
+struct TreeNode {
+	struct IterableEntry header;
+	struct TreeNode *left, *right;
+};
+
+/* Structure for an object Tree */
+struct Tree {
+	struct Iterable header;
+	struct TreeNode *root;
+	struct ObjectType *type;
+	int (*compare)(void *, void *);
+};
+
+unsigned int _tree_count(struct TreeNode *root);
+struct TreeNode *_tree_rotate_left(struct TreeNode *root);
+struct TreeNode *_tree_rotate_right(struct TreeNode *root);
+
+#endif
diff --git a/tree/tree_add.c b/tree/tree_add.c
new file mode 100644
index 0000000..67397ba
--- /dev/null
+++ b/tree/tree_add.c
@@ -0,0 +1,95 @@
+/*
+ * Copyright (C) 2023 Barry
+ *
+ * This file is part of Barry's Little Objects in C Library (libBLOC).
+ *
+ * libBLOC is free software: you can redistribute it and/or modify it under the
+ * terms of the GNU General Public License as published by the Free Software
+ * Foundation, either version 3 of the License, or (at your option) any later
+ * version.
+ *
+ * libBLOC is distributed in the hope that it will be useful, but WITHOUT ANY
+ * WARRANTY; without even the implied warranty of MERCHANTIBILITY or FITNESS
+ * FOR A PARTICULAR PURPOSE.  See the GNU General Public License for more
+ * details.
+ *
+ * You should have received a copy of the GNU General Public License along with
+ * libBLOC.  If not, see <https://www.gnu.org/licenses/>.
+ */
+
+#include <BLOC/object.h>
+#include <BLOC/tree.h>
+#include "../assert.h"
+#include "../object.h"
+#include "tree.h"
+
+/* Add a node to a tree recursively */
+static struct TreeNode *
+_tree_add_r(Tree *t, struct TreeNode *parent, struct TreeNode *child)
+{
+	/* Add to correct side */
+	if (t->compare(parent->header.obj, child->header.obj) > 0) {
+		if (!parent->left) {
+			parent->left = child;
+
+			child->header.prev = parent->header.prev;
+			child->header.next = &parent->header;
+			parent->header.prev = &child->header;
+			if (child->header.prev)
+				child->header.prev->next = &child->header;
+			else
+				t->header.start = &child->header;
+		} else {
+			parent->left = _tree_add_r(t, parent->left, child);
+		}
+	} else {
+		if (!parent->right) {
+			parent->right = child;
+
+			child->header.next = parent->header.next;
+			child->header.prev = &parent->header;
+			parent->header.next = &child->header;
+			if (child->header.next)
+				child->header.next->prev = &child->header;
+			else
+				t->header.end = &child->header;
+		} else {
+			parent->right = _tree_add_r(t, parent->right, child);
+		}
+	}
+
+	/* Balance */
+	if (_tree_count(parent->right) > _tree_count(parent->left) + 1)
+		return _tree_rotate_left(parent);
+	if (_tree_count(parent->left) > _tree_count(parent->right) + 1)
+		return _tree_rotate_right(parent);
+	return parent;
+}
+
+/* Add an object to a tree */
+void
+tree_add(Tree *tree, void *obj)
+{
+	ASSERT(tree);
+	ASSERT(obj);
+	struct ObjectHeader *header = object_header(obj);
+	ASSERT(header->magic == OBJECT_MAGIC);
+	if (tree->type && tree->type != header->type)
+		return;
+
+	struct TreeNode *node;
+	node = malloc(sizeof(struct TreeNode));
+	node->left = node->right = NULL;
+	node->header.prev = node->header.next = NULL;
+	node->header.obj = obj_get(obj);
+
+	obj_lock(tree);
+	tree->header.entries++;
+	if (!tree->root) {
+		tree->root = node;
+		tree->header.start = tree->header.end = &node->header;
+	} else {
+		tree->root = _tree_add_r(tree, tree->root, node);
+	}
+	obj_unlock(tree);
+}
diff --git a/tree/tree_remove.c b/tree/tree_remove.c
new file mode 100644
index 0000000..6510a03
--- /dev/null
+++ b/tree/tree_remove.c
@@ -0,0 +1,111 @@
+/*
+ * Copyright (C) 2023 Barry
+ *
+ * This file is part of Barry's Little Objects in C Library (libBLOC).
+ *
+ * libBLOC is free software: you can redistribute it and/or modify it under the
+ * terms of the GNU General Public License as published by the Free Software
+ * Foundation, either version 3 of the License, or (at your option) any later
+ * version.
+ *
+ * libBLOC is distributed in the hope that it will be useful, but WITHOUT ANY
+ * WARRANTY; without even the implied warranty of MERCHANTIBILITY or FITNESS
+ * FOR A PARTICULAR PURPOSE.  See the GNU General Public License for more
+ * details.
+ *
+ * You should have received a copy of the GNU General Public License along with
+ * libBLOC.  If not, see <https://www.gnu.org/licenses/>.
+ */
+
+#include <BLOC/object.h>
+#include <BLOC/tree.h>
+#include "../assert.h"
+#include "../object.h"
+#include "tree.h"
+
+/* Find a node recursively */
+static struct TreeNode *
+_tree_find(Tree *t, struct TreeNode *root, void *obj)
+{
+	if (!root)
+		return NULL;
+	if (root->header.obj == obj)
+		return root;
+	if (t->compare(root->header.obj, obj) > 0)
+		return _tree_find(t, root->left, obj);
+	else
+		return _tree_find(t, root->right, obj);
+}
+
+/* Remove a node from a tree recursively */
+static struct TreeNode *
+_tree_remove_r(Tree *t, struct TreeNode *parent, struct TreeNode *child)
+{
+	/* Remove node */
+	struct TreeNode *tmp;
+	if (t->compare(parent->header.obj, child->header.obj) > 0) {
+		parent->left = _tree_remove_r(t, parent->left, child);
+	} else if (t->compare(parent->header.obj, child->header.obj) < 0) {
+		parent->right = _tree_remove_r(t, parent->right, child);
+	} else {
+		/* Less that two children: adopt child if available */
+		if (!child->left)
+			return child->right;
+		if (!child->right)
+			return child->left;
+		/* Two children: swap for logical successor */
+		tmp = (struct TreeNode *) child->header.next;
+		if (child->right != tmp)
+			tmp->right = _tree_remove_r(t, child->right, tmp);
+		tmp->left = child->left;
+		return tmp;
+	}
+
+	/* Balance */
+	if (_tree_count(parent->right) > _tree_count(parent->left) + 1)
+		return _tree_rotate_left(parent);
+	if (_tree_count(parent->left) > _tree_count(parent->right) + 1)
+		return _tree_rotate_right(parent);
+	return parent;
+}
+
+/* Remove an object from a tree */
+void
+tree_remove(Tree *tree, void *obj)
+{
+	ASSERT(tree);
+	ASSERT(obj);
+	struct ObjectHeader *header = object_header(obj);
+	ASSERT(header->magic == OBJECT_MAGIC);
+	if (!tree->header.start)
+		return;
+	if (tree->type && tree->type != header->type)
+		return;
+	obj_lock(tree);
+
+	/* Search for object */
+	struct TreeNode *node = _tree_find(tree, tree->root, obj);
+	if (!node) {
+		obj_unlock(tree);
+		return;
+	}
+
+	/* Unlink from tree */
+	tree->root = _tree_remove_r(tree, tree->root, node);
+
+	/* Unlink from linear list*/
+	if (tree->header.start == &node->header)
+		tree->header.start = node->header.next;
+	if (tree->header.end == &node->header)
+		tree->header.end = node->header.prev;
+	if (node->header.prev)
+		node->header.prev->next = node->header.next;
+	if (node->header.next)
+		node->header.next->prev = node->header.prev;
+
+	obj_put(node->header.obj);
+	tree->header.entries--;
+	free(node);
+
+	obj_unlock(tree);
+}