aboutsummaryrefslogtreecommitdiffstats
path: root/src/lib.rs
blob: 5f46419f053e23204c8be5d440e15808733d2807 (plain)
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
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
// -*- coding: utf-8 -*-
//
// Iterator Peekable with multi-forward-peek and multi-backward-peek
//
// Copyright 2024 Michael Büsch <m@bues.ch>
//
// Licensed under the Apache License version 2.0
// or the MIT license, at your option.
// SPDX-License-Identifier: Apache-2.0 OR MIT
//

//! Iterator Peekable with multi-forward-peek and multi-backward-peek
//!
//! This crate is `#![no_std]`, does not heap-allocate and does not contain `unsafe` code.
//!
//! The [Iterator::Item] must implement [Clone].
//!
//! ```
//! use peekable_fwd_bwd::Peekable;
//! use core::slice::Iter;
//!
//! let array = [10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25];
//!
//! const BWD_SIZE: usize = 2; // size of backward peek buffer.
//! const FWD_SIZE: usize = 8; // size of forward peek buffer.
//!
//! let mut iter = Peekable::<Iter<i32>, BWD_SIZE, FWD_SIZE>::new(&array);
//!
//! assert_eq!(iter.next(), Some(&10));
//! assert_eq!(iter.next(), Some(&11));
//!
//! // forward peek into the future.
//! assert_eq!(iter.peek(), Some(&&12));
//! assert_eq!(iter.peek_nth(0), Some(&&12));
//! assert_eq!(iter.peek_nth(1), Some(&&13));
//! assert_eq!(iter.peek_nth(8), None); // FWD_SIZE too small.
//!
//! assert_eq!(iter.next(), Some(&12));
//!
//! // backward peek into the past.
//! assert_eq!(iter.peek_bwd(), Some(&&12));
//! assert_eq!(iter.peek_bwd_nth(0), Some(&&12));
//! assert_eq!(iter.peek_bwd_nth(1), Some(&&11));
//! assert_eq!(iter.peek_bwd_nth(2), None); // BWD_SIZE too small.
//! ```

#![no_std]
#![forbid(unsafe_code)]

use arraydeque::{ArrayDeque, Wrapping};
use core::iter::Fuse;

/// Iterator Peekable with multi-forward-peek and multi-backward-peek
///
/// Generic parameters:
/// - `I`: An [Iterator]. The [Iterator::Item] must implement [Clone].
/// - `BWD_SIZE`: The size of the backward-peek buffer. In number of elements.
/// - `FWD_SIZE`: The size of the forward-peek buffer. In number of elements.
pub struct Peekable<I, const BWD_SIZE: usize, const FWD_SIZE: usize>
where
    I: Iterator,
    I::Item: Clone,
{
    iter: Fuse<I>,
    bwd_buf: ArrayDeque<I::Item, BWD_SIZE, Wrapping>,
    fwd_buf: ArrayDeque<I::Item, FWD_SIZE, Wrapping>,
}

impl<I, const BWD_SIZE: usize, const FWD_SIZE: usize> Peekable<I, BWD_SIZE, FWD_SIZE>
where
    I: Iterator,
    I::Item: Clone,
{
    /// Wrap an iterator into a new [Peekable].
    ///
    /// The [Iterator::Item]s must implement [Clone].
    #[inline]
    pub fn new<II>(iter: II) -> Peekable<II::IntoIter, BWD_SIZE, FWD_SIZE>
    where
        II: IntoIterator,
        II::Item: Clone,
    {
        Peekable {
            iter: iter.into_iter().fuse(),
            bwd_buf: ArrayDeque::new(),
            fwd_buf: ArrayDeque::new(),
        }
    }

    /// Peek the previous element that has last been returned from [Self::next].
    ///
    /// This does neiter advance this iterator nor increment any other internal cursor.
    ///
    /// Successive peeks will return the same element.
    /// See [Self::peek_bwd_nth] for peeking more than one element into the past.
    ///
    /// Returns None, if:
    /// - there was no call to [Self::next], yet, or
    /// - the backward peek buffer is too small to hold one element.
    #[inline]
    pub fn peek_bwd(&mut self) -> Option<&I::Item> {
        self.peek_bwd_nth(0)
    }

    /// Peek the next element.
    ///
    /// This does neiter advance this iterator nor increment any other internal cursor.
    ///
    /// Successive peeks will return the same element.
    /// See [Self::peek_fwd_nth] for peeking more than one element into the future.
    ///
    /// Returns None, if the inner iterator is exhausted and there is no next element.
    #[inline]
    pub fn peek_fwd(&mut self) -> Option<&I::Item> {
        self.peek_fwd_nth(0)
    }

    /// Alias for [Self::peek_fwd].
    ///
    /// This function is just here to make this library more compatible with
    /// other Peekable implementations.
    #[inline]
    pub fn peek(&mut self) -> Option<&I::Item> {
        self.peek_fwd()
    }

    /// Peek the previous n-th element that has been returned from [Self::next].
    ///
    /// - 0 -> Returns the element from the last [Self::next] call.
    /// - 1 -> Returns the element from the previous to last [Self::next] call.
    /// - 2 -> etc ...
    /// - etc ...
    ///
    /// This does neiter advance this iterator nor increment any other internal cursor.
    ///
    /// Successive peeks at the same position will return the same element.
    ///
    /// Returns None, if:
    /// - there were not enough calls to [Self::next], yet, or
    /// - the backward peek buffer is too small to hold `i + 1` elements.
    #[inline]
    pub fn peek_bwd_nth(&mut self, i: usize) -> Option<&I::Item> {
        self.bwd_buf.get(i)
    }

    /// Peek the next n-th element.
    ///
    /// This does neiter advance this iterator nor increment any other internal cursor.
    ///
    /// Successive peeks at the same position will return the same element.
    ///
    /// Returns None, if the inner iterator is exhausted and there is no n-th element.
    pub fn peek_fwd_nth(&mut self, i: usize) -> Option<&I::Item> {
        if i < self.fwd_buf.capacity() {
            while self.fwd_buf.len() <= i {
                self.fwd_buf.push_back(self.iter.next()?);
            }
            Some(&self.fwd_buf[i])
        } else {
            None
        }
    }

    /// Alias for [Self::peek_fwd_nth].
    ///
    /// This is function just here to make this library more compatible with
    /// other Peekable implementations.
    #[inline]
    pub fn peek_nth(&mut self, i: usize) -> Option<&I::Item> {
        self.peek_fwd_nth(i)
    }
}

impl<I, const BWD_SIZE: usize, const FWD_SIZE: usize> Iterator for Peekable<I, BWD_SIZE, FWD_SIZE>
where
    I: Iterator,
    I::Item: Clone,
{
    type Item = I::Item;

    fn next(&mut self) -> Option<Self::Item> {
        let item = self.fwd_buf.pop_front().or_else(|| self.iter.next());
        if let Some(item) = &item {
            self.bwd_buf.push_front(item.clone());
        }
        item
    }
}

#[cfg(test)]
mod tests {
    use super::*;
    use core::slice::Iter;

    #[test]
    fn test_next() {
        let a = [1, 2, 3];
        let mut it = Peekable::<Iter<i32>, 4, 4>::new(&a);

        assert_eq!(it.next(), Some(&1));
        assert_eq!(it.next(), Some(&2));
        assert_eq!(it.next(), Some(&3));
        assert_eq!(it.next(), None);
        assert_eq!(it.next(), None);
        assert_eq!(it.next(), None);
    }

    #[test]
    fn test_peek_fwd() {
        let a = [1, 2, 3];
        let mut it = Peekable::<Iter<i32>, 4, 4>::new(&a);

        assert_eq!(it.peek(), Some(&&1));
        assert_eq!(it.peek(), Some(&&1));
        assert_eq!(it.peek_fwd(), Some(&&1));
        assert_eq!(it.peek_fwd_nth(0), Some(&&1));
        assert_eq!(it.peek_fwd_nth(1), Some(&&2));
        assert_eq!(it.peek_fwd_nth(2), Some(&&3));
        assert_eq!(it.peek_fwd_nth(3), None);
        assert_eq!(it.peek_fwd_nth(4), None);

        assert_eq!(it.next(), Some(&1));
        assert_eq!(it.peek(), Some(&&2));
        assert_eq!(it.peek_fwd_nth(1), Some(&&3));
    }

    #[test]
    fn test_peek_fwd_lim() {
        let a = [1, 2, 3, 4, 5, 6, 7, 8];
        let mut it = Peekable::<Iter<i32>, 2, 4>::new(&a);

        assert_eq!(it.peek_fwd_nth(0), Some(&&1));
        assert_eq!(it.peek_fwd_nth(1), Some(&&2));
        assert_eq!(it.peek_fwd_nth(2), Some(&&3));
        assert_eq!(it.peek_fwd_nth(3), Some(&&4));
        assert_eq!(it.peek_fwd_nth(4), None);
        assert_eq!(it.peek_fwd_nth(5), None);

        assert_eq!(it.next(), Some(&1));
        assert_eq!(it.peek_fwd_nth(0), Some(&&2));
        assert_eq!(it.peek_fwd_nth(1), Some(&&3));
        assert_eq!(it.peek_fwd_nth(2), Some(&&4));
        assert_eq!(it.peek_fwd_nth(3), Some(&&5));
        assert_eq!(it.peek_fwd_nth(4), None);
        assert_eq!(it.peek_fwd_nth(5), None);
    }

    #[test]
    fn test_peek_bwd() {
        let a = [1, 2, 3];
        let mut it = Peekable::<Iter<i32>, 4, 4>::new(&a);

        assert_eq!(it.peek_bwd(), None);
        assert_eq!(it.peek_bwd_nth(0), None);
        assert_eq!(it.peek_bwd_nth(1), None);

        assert_eq!(it.next(), Some(&1));
        assert_eq!(it.peek_bwd(), Some(&&1));
        assert_eq!(it.peek_bwd_nth(1), None);
        assert_eq!(it.peek_bwd_nth(2), None);

        assert_eq!(it.next(), Some(&2));
        assert_eq!(it.peek_bwd(), Some(&&2));
        assert_eq!(it.peek_bwd_nth(1), Some(&&1));
        assert_eq!(it.peek_bwd_nth(2), None);
    }

    #[test]
    fn test_peek_bwd_lim() {
        let a = [1, 2, 3, 4, 5, 6, 7, 8];
        let mut it = Peekable::<Iter<i32>, 2, 4>::new(&a);

        assert_eq!(it.next(), Some(&1));
        assert_eq!(it.next(), Some(&2));
        assert_eq!(it.next(), Some(&3));
        assert_eq!(it.peek_bwd_nth(0), Some(&&3));
        assert_eq!(it.peek_bwd_nth(1), Some(&&2));
        assert_eq!(it.peek_bwd_nth(2), None);
        assert_eq!(it.peek_bwd_nth(3), None);
    }
}

// vim: ts=4 sw=4 expandtab
bues.ch cgit interface