diff --git a/src/graph.c b/src/graph.c index 42280ec..8021d28 100644 --- a/src/graph.c +++ b/src/graph.c @@ -1,12 +1,12 @@ /** * @file graph.c * @author wenjf (Orig5826@163.com) - * @brief + * @brief * @version 0.1 * @date 2024-09-03 - * + * * @copyright Copyright (c) 2024 - * + * */ #include "graph.h" #include "queue.h" @@ -15,7 +15,7 @@ #if 0 static uint32_t graph_size(struct _graph* self) { - if(self == NULL) + if (self == NULL) { return 0; } @@ -24,22 +24,22 @@ static uint32_t graph_size(struct _graph* self) static uint32_t graph_capacity(struct _graph* self) { - if(self == NULL) + if (self == NULL) { return 0; } return self->_capacity; } -static bool graph_clear(struct _graph *self) +static bool graph_clear(struct _graph* self) { - if(self == NULL && self->_head == NULL) + if (self == NULL && self->_head == NULL) { return 0; } - for(uint32_t i = 0; i < self->_capacity; i++) + for (uint32_t i = 0; i < self->_capacity; i++) { - for(uint32_t j = 0; j < self->_capacity; j++) + for (uint32_t j = 0; j < self->_capacity; j++) { self->_head->edge[i][j] = 0; } @@ -48,30 +48,30 @@ static bool graph_clear(struct _graph *self) return self->_size = 0; } -static void graph_destory(struct _graph *self) +static void graph_destory(struct _graph* self) { - if(self == NULL) + if (self == NULL) { return; } self->clear(self); - if(self->_head != NULL) + if (self->_head != NULL) { - if(self->_head->visited != NULL) + if (self->_head->visited != NULL) { free(self->_head->visited); } - if(self->_head->obj != NULL) + if (self->_head->obj != NULL) { free(self->_head->obj); } - if(self->_head->edge != NULL) + if (self->_head->edge != NULL) { - for(uint32_t i = 0; i < self->_capacity; i++) + for (uint32_t i = 0; i < self->_capacity; i++) { - if(self->_head->edge[i] != NULL) + if (self->_head->edge[i] != NULL) { free(self->_head->edge[i]); } @@ -83,23 +83,23 @@ static void graph_destory(struct _graph *self) } } -static void graph_print(struct _graph *self) +static void graph_print(struct _graph* self) { - if(self == NULL || self->_head == NULL || self->print_obj == NULL) + if (self == NULL || self->_head == NULL || self->print_obj == NULL) { return; } printf("\n "); - for(uint32_t i = 0; i < self->_capacity; i++) + for (uint32_t i = 0; i < self->_capacity; i++) { - self->print_obj((char *)self->_head->obj + i * self->_obj_size); + self->print_obj((char*)self->_head->obj + i * self->_obj_size); } printf("\n"); - for(uint32_t i = 0; i < self->_capacity; i++) + 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++) + 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]); } @@ -109,25 +109,25 @@ static void graph_print(struct _graph *self) printf("print done.\n"); } -static bool graph_from_matrix(struct _graph *self, void *obj, uint32_t *edges, uint32_t size) +static bool graph_from_matrix(struct _graph* self, void* obj, uint32_t* edges, uint32_t size) { - if(self == NULL || self->_head == NULL) + if (self == NULL || self->_head == NULL) { return false; } - if(size > self->_capacity || obj == NULL || edges == NULL) + if (size > self->_capacity || obj == NULL || edges == NULL) { return false; } - for(uint32_t i = 0; i < size; i++) + for (uint32_t i = 0; i < size; i++) { - memmove((char *)self->_head->obj + i * self->_obj_size, (char *)obj + i * self->_obj_size, self->_obj_size); + memmove((char*)self->_head->obj + i * self->_obj_size, (char*)obj + i * self->_obj_size, self->_obj_size); } - for(uint32_t i = 0; i < size; i++) + for (uint32_t i = 0; i < size; i++) { - for(uint32_t j = 0; j < size; j++) + for (uint32_t j = 0; j < size; j++) { self->_head->edge[i][j] = edges[i * size + j]; } @@ -137,19 +137,19 @@ static bool graph_from_matrix(struct _graph *self, void *obj, uint32_t *edges, u return true; } -static bool graph_bfs(struct _graph *self, uint32_t idx) +static bool graph_bfs(struct _graph* self, uint32_t idx) { - if(self == NULL || self->_head == NULL) + if (self == NULL || self->_head == NULL) { return false; } - if(idx >= self->_size || idx >= self->_capacity) + if (idx >= self->_size || idx >= self->_capacity) { return false; } - for(uint32_t i = 0; i < self->_size; i++) + for (uint32_t i = 0; i < self->_size; i++) { self->_head->visited[i] = 0; } @@ -162,11 +162,11 @@ static bool graph_bfs(struct _graph *self, uint32_t idx) { queue->pop(queue, &idx); self->_head->visited[idx] = 1; - for(uint32_t i = 0; i < self->_size; i++) + for (uint32_t i = 0; i < self->_size; i++) { - if(self->_head->edge[idx][i] == 1) + if (self->_head->edge[idx][i] == 1) { - if(self->_head->visited[i] == 0) + if (self->_head->visited[i] == 0) { queue->push(queue, &i); } @@ -184,15 +184,15 @@ static bool graph_bfs(struct _graph *self, uint32_t idx) return true; } -static bool graph_dfs(struct _graph *self, uint32_t idx) +static bool graph_dfs(struct _graph* self, uint32_t idx) { } -static bool graph_init2(struct _graph *self, uint32_t obj_size, uint32_t capacity) +static bool graph_init2(struct _graph* self, uint32_t obj_size, uint32_t capacity) { assert(self != NULL); - if(self == NULL) + if (self == NULL) { return false; } @@ -204,37 +204,37 @@ static bool graph_init2(struct _graph *self, uint32_t obj_size, uint32_t capacit self->_capacity = capacity; self->_ratio = 1; - self->_head = (struct _graph_node *)malloc(sizeof(struct _graph_node)); - if(self->_head == NULL) + self->_head = (struct _graph_node*)malloc(sizeof(struct _graph_node)); + if (self->_head == NULL) { goto done; } - self->_head->obj = (void *)malloc(self->_obj_size); - if(self->_head->obj == NULL) + self->_head->obj = (void*)malloc(self->_obj_size); + if (self->_head->obj == NULL) { goto done1; } - self->_head->edge = (uint32_t **)malloc(self->_capacity * sizeof(uint32_t *)); - if(self->_head->edge == NULL) + self->_head->edge = (uint32_t**)malloc(self->_capacity * sizeof(uint32_t*)); + if (self->_head->edge == NULL) { goto done2; } uint32_t i = 0; - for(i = 0; i < self->_capacity; i++) + for (i = 0; i < self->_capacity; i++) { - self->_head->edge[i] = (uint32_t *)malloc(self->_capacity * sizeof(uint32_t)); - if(self->_head->edge[i] == NULL) + self->_head->edge[i] = (uint32_t*)malloc(self->_capacity * sizeof(uint32_t)); + if (self->_head->edge[i] == NULL) { edges += 1; goto done3; } } - self->_head->visited = (uint8_t *)calloc(1, self->_capacity * sizeof(uint8_t)); - if(self->_head->visited == NULL) + self->_head->visited = (uint8_t*)calloc(1, self->_capacity * sizeof(uint8_t)); + if (self->_head->visited == NULL) { goto done4; } @@ -254,10 +254,10 @@ static bool graph_init2(struct _graph *self, uint32_t obj_size, uint32_t capacit self->print_obj = NULL; self->print = graph_print; - for(uint32_t i = 0; i < self->_capacity; i++) + for (uint32_t i = 0; i < self->_capacity; i++) { - *((int *)self->_head->obj + i) = i; - for(uint32_t j = 0; j < self->_capacity; j++) + *((int*)self->_head->obj + i) = i; + for (uint32_t j = 0; j < self->_capacity; j++) { self->_head->edge[i][j] = 0; } @@ -266,7 +266,7 @@ static bool graph_init2(struct _graph *self, uint32_t obj_size, uint32_t capacit return true; done4: done3: - for(uint32_t j = 0; j < edges; j++) + for (uint32_t j = 0; j < edges; j++) { free(self->_head->edge[j]); } @@ -283,7 +283,7 @@ graph_t graph_new2(uint32_t obj_size, uint32_t capacity) { graph_t graph = NULL; graph = malloc(sizeof(struct _graph)); - if(graph == NULL) + if (graph == NULL) { return NULL; } @@ -296,9 +296,9 @@ graph_t graph_new2(uint32_t obj_size, uint32_t capacity) return graph; } -void graph_free(graph_t *graph) +void graph_free(graph_t* graph) { - if(graph == NULL || *graph == NULL) + if (graph == NULL || *graph == NULL) { return; } @@ -314,7 +314,7 @@ void graph_free(graph_t *graph) -static struct _graph_node* graph_node_new(void *obj, uint32_t obj_size) +static struct _graph_node* graph_node_new(void* obj, uint32_t obj_size) { void* new_obj = (void*)calloc(1, obj_size); if (new_obj == NULL) @@ -339,15 +339,15 @@ static struct _graph_node* graph_node_new(void *obj, uint32_t obj_size) static void greph_node_free(struct _graph_node** node) { - if(node != NULL && *node != NULL) + if (node != NULL && *node != NULL) { - if((*node)->obj != NULL) + if ((*node)->obj != NULL) { free((*node)->obj); (*node)->obj = NULL; } - if((*node)->edge != NULL) + if ((*node)->edge != NULL) { free((*node)->edge); (*node)->edge = NULL; @@ -374,7 +374,7 @@ static struct _graph_edge* graph_edge_new(void) static void greph_edge_free(struct _graph_edge** edge) { - if(edge != NULL && *edge != NULL) + if (edge != NULL && *edge != NULL) { free(*edge); *edge = NULL; @@ -383,7 +383,7 @@ static void greph_edge_free(struct _graph_edge** edge) static uint32_t graph_size(struct _graph* self) { - if(self == NULL) + if (self == NULL) { return 0; } @@ -392,7 +392,7 @@ static uint32_t graph_size(struct _graph* self) static bool graph_empty(struct _graph* self) { - if(self == NULL) + if (self == NULL) { return 0; } @@ -401,23 +401,23 @@ static bool graph_empty(struct _graph* self) static uint32_t graph_capacity(struct _graph* self) { - if(self == NULL) + if (self == NULL) { return 0; } return self->_capacity; } -static bool graph_clear(struct _graph *self) +static bool graph_clear(struct _graph* self) { - if(self == NULL && self->_head == NULL) + if (self == NULL && self->_head == NULL) { return 0; } - struct _graph_node *cur = self->_head->next; - struct _graph_node *next = NULL; - while(cur != NULL) + struct _graph_node* cur = self->_head->next; + struct _graph_node* next = NULL; + while (cur != NULL) { next = cur->next; greph_node_free(&cur); @@ -429,51 +429,55 @@ static bool graph_clear(struct _graph *self) return true; } -static void graph_destory(struct _graph *self) +static void graph_destory(struct _graph* self) { - if(self == NULL) + if (self == NULL) { return; } self->clear(self); - if(self->_head != NULL) + if (self->_head != NULL) { free(self->_head); self->_head = NULL; } } -static void graph_print(struct _graph *self) +static void graph_print(struct _graph* self) { - if(self == NULL || self->_head == NULL || self->print_obj == NULL) + if (self == NULL || self->_head == NULL || self->print_obj == NULL) { return; } printf("vertex : \n"); - struct _graph_node * cur = self->_head->next; - while(cur != NULL) + struct _graph_node* cur = self->_head->next; + while (cur != NULL) { self->print_obj(cur->obj); cur = cur->next; } + printf("\n"); - // 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("edge : \n"); + cur = self->_head->next; + while (cur != NULL) + { + struct _graph_edge* edge = cur->edge; + while (edge != NULL) + { + struct _graph_node* target = (struct _graph_node*)edge->target; + printf("from "); + self->print_obj(cur->obj); + printf(" to "); + self->print_obj(target->obj); + printf(": %d \n", edge->weight); + edge = edge->next; + } + // self->print_obj(cur->obj); + cur = cur->next; + } printf("\n"); printf("print done.\n"); } @@ -481,11 +485,11 @@ static void graph_print(struct _graph *self) static bool graph_add_vertex(struct _graph* self, void* obj) { assert(self != NULL); - if(self->_head->next == NULL) + if (self->_head->next == NULL) { // no vertex - struct _graph_node * new_node = graph_node_new(obj, self->_obj_size); - if(new_node == NULL) + struct _graph_node* new_node = graph_node_new(obj, self->_obj_size); + if (new_node == NULL) { return false; } @@ -493,27 +497,27 @@ static bool graph_add_vertex(struct _graph* self, void* obj) } else { - struct _graph_node * cur = self->_head->next; + struct _graph_node* cur = self->_head->next; // find if exist - while(cur != NULL) + while (cur != NULL) { - if(self->compare(cur->obj, obj) == 0) + if (self->compare(cur->obj, obj) == 0) { return true; } cur = cur->next; } - struct _graph_node * new_node = graph_node_new(obj, self->_obj_size); - if(new_node == NULL) + struct _graph_node* new_node = graph_node_new(obj, self->_obj_size); + if (new_node == NULL) { return false; } // add to tail cur = self->_head->next; - while(cur->next != NULL) + while (cur->next != NULL) { cur = cur->next; } @@ -527,20 +531,20 @@ static bool graph_add_vertex(struct _graph* self, void* obj) static bool graph_del_vertex(struct _graph* self, void* obj) { assert(self != NULL); - if(obj == NULL) + if (obj == NULL) { return false; } - if(self->empty(self)) + if (self->empty(self)) { return false; } - struct _graph_node * cur = self->_head->next; - struct _graph_node * pre = self->_head; - while(cur != NULL) + struct _graph_node* cur = self->_head->next; + struct _graph_node* pre = self->_head; + while (cur != NULL) { - if(self->compare(cur->obj, obj) == 0) + if (self->compare(cur->obj, obj) == 0) { pre->next = cur->next; break; @@ -548,7 +552,7 @@ static bool graph_del_vertex(struct _graph* self, void* obj) pre = cur; cur = cur->next; } - if(cur == NULL) + if (cur == NULL) { return false; } @@ -561,18 +565,18 @@ static bool graph_del_vertex(struct _graph* self, void* obj) static bool graph_find_vertex(struct _graph* self, void* obj) { assert(self != NULL); - if(obj == NULL) + if (obj == NULL) { return false; } - if(self->empty(self)) + if (self->empty(self)) { return false; } - struct _graph_node * cur = self->_head->next; - while(cur != NULL) + struct _graph_node* cur = self->_head->next; + while (cur != NULL) { - if(self->compare(cur->obj, obj) == 0) + if (self->compare(cur->obj, obj) == 0) { // obj is found break; @@ -580,15 +584,15 @@ static bool graph_find_vertex(struct _graph* self, void* obj) cur = cur->next; } - return cur == NULL ? false: true; + return cur == NULL ? false : true; } static struct _graph_node* find_node(struct _graph* self, void* obj) { - struct _graph_node * cur = self->_head->next; - while(cur != NULL) + struct _graph_node* cur = self->_head->next; + while (cur != NULL) { - if(self->compare(cur->obj, obj) == 0) + if (self->compare(cur->obj, obj) == 0) { // obj is found break; @@ -602,30 +606,30 @@ static struct _graph_node* find_node(struct _graph* self, void* obj) static bool graph_add_edge(struct _graph* self, void* from, void* to, uint32_t weight) { assert(self != NULL); - if(self->empty(self)) + if (self->empty(self)) { return false; } - struct _graph_node * from_node = self->_head->next; - struct _graph_node * to_node = self->_head->next; + struct _graph_node* from_node = self->_head->next; + struct _graph_node* to_node = self->_head->next; from_node = find_node(self, from); - if(from_node == NULL) + if (from_node == NULL) { return false; } to_node = find_node(self, from); - if(to_node == NULL) + if (to_node == NULL) { return false; } // add edge - if(from_node->edge == NULL) + if (from_node->edge == NULL) { from_node->edge = graph_edge_new(); - if(from_node->edge == NULL) + if (from_node->edge == NULL) { return false; } @@ -637,22 +641,22 @@ static bool graph_add_edge(struct _graph* self, void* from, void* to, uint32_t w struct _graph_edge* next_edge = from_node->edge->next; struct _graph_edge* pre_edge = NULL; struct _graph_edge* new_edge = graph_edge_new(); - if(new_edge == NULL) + if (new_edge == NULL) { return false; } new_edge->weight = weight; new_edge->target = to_node; - + new_edge->next = next_edge; from_node->edge->next = new_edge; } // if graph is undirected - if(to_node->edge == NULL) + if (to_node->edge == NULL) { to_node->edge = graph_edge_new(); - if(to_node->edge == NULL) + if (to_node->edge == NULL) { return false; } @@ -664,17 +668,17 @@ static bool graph_add_edge(struct _graph* self, void* from, void* to, uint32_t w struct _graph_edge* next_edge = to_node->edge->next; struct _graph_edge* pre_edge = NULL; struct _graph_edge* new_edge = graph_edge_new(); - if(new_edge == NULL) + if (new_edge == NULL) { return false; } new_edge->weight = weight; new_edge->target = from_node; - + new_edge->next = next_edge; to_node->edge->next = new_edge; } - + return true; } @@ -688,10 +692,10 @@ static bool graph_find_edge(struct _graph* self, void* from, void* to) return true; } -static bool graph_init(struct _graph *self, uint32_t obj_size) +static bool graph_init(struct _graph* self, uint32_t obj_size) { assert(self != NULL); - if(self == NULL) + if (self == NULL) { return false; } @@ -702,12 +706,12 @@ static bool graph_init(struct _graph *self, uint32_t obj_size) self->_capacity = UINT32_MAX; self->_ratio = 1; - self->_head = (struct _graph_node *)malloc(sizeof(struct _graph_node)); - if(self->_head == NULL) + self->_head = (struct _graph_node*)malloc(sizeof(struct _graph_node)); + if (self->_head == NULL) { return false; } - self->_head->visited = false; + self->_head->visited = false; self->_head->obj = NULL; self->_head->edge = NULL; self->_head->next = NULL; @@ -748,12 +752,12 @@ graph_t graph_new(uint32_t obj_size) { graph_t graph = NULL; graph = malloc(sizeof(struct _graph)); - if(graph == NULL) + if (graph == NULL) { return NULL; } - if(graph_init(graph, obj_size) != true) + if (graph_init(graph, obj_size) != true) { free(graph); return NULL; @@ -761,9 +765,9 @@ graph_t graph_new(uint32_t obj_size) return graph; } -void graph_free(graph_t *graph) +void graph_free(graph_t* graph) { - if(graph == NULL || *graph == NULL) + if (graph == NULL || *graph == NULL) { return; } diff --git a/test/test_graph.c b/test/test_graph.c index 2715dd3..0018679 100644 --- a/test/test_graph.c +++ b/test/test_graph.c @@ -69,6 +69,40 @@ void test_graph_add_vertex(void) TEST_ASSERT_NULL(graph); } + +void test_graph_add_edge(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)); + TEST_ASSERT_NOT_NULL(graph); + graph->compare = compare_num; + graph->print_obj = print_num; + + // test add_vertex + for(i = 0; i < size; i++) + { + TEST_ASSERT_TRUE(graph->add_vertex(graph, &data[i])); + } + graph->print(graph); + + // test add_edge + TEST_ASSERT_TRUE(graph->add_edge(graph, &data[0], &data[1], 0)); + TEST_ASSERT_TRUE(graph->add_edge(graph, &data[0], &data[2], 0)); + TEST_ASSERT_TRUE(graph->add_edge(graph, &data[1], &data[3], 0)); + graph->print(graph); + + TEST_ASSERT_FALSE(graph->add_edge(graph, &temp, &data[1], 0)); + + graph_free(&graph); + TEST_ASSERT_NULL(graph); +} + #if 0 void test_graph_print(void) { @@ -116,6 +150,7 @@ void test_graph(void) RUN_TEST(test_graph_new); RUN_TEST(test_graph_add_vertex); + RUN_TEST(test_graph_add_edge); // RUN_TEST(test_graph_print); // RUN_TEST(test_graph_from_matrix);