11 #include "graph-private.h" 56 static const int NODES_DEFAULT_SIZE = 128;
58 static const int EDGES_DEFAULT_SIZE = 8;
62 Graph *g = malloc(
sizeof *g);
64 g->size = NODES_DEFAULT_SIZE;
66 g->nodes = calloc(g->size,
sizeof *g->nodes);
78 if (g->used >= g->size) {
79 Node **tmp = realloc(g->nodes, g->size * 2 *
sizeof *g->nodes);
80 if(!tmp)
return false;
85 g->nodes[g->used] = malloc(
sizeof **g->nodes);
86 if (!g->nodes[g->used])
return false;
88 Node *n = g->nodes[g->used];
91 n->edges_size = EDGES_DEFAULT_SIZE;
92 n->edges = calloc(n->edges_size,
sizeof(
struct edge));
106 if (!g)
return false;
109 if (!from || !to)
return false;
111 if (from->edges_num >= from->edges_size) {
112 struct edge *tmp = realloc(from->edges,
113 from->edges_size * 2 *
sizeof *tmp);
114 if (!tmp)
return false;
116 from->edges_size *= 2;
119 struct edge *
edge = &from->edges[from->edges_num++];
125 static int node_compare(
const void *a,
const void *b)
129 return n1->id - n2->id;
134 if (!g)
return false;
137 qsort(g->nodes, g->used,
sizeof *g->nodes, node_compare);
141 size_t high = g->used;
146 size_t mid = (low + high) / 2;
148 if (
id == g->nodes[mid]->id) {
149 return g->nodes[mid];
151 }
else if (id < g->nodes[mid]->
id) {
168 static void node_free(
Node *node)
179 for (
size_t i = 0; i < g->used; ++i) {
180 node_free(g->nodes[i]);
187 Node **graph_dup_data(
Graph *g,
size_t *len)
189 if (!g || !len)
return NULL;
191 Node **arr = malloc(*len *
sizeof *arr);
192 return arr ? memcpy(arr, g->nodes, *len *
sizeof *arr) : NULL;
Node * destination
Pointer to destination node of this edge.
unsigned int node_get_distance(Node *n)
Get distance of this node from starting node.
Representation of an edge.
Node * graph_get_node(Graph *g, unsigned int id)
Retrieve a node.
struct node Node
Node is an opaque type.
bool graph_insert_edge(Graph *g, unsigned int source, unsigned int dest, int mindelay)
Insert an edge into the graph.
int mindelay
Minimum delay of this edge.
void graph_free(Graph *g)
Free memory used by nodes and edges.
unsigned short node_get_n_outgoing(Node *n)
Get number of outgoing edges from a node.
Graph * graph_new(void)
Create a new graph.
Node * node_get_previous(Node *n)
Get node on (already found) shortest path from which queried node was visited.
bool graph_insert_node(Graph *g, unsigned int id)
Insert node into the graph.
Interface for creating graph.
struct edge * node_get_edges(Node *n)
Get array of outgoing edges.
struct graph Graph
Graph is an opaque type.
unsigned int node_get_id(Node *n)
Get id of a node.