thomasp85 / tidygraph
1
#include <Rcpp.h>
2
using namespace Rcpp;
3

4
//[[Rcpp::export]]
5 0
List get_paths(IntegerVector parent) {
6 0
  std::deque< std::deque<int> > paths;
7
  int i, next;
8 0
  LogicalVector last = is_na(parent);
9 0
  for (i = 0; i < parent.size(); ++i) {
10 0
    std::deque<int> path;
11 0
    next = i;
12 0
    while(!last[next]) {
13 0
      next = parent[next] - 1;
14 0
      path.push_back(next + 1);
15
    }
16 0
    std::reverse(path.begin(), path.end());
17 0
    paths.push_back(path);
18
  }
19 0
  return wrap(paths);
20
}
21

22
//[[Rcpp::export]]
23 0
List collect_offspring(ListOf<IntegerVector> offspring, IntegerVector order) {
24 0
  std::deque< std::deque<int> > offsprings;
25
  int i, j, node, n_children, child;
26 0
  for (i = 0; i < order.size(); ++i) {
27 0
    std::deque<int> off(offspring[i].begin(), offspring[i].end());
28 0
    offsprings.push_back(off);
29
  }
30 0
  for (i = 0; i < order.size(); ++i) {
31 0
    node = order[i] - 1;
32 0
    n_children = offsprings[node].size();
33 0
    for (j = 0; j < n_children; ++j) {
34 0
      child = offsprings[node][j] - 1;
35 0
      offsprings[node].insert(offsprings[node].end(), offsprings[child].begin(), offsprings[child].end());
36
    }
37
  }
38 0
  return wrap(offsprings);
39
}

Read our documentation on viewing source code .

Loading