quickjs_jit_stdlib/llrt/llrt_utils/
reuse_list.rs1use std::fmt::Debug;
4
5#[derive(Default, Clone)]
6pub struct ReuseList<T> {
7 items: Vec<Option<T>>,
8 slots: Vec<usize>,
9 last_slot_idx: usize,
10 len: usize,
11 slot_size: usize,
12}
13
14impl<T: Debug> Debug for ReuseList<T> {
15 fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
16 f.debug_struct("ReuseList")
17 .field("items", &self.items)
18 .field("slots", &self.slots)
19 .finish()
20 }
21}
22
23impl<T> ReuseList<T> {
24 pub fn new() -> Self {
25 Self::with_capacity(0)
26 }
27
28 pub fn is_empty(&self) -> bool {
30 self.len == 0
31 }
32
33 pub fn with_capacity(capacity: usize) -> Self {
35 Self {
36 items: Vec::with_capacity(capacity),
37 slots: Vec::with_capacity(capacity >> 2),
38 last_slot_idx: 0,
39 len: 0,
40 slot_size: 0,
41 }
42 }
43
44 pub fn append(&mut self, item: T) -> usize {
45 if self.slot_size > 0 {
46 let slot = self.slots[self.last_slot_idx - 1];
48 if slot > 0 {
49 self.items[slot - 1] = Some(item);
50 self.slots[self.last_slot_idx - 1] = 0;
51 if self.last_slot_idx > 1 {
52 self.last_slot_idx -= 1;
53 }
54
55 self.len += 1;
56 return slot - 1;
57 }
58 }
59 self.items.push(Some(item));
61 self.len += 1;
62 self.items.len() - 1
63 }
64
65 pub fn remove(&mut self, index: usize) -> Option<T> {
66 if index >= self.items.len() {
67 return None;
68 }
69
70 let item = self.items[index].take();
71 if item.is_some() {
72 if self.slot_size > 0 && self.slots[self.last_slot_idx - 1] == 0 {
73 self.slots[self.last_slot_idx - 1] = index + 1;
74 } else {
75 self.slots.push(index + 1);
76 self.last_slot_idx += 1;
77 self.slot_size += 1;
78 }
79 self.len -= 1;
80 }
81 item
82 }
83
84 pub fn get(&self, index: usize) -> Option<&T> {
85 if index >= self.items.len() {
86 None
87 } else {
88 self.items[index].as_ref()
89 }
90 }
91
92 pub fn get_mut(&mut self, index: usize) -> Option<&mut T> {
93 if index >= self.items.len() {
94 None
95 } else {
96 self.items[index].as_mut()
97 }
98 }
99
100 pub fn capacity(&self) -> usize {
101 self.items.capacity()
102 }
103
104 pub fn len(&self) -> usize {
105 self.len
106 }
107
108 pub fn iter(&self) -> impl Iterator<Item = &T> {
109 self.items.iter().filter_map(|x| x.as_ref())
110 }
111
112 pub fn iter_mut(&mut self) -> impl Iterator<Item = &mut T> {
113 self.items.iter_mut().filter_map(|x| x.as_mut())
114 }
115
116 pub fn clear(&mut self) {
118 self.items.clear();
119 self.slots.clear();
120 self.last_slot_idx = 0;
121 self.len = 0;
122 self.slot_size = 0;
123 }
124
125 pub fn optimize(&mut self) {
126 let mut new_items = Vec::with_capacity(self.len);
127
128 for item in self.items.iter_mut() {
129 let a = item.take();
130 if a.is_some() {
131 new_items.push(a);
132 }
133 }
134 self.items = new_items;
135 self.slots.clear();
136 self.last_slot_idx = 0;
137 self.slot_size = 0;
138 }
139}
140
141#[cfg(test)]
142mod tests {
143 use super::*;
144
145 #[test]
146 fn test_new() {
147 let list: ReuseList<i32> = ReuseList::new();
148 assert_eq!(list.len(), 0);
149 assert_eq!(list.capacity(), 0);
150 assert_eq!(list.items.len(), 0);
151 assert_eq!(list.slots.len(), 0);
152 }
153
154 #[test]
155 fn test_with_capacity() {
156 let list: ReuseList<i32> = ReuseList::with_capacity(10);
157 assert_eq!(list.len(), 0);
158 assert_eq!(list.capacity(), 10);
159 assert_eq!(list.items.len(), 0);
160 assert_eq!(list.slots.len(), 0);
161 }
162
163 #[test]
164 fn test_append() {
165 let mut list = ReuseList::new();
166 assert_eq!(list.append(1), 0);
167 assert_eq!(list.append(2), 1);
168 assert_eq!(list.append(3), 2);
169 assert_eq!(list.len(), 3);
170
171 let items: Vec<i32> = list.iter().cloned().collect();
172 assert_eq!(items, vec![1, 2, 3]);
173 assert_eq!(list.items, vec![Some(1), Some(2), Some(3)]);
174 assert_eq!(list.slots, Vec::<usize>::new());
175 }
176
177 #[test]
178 fn test_remove() {
179 let mut list = ReuseList::new();
180 list.append(1);
181 list.append(2);
182 list.append(3);
183
184 assert_eq!(list.remove(1), Some(2));
185 assert_eq!(list.len(), 2);
186
187 let items: Vec<i32> = list.iter().cloned().collect();
188 assert_eq!(items, vec![1, 3]);
189 assert_eq!(list.items, vec![Some(1), None, Some(3)]);
190 assert_eq!(list.slots, vec![2]);
191
192 assert_eq!(list.remove(5), None);
193 }
194
195 #[test]
196 fn test_reuse_slots() {
197 let mut list = ReuseList::new();
198 list.append(1);
199 list.append(2);
200 list.append(3);
201
202 list.remove(1); assert_eq!(list.append(4), 1); let items: Vec<i32> = list.iter().cloned().collect();
206 assert_eq!(items, vec![1, 4, 3]);
207 assert_eq!(list.items, vec![Some(1), Some(4), Some(3)]);
208 assert_eq!(list.slots, vec![0]);
209 }
210
211 #[test]
212 fn test_get() {
213 let mut list = ReuseList::new();
214 list.append(1);
215 list.append(2);
216
217 assert_eq!(list.get(0), Some(&1));
218 assert_eq!(list.get(1), Some(&2));
219 assert_eq!(list.get(2), None);
220 assert_eq!(list.items, vec![Some(1), Some(2)]);
221 assert_eq!(list.slots, Vec::<usize>::new());
222 }
223
224 #[test]
225 fn test_get_mut() {
226 let mut list = ReuseList::new();
227 list.append(1);
228 list.append(2);
229
230 if let Some(value) = list.get_mut(0) {
231 *value = 10;
232 }
233
234 assert_eq!(list.get(0), Some(&10));
235 assert_eq!(list.items, vec![Some(10), Some(2)]);
236 assert_eq!(list.slots, Vec::<usize>::new());
237 }
238
239 #[test]
240 fn test_iter() {
241 let mut list = ReuseList::new();
242 list.append(1);
243 list.append(2);
244 list.append(3);
245 list.remove(1);
246
247 let items: Vec<i32> = list.iter().cloned().collect();
248 assert_eq!(items, vec![1, 3]);
249 assert_eq!(list.items, vec![Some(1), None, Some(3)]);
250 assert_eq!(list.slots, vec![2]);
251 }
252
253 #[test]
254 fn test_iter_mut() {
255 let mut list = ReuseList::new();
256 list.append(1);
257 list.append(2);
258 list.append(3);
259
260 for item in list.iter_mut() {
261 *item *= 2;
262 }
263
264 let items: Vec<i32> = list.iter().cloned().collect();
265 assert_eq!(items, vec![2, 4, 6]);
266 assert_eq!(list.items, vec![Some(2), Some(4), Some(6)]);
267 assert_eq!(list.slots, Vec::<usize>::new());
268 }
269
270 #[test]
271 fn test_multiple_removes() {
272 let mut list = ReuseList::new();
273 for i in 0..5 {
274 list.append(i);
275 }
276
277 list.remove(1);
278 list.remove(3);
279
280 let items: Vec<i32> = list.iter().cloned().collect();
281 assert_eq!(items, vec![0, 2, 4]);
282 assert_eq!(list.items, vec![Some(0), None, Some(2), None, Some(4)]);
283 assert_eq!(list.slots, vec![2, 4]);
284
285 list.append(10);
287 list.append(11);
288
289 let items: Vec<i32> = list.iter().cloned().collect();
290 assert_eq!(items, vec![0, 11, 2, 10, 4]);
291 assert_eq!(
292 list.items,
293 vec![Some(0), Some(11), Some(2), Some(10), Some(4)]
294 );
295 assert_eq!(list.slots, vec![0, 0]);
296
297 list.remove(0);
298
299 let items: Vec<i32> = list.iter().cloned().collect();
300 assert_eq!(items, vec![11, 2, 10, 4]);
301 assert_eq!(list.items, vec![None, Some(11), Some(2), Some(10), Some(4)]);
302 assert_eq!(list.slots, vec![1, 0]);
303
304 list.append(20);
305
306 let items: Vec<i32> = list.iter().cloned().collect();
307 assert_eq!(items, vec![20, 11, 2, 10, 4]);
308 assert_eq!(
309 list.items,
310 vec![Some(20), Some(11), Some(2), Some(10), Some(4)]
311 );
312 assert_eq!(list.slots, vec![0, 0]);
313
314 list.clear();
316 }
317
318 #[test]
319 fn test_optimize() {
320 let mut list = ReuseList::new();
321 list.append(1);
322 list.append(2);
323 list.append(3);
324 list.remove(1);
325
326 assert_eq!(list.items, vec![Some(1), None, Some(3)]);
327 assert_eq!(list.slots, vec![2]);
328
329 list.optimize();
330
331 assert_eq!(list.items, vec![Some(1), Some(3)]);
332 assert_eq!(list.slots, Vec::<usize>::new());
333 assert_eq!(list.last_slot_idx, 0);
334 assert_eq!(list.slot_size, 0);
335
336 let items: Vec<i32> = list.iter().cloned().collect();
337 assert_eq!(items, vec![1, 3]);
338 }
339}