添加图框架

This commit is contained in:
建峰 2024-09-03 00:59:34 +08:00
parent d8d6256c0a
commit 2d1c498c91
2 changed files with 167 additions and 0 deletions

54
include/graph.h Normal file
View File

@ -0,0 +1,54 @@
/**
* @file graph.h
* @author wenjf (Orig5826@163.com)
* @brief
* @version 0.1
* @date 2024-09-03
*
* @copyright Copyright (c) 2024
*
*/
#ifndef _GRAPH_H
#define _GRAPH_H
#include "common.h"
struct _graph_node {
void *obj;
struct _graph_node *next;
};
struct _graph {
struct _graph_node *_head;
uint32_t **edges;
uint32_t _size;
uint32_t _obj_size;
uint32_t _capacity;
uint32_t _ratio;
// init
void (*init)(struct _graph *self);
// kernel
bool (*add)(struct _graph *self, void *obj);
bool (*get)(struct _graph *self, uint32_t idx, void *obj);
bool (*remove)(struct _graph *self, uint32_t idx);
// base
uint32_t (*size)(struct _graph *self);
uint32_t (*capacity)(struct _graph *self);
bool (*empty)(struct _graph *self);
bool (*full)(struct _graph *self);
//
bool (*clear)(struct _graph *self);
void (*destory)(struct _graph *self);
};
typedef struct _graph* graph_t;
graph_t graph_new2(uint32_t obj_size, uint32_t capacity);
void graph_free(graph_t *graph);
#endif

113
src/graph.c Normal file
View File

@ -0,0 +1,113 @@
/**
* @file graph.c
* @author wenjf (Orig5826@163.com)
* @brief
* @version 0.1
* @date 2024-09-03
*
* @copyright Copyright (c) 2024
*
*/
#include "graph.h"
static uint32_t graph_size(struct _graph* self)
{
if(self == NULL)
{
return 0;
}
return self->_size;
}
static uint32_t graph_capacity(struct _graph* self)
{
if(self == NULL)
{
return 0;
}
return self->_capacity;
}
static bool graph_clear(struct _graph *self)
{
if(self == NULL && self->_head == NULL)
{
return 0;
}
self->_head->obj = NULL;
return self->_size = 0;
}
static void graph_destory(struct _graph *self)
{
if(self == NULL)
{
return;
}
self->clear(self);
if(self->_head != NULL)
{
free(self->_head);
self->_head = NULL;
}
}
graph_t graph_new2(uint32_t obj_size, uint32_t capacity)
{
if(obj_size == 0 || capacity == 0)
{
return NULL;
}
graph_t graph = malloc(sizeof(struct _graph));
if(graph == NULL)
{
goto done;
}
graph->_size = 0;
graph->_obj_size = obj_size;
graph->_capacity = capacity;
graph->_ratio = 1;
graph->size = graph_size;
graph->capacity = graph_capacity;
graph->_head = (struct _graph_node *)malloc(sizeof(struct _graph_node));
if(graph->_head == NULL)
{
goto done1;
}
graph->_head->next = NULL;
graph->_head->obj = (void *)malloc(graph->_obj_size * graph->_capacity * graph->_capacity);
if(graph->_head->obj == NULL)
{
goto done2;
}
// init graph
graph->init(graph);
return graph;
// free(graph->_head->obj)
done2:
free(graph->_head);
done1:
free(graph);
done:
return NULL;
}
void graph_free(graph_t *graph)
{
if(graph == NULL || *graph == NULL)
{
return;
}
(*graph)->destory(*graph);
free(*graph);
*graph = NULL;
}