ds: tree: convert to new object system
This commit is contained in:
186
ds/tree.c
186
ds/tree.c
@@ -1,8 +1,4 @@
|
||||
#include "tree.h"
|
||||
|
||||
#include <blue/ds/object.h>
|
||||
#include <blue/ds/tree.h>
|
||||
#include <blue/ds/type.h>
|
||||
#include <stdlib.h>
|
||||
#include <string.h>
|
||||
|
||||
@@ -16,27 +12,85 @@
|
||||
#define NODE_FIRST_CHILD(n) ((n)->__p02)
|
||||
#define NODE_NEXT_SIBLING(n) ((n)->__p03)
|
||||
|
||||
static struct b_dsref_type tree_type = {
|
||||
.t_name = "corelib::tree",
|
||||
.t_flags = B_DSREF_FUNDAMENTAL,
|
||||
.t_id = B_DSREF_TYPE_TREE,
|
||||
.t_instance_size = sizeof(struct b_tree),
|
||||
/*** PRIVATE DATA *************************************************************/
|
||||
|
||||
struct b_tree_p {
|
||||
struct b_tree_node *t_root;
|
||||
};
|
||||
|
||||
struct b_tree *b_tree_create(void)
|
||||
/*** PRIVATE FUNCTIONS ********************************************************/
|
||||
|
||||
static void tree_set_root(struct b_tree_p *tree, struct b_tree_node *node)
|
||||
{
|
||||
struct b_tree *tree
|
||||
= (struct b_tree *)b_dsref_type_instantiate(&tree_type);
|
||||
if (!tree) {
|
||||
tree->t_root = node;
|
||||
}
|
||||
|
||||
static const struct b_tree_node *next_node(
|
||||
const struct b_tree_node *node, bool recursive, int *depth_diff)
|
||||
{
|
||||
if (!node) {
|
||||
return NULL;
|
||||
}
|
||||
|
||||
return tree;
|
||||
if (!recursive) {
|
||||
node = NODE_NEXT_SIBLING(node);
|
||||
return node;
|
||||
}
|
||||
|
||||
int d = 0;
|
||||
struct b_tree_node *next = NODE_FIRST_CHILD(node);
|
||||
if (next) {
|
||||
d = 1;
|
||||
*depth_diff = d;
|
||||
return next;
|
||||
}
|
||||
|
||||
const struct b_tree_node *n = node;
|
||||
next = NODE_NEXT_SIBLING(n);
|
||||
while (!next) {
|
||||
n = NODE_PARENT(n);
|
||||
if (!n) {
|
||||
break;
|
||||
}
|
||||
|
||||
d--;
|
||||
next = NODE_NEXT_SIBLING(n);
|
||||
}
|
||||
|
||||
*depth_diff = d;
|
||||
return next;
|
||||
}
|
||||
|
||||
void b_tree_set_root(struct b_tree *tree, struct b_tree_node *node)
|
||||
static void reparent_children(
|
||||
struct b_tree_node *old_parent, struct b_tree_node *new_parent)
|
||||
{
|
||||
tree->t_root = node;
|
||||
struct b_tree_node *last = NODE_FIRST_CHILD(new_parent);
|
||||
while (last && NODE_NEXT_SIBLING(last)) {
|
||||
last = NODE_NEXT_SIBLING(last);
|
||||
}
|
||||
|
||||
struct b_tree_node *cur = NODE_FIRST_CHILD(old_parent);
|
||||
while (cur) {
|
||||
struct b_tree_node *next = NODE_NEXT_SIBLING(cur);
|
||||
NODE_PARENT(cur) = new_parent;
|
||||
NODE_NEXT_SIBLING(cur) = NULL;
|
||||
|
||||
if (last) {
|
||||
NODE_NEXT_SIBLING(last) = cur;
|
||||
} else {
|
||||
NODE_FIRST_CHILD(new_parent) = cur;
|
||||
}
|
||||
|
||||
last = cur;
|
||||
cur = next;
|
||||
}
|
||||
}
|
||||
|
||||
/*** PUBLIC FUNCTIONS *********************************************************/
|
||||
|
||||
void b_tree_set_root(b_tree *tree, struct b_tree_node *node)
|
||||
{
|
||||
B_CLASS_DISPATCH_STATIC(B_TYPE_TREE, tree_set_root, tree, node);
|
||||
}
|
||||
|
||||
void b_tree_node_add_child(struct b_tree_node *parent, struct b_tree_node *child)
|
||||
@@ -85,6 +139,35 @@ struct b_tree_node *b_tree_node_get_child(struct b_tree_node *node, size_t at)
|
||||
return cur;
|
||||
}
|
||||
|
||||
/*** VIRTUAL FUNCTIONS ********************************************************/
|
||||
|
||||
static void tree_init(b_object *obj, void *priv)
|
||||
{
|
||||
struct b_tree_p *tree = priv;
|
||||
}
|
||||
|
||||
static void tree_fini(b_object *obj, void *priv)
|
||||
{
|
||||
struct b_tree_p *tree = priv;
|
||||
}
|
||||
|
||||
/*** CLASS DEFINITION *********************************************************/
|
||||
|
||||
B_TYPE_CLASS_DEFINITION_BEGIN(b_tree)
|
||||
B_TYPE_CLASS_INTERFACE_BEGIN(b_object, B_TYPE_OBJECT)
|
||||
B_INTERFACE_ENTRY(to_string) = NULL;
|
||||
B_TYPE_CLASS_INTERFACE_END(b_object, B_TYPE_OBJECT)
|
||||
B_TYPE_CLASS_DEFINITION_END(b_tree)
|
||||
|
||||
B_TYPE_DEFINITION_BEGIN(b_tree)
|
||||
B_TYPE_ID(0x8d8fa36b, 0xc515, 0x4803, 0x8124, 0xfd704f01b8ae);
|
||||
B_TYPE_CLASS(b_tree_class);
|
||||
B_TYPE_INSTANCE_INIT(tree_init);
|
||||
B_TYPE_INSTANCE_INIT(tree_fini);
|
||||
B_TYPE_DEFINITION_END(b_tree)
|
||||
|
||||
/*** ITERATOR FUNCTIONS *******************************************************/
|
||||
|
||||
static bool tree_iterator_next(struct b_iterator *it)
|
||||
{
|
||||
return b_tree_iterator_next((struct b_tree_iterator *)it);
|
||||
@@ -105,9 +188,10 @@ struct b_tree_node *b_tree_node_get_parent(struct b_tree_node *node)
|
||||
return NODE_PARENT(node);
|
||||
}
|
||||
|
||||
int b_tree_iterator_begin(struct b_tree *tree, b_tree_iterator *it)
|
||||
int b_tree_iterator_begin(b_tree *tree, b_tree_iterator *it)
|
||||
{
|
||||
return b_tree_iterator_begin_at_node_recursive(tree->t_root, it);
|
||||
struct b_tree_p *p = b_object_get_private(tree, B_TYPE_TREE);
|
||||
return b_tree_iterator_begin_at_node_recursive(p->t_root, it);
|
||||
}
|
||||
|
||||
static b_iterator_ops it_ops = {
|
||||
@@ -143,42 +227,6 @@ int b_tree_iterator_begin_at_node_recursive(
|
||||
return 0;
|
||||
}
|
||||
|
||||
static const struct b_tree_node *next_node(
|
||||
const struct b_tree_node *node, bool recursive, int *depth_diff)
|
||||
{
|
||||
if (!node) {
|
||||
return NULL;
|
||||
}
|
||||
|
||||
if (!recursive) {
|
||||
node = NODE_NEXT_SIBLING(node);
|
||||
return node;
|
||||
}
|
||||
|
||||
int d = 0;
|
||||
struct b_tree_node *next = NODE_FIRST_CHILD(node);
|
||||
if (next) {
|
||||
d = 1;
|
||||
*depth_diff = d;
|
||||
return next;
|
||||
}
|
||||
|
||||
const struct b_tree_node *n = node;
|
||||
next = NODE_NEXT_SIBLING(n);
|
||||
while (!next) {
|
||||
n = NODE_PARENT(n);
|
||||
if (!n) {
|
||||
break;
|
||||
}
|
||||
|
||||
d--;
|
||||
next = NODE_NEXT_SIBLING(n);
|
||||
}
|
||||
|
||||
*depth_diff = d;
|
||||
return next;
|
||||
}
|
||||
|
||||
bool b_tree_iterator_next(struct b_tree_iterator *it)
|
||||
{
|
||||
int depth_diff = 0;
|
||||
@@ -229,31 +277,6 @@ static void remove_node(struct b_tree_node *node)
|
||||
NODE_PARENT(n0) = NODE_NEXT_SIBLING(n0) = NULL;
|
||||
}
|
||||
|
||||
static void reparent_children(
|
||||
struct b_tree_node *old_parent, struct b_tree_node *new_parent)
|
||||
{
|
||||
struct b_tree_node *last = NODE_FIRST_CHILD(new_parent);
|
||||
while (last && NODE_NEXT_SIBLING(last)) {
|
||||
last = NODE_NEXT_SIBLING(last);
|
||||
}
|
||||
|
||||
struct b_tree_node *cur = NODE_FIRST_CHILD(old_parent);
|
||||
while (cur) {
|
||||
struct b_tree_node *next = NODE_NEXT_SIBLING(cur);
|
||||
NODE_PARENT(cur) = new_parent;
|
||||
NODE_NEXT_SIBLING(cur) = NULL;
|
||||
|
||||
if (last) {
|
||||
NODE_NEXT_SIBLING(last) = cur;
|
||||
} else {
|
||||
NODE_FIRST_CHILD(new_parent) = cur;
|
||||
}
|
||||
|
||||
last = cur;
|
||||
cur = next;
|
||||
}
|
||||
}
|
||||
|
||||
b_status b_tree_iterator_erase(struct b_tree_iterator *it)
|
||||
{
|
||||
if (!it->node) {
|
||||
@@ -293,8 +316,3 @@ bool b_tree_iterator_is_valid(const struct b_tree_iterator *it)
|
||||
{
|
||||
return it->node != NULL;
|
||||
}
|
||||
|
||||
b_dsref_type_id b_tree_type_id(void)
|
||||
{
|
||||
return (b_dsref_type_id)&tree_type;
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user