-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsentinel_graph.c
More file actions
76 lines (62 loc) · 2.26 KB
/
sentinel_graph.c
File metadata and controls
76 lines (62 loc) · 2.26 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
/*
sentinel_graph.c
PURPOSE:
Implementation of robot memory graph engine.
*/
#include "sentinel_graph.h"
#include <stdlib.h>
#include <string.h>
static SenString sg_make_string(const char* s) {
SenString str;
str.data = s;
str.length = s ? (int)strlen(s) : 0;
return str;
}
SenStatus sentinel_graph_init(SenGraph* graph) {
if (!graph) return SEN_STATUS_ERROR_INVALID_ARGUMENT;
graph->nodes = NULL;
graph->node_count = 0;
graph->edges = NULL;
graph->edge_count = 0;
return SEN_STATUS_OK;
}
SenStatus sentinel_graph_add_node(SenGraph* graph,
const char* name,
SenRepoType type) {
if (!graph || !name) return SEN_STATUS_ERROR_INVALID_ARGUMENT;
int new_count = graph->node_count + 1;
SenGraphNode* new_nodes = realloc(graph->nodes,
sizeof(SenGraphNode) * new_count);
if (!new_nodes) return SEN_STATUS_ERROR_INTERNAL;
graph->nodes = new_nodes;
graph->nodes[graph->node_count].name = sg_make_string(name);
graph->nodes[graph->node_count].type = type;
graph->node_count = new_count;
return SEN_STATUS_OK;
}
SenStatus sentinel_graph_add_edge(SenGraph* graph,
const char* from,
const char* to,
const char* relation) {
if (!graph || !from || !to || !relation)
return SEN_STATUS_ERROR_INVALID_ARGUMENT;
int new_count = graph->edge_count + 1;
SenGraphEdge* new_edges = realloc(graph->edges,
sizeof(SenGraphEdge) * new_count);
if (!new_edges) return SEN_STATUS_ERROR_INTERNAL;
graph->edges = new_edges;
graph->edges[graph->edge_count].from = sg_make_string(from);
graph->edges[graph->edge_count].to = sg_make_string(to);
graph->edges[graph->edge_count].relation = sg_make_string(relation);
graph->edge_count = new_count;
return SEN_STATUS_OK;
}
void sentinel_graph_free(SenGraph* graph) {
if (!graph) return;
if (graph->nodes) free(graph->nodes);
if (graph->edges) free(graph->edges);
graph->nodes = NULL;
graph->edges = NULL;
graph->node_count = 0;
graph->edge_count = 0;
}