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
use crate::BitBlock;
use crate::primitive::Primitive;
pub trait IBlock: Sized + Default{
type Mask: BitBlock;
fn mask(&self) -> &Self::Mask;
unsafe fn mask_mut(&mut self) -> &mut Self::Mask;
type Item: Primitive;
/*/// # Safety
///
/// - index is not checked for out-of-bounds.
/// - index is not checked for validity (must exist).
unsafe fn get_unchecked(&self, index: usize) -> Self::Item;*/
/// Returns 0 if item does not exist at `index`.
///
/// # Safety
///
/// index is not checked for out-of-bounds.
unsafe fn get_or_zero(&self, index: usize) -> Self::Item;
/// # Safety
///
/// index is not checked.
unsafe fn get_or_insert(
&mut self,
index: usize,
f: impl FnMut() -> Self::Item
) -> Self::Item;
/// # Safety
///
/// * index is not checked.
/// * item at index must NOT exist in level.
unsafe fn insert_unchecked(
&mut self,
index: usize,
item : Self::Item
);
/// Same as [Self::insert_unchecked()] but does not update mask.
///
/// # Safety
///
/// * index is not checked.
/// * item at index must NOT exist in level.
unsafe fn insert_unchecked_no_mask(
&mut self,
index: usize,
item : Self::Item
);
/// Return previous mask bit.
///
/// # Safety
///
/// * `index` must be set
/// * `index` is not checked for out-of-bounds.
unsafe fn remove_unchecked(&mut self, index: usize);
/// Same as [Self::remove_unchecked()] but does not update mask.
///
/// # Safety
///
/// * `index` must be set
/// * `index` is not checked for out-of-bounds.
unsafe fn remove_unchecked_no_mask(&mut self, index: usize);
#[inline]
fn is_empty(&self) -> bool {
Self::Mask::is_zero(self.mask())
}
}
#[derive(Clone)]
pub struct Level<Block: IBlock>{
blocks: Vec<Block>,
/// Single linked list of empty block indices.
/// Mask of empty block used as a "next free block".
/// u64::MAX - terminator.
///
/// This is index of first block in list.
root_empty_block: u64,
/// We need this for `len()`.
empty_blocks_count: usize,
}
impl<Block: IBlock> Default for Level<Block> {
#[inline]
fn default() -> Self {
unsafe{ Self::from_blocks_unchecked(vec![Default::default()]) }
}
}
impl<Block: IBlock> Level<Block> {
/// # Safety
///
/// Always have empty block at index 0.
#[inline]
pub unsafe fn from_blocks_unchecked(blocks: Vec<Block>) -> Self {
Self{blocks, root_empty_block: u64::MAX, empty_blocks_count: 0 }
}
#[inline]
pub fn blocks(&self) -> &[Block] {
self.blocks.as_slice()
}
#[inline]
pub fn blocks_mut(&mut self) -> &mut [Block] {
self.blocks.as_mut_slice()
}
/// Next empty block link
///
/// Block's mask used as index to next empty block
#[inline]
unsafe fn next_empty_block_index(block: &mut Block) -> &mut u64 {
block.mask_mut().as_array_mut().get_unchecked_mut(0)
}
#[inline]
fn pop_empty_block(&mut self) -> Option<usize> {
// It is marginally faster to compare with zero,
// then self.root_empty_block == u64::MAX
if self.empty_blocks_count == 0 {
return None;
}
let index = self.root_empty_block as usize;
unsafe{
let empty_block = self.blocks.get_unchecked_mut(index);
let next_empty_block_index = Self::next_empty_block_index(empty_block);
// update list root
self.root_empty_block = *next_empty_block_index;
// restore original mask zero state
*next_empty_block_index = 0;
self.empty_blocks_count -= 1;
}
Some(index)
}
/// # Safety
///
/// block must be empty and not in use!
#[inline]
unsafe fn push_empty_block(&mut self, block_index: usize){
let empty_block = self.blocks.get_unchecked_mut(block_index);
let next_empty_block_index = Self::next_empty_block_index(empty_block);
*next_empty_block_index = self.root_empty_block;
self.root_empty_block = block_index as u64;
self.empty_blocks_count += 1;
}
/// Inserts empty block and return its index.
///
/// Faster then `insert_block(Default::default())`.
#[inline]
pub fn insert_empty_block(&mut self) -> usize {
if let Some(index) = self.pop_empty_block(){
index
} else {
let index = self.blocks.len();
self.blocks.push(Default::default());
index
}
}
/// Inserts `block` and return its index.
#[inline]
pub fn insert_block(&mut self, block: Block) -> usize {
if let Some(index) = self.pop_empty_block(){
unsafe{ *self.blocks.get_unchecked_mut(index) = block; }
index
} else {
let index = self.blocks.len();
self.blocks.push(block);
index
}
}
/// Unlike [insert_block] - never re-use block.
#[inline]
pub fn push_block(&mut self, block: Block) -> usize {
debug_assert_eq!(self.empty_blocks_count, 0);
let index = self.blocks.len();
self.blocks.push(block);
index
}
/// Reserve for `len` blocks total.
#[inline]
pub fn reserve_for(&mut self, mut len: usize) {
len += 1; // One for a first empty block
let cap = self.blocks.capacity();
if cap >= len{
return;
}
self.blocks.reserve(len - cap);
}
/// Actual len including first empty block
#[inline]
pub fn len(&self) -> usize {
self.blocks.len() - self.empty_blocks_count
}
/// Actual cap
#[inline]
pub fn cap(&self) -> usize {
self.blocks.capacity()
}
/// # Safety
///
/// block_index and block emptiness are not checked.
#[inline]
pub unsafe fn remove_empty_block_unchecked(&mut self, block_index: usize) {
self.push_empty_block(block_index);
// Do not touch block itself - it should be already empty
}
}