Skip to content

Commit fef4fd5

Browse files
committed
added neighbouring vertices traversal to infinite adjacency matrix
1 parent 7b1dcda commit fef4fd5

2 files changed

Lines changed: 43 additions & 10 deletions

File tree

include/graph/imatrix_graph.h

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -196,20 +196,22 @@ iam_graph_s subgraph_iam_list(const iam_graph_s * graph, const iam_list_s * tabl
196196
/// @param graph Structure to iterate.
197197
/// @param handle Function pointer to handle each element reference using generic arguments.
198198
/// @param arguments Arguments for operate function pointer.
199-
void map_vertex_iam_graph(const iam_graph_s * graph, const handle_fn handle, void * arguments);
199+
void each_vertex_iam_graph(const iam_graph_s * graph, const handle_fn handle, void * arguments);
200+
201+
void each_neighbor_iam_graph(iam_graph_s const * const graph, size_t const index, handle_fn const handle, void * const arguments);
200202

201203
/// @brief Iterates over each edge element in structure starting from the beginning.
202204
/// @param graph Structure to iterate.
203205
/// @param handle Function pointer to handle each element reference using generic arguments.
204206
/// @param arguments Arguments for operate function pointer.
205-
void map_edge_iam_graph(const iam_graph_s * graph, const handle_fn handle, void * arguments);
207+
void each_edge_iam_graph(const iam_graph_s * graph, const handle_fn handle, void * arguments);
206208

207209
/// @brief Traverses the costs of the specified structure using a generated table.
208210
/// @param graph Structure to traverse.
209211
/// @param table Structure to reference.
210212
/// @param start Starting vertex index.
211213
/// @param handle Operate function pointer to operate on each vertex.
212214
/// @param arguments Arguments for operate function pointer.
213-
void map_cost_iam_list(const iam_graph_s * graph, const iam_list_s * table, const size_t start, const handle_fn handle, void * arguments);
215+
void each_cost_iam_list(const iam_graph_s * graph, const iam_list_s * table, const size_t start, const handle_fn handle, void * arguments);
214216

215217
#endif // IMATRIX_H

source/graph/imatrix_graph.c

Lines changed: 38 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -494,9 +494,6 @@ iam_list_s a_star_iam_list(const iam_graph_s * graph, const iam_cost_s * cost, c
494494
return (iam_list_s) { 0 };
495495
}
496496

497-
void map_cost_iam_list(const iam_graph_s * graph, const iam_list_s * table, const size_t start, const handle_fn handle, void * arguments) {
498-
}
499-
500497
void destroy_iam_list(const iam_graph_s * graph, iam_list_s * table) {
501498
free(table->cost);
502499
free(table->previous);
@@ -508,17 +505,51 @@ iam_graph_s subgraph_iam_list(const iam_graph_s * graph, const iam_list_s * tabl
508505
return (iam_graph_s) { 0 };
509506
}
510507

511-
void map_vertex_iam_graph(const iam_graph_s * graph, const handle_fn handle, void * arguments) {
508+
void each_vertex_iam_graph(const iam_graph_s * graph, const handle_fn handle, void * arguments) {
512509
for (char * v = graph->vertices; v < graph->vertices + (graph->length * graph->vertex_size); v += graph->vertex_size) {
513510
if (!handle(v, arguments)) { return; } // if handler terminates (returns false) end loop
514511
}
515512
}
516513

517-
void map_edge_iam_graph(const iam_graph_s * graph, const handle_fn handle, void * arguments) {
514+
void each_edge_iam_graph(const iam_graph_s * graph, const handle_fn handle, void * arguments) {
518515
const size_t edge_length = (graph->length * (graph->length - 1)) / 2;
519516
for (char * e = graph->edges; e < graph->edges + (edge_length * graph->edge_size); e += graph->edge_size) {
520-
if (!graph->compare(e, graph->none)) { continue; } // if edge is none continue since handle can't be performed
521-
if (!handle(e, arguments)) { return; } // if handler terminates (returns false) end loop
517+
// if edge is none continue since handle can't be performed (first condition is false so other won't be checked.)
518+
// if handler terminates (returns false) end loop
519+
if (graph->compare(e, graph->none) && !handle(e, arguments)) {
520+
break;
521+
}
522+
}
523+
}
524+
525+
void each_neighbor_iam_graph(iam_graph_s const * const graph, size_t const index, handle_fn const handle, void * const arguments) {
526+
const size_t offset = (index * (index - 1)) / 2;
527+
for (size_t i = 0, e = offset; i < index; ++i, e++) {
528+
char const * edge = graph->edges + (e * graph->edge_size);
529+
char * vertex = graph->vertices + (i * graph->vertex_size);
530+
531+
// if edge is none continue since handle can't be performed (first condition is false so other won't be checked.)
532+
// if handler terminates (returns false) end loop
533+
if (graph->compare(edge, graph->none) && !handle(vertex, arguments)) {
534+
return;
535+
}
536+
}
537+
538+
for (size_t i = index + 1, e = offset + (2 * index); i < graph->length; e += i++) {
539+
char const * edge = graph->edges + (e * graph->edge_size);
540+
char * vertex = graph->vertices + (i * graph->vertex_size);
541+
542+
// if edge is none continue since handle can't be performed (first condition is false so other won't be checked.)
543+
// if handler terminates (returns false) end loop
544+
if (graph->compare(edge, graph->none) && !handle(vertex, arguments)) {
545+
return;
546+
}
547+
}
548+
}
549+
550+
void each_cost_iam_list(const iam_graph_s * graph, const iam_list_s * table, const size_t start, const handle_fn handle, void * arguments) {
551+
for (size_t i = 0; i < graph->length; ++i) {
552+
if (!handle(table->cost + (table->data->size * i), arguments)) { break; }
522553
}
523554
}
524555

0 commit comments

Comments
 (0)