mirror of
https://gitee.com/apaki/unicstl.git
synced 2025-07-06 00:46:52 +08:00
Compare commits
5 Commits
3ac92de18e
...
5e83dc2917
Author | SHA1 | Date | |
---|---|---|---|
5e83dc2917 | |||
43b237487a | |||
43706105bb | |||
98eb005a4a | |||
ce973043d8 |
@ -13,6 +13,9 @@
|
|||||||
|
|
||||||
#include "common.h"
|
#include "common.h"
|
||||||
|
|
||||||
|
#include "stack.h"
|
||||||
|
#include "queue.h"
|
||||||
|
|
||||||
enum _graph_type
|
enum _graph_type
|
||||||
{
|
{
|
||||||
GRAPH_UNDIRECTED,
|
GRAPH_UNDIRECTED,
|
||||||
@ -21,6 +24,13 @@ 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;
|
||||||
@ -47,6 +57,11 @@ 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);
|
||||||
@ -77,7 +92,7 @@ struct _graph
|
|||||||
bool (*full)(struct _graph* self);
|
bool (*full)(struct _graph* self);
|
||||||
|
|
||||||
// iter
|
// iter
|
||||||
iterator_t (*iter)(struct _graph* self);
|
iterator_t (*iter)(struct _graph* self, enum _graph_search search_type, void *obj);
|
||||||
|
|
||||||
// config
|
// config
|
||||||
compare_fun_t compare; // !!! you have to implement this function
|
compare_fun_t compare; // !!! you have to implement this function
|
||||||
|
358
src/graph.c
358
src/graph.c
@ -310,20 +310,16 @@ 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);
|
||||||
if (new_obj == NULL)
|
if (new_obj == NULL)
|
||||||
{
|
{
|
||||||
return NULL;
|
return NULL;
|
||||||
}
|
}
|
||||||
memmove(new_obj, obj, obj_size);
|
memmove(new_obj, obj, obj_size);
|
||||||
|
|
||||||
struct _graph_node* new_node = (struct _graph_node*)malloc(sizeof(struct _graph_node));
|
struct _graph_node *new_node = (struct _graph_node *)malloc(sizeof(struct _graph_node));
|
||||||
if (new_node == NULL)
|
if (new_node == NULL)
|
||||||
{
|
{
|
||||||
free(new_obj);
|
free(new_obj);
|
||||||
@ -337,7 +333,7 @@ static struct _graph_node* graph_node_new(void* obj, uint32_t obj_size)
|
|||||||
return new_node;
|
return new_node;
|
||||||
}
|
}
|
||||||
|
|
||||||
static void greph_node_free(struct _graph_node** node)
|
static void greph_node_free(struct _graph_node **node)
|
||||||
{
|
{
|
||||||
if (node != NULL && *node != NULL)
|
if (node != NULL && *node != NULL)
|
||||||
{
|
{
|
||||||
@ -359,9 +355,9 @@ static void greph_node_free(struct _graph_node** node)
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
static struct _graph_edge* graph_edge_new(void *target, uint32_t weight)
|
static struct _graph_edge *graph_edge_new(void *target, uint32_t weight)
|
||||||
{
|
{
|
||||||
struct _graph_edge* new_edge = (struct _graph_edge*)malloc(sizeof(struct _graph_edge));
|
struct _graph_edge *new_edge = (struct _graph_edge *)malloc(sizeof(struct _graph_edge));
|
||||||
if (new_edge == NULL)
|
if (new_edge == NULL)
|
||||||
{
|
{
|
||||||
return NULL;
|
return NULL;
|
||||||
@ -372,7 +368,7 @@ static struct _graph_edge* graph_edge_new(void *target, uint32_t weight)
|
|||||||
return new_edge;
|
return new_edge;
|
||||||
}
|
}
|
||||||
|
|
||||||
static void graph_edge_free(struct _graph_edge** edge)
|
static void graph_edge_free(struct _graph_edge **edge)
|
||||||
{
|
{
|
||||||
if (edge != NULL && *edge != NULL)
|
if (edge != NULL && *edge != NULL)
|
||||||
{
|
{
|
||||||
@ -381,7 +377,7 @@ static void graph_edge_free(struct _graph_edge** edge)
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
static uint32_t graph_size(struct _graph* self)
|
static uint32_t graph_size(struct _graph *self)
|
||||||
{
|
{
|
||||||
if (self == NULL)
|
if (self == NULL)
|
||||||
{
|
{
|
||||||
@ -390,7 +386,7 @@ static uint32_t graph_size(struct _graph* self)
|
|||||||
return self->_size;
|
return self->_size;
|
||||||
}
|
}
|
||||||
|
|
||||||
static bool graph_empty(struct _graph* self)
|
static bool graph_empty(struct _graph *self)
|
||||||
{
|
{
|
||||||
if (self == NULL)
|
if (self == NULL)
|
||||||
{
|
{
|
||||||
@ -399,7 +395,7 @@ static bool graph_empty(struct _graph* self)
|
|||||||
return self->size(self) == 0;
|
return self->size(self) == 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
static uint32_t graph_capacity(struct _graph* self)
|
static uint32_t graph_capacity(struct _graph *self)
|
||||||
{
|
{
|
||||||
if (self == NULL)
|
if (self == NULL)
|
||||||
{
|
{
|
||||||
@ -408,15 +404,15 @@ static uint32_t graph_capacity(struct _graph* self)
|
|||||||
return self->_capacity;
|
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;
|
return 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
struct _graph_node* cur = self->_head->next;
|
struct _graph_node *cur = self->_head->next;
|
||||||
struct _graph_node* next = NULL;
|
struct _graph_node *next = NULL;
|
||||||
while (cur != NULL)
|
while (cur != NULL)
|
||||||
{
|
{
|
||||||
next = cur->next;
|
next = cur->next;
|
||||||
@ -429,7 +425,7 @@ static bool graph_clear(struct _graph* self)
|
|||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
static void graph_destory(struct _graph* self)
|
static void graph_destory(struct _graph *self)
|
||||||
{
|
{
|
||||||
if (self == NULL)
|
if (self == NULL)
|
||||||
{
|
{
|
||||||
@ -441,9 +437,18 @@ 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)
|
||||||
{
|
{
|
||||||
if (self == NULL || self->_head == NULL || self->print_obj == NULL)
|
if (self == NULL || self->_head == NULL || self->print_obj == NULL)
|
||||||
{
|
{
|
||||||
@ -451,7 +456,7 @@ static void graph_print(struct _graph* self)
|
|||||||
}
|
}
|
||||||
|
|
||||||
printf("vertex : \n");
|
printf("vertex : \n");
|
||||||
struct _graph_node* cur = self->_head->next;
|
struct _graph_node *cur = self->_head->next;
|
||||||
while (cur != NULL)
|
while (cur != NULL)
|
||||||
{
|
{
|
||||||
self->print_obj(cur->obj);
|
self->print_obj(cur->obj);
|
||||||
@ -463,13 +468,13 @@ static void graph_print(struct _graph* self)
|
|||||||
cur = self->_head->next;
|
cur = self->_head->next;
|
||||||
while (cur != NULL)
|
while (cur != NULL)
|
||||||
{
|
{
|
||||||
if(cur->edgehead != NULL)
|
if (cur->edgehead != NULL)
|
||||||
{
|
{
|
||||||
// struct _graph_edge* edge = cur->edgehead->next;
|
// struct _graph_edge* edge = cur->edgehead->next;
|
||||||
struct _graph_edge* edge = cur->edgehead;
|
struct _graph_edge *edge = cur->edgehead;
|
||||||
while (edge != NULL)
|
while (edge != NULL)
|
||||||
{
|
{
|
||||||
struct _graph_node* target = (struct _graph_node*)edge->target;
|
struct _graph_node *target = (struct _graph_node *)edge->target;
|
||||||
|
|
||||||
printf("from ");
|
printf("from ");
|
||||||
self->print_obj(cur->obj);
|
self->print_obj(cur->obj);
|
||||||
@ -488,13 +493,13 @@ static void graph_print(struct _graph* self)
|
|||||||
printf("print done.\n");
|
printf("print done.\n");
|
||||||
}
|
}
|
||||||
|
|
||||||
static bool graph_add_vertex(struct _graph* self, void* obj)
|
static bool graph_add_vertex(struct _graph *self, void *obj)
|
||||||
{
|
{
|
||||||
assert(self != NULL);
|
assert(self != NULL);
|
||||||
if (self->_head->next == NULL)
|
if (self->_head->next == NULL)
|
||||||
{
|
{
|
||||||
// no vertex
|
// no vertex
|
||||||
struct _graph_node* new_node = graph_node_new(obj, self->_obj_size);
|
struct _graph_node *new_node = graph_node_new(obj, self->_obj_size);
|
||||||
if (new_node == NULL)
|
if (new_node == NULL)
|
||||||
{
|
{
|
||||||
return false;
|
return false;
|
||||||
@ -503,7 +508,7 @@ static bool graph_add_vertex(struct _graph* self, void* obj)
|
|||||||
}
|
}
|
||||||
else
|
else
|
||||||
{
|
{
|
||||||
struct _graph_node* cur = self->_head->next;
|
struct _graph_node *cur = self->_head->next;
|
||||||
|
|
||||||
// find if exist
|
// find if exist
|
||||||
while (cur != NULL)
|
while (cur != NULL)
|
||||||
@ -515,7 +520,7 @@ static bool graph_add_vertex(struct _graph* self, void* obj)
|
|||||||
cur = cur->next;
|
cur = cur->next;
|
||||||
}
|
}
|
||||||
|
|
||||||
struct _graph_node* new_node = graph_node_new(obj, self->_obj_size);
|
struct _graph_node *new_node = graph_node_new(obj, self->_obj_size);
|
||||||
if (new_node == NULL)
|
if (new_node == NULL)
|
||||||
{
|
{
|
||||||
return false;
|
return false;
|
||||||
@ -534,7 +539,7 @@ static bool graph_add_vertex(struct _graph* self, void* obj)
|
|||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
static bool graph_del_vertex(struct _graph* self, void* obj)
|
static bool graph_del_vertex(struct _graph *self, void *obj)
|
||||||
{
|
{
|
||||||
assert(self != NULL);
|
assert(self != NULL);
|
||||||
if (obj == NULL)
|
if (obj == NULL)
|
||||||
@ -546,8 +551,8 @@ static bool graph_del_vertex(struct _graph* self, void* obj)
|
|||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
struct _graph_node* cur = self->_head->next;
|
struct _graph_node *cur = self->_head->next;
|
||||||
struct _graph_node* pre = self->_head;
|
struct _graph_node *pre = self->_head;
|
||||||
while (cur != NULL)
|
while (cur != NULL)
|
||||||
{
|
{
|
||||||
if (self->compare(cur->obj, obj) == 0)
|
if (self->compare(cur->obj, obj) == 0)
|
||||||
@ -568,7 +573,7 @@ static bool graph_del_vertex(struct _graph* self, void* obj)
|
|||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
static bool graph_find_vertex(struct _graph* self, void* obj)
|
static bool graph_find_vertex(struct _graph *self, void *obj)
|
||||||
{
|
{
|
||||||
assert(self != NULL);
|
assert(self != NULL);
|
||||||
if (obj == NULL)
|
if (obj == NULL)
|
||||||
@ -579,7 +584,7 @@ static bool graph_find_vertex(struct _graph* self, void* obj)
|
|||||||
{
|
{
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
struct _graph_node* cur = self->_head->next;
|
struct _graph_node *cur = self->_head->next;
|
||||||
while (cur != NULL)
|
while (cur != NULL)
|
||||||
{
|
{
|
||||||
if (self->compare(cur->obj, obj) == 0)
|
if (self->compare(cur->obj, obj) == 0)
|
||||||
@ -593,9 +598,9 @@ static bool graph_find_vertex(struct _graph* self, void* obj)
|
|||||||
return cur == NULL ? false : true;
|
return cur == NULL ? false : true;
|
||||||
}
|
}
|
||||||
|
|
||||||
static struct _graph_node* find_node(struct _graph* self, void* obj)
|
static struct _graph_node *find_node(struct _graph *self, void *obj)
|
||||||
{
|
{
|
||||||
struct _graph_node* cur = self->_head->next;
|
struct _graph_node *cur = self->_head->next;
|
||||||
while (cur != NULL)
|
while (cur != NULL)
|
||||||
{
|
{
|
||||||
if (self->compare(cur->obj, obj) == 0)
|
if (self->compare(cur->obj, obj) == 0)
|
||||||
@ -608,8 +613,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);
|
||||||
if (self->empty(self))
|
if (self->empty(self))
|
||||||
@ -617,8 +621,8 @@ static bool graph_add_edge(struct _graph* self, void* from, void* to, uint32_t w
|
|||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
struct _graph_node* from_node = self->_head->next;
|
struct _graph_node *from_node = self->_head->next;
|
||||||
struct _graph_node* to_node = self->_head->next;
|
struct _graph_node *to_node = self->_head->next;
|
||||||
from_node = find_node(self, from);
|
from_node = find_node(self, from);
|
||||||
if (from_node == NULL)
|
if (from_node == NULL)
|
||||||
{
|
{
|
||||||
@ -632,7 +636,7 @@ static bool graph_add_edge(struct _graph* self, void* from, void* to, uint32_t w
|
|||||||
}
|
}
|
||||||
|
|
||||||
// from_node add edge
|
// from_node add edge
|
||||||
struct _graph_edge* new_edge = graph_edge_new(to_node, weight);
|
struct _graph_edge *new_edge = graph_edge_new(to_node, weight);
|
||||||
if (new_edge == NULL)
|
if (new_edge == NULL)
|
||||||
{
|
{
|
||||||
return false;
|
return false;
|
||||||
@ -647,11 +651,11 @@ static bool graph_add_edge(struct _graph* self, void* from, void* to, uint32_t w
|
|||||||
from_node->edgehead->next = new_edge;
|
from_node->edgehead->next = new_edge;
|
||||||
}
|
}
|
||||||
|
|
||||||
if(self->_type == GRAPH_UNDIRECTED)
|
if (self->_type == GRAPH_UNDIRECTED)
|
||||||
{
|
{
|
||||||
// if graph is undirected
|
// if graph is undirected
|
||||||
// to_node add edge
|
// to_node add edge
|
||||||
struct _graph_edge* new_edge2 = graph_edge_new(from_node, weight);
|
struct _graph_edge *new_edge2 = graph_edge_new(from_node, weight);
|
||||||
if (new_edge2 == NULL)
|
if (new_edge2 == NULL)
|
||||||
{
|
{
|
||||||
return false;
|
return false;
|
||||||
@ -671,7 +675,7 @@ static bool graph_add_edge(struct _graph* self, void* from, void* to, uint32_t w
|
|||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
static bool graph_del_edge(struct _graph* self, void* from, void* to)
|
static bool graph_del_edge(struct _graph *self, void *from, void *to)
|
||||||
{
|
{
|
||||||
assert(self != NULL);
|
assert(self != NULL);
|
||||||
if (self->empty(self))
|
if (self->empty(self))
|
||||||
@ -679,8 +683,8 @@ static bool graph_del_edge(struct _graph* self, void* from, void* to)
|
|||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
struct _graph_node* from_node = self->_head->next;
|
struct _graph_node *from_node = self->_head->next;
|
||||||
struct _graph_node* to_node = self->_head->next;
|
struct _graph_node *to_node = self->_head->next;
|
||||||
from_node = find_node(self, from);
|
from_node = find_node(self, from);
|
||||||
if (from_node == NULL)
|
if (from_node == NULL)
|
||||||
{
|
{
|
||||||
@ -693,14 +697,14 @@ static bool graph_del_edge(struct _graph* self, void* from, void* to)
|
|||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
struct _graph_edge* cur = from_node->edgehead;
|
struct _graph_edge *cur = from_node->edgehead;
|
||||||
struct _graph_edge* pre = from_node->edgehead;
|
struct _graph_edge *pre = from_node->edgehead;
|
||||||
while(cur != NULL)
|
while (cur != NULL)
|
||||||
{
|
{
|
||||||
if(cur->target == to_node)
|
if (cur->target == to_node)
|
||||||
{
|
{
|
||||||
pre->next = cur->next;
|
pre->next = cur->next;
|
||||||
if(pre == cur)
|
if (pre == cur)
|
||||||
{
|
{
|
||||||
from_node->edgehead = cur->next;
|
from_node->edgehead = cur->next;
|
||||||
}
|
}
|
||||||
@ -711,16 +715,16 @@ static bool graph_del_edge(struct _graph* self, void* from, void* to)
|
|||||||
cur = cur->next;
|
cur = cur->next;
|
||||||
}
|
}
|
||||||
|
|
||||||
if(self->_type == GRAPH_UNDIRECTED)
|
if (self->_type == GRAPH_UNDIRECTED)
|
||||||
{
|
{
|
||||||
struct _graph_edge* cur2 = to_node->edgehead;
|
struct _graph_edge *cur2 = to_node->edgehead;
|
||||||
struct _graph_edge* pre2 = to_node->edgehead;
|
struct _graph_edge *pre2 = to_node->edgehead;
|
||||||
while(cur2 != NULL)
|
while (cur2 != NULL)
|
||||||
{
|
{
|
||||||
if(cur2->target == from_node)
|
if (cur2->target == from_node)
|
||||||
{
|
{
|
||||||
pre2->next = cur2->next;
|
pre2->next = cur2->next;
|
||||||
if(pre2 == cur2)
|
if (pre2 == cur2)
|
||||||
{
|
{
|
||||||
to_node->edgehead = cur2->next;
|
to_node->edgehead = cur2->next;
|
||||||
}
|
}
|
||||||
@ -734,15 +738,15 @@ static bool graph_del_edge(struct _graph* self, void* from, void* to)
|
|||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
static bool graph_find_edge(struct _graph* self, void* from, void* to)
|
static bool graph_find_edge(struct _graph *self, void *from, void *to)
|
||||||
{
|
{
|
||||||
assert(self != NULL);
|
assert(self != NULL);
|
||||||
if (self->empty(self))
|
if (self->empty(self))
|
||||||
{
|
{
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
struct _graph_node* from_node = self->_head->next;
|
struct _graph_node *from_node = self->_head->next;
|
||||||
struct _graph_node* to_node = self->_head->next;
|
struct _graph_node *to_node = self->_head->next;
|
||||||
from_node = find_node(self, from);
|
from_node = find_node(self, from);
|
||||||
if (from_node == NULL)
|
if (from_node == NULL)
|
||||||
{
|
{
|
||||||
@ -755,24 +759,227 @@ static bool graph_find_edge(struct _graph* self, void* from, void* to)
|
|||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
struct _graph_edge* cur = from_node->edgehead;
|
struct _graph_edge *cur = from_node->edgehead;
|
||||||
while(cur != NULL)
|
while (cur != NULL)
|
||||||
{
|
{
|
||||||
if(cur->target == to_node)
|
if (cur->target == to_node)
|
||||||
{
|
{
|
||||||
// found edge
|
// found edge
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
cur = cur->next;
|
cur = cur->next;
|
||||||
}
|
}
|
||||||
if(cur == NULL)
|
if (cur == NULL)
|
||||||
{
|
{
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
static bool graph_init(struct _graph* self, uint32_t obj_size)
|
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)
|
||||||
{
|
{
|
||||||
assert(self != NULL);
|
assert(self != NULL);
|
||||||
if (self == NULL)
|
if (self == NULL)
|
||||||
@ -786,9 +993,23 @@ 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->_head = (struct _graph_node*)malloc(sizeof(struct _graph_node));
|
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));
|
||||||
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;
|
||||||
@ -799,6 +1020,9 @@ 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 --------------------
|
||||||
@ -818,6 +1042,10 @@ 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;
|
||||||
@ -848,7 +1076,7 @@ graph_t graph_new(uint32_t obj_size)
|
|||||||
return graph;
|
return graph;
|
||||||
}
|
}
|
||||||
|
|
||||||
void graph_free(graph_t* graph)
|
void graph_free(graph_t *graph)
|
||||||
{
|
{
|
||||||
if (graph == NULL || *graph == NULL)
|
if (graph == NULL || *graph == 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,16 +109,71 @@ 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)
|
||||||
{
|
{
|
||||||
@ -167,6 +222,7 @@ 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