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
/// A fixed-size queue.
/// The `const`-parameter `C` denotes the capacity.
#[derive(Clone, Copy, Debug)]
pub struct Queue<T, const C: usize> {
    elements: [Option<T>; C],
    head: usize,
    length: usize,
}

impl<T: Copy, const C: usize> Queue<T, C> {
    /// Constructs a new, empty `Queue<T, C>`.
    pub fn new() -> Self {
        let elements: [Option<T>; C] = [None; C];
        Self {
            elements,
            head: 0,
            length: 0,
        }
    }
}

impl<T, const C: usize> Queue<T, C> {
    /// Returns the number of elements the queue can hold.
    ///
    /// # Example
    /// ```
    /// use fundamental::Queue;
    /// 
    /// let queue = Queue::<i32, 4>::new();
    /// assert_eq!(queue.capacity(), 4);
    /// ```
    #[inline]
    pub const fn capacity(&self) -> usize {
        C
    }

    /// Returns the number of elements in the queue.
    /// 
    /// # Example
    /// ```
    /// use fundamental::Queue;
    /// 
    /// let mut queue = Queue::<i32, 1>::new();
    /// assert_eq!(queue.len(), 0);
    /// queue.enqueue(1);
    /// assert_eq!(queue.len(), 1);
    /// ```
    #[inline]
    pub const fn len(&self) -> usize {
        self.length
    }

    /// Returns `true` if the queue contains no elements.
    /// 
    /// # Example
    /// ```
    /// use fundamental::Queue;
    /// 
    /// let mut queue = Queue::<i32, 1>::new();
    /// assert_eq!(queue.is_empty(), true);
    /// queue.enqueue(1);
    /// assert_eq!(queue.is_empty(), false);
    /// ```
    #[inline]
    pub const fn is_empty(&self) -> bool {
        self.len() == 0
    }

    /// Returns `true` if the queue cannot contain any more elements.
    /// 
    /// # Example
    /// ```
    /// use fundamental::Queue;
    /// 
    /// let mut queue = Queue::<i32, 1>::new();
    /// assert_eq!(queue.is_full(), false);
    /// queue.enqueue(1);
    /// assert_eq!(queue.is_full(), true);
    /// ```
    #[inline]
    pub const fn is_full(&self) -> bool {
        self.len() == self.capacity()
    }

    /// Returns a reference to the element at the given index relative
    /// to the start of the queue.
    /// Returns `None` if there is no element at the position.
    /// 
    /// # Example
    /// ```
    /// use fundamental::Queue;
    /// 
    /// let mut queue = Queue::<i32, 1>::new();
    /// assert_eq!(queue.get(0), None);
    /// queue.enqueue(1);
    /// assert_eq!(queue.get(0), Some(&1));
    /// ```
    #[inline]
    pub const fn get(&self, index: usize) -> Option<&T> {
        // If the index is greater than or equal to `len`,
        // then the computed index would wrap around more
        // than once, making it incorrect.
        if index >= self.len() {
            return None;
        }
        self.elements[(self.head + index) % self.capacity()].as_ref()
    }

    /// Returns the index of the first occupied slot in the queue.
    #[inline]
    const fn head(&self) -> usize {
        self.head
    }

    /// Returns the index of the first empty slot in the queue.
    #[inline]
    const fn tail(&self) -> usize {
        (self.head + self.len()) % self.capacity()
    }

    /// Returns a reference to the underlying storage of the queue.
    /// 
    /// # Example
    /// ```
    /// use fundamental::Queue;
    /// 
    /// let mut queue = Queue::<i32, 3>::new();
    /// assert_eq!(queue.as_slice(), &[None, None, None]);
    /// ```
    #[inline]
    pub const fn as_slice(&self) -> &[Option<T>] {
        &self.elements
    }

    /// Insert an element at the back of the queue.
    /// Returns `Err(element)` if the queue is full.
    /// 
    /// # Example
    /// ```
    /// use fundamental::Queue;
    /// 
    /// let mut queue = Queue::<i32, 3>::new();
    /// assert_eq!(queue.as_slice(), &[None, None, None]);
    /// let _ = queue.enqueue(1);
    /// assert_eq!(queue.as_slice(), &[Some(1), None, None]);
    /// let _ = queue.enqueue(2);
    /// assert_eq!(queue.as_slice(), &[Some(1), Some(2), None]);
    /// ```
    #[inline]
    pub fn enqueue(&mut self, element: T) -> Result<(), T> {
        if self.is_full() {
            return Err(element);
        }
        self.elements[self.tail()] = Some(element);
        self.length += 1;
        Ok(())
    }

    /// Take an element out of the front of the queue.
    /// Returns `None` if the queue is empty.
    /// 
    /// # Example
    /// ```
    /// use fundamental::Queue;
    /// 
    /// let mut queue = Queue::<i32, 3>::new();
    /// queue.enqueue(1);
    /// queue.enqueue(2);
    /// queue.enqueue(3);
    /// assert_eq!(queue.dequeue(), Some(1));
    /// assert_eq!(queue.dequeue(), Some(2));
    /// assert_eq!(queue.dequeue(), Some(3));
    /// assert_eq!(queue.dequeue(), None);
    /// ```
    #[inline]
    pub fn dequeue(&mut self) -> Option<T> {
        let element = self.elements[self.head()].take();
        if element.is_some() {
            self.head = (self.head + 1) % self.capacity();
            self.length -= 1;
        }
        element
    }
}

#[cfg(test)]
mod tests {
    use super::Queue;

    #[test]
    fn new() {
        let queue = Queue::<usize, 5>::new();
        assert_eq!(queue.capacity(), 5);
        assert_eq!(queue.len(), 0);
        assert_eq!(queue.head(), 0);
        assert_eq!(queue.tail(), 0);
        assert_eq!(queue.is_empty(), true);
        assert_eq!(queue.is_full(), false);
    }

    #[test]
    fn enqueue() {
        let mut queue = Queue::<usize, 2>::new();
        assert_eq!(queue.enqueue(1), Ok(()));
        assert_eq!(queue.len(), 1);
        assert_eq!(queue.head(), 0);
        assert_eq!(queue.tail(), 1);
        assert_eq!(queue.is_empty(), false);
        assert_eq!(queue.is_full(), false);

        assert_eq!(queue.enqueue(2), Ok(()));
        assert_eq!(queue.len(), 2);
        assert_eq!(queue.head(), 0);
        assert_eq!(queue.tail(), 0);
        assert_eq!(queue.is_empty(), false);
        assert_eq!(queue.is_full(), true);

        assert_eq!(queue.enqueue(3), Err(3));
        assert_eq!(queue.len(), 2);
        assert_eq!(queue.head(), 0);
        assert_eq!(queue.tail(), 0);
        assert_eq!(queue.is_empty(), false);
        assert_eq!(queue.is_full(), true);
    }

    #[test]
    fn dequeue() {
        let mut queue = Queue::<usize, 2>::new();
        assert_eq!(queue.enqueue(1), Ok(()));
        assert_eq!(queue.enqueue(2), Ok(()));

        assert_eq!(queue.dequeue(), Some(1));
        assert_eq!(queue.len(), 1);
        assert_eq!(queue.head(), 1);
        assert_eq!(queue.tail(), 0);
        assert_eq!(queue.is_empty(), false);
        assert_eq!(queue.is_full(), false);

        assert_eq!(queue.dequeue(), Some(2));
        assert_eq!(queue.len(), 0);
        assert_eq!(queue.head(), 0);
        assert_eq!(queue.tail(), 0);
        assert_eq!(queue.is_empty(), true);
        assert_eq!(queue.is_full(), false);
    }
}