csc_7b_fc/
circularqueue.rs1pub struct CircularQueue<T, const INITCAP: usize = 64> {
7 q: Vec<Option<T>>,
8 front: usize,
9 size: usize,
10}
11
12impl<T, const INITCAP: usize> CircularQueue<T, INITCAP> {
13 pub const fn len(&self) -> usize {
17 self.size
18 }
19
20 pub fn capacity(&self) -> usize {
22 self.q.len()
23 }
24
25 pub fn new() -> Self {
28 let mut v = Vec::with_capacity(INITCAP);
30 v.resize_with(INITCAP, || None); CircularQueue {
34 front: 0,
35 size: 0,
36 q: v,
37 }
38 } fn index(&self, i: usize) -> usize {
41 (self.front + i) % self.q.len()
42 } fn resize(&mut self) {
45 let newcap = self.q.len() * 2;
47 let mut newq = Vec::with_capacity(newcap);
48 newq.resize_with(newcap, || None);
49 let size = self.size;
50 for i in 0..size {
51 let k = self.index(i);
52 std::mem::swap(&mut self.q[k], &mut newq[i]);
53 }
54 self.q = newq; self.front = 0;
56 } pub fn push_back(&mut self, x: T) {
61 if self.size >= self.q.len() {
62 self.resize();
63 }
64 let back = self.index(self.size);
65 self.q[back] = Some(x); self.size += 1;
67 }
68
69 pub fn push_front(&mut self, x: T) {
72 if self.size >= self.q.len() {
73 self.resize();
74 }
75 let newfront = self.index(self.q.len() - 1);
76 self.q[newfront] = Some(x);
77 self.front = newfront;
78 self.size += 1;
79 }
80
81 pub fn pop_back(&mut self) -> Option<T> {
83 if self.size == 0 {
84 return None;
85 }
86 let mut answer = None;
87 let last = self.index(self.size - 1);
88 std::mem::swap(&mut answer, &mut self.q[last]);
89 self.size -= 1;
90 answer
91 }
92
93 pub fn pop_front(&mut self) -> Option<T> {
95 if self.size == 0 {
96 return None;
97 }
98 let mut answer = None;
99 let first = self.index(0);
100 std::mem::swap(&mut answer, &mut self.q[first]);
101 self.front = self.index(1);
102 self.size -= 1;
103 answer
104 }
105
106 pub fn mapfun<F: FnMut(&mut T)>(&mut self, mut f: F) {
110 for i in 0..self.size {
111 let k = self.index(i);
112 f(self.q[k].as_mut().unwrap());
113 }
114 }
115
116 pub fn iter<'lt>(&'lt self) -> Cqiter<'lt, T, INITCAP> {
118 Cqiter { cq: self, index: 0 }
119 } } use std::ops::{Index, IndexMut};
124
125impl<T, const C: usize> Index<usize> for CircularQueue<T, C> {
126 type Output = T;
127 fn index(&self, i: usize) -> &Self::Output {
128 let k = self.index(i);
129 self.q[k].as_ref().unwrap()
130 }
131}
132
133impl<T, const C: usize> IndexMut<usize> for CircularQueue<T, C> {
134 fn index_mut(&mut self, i: usize) -> &mut Self::Output {
135 let k = self.index(i);
136 self.q[k].as_mut().unwrap()
137 }
138}
139
140pub struct Cqiter<'lt, T, const C: usize> {
143 cq: &'lt CircularQueue<T, C>,
144 index: usize, }
146impl<'lt, T, const C: usize> Iterator for Cqiter<'lt, T, C> {
147 type Item = &'lt T;
148 fn next(&mut self) -> Option<Self::Item> {
149 if self.index >= self.cq.len() {
150 None
151 } else {
152 let answer = Some(&self.cq[self.index]);
153 self.index += 1;
154 answer
155 }
156 } } impl<'lt, T, const C: usize> IntoIterator for &'lt CircularQueue<T, C> {
162 type Item = &'lt T;
163 type IntoIter = Cqiter<'lt, T, C>;
164 fn into_iter(self) -> Self::IntoIter {
165 self.iter()
167 }
168} pub fn main() {
172 let mut cq = CircularQueue::<usize>::new();
173 for i in 0..100 {
174 cq.push_back(i * 2);
175 cq.push_front(i * 2 + 1);
176 }
177 cq.mapfun(|x| *x = *x + 1);
178 for _ in 0..50 {
179 cq.pop_front();
180 cq.pop_back();
181 }
182 for x in &cq {
184 print!("{} ", x);
185 }
186 println!("\nsize: {}, capacity {}", cq.len(), cq.capacity());
187 println!("cq[5] : {}", cq[5]);
188 cq[5] = 99999;
189 println!("cq[5] : {}", cq[5]);
190 let mut sum = 0;
191 cq.mapfun(|x| sum += *x);
192 println!("sum = {}", sum);
193}