From 163f3c55dabeffe843a9ff0cc4a88471b64db4bc Mon Sep 17 00:00:00 2001 From: wjf-hs Date: Fri, 25 Apr 2025 13:39:17 +0800 Subject: [PATCH] =?UTF-8?q?vertex=E7=9A=84=E6=B7=BB=E5=8A=A0=E4=BB=A3?= =?UTF-8?q?=E7=A0=81=E5=AE=9E=E7=8E=B0=E5=B9=B6=E8=B0=83=E8=AF=95=E9=80=9A?= =?UTF-8?q?=E8=BF=87?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- include/graph.h | 11 +++-- src/graph.c | 107 ++++++++++++++++++++++++++++++++++++++++++++-- test/test_graph.c | 31 ++++++++++++-- 3 files changed, 139 insertions(+), 10 deletions(-) diff --git a/include/graph.h b/include/graph.h index e390713..e31ea55 100644 --- a/include/graph.h +++ b/include/graph.h @@ -20,18 +20,18 @@ struct _graph_edge void *target; }; -struct _graph_vertex +struct _graph_node { void* obj; - struct _graph_vertex* next; - struct _graph_edge* edges; + struct _graph_node* next; + struct _graph_edge* edge; bool visited; }; struct _graph { // -------------------- private -------------------- - struct _graph_vertex* _head; + struct _graph_node* _head; uint32_t _size; uint32_t _obj_size; @@ -70,6 +70,9 @@ struct _graph // iter iterator_t (*iter)(struct _graph* self); + // config + compare_fun_t compare; // !!! you have to implement this function + // others bool (*from_matrix)(struct _graph* self, void* obj, uint32_t* edges, uint32_t size); diff --git a/src/graph.c b/src/graph.c index a1a9804..ec121f1 100644 --- a/src/graph.c +++ b/src/graph.c @@ -387,6 +387,14 @@ static void graph_print(struct _graph *self) return; } + printf("vertex : \n"); + struct _graph_node * cur = self->_head->next; + while(cur != NULL) + { + self->print_obj(cur->obj); + cur = cur->next; + } + // printf("\n "); // for(uint32_t i = 0; i < self->_capacity; i++) // { @@ -402,12 +410,103 @@ static void graph_print(struct _graph *self) // } // printf("\n"); // } - // printf("\n"); + + printf("\n"); printf("print done.\n"); } +static struct _graph_node* stack_node_new(void *obj, uint32_t obj_size) +{ + void* new_obj = (void*)calloc(1, obj_size); + if (new_obj == NULL) + { + return NULL; + } + memmove(new_obj, obj, obj_size); + + struct _graph_node* new_node = (struct _graph_node*)malloc(sizeof(struct _graph_node)); + if (new_node == NULL) + { + free(new_obj); + return NULL; + } + new_node->obj = new_obj; + new_node->next = NULL; + new_node->edge = NULL; + new_node->visited = false; + + return new_node; +} + +static void stack_node_free(struct _graph_node** node) +{ + if(node != NULL && *node != NULL) + { + if((*node)->obj != NULL) + { + free((*node)->obj); + (*node)->obj = NULL; + } + + if((*node)->edge != NULL) + { + free((*node)->edge); + (*node)->edge = NULL; + } + + free(*node); + + *node = NULL; + } +} + static bool graph_add_vertex(struct _graph* self, void* obj) { + assert(self != NULL); + if(self->_size >= self->_capacity) + { + return false; + } + + if(self->_head->next == NULL) + { + // no vertex + struct _graph_node * new_node = stack_node_new(obj, self->_obj_size); + if(new_node == NULL) + { + return false; + } + self->_head->next = new_node; + } + else + { + struct _graph_node * cur = self->_head->next; + + // find if exist + while(cur != NULL) + { + if(self->compare((char *)cur->obj, (char *)obj) == 0) + { + return true; + } + cur = cur->next; + } + + struct _graph_node * new_node = stack_node_new(obj, self->_obj_size); + if(new_node == NULL) + { + return false; + } + + // add to tail + cur = self->_head->next; + while(cur->next != NULL) + { + cur = cur->next; + } + cur->next = new_node; + } + return true; } @@ -450,14 +549,14 @@ static bool graph_init(struct _graph *self, uint32_t obj_size) self->_capacity = UINT32_MAX; self->_ratio = 1; - self->_head = (struct _graph_vertex *)malloc(sizeof(struct _graph_vertex)); + self->_head = (struct _graph_node *)malloc(sizeof(struct _graph_node)); if(self->_head == NULL) { return false; } self->_head->visited = false; self->_head->obj = NULL; - self->_head->edges = NULL; + self->_head->edge = NULL; self->_head->next = NULL; self->_destory = graph_destory; @@ -482,6 +581,8 @@ static bool graph_init(struct _graph *self, uint32_t obj_size) self->bfs = NULL; self->dfs = NULL; + self->compare = NULL; + // -------------------- debug -------------------- self->print_obj = NULL; self->print = graph_print; diff --git a/test/test_graph.c b/test/test_graph.c index 15f2564..9b7d200 100644 --- a/test/test_graph.c +++ b/test/test_graph.c @@ -10,15 +10,38 @@ */ #include "test.h" -#if 0 void test_graph_new(void) { - graph_t graph = graph_new2(sizeof(int), 10); + graph_t graph = graph_new(sizeof(int)); TEST_ASSERT_NOT_NULL(graph); graph_free(&graph); TEST_ASSERT_NULL(graph); } +void test_graph_add_vertex(void) +{ + const int size = 10; + int data[10] = { + 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, + }; + uint32_t i = 0; + + graph_t graph = graph_new(sizeof(int)); + TEST_ASSERT_NOT_NULL(graph); + graph->compare = compare_num; + graph->print_obj = print_num; + + for(i = 0; i < size; i++) + { + graph->add_vertex(graph, &data[i]); + } + graph->print(graph); + + graph_free(&graph); + TEST_ASSERT_NULL(graph); +} + +#if 0 void test_graph_print(void) { graph_t graph = graph_new2(sizeof(int), 10); @@ -63,7 +86,9 @@ void test_graph(void) { UnitySetTestFile(__FILE__); - // RUN_TEST(test_graph_new); + RUN_TEST(test_graph_new); + RUN_TEST(test_graph_add_vertex); + // RUN_TEST(test_graph_print); // RUN_TEST(test_graph_from_matrix); }