-
Notifications
You must be signed in to change notification settings - Fork 46
/
rs_unsafe.rs
64 lines (50 loc) · 1.98 KB
/
rs_unsafe.rs
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
55
56
57
58
59
60
61
62
63
extern crate time;
use std::io::{BufferedReader, File};
use time::precise_time_ns;
struct Route {
dest: i32,
cost: i32,
}
struct Node {
neighbours: Vec<Route>,
}
fn read_places() -> Vec<Node> {
let path = Path::new("agraph");
let mut file = BufferedReader::new(File::open(&path));
let mut lines = file.lines().map(|x| x.unwrap());
let numnodes: uint = match lines.next() {
Some(num) => from_str(num.as_slice().trim()).unwrap(),
_ => panic!("Error, first line of file should describe the amount of nodes")
};
let mut nodes = Vec::from_fn(numnodes, |_| Node { neighbours: Vec::with_capacity(numnodes) });
for line in lines {
let nums: Vec<&str> = line.split(' ').collect();
let node : uint = from_str(nums[0] ).expect("Error: node id was not a uint");
let neighbour: i32 = from_str(nums[1] ).expect("Error: neighbour id was not an int");
let cost : i32 = from_str(nums[2].trim()).expect("Error: route cost was not an int");
nodes[node].neighbours.push(Route {dest: neighbour, cost: cost});
}
return nodes;
}
fn get_longest_path(nodes: &Vec<Node>, node_id: i32, visited: &mut Vec<bool>) -> i32 {
unsafe {*visited.unsafe_mut(node_id as uint) = true;}
let mut max = 0i32;
for neighbour in nodes[node_id as uint].neighbours.iter() {
if ! unsafe{*visited.unsafe_get(neighbour.dest as uint)} {
let dist = neighbour.cost + get_longest_path(nodes, neighbour.dest, visited);
if dist > max {
max = dist;
}
}
}
unsafe {*visited.unsafe_mut(node_id as uint) = false;}
return max;
}
fn main() {
let nodes = read_places();
let mut visited = Vec::from_elem(nodes.len(), false);
let startTime = precise_time_ns();
let path = get_longest_path(&nodes, 0, &mut visited);
let duration = (precise_time_ns() - startTime) / 1000000;
println!("{} LANGUAGE RustUnsafe {}", path, duration);
}