修改common为unicstl_internal,顺便给其他数据结构都添加默认的compare和print_obj接口

This commit is contained in:
建峰 2025-04-30 23:26:36 +08:00
parent 32f2605656
commit 9322db6b62
22 changed files with 154 additions and 53 deletions

View File

@ -29,6 +29,7 @@
"iterator.h": "c", "iterator.h": "c",
"assert.h": "c", "assert.h": "c",
"limits.h": "c", "limits.h": "c",
"cstdint": "c" "cstdint": "c",
"unicstl_internal.h": "c"
} }
} }

View File

@ -64,6 +64,7 @@ int main()
demo_deque(); demo_deque();
demo_tree(); demo_tree();
demo_heap(); demo_heap();
demo_graph();
} }
printf("----- unicstl ok -----\n"); printf("----- unicstl ok -----\n");

View File

@ -45,6 +45,7 @@ void demo_deque(void);
void demo_queue(void); void demo_queue(void);
void demo_tree(void); void demo_tree(void);
void demo_heap(void); void demo_heap(void);
void demo_graph(void);
#endif // _DEMO_H_ #endif // _DEMO_H_

67
demo/demo_graph.c Normal file
View File

@ -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();
}

View File

@ -11,7 +11,7 @@
#ifndef _DEQUE_H_ #ifndef _DEQUE_H_
#define _DEQUE_H_ #define _DEQUE_H_
#include "common.h" #include "unicstl_internal.h"
enum _deque_order enum _deque_order
{ {

View File

@ -11,7 +11,7 @@
#ifndef _GRAPH_H #ifndef _GRAPH_H
#define _GRAPH_H #define _GRAPH_H
#include "common.h" #include "unicstl_internal.h"
#include "stack.h" #include "stack.h"
#include "queue.h" #include "queue.h"

View File

@ -11,7 +11,7 @@
#ifndef _HEAP_H_ #ifndef _HEAP_H_
#define _HEAP_H_ #define _HEAP_H_
#include "common.h" #include "unicstl_internal.h"
typedef enum typedef enum
{ {

View File

@ -11,7 +11,7 @@
#ifndef _ITER_H_ #ifndef _ITER_H_
#define _ITER_H_ #define _ITER_H_
#include "common.h" #include "unicstl_internal.h"
struct _iterator struct _iterator
{ {

View File

@ -15,7 +15,7 @@
#ifndef _LIST_H_ #ifndef _LIST_H_
#define _LIST_H_ #define _LIST_H_
#include "common.h" #include "unicstl_internal.h"
#include "iterator.h" #include "iterator.h"
#define LIST_UNLIMITED INT32_MAX #define LIST_UNLIMITED INT32_MAX

View File

@ -11,7 +11,7 @@
#ifndef _QUEUE_H_ #ifndef _QUEUE_H_
#define _QUEUE_H_ #define _QUEUE_H_
#include "common.h" #include "unicstl_internal.h"
struct _queue_node struct _queue_node
{ {

View File

@ -11,7 +11,7 @@
#ifndef _STACK_H_ #ifndef _STACK_H_
#define _STACK_H_ #define _STACK_H_
#include "common.h" #include "unicstl_internal.h"
struct _stack_node struct _stack_node
{ {

View File

@ -11,7 +11,7 @@
#ifndef _TREE_H_ #ifndef _TREE_H_
#define _TREE_H_ #define _TREE_H_
#include "common.h" #include "unicstl_internal.h"
#include "stack.h" #include "stack.h"
#include "queue.h" #include "queue.h"

View File

@ -16,7 +16,7 @@
#define UNICSTL_VERSION_MICRO 2 #define UNICSTL_VERSION_MICRO 2
#define UNICSTL_VERSION ((UNICSTL_VERSION_MAJOR << 16) | (UNICSTL_VERSION_MINOR << 8) | UNICSTL_VERSION_MICRO) #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 "list.h"
#include "stack.h" #include "stack.h"

View File

@ -1,11 +1,11 @@
/** /**
* @file common.h * @file unicstl_internal.h
* @author wenjf (Orig5826@163.com) * @author wenjf (Orig5826@163.com)
* @brief * @brief
* @version 0.1 * @version 0.1
* @date 2024-07-03 * @date 2025-04-30
* *
* @copyright Copyright (c) 2024 * @copyright Copyright (c) 2025
* *
*/ */
#ifndef _COMMON_H_ #ifndef _COMMON_H_

View File

@ -341,6 +341,9 @@ static bool deque_init(struct _deque* self, uint32_t obj_size)
// iter // iter
self->iter = deque_iter; self->iter = deque_iter;
// -------------------- default --------------------
self->print_obj = default_print_obj;
// -------------------- debug -------------------- // -------------------- debug --------------------
self->print = deque_print; self->print = deque_print;

View File

@ -1056,10 +1056,11 @@ static bool graph_init(struct _graph *self, uint32_t obj_size)
// others // others
self->from_matrix = NULL; self->from_matrix = NULL;
// -------------------- default --------------------
self->compare = default_compare;
self->print_obj = default_print_obj;
self->compare = NULL;
// -------------------- debug -------------------- // -------------------- debug --------------------
self->print_obj = NULL;
self->print = graph_print; self->print = graph_print;
return true; return true;

View File

@ -326,6 +326,10 @@ static bool heap_init2(struct _heap* self, uint32_t obj_size, uint32_t capacity)
// config // config
self->compare = NULL; self->compare = NULL;
// -------------------- default --------------------
self->compare = default_compare;
self->print_obj = default_print_obj;
// -------------------- debug -------------------- // -------------------- debug --------------------
self->print = heap_print; self->print = heap_print;
return true; return true;

View File

@ -364,63 +364,64 @@ iterator_t list_iter(struct _list* self)
return iter; 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); assert(self != NULL);
if (list == NULL || obj_size == 0 || capacity == 0) if (self == NULL || obj_size == 0 || capacity == 0)
{ {
return false; return false;
} }
// -------------------- private -------------------- // -------------------- private --------------------
list->_obj_size = obj_size; self->_obj_size = obj_size;
list->_size = 0; self->_size = 0;
list->_capacity = capacity; self->_capacity = capacity;
list->_ratio = 2; self->_ratio = 2;
list->_cur = 0; self->_cur = 0;
list->obj = (void*)malloc(list->_capacity * list->_obj_size); self->obj = (void*)malloc(self->_capacity * self->_obj_size);
if (list->obj == NULL) if (self->obj == NULL)
{ {
return false; return false;
} }
list->_iter.next = list_iter_next; self->_iter.next = list_iter_next;
list->_iter.hasnext = list_iter_hasnext; self->_iter.hasnext = list_iter_hasnext;
list->_destory = list_destory; self->_destory = list_destory;
// -------------------- public -------------------- // -------------------- public --------------------
// kernel // kernel
list->append = list_append; self->append = list_append;
list->pop = list_pop; self->pop = list_pop;
list->insert = list_insert; self->insert = list_insert;
list->delete = list_delete; self->delete = list_delete;
list->get = list_get; self->get = list_get;
list->set = list_set; self->set = list_set;
list->index = list_index; self->index = list_index;
// list->contains = list_contains; // self->contains = list_contains;
// base // base
list->clear = list_clear; self->clear = list_clear;
list->size = list_size; self->size = list_size;
list->empty = list_empty; self->empty = list_empty;
list->capacity = list_capacity; self->capacity = list_capacity;
// iter // iter
list->iter = list_iter; self->iter = list_iter;
// others // 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 -------------------- // -------------------- debug --------------------
list->print_obj = default_print_obj; self->print = list_print;
list->print = list_print;
return true; return true;
} }

View File

@ -420,6 +420,9 @@ static bool queue_init(struct _queue * self, uint32_t obj_size)
// iter // iter
self->iter = queue_iter; self->iter = queue_iter;
// -------------------- default --------------------
self->print_obj = default_print_obj;
// -------------------- debug -------------------- // -------------------- debug --------------------
self->print = queue_print; self->print = queue_print;
@ -483,6 +486,9 @@ static bool queue_init2(struct _queue * self, uint32_t obj_size, uint32_t capaci
// iter // iter
self->iter = queue2_iter; self->iter = queue2_iter;
// -------------------- default --------------------
self->print_obj = default_print_obj;
// -------------------- debug -------------------- // -------------------- debug --------------------
self->print = queue2_print; self->print = queue2_print;

View File

@ -323,7 +323,7 @@ static bool stack_init(struct _stack* self, uint32_t obj_size)
return false; return false;
} }
// ---------- private ---------- // -------------------- private --------------------
self->_obj_size = obj_size; self->_obj_size = obj_size;
self->_size = 0; self->_size = 0;
self->_capacity = UINT32_MAX; self->_capacity = UINT32_MAX;
@ -339,7 +339,7 @@ static bool stack_init(struct _stack* self, uint32_t obj_size)
self->_destory = stack_destory; self->_destory = stack_destory;
// ---------- public ---------- // -------------------- public --------------------
// kernel // kernel
self->push = stack_push; self->push = stack_push;
self->pop = stack_pop; self->pop = stack_pop;
@ -354,8 +354,12 @@ static bool stack_init(struct _stack* self, uint32_t obj_size)
// iter // iter
self->iter = stack_iter; self->iter = stack_iter;
// ---------- debug ---------- // -------------------- default --------------------
self->print_obj = default_print_obj;
// -------------------- debug --------------------
self->print = stack_print; self->print = stack_print;
return true; return true;
} }
@ -367,7 +371,7 @@ static bool stack_init2(struct _stack* self, uint32_t obj_size, uint32_t capacit
return false; return false;
} }
// ---------- private ---------- // -------------------- private --------------------
self->_obj_size = obj_size; self->_obj_size = obj_size;
self->_size = 0; self->_size = 0;
self->_capacity = capacity; 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; self->_destory = stack2_destory;
// ---------- public ---------- // -------------------- public --------------------
// kernel // kernel
self->push = stack2_push; self->push = stack2_push;
self->pop = stack2_pop; self->pop = stack2_pop;
@ -408,8 +412,12 @@ static bool stack_init2(struct _stack* self, uint32_t obj_size, uint32_t capacit
// iter // iter
self->iter = stack_iter; self->iter = stack_iter;
// ---------- debug ---------- // -------------------- default --------------------
self->print_obj = default_print_obj;
// -------------------- debug --------------------
self->print = stack2_print; self->print = stack2_print;
return true; return true;
} }

View File

@ -1420,6 +1420,10 @@ static bool tree_avl_init(struct _tree* self, uint32_t obj_size)
// others // others
self->max = tree_max; self->max = tree_max;
self->min = tree_min; self->min = tree_min;
// -------------------- default --------------------
self->compare = default_compare;
self->print_obj = default_print_obj;
return true; return true;
} }
@ -1469,6 +1473,10 @@ static bool tree_rb_init(struct _tree* self, uint32_t obj_size)
self->max = tree_max; self->max = tree_max;
self->min = tree_min; self->min = tree_min;
// -------------------- default --------------------
self->compare = default_compare;
self->print_obj = default_print_obj;
return true; return true;
} }

View File

@ -8,7 +8,7 @@
* @copyright Copyright (c) 2025 * @copyright Copyright (c) 2025
* *
*/ */
#include "common.h" #include "unicstl_internal.h"
int default_compare(void* obj1, void* obj2) int default_compare(void* obj1, void* obj2)
{ {