first commit
[synclust.git] / src / adapters / a.dijkstra.c
1 #include <R.h>
2 #include <Rdefines.h>
3 #include "sources/dijkstra.h"
4
5 // Dijkstra from index start : return vector of distances to every other vertex
6 // NOTE [space VS perf]: passing neighborhoods infos would be enough, but
7 // require extra computation to translate R list argument
8 SEXP dijkstra(
9 SEXP distances_,
10 SEXP start_
11 ) {
12 // get arguments
13 SEXP dim = getAttrib(distances_, R_DimSymbol);
14 int n = INTEGER(dim)[0];
15 double* pDistsIn = REAL(distances_);
16 int start = INTEGER_VALUE(start_) - 1; // R indices start at 1
17
18 // Main call to core algorithm
19 double* distances = dijkstra_core(pDistsIn, start, n);
20
21 // allocate vector output and obtain R vector object
22 SEXP distsOut;
23 PROTECT(distsOut = allocVector(REALSXP, n));
24 double* pDistsOut = NUMERIC_POINTER(distsOut);
25 for (int i=0; i<n; i++)
26 pDistsOut[i] = distances[i];
27
28 free(distances);
29 UNPROTECT(1);
30
31 return distsOut;
32 }