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
use core::fmt;
use core::mem::MaybeUninit;
use core::ops::{Index, IndexMut};
use core::{ptr, slice};

pub struct VecDeque<T, const N: usize> {
    buf: MaybeUninit<[T; N]>,
    end: usize,
    //Tail always points to the first element
    start: usize,
    is_full: bool,
}
impl<T, const N: usize> VecDeque<T, N> {
    const CAPACITY: usize = N;
    pub const fn new() -> Self {
        VecDeque {
            buf: MaybeUninit::uninit(),
            end: 0,
            start: 0,
            is_full: false,
        }
    }
    fn ptr(&self) -> *mut T {
        self.buf.as_ptr() as *mut T
    }
    pub fn capacity(&self) -> usize {
        Self::CAPACITY
    }
    pub fn len(&self) -> usize {
        let start = self.start;
        let end = self.end;
        if self.is_full() {
            self.capacity()
        } else if end >= start {
            end - start
        } else {
            self.capacity() - start + end
        }
    }
    pub fn is_empty(&self) -> bool {
        self.start == self.end && !self.is_full
    }
    pub fn is_full(&self) -> bool {
        self.is_full
    }
    #[inline]
    unsafe fn buffer_read(&mut self, off: usize) -> T {
        ptr::read(self.ptr().add(off))
    }
    #[inline]
    unsafe fn buffer_write(&mut self, off: usize, value: T) {
        ptr::write(self.ptr().add(off), value);
    }
    #[inline]
    fn wrap_add(&self, idx: usize, addend: usize) -> usize {
        let (index, overflow) = idx.overflowing_add(addend);
        if index >= self.capacity() || overflow {
            index.wrapping_sub(self.capacity())
        } else {
            index
        }
    }
    #[inline]
    fn wrap_sub(&self, idx: usize, subtrahend: usize) -> usize {
        let (index, overflow) = idx.overflowing_sub(subtrahend);
        if overflow {
            index.wrapping_add(self.capacity())
        } else {
            index
        }
    }
    pub fn get(&self, index: usize) -> Option<&T> {
        if index < self.len() {
            let idx = self.wrap_add(self.start, index);
            unsafe { Some(&*self.ptr().add(idx)) }
        } else {
            None
        }
    }
    pub fn get_mut(&mut self, index: usize) -> Option<&mut T> {
        if index < self.len() {
            let idx = self.wrap_add(self.start, index);
            unsafe { Some(&mut *self.ptr().add(idx)) }
        } else {
            None
        }
    }
    pub fn as_slices(&self) -> (&[T], &[T]) {
        let ptr = self.ptr() as *const T;
        if self.end >= self.start && !self.is_full {
            (
                unsafe { slice::from_raw_parts(ptr.add(self.start), self.end - self.start) },
                &mut [],
            )
        } else {
            (
                unsafe { slice::from_raw_parts(ptr.add(self.start), N - self.start) },
                unsafe { slice::from_raw_parts(ptr, self.end) },
            )
        }
    }
    pub fn as_mut_slices(&mut self) -> (&mut [T], &mut [T]) {
        let ptr = self.ptr();
        if self.end >= self.start && !self.is_full {
            (
                unsafe { slice::from_raw_parts_mut(ptr.add(self.start), self.end - self.start) },
                &mut [],
            )
        } else {
            (
                unsafe { slice::from_raw_parts_mut(ptr.add(self.start), N - self.start) },
                unsafe { slice::from_raw_parts_mut(ptr, self.end) },
            )
        }
    }
    pub fn clear(&mut self) {
        let (a, b) = self.as_mut_slices();
        unsafe { ptr::drop_in_place(a) };
        unsafe { ptr::drop_in_place(b) };
        self.end = 0;
        self.start = 0;
        self.is_full = false;
    }
    pub fn pop_front(&mut self) -> Option<T> {
        if self.is_empty() {
            None
        } else {
            let start = self.start;
            self.start = self.wrap_add(self.start, 1);
            if self.is_full {
                self.is_full = false;
            }
            unsafe { Some(self.buffer_read(start)) }
        }
    }
    pub fn pop_back(&mut self) -> Option<T> {
        if self.is_empty() {
            None
        } else {
            self.end = self.wrap_sub(self.end, 1);
            let end = self.end;
            if self.is_full {
                self.is_full = false;
            }
            unsafe { Some(self.buffer_read(end)) }
        }
    }
    pub fn push_front(&mut self, value: T) -> Result<(), T> {
        if self.is_full() {
            return Err(value);
        }

        if self.len() == self.capacity() - 1 {
            self.is_full = true;
        }
        self.start = self.wrap_sub(self.start, 1);
        unsafe { self.buffer_write(self.start, value) };
        Ok(())
    }
    pub fn push_back(&mut self, value: T) -> Result<(), T> {
        if self.is_full() {
            return Err(value);
        }

        if self.len() == self.capacity() - 1 {
            self.is_full = true;
        }
        unsafe { self.buffer_write(self.end, value) };
        self.end = self.wrap_add(self.end, 1);
        Ok(())
    }
}
impl<T, const N: usize> Index<usize> for VecDeque<T, N> {
    type Output = T;

    #[inline]
    fn index(&self, index: usize) -> &T {
        self.get(index).expect("Out of bounds access")
    }
}

impl<T, const N: usize> IndexMut<usize> for VecDeque<T, N> {
    #[inline]
    fn index_mut(&mut self, index: usize) -> &mut T {
        self.get_mut(index).expect("Out of bounds access")
    }
}
impl<T: fmt::Debug, const N: usize> fmt::Debug for VecDeque<T, N> {
    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
        fmt::Debug::fmt(&self.as_slices(), f)
    }
}
impl<T, const N: usize> Drop for VecDeque<T, N> {
    fn drop(&mut self) {
        self.clear()
    }
}