diff --git a/.vscode/settings.json b/.vscode/settings.json index 23e73d7..0df7e95 100644 --- a/.vscode/settings.json +++ b/.vscode/settings.json @@ -29,6 +29,7 @@ "iterator.h": "c", "assert.h": "c", "limits.h": "c", - "cstdint": "c" + "cstdint": "c", + "unicstl_internal.h": "c" } } \ No newline at end of file diff --git a/demo/demo.c b/demo/demo.c index 74af719..8fd720f 100644 --- a/demo/demo.c +++ b/demo/demo.c @@ -64,6 +64,7 @@ int main() demo_deque(); demo_tree(); demo_heap(); + demo_graph(); } printf("----- unicstl ok -----\n"); diff --git a/demo/demo.h b/demo/demo.h index 7001b9a..36db592 100644 --- a/demo/demo.h +++ b/demo/demo.h @@ -45,6 +45,7 @@ void demo_deque(void); void demo_queue(void); void demo_tree(void); void demo_heap(void); +void demo_graph(void); #endif // _DEMO_H_ diff --git a/demo/demo_graph.c b/demo/demo_graph.c new file mode 100644 index 0000000..fd0e26d --- /dev/null +++ b/demo/demo_graph.c @@ -0,0 +1,67 @@ +/** + * @file demo_graph.c + * @author wenjf (Orig5826@163.com) + * @brief + * @version 0.1 + * @date 2025-04-30 + * + * @copyright Copyright (c) 2025 + * + */ +#include "demo.h" + +void demo_graph_add_vertex(void) +{ + const int size = 10; + int data[10] = { + 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, + }; + int temp = 11; + uint32_t i = 0; + + graph_t graph = graph_new(sizeof(int)); + graph->compare = compare_num; + graph->print_obj = print_num; + + // test add_vertex + for(i = 0; i < size; i++) + { + graph->add_vertex(graph, &data[i]); + } + // graph->print(graph); + + // test add_edge + graph->add_edge(graph, &data[0], &data[1], 12); + graph->add_edge(graph, &data[0], &data[2], 13); + graph->add_edge(graph, &data[1], &data[3], 24); + graph->add_edge(graph, &data[5], &data[6], 67); + graph->add_edge(graph, &data[7], &data[6], 87); + graph->add_edge(graph, &data[8], &data[2], 92); + + graph->add_edge(graph, &temp, &data[1], 0); + graph->print(graph); + + iterator_t iter_vertex = NULL; + iter_vertex = graph->iter(graph, GRAPH_BFS, &data[0]); + while(iter_vertex->hasnext(iter_vertex)) + { + temp = *(int *)iter_vertex->next(iter_vertex); + graph->print_obj(&temp); + } + printf("\n"); + + iter_vertex = graph->iter(graph, GRAPH_DFS, &data[0]); + while(iter_vertex->hasnext(iter_vertex)) + { + temp = *(int *)iter_vertex->next(iter_vertex); + graph->print_obj(&temp); + } + printf("\n"); + + graph_free(&graph); +} + +void demo_graph(void) +{ + demo_graph_add_vertex(); +} \ No newline at end of file diff --git a/include/deque.h b/include/deque.h index 68a88dd..5bd53ff 100644 --- a/include/deque.h +++ b/include/deque.h @@ -11,7 +11,7 @@ #ifndef _DEQUE_H_ #define _DEQUE_H_ -#include "common.h" +#include "unicstl_internal.h" enum _deque_order { diff --git a/include/graph.h b/include/graph.h index 34a41d9..739af8b 100644 --- a/include/graph.h +++ b/include/graph.h @@ -11,7 +11,7 @@ #ifndef _GRAPH_H #define _GRAPH_H -#include "common.h" +#include "unicstl_internal.h" #include "stack.h" #include "queue.h" diff --git a/include/heap.h b/include/heap.h index d0c0d75..a68b602 100644 --- a/include/heap.h +++ b/include/heap.h @@ -11,7 +11,7 @@ #ifndef _HEAP_H_ #define _HEAP_H_ -#include "common.h" +#include "unicstl_internal.h" typedef enum { diff --git a/include/iterator.h b/include/iterator.h index 5a2c153..4c5554d 100644 --- a/include/iterator.h +++ b/include/iterator.h @@ -11,7 +11,7 @@ #ifndef _ITER_H_ #define _ITER_H_ -#include "common.h" +#include "unicstl_internal.h" struct _iterator { diff --git a/include/list.h b/include/list.h index 7b13a81..b62a22a 100644 --- a/include/list.h +++ b/include/list.h @@ -15,7 +15,7 @@ #ifndef _LIST_H_ #define _LIST_H_ -#include "common.h" +#include "unicstl_internal.h" #include "iterator.h" #define LIST_UNLIMITED INT32_MAX diff --git a/include/queue.h b/include/queue.h index c6a7ab2..34f04f4 100644 --- a/include/queue.h +++ b/include/queue.h @@ -11,7 +11,7 @@ #ifndef _QUEUE_H_ #define _QUEUE_H_ -#include "common.h" +#include "unicstl_internal.h" struct _queue_node { diff --git a/include/stack.h b/include/stack.h index 741745b..236e618 100644 --- a/include/stack.h +++ b/include/stack.h @@ -11,7 +11,7 @@ #ifndef _STACK_H_ #define _STACK_H_ -#include "common.h" +#include "unicstl_internal.h" struct _stack_node { diff --git a/include/tree.h b/include/tree.h index 5558b4c..7353187 100644 --- a/include/tree.h +++ b/include/tree.h @@ -11,7 +11,7 @@ #ifndef _TREE_H_ #define _TREE_H_ -#include "common.h" +#include "unicstl_internal.h" #include "stack.h" #include "queue.h" diff --git a/include/unicstl.h b/include/unicstl.h index 06119a4..8fe81e4 100644 --- a/include/unicstl.h +++ b/include/unicstl.h @@ -16,7 +16,7 @@ #define UNICSTL_VERSION_MICRO 2 #define UNICSTL_VERSION ((UNICSTL_VERSION_MAJOR << 16) | (UNICSTL_VERSION_MINOR << 8) | UNICSTL_VERSION_MICRO) -#include "common.h" +#include "unicstl_internal.h" #include "list.h" #include "stack.h" diff --git a/include/common.h b/include/unicstl_internal.h similarity index 89% rename from include/common.h rename to include/unicstl_internal.h index 6bf1b89..fc33722 100644 --- a/include/common.h +++ b/include/unicstl_internal.h @@ -1,11 +1,11 @@ /** - * @file common.h + * @file unicstl_internal.h * @author wenjf (Orig5826@163.com) * @brief * @version 0.1 - * @date 2024-07-03 + * @date 2025-04-30 * - * @copyright Copyright (c) 2024 + * @copyright Copyright (c) 2025 * */ #ifndef _COMMON_H_ diff --git a/src/deque.c b/src/deque.c index 655d908..339365a 100644 --- a/src/deque.c +++ b/src/deque.c @@ -341,6 +341,9 @@ static bool deque_init(struct _deque* self, uint32_t obj_size) // iter self->iter = deque_iter; + // -------------------- default -------------------- + self->print_obj = default_print_obj; + // -------------------- debug -------------------- self->print = deque_print; diff --git a/src/graph.c b/src/graph.c index 5bd349f..86ea38b 100644 --- a/src/graph.c +++ b/src/graph.c @@ -1056,10 +1056,11 @@ static bool graph_init(struct _graph *self, uint32_t obj_size) // others self->from_matrix = NULL; + // -------------------- default -------------------- + self->compare = default_compare; + self->print_obj = default_print_obj; - self->compare = NULL; // -------------------- debug -------------------- - self->print_obj = NULL; self->print = graph_print; return true; diff --git a/src/heap.c b/src/heap.c index 72531a5..b4ecbc6 100644 --- a/src/heap.c +++ b/src/heap.c @@ -326,6 +326,10 @@ static bool heap_init2(struct _heap* self, uint32_t obj_size, uint32_t capacity) // config self->compare = NULL; + // -------------------- default -------------------- + self->compare = default_compare; + self->print_obj = default_print_obj; + // -------------------- debug -------------------- self->print = heap_print; return true; diff --git a/src/list.c b/src/list.c index ab4b0d6..995f518 100644 --- a/src/list.c +++ b/src/list.c @@ -364,63 +364,64 @@ iterator_t list_iter(struct _list* self) return iter; } -static bool list_init2(struct _list* list, uint32_t obj_size, uint32_t capacity) +static bool list_init2(struct _list* self, uint32_t obj_size, uint32_t capacity) { - assert(list != NULL); - if (list == NULL || obj_size == 0 || capacity == 0) + assert(self != NULL); + if (self == NULL || obj_size == 0 || capacity == 0) { return false; } // -------------------- private -------------------- - list->_obj_size = obj_size; - list->_size = 0; - list->_capacity = capacity; - list->_ratio = 2; - list->_cur = 0; + self->_obj_size = obj_size; + self->_size = 0; + self->_capacity = capacity; + self->_ratio = 2; + self->_cur = 0; - list->obj = (void*)malloc(list->_capacity * list->_obj_size); - if (list->obj == NULL) + self->obj = (void*)malloc(self->_capacity * self->_obj_size); + if (self->obj == NULL) { return false; } - list->_iter.next = list_iter_next; - list->_iter.hasnext = list_iter_hasnext; + self->_iter.next = list_iter_next; + self->_iter.hasnext = list_iter_hasnext; - list->_destory = list_destory; + self->_destory = list_destory; // -------------------- public -------------------- // kernel - list->append = list_append; - list->pop = list_pop; + self->append = list_append; + self->pop = list_pop; - list->insert = list_insert; - list->delete = list_delete; + self->insert = list_insert; + self->delete = list_delete; - list->get = list_get; - list->set = list_set; + self->get = list_get; + self->set = list_set; - list->index = list_index; - // list->contains = list_contains; + self->index = list_index; + // self->contains = list_contains; // base - list->clear = list_clear; - list->size = list_size; - list->empty = list_empty; - list->capacity = list_capacity; + self->clear = list_clear; + self->size = list_size; + self->empty = list_empty; + self->capacity = list_capacity; // iter - list->iter = list_iter; + self->iter = list_iter; // others - list->slice = list_slice; + self->slice = list_slice; + + // -------------------- default -------------------- + self->compare = default_compare; + self->print_obj = default_print_obj; - // config - list->compare = default_compare; // -------------------- debug -------------------- - list->print_obj = default_print_obj; - list->print = list_print; + self->print = list_print; return true; } diff --git a/src/queue.c b/src/queue.c index bbe91d3..a1fe3c0 100644 --- a/src/queue.c +++ b/src/queue.c @@ -420,6 +420,9 @@ static bool queue_init(struct _queue * self, uint32_t obj_size) // iter self->iter = queue_iter; + // -------------------- default -------------------- + self->print_obj = default_print_obj; + // -------------------- debug -------------------- self->print = queue_print; @@ -483,6 +486,9 @@ static bool queue_init2(struct _queue * self, uint32_t obj_size, uint32_t capaci // iter self->iter = queue2_iter; + // -------------------- default -------------------- + self->print_obj = default_print_obj; + // -------------------- debug -------------------- self->print = queue2_print; diff --git a/src/stack.c b/src/stack.c index f21bd14..f700da7 100644 --- a/src/stack.c +++ b/src/stack.c @@ -323,7 +323,7 @@ static bool stack_init(struct _stack* self, uint32_t obj_size) return false; } - // ---------- private ---------- + // -------------------- private -------------------- self->_obj_size = obj_size; self->_size = 0; self->_capacity = UINT32_MAX; @@ -339,7 +339,7 @@ static bool stack_init(struct _stack* self, uint32_t obj_size) self->_destory = stack_destory; - // ---------- public ---------- + // -------------------- public -------------------- // kernel self->push = stack_push; self->pop = stack_pop; @@ -354,8 +354,12 @@ static bool stack_init(struct _stack* self, uint32_t obj_size) // iter self->iter = stack_iter; - // ---------- debug ---------- + // -------------------- default -------------------- + self->print_obj = default_print_obj; + + // -------------------- debug -------------------- self->print = stack_print; + return true; } @@ -367,7 +371,7 @@ static bool stack_init2(struct _stack* self, uint32_t obj_size, uint32_t capacit return false; } - // ---------- private ---------- + // -------------------- private -------------------- self->_obj_size = obj_size; self->_size = 0; self->_capacity = capacity; @@ -393,7 +397,7 @@ static bool stack_init2(struct _stack* self, uint32_t obj_size, uint32_t capacit self->_destory = stack2_destory; - // ---------- public ---------- + // -------------------- public -------------------- // kernel self->push = stack2_push; self->pop = stack2_pop; @@ -408,8 +412,12 @@ static bool stack_init2(struct _stack* self, uint32_t obj_size, uint32_t capacit // iter self->iter = stack_iter; - // ---------- debug ---------- + // -------------------- default -------------------- + self->print_obj = default_print_obj; + + // -------------------- debug -------------------- self->print = stack2_print; + return true; } diff --git a/src/tree.c b/src/tree.c index 9b75030..9691fcf 100644 --- a/src/tree.c +++ b/src/tree.c @@ -1420,6 +1420,10 @@ static bool tree_avl_init(struct _tree* self, uint32_t obj_size) // others self->max = tree_max; self->min = tree_min; + + // -------------------- default -------------------- + self->compare = default_compare; + self->print_obj = default_print_obj; return true; } @@ -1469,6 +1473,10 @@ static bool tree_rb_init(struct _tree* self, uint32_t obj_size) self->max = tree_max; self->min = tree_min; + // -------------------- default -------------------- + self->compare = default_compare; + self->print_obj = default_print_obj; + return true; } diff --git a/src/unicstl_internal.c b/src/unicstl_internal.c index e502dfc..8c271a6 100644 --- a/src/unicstl_internal.c +++ b/src/unicstl_internal.c @@ -8,7 +8,7 @@ * @copyright Copyright (c) 2025 * */ -#include "common.h" +#include "unicstl_internal.h" int default_compare(void* obj1, void* obj2) {