From d3908006349f38bcfc0151daebd98b6873a2dbfc Mon Sep 17 00:00:00 2001 From: Nikias Bassen Date: Mon, 6 Feb 2023 18:28:28 +0100 Subject: libcnary: Updated typedefs of node_t and node_list_t to contain pointer This makes the code more readable. Obviously all the code that uses it is also updated. --- libcnary/include/node.h | 42 ++++++++++++++++++------------------- libcnary/include/node_list.h | 23 ++++++++++++--------- libcnary/node.c | 49 ++++++++++++++++++++++++-------------------- libcnary/node_list.c | 28 ++++++++++++++----------- 4 files changed, 77 insertions(+), 65 deletions(-) (limited to 'libcnary') 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 @@ #ifndef NODE_H_ #define NODE_H_ +#include "node_list.h" #include "object.h" #define NODE_TYPE 1; -struct node_list_t; - // This class implements the abstract iterator class -typedef struct node_t { +typedef struct node* node_t; +struct node { // Super class - struct node_t* next; - struct node_t* prev; + node_t next; + node_t prev; unsigned int count; // Local Members void *data; - struct node_t* parent; - struct node_list_t* children; -} node_t; + node_t parent; + node_list_t children; +}; -void node_destroy(struct node_t* node); -struct node_t* node_create(struct node_t* parent, void* data); +void node_destroy(node_t node); +node_t node_create(node_t parent, void* data); -int node_attach(struct node_t* parent, struct node_t* child); -int node_detach(struct node_t* parent, struct node_t* child); -int node_insert(struct node_t* parent, unsigned int index, struct node_t* child); +int node_attach(node_t parent, node_t child); +int node_detach(node_t parent, node_t child); +int node_insert(node_t parent, unsigned int index, node_t child); -unsigned int node_n_children(struct node_t* node); -node_t* node_nth_child(struct node_t* node, unsigned int n); -node_t* node_first_child(struct node_t* node); -node_t* node_prev_sibling(struct node_t* node); -node_t* node_next_sibling(struct node_t* node); -int node_child_position(struct node_t* parent, node_t* child); +unsigned int node_n_children(node_t node); +node_t node_nth_child(node_t node, unsigned int n); +node_t node_first_child(node_t node); +node_t node_prev_sibling(node_t node); +node_t node_next_sibling(node_t node); +int node_child_position(node_t parent, node_t child); typedef void* (*copy_func_t)(const void *src); -node_t* node_copy_deep(node_t* node, copy_func_t copy_func); +node_t node_copy_deep(node_t node, copy_func_t copy_func); -void node_debug(struct node_t* node); +void node_debug(node_t node); #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 @@ #ifndef NODE_LIST_H_ #define NODE_LIST_H_ -struct node_t; +#include "node.h" + +typedef struct node* node_t; // This class implements the list_t abstract class -typedef struct node_list_t { +struct node_list { // list_t members - struct node_t* begin; - struct node_t* end; + node_t begin; + node_t end; // node_list_t members unsigned int count; -} node_list_t; +}; +typedef struct node_list* node_list_t; -void node_list_destroy(struct node_list_t* list); -struct node_list_t* node_list_create(); +void node_list_destroy(node_list_t list); +node_list_t node_list_create(); -int node_list_add(node_list_t* list, node_t* node); -int node_list_insert(node_list_t* list, unsigned int index, node_t* node); -int node_list_remove(node_list_t* list, node_t* node); +int node_list_add(node_list_t list, node_t node); +int node_list_insert(node_list_t list, unsigned int index, node_t node); +int node_list_remove(node_list_t list, node_t node); #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 @@ #include "node.h" #include "node_list.h" -void node_destroy(node_t* node) { +void node_destroy(node_t node) +{ if(!node) return; if (node->children && node->children->count > 0) { - node_t* ch; + node_t ch; while ((ch = node->children->begin)) { node_list_remove(node->children, ch); node_destroy(ch); @@ -43,10 +44,11 @@ void node_destroy(node_t* node) { free(node); } -node_t* node_create(node_t* parent, void* data) { +node_t node_create(node_t parent, void* data) +{ int error = 0; - node_t* node = (node_t*)calloc(1, sizeof(node_t)); + node_t node = (node_t)calloc(1, sizeof(struct node)); if (node == NULL) { return NULL; } @@ -73,7 +75,8 @@ node_t* node_create(node_t* parent, void* data) { return node; } -int node_attach(node_t* parent, node_t* child) { +int node_attach(node_t parent, node_t child) +{ if (!parent || !child) return -1; child->parent = parent; if(!parent->children) { @@ -86,7 +89,8 @@ int node_attach(node_t* parent, node_t* child) { return res; } -int node_detach(node_t* parent, node_t* child) { +int node_detach(node_t parent, node_t child) +{ if (!parent || !child) return -1; int node_index = node_list_remove(parent->children, child); if (node_index >= 0) { @@ -95,7 +99,7 @@ int node_detach(node_t* parent, node_t* child) { return node_index; } -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; child->parent = parent; @@ -109,9 +113,10 @@ int node_insert(node_t* parent, unsigned int node_index, node_t* child) return res; } -static void _node_debug(node_t* node, unsigned int depth) { +static void _node_debug(node_t node, unsigned int depth) +{ unsigned int i = 0; - node_t* current = NULL; + node_t current = NULL; for(i = 0; i < depth; i++) { printf("\t"); } @@ -132,23 +137,23 @@ static void _node_debug(node_t* node, unsigned int depth) { } -void node_debug(node_t* node) +void node_debug(node_t node) { _node_debug(node, 0); } -unsigned int node_n_children(struct node_t* node) +unsigned int node_n_children(node_t node) { if (!node) return 0; return node->count; } -node_t* node_nth_child(struct node_t* node, unsigned int n) +node_t node_nth_child(node_t node, unsigned int n) { if (!node || !node->children || !node->children->begin) return NULL; unsigned int node_index = 0; int found = 0; - node_t *ch; + node_t ch; for (ch = node_first_child(node); ch; ch = node_next_sibling(ch)) { if (node_index++ == n) { found = 1; @@ -161,30 +166,30 @@ node_t* node_nth_child(struct node_t* node, unsigned int n) return ch; } -node_t* node_first_child(struct node_t* node) +node_t node_first_child(node_t node) { if (!node || !node->children) return NULL; return node->children->begin; } -node_t* node_prev_sibling(struct node_t* node) +node_t node_prev_sibling(node_t node) { if (!node) return NULL; return node->prev; } -node_t* node_next_sibling(struct node_t* node) +node_t node_next_sibling(node_t node) { if (!node) return NULL; return node->next; } -int node_child_position(struct node_t* parent, node_t* child) +int node_child_position(node_t parent, node_t child) { if (!parent || !parent->children || !parent->children->begin || !child) return -1; int node_index = 0; int found = 0; - node_t *ch; + node_t ch; for (ch = node_first_child(parent); ch; ch = node_next_sibling(ch)) { if (ch == child) { found = 1; @@ -198,17 +203,17 @@ int node_child_position(struct node_t* parent, node_t* child) return node_index; } -node_t* node_copy_deep(node_t* node, copy_func_t copy_func) +node_t node_copy_deep(node_t node, copy_func_t copy_func) { if (!node) return NULL; void *data = NULL; if (copy_func) { data = copy_func(node->data); } - node_t* copy = node_create(NULL, data); - node_t* ch; + node_t copy = node_create(NULL, data); + node_t ch; for (ch = node_first_child(node); ch; ch = node_next_sibling(ch)) { - node_t* cc = node_copy_deep(ch, copy_func); + node_t cc = node_copy_deep(ch, copy_func); node_attach(copy, cc); } 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 @@ #include "node.h" #include "node_list.h" -void node_list_destroy(node_list_t* list) { +void node_list_destroy(node_list_t list) +{ free(list); } -node_list_t* node_list_create() { - node_list_t* list = (node_list_t*)calloc(1, sizeof(node_list_t)); +node_list_t node_list_create() +{ + node_list_t list = (node_list_t)calloc(1, sizeof(struct node_list)); if (list == NULL) { return NULL; } @@ -45,11 +47,12 @@ node_list_t* node_list_create() { return list; } -int node_list_add(node_list_t* list, node_t* node) { +int node_list_add(node_list_t list, node_t node) +{ if (!list || !node) return -1; // Find the last element in the list - node_t* last = list->end; + node_t last = list->end; // Setup our new node as the new last element node->next = NULL; @@ -72,17 +75,18 @@ int node_list_add(node_list_t* list, node_t* node) { return 0; } -int node_list_insert(node_list_t* list, unsigned int node_index, node_t* node) { +int node_list_insert(node_list_t list, unsigned int node_index, node_t node) +{ if (!list || !node) return -1; if (node_index >= list->count) { return node_list_add(list, node); } // Get the first element in the list - node_t* cur = list->begin; + node_t cur = list->begin; unsigned int pos = 0; - node_t* prev = NULL; + node_t prev = NULL; if (node_index > 0) { while (pos < node_index) { @@ -120,15 +124,16 @@ int node_list_insert(node_list_t* list, unsigned int node_index, node_t* node) { return 0; } -int node_list_remove(node_list_t* list, node_t* node) { +int node_list_remove(node_list_t list, node_t node) +{ if (!list || !node) return -1; if (list->count == 0) return -1; int node_index = 0; - node_t* n; + node_t n; for (n = list->begin; n; n = n->next) { if (node == n) { - node_t* newnode = node->next; + node_t newnode = node->next; if (node->prev) { node->prev->next = newnode; if (newnode) { @@ -153,4 +158,3 @@ int node_list_remove(node_list_t* list, node_t* node) { } return -1; } - -- cgit v1.1-32-gdbae