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
#![warn(missing_docs)]
mod controller;
pub mod error;
pub mod iter;
mod slot;
#[cfg(test)]
mod test;
pub use controller::Controller;
use error::{ArenaFull, InsertWithKeyError};
use iter::{DrainFilter, Iter, IterMut};
use slot::{ArenaSlot, ArenaSlotState};
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
pub struct Key {
index: usize,
generation: usize,
}
#[derive(Debug)]
pub struct Arena<T> {
controller: Controller,
slots: Vec<ArenaSlot<T>>,
first_occupied_slot_index: Option<usize>,
}
impl<T> Arena<T> {
pub fn new(capacity: usize) -> Self {
Self {
controller: Controller::new(capacity),
slots: (0..capacity).map(|_| ArenaSlot::new()).collect(),
first_occupied_slot_index: None,
}
}
pub fn controller(&self) -> Controller {
self.controller.clone()
}
pub fn capacity(&self) -> usize {
self.slots.len()
}
pub fn len(&self) -> usize {
self.slots
.iter()
.filter(|slot| matches!(&slot.state, ArenaSlotState::Occupied { .. }))
.count()
}
pub fn is_empty(&self) -> bool {
self.len() == 0
}
pub fn insert_with_key(&mut self, key: Key, data: T) -> Result<(), InsertWithKeyError> {
if let Some(slot) = self.slots.get(key.index) {
if slot.generation != key.generation {
return Err(InsertWithKeyError::InvalidKey);
}
if let ArenaSlotState::Occupied { .. } = &slot.state {
return Err(InsertWithKeyError::KeyNotReserved);
}
} else {
return Err(InsertWithKeyError::InvalidKey);
}
if let Some(head_index) = self.first_occupied_slot_index {
self.slots[head_index].set_previous_occupied_slot_index(Some(key.index));
}
self.slots[key.index].state = ArenaSlotState::Occupied {
data,
previous_occupied_slot_index: None,
next_occupied_slot_index: self.first_occupied_slot_index,
};
self.first_occupied_slot_index = Some(key.index);
Ok(())
}
pub fn insert(&mut self, data: T) -> Result<Key, ArenaFull> {
let key = self.controller.try_reserve()?;
self.insert_with_key(key, data).unwrap();
Ok(key)
}
fn remove_from_slot(&mut self, index: usize) -> Option<T> {
let slot = &mut self.slots[index];
let state = std::mem::replace(&mut slot.state, ArenaSlotState::Free);
match state {
ArenaSlotState::Free => None,
ArenaSlotState::Occupied {
data,
previous_occupied_slot_index,
next_occupied_slot_index,
} => {
slot.generation += 1;
self.controller.free(index);
if let Some(previous_index) = previous_occupied_slot_index {
self.slots[previous_index]
.set_next_occupied_slot_index(next_occupied_slot_index);
}
if let Some(next_index) = next_occupied_slot_index {
self.slots[next_index]
.set_previous_occupied_slot_index(previous_occupied_slot_index);
}
if self.first_occupied_slot_index.unwrap() == index {
self.first_occupied_slot_index = next_occupied_slot_index;
}
Some(data)
}
}
}
pub fn remove(&mut self, key: Key) -> Option<T> {
let slot = &mut self.slots[key.index];
if slot.generation != key.generation {
return None;
}
self.remove_from_slot(key.index)
}
pub fn get(&self, key: Key) -> Option<&T> {
let slot = &self.slots[key.index];
if slot.generation != key.generation {
return None;
}
match &slot.state {
ArenaSlotState::Free => None,
ArenaSlotState::Occupied { data, .. } => Some(data),
}
}
pub fn get_mut(&mut self, key: Key) -> Option<&mut T> {
let slot = &mut self.slots[key.index];
if slot.generation != key.generation {
return None;
}
match &mut slot.state {
ArenaSlotState::Free => None,
ArenaSlotState::Occupied { data, .. } => Some(data),
}
}
pub fn retain(&mut self, mut f: impl FnMut(&T) -> bool) {
let mut index = match self.first_occupied_slot_index {
Some(index) => index,
None => return,
};
loop {
if let ArenaSlotState::Occupied {
data,
next_occupied_slot_index,
..
} = &self.slots[index].state
{
let next_occupied_slot_index = next_occupied_slot_index.as_ref().copied();
if !f(data) {
self.remove_from_slot(index);
}
index = match next_occupied_slot_index {
Some(index) => index,
None => return,
}
} else {
panic!("expected the slot pointed to by first_occupied_slot_index/next_occupied_slot_index to be occupied")
}
}
}
pub fn iter(&self) -> Iter<T> {
Iter::new(self)
}
pub fn iter_mut(&mut self) -> IterMut<T> {
IterMut::new(self)
}
pub fn drain_filter<F: FnMut(&T) -> bool>(&mut self, filter: F) -> DrainFilter<T, F> {
DrainFilter::new(self, filter)
}
}
impl<T> std::ops::Index<Key> for Arena<T> {
type Output = T;
fn index(&self, key: Key) -> &Self::Output {
self.get(key).expect("No item associated with this key")
}
}
impl<T> std::ops::IndexMut<Key> for Arena<T> {
fn index_mut(&mut self, key: Key) -> &mut Self::Output {
self.get_mut(key).expect("No item associated with this key")
}
}
impl<'a, T> IntoIterator for &'a Arena<T> {
type Item = (Key, &'a T);
type IntoIter = Iter<'a, T>;
fn into_iter(self) -> Self::IntoIter {
self.iter()
}
}
impl<'a, T> IntoIterator for &'a mut Arena<T> {
type Item = (Key, &'a mut T);
type IntoIter = IterMut<'a, T>;
fn into_iter(self) -> Self::IntoIter {
self.iter_mut()
}
}