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
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
use std::collections::vec_deque::Drain;
use std::collections::VecDeque;
use core::time::Duration;
use std::marker::PhantomData;

type Item<T> = (Duration, T);

pub type TimeVecItem<T> = Item<T>;

#[derive(Clone, Debug)]
pub struct TimeVec<T> {
    pub limit: Duration,
    buffer: VecDeque<Item<T>>,
}

impl<T> TimeVec<T> {
    pub fn new(limit: Duration, capacity: usize) -> Self {
        let buffer = VecDeque::with_capacity(capacity);
        Self { limit, buffer }
    }

    pub fn builder() -> TimeVecBuilder<T> {
        TimeVecBuilder::<T>::default()
    }

    #[inline]
    pub fn checked_duration(&self) -> Option<Duration> {
        self.buffer
            .front()
            .map(|oldest| self.buffer.back().unwrap().0 - oldest.0)
    }

    #[inline]
    pub fn duration(&self) -> Duration {
        self.checked_duration().unwrap_or(Duration::ZERO)
    }

    #[inline]
    fn check_timestamp(&self, value: Duration) -> bool {
        self.buffer
            .back()
            .map(|i| value > i.0)
            .unwrap_or(true)
    }

    #[inline]
    pub fn push(&mut self, timestamp: Duration, item: T) -> Option<Drain<Item<T>>> {
        if self.check_timestamp(timestamp) {
            self.buffer.push_back((timestamp, item));

            let partition_timestamp = timestamp.saturating_sub(self.limit);
            let partition_point = self.buffer.partition_point(|i| i.0 < partition_timestamp);

            Some(self.buffer.drain(0..partition_point))
        } else {
            None
        }
    }

    #[inline]
    pub fn pop_front(&mut self) -> Option<Item<T>> {
        self.buffer.pop_front()
    }

    #[inline]
    pub fn pop_back(&mut self) -> Option<Item<T>> {
        self.buffer.pop_back()
    }

    #[inline]
    pub fn duration_from_back(&self, duration: &Duration) -> Option<Duration> {
        self.buffer.back().map(|item| duration.checked_sub(item.0)).flatten()
    }

    #[inline]
    pub fn clear(&mut self) {
        self.buffer.clear()
    }

    #[inline]
    pub fn len(&self) -> usize {
        self.buffer.len()
    }

    #[inline]
    pub fn iter<'a>(&'a self) -> impl ExactSizeIterator<Item = &Item<T>> + 'a {
        self.buffer.iter()
    }

    #[inline]
    pub fn iter_data<'a>(&'a self) -> impl ExactSizeIterator<Item = &T> + 'a {
        self.buffer.iter().map(|snap| &snap.1)
    }

    #[inline]
    pub fn iter_time<'a>(&'a self) -> impl ExactSizeIterator<Item = &Duration> + 'a {
        self.buffer.iter().map(|snap| &snap.0)
    }

    #[inline]
    pub fn is_empty(&self) -> bool {
        self.buffer.is_empty()
    }

    #[inline]
    pub fn drain(&mut self) -> Drain<Item<T>> {
        self.buffer.drain(..)
    }
}

#[derive(Copy, Clone, Debug)]
pub struct TimeVecBuilder<T> {
    pub limit: Option<Duration>,
    pub capacity: Option<usize>,
    _item: PhantomData<T>,
}

impl<T> Default for TimeVecBuilder<T> {
    fn default() -> Self {
        Self { limit: None, capacity: None, _item: PhantomData }
    }
}

impl<T> TimeVecBuilder<T> {
    pub fn with_limit(mut self, value: Duration) -> Self {
        self.limit = Some(value);
        self
    }

    pub fn with_limit_secs(self, value: u64) -> Self {
        self.with_limit(Duration::from_secs(value))
    }

    pub fn with_limit_micros(self, value: u64) -> Self {
        self.with_limit(Duration::from_micros(value))
    }

    pub fn with_limit_millis(self, value: u64) -> Self {
        self.with_limit(Duration::from_millis(value))
    }

    pub fn with_limit_nanos(self, value: u64) -> Self {
        self.with_limit(Duration::from_nanos(value))
    }

    pub fn with_capacity(mut self, value: usize) -> Self {
        self.capacity = Some(value);
        self
    }

    pub fn build(self) -> TimeVec<T> {
        TimeVec {
            limit: self.limit.unwrap_or_default(),
            buffer: self.capacity
                .map(|value| VecDeque::<Item<T>>::with_capacity(value))
                .unwrap_or_default()
                
        }
    }
}

#[cfg(test)]
mod tests {
    use super::*;
    use std::time::Duration;

    #[test]
    fn with_zero_limit() {
        let mut tv = TimeVec::<()>::builder()
            .with_limit(Duration::ZERO)
            .build();
        assert!(tv.is_empty());

        assert_eq!(tv.checked_duration(), None);
        assert_eq!(tv.duration(), Duration::ZERO);
        assert_eq!(tv.len(), 0);

        tv.push(Duration::from_secs(1), ());
        assert_eq!(tv.checked_duration(), Some(Duration::ZERO));
        assert_eq!(tv.duration(), Duration::ZERO);
        assert_eq!(tv.len(), 1);
    }

    #[test]
    fn push_two_items_with_same_timestamp() {
        let mut tv = TimeVec::<()>::builder()
            .with_limit_nanos(1)
            .build();
        
        tv.push(Duration::from_secs(1), ());
        assert_eq!(tv.len(), 1);

        tv.push(Duration::from_secs(1), ());
        assert_eq!(tv.len(), 1);
    }

    #[test]
    fn min_limit() {
        let mut tv = TimeVec::<()>::builder()
            .with_limit_nanos(1)
            .build();
        
        tv.push(Duration::ZERO, ());
        assert_eq!(tv.len(), 1);

        tv.push(Duration::from_nanos(1), ());
        assert_eq!(tv.len(), 2);

        tv.push(Duration::from_nanos(2), ());
        assert_eq!(tv.checked_duration(), Some(Duration::from_nanos(1)));
        assert_eq!(tv.len(), 2);
    }

    #[test]
    fn push_above_the_non_min_limit() {
        let mut tv = TimeVec::<()>::builder()
            .with_limit_nanos(3)
            .build();
        
        tv.push(Duration::ZERO, ());
        tv.push(Duration::from_nanos(1), ());
        tv.push(Duration::from_nanos(2), ());
        tv.push(Duration::from_nanos(3), ());
        assert_eq!(tv.len(), 4);

        tv.push(Duration::from_nanos(4), ());
        assert_eq!(tv.len(), 4);
    }
}