mirror of
https://gitee.com/apaki/unicstl.git
synced 2025-07-03 23:56:54 +08:00
二维数组传参的问题
This commit is contained in:
parent
0ac0976bb6
commit
f148c26764
@ -29,7 +29,7 @@ struct _graph {
|
|||||||
|
|
||||||
// init
|
// init
|
||||||
void (*init)(struct _graph *self);
|
void (*init)(struct _graph *self);
|
||||||
void (*from_matrix)(struct _graph *self, uint32_t **edges, uint32_t size);
|
bool (*from_matrix)(struct _graph *self, void *obj, uint32_t *edges, uint32_t size);
|
||||||
|
|
||||||
// kernel
|
// kernel
|
||||||
bool (*add)(struct _graph *self, void *obj);
|
bool (*add)(struct _graph *self, void *obj);
|
||||||
|
28
src/graph.c
28
src/graph.c
@ -100,6 +100,32 @@ static void graph_print(struct _graph *self)
|
|||||||
printf("\n");
|
printf("\n");
|
||||||
}
|
}
|
||||||
|
|
||||||
|
static bool graph_from_matrix(struct _graph *self, void *obj, uint32_t *edges, uint32_t size)
|
||||||
|
{
|
||||||
|
if(self == NULL || self->_head == NULL)
|
||||||
|
{
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
if(size > self->_capacity || obj == NULL || edges == NULL)
|
||||||
|
{
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
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);
|
||||||
|
}
|
||||||
|
for(uint32_t i = 0; i < size; i++)
|
||||||
|
{
|
||||||
|
for(uint32_t j = 0; j < size; j++)
|
||||||
|
{
|
||||||
|
self->_head->edge[i][j] = edges[i * size + j];
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
|
||||||
static void graph_init2(struct _graph *self)
|
static void graph_init2(struct _graph *self)
|
||||||
{
|
{
|
||||||
if(self == NULL || self->_head == NULL)
|
if(self == NULL || self->_head == NULL)
|
||||||
@ -142,6 +168,8 @@ graph_t graph_new2(uint32_t obj_size, uint32_t capacity)
|
|||||||
graph->capacity = graph_capacity;
|
graph->capacity = graph_capacity;
|
||||||
graph->clear = graph_clear;
|
graph->clear = graph_clear;
|
||||||
|
|
||||||
|
graph->from_matrix = graph_from_matrix;
|
||||||
|
|
||||||
graph->print_obj = NULL;
|
graph->print_obj = NULL;
|
||||||
graph->print = graph_print;
|
graph->print = graph_print;
|
||||||
|
|
||||||
|
@ -30,8 +30,34 @@ void test_graph_print(void)
|
|||||||
TEST_ASSERT_NULL(graph);
|
TEST_ASSERT_NULL(graph);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
void test_graph_from_matrix(void)
|
||||||
|
{
|
||||||
|
// const uint32_t size = 10;
|
||||||
|
#define size 5
|
||||||
|
int vertexs[size] = {1,2,3,4,5};
|
||||||
|
int matrix[size * size] = {
|
||||||
|
0, 1, 0, 0, 0,
|
||||||
|
1, 0, 1, 0, 0,
|
||||||
|
0, 1, 0, 1, 0,
|
||||||
|
0, 0, 1, 0, 1,
|
||||||
|
0, 0, 0, 1, 0,
|
||||||
|
};
|
||||||
|
|
||||||
|
graph_t graph = graph_new2(sizeof(int), 10);
|
||||||
|
TEST_ASSERT_NOT_NULL(graph);
|
||||||
|
graph->print_obj = print_num;
|
||||||
|
|
||||||
|
graph->print(graph);
|
||||||
|
graph->from_matrix(graph, vertexs, matrix, size);
|
||||||
|
graph->print(graph);
|
||||||
|
|
||||||
|
graph_free(&graph);
|
||||||
|
TEST_ASSERT_NULL(graph);
|
||||||
|
}
|
||||||
|
|
||||||
void test_graph(void)
|
void test_graph(void)
|
||||||
{
|
{
|
||||||
RUN_TEST(test_graph_new);
|
RUN_TEST(test_graph_new);
|
||||||
RUN_TEST(test_graph_print);
|
// RUN_TEST(test_graph_print);
|
||||||
|
RUN_TEST(test_graph_from_matrix);
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user