mirror of
https://gitee.com/apaki/unicstl.git
synced 2025-07-06 16:56:54 +08:00
Compare commits
No commits in common. "5e83dc291785394297cc165da9f301eed16a2f5d" and "3ac92de18e732845f7f27ae8cddcde536d1ed0a0" have entirely different histories.
5e83dc2917
...
3ac92de18e
@ -13,9 +13,6 @@
|
|||||||
|
|
||||||
#include "common.h"
|
#include "common.h"
|
||||||
|
|
||||||
#include "stack.h"
|
|
||||||
#include "queue.h"
|
|
||||||
|
|
||||||
enum _graph_type
|
enum _graph_type
|
||||||
{
|
{
|
||||||
GRAPH_UNDIRECTED,
|
GRAPH_UNDIRECTED,
|
||||||
@ -24,13 +21,6 @@ enum _graph_type
|
|||||||
// GRAPH_DIRECTED_WEIGHT,
|
// GRAPH_DIRECTED_WEIGHT,
|
||||||
};
|
};
|
||||||
|
|
||||||
enum _graph_search
|
|
||||||
{
|
|
||||||
GRAPH_DFS,
|
|
||||||
GRAPH_BFS,
|
|
||||||
};
|
|
||||||
|
|
||||||
|
|
||||||
struct _graph_edge
|
struct _graph_edge
|
||||||
{
|
{
|
||||||
uint32_t weight;
|
uint32_t weight;
|
||||||
@ -57,11 +47,6 @@ struct _graph
|
|||||||
uint32_t _ratio;
|
uint32_t _ratio;
|
||||||
|
|
||||||
enum _graph_type _type;
|
enum _graph_type _type;
|
||||||
enum _graph_search _search;
|
|
||||||
|
|
||||||
stack_t stack;
|
|
||||||
queue_t queue;
|
|
||||||
|
|
||||||
struct _iterator _iter;
|
struct _iterator _iter;
|
||||||
|
|
||||||
void (*_destory)(struct _graph* self);
|
void (*_destory)(struct _graph* self);
|
||||||
@ -92,7 +77,7 @@ struct _graph
|
|||||||
bool (*full)(struct _graph* self);
|
bool (*full)(struct _graph* self);
|
||||||
|
|
||||||
// iter
|
// iter
|
||||||
iterator_t (*iter)(struct _graph* self, enum _graph_search search_type, void *obj);
|
iterator_t (*iter)(struct _graph* self);
|
||||||
|
|
||||||
// config
|
// config
|
||||||
compare_fun_t compare; // !!! you have to implement this function
|
compare_fun_t compare; // !!! you have to implement this function
|
||||||
|
238
src/graph.c
238
src/graph.c
@ -310,6 +310,10 @@ void graph_free(graph_t* graph)
|
|||||||
|
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
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);
|
void* new_obj = (void*)calloc(1, obj_size);
|
||||||
@ -437,15 +441,6 @@ static void graph_destory(struct _graph *self)
|
|||||||
free(self->_head);
|
free(self->_head);
|
||||||
self->_head = NULL;
|
self->_head = NULL;
|
||||||
}
|
}
|
||||||
|
|
||||||
if (self->stack != NULL)
|
|
||||||
{
|
|
||||||
stack_free(&self->stack);
|
|
||||||
}
|
|
||||||
if (self->queue != NULL)
|
|
||||||
{
|
|
||||||
queue_free(&self->queue);
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
static void graph_print(struct _graph* self)
|
static void graph_print(struct _graph* self)
|
||||||
@ -613,6 +608,7 @@ static struct _graph_node *find_node(struct _graph *self, void *obj)
|
|||||||
return cur;
|
return cur;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
static bool graph_add_edge(struct _graph* self, void* from, void* to, uint32_t weight)
|
static bool graph_add_edge(struct _graph* self, void* from, void* to, uint32_t weight)
|
||||||
{
|
{
|
||||||
assert(self != NULL);
|
assert(self != NULL);
|
||||||
@ -776,209 +772,6 @@ static bool graph_find_edge(struct _graph *self, void *from, void *to)
|
|||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
iterator_t graph_iter(struct _graph *self, enum _graph_search search_type, void *start)
|
|
||||||
{
|
|
||||||
assert(self != NULL);
|
|
||||||
iterator_t iter = &self->_iter;
|
|
||||||
|
|
||||||
iter->_parent = self;
|
|
||||||
iter->_cur = 0;
|
|
||||||
iter->_cur_node = self->_head->next;
|
|
||||||
|
|
||||||
struct _graph_node *start_node = find_node(self, start);
|
|
||||||
if (start_node == NULL)
|
|
||||||
{
|
|
||||||
goto done;
|
|
||||||
}
|
|
||||||
iter->_cur_node = start_node;
|
|
||||||
|
|
||||||
struct _graph_node *node = self->_head->next;
|
|
||||||
while (node != NULL)
|
|
||||||
{
|
|
||||||
node->visited = false;
|
|
||||||
node = node->next;
|
|
||||||
}
|
|
||||||
|
|
||||||
self->_search = search_type;
|
|
||||||
switch (self->_search)
|
|
||||||
{
|
|
||||||
case GRAPH_BFS:
|
|
||||||
{
|
|
||||||
self->queue->push(self->queue, &iter->_cur_node);
|
|
||||||
}
|
|
||||||
break;
|
|
||||||
case GRAPH_DFS:
|
|
||||||
{
|
|
||||||
// pass
|
|
||||||
}
|
|
||||||
break;
|
|
||||||
default:
|
|
||||||
{
|
|
||||||
}
|
|
||||||
break;
|
|
||||||
}
|
|
||||||
|
|
||||||
done:
|
|
||||||
return iter;
|
|
||||||
}
|
|
||||||
|
|
||||||
bool graph_iter_hasnext(struct _iterator *iter)
|
|
||||||
{
|
|
||||||
assert(iter != NULL);
|
|
||||||
assert(iter->parent != NULL);
|
|
||||||
|
|
||||||
graph_t self = (graph_t)iter->_parent;
|
|
||||||
if (iter->_cur < self->size(self))
|
|
||||||
{
|
|
||||||
return true;
|
|
||||||
}
|
|
||||||
return false;
|
|
||||||
}
|
|
||||||
|
|
||||||
const void *graph_iter_next(struct _iterator *iter)
|
|
||||||
{
|
|
||||||
assert(iter != NULL);
|
|
||||||
assert(iter->parent != NULL);
|
|
||||||
|
|
||||||
graph_t self = (graph_t)iter->_parent;
|
|
||||||
void *obj = NULL;
|
|
||||||
|
|
||||||
iter->_cur += 1;
|
|
||||||
switch (self->_search)
|
|
||||||
{
|
|
||||||
case GRAPH_BFS:
|
|
||||||
{
|
|
||||||
struct _graph_node *cur_node = iter->_cur_node;
|
|
||||||
struct _graph_edge *cur_edge = cur_node->edgehead;
|
|
||||||
struct _graph_node *target = NULL;
|
|
||||||
struct _graph_node *node = cur_node;
|
|
||||||
|
|
||||||
queue_t queue = self->queue;
|
|
||||||
|
|
||||||
if (queue->empty(queue))
|
|
||||||
{
|
|
||||||
cur_node = self->_head->next;
|
|
||||||
while (cur_node != NULL)
|
|
||||||
{
|
|
||||||
if (cur_node->visited != true)
|
|
||||||
{
|
|
||||||
queue->push(queue, &cur_node);
|
|
||||||
break;
|
|
||||||
}
|
|
||||||
cur_node = cur_node->next;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
if (!queue->empty(queue) && node != NULL)
|
|
||||||
{
|
|
||||||
queue->pop(queue, &node);
|
|
||||||
node->visited = true;
|
|
||||||
|
|
||||||
cur_edge = node->edgehead;
|
|
||||||
while (cur_edge != NULL)
|
|
||||||
{
|
|
||||||
target = cur_edge->target;
|
|
||||||
if (target != NULL && target->visited != true)
|
|
||||||
{
|
|
||||||
queue->push(queue, &target);
|
|
||||||
|
|
||||||
// self->print_obj(node->obj);
|
|
||||||
// printf(" -> ");
|
|
||||||
// self->print_obj(target->obj);
|
|
||||||
}
|
|
||||||
cur_edge = cur_edge->next;
|
|
||||||
}
|
|
||||||
|
|
||||||
cur_node = node;
|
|
||||||
}
|
|
||||||
|
|
||||||
iter->_cur_node = cur_node;
|
|
||||||
obj = cur_node->obj;
|
|
||||||
}
|
|
||||||
break;
|
|
||||||
case GRAPH_DFS:
|
|
||||||
{
|
|
||||||
// self->stack->push(self->stack, iter->_cur_node);
|
|
||||||
struct _graph_node *cur_node = self->_iter._cur_node;
|
|
||||||
struct _graph_node *node = NULL;
|
|
||||||
struct _graph_edge *cur_edge = cur_node->edgehead;
|
|
||||||
struct _graph_node *target = NULL;
|
|
||||||
|
|
||||||
stack_t stack = self->stack;
|
|
||||||
|
|
||||||
// while (!self->stack->empty(self->stack) || cur_node != NULL)
|
|
||||||
do
|
|
||||||
{
|
|
||||||
if(cur_node == NULL)
|
|
||||||
{
|
|
||||||
break;
|
|
||||||
}
|
|
||||||
|
|
||||||
if(cur_node->visited == true)
|
|
||||||
{
|
|
||||||
stack->pop(stack, &cur_node);
|
|
||||||
cur_node->visited = true;
|
|
||||||
node = cur_node;
|
|
||||||
break;
|
|
||||||
}
|
|
||||||
|
|
||||||
bool accessible_vertex_exists = false;
|
|
||||||
|
|
||||||
// 1. add cur_node to stack
|
|
||||||
stack->push(stack, &cur_node);
|
|
||||||
|
|
||||||
// 2. find the first unvisited vertex
|
|
||||||
cur_edge = cur_node->edgehead;
|
|
||||||
while(cur_edge != NULL)
|
|
||||||
{
|
|
||||||
target = cur_edge->target;
|
|
||||||
if(target != NULL && target->visited != true)
|
|
||||||
{
|
|
||||||
accessible_vertex_exists = true;
|
|
||||||
|
|
||||||
cur_node = target;
|
|
||||||
break;
|
|
||||||
}
|
|
||||||
cur_edge = cur_edge->next;
|
|
||||||
}
|
|
||||||
|
|
||||||
iterator_t iter = stack->iter(stack);
|
|
||||||
struct _graph_node *temp_node = NULL;
|
|
||||||
while (iter->hasnext(iter))
|
|
||||||
{
|
|
||||||
temp_node = *(struct _graph_node **)iter->next(iter);
|
|
||||||
self->print_obj(temp_node->obj);
|
|
||||||
}
|
|
||||||
printf("\n");
|
|
||||||
|
|
||||||
if(accessible_vertex_exists != true ||
|
|
||||||
cur_edge == NULL)
|
|
||||||
{
|
|
||||||
// if no accessible vertex, pop from stack
|
|
||||||
stack->pop(stack, &node);
|
|
||||||
node->visited = true;
|
|
||||||
|
|
||||||
stack->peek(stack, &cur_node);
|
|
||||||
break;
|
|
||||||
}
|
|
||||||
}while(!stack->empty(stack));
|
|
||||||
|
|
||||||
printf("--- end ---\n");
|
|
||||||
iter->_cur_node = cur_node;
|
|
||||||
printf("--- end1 ---\n");
|
|
||||||
obj = node->obj;
|
|
||||||
printf("--- end2 ---\n");
|
|
||||||
}
|
|
||||||
break;
|
|
||||||
default:
|
|
||||||
{
|
|
||||||
}
|
|
||||||
break;
|
|
||||||
}
|
|
||||||
|
|
||||||
return obj;
|
|
||||||
}
|
|
||||||
|
|
||||||
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);
|
assert(self != NULL);
|
||||||
@ -993,23 +786,9 @@ static bool graph_init(struct _graph *self, uint32_t obj_size)
|
|||||||
self->_capacity = UINT32_MAX;
|
self->_capacity = UINT32_MAX;
|
||||||
self->_ratio = 1;
|
self->_ratio = 1;
|
||||||
|
|
||||||
self->stack = stack_new(sizeof(struct _graph_node *));
|
|
||||||
if (self->stack == NULL)
|
|
||||||
{
|
|
||||||
return false;
|
|
||||||
}
|
|
||||||
self->queue = queue_new(sizeof(struct _graph_node *));
|
|
||||||
if (self->queue == NULL)
|
|
||||||
{
|
|
||||||
stack_free(&self->stack);
|
|
||||||
return false;
|
|
||||||
}
|
|
||||||
|
|
||||||
self->_head = (struct _graph_node*)malloc(sizeof(struct _graph_node));
|
self->_head = (struct _graph_node*)malloc(sizeof(struct _graph_node));
|
||||||
if (self->_head == NULL)
|
if (self->_head == NULL)
|
||||||
{
|
{
|
||||||
stack_free(&self->stack);
|
|
||||||
queue_free(&self->queue);
|
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
self->_head->visited = false;
|
self->_head->visited = false;
|
||||||
@ -1020,9 +799,6 @@ static bool graph_init(struct _graph *self, uint32_t obj_size)
|
|||||||
self->_type = GRAPH_UNDIRECTED;
|
self->_type = GRAPH_UNDIRECTED;
|
||||||
// self->_type = GRAPH_DIRECTED;
|
// self->_type = GRAPH_DIRECTED;
|
||||||
|
|
||||||
self->_iter.hasnext = graph_iter_hasnext;
|
|
||||||
self->_iter.next = graph_iter_next;
|
|
||||||
|
|
||||||
self->_destory = graph_destory;
|
self->_destory = graph_destory;
|
||||||
|
|
||||||
// -------------------- public --------------------
|
// -------------------- public --------------------
|
||||||
@ -1042,10 +818,6 @@ static bool graph_init(struct _graph *self, uint32_t obj_size)
|
|||||||
self->empty = graph_empty;
|
self->empty = graph_empty;
|
||||||
self->capacity = graph_capacity;
|
self->capacity = graph_capacity;
|
||||||
|
|
||||||
// iter
|
|
||||||
self->iter = graph_iter;
|
|
||||||
|
|
||||||
// others
|
|
||||||
self->from_matrix = NULL;
|
self->from_matrix = NULL;
|
||||||
self->bfs = NULL;
|
self->bfs = NULL;
|
||||||
self->dfs = NULL;
|
self->dfs = NULL;
|
||||||
|
@ -90,7 +90,7 @@ void test_graph_add_edge(void)
|
|||||||
{
|
{
|
||||||
TEST_ASSERT_TRUE(graph->add_vertex(graph, &data[i]));
|
TEST_ASSERT_TRUE(graph->add_vertex(graph, &data[i]));
|
||||||
}
|
}
|
||||||
//graph->print(graph);
|
graph->print(graph);
|
||||||
|
|
||||||
// test add_edge
|
// test add_edge
|
||||||
TEST_ASSERT_TRUE(graph->add_edge(graph, &data[0], &data[1], 12));
|
TEST_ASSERT_TRUE(graph->add_edge(graph, &data[0], &data[1], 12));
|
||||||
@ -98,7 +98,7 @@ void test_graph_add_edge(void)
|
|||||||
TEST_ASSERT_TRUE(graph->add_edge(graph, &data[1], &data[3], 24));
|
TEST_ASSERT_TRUE(graph->add_edge(graph, &data[1], &data[3], 24));
|
||||||
TEST_ASSERT_TRUE(graph->add_edge(graph, &data[5], &data[6], 67));
|
TEST_ASSERT_TRUE(graph->add_edge(graph, &data[5], &data[6], 67));
|
||||||
TEST_ASSERT_FALSE(graph->add_edge(graph, &temp, &data[1], 0));
|
TEST_ASSERT_FALSE(graph->add_edge(graph, &temp, &data[1], 0));
|
||||||
//graph->print(graph);
|
graph->print(graph);
|
||||||
|
|
||||||
// test find_edge
|
// test find_edge
|
||||||
TEST_ASSERT_TRUE(graph->find_edge(graph, &data[0], &data[1]));
|
TEST_ASSERT_TRUE(graph->find_edge(graph, &data[0], &data[1]));
|
||||||
@ -109,71 +109,16 @@ void test_graph_add_edge(void)
|
|||||||
|
|
||||||
// test del_edge
|
// test del_edge
|
||||||
TEST_ASSERT_TRUE(graph->del_edge(graph, &data[0], &data[1]));
|
TEST_ASSERT_TRUE(graph->del_edge(graph, &data[0], &data[1]));
|
||||||
//graph->print(graph);
|
graph->print(graph);
|
||||||
|
|
||||||
TEST_ASSERT_FALSE(graph->find_edge(graph, &data[0], &data[1]));
|
TEST_ASSERT_FALSE(graph->find_edge(graph, &data[0], &data[1]));
|
||||||
TEST_ASSERT_TRUE(graph->del_edge(graph, &data[5], &data[6]));
|
TEST_ASSERT_TRUE(graph->del_edge(graph, &data[5], &data[6]));
|
||||||
//graph->print(graph);
|
graph->print(graph);
|
||||||
|
|
||||||
graph_free(&graph);
|
graph_free(&graph);
|
||||||
TEST_ASSERT_NULL(graph);
|
TEST_ASSERT_NULL(graph);
|
||||||
}
|
}
|
||||||
|
|
||||||
void test_graph_iter(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], 12));
|
|
||||||
TEST_ASSERT_TRUE(graph->add_edge(graph, &data[0], &data[2], 13));
|
|
||||||
TEST_ASSERT_TRUE(graph->add_edge(graph, &data[1], &data[3], 24));
|
|
||||||
TEST_ASSERT_TRUE(graph->add_edge(graph, &data[5], &data[6], 67));
|
|
||||||
|
|
||||||
TEST_ASSERT_TRUE(graph->add_edge(graph, &data[7], &data[6], 87));
|
|
||||||
TEST_ASSERT_TRUE(graph->add_edge(graph, &data[8], &data[2], 92));
|
|
||||||
TEST_ASSERT_FALSE(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]);
|
|
||||||
TEST_ASSERT_NOT_NULL(iter_vertex);
|
|
||||||
while(iter_vertex->hasnext(iter_vertex))
|
|
||||||
{
|
|
||||||
temp = *(int *)iter_vertex->next(iter_vertex);
|
|
||||||
// graph->print_obj(&temp);
|
|
||||||
}
|
|
||||||
|
|
||||||
iter_vertex = graph->iter(graph, GRAPH_DFS, &data[0]);
|
|
||||||
TEST_ASSERT_NOT_NULL(iter_vertex);
|
|
||||||
while(iter_vertex->hasnext(iter_vertex))
|
|
||||||
{
|
|
||||||
temp = *(int *)iter_vertex->next(iter_vertex);
|
|
||||||
printf("temp = %d\n", temp);
|
|
||||||
}
|
|
||||||
|
|
||||||
graph_free(&graph);
|
|
||||||
TEST_ASSERT_NULL(graph);
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
#if 0
|
#if 0
|
||||||
void test_graph_print(void)
|
void test_graph_print(void)
|
||||||
{
|
{
|
||||||
@ -222,7 +167,6 @@ void test_graph(void)
|
|||||||
RUN_TEST(test_graph_new);
|
RUN_TEST(test_graph_new);
|
||||||
RUN_TEST(test_graph_add_vertex);
|
RUN_TEST(test_graph_add_vertex);
|
||||||
RUN_TEST(test_graph_add_edge);
|
RUN_TEST(test_graph_add_edge);
|
||||||
RUN_TEST(test_graph_iter);
|
|
||||||
|
|
||||||
// RUN_TEST(test_graph_print);
|
// RUN_TEST(test_graph_print);
|
||||||
// RUN_TEST(test_graph_from_matrix);
|
// RUN_TEST(test_graph_from_matrix);
|
||||||
|
Loading…
Reference in New Issue
Block a user