Commit 74536d7a authored by Nikias Bassen's avatar Nikias Bassen

libcnary: Remove redundant members from node_t struct

parent 2e67a01b
...@@ -37,20 +37,10 @@ typedef struct node_t { ...@@ -37,20 +37,10 @@ typedef struct node_t {
struct node_t* prev; struct node_t* prev;
unsigned int count; unsigned int count;
// Local Properties
int isRoot;
int isLeaf;
// Local Members // Local Members
void *data; void *data;
unsigned int depth;
struct node_t* parent; struct node_t* parent;
struct node_list_t* children; struct node_list_t* children;
// Virtual Functions
int(*attach)(struct node_t* parent, struct node_t* child);
int(*detach)(struct node_t* parent, struct node_t* child);
} node_t; } node_t;
void node_destroy(struct node_t* node); void node_destroy(struct node_t* node);
......
...@@ -54,14 +54,11 @@ node_t* node_create(node_t* parent, void* data) { ...@@ -54,14 +54,11 @@ node_t* node_create(node_t* parent, void* data) {
memset(node, '\0', sizeof(node_t)); memset(node, '\0', sizeof(node_t));
node->data = data; node->data = data;
node->depth = 0;
node->next = NULL; node->next = NULL;
node->prev = NULL; node->prev = NULL;
node->count = 0; node->count = 0;
node->isLeaf = TRUE;
node->isRoot = TRUE;
node->parent = NULL; node->parent = NULL;
node->children = node_list_create(); node->children = NULL;
// Pass NULL to create a root node // Pass NULL to create a root node
if(parent != NULL) { if(parent != NULL) {
...@@ -80,12 +77,9 @@ node_t* node_create(node_t* parent, void* data) { ...@@ -80,12 +77,9 @@ node_t* node_create(node_t* parent, void* data) {
int node_attach(node_t* parent, node_t* child) { int node_attach(node_t* parent, node_t* child) {
if (!parent || !child) return -1; if (!parent || !child) return -1;
child->isLeaf = TRUE;
child->isRoot = FALSE;
child->parent = parent; child->parent = parent;
child->depth = parent->depth + 1; if(!parent->children) {
if(parent->isLeaf == TRUE) { parent->children = node_list_create();
parent->isLeaf = FALSE;
} }
int res = node_list_add(parent->children, child); int res = node_list_add(parent->children, child);
if (res == 0) { if (res == 0) {
...@@ -106,12 +100,9 @@ int node_detach(node_t* parent, node_t* child) { ...@@ -106,12 +100,9 @@ int node_detach(node_t* parent, node_t* child) {
int node_insert(node_t* parent, unsigned int node_index, node_t* child) int node_insert(node_t* parent, unsigned int node_index, node_t* child)
{ {
if (!parent || !child) return -1; if (!parent || !child) return -1;
child->isLeaf = TRUE;
child->isRoot = FALSE;
child->parent = parent; child->parent = parent;
child->depth = parent->depth + 1; if(!parent->children) {
if(parent->isLeaf == TRUE) { parent->children = node_list_create();
parent->isLeaf = FALSE;
} }
int res = node_list_insert(parent->children, node_index, child); int res = node_list_insert(parent->children, node_index, child);
if (res == 0) { if (res == 0) {
...@@ -120,33 +111,37 @@ int node_insert(node_t* parent, unsigned int node_index, node_t* child) ...@@ -120,33 +111,37 @@ int node_insert(node_t* parent, unsigned int node_index, node_t* child)
return res; return res;
} }
void node_debug(node_t* node) { static void _node_debug(node_t* node, unsigned int depth) {
unsigned int i = 0; unsigned int i = 0;
node_t* current = NULL; node_t* current = NULL;
node_iterator_t* iter = NULL; node_iterator_t* iter = NULL;
for(i = 0; i < node->depth; i++) { for(i = 0; i < depth; i++) {
printf("\t"); printf("\t");
} }
if(node->isRoot) { if(!node->parent) {
printf("ROOT\n"); printf("ROOT\n");
} }
if(node->isLeaf && !node->isRoot) { if(!node->children && node->parent) {
printf("LEAF\n"); printf("LEAF\n");
} else { } else {
if(!node->isRoot) { if(node->parent) {
printf("NODE\n"); printf("NODE\n");
} }
iter = node_iterator_create(node->children); iter = node_iterator_create(node->children);
for(current = iter->begin; current != NULL; current = iter->next(iter)) { while ((current = iter->next(iter))) {
node_debug(current); _node_debug(current, depth+1);
} }
node_iterator_destroy(iter); node_iterator_destroy(iter);
} }
} }
void node_debug(node_t* node)
{
_node_debug(node, 0);
}
unsigned int node_n_children(struct node_t* node) unsigned int node_n_children(struct node_t* node)
{ {
if (!node) return 0; if (!node) return 0;
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment