diff options
Diffstat (limited to 'libcnary')
| -rw-r--r-- | libcnary/include/node.h | 10 | ||||
| -rw-r--r-- | libcnary/node.c | 39 |
2 files changed, 17 insertions, 32 deletions
diff --git a/libcnary/include/node.h b/libcnary/include/node.h index f9afdd6..7e9da50 100644 --- a/libcnary/include/node.h +++ b/libcnary/include/node.h | |||
| @@ -37,20 +37,10 @@ typedef struct node_t { | |||
| 37 | struct node_t* prev; | 37 | struct node_t* prev; |
| 38 | unsigned int count; | 38 | unsigned int count; |
| 39 | 39 | ||
| 40 | // Local Properties | ||
| 41 | int isRoot; | ||
| 42 | int isLeaf; | ||
| 43 | |||
| 44 | // Local Members | 40 | // Local Members |
| 45 | void *data; | 41 | void *data; |
| 46 | unsigned int depth; | ||
| 47 | struct node_t* parent; | 42 | struct node_t* parent; |
| 48 | struct node_list_t* children; | 43 | struct node_list_t* children; |
| 49 | |||
| 50 | // Virtual Functions | ||
| 51 | int(*attach)(struct node_t* parent, struct node_t* child); | ||
| 52 | int(*detach)(struct node_t* parent, struct node_t* child); | ||
| 53 | |||
| 54 | } node_t; | 44 | } node_t; |
| 55 | 45 | ||
| 56 | void node_destroy(struct node_t* node); | 46 | void node_destroy(struct node_t* node); |
diff --git a/libcnary/node.c b/libcnary/node.c index 3bd6975..4b550dd 100644 --- a/libcnary/node.c +++ b/libcnary/node.c | |||
| @@ -54,14 +54,11 @@ node_t* node_create(node_t* parent, void* data) { | |||
| 54 | memset(node, '\0', sizeof(node_t)); | 54 | memset(node, '\0', sizeof(node_t)); |
| 55 | 55 | ||
| 56 | node->data = data; | 56 | node->data = data; |
| 57 | node->depth = 0; | ||
| 58 | node->next = NULL; | 57 | node->next = NULL; |
| 59 | node->prev = NULL; | 58 | node->prev = NULL; |
| 60 | node->count = 0; | 59 | node->count = 0; |
| 61 | node->isLeaf = TRUE; | ||
| 62 | node->isRoot = TRUE; | ||
| 63 | node->parent = NULL; | 60 | node->parent = NULL; |
| 64 | node->children = node_list_create(); | 61 | node->children = NULL; |
| 65 | 62 | ||
| 66 | // Pass NULL to create a root node | 63 | // Pass NULL to create a root node |
| 67 | if(parent != NULL) { | 64 | if(parent != NULL) { |
| @@ -80,12 +77,9 @@ node_t* node_create(node_t* parent, void* data) { | |||
| 80 | 77 | ||
| 81 | int node_attach(node_t* parent, node_t* child) { | 78 | int node_attach(node_t* parent, node_t* child) { |
| 82 | if (!parent || !child) return -1; | 79 | if (!parent || !child) return -1; |
| 83 | child->isLeaf = TRUE; | ||
| 84 | child->isRoot = FALSE; | ||
| 85 | child->parent = parent; | 80 | child->parent = parent; |
| 86 | child->depth = parent->depth + 1; | 81 | if(!parent->children) { |
| 87 | if(parent->isLeaf == TRUE) { | 82 | parent->children = node_list_create(); |
| 88 | parent->isLeaf = FALSE; | ||
| 89 | } | 83 | } |
| 90 | int res = node_list_add(parent->children, child); | 84 | int res = node_list_add(parent->children, child); |
| 91 | if (res == 0) { | 85 | if (res == 0) { |
| @@ -106,12 +100,9 @@ int node_detach(node_t* parent, node_t* child) { | |||
| 106 | int node_insert(node_t* parent, unsigned int node_index, node_t* child) | 100 | int node_insert(node_t* parent, unsigned int node_index, node_t* child) |
| 107 | { | 101 | { |
| 108 | if (!parent || !child) return -1; | 102 | if (!parent || !child) return -1; |
| 109 | child->isLeaf = TRUE; | ||
| 110 | child->isRoot = FALSE; | ||
| 111 | child->parent = parent; | 103 | child->parent = parent; |
| 112 | child->depth = parent->depth + 1; | 104 | if(!parent->children) { |
| 113 | if(parent->isLeaf == TRUE) { | 105 | parent->children = node_list_create(); |
| 114 | parent->isLeaf = FALSE; | ||
| 115 | } | 106 | } |
| 116 | int res = node_list_insert(parent->children, node_index, child); | 107 | int res = node_list_insert(parent->children, node_index, child); |
| 117 | if (res == 0) { | 108 | if (res == 0) { |
| @@ -120,33 +111,37 @@ int node_insert(node_t* parent, unsigned int node_index, node_t* child) | |||
| 120 | return res; | 111 | return res; |
| 121 | } | 112 | } |
| 122 | 113 | ||
| 123 | void node_debug(node_t* node) { | 114 | static void _node_debug(node_t* node, unsigned int depth) { |
| 124 | unsigned int i = 0; | 115 | unsigned int i = 0; |
| 125 | node_t* current = NULL; | 116 | node_t* current = NULL; |
| 126 | node_iterator_t* iter = NULL; | 117 | node_iterator_t* iter = NULL; |
| 127 | for(i = 0; i < node->depth; i++) { | 118 | for(i = 0; i < depth; i++) { |
| 128 | printf("\t"); | 119 | printf("\t"); |
| 129 | } | 120 | } |
| 130 | if(node->isRoot) { | 121 | if(!node->parent) { |
| 131 | printf("ROOT\n"); | 122 | printf("ROOT\n"); |
| 132 | } | 123 | } |
| 133 | 124 | ||
| 134 | if(node->isLeaf && !node->isRoot) { | 125 | if(!node->children && node->parent) { |
| 135 | printf("LEAF\n"); | 126 | printf("LEAF\n"); |
| 136 | |||
| 137 | } else { | 127 | } else { |
| 138 | if(!node->isRoot) { | 128 | if(node->parent) { |
| 139 | printf("NODE\n"); | 129 | printf("NODE\n"); |
| 140 | } | 130 | } |
| 141 | iter = node_iterator_create(node->children); | 131 | iter = node_iterator_create(node->children); |
| 142 | for(current = iter->begin; current != NULL; current = iter->next(iter)) { | 132 | while ((current = iter->next(iter))) { |
| 143 | node_debug(current); | 133 | _node_debug(current, depth+1); |
| 144 | } | 134 | } |
| 145 | node_iterator_destroy(iter); | 135 | node_iterator_destroy(iter); |
| 146 | } | 136 | } |
| 147 | 137 | ||
| 148 | } | 138 | } |
| 149 | 139 | ||
| 140 | void node_debug(node_t* node) | ||
| 141 | { | ||
| 142 | _node_debug(node, 0); | ||
| 143 | } | ||
| 144 | |||
| 150 | unsigned int node_n_children(struct node_t* node) | 145 | unsigned int node_n_children(struct node_t* node) |
| 151 | { | 146 | { |
| 152 | if (!node) return 0; | 147 | if (!node) return 0; |
