From 0ac0976bb6c67fc83f7941d0da88835b70480e5d Mon Sep 17 00:00:00 2001 From: wjf-hs Date: Tue, 3 Sep 2024 12:49:08 +0800 Subject: [PATCH] =?UTF-8?q?=E6=B7=BB=E5=8A=A0=E6=89=93=E5=8D=B0=E4=BF=A1?= =?UTF-8?q?=E6=81=AF?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- include/graph.h | 4 ++++ src/graph.c | 29 +++++++++++++++++++++++++++++ test/test_graph.c | 21 +++++++++++++++++---- 3 files changed, 50 insertions(+), 4 deletions(-) diff --git a/include/graph.h b/include/graph.h index c1033ff..767974e 100644 --- a/include/graph.h +++ b/include/graph.h @@ -29,6 +29,7 @@ struct _graph { // init void (*init)(struct _graph *self); + void (*from_matrix)(struct _graph *self, uint32_t **edges, uint32_t size); // kernel bool (*add)(struct _graph *self, void *obj); @@ -45,6 +46,9 @@ struct _graph { // bool (*clear)(struct _graph *self); void (*destory)(struct _graph *self); + + void (*print)(struct _graph *self); + void (*print_obj)(void *obj); }; typedef struct _graph* graph_t; diff --git a/src/graph.c b/src/graph.c index b842078..e152e1f 100644 --- a/src/graph.c +++ b/src/graph.c @@ -75,6 +75,31 @@ static void graph_destory(struct _graph *self) } } +static void graph_print(struct _graph *self) +{ + if(self == NULL || self->_head == NULL || self->print_obj == NULL) + { + return; + } + + printf("\n "); + for(uint32_t i = 0; i < self->_capacity; i++) + { + self->print_obj((char *)self->_head->obj + i * self->_obj_size); + } + printf("\n"); + for(uint32_t i = 0; i < self->_capacity; i++) + { + self->print_obj((char *)self->_head->obj + i * self->_obj_size); + for(uint32_t j = 0; j < self->_capacity; j++) + { + printf(" %2d ", self->_head->edge[i][j]); + } + printf("\n"); + } + printf("\n"); +} + static void graph_init2(struct _graph *self) { if(self == NULL || self->_head == NULL) @@ -84,6 +109,7 @@ static void graph_init2(struct _graph *self) for(uint32_t i = 0; i < self->_capacity; i++) { + *((int *)self->_head->obj + i) = i; for(uint32_t j = 0; j < self->_capacity; j++) { self->_head->edge[i][j] = 0; @@ -116,6 +142,9 @@ graph_t graph_new2(uint32_t obj_size, uint32_t capacity) graph->capacity = graph_capacity; graph->clear = graph_clear; + graph->print_obj = NULL; + graph->print = graph_print; + graph->_head = (struct _graph_node *)malloc(sizeof(struct _graph_node)); if(graph->_head == NULL) { diff --git a/test/test_graph.c b/test/test_graph.c index 2047d4e..a81fcec 100644 --- a/test/test_graph.c +++ b/test/test_graph.c @@ -12,13 +12,26 @@ void test_graph_new(void) { - graph_t g = graph_new2(sizeof(int), 10); - TEST_ASSERT(g != NULL); - graph_free(&g); - TEST_ASSERT(g == NULL); + graph_t graph = graph_new2(sizeof(int), 10); + TEST_ASSERT_NOT_NULL(graph); + graph_free(&graph); + TEST_ASSERT_NULL(graph); +} + +void test_graph_print(void) +{ + graph_t graph = graph_new2(sizeof(int), 10); + TEST_ASSERT_NOT_NULL(graph); + graph->print_obj = print_num; + + graph->print(graph); + + graph_free(&graph); + TEST_ASSERT_NULL(graph); } void test_graph(void) { RUN_TEST(test_graph_new); + RUN_TEST(test_graph_print); }