structures/
queue.rs

1//! # Queue
2//!
3//! Contains a 'QueueCollection' trait for implementing a queue, as well as a default implementation
4//! of a queue called 'Queue'. A 'queue' is a list of elements that can append new elements to the
5//! back and remove elements from the front.
6
7pub mod deque;
8
9use core::fmt::{Debug, Formatter};
10use std::collections::VecDeque;
11use len_trait::{Clear, Empty, Len};
12use crate::collection::*;
13
14// A trait for 'collections' that can implement a 'queue'.
15pub trait QueueCollection<T>: Collection + Full
16    where
17        T: PartialEq + PartialOrd + Clone + Debug,
18{
19    /// Removes the first element from the 'queue' if there is one. Returns the first element or
20    /// None if there isn't one.
21    fn dequeue(&mut self) -> Option<T>;
22
23    /// Appends the specified element to the end of the 'queue'. Returns true if successful.
24    fn enqueue(&mut self, item: T) -> bool;
25
26    /// Returns the first element in the 'queue' or None if there isn't one.
27    fn peek(&self) -> Option<&T>;
28}
29
30////////////////////////////////////////////////////////////////////////////////////////////////////
31// Queue
32////////////////////////////////////////////////////////////////////////////////////////////////////
33/// The default capacity for a new empty 'queue'.
34const DEF_QUEUE_CAPACITY: usize = 10;
35
36/// A collection that dequeues (removes) elements from the front and enqueues (adds) elements to
37/// the end.
38pub struct Queue<T>
39    where
40        T: PartialEq + PartialOrd + Clone + Debug,
41{
42    /// The VecDeque backing this 'queue'.
43    deq: VecDeque<T>,
44}
45
46// Clear function for Queue
47impl<T> Clear for Queue<T>
48    where
49        T: PartialEq + PartialOrd + Clone + Debug,
50{
51    /// Clears all elements from this 'queue'.
52    fn clear(&mut self) {
53        self.deq.clear()
54    }
55}
56
57// Clone function for Queue
58impl<T> Clone for Queue<T>
59    where
60        T: PartialEq + PartialOrd + Clone + Debug,
61{
62    /// Returns a clone of this 'queue'.
63    fn clone(&self) -> Self {
64        Queue { deq: self.deq.clone() }
65    }
66}
67
68// Debug function for Queue
69impl<T> Debug for Queue<T>
70    where
71        T: Clone + PartialEq + PartialOrd + Debug,
72{
73    /// Displays the debug information for this 'queue'.
74    fn fmt(&self, f: &mut Formatter<'_>) -> core::fmt::Result {
75        f.debug_struct("Queue")
76            .field("deq", &self.deq)
77            .finish()
78    }
79}
80
81// Empty function for Queue
82impl<T> Empty for Queue<T>
83    where
84        T: PartialEq + PartialOrd + Clone + Debug,
85{
86    /// Returns true if this 'queue' is empty.
87    fn is_empty(&self) -> bool {
88        self.deq.is_empty()
89    }
90}
91
92// Full function for Queue
93impl<T> Full for Queue<T>
94    where
95        T: PartialEq + PartialOrd + Clone + Debug,
96{
97    /// Returns true if this 'stack' is full.
98    fn is_full(&self) -> bool {
99        self.deq.len() == self.deq.capacity()
100    }
101}
102
103// IntoIterator function for Queue
104impl<T> IntoIterator for Queue<T>
105    where
106        T: PartialEq + PartialOrd + Clone + Debug,
107{
108    /// The Item type.
109    type Item = T;
110    /// The IntoIter type.
111    type IntoIter = alloc::collections::vec_deque::IntoIter<T>;
112
113    /// Converts this 'queue' into an 'iterator'.
114    fn into_iter(self) -> Self::IntoIter {
115        self.deq.into_iter()
116    }
117}
118
119// Length function for Queue
120impl<T> Len for Queue<T>
121    where
122        T: PartialEq + PartialOrd + Clone + Debug,
123{
124    /// Returns the length of this 'queue'.
125    fn len(&self) -> usize {
126        self.deq.len()
127    }
128}
129
130// PartialEq function for Queue
131impl<T> PartialEq for Queue<T>
132    where
133        T: Clone + PartialEq + PartialOrd + Debug,
134{
135    /// Returns true if this 'queue' and the specified 'queue' are equal, meaning they are the same
136    /// length and contain the same elements.
137    fn eq(&self, other: &Self) -> bool {
138        // If lengths do not match, return false.
139        if self.len() != other.len() {
140            return false;
141        }
142
143        // If a value does not match, return false.
144        for i in 0..self.len() {
145            if self.deq[i] != other.deq[i] {
146                return false;
147            }
148        }
149
150        true
151    }
152}
153
154// Reversible function for Queue
155impl<V> Reversible for Queue<V>
156    where
157        V: Clone + Debug + PartialEq + PartialOrd,
158{
159    /// Returns a copy of this 'queue' in reverse order.
160    fn reverse(&mut self) -> Self {
161        let mut rev: Queue<V> = Queue::new();
162
163        for i in (0..self.len()).rev() {
164            rev.enqueue(self.deq[i].clone());
165        }
166
167        rev
168    }
169}
170
171// Collection functions for Queue
172impl<T> Collection for Queue<T>
173    where
174        T: PartialEq + PartialOrd + Clone + Debug,
175{
176    /// The element type.
177    type Element = T;
178    
179    /// Returns the capacity of this 'queue'.
180    fn capacity(&self) -> usize {
181        self.deq.capacity()
182    }
183
184    /// Returns true if this 'queue' contains the specified element.
185    fn contains(&self, item: &T) -> bool {
186        self.deq.contains(item)
187    }
188
189    /// Returns true if this 'queue' contains the specified vector.
190    fn contains_all(&self, vec: &Vec<T>) -> bool {
191        for i in 0..vec.len() {
192            if !self.deq.contains(&vec[i]) {
193                return false;
194            }
195        }
196
197        true
198    }
199
200    /// Returns a 'vector' containing the elements of this 'queue'.
201    fn to_vec(&self) -> Vec<T> {
202        let mut vec: Vec<T> = Vec::new();
203
204        for i in self.clone().into_iter() {
205            vec.push(i);
206        }
207
208        vec
209    }
210}
211
212// QueueCollection functions for Queue
213impl<T> QueueCollection<T> for Queue<T>
214    where
215        T: PartialEq + PartialOrd + Clone + Debug,
216{
217    /// Removes the first element from the 'queue' if there is one. Returns the first element or
218    /// None if there isn't one.
219    fn dequeue(&mut self) -> Option<T> {
220        self.deq.pop_front()
221    }
222
223    /// Appends the specified element to the end of the 'queue'. Returns true if successful or false
224    /// if the 'queue' is full.
225    fn enqueue(&mut self, item: T) -> bool {
226        if self.is_full() { return false; }
227
228        self.deq.push_back(item);
229
230        true
231    }
232
233    /// Returns the first element in the 'queue' or None if there isn't one.
234    fn peek(&self) -> Option<&T> {
235        self.deq.front()
236    }
237}
238
239// Queue functions
240impl<T> Queue<T>
241    where
242        T: PartialEq + PartialOrd + Clone + Debug,
243{
244    /// Creates a new empty 'queue' with a default capacity of 10.
245    pub fn new() -> Self {
246        Queue { deq: VecDeque::with_capacity(DEF_QUEUE_CAPACITY) }
247    }
248
249    /// Creates a new 'queue' that contains the elements in the specified 'vector'.
250    #[allow(dead_code)]
251    pub fn from_vec(v: &Vec<T>) -> Self {
252        let mut queue: Queue<T> = Queue { deq: VecDeque::new() };
253
254        for i in v.into_iter() {
255            queue.deq.push_back(i.clone());
256        }
257
258        queue
259    }
260
261    /// Creates a new 'queue' with the specified capacity.
262    #[allow(dead_code)]
263    pub fn with_capacity(capacity: usize) -> Self {
264        Queue { deq: VecDeque::with_capacity(capacity) }
265    }
266}