Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Functions for oldest to newest iterator #3

Merged
merged 2 commits into from
Jan 9, 2020
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
61 changes: 61 additions & 0 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,12 @@ pub type Iter<'a, T> = Chain<Rev<SliceIter<'a, T>>, Rev<SliceIter<'a, T>>>;
/// A mutable iterator over `CircularQueue<T>`.
pub type IterMut<'a, T> = Chain<Rev<SliceIterMut<'a, T>>, Rev<SliceIterMut<'a, T>>>;

/// An ascending iterator over `CircularQueue<T>`.
pub type AscIter<'a, T> = Chain<SliceIter<'a, T>, SliceIter<'a, T>>;

/// An mutable ascending iterator over `CircularQueue<T>`.
pub type AscIterMut<'a, T> = Chain<SliceIterMut<'a, T>, SliceIterMut<'a, T>>;

impl<T> CircularQueue<T> {
/// Constructs a new, empty `CircularQueue<T>` with the requested capacity.
///
Expand Down Expand Up @@ -185,6 +191,7 @@ impl<T> CircularQueue<T> {
/// assert_eq!(iter.next(), Some(&3));
/// assert_eq!(iter.next(), Some(&2));
/// ```
#[inline]
pub fn push(&mut self, x: T) {
if self.data.len() < self.capacity() {
self.data.push(x);
Expand Down Expand Up @@ -248,6 +255,60 @@ impl<T> CircularQueue<T> {
let (a, b) = self.data.split_at_mut(self.insertion_index);
a.iter_mut().rev().chain(b.iter_mut().rev())
}

/// Returns an ascending iterator over the queue's contents.
///
/// The iterator goes from the least recently pushed items to the newest ones.
///
/// # Examples
///
/// ```
/// use circular_queue::CircularQueue;
///
/// let mut queue = CircularQueue::with_capacity(3);
/// queue.push(1);
/// queue.push(2);
/// queue.push(3);
/// queue.push(4);
///
/// let mut iter = queue.asc_iter();
///
/// assert_eq!(iter.next(), Some(&2));
/// assert_eq!(iter.next(), Some(&3));
/// assert_eq!(iter.next(), Some(&4));
/// ```
#[inline]
pub fn asc_iter(&self) -> AscIter<T> {
let (a, b) = self.data.split_at(self.insertion_index);
b.iter().chain(a.iter())
}

/// Returns a mutable ascending iterator over the queue's contents.
///
/// The iterator goes from the least recently pushed items to the newest ones.
///
/// # Examples
///
/// ```
/// use circular_queue::CircularQueue;
///
/// let mut queue = CircularQueue::with_capacity(3);
/// queue.push(1);
/// queue.push(2);
/// queue.push(3);
/// queue.push(4);
///
/// let mut iter = queue.asc_iter_mut();
///
/// assert_eq!(iter.next(), Some(&mut 2));
/// assert_eq!(iter.next(), Some(&mut 3));
/// assert_eq!(iter.next(), Some(&mut 4));
/// ```
#[inline]
pub fn asc_iter_mut(&mut self) -> AscIterMut<T> {
let (a, b) = self.data.split_at_mut(self.insertion_index);
b.iter_mut().chain(a.iter_mut())
}
}

impl<T: PartialEq> PartialEq for CircularQueue<T> {
Expand Down