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
extern crate free_ranges;
use free_ranges::FreeRanges;
use free_ranges::Range;
use std::error::Error;
use std::fmt;
use std::usize;
pub mod iter;
#[derive(Debug)]
pub struct IndexPool {
next_id: usize,
in_use: usize,
free_list: FreeRanges,
}
impl IndexPool {
#[inline]
pub fn new() -> Self {
Self::with_initial_index(0)
}
pub fn with_initial_index(index: usize) -> Self {
IndexPool {
next_id: index,
in_use: 0,
free_list: FreeRanges::new(),
}
}
#[inline]
pub fn new_id(&mut self) -> usize {
self.in_use += 1;
if let Some(id) = self.free_list.set_first_used() {
return id;
}
let id = self.next_id;
self.next_id += 1;
return id;
}
#[inline]
pub fn request_id(&mut self, id: usize) -> Result<(), AlreadyInUse> {
assert!(id < usize::MAX);
if id == self.next_id {
self.next_id += 1;
self.in_use += 1;
Ok(())
} else if id > self.next_id {
self.free_list.set_range_free(Range {
min: self.next_id,
max: id - 1,
});
self.next_id = id + 1;
self.in_use += 1;
Ok(())
} else if self.free_list.set_used(id) {
self.in_use += 1;
Ok(())
} else {
Err(AlreadyInUse)
}
}
#[inline]
pub fn return_id(&mut self, id: usize) -> Result<(), AlreadyReturned> {
if id >= self.next_id {
return Err(AlreadyReturned);
}
if id + 1 == self.next_id {
self.next_id -= 1;
} else {
if !self.free_list.set_free(id) {
return Err(AlreadyReturned);
}
assert!(self.free_list.is_free(id));
}
self.in_use -= 1;
while self.collapse_next() {}
Ok(())
}
#[inline]
pub fn maximum(&self) -> usize {
self.next_id
}
#[inline]
pub fn in_use(&self) -> usize {
self.in_use
}
#[inline]
pub fn is_free(&self, id: usize) -> bool {
id >= self.next_id || self.free_list.is_free(id)
}
#[inline]
pub fn all_indices(&self) -> iter::IndexIter {
iter::IndexIter::new(self.free_list.free_ranges(), self.next_id)
}
#[inline]
pub fn all_indices_after(&self, after: usize) -> iter::IndexAfterIter {
iter::IndexAfterIter::new(self.free_list.free_ranges_after(after), after, self.next_id)
}
#[inline]
fn collapse_next(&mut self) -> bool {
if let Some(last_range) = self.free_list.free_ranges().rev().nth(0).cloned() {
if last_range.max + 1 == self.next_id {
self.free_list.remove_last_contiguous();
self.next_id = last_range.min;
return true;
}
}
false
}
#[inline]
pub fn clear(&mut self) {
self.free_list.clear();
self.in_use = 0;
self.next_id = 0;
}
}
impl Default for IndexPool {
#[inline]
fn default() -> Self {
IndexPool::new()
}
}
#[derive(Debug, PartialEq, Eq)]
pub struct AlreadyReturned;
impl fmt::Display for AlreadyReturned {
fn fmt(&self, fmt: &mut fmt::Formatter) -> fmt::Result {
fmt.write_str(self.description())
}
}
impl Error for AlreadyReturned {
fn description(&self) -> &str {
"An index was tried to be returned to the pool, but it was already marked as free."
}
}
#[derive(Debug, PartialEq, Eq)]
pub struct AlreadyInUse;
impl fmt::Display for AlreadyInUse {
fn fmt(&self, fmt: &mut fmt::Formatter) -> fmt::Result {
fmt.write_str(self.description())
}
}
impl Error for AlreadyInUse {
fn description(&self) -> &str {
"An index was requested which was already marked as in use."
}
}