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
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
use crate::utils::access;
use crate::value::Value;
use crate::{config::Config, vec_wrapper::VecWrapper};
use rayon::{
iter::{
FromParallelIterator, IntoParallelIterator, IntoParallelRefIterator,
},
slice::Iter,
vec::IntoIter,
};
use serde_derive::{Deserialize, Serialize};
use std::{iter::FromIterator, ops::Index};
#[derive(Clone, Debug, Deserialize, Eq, Hash, PartialEq, Serialize)]
pub struct Schedule<T>(Vec<Config<T>>);
pub type IntegralSchedule = Schedule<i32>;
pub type FractionalSchedule = Schedule<f64>;
impl<'a, T> Schedule<T>
where
T: Value<'a>,
{
pub fn new(x: Vec<Config<T>>) -> Schedule<T> {
Schedule(x)
}
pub fn empty() -> Schedule<T> {
Schedule(vec![])
}
pub fn repeat(x: Config<T>, t: i32) -> Schedule<T> {
Schedule(vec![x; t as usize])
}
pub fn is_empty(&self) -> bool {
self.t_end() == 0
}
pub fn t_end(&self) -> i32 {
self.0.len() as i32
}
pub fn now(&self) -> Config<T> {
self[self.0.len() - 1].clone()
}
pub fn now_with_default(&self, default: Config<T>) -> Config<T> {
if self.is_empty() {
default
} else {
self[self.0.len() - 1].clone()
}
}
pub fn get(&self, t: i32) -> Option<&Config<T>> {
access(&self.0, t)
}
pub fn shift(&mut self, x: Config<T>) {
self.0.insert(0, x)
}
pub fn push(&mut self, x: Config<T>) {
self.0.push(x)
}
pub fn extend(&self, x: Config<T>) -> Schedule<T> {
Schedule([&self.0[..], &[x]].concat())
}
pub fn to_vec(&self) -> Vec<Vec<T>> {
self.0.iter().map(|x| x.to_vec()).collect()
}
pub fn from_raw(d: i32, w: i32, raw_xs: &[T]) -> Schedule<T> {
assert_eq!(
raw_xs.len() as i32,
Schedule::<T>::raw_encoding_len(d, w),
"length of raw encoding does not match expected length"
);
Schedule::new(
(0..w as usize)
.into_iter()
.map(|t| {
let i = d as usize * t;
Config::new(raw_xs[i..i + d as usize].to_vec())
})
.collect(),
)
}
pub fn build_raw(w: i32, x: &Config<T>) -> Vec<T> {
let raw_xs: Vec<T> = (0..w as usize)
.into_iter()
.flat_map(|_| x.iter().cloned())
.collect();
assert_eq!(
raw_xs.len() as i32,
Schedule::<T>::raw_encoding_len(x.d(), w),
"length of raw encoding does not match expected length"
);
raw_xs
}
pub fn raw_encoding_len(d: i32, w: i32) -> i32 {
d * w
}
}
impl<'a, T> Index<usize> for Schedule<T>
where
T: Value<'a>,
{
type Output = Config<T>;
fn index(&self, t: usize) -> &Config<T> {
assert!(
t < self.0.len(),
"argument must denote one of {} time steps, is {}",
self.0.len(),
t + 1
);
&self.0[t]
}
}
impl<'a, T> VecWrapper for Schedule<T>
where
T: Value<'a>,
{
type Item = Config<T>;
fn to_vec(&self) -> &Vec<Self::Item> {
&self.0
}
}
impl<'a, T> FromIterator<Config<T>> for Schedule<T>
where
T: Value<'a>,
{
fn from_iter<I>(iter: I) -> Self
where
I: IntoIterator<Item = Config<T>>,
{
Schedule::new(Vec::<Config<T>>::from_iter(iter))
}
}
impl<'a, T> FromParallelIterator<Config<T>> for Schedule<T>
where
T: Value<'a>,
{
fn from_par_iter<I>(iter: I) -> Self
where
I: IntoParallelIterator<Item = Config<T>>,
{
Schedule::new(Vec::<Config<T>>::from_par_iter(iter))
}
}
impl<'a, 'b, T> IntoParallelIterator for &'a Schedule<T>
where
T: Value<'b>,
{
type Item = &'a Config<T>;
type Iter = Iter<'a, Config<T>>;
fn into_par_iter(self) -> Self::Iter {
self.0.par_iter()
}
}
impl<'a, T> IntoParallelIterator for Schedule<T>
where
T: Value<'a>,
{
type Item = Config<T>;
type Iter = IntoIter<Config<T>>;
fn into_par_iter(self) -> Self::Iter {
self.0.into_par_iter()
}
}