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
284
285
286
287
288
289
290
291
292
293
294
295
296
//! The queue module contains a series of structs to create queues and their utility functionalities using cyclic lists.
//! 
//! As a result, the queue inherits the O(1) insertion and deletion for enqueuing & dequeuing.

use std::{fmt::{Display, Debug}, collections::LinkedList, ops::{DerefMut, Deref}};

use crate::{list::List, error::Error};

#[cfg(test)]
mod tests;

// #[derive(Default, PartialEq)]
// struct PriorityQueue<const SIZE: usize, T, const WRITE_OVER: bool> (List<SIZE, T, WRITE_OVER>) where T: PartialOrd + PartialEq;

/// `Queue` is the `struct` used to define the state of a queue using cyclic [`List`]. As a result, the queue inherits the O(1) insertion and deletion for enqueuing & dequeuing.
/// 
/// # Generics
/// List types are derived using 3 generics.
/// 
/// 1. `const SIZE: usize`
/// 
/// SIZE is a generic constant [^note] that defines the maximum size of the queue
/// 
/// 2. `T: Sized`
/// 
/// T is the type of element stored in the queue
/// 
/// 3. `const WRITE_OVER: bool>`
/// 
/// # Creating Queue
/// 
/// Queue can be created in a couple of ways.
/// 
/// 1. Empty Queue
/// 
/// Empty Queue are created using the [`Default`] trait implementation for Queue.
/// 
/// ```
/// # use cyclic_data_types::queue::Queue;
/// # const SIZE: usize = 5;
/// let queue: Queue<SIZE, i64, false> = Queue::default();
/// 
/// assert_eq!(queue.len(), 0);
/// ```
/// 
/// 2. From Array
/// 
/// Queue can also be derived from arrays. The maximum size of the queue is the same size as the array. This is done using the [`From<[SIZE; T]`] trait implementation for List.
/// 
/// ```
/// # use cyclic_data_types::queue::Queue;
/// # const SIZE: usize = 5;
/// let mut queue: Queue<SIZE, i64, false> = [1i64,2i64,3i64,4i64,5i64].into();
/// #
/// # assert_eq!(queue.len(), 5);
/// # assert_eq!(queue.dequeue(), Some(1i64));
/// # assert_eq!(queue.dequeue(), Some(2i64));
/// # assert_eq!(queue.dequeue(), Some(3i64));
/// # assert_eq!(queue.dequeue(), Some(4i64));
/// # assert_eq!(queue.dequeue(), Some(5i64));
/// # assert_eq!(queue.dequeue(), None);
/// ```
/// 
/// 3. From Vectors, Linked Lists and Iterators
/// 
/// Since collections (Vectors, Linked Lists and Iterators) cannot guarantee a size at compile time - the conversion is not always guaranteed to succeed. This occurs when collection is larger than the queue variant. As a result, the new queue cannot be created without resulting in a [`Error::Overflow`]. This can be resolved by either making sure the collection is at max the same size as the queue variant or the cyclic list variant permits `WRITE_OVER`.
/// 
/// Therefore, the [`TryFrom`] trait implementation of queue must be used.
/// 
/// Example of a successful conversion
/// ```
/// # use cyclic_data_types::queue::Queue;
/// const SIZE: usize = 5;
/// let queue: Queue<SIZE, i64, false> = Queue::try_from(vec![1i64,2i64,3i64,4i64,5i64])
///     .unwrap();
/// #
/// # assert_eq!(queue.len(), 5);
/// # assert_eq!(queue, [1i64,2i64,3i64,4i64,5i64].into());
/// ```
/// ```
/// # use cyclic_data_types::queue::Queue;
/// const SIZE: usize = 5;
/// let queue: Queue<SIZE, i64, true> = vec![1i64,2i64,3i64,4i64,5i64,6i64].try_into()
///     .unwrap();
/// #
/// # assert_eq!(queue.len(), 5);
/// # assert_eq!(queue, [2i64,3i64,4i64,5i64,6i64].into());
/// ```
/// Example of a failed conversion
/// ```
/// # use cyclic_data_types::queue::Queue;
/// # use cyclic_data_types::error::Error;
/// const SIZE: usize = 5;
/// let queue: Result<Queue<SIZE, i64, false>, Error> = Queue::try_from(vec![1i64,2i64,3i64,4i64,5i64,6i64]);
/// 
/// assert_eq!(queue, Err(Error::Overflow))
/// ```
/// 
/// WRITE_OVER is a generic constant [^note] that is used to determine if elements should be over written on overflow
/// [note]: [Generic Constraints](https://rust-lang.github.io/rfcs/2000-const-generics.html)
#[derive(Default, PartialEq)]
pub struct Queue<const SIZE: usize, T, const WRITE_OVER: bool> (List<SIZE, T, WRITE_OVER>);

impl<const SIZE: usize, T, const WRITE_OVER: bool> Queue<SIZE, T, WRITE_OVER> {
    /// Returns the number of elements in the queue.
    /// 
    /// ```
    /// # use cyclic_data_types::queue::Queue;
    /// # const SIZE: usize = 5;
    /// let mut queue: Queue<SIZE, i64, false> = Queue::default();
    /// 
    /// assert_eq!(queue.len(), 0);
    /// 
    /// assert!(queue.enqueue(1).is_ok());
    /// 
    /// assert_eq!(queue.len(), 1);
    /// ```
    pub fn len(&self) -> usize {
        self.0.len()
    }

    ///  Pushes an element to the end of the queue.
    /// 
    /// ```
    /// # use cyclic_data_types::queue::Queue;
    /// # const SIZE: usize = 5;
    /// 
    /// let mut queue: Queue<SIZE, i64, false> = Queue::default();
    /// 
    /// assert!(queue.enqueue(1).is_ok());
    /// 
    /// assert!(queue.enqueue(2).is_ok());
    /// 
    /// # assert_eq!(queue.len(), 2);
    /// ```
    pub fn enqueue(&mut self, elem: T) -> Result<&mut Self, Error> {
        match self.0.push_back(elem) {
            Ok(_) => Ok(self),
            Err(err) => Err(err),
        }
    }
    /// Returns a reference to the first element in the queue.
    /// 
    /// ```
    /// # use cyclic_data_types::queue::Queue;
    /// # const SIZE: usize = 5;
    /// 
    /// let mut queue: Queue<SIZE, i64, false> = vec![1,2,3].try_into().unwrap();
    /// 
    /// # assert_eq!(queue.len(), 3);
    /// assert_eq!(queue.peek(), Some(&1));
    /// ```
    pub fn peek(&mut self) -> Option<&T> {
        if self.0.len() == 0 {
            return None;
        }
        Some(&self.0[0usize])
    }

    /// Returns the first element of the queue - after removing said element from the queue.
    /// 
    /// ```
    /// # use cyclic_data_types::queue::Queue;
    /// # const SIZE: usize = 5;
    /// 
    /// let mut queue: Queue<SIZE, i64, false> = vec![1,2,3].try_into().unwrap();
    /// 
    /// # assert_eq!(queue.len(), 3);
    /// assert_eq!(queue.dequeue(), Some(1));
    /// # assert_eq!(queue.len(), 2);
    /// assert_eq!(queue.dequeue(), Some(2));
    /// # assert_eq!(queue.len(), 1);
    /// assert_eq!(queue.dequeue(), Some(3));
    /// # assert_eq!(queue.len(), 0);
    /// assert_eq!(queue.dequeue(), None);
    /// ```
    pub fn dequeue(&mut self) -> Option<T> {
        self.0.remove_front()
    }
}

impl<const S: usize, T, const W: bool> Display for Queue<S, T, W> where T: Display{
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        write!(f, "{}", self.0)
    }
}

impl<const S: usize, T, const W: bool> Debug for Queue<S, T, W> where T: Debug{
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        f.debug_struct("Queue")
            .field("", &self.0)
            .finish()
    }
}

impl<const QUEUE_SIZE: usize, T, const WRITE_OVER: bool> TryFrom<Vec<T>> for Queue<QUEUE_SIZE, T, WRITE_OVER> where T: Clone + Default {
    type Error = Error;

    fn try_from(value: Vec<T>) -> Result<Self, Self::Error> {
        Ok(
            Queue(
                {
                    match value.try_into() {
                        Ok(value) => value,
                        Err(err) => {
                            return Err(err)
                        },
                    }
                }
            )
        )
    }
}

impl<const QUEUE_SIZE: usize, T, const WRITE_OVER: bool> TryFrom<LinkedList<T>> for Queue<QUEUE_SIZE, T, WRITE_OVER> where T: Clone + Default {
    type Error = Error;

    fn try_from(value: LinkedList<T>) -> Result<Self, Self::Error> {
        Ok(
            Queue(
                {
                    match value.try_into() {
                        Ok(value) => value,
                        Err(err) => {
                            return Err(err)
                        },
                    }
                }
            )
        )
    }
}

impl<const QUEUE_SIZE: usize, T, const WRITE_OVER: bool> FromIterator<T> for Queue<QUEUE_SIZE, T, WRITE_OVER> where T: Default {
    fn from_iter<A: IntoIterator<Item = T>>(iter: A) -> Self {
        Queue(iter.into_iter().collect())
    }
}

//generic generator
impl<const QUEUE_SIZE: usize, T, const WRITE_OVER: bool> TryFrom<Box<dyn Iterator<Item = T>>> for Queue<QUEUE_SIZE, T, WRITE_OVER> where T: Clone + Default {
    type Error = Error;

    fn try_from(value: Box<dyn Iterator<Item = T>>) -> Result<Self, Self::Error> {
        Ok(
            Queue(
                {
                    match value.try_into() {
                        Ok(value) => value,
                        Err(err) => {
                            return Err(err)
                        },
                    }
                }
            )
        )
    }
}

impl<const QUEUE_SIZE: usize, T, const WRITE_OVER: bool> From<[T; QUEUE_SIZE]> for Queue<QUEUE_SIZE, T, WRITE_OVER> {
    fn from(value: [T; QUEUE_SIZE]) -> Self {
        Queue(value.into())
    }
}

impl<const QUEUE_SIZE: usize, T, const WRITE_OVER: bool> From<List<QUEUE_SIZE, T, WRITE_OVER>> for Queue<QUEUE_SIZE, T, WRITE_OVER>{
    fn from(value: List<QUEUE_SIZE, T, WRITE_OVER>) -> Self {
        Self(value)
    }
}

impl<const QUEUE_SIZE: usize, T> From<Queue<QUEUE_SIZE, T, true>> for Queue<QUEUE_SIZE, T, false> {
    fn from(value: Queue<QUEUE_SIZE, T, true>) -> Self {
        Self(value.0.into())
    }
}

impl<const QUEUE_SIZE: usize, T> From<Queue<QUEUE_SIZE, T, false>> for Queue<QUEUE_SIZE, T, true> {
    fn from(value: Queue<QUEUE_SIZE, T, false>) -> Self {
        Self(value.0.into())
    }
}

impl<const QUEUE_SIZE: usize, T, const WRITE_OVER: bool> Deref for Queue<QUEUE_SIZE, T, WRITE_OVER> {
    type Target = List<QUEUE_SIZE, T, WRITE_OVER>;

    fn deref(&self) -> &Self::Target {
        &self.0
    }
}

impl<const QUEUE_SIZE: usize, T, const WRITE_OVER: bool> DerefMut for Queue<QUEUE_SIZE, T, WRITE_OVER> {
    fn deref_mut(&mut self) -> &mut Self::Target {
        &mut self.0
    }
}