mirror of
https://gitee.com/apaki/unicstl.git
synced 2025-05-17 19:41:36 +08:00
Compare commits
2 Commits
32f2605656
...
df006ba37a
Author | SHA1 | Date | |
---|---|---|---|
df006ba37a | |||
9322db6b62 |
3
.vscode/settings.json
vendored
3
.vscode/settings.json
vendored
@ -29,6 +29,7 @@
|
||||
"iterator.h": "c",
|
||||
"assert.h": "c",
|
||||
"limits.h": "c",
|
||||
"cstdint": "c"
|
||||
"cstdint": "c",
|
||||
"unicstl_internal.h": "c"
|
||||
}
|
||||
}
|
@ -64,6 +64,7 @@ int main()
|
||||
demo_deque();
|
||||
demo_tree();
|
||||
demo_heap();
|
||||
demo_graph();
|
||||
}
|
||||
|
||||
printf("----- unicstl ok -----\n");
|
||||
|
@ -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_
|
||||
|
||||
|
67
demo/demo_graph.c
Normal file
67
demo/demo_graph.c
Normal 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();
|
||||
}
|
@ -200,7 +200,6 @@ static void demo_stack_struct(void)
|
||||
}
|
||||
}
|
||||
|
||||
stack->_destory(stack);
|
||||
stack_free(&stack);
|
||||
}
|
||||
|
||||
|
@ -11,7 +11,7 @@
|
||||
#ifndef _DEQUE_H_
|
||||
#define _DEQUE_H_
|
||||
|
||||
#include "common.h"
|
||||
#include "unicstl_internal.h"
|
||||
|
||||
enum _deque_order
|
||||
{
|
||||
|
@ -11,7 +11,7 @@
|
||||
#ifndef _GRAPH_H
|
||||
#define _GRAPH_H
|
||||
|
||||
#include "common.h"
|
||||
#include "unicstl_internal.h"
|
||||
|
||||
#include "stack.h"
|
||||
#include "queue.h"
|
||||
|
@ -11,7 +11,7 @@
|
||||
#ifndef _HEAP_H_
|
||||
#define _HEAP_H_
|
||||
|
||||
#include "common.h"
|
||||
#include "unicstl_internal.h"
|
||||
|
||||
typedef enum
|
||||
{
|
||||
|
@ -11,7 +11,7 @@
|
||||
#ifndef _ITER_H_
|
||||
#define _ITER_H_
|
||||
|
||||
#include "common.h"
|
||||
#include "unicstl_internal.h"
|
||||
|
||||
struct _iterator
|
||||
{
|
||||
|
@ -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
|
||||
|
@ -11,7 +11,7 @@
|
||||
#ifndef _QUEUE_H_
|
||||
#define _QUEUE_H_
|
||||
|
||||
#include "common.h"
|
||||
#include "unicstl_internal.h"
|
||||
|
||||
struct _queue_node
|
||||
{
|
||||
|
@ -11,7 +11,7 @@
|
||||
#ifndef _STACK_H_
|
||||
#define _STACK_H_
|
||||
|
||||
#include "common.h"
|
||||
#include "unicstl_internal.h"
|
||||
|
||||
struct _stack_node
|
||||
{
|
||||
|
@ -11,7 +11,7 @@
|
||||
#ifndef _TREE_H_
|
||||
#define _TREE_H_
|
||||
|
||||
#include "common.h"
|
||||
#include "unicstl_internal.h"
|
||||
|
||||
#include "stack.h"
|
||||
#include "queue.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"
|
||||
|
@ -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_
|
@ -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;
|
||||
|
||||
|
@ -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;
|
||||
|
@ -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;
|
||||
|
63
src/list.c
63
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;
|
||||
}
|
||||
|
@ -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;
|
||||
|
||||
|
21
src/stack.c
21
src/stack.c
@ -124,7 +124,6 @@ static uint32_t stack_capacity(struct _stack* self)
|
||||
static bool stack_clear(struct _stack* self)
|
||||
{
|
||||
assert(self != NULL);
|
||||
assert(self->_head != NULL);
|
||||
if (self->empty(self))
|
||||
{
|
||||
return true;
|
||||
@ -323,7 +322,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 +338,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 +353,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 +370,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 +396,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 +411,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;
|
||||
}
|
||||
|
||||
|
@ -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;
|
||||
}
|
||||
|
||||
|
@ -8,7 +8,7 @@
|
||||
* @copyright Copyright (c) 2025
|
||||
*
|
||||
*/
|
||||
#include "common.h"
|
||||
#include "unicstl_internal.h"
|
||||
|
||||
int default_compare(void* obj1, void* obj2)
|
||||
{
|
||||
|
Loading…
Reference in New Issue
Block a user