lifo-dijkstraserv/src/dijkstrasearch.c

46 lines
1.1 KiB
C
Raw Normal View History

2020-05-02 17:31:26 +02:00
#include "dijkstrasearch.h"
void dijkstra_search_reset(struct dijkstra_search *search)
{
unsigned int i;
for (i=0; i < search->node_count; i++) {
search->states[i]->cost = DIJKSTRA_COST_MAX;
search->states[i]->visited = 0;
}
}
// WARNING: After creating this object, you must NOT modify the solver!
struct dijkstra_search *dijkstra_search_new(struct dijkstra_solver *solver)
{
struct dijkstra_search *search;
unsigned int i;
search = malloc(sizeof(*search));
if (search) {
search->states = malloc(sizeof(*search->states) * g_list_length(solver->nodes));
search->node_count = g_list_length(solver->nodes);
for (i=0; i < search->node_count; i++) {
search->states[i] = malloc(sizeof(struct dijkstra_state));
if (!search->states) {
// TODO: I should dealloc all allocated memory before returning,
// but whatever, we'll crash and burn one way of the other.
return NULL;
}
}
dijkstra_search_reset(search);
}
return search;
}
void dijkstra_search_free(struct dijkstra_search *search)
{
unsigned int i;
for (i=0; i < search->node_count; i++) {
free(search->states[i]);
}
free(search->states);
free(search);
}