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
use std::cell::RefCell;
use std::cmp;
use std::iter;
use std::mem;
use std::slice;
#[cfg(test)]
mod test;
const INITIAL_SIZE: usize = 1024;
const MIN_CAPACITY: usize = 1;
pub struct Arena<T> {
chunks: RefCell<ChunkList<T>>,
}
struct ChunkList<T> {
current: Vec<T>,
rest: Vec<Vec<T>>,
}
impl<T> Arena<T> {
pub fn new() -> Arena<T> {
let size = cmp::max(1, mem::size_of::<T>());
Arena::with_capacity(INITIAL_SIZE / size)
}
pub fn with_capacity(n: usize) -> Arena<T> {
let n = cmp::max(MIN_CAPACITY, n);
Arena {
chunks: RefCell::new(ChunkList {
current: Vec::with_capacity(n),
rest: vec![],
}),
}
}
pub fn alloc(&self, value: T) -> &mut T {
&mut self.alloc_extend(iter::once(value))[0]
}
pub fn alloc_extend<I>(&self, iterable: I) -> &mut [T]
where I: IntoIterator<Item = T>
{
let mut iter = iterable.into_iter();
let mut chunks = self.chunks.borrow_mut();
let iter_min_len = iter.size_hint().0;
let mut next_item_index;
if chunks.current.len() + iter_min_len > chunks.current.capacity() {
chunks.reserve(iter_min_len);
chunks.current.extend(iter);
next_item_index = 0;
} else {
next_item_index = chunks.current.len();
let mut i = 0;
while let Some(elem) = iter.next() {
if chunks.current.len() == chunks.current.capacity() {
let chunks = &mut *chunks;
chunks.reserve(i + 1);
let previous_chunk = chunks.rest.last_mut().unwrap();
let previous_chunk_len = previous_chunk.len();
chunks.current.extend(previous_chunk.drain(previous_chunk_len - i..));
chunks.current.push(elem);
chunks.current.extend(iter);
next_item_index = 0;
break;
} else {
chunks.current.push(elem);
}
i += 1;
}
}
let new_slice_ref = {
let new_slice_ref = &mut chunks.current[next_item_index..];
unsafe { mem::transmute::<&mut [T], &mut [T]>(new_slice_ref) }
};
new_slice_ref
}
pub unsafe fn alloc_uninitialized(&self, num: usize) -> *mut [T] {
let mut chunks = self.chunks.borrow_mut();
if chunks.current.len() + num > chunks.current.capacity() {
chunks.reserve(num);
}
let next_item_index = chunks.current.len();
chunks.current.set_len(next_item_index + num);
&mut chunks.current[next_item_index..] as *mut _
}
pub fn uninitialized_array(&self) -> *mut [T] {
let chunks = self.chunks.borrow();
let len = chunks.current.capacity() - chunks.current.len();
let next_item_index = chunks.current.len();
let slice = &chunks.current[next_item_index..];
unsafe { slice::from_raw_parts_mut(slice.as_ptr() as *mut T, len) as *mut _ }
}
pub fn into_vec(self) -> Vec<T> {
let mut chunks = self.chunks.into_inner();
let n = chunks.rest.iter().fold(chunks.current.len(), |a, v| a + v.len());
let mut result = Vec::with_capacity(n);
for mut vec in chunks.rest {
result.append(&mut vec);
}
result.append(&mut chunks.current);
result
}
}
impl<T> ChunkList<T> {
#[inline(never)]
#[cold]
fn reserve(&mut self, additional: usize) {
let double_cap = self.current.capacity().checked_mul(2).expect("capacity overflow");
let required_cap = additional.checked_next_power_of_two().expect("capacity overflow");
let new_capacity = cmp::max(double_cap, required_cap);
let chunk = mem::replace(&mut self.current, Vec::with_capacity(new_capacity));
self.rest.push(chunk);
}
}