summaryrefslogtreecommitdiffstats
path: root/libcnary
diff options
context:
space:
mode:
Diffstat (limited to 'libcnary')
-rw-r--r--libcnary/include/node.h42
-rw-r--r--libcnary/include/node_list.h23
-rw-r--r--libcnary/node.c49
-rw-r--r--libcnary/node_list.c28
4 files changed, 77 insertions, 65 deletions
diff --git a/libcnary/include/node.h b/libcnary/include/node.h
index 7e9da50..123241a 100644
--- a/libcnary/include/node.h
+++ b/libcnary/include/node.h
@@ -24,42 +24,42 @@
24#ifndef NODE_H_ 24#ifndef NODE_H_
25#define NODE_H_ 25#define NODE_H_
26 26
27#include "node_list.h"
27#include "object.h" 28#include "object.h"
28 29
29#define NODE_TYPE 1; 30#define NODE_TYPE 1;
30 31
31struct node_list_t;
32
33// This class implements the abstract iterator class 32// This class implements the abstract iterator class
34typedef struct node_t { 33typedef struct node* node_t;
34struct node {
35 // Super class 35 // Super class
36 struct node_t* next; 36 node_t next;
37 struct node_t* prev; 37 node_t prev;
38 unsigned int count; 38 unsigned int count;
39 39
40 // Local Members 40 // Local Members
41 void *data; 41 void *data;
42 struct node_t* parent; 42 node_t parent;
43 struct node_list_t* children; 43 node_list_t children;
44} node_t; 44};
45 45
46void node_destroy(struct node_t* node); 46void node_destroy(node_t node);
47struct node_t* node_create(struct node_t* parent, void* data); 47node_t node_create(node_t parent, void* data);
48 48
49int node_attach(struct node_t* parent, struct node_t* child); 49int node_attach(node_t parent, node_t child);
50int node_detach(struct node_t* parent, struct node_t* child); 50int node_detach(node_t parent, node_t child);
51int node_insert(struct node_t* parent, unsigned int index, struct node_t* child); 51int node_insert(node_t parent, unsigned int index, node_t child);
52 52
53unsigned int node_n_children(struct node_t* node); 53unsigned int node_n_children(node_t node);
54node_t* node_nth_child(struct node_t* node, unsigned int n); 54node_t node_nth_child(node_t node, unsigned int n);
55node_t* node_first_child(struct node_t* node); 55node_t node_first_child(node_t node);
56node_t* node_prev_sibling(struct node_t* node); 56node_t node_prev_sibling(node_t node);
57node_t* node_next_sibling(struct node_t* node); 57node_t node_next_sibling(node_t node);
58int node_child_position(struct node_t* parent, node_t* child); 58int node_child_position(node_t parent, node_t child);
59 59
60typedef void* (*copy_func_t)(const void *src); 60typedef void* (*copy_func_t)(const void *src);
61node_t* node_copy_deep(node_t* node, copy_func_t copy_func); 61node_t node_copy_deep(node_t node, copy_func_t copy_func);
62 62
63void node_debug(struct node_t* node); 63void node_debug(node_t node);
64 64
65#endif /* NODE_H_ */ 65#endif /* NODE_H_ */
diff --git a/libcnary/include/node_list.h b/libcnary/include/node_list.h
index 380916e..d566b00 100644
--- a/libcnary/include/node_list.h
+++ b/libcnary/include/node_list.h
@@ -24,24 +24,27 @@
24#ifndef NODE_LIST_H_ 24#ifndef NODE_LIST_H_
25#define NODE_LIST_H_ 25#define NODE_LIST_H_
26 26
27struct node_t; 27#include "node.h"
28
29typedef struct node* node_t;
28 30
29// This class implements the list_t abstract class 31// This class implements the list_t abstract class
30typedef struct node_list_t { 32struct node_list {
31 // list_t members 33 // list_t members
32 struct node_t* begin; 34 node_t begin;
33 struct node_t* end; 35 node_t end;
34 36
35 // node_list_t members 37 // node_list_t members
36 unsigned int count; 38 unsigned int count;
37 39
38} node_list_t; 40};
41typedef struct node_list* node_list_t;
39 42
40void node_list_destroy(struct node_list_t* list); 43void node_list_destroy(node_list_t list);
41struct node_list_t* node_list_create(); 44node_list_t node_list_create();
42 45
43int node_list_add(node_list_t* list, node_t* node); 46int node_list_add(node_list_t list, node_t node);
44int node_list_insert(node_list_t* list, unsigned int index, node_t* node); 47int node_list_insert(node_list_t list, unsigned int index, node_t node);
45int node_list_remove(node_list_t* list, node_t* node); 48int node_list_remove(node_list_t list, node_t node);
46 49
47#endif /* NODE_LIST_H_ */ 50#endif /* NODE_LIST_H_ */
diff --git a/libcnary/node.c b/libcnary/node.c
index 6d68f6e..8d3708b 100644
--- a/libcnary/node.c
+++ b/libcnary/node.c
@@ -27,11 +27,12 @@
27#include "node.h" 27#include "node.h"
28#include "node_list.h" 28#include "node_list.h"
29 29
30void node_destroy(node_t* node) { 30void node_destroy(node_t node)
31{
31 if(!node) return; 32 if(!node) return;
32 33
33 if (node->children && node->children->count > 0) { 34 if (node->children && node->children->count > 0) {
34 node_t* ch; 35 node_t ch;
35 while ((ch = node->children->begin)) { 36 while ((ch = node->children->begin)) {
36 node_list_remove(node->children, ch); 37 node_list_remove(node->children, ch);
37 node_destroy(ch); 38 node_destroy(ch);
@@ -43,10 +44,11 @@ void node_destroy(node_t* node) {
43 free(node); 44 free(node);
44} 45}
45 46
46node_t* node_create(node_t* parent, void* data) { 47node_t node_create(node_t parent, void* data)
48{
47 int error = 0; 49 int error = 0;
48 50
49 node_t* node = (node_t*)calloc(1, sizeof(node_t)); 51 node_t node = (node_t)calloc(1, sizeof(struct node));
50 if (node == NULL) { 52 if (node == NULL) {
51 return NULL; 53 return NULL;
52 } 54 }
@@ -73,7 +75,8 @@ node_t* node_create(node_t* parent, void* data) {
73 return node; 75 return node;
74} 76}
75 77
76int node_attach(node_t* parent, node_t* child) { 78int node_attach(node_t parent, node_t child)
79{
77 if (!parent || !child) return -1; 80 if (!parent || !child) return -1;
78 child->parent = parent; 81 child->parent = parent;
79 if(!parent->children) { 82 if(!parent->children) {
@@ -86,7 +89,8 @@ int node_attach(node_t* parent, node_t* child) {
86 return res; 89 return res;
87} 90}
88 91
89int node_detach(node_t* parent, node_t* child) { 92int node_detach(node_t parent, node_t child)
93{
90 if (!parent || !child) return -1; 94 if (!parent || !child) return -1;
91 int node_index = node_list_remove(parent->children, child); 95 int node_index = node_list_remove(parent->children, child);
92 if (node_index >= 0) { 96 if (node_index >= 0) {
@@ -95,7 +99,7 @@ int node_detach(node_t* parent, node_t* child) {
95 return node_index; 99 return node_index;
96} 100}
97 101
98int node_insert(node_t* parent, unsigned int node_index, node_t* child) 102int node_insert(node_t parent, unsigned int node_index, node_t child)
99{ 103{
100 if (!parent || !child) return -1; 104 if (!parent || !child) return -1;
101 child->parent = parent; 105 child->parent = parent;
@@ -109,9 +113,10 @@ int node_insert(node_t* parent, unsigned int node_index, node_t* child)
109 return res; 113 return res;
110} 114}
111 115
112static void _node_debug(node_t* node, unsigned int depth) { 116static void _node_debug(node_t node, unsigned int depth)
117{
113 unsigned int i = 0; 118 unsigned int i = 0;
114 node_t* current = NULL; 119 node_t current = NULL;
115 for(i = 0; i < depth; i++) { 120 for(i = 0; i < depth; i++) {
116 printf("\t"); 121 printf("\t");
117 } 122 }
@@ -132,23 +137,23 @@ static void _node_debug(node_t* node, unsigned int depth) {
132 137
133} 138}
134 139
135void node_debug(node_t* node) 140void node_debug(node_t node)
136{ 141{
137 _node_debug(node, 0); 142 _node_debug(node, 0);
138} 143}
139 144
140unsigned int node_n_children(struct node_t* node) 145unsigned int node_n_children(node_t node)
141{ 146{
142 if (!node) return 0; 147 if (!node) return 0;
143 return node->count; 148 return node->count;
144} 149}
145 150
146node_t* node_nth_child(struct node_t* node, unsigned int n) 151node_t node_nth_child(node_t node, unsigned int n)
147{ 152{
148 if (!node || !node->children || !node->children->begin) return NULL; 153 if (!node || !node->children || !node->children->begin) return NULL;
149 unsigned int node_index = 0; 154 unsigned int node_index = 0;
150 int found = 0; 155 int found = 0;
151 node_t *ch; 156 node_t ch;
152 for (ch = node_first_child(node); ch; ch = node_next_sibling(ch)) { 157 for (ch = node_first_child(node); ch; ch = node_next_sibling(ch)) {
153 if (node_index++ == n) { 158 if (node_index++ == n) {
154 found = 1; 159 found = 1;
@@ -161,30 +166,30 @@ node_t* node_nth_child(struct node_t* node, unsigned int n)
161 return ch; 166 return ch;
162} 167}
163 168
164node_t* node_first_child(struct node_t* node) 169node_t node_first_child(node_t node)
165{ 170{
166 if (!node || !node->children) return NULL; 171 if (!node || !node->children) return NULL;
167 return node->children->begin; 172 return node->children->begin;
168} 173}
169 174
170node_t* node_prev_sibling(struct node_t* node) 175node_t node_prev_sibling(node_t node)
171{ 176{
172 if (!node) return NULL; 177 if (!node) return NULL;
173 return node->prev; 178 return node->prev;
174} 179}
175 180
176node_t* node_next_sibling(struct node_t* node) 181node_t node_next_sibling(node_t node)
177{ 182{
178 if (!node) return NULL; 183 if (!node) return NULL;
179 return node->next; 184 return node->next;
180} 185}
181 186
182int node_child_position(struct node_t* parent, node_t* child) 187int node_child_position(node_t parent, node_t child)
183{ 188{
184 if (!parent || !parent->children || !parent->children->begin || !child) return -1; 189 if (!parent || !parent->children || !parent->children->begin || !child) return -1;
185 int node_index = 0; 190 int node_index = 0;
186 int found = 0; 191 int found = 0;
187 node_t *ch; 192 node_t ch;
188 for (ch = node_first_child(parent); ch; ch = node_next_sibling(ch)) { 193 for (ch = node_first_child(parent); ch; ch = node_next_sibling(ch)) {
189 if (ch == child) { 194 if (ch == child) {
190 found = 1; 195 found = 1;
@@ -198,17 +203,17 @@ int node_child_position(struct node_t* parent, node_t* child)
198 return node_index; 203 return node_index;
199} 204}
200 205
201node_t* node_copy_deep(node_t* node, copy_func_t copy_func) 206node_t node_copy_deep(node_t node, copy_func_t copy_func)
202{ 207{
203 if (!node) return NULL; 208 if (!node) return NULL;
204 void *data = NULL; 209 void *data = NULL;
205 if (copy_func) { 210 if (copy_func) {
206 data = copy_func(node->data); 211 data = copy_func(node->data);
207 } 212 }
208 node_t* copy = node_create(NULL, data); 213 node_t copy = node_create(NULL, data);
209 node_t* ch; 214 node_t ch;
210 for (ch = node_first_child(node); ch; ch = node_next_sibling(ch)) { 215 for (ch = node_first_child(node); ch; ch = node_next_sibling(ch)) {
211 node_t* cc = node_copy_deep(ch, copy_func); 216 node_t cc = node_copy_deep(ch, copy_func);
212 node_attach(copy, cc); 217 node_attach(copy, cc);
213 } 218 }
214 return copy; 219 return copy;
diff --git a/libcnary/node_list.c b/libcnary/node_list.c
index aee3bd6..f6c2c70 100644
--- a/libcnary/node_list.c
+++ b/libcnary/node_list.c
@@ -28,12 +28,14 @@
28#include "node.h" 28#include "node.h"
29#include "node_list.h" 29#include "node_list.h"
30 30
31void node_list_destroy(node_list_t* list) { 31void node_list_destroy(node_list_t list)
32{
32 free(list); 33 free(list);
33} 34}
34 35
35node_list_t* node_list_create() { 36node_list_t node_list_create()
36 node_list_t* list = (node_list_t*)calloc(1, sizeof(node_list_t)); 37{
38 node_list_t list = (node_list_t)calloc(1, sizeof(struct node_list));
37 if (list == NULL) { 39 if (list == NULL) {
38 return NULL; 40 return NULL;
39 } 41 }
@@ -45,11 +47,12 @@ node_list_t* node_list_create() {
45 return list; 47 return list;
46} 48}
47 49
48int node_list_add(node_list_t* list, node_t* node) { 50int node_list_add(node_list_t list, node_t node)
51{
49 if (!list || !node) return -1; 52 if (!list || !node) return -1;
50 53
51 // Find the last element in the list 54 // Find the last element in the list
52 node_t* last = list->end; 55 node_t last = list->end;
53 56
54 // Setup our new node as the new last element 57 // Setup our new node as the new last element
55 node->next = NULL; 58 node->next = NULL;
@@ -72,17 +75,18 @@ int node_list_add(node_list_t* list, node_t* node) {
72 return 0; 75 return 0;
73} 76}
74 77
75int node_list_insert(node_list_t* list, unsigned int node_index, node_t* node) { 78int node_list_insert(node_list_t list, unsigned int node_index, node_t node)
79{
76 if (!list || !node) return -1; 80 if (!list || !node) return -1;
77 if (node_index >= list->count) { 81 if (node_index >= list->count) {
78 return node_list_add(list, node); 82 return node_list_add(list, node);
79 } 83 }
80 84
81 // Get the first element in the list 85 // Get the first element in the list
82 node_t* cur = list->begin; 86 node_t cur = list->begin;
83 87
84 unsigned int pos = 0; 88 unsigned int pos = 0;
85 node_t* prev = NULL; 89 node_t prev = NULL;
86 90
87 if (node_index > 0) { 91 if (node_index > 0) {
88 while (pos < node_index) { 92 while (pos < node_index) {
@@ -120,15 +124,16 @@ int node_list_insert(node_list_t* list, unsigned int node_index, node_t* node) {
120 return 0; 124 return 0;
121} 125}
122 126
123int node_list_remove(node_list_t* list, node_t* node) { 127int node_list_remove(node_list_t list, node_t node)
128{
124 if (!list || !node) return -1; 129 if (!list || !node) return -1;
125 if (list->count == 0) return -1; 130 if (list->count == 0) return -1;
126 131
127 int node_index = 0; 132 int node_index = 0;
128 node_t* n; 133 node_t n;
129 for (n = list->begin; n; n = n->next) { 134 for (n = list->begin; n; n = n->next) {
130 if (node == n) { 135 if (node == n) {
131 node_t* newnode = node->next; 136 node_t newnode = node->next;
132 if (node->prev) { 137 if (node->prev) {
133 node->prev->next = newnode; 138 node->prev->next = newnode;
134 if (newnode) { 139 if (newnode) {
@@ -153,4 +158,3 @@ int node_list_remove(node_list_t* list, node_t* node) {
153 } 158 }
154 return -1; 159 return -1;
155} 160}
156