1pub mod deque;
8
9use core::fmt::{Debug, Formatter};
10use std::collections::VecDeque;
11use len_trait::{Clear, Empty, Len};
12use crate::collection::*;
13
14pub trait QueueCollection<T>: Collection + Full
16 where
17 T: PartialEq + PartialOrd + Clone + Debug,
18{
19 fn dequeue(&mut self) -> Option<T>;
22
23 fn enqueue(&mut self, item: T) -> bool;
25
26 fn peek(&self) -> Option<&T>;
28}
29
30const DEF_QUEUE_CAPACITY: usize = 10;
35
36pub struct Queue<T>
39 where
40 T: PartialEq + PartialOrd + Clone + Debug,
41{
42 deq: VecDeque<T>,
44}
45
46impl<T> Clear for Queue<T>
48 where
49 T: PartialEq + PartialOrd + Clone + Debug,
50{
51 fn clear(&mut self) {
53 self.deq.clear()
54 }
55}
56
57impl<T> Clone for Queue<T>
59 where
60 T: PartialEq + PartialOrd + Clone + Debug,
61{
62 fn clone(&self) -> Self {
64 Queue { deq: self.deq.clone() }
65 }
66}
67
68impl<T> Debug for Queue<T>
70 where
71 T: Clone + PartialEq + PartialOrd + Debug,
72{
73 fn fmt(&self, f: &mut Formatter<'_>) -> core::fmt::Result {
75 f.debug_struct("Queue")
76 .field("deq", &self.deq)
77 .finish()
78 }
79}
80
81impl<T> Empty for Queue<T>
83 where
84 T: PartialEq + PartialOrd + Clone + Debug,
85{
86 fn is_empty(&self) -> bool {
88 self.deq.is_empty()
89 }
90}
91
92impl<T> Full for Queue<T>
94 where
95 T: PartialEq + PartialOrd + Clone + Debug,
96{
97 fn is_full(&self) -> bool {
99 self.deq.len() == self.deq.capacity()
100 }
101}
102
103impl<T> IntoIterator for Queue<T>
105 where
106 T: PartialEq + PartialOrd + Clone + Debug,
107{
108 type Item = T;
110 type IntoIter = alloc::collections::vec_deque::IntoIter<T>;
112
113 fn into_iter(self) -> Self::IntoIter {
115 self.deq.into_iter()
116 }
117}
118
119impl<T> Len for Queue<T>
121 where
122 T: PartialEq + PartialOrd + Clone + Debug,
123{
124 fn len(&self) -> usize {
126 self.deq.len()
127 }
128}
129
130impl<T> PartialEq for Queue<T>
132 where
133 T: Clone + PartialEq + PartialOrd + Debug,
134{
135 fn eq(&self, other: &Self) -> bool {
138 if self.len() != other.len() {
140 return false;
141 }
142
143 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
154impl<V> Reversible for Queue<V>
156 where
157 V: Clone + Debug + PartialEq + PartialOrd,
158{
159 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
171impl<T> Collection for Queue<T>
173 where
174 T: PartialEq + PartialOrd + Clone + Debug,
175{
176 type Element = T;
178
179 fn capacity(&self) -> usize {
181 self.deq.capacity()
182 }
183
184 fn contains(&self, item: &T) -> bool {
186 self.deq.contains(item)
187 }
188
189 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 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
212impl<T> QueueCollection<T> for Queue<T>
214 where
215 T: PartialEq + PartialOrd + Clone + Debug,
216{
217 fn dequeue(&mut self) -> Option<T> {
220 self.deq.pop_front()
221 }
222
223 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 fn peek(&self) -> Option<&T> {
235 self.deq.front()
236 }
237}
238
239impl<T> Queue<T>
241 where
242 T: PartialEq + PartialOrd + Clone + Debug,
243{
244 pub fn new() -> Self {
246 Queue { deq: VecDeque::with_capacity(DEF_QUEUE_CAPACITY) }
247 }
248
249 #[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 #[allow(dead_code)]
263 pub fn with_capacity(capacity: usize) -> Self {
264 Queue { deq: VecDeque::with_capacity(capacity) }
265 }
266}