File: distance_to_head.cpp

package info (click to toggle)
vg 1.30.0%2Bds-1
  • links: PTS, VCS
  • area: main
  • in suites: bullseye
  • size: 267,848 kB
  • sloc: cpp: 446,974; ansic: 116,148; python: 22,805; cs: 17,888; javascript: 11,031; sh: 5,866; makefile: 4,039; java: 1,415; perl: 1,303; xml: 442; lisp: 242
file content (54 lines) | stat: -rw-r--r-- 1,130 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
#include "distance_to_head.hpp"

#include <unordered_map>

namespace vg {
namespace algorithms {

using namespace std;

bool is_head_node(handle_t h, const HandleGraph* g) {
	bool no_left_edges = true;
	h = g->forward(h);
	g->follow_edges(h, true, [&](const handle_t& ignored) {
        // We found a left edge!
        no_left_edges = false;
        // We only need one
        return false;
    });
    return no_left_edges;
}

int32_t distance_to_head(handle_t h, int32_t limit, const HandleGraph* graph) {
	unordered_set<handle_t> seen;
	return distance_to_head(h, limit, 0, seen, graph);
}

int32_t distance_to_head(handle_t h, int32_t limit, int32_t dist, unordered_set<handle_t>& seen, const HandleGraph* graph) {
	if (seen.count(h)) return -1;
	seen.insert(h);
	if (limit <= 0) {
		return -1;
	}
	
	if(is_head_node(h, graph)) {
		return dist;
	}

	int32_t t = -1; 

	graph->follow_edges(h, true, [&](const handle_t& current) {
		int32_t l = graph->get_length(current);
		t = distance_to_head(current, limit-l, dist+l, seen, graph);
		if (t != -1) {
			return false;
		}
		else {
			return true;
		}
	});
	return t;
}
}
}