-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
doubly_shuffling_around.rs
149 lines (122 loc) · 4 KB
/
doubly_shuffling_around.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
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
use criterion::{criterion_group, criterion_main, BenchmarkId, Criterion};
use orx_linked_list::*;
use rand::prelude::*;
use rand_chacha::ChaCha8Rng;
fn get_cities(num_cities: usize) -> impl Iterator<Item = City> {
(0..num_cities).map(|id| City {
id,
name: format!("city-{}", id),
coordinates: [7, 42],
})
}
fn get_moves(num_moves: usize, num_cities: usize) -> Vec<(usize, usize)> {
let mut rng = ChaCha8Rng::seed_from_u64(56456);
let mut moves = vec![];
for _ in 0..num_moves {
let a = rng.gen_range(0..num_cities);
let b = rng.gen_range(0..num_cities);
moves.push((a, b));
}
moves
}
#[derive(PartialEq, Eq)]
struct City {
id: usize,
name: String,
coordinates: [i32; 2],
}
struct TourVecCities {
cities: Vec<City>,
}
impl TourVecCities {
fn insert_after(&mut self, city: usize, city_to_succeed: usize) {
if city == city_to_succeed {
return;
}
let mut pos_a = usize::MAX;
let mut pos_b = usize::MAX;
for (i, c) in self.cities.iter().enumerate() {
match c.id {
x if x == city => {
pos_a = i;
if pos_b < usize::MAX {
break;
}
}
x if x == city_to_succeed => {
pos_b = i;
if pos_a < usize::MAX {
break;
}
}
_ => {}
}
}
let target_position = match pos_a < pos_b {
true => pos_b,
false => pos_b + 1,
};
let city = self.cities.remove(pos_a);
self.cities.insert(target_position, city);
}
fn create_tour(num_cities: usize, moves: &[(usize, usize)]) -> Self {
let cities = get_cities(num_cities).collect();
let mut tour = TourVecCities { cities };
for (city, city_to_succeed) in moves {
tour.insert_after(*city, *city_to_succeed);
}
tour
}
}
struct TourLinkedList {
cities: DoublyList<City>,
idx: Vec<DoublyIdx<City>>,
}
impl TourLinkedList {
fn insert_after(&mut self, city: usize, city_to_succeed: usize) {
let a = &self.idx[city];
let b = &self.idx[city_to_succeed];
self.cities.move_next_to(&a, &b);
}
fn create_tour(num_cities: usize, moves: &[(usize, usize)]) -> Self {
let cities: DoublyList<_> = get_cities(num_cities).collect();
let idx = cities.indices().collect();
let mut tour = TourLinkedList { cities, idx };
for (city, city_to_succeed) in moves {
tour.insert_after(*city, *city_to_succeed);
}
tour
}
}
fn bench(c: &mut Criterion) {
let treatments = vec![
(10, 10_000),
(100, 10_000),
(1_000, 10_000),
(10_000, 10_000),
(100_000, 10_000),
];
let mut group = c.benchmark_group("doubly_shuffling_around");
for (num_cities, num_moves) in &treatments {
let n = &format!("num_cities:{};num_moves:{}", num_cities, num_moves);
let moves = get_moves(*num_moves, *num_cities);
let result_vec = TourVecCities::create_tour(*num_cities, &moves);
let result_list = TourLinkedList::create_tour(*num_cities, &moves);
assert!(result_list.cities.eq_to_iter_refs(&result_vec.cities));
group.bench_with_input(BenchmarkId::new("TourLinkedList", n), n, |b, _| {
b.iter(|| {
let result = TourLinkedList::create_tour(*num_cities, &moves);
assert_eq!(result.cities.len(), result_vec.cities.len());
})
});
group.bench_with_input(BenchmarkId::new("TourVec", n), n, |b, _| {
b.iter(|| {
let result = TourVecCities::create_tour(*num_cities, &moves);
assert_eq!(result.cities.len(), result_vec.cities.len());
})
});
}
group.finish();
}
criterion_group!(benches, bench);
criterion_main!(benches);