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
150
151
pub mod best;
pub mod worst;
#[cfg(test)]
mod tests;
use crate::{Segment, Time, TimeSpan, TimingMethod};
#[derive(Copy, Clone, Default, Debug, PartialEq, Eq)]
pub struct Prediction {
pub time: TimeSpan,
pub predecessor: usize,
}
pub fn calculate_best(
segments: &[Segment],
simple_calculation: bool,
use_current_run: bool,
method: TimingMethod,
) -> Option<TimeSpan> {
let mut predictions = vec![None; segments.len() + 1];
best::calculate(
segments,
&mut predictions,
simple_calculation,
use_current_run,
method,
)
}
pub fn calculate_worst(
segments: &[Segment],
use_current_run: bool,
method: TimingMethod,
) -> Option<TimeSpan> {
let mut predictions = vec![None; segments.len() + 1];
worst::calculate(segments, &mut predictions, use_current_run, method)
}
fn track_current_run(
segments: &[Segment],
current_time: Option<TimeSpan>,
segment_index: usize,
method: TimingMethod,
) -> (usize, Time) {
if let Some(first_split_time) = segment_index
.checked_sub(1)
.map_or(Some(TimeSpan::zero()), |i| segments[i].split_time()[method])
{
for (segment_index, segment) in segments.iter().enumerate().skip(segment_index) {
let second_split_time = segment.split_time()[method];
if let Some(second_split_time) = second_split_time {
return (
segment_index + 1,
Time::new().with_timing_method(
method,
current_time.map(|t| second_split_time - first_split_time + t),
),
);
}
}
}
(0, Time::default())
}
fn track_personal_best_run(
segments: &[Segment],
current_time: Option<TimeSpan>,
segment_index: usize,
method: TimingMethod,
) -> (usize, Time) {
if let Some(first_split_time) = segment_index
.checked_sub(1)
.map_or(Some(TimeSpan::zero()), |i| {
segments[i].personal_best_split_time()[method]
})
{
for (segment_index, segment) in segments.iter().enumerate().skip(segment_index) {
let second_split_time = segment.personal_best_split_time()[method];
if let Some(second_split_time) = second_split_time {
return (
segment_index + 1,
Time::new().with_timing_method(
method,
current_time.map(|t| second_split_time - first_split_time + t),
),
);
}
}
}
(0, Time::default())
}
pub fn track_branch(
segments: &[Segment],
current_time: Option<TimeSpan>,
segment_index: usize,
run_index: i32,
method: TimingMethod,
) -> (usize, Time) {
for (segment_index, segment) in segments.iter().enumerate().skip(segment_index) {
if let Some(cur_time) = segment.segment_history().get(run_index) {
if let Some(cur_time) = cur_time[method] {
return (
segment_index + 1,
Time::new().with_timing_method(method, current_time.map(|t| cur_time + t)),
);
}
} else {
break;
}
}
(0, Time::default())
}