Compare commits

..

No commits in common. "df006ba37adaf3a7e3a9d48412aa9449a6d83ff2" and "32f2605656883c1a1c7810f0f644d0e2df0cf374" have entirely different histories.

23 changed files with 55 additions and 154 deletions

View File

@ -29,7 +29,6 @@
"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,7 +64,6 @@ 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,7 +45,6 @@ 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_

View File

@ -1,67 +0,0 @@
/**
* @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

@ -200,6 +200,7 @@ static void demo_stack_struct(void)
} }
} }
stack->_destory(stack);
stack_free(&stack); stack_free(&stack);
} }

View File

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

View File

@ -11,7 +11,7 @@
#ifndef _DEQUE_H_ #ifndef _DEQUE_H_
#define _DEQUE_H_ #define _DEQUE_H_
#include "unicstl_internal.h" #include "common.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 "unicstl_internal.h" #include "common.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 "unicstl_internal.h" #include "common.h"
typedef enum typedef enum
{ {

View File

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

View File

@ -15,7 +15,7 @@
#ifndef _LIST_H_ #ifndef _LIST_H_
#define _LIST_H_ #define _LIST_H_
#include "unicstl_internal.h" #include "common.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 "unicstl_internal.h" #include "common.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 "unicstl_internal.h" #include "common.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 "unicstl_internal.h" #include "common.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 "unicstl_internal.h" #include "common.h"
#include "list.h" #include "list.h"
#include "stack.h" #include "stack.h"

View File

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

View File

@ -420,9 +420,6 @@ 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;
@ -486,9 +483,6 @@ 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

@ -124,6 +124,7 @@ static uint32_t stack_capacity(struct _stack* self)
static bool stack_clear(struct _stack* self) static bool stack_clear(struct _stack* self)
{ {
assert(self != NULL); assert(self != NULL);
assert(self->_head != NULL);
if (self->empty(self)) if (self->empty(self))
{ {
return true; return true;
@ -322,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;
@ -338,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;
@ -353,12 +354,8 @@ static bool stack_init(struct _stack* self, uint32_t obj_size)
// iter // iter
self->iter = stack_iter; self->iter = stack_iter;
// -------------------- default -------------------- // ---------- debug ----------
self->print_obj = default_print_obj;
// -------------------- debug --------------------
self->print = stack_print; self->print = stack_print;
return true; return true;
} }
@ -370,7 +367,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;
@ -396,7 +393,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;
@ -411,12 +408,8 @@ static bool stack_init2(struct _stack* self, uint32_t obj_size, uint32_t capacit
// iter // iter
self->iter = stack_iter; self->iter = stack_iter;
// -------------------- default -------------------- // ---------- debug ----------
self->print_obj = default_print_obj;
// -------------------- debug --------------------
self->print = stack2_print; self->print = stack2_print;
return true; return true;
} }

View File

@ -1420,10 +1420,6 @@ 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;
} }
@ -1473,10 +1469,6 @@ 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 "unicstl_internal.h" #include "common.h"
int default_compare(void* obj1, void* obj2) int default_compare(void* obj1, void* obj2)
{ {