Skip to content

Commit 2314bdb

Browse files
committed
rust refactoring
1 parent d727942 commit 2314bdb

File tree

2 files changed

+37
-35
lines changed

2 files changed

+37
-35
lines changed

results-rust.txt

+17-17
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,18 @@
11
{"map": "randomMedium.txt", "edges": 187, "graph_hash": "10172", "time": "0.275", "nodes": 97, "name": "rust"}
2-
{"map": "tube.txt", "edges": 386, "graph_hash": "77694", "time": "1.988", "nodes": 301, "name": "rust"}
3-
{"map": "sample.txt", "edges": 12, "graph_hash": "52", "time": "0.022", "nodes": 8, "name": "rust"}
4-
{"map": "vancouver.txt", "edges": 3601, "graph_hash": "3987248", "time": "117.323", "nodes": 1986, "name": "rust"}
5-
{"map": "lambda.txt", "edges": 60, "graph_hash": "2454", "time": "0.057", "nodes": 38, "name": "rust"}
6-
{"map": "boston-sparse.txt", "edges": 945, "graph_hash": "310052", "time": "6.015", "nodes": 488, "name": "rust"}
7-
{"map": "edinburgh-sparse.txt", "edges": 1751, "graph_hash": "865590", "time": "27.052", "nodes": 961, "name": "rust"}
8-
{"map": "oxford2-sparse-2.txt", "edges": 3632, "graph_hash": "5686142", "time": "153.934", "nodes": 2389, "name": "rust"}
9-
{"map": "oxford-center-sparse.txt", "edges": 2020, "graph_hash": "4190664", "time": "55.919", "nodes": 1425, "name": "rust"}
10-
{"map": "oxford-10000.txt", "edges": 10000, "graph_hash": "14165324", "time": "1424.255", "nodes": 6658, "name": "rust"}
11-
{"map": "icfp-coauthors-pj.txt", "edges": 3425, "graph_hash": "54594", "time": "23.479", "nodes": 892, "name": "rust"}
12-
{"map": "van-city-sparse.txt", "edges": 3601, "graph_hash": "3987248", "time": "115.222", "nodes": 1986, "name": "rust"}
13-
{"map": "randomSparse.txt", "edges": 123, "graph_hash": "20844", "time": "0.177", "nodes": 86, "name": "rust"}
14-
{"map": "Sierpinski-triangle.txt", "edges": 81, "graph_hash": "2232", "time": "0.068", "nodes": 42, "name": "rust"}
15-
{"map": "nara-sparse.txt", "edges": 2197, "graph_hash": "2541640", "time": "65.781", "nodes": 1560, "name": "rust"}
16-
{"map": "gothenburg-sparse.txt", "edges": 2234, "graph_hash": "982450", "time": "39.337", "nodes": 1175, "name": "rust"}
17-
{"map": "edinburgh-10000.txt", "edges": 10000, "graph_hash": "25273694", "time": "1530.794", "nodes": 7357, "name": "rust"}
18-
{"map": "circle.txt", "edges": 65, "graph_hash": "278", "time": "0.037", "nodes": 27, "name": "rust"}
2+
{"map": "tube.txt", "edges": 386, "graph_hash": "77694", "time": "2.05", "nodes": 301, "name": "rust"}
3+
{"map": "sample.txt", "edges": 12, "graph_hash": "52", "time": "0.023", "nodes": 8, "name": "rust"}
4+
{"map": "vancouver.txt", "edges": 3601, "graph_hash": "3987248", "time": "116.834", "nodes": 1986, "name": "rust"}
5+
{"map": "lambda.txt", "edges": 60, "graph_hash": "2454", "time": "0.056", "nodes": 38, "name": "rust"}
6+
{"map": "boston-sparse.txt", "edges": 945, "graph_hash": "310052", "time": "5.915", "nodes": 488, "name": "rust"}
7+
{"map": "edinburgh-sparse.txt", "edges": 1751, "graph_hash": "865590", "time": "26.167", "nodes": 961, "name": "rust"}
8+
{"map": "oxford2-sparse-2.txt", "edges": 3632, "graph_hash": "5686142", "time": "151.982", "nodes": 2389, "name": "rust"}
9+
{"map": "oxford-center-sparse.txt", "edges": 2020, "graph_hash": "4190664", "time": "54.448", "nodes": 1425, "name": "rust"}
10+
{"map": "oxford-10000.txt", "edges": 10000, "graph_hash": "14165324", "time": "1379.742", "nodes": 6658, "name": "rust"}
11+
{"map": "icfp-coauthors-pj.txt", "edges": 3425, "graph_hash": "54594", "time": "23.968", "nodes": 892, "name": "rust"}
12+
{"map": "van-city-sparse.txt", "edges": 3601, "graph_hash": "3987248", "time": "112.328", "nodes": 1986, "name": "rust"}
13+
{"map": "randomSparse.txt", "edges": 123, "graph_hash": "20844", "time": "0.178", "nodes": 86, "name": "rust"}
14+
{"map": "Sierpinski-triangle.txt", "edges": 81, "graph_hash": "2232", "time": "0.06", "nodes": 42, "name": "rust"}
15+
{"map": "nara-sparse.txt", "edges": 2197, "graph_hash": "2541640", "time": "67.388", "nodes": 1560, "name": "rust"}
16+
{"map": "gothenburg-sparse.txt", "edges": 2234, "graph_hash": "982450", "time": "48.34", "nodes": 1175, "name": "rust"}
17+
{"map": "edinburgh-10000.txt", "edges": 10000, "graph_hash": "25273694", "time": "1509.373", "nodes": 7357, "name": "rust"}
18+
{"map": "circle.txt", "edges": 65, "graph_hash": "278", "time": "0.036", "nodes": 27, "name": "rust"}

rust/src/main.rs

+20-18
Original file line numberDiff line numberDiff line change
@@ -4,13 +4,13 @@ use std::str;
44
use std::collections::VecDeque;
55
use std::time::{Duration, Instant};
66

7-
type Graph = Vec<Vec<i32>>;
7+
type Graph = Vec<Vec<usize>>;
88

99
#[derive(Debug)]
1010
struct Input {
1111
graph: Graph,
1212
mines: Vec<String>,
13-
num_cities: i32,
13+
num_cities: usize,
1414
}
1515

1616
impl Input {
@@ -34,7 +34,7 @@ fn read_from_file(filename: &String) -> Input {
3434
let mut lines = text.lines();
3535
let num_cities: i32 = read_i32(&mut lines);
3636

37-
input.num_cities = num_cities;
37+
input.num_cities = num_cities as usize;
3838
input.graph.resize(num_cities as usize, vec![]);
3939

4040
let num_mines: i32 = read_i32(&mut lines);
@@ -49,31 +49,31 @@ fn read_from_file(filename: &String) -> Input {
4949
for _ in 0..num_edges {
5050
let edge: Vec<&str> = lines.next().unwrap().split(" ").collect();
5151
// println!("{:?}", edge);
52-
let s: i32 = edge[0].parse().unwrap();
53-
let t: i32 = edge[1].parse().unwrap();
54-
input.graph[s as usize].push(t);
55-
input.graph[t as usize].push(s);
52+
let s: usize = edge[0].parse().unwrap();
53+
let t: usize = edge[1].parse().unwrap();
54+
input.graph[s].push(t);
55+
input.graph[t].push(s);
5656
}
5757

5858
input
5959
}
6060

6161
const NOT_PROCESSED: i32 = -1;
6262

63-
fn run_bfs(start: i32, input: &Input, scores: &mut Vec<i32>) {
64-
scores.resize(input.num_cities as usize, NOT_PROCESSED);
65-
scores[start as usize] = 0;
63+
fn run_bfs(start: usize, input: &Input, scores: &mut Vec<i32>) {
64+
scores.resize(input.num_cities, NOT_PROCESSED);
65+
scores[start] = 0;
6666

67-
let mut queue: VecDeque<i32> = VecDeque::new();
67+
let mut queue: VecDeque<usize> = VecDeque::new();
6868
queue.push_back(start);
6969

7070
while !queue.is_empty() {
7171
let now = queue.pop_front().unwrap();
72-
let value = scores[now as usize];
72+
let value = scores[now];
7373

74-
for next in input.graph[now as usize].iter() {
75-
if scores[*next as usize] == NOT_PROCESSED {
76-
scores[*next as usize] = value + 1;
74+
for next in &input.graph[now] {
75+
if scores[*next] == NOT_PROCESSED {
76+
scores[*next] = value + 1;
7777
queue.push_back(*next);
7878
}
7979
}
@@ -85,16 +85,18 @@ fn compute_hash(input: &Input) {
8585
let now = Instant::now();
8686

8787
let mut distances: Vec<Vec<i32>> = vec![];
88-
distances.resize(input.num_cities as usize, vec![]);
88+
distances.resize(input.num_cities, vec![]);
8989

9090
for i in 0..input.num_cities {
91-
run_bfs(i, &input, &mut distances[i as usize]);
91+
let node = i as usize;
92+
run_bfs(node, &input, &mut distances[node]);
9293
}
9394

9495
let mut total_score: i32 = 0;
9596
for i in 0..input.num_cities {
9697
let mut current_score: i32 = 0;
97-
for d in distances[i as usize].iter() {
98+
let node = i as usize;
99+
for d in &distances[node] {
98100
if *d == NOT_PROCESSED {
99101
continue;
100102
}

0 commit comments

Comments
 (0)