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
use std::{mem, ptr, slice};
use crate::depot::{
map,
map::priority::{self, Plan},
};
const NONE: u32 = u32::MAX;
pub(in crate::depot::map::priority) struct Pair<K, V, P> {
pub(in crate::depot::map::priority) key: K,
pub(in crate::depot::map::priority) value: V,
pub(in crate::depot::map::priority) priority: P,
}
pub(in crate::depot::map::priority) struct Store<K, V, P> {
nodes: ptr::NonNull<priority::Node<K, V, P>>,
plan: Plan<K, P, V>,
next: u32,
free: u32,
len: u32,
}
pub(in crate::depot::map::priority) enum Slot {
Free(u32),
Fresh(u32),
}
impl Slot {
pub(in crate::depot::map::priority) fn index(&self) -> u32 {
match self {
Self::Free(index) | Self::Fresh(index) => *index,
}
}
}
impl<K, V, P> Store<K, V, P> {
/// # Safety
/// `nodes` must point to `plan.capacity()` allocated node slots that this
/// store exclusively owns for its whole lifetime, initialized to `MaybeUninit`.
pub(in crate::depot::map::priority) const unsafe fn new(
nodes: ptr::NonNull<priority::Node<K, V, P>>,
plan: Plan<K, P, V>,
) -> Self {
Self {
nodes,
plan,
next: 0,
free: NONE,
len: 0,
}
}
pub(in crate::depot::map::priority) const fn capacity(&self) -> usize {
self.plan.capacity().get()
}
pub(in crate::depot::map::priority) const fn plan(&self) -> Plan<K, P, V> {
self.plan
}
pub(in crate::depot::map::priority) const fn len(&self) -> usize {
self.len as usize
}
pub(in crate::depot::map::priority) fn edit(
&mut self,
) -> (&mut u32, &mut [SlotStorage<K, V, P>]) {
// SAFETY: this exclusive owner holds the whole `capacity`-wide node
// allocation, and `SlotStorage` admits uninitialized contents.
let nodes =
unsafe { slice::from_raw_parts_mut(self.nodes.as_ptr().cast(), self.capacity()) };
(&mut self.len, nodes)
}
pub(in crate::depot::map::priority) fn vacant(&self) -> Option<Slot> {
if self.free != NONE {
return Some(Slot::Free(self.free));
}
((self.next as usize) < self.capacity()).then_some(Slot::Fresh(self.next))
}
/// # Safety
/// `index` must be below `capacity` and name one initialized node.
pub(in crate::depot::map::priority) unsafe fn pair(&self, index: u32) -> &Pair<K, V, P> {
// SAFETY: the caller bounds `index` into this owner's node allocation.
let node = unsafe { &*self.nodes.as_ptr().add(index as usize) };
// SAFETY: a live node holds one initialized pair at this slot.
unsafe { node.pair.assume_init_ref() }
}
/// # Safety
/// `index` must be below `capacity` and name one initialized node.
pub(in crate::depot::map::priority) unsafe fn pair_mut(
&mut self,
index: u32,
) -> &mut Pair<K, V, P> {
// SAFETY: the caller bounds `index` into this owner's node allocation; the exclusive borrow keeps it unique.
let node = unsafe { &mut *self.nodes.as_ptr().add(index as usize) };
// SAFETY: a live node holds one initialized pair at this slot.
unsafe { node.pair.assume_init_mut() }
}
pub(in crate::depot::map::priority) fn insert(
&mut self,
slot: Slot,
pair: Pair<K, V, P>,
bucket: &map::raw::Vacant,
) -> u32 {
let index = match slot {
Slot::Free(index) => {
// SAFETY: a recycled slot retains its next free index in this allocation.
self.free = unsafe { (*self.nodes.as_ptr().add(index as usize)).bucket };
index
}
Slot::Fresh(index) => {
self.next += 1;
index
}
};
// SAFETY: `slot` proves `index` is a fresh or recycled node of this
// owner, so the write initializes it exactly once.
unsafe {
self.nodes
.as_ptr()
.add(index as usize)
.write(priority::Node {
pair: mem::MaybeUninit::new(pair),
bucket: bucket.index() as u32,
position: NONE,
});
}
index
}
/// # Safety
/// `index` must be below `capacity`, name one initialized pair, and already be absent from the heap.
pub(in crate::depot::map::priority) unsafe fn remove(&mut self, index: u32) -> Pair<K, V, P> {
// SAFETY: the caller bounds `index` into this owner's node allocation; the exclusive borrow keeps it unique.
let node = unsafe { &mut *self.nodes.as_ptr().add(index as usize) };
// SAFETY: the caller proves the node initialized, so its pair is taken exactly once.
let pair = unsafe { node.pair.assume_init_read() };
node.bucket = self.free;
self.free = index;
pair
}
/// # Safety
/// `index` must be below `capacity` and name one initialized node.
pub(in crate::depot::map::priority) unsafe fn bucket(&self, index: u32) -> usize {
// SAFETY: the caller bounds `index` into this owner's node allocation.
unsafe { (*self.nodes.as_ptr().add(index as usize)).bucket as usize }
}
/// # Safety
/// `index` must be below `capacity` and name one initialized node.
pub(in crate::depot::map::priority) unsafe fn set_bucket(
&mut self,
index: u32,
bucket: map::raw::Occupied,
) {
// SAFETY: the caller bounds `index` into this owner's node allocation; the exclusive borrow keeps it unique.
unsafe { (*self.nodes.as_ptr().add(index as usize)).bucket = bucket.index() as u32 };
}
pub(in crate::depot::map::priority) fn clear(&mut self) {
for index in 0..self.next {
// SAFETY: `index < next` names one node this store has initialized.
let node = unsafe { &mut *self.nodes.as_ptr().add(index as usize) };
if node.position != NONE {
// SAFETY: a live position marks exactly one pair left to drop.
unsafe { node.pair.assume_init_drop() };
}
}
}
}
/// Node slots retain their initialization state beside heap positions.
type SlotStorage<K, V, P> = mem::MaybeUninit<priority::Node<K, V, P>>;