Start implementing actual search
This commit is contained in:
parent
127e0d23ca
commit
7d41c1cdf7
@ -5,18 +5,14 @@
|
||||
|
||||
#define DIJKSTRA_WEIGHT_AUTO (-1)
|
||||
|
||||
typedef int dijkstra_cost;
|
||||
typedef unsigned int dijkstra_cost;
|
||||
#define DIJKSTRA_COST_MAX UINT_MAX
|
||||
|
||||
struct dijkstra_solver {
|
||||
GList *nodes;
|
||||
GList *paths;
|
||||
};
|
||||
|
||||
struct dijkstra_state {
|
||||
dijkstra_cost cost;
|
||||
int visited;
|
||||
};
|
||||
|
||||
struct position {
|
||||
float x;
|
||||
float y;
|
||||
|
45
src/dijkstrasearch.c
Normal file
45
src/dijkstrasearch.c
Normal file
@ -0,0 +1,45 @@
|
||||
#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);
|
||||
}
|
19
src/dijkstrasearch.h
Normal file
19
src/dijkstrasearch.h
Normal file
@ -0,0 +1,19 @@
|
||||
#ifndef DIJKSTRASEARCH_H
|
||||
#define DIJKSTRASEARCH_H
|
||||
|
||||
#include "dijkstragraph.h"
|
||||
|
||||
struct dijkstra_state {
|
||||
dijkstra_cost cost;
|
||||
int visited;
|
||||
};
|
||||
|
||||
struct dijkstra_search {
|
||||
struct dijkstra_state **states;
|
||||
unsigned int node_count;
|
||||
};
|
||||
|
||||
struct dijkstra_search *dijkstra_search_new(struct dijkstra_solver *solver);
|
||||
void dijkstra_search_free(struct dijkstra_search *search);
|
||||
|
||||
#endif // DIJKSTRASEARCH_H
|
20
src/main.c
20
src/main.c
@ -1,9 +1,10 @@
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#include <getopt.h>
|
||||
#include <glib.h>
|
||||
|
||||
#include "dijkstragraph.h"
|
||||
#include <glib/glist.h>
|
||||
#include "dijkstrasearch.h"
|
||||
#include "geojson.h"
|
||||
#include "logging.h"
|
||||
|
||||
@ -44,9 +45,12 @@ int main(int argc, char *argv[])
|
||||
struct dijkstra_node *node;
|
||||
struct dijkstra_path *path;
|
||||
|
||||
struct dijkstra_search *search;
|
||||
|
||||
/* Init stuff */
|
||||
set_log_level(LL_INFO);
|
||||
|
||||
// Process arguments
|
||||
/* Process arguments */
|
||||
while((opt = getopt(argc, argv, "vs")) != -1) {
|
||||
switch(opt)
|
||||
{
|
||||
@ -70,13 +74,14 @@ int main(int argc, char *argv[])
|
||||
return 1;
|
||||
}
|
||||
|
||||
/* Generate solver */
|
||||
solver = dijkstra_solver_new();
|
||||
if (!solver) {
|
||||
report(LL_CRITICAL, "Memory allocation failure.");
|
||||
return 2;
|
||||
}
|
||||
|
||||
/* Parse file list */
|
||||
/* Load map data -> Generate graph */
|
||||
for (i = optind; i < argc; ++i) {
|
||||
add_geojson_to_dijkstra(solver, argv[i]);
|
||||
}
|
||||
@ -104,6 +109,15 @@ int main(int argc, char *argv[])
|
||||
g_list_length(solver->nodes),
|
||||
g_list_length(solver->paths));
|
||||
|
||||
/* Perform search */
|
||||
search = dijkstra_search_new(solver);
|
||||
if (!search) {
|
||||
report(LL_ERROR, "Error allocating dijkstra_search");
|
||||
dijkstra_solver_free(solver);
|
||||
return -3;
|
||||
}
|
||||
|
||||
dijkstra_search_free(search);
|
||||
dijkstra_solver_free(solver);
|
||||
|
||||
return 0;
|
||||
|
Loading…
Reference in New Issue
Block a user