diff --git a/.vscode/settings.json b/.vscode/settings.json index d93da67..d113c91 100644 --- a/.vscode/settings.json +++ b/.vscode/settings.json @@ -22,6 +22,7 @@ "unity.h": "c", "unity_config.h": "c", "unity_internals.h": "c", - "stdarg.h": "c" + "stdarg.h": "c", + "graph.h": "c" } } \ No newline at end of file diff --git a/include/graph.h b/include/graph.h index 5d4ed8e..c1033ff 100644 --- a/include/graph.h +++ b/include/graph.h @@ -15,7 +15,7 @@ struct _graph_node { void *obj; - struct _graph_node *next; + uint32_t **edge; }; struct _graph { diff --git a/include/unicstl.h b/include/unicstl.h index e8b281c..7ec3da5 100644 --- a/include/unicstl.h +++ b/include/unicstl.h @@ -17,5 +17,6 @@ #include "deque.h" #include "tree.h" #include "heap.h" +#include "graph.h" #endif // _UNICSTL_H_ diff --git a/src/graph.c b/src/graph.c index db7dbc7..b842078 100644 --- a/src/graph.c +++ b/src/graph.c @@ -34,7 +34,14 @@ static bool graph_clear(struct _graph *self) { return 0; } - self->_head->obj = NULL; + for(uint32_t i = 0; i < self->_capacity; i++) + { + for(uint32_t j = 0; j < self->_capacity; j++) + { + self->_head->edge[i][j] = 0; + } + } + self->_size = 0; return self->_size = 0; } @@ -47,11 +54,43 @@ static void graph_destory(struct _graph *self) self->clear(self); if(self->_head != NULL) { + if(self->_head->obj != NULL) + { + free(self->_head->obj); + } + + if(self->_head->edge != NULL) + { + for(uint32_t i = 0; i < self->_capacity; i++) + { + if(self->_head->edge[i] != NULL) + { + free(self->_head->edge[i]); + } + } + free(self->_head->edge); + } free(self->_head); self->_head = NULL; } } +static void graph_init2(struct _graph *self) +{ + if(self == NULL || self->_head == NULL) + { + return; + } + + for(uint32_t i = 0; i < self->_capacity; i++) + { + for(uint32_t j = 0; j < self->_capacity; j++) + { + self->_head->edge[i][j] = 0; + } + } +} + graph_t graph_new2(uint32_t obj_size, uint32_t capacity) { if(obj_size == 0 || capacity == 0) @@ -70,28 +109,53 @@ graph_t graph_new2(uint32_t obj_size, uint32_t capacity) graph->_capacity = capacity; graph->_ratio = 1; + graph->init = graph_init2; + graph->destory = graph_destory; + graph->size = graph_size; graph->capacity = graph_capacity; + graph->clear = graph_clear; graph->_head = (struct _graph_node *)malloc(sizeof(struct _graph_node)); if(graph->_head == NULL) { goto done1; } - graph->_head->next = NULL; - graph->_head->obj = (void *)malloc(graph->_obj_size * graph->_capacity * graph->_capacity); + graph->_head->obj = (void *)malloc(graph->_obj_size); if(graph->_head->obj == NULL) { goto done2; } + graph->_head->edge = (uint32_t **)malloc(graph->_capacity * sizeof(uint32_t *)); + if(graph->_head->edge == NULL) + { + goto done3; + } + + uint32_t i = 0; + for(i = 0; i < graph->_capacity; i++) + { + graph->_head->edge[i] = (uint32_t *)malloc(graph->_capacity * sizeof(uint32_t)); + if(graph->_head->edge[i] == NULL) + { + goto done4; + } + } + // init graph graph->init(graph); return graph; - - // free(graph->_head->obj) +done4: + for(uint32_t j = 0; j < i; j++) + { + free(graph->_head->edge[j]); + } + free(graph->_head->edge); +done3: + free(graph->_head->obj); done2: free(graph->_head); done1: diff --git a/test/test.c b/test/test.c index e046aaf..846e1e2 100644 --- a/test/test.c +++ b/test/test.c @@ -84,6 +84,7 @@ int main(int argc, char const *argv[]) TEST_ADD(test_deque); TEST_ADD(test_heap); TEST_ADD(test_tree); + TEST_ADD(test_graph); return UNITY_END(); } diff --git a/test/test.h b/test/test.h index a8ec0db..514c0af 100644 --- a/test/test.h +++ b/test/test.h @@ -47,5 +47,6 @@ void test_list(void); void test_deque(void); void test_tree(void); void test_heap(void); +void test_graph(void); #endif // _TEST_H_ diff --git a/test/test_graph.c b/test/test_graph.c new file mode 100644 index 0000000..2047d4e --- /dev/null +++ b/test/test_graph.c @@ -0,0 +1,24 @@ +/** + * @file test_graph.c + * @author wenjf (Orig5826@163.com) + * @brief + * @version 0.1 + * @date 2024-09-03 + * + * @copyright Copyright (c) 2024 + * + */ +#include "test.h" + +void test_graph_new(void) +{ + graph_t g = graph_new2(sizeof(int), 10); + TEST_ASSERT(g != NULL); + graph_free(&g); + TEST_ASSERT(g == NULL); +} + +void test_graph(void) +{ + RUN_TEST(test_graph_new); +}