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
#![deny(missing_docs,
missing_debug_implementations, missing_copy_implementations,
trivial_casts,
unsafe_code,
unstable_features,
unused_import_braces, unused_qualifications)]
#![allow(unknown_lints)]
#[cfg(test)]
extern crate rand;
extern crate arrayvec;
mod internal;
pub use internal::{ProgressHandler, TimeDelta, TimePoint, TimeSpan};
use std::vec::from_elem;
use internal::{Aligner, prepare_time_spans};
pub fn align(list: Vec<TimeSpan>,
reference: Vec<TimeSpan>,
split_penalty_normalized: f64,
progress_handler: Option<Box<ProgressHandler>>)
-> Vec<TimeDelta> {
let (list_nonoverlapping, list_indices) = prepare_time_spans(list.clone());
let (ref_nonoverlapping, _) = prepare_time_spans(reference.clone());
if list_nonoverlapping.is_empty() || ref_nonoverlapping.is_empty() {
return from_elem(TimeDelta::zero(), list.len());
}
let list_len = list_nonoverlapping.len();
let aligner_opt = Aligner::new(list_nonoverlapping,
ref_nonoverlapping,
split_penalty_normalized,
progress_handler);
let deltas = match aligner_opt {
Some(mut aligner) => aligner.align_all_spans(),
None => (0..list_len).map(|_| TimeDelta::zero()).collect(),
};
list_indices.into_iter().map(|i| deltas[i]).collect()
}
#[cfg(test)]
mod tests {
use super::*;
use rand;
use rand::Rng;
use internal::{TimePoint, prepare_time_spans};
fn predefined_time_spans() -> Vec<Vec<TimeSpan>> {
let t0 = TimePoint::from(0);
let t1000 = TimePoint::from(1000);
let t2000 = TimePoint::from(2000);
vec![vec![],
vec![TimeSpan::new(t0, t0)],
vec![TimeSpan::new(t0, t1000)],
vec![TimeSpan::new(t0, t1000), TimeSpan::new(t1000, t1000)],
vec![TimeSpan::new(t0, t1000), TimeSpan::new(t1000, t1000), TimeSpan::new(t1000, t2000)],
vec![TimeSpan::new(t1000, t1000), TimeSpan::new(t1000, t1000)]]
}
fn generate_random_time_spans() -> Vec<TimeSpan> {
let mut rng = rand::thread_rng();
let len: usize = (rng.next_u32() % 400) as usize;
let mut v = Vec::with_capacity(len);
let mut current_pos = 0i64;
for _ in 0..len {
current_pos += (rng.next_u32() % 200) as i64 - 50;
let current_len = (rng.next_u32() % 400) as i64;
v.push(TimeSpan::new(TimePoint::from(current_pos),
TimePoint::from(current_pos + current_len)));
}
v
}
pub fn get_test_time_spans() -> Vec<Vec<TimeSpan>> {
(0..100)
.map(|_| generate_random_time_spans())
.chain(predefined_time_spans().into_iter())
.collect()
}
pub fn get_random_prepared_test_time_spans() -> Vec<TimeSpan> {
prepare_time_spans(generate_random_time_spans()).0
}
}