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
//! Concurrent containers using the same generational ids as the plain types.
//!
//! **Grow shared, shrink exclusive:** shared access may install pages and
//! recycle slots, but allocated pages and directory chunks remain at stable
//! addresses until exclusive access or destruction. Memory reclamation needs
//! no epochs or hazard pointers. Slot state is synchronized separately.
//!
//! Available with the `concurrent` feature on supported atomic targets.
//! The implementation uses `core` atomics and `alloc`, without requiring `std`.
//!
//! [`ConcurrentHive`] supports shared insertion and removal. A lookup returns
//! a [`Ref`] guard, which pins its value until dropped. A successful lookup
//! costs two atomic writes: acquiring the guard and releasing it. Hot values
//! can therefore contend even between readers. Prefer the plain
//! [`Hive`](crate::hive::Hive) when shared mutation is unnecessary.
//!
//! [`ConcurrentAppendHive`] supports shared insertion and ordinary `&T`
//! lookups with one Acquire slot-state load and no guard or reference count.
//! Removal and mutable access require exclusive ownership. Iteration may
//! miss insertions in flight; conversions to and from [`Hive`](crate::hive::Hive)
//! preserve live ids and stale-id history in O(index bound) time.
//!
//! [`ConcurrentPagedVec`] appends without waiting for earlier writers. Its
//! point lookups see completed pushes; `len`, iteration and page slices use a
//! hole-free prefix which can lag while an earlier push is in flight. When
//! no push is in flight, the prefix equals the number of reservations.
//!
//! [`ConcurrentSparseSet`] and [`ConcurrentSparseMap`] allow shared insertion
//! and return ordinary shared references. Entries cannot be removed or
//! changed while shared. Insertions of the same slot wait for its writer;
//! unrelated slots proceed independently. An occupied slot refuses other
//! generations. The vector and both sparse types support removal and
//! conversion to their plain equivalents
//! under exclusive ownership. Conversion moves elements in O(len) time.
//!
//! [`ConcurrentDirectMap`] permits shared replacement and removal through one
//! reader-writer lock per page. Neighbouring keys contend; guards and entries
//! must be released before locking the same page again. Ordered traversals
//! are consistent per page only. Pages are freed only with exclusive access.
//!
//! [`AtomicDirectMap`] and [`AtomicIdMap`] provide lock-free shared updates
//! of one-word values. Prefer them over [`ConcurrentDirectMap`] when values
//! fit their encodings: `usize` keys with `u64` values below `u64::MAX`, or
//! generational ids with any `u32` value. Traversals copy values in index order.
//!
//! The sparse types, [`ConcurrentDirectMap`] and [`AtomicDirectMap`] accept a
//! [`SharedDirectory`] parameter. The default [`Flat`](crate::page::Flat)
//! has O(1) page lookups, directory memory following the highest page, and a
//! finite index range. [`Radix`](crate::page::Radix) accepts any `usize` index,
//! uses directory memory proportional to installed pages, and has O(depth)
//! page lookups. [`AtomicIdMap`] remains Flat-only; `Tree` has no concurrent form.
//!
//! # Element invariance
//!
//! Shared insertion requires invariance in every element type: shortening a
//! stored reference's lifetime would allow inserting a reference that expires
//! while the original owner still expects a `'static` reference.
//! The following identity functions typecheck; changing only their returned
//! element lifetimes is rejected, for either directory.
//!
//! ```
//! # #[cfg(feature = "concurrent")] {
//! use paged_collections::concurrent::*;
//! use paged_collections::page::{Flat, Radix};
//! use paged_collections::key::{DirectKey, Key};
//! #[derive(PartialEq, Eq)]
//! struct K(usize);
//! impl Key for &K { fn index(self) -> usize { self.0 } }
//! impl DirectKey for &K { type Tag = Self; fn tag(self) -> Self { self } fn from_parts(_: usize, k: Self) -> Self { k } }
//! fn identity_0(x: ConcurrentHive<&'static u8>) -> ConcurrentHive<&'static u8> { x }
//! fn identity_1(x: ConcurrentAppendHive<&'static u8>) -> ConcurrentAppendHive<&'static u8> { x }
//! fn identity_2(x: ConcurrentPagedVec<&'static u8>) -> ConcurrentPagedVec<&'static u8> { x }
//! fn identity_3(x: ConcurrentSparseSet<&'static K, 4, Flat>) -> ConcurrentSparseSet<&'static K, 4, Flat> { x }
//! fn identity_4(x: ConcurrentSparseMap<&'static K, (), 4, Flat>) -> ConcurrentSparseMap<&'static K, (), 4, Flat> { x }
//! fn identity_5(x: ConcurrentSparseMap<usize, &'static u8, 4, Flat>) -> ConcurrentSparseMap<usize, &'static u8, 4, Flat> { x }
//! fn identity_6(x: ConcurrentDirectMap<&'static K, (), 4, Flat>) -> ConcurrentDirectMap<&'static K, (), 4, Flat> { x }
//! fn identity_7(x: ConcurrentDirectMap<usize, &'static u8, 4, Flat>) -> ConcurrentDirectMap<usize, &'static u8, 4, Flat> { x }
//! fn identity_8(x: ConcurrentSparseSet<&'static K, 4, Radix>) -> ConcurrentSparseSet<&'static K, 4, Radix> { x }
//! fn identity_9(x: ConcurrentSparseMap<&'static K, (), 4, Radix>) -> ConcurrentSparseMap<&'static K, (), 4, Radix> { x }
//! fn identity_10(x: ConcurrentSparseMap<usize, &'static u8, 4, Radix>) -> ConcurrentSparseMap<usize, &'static u8, 4, Radix> { x }
//! fn identity_11(x: ConcurrentDirectMap<&'static K, (), 4, Radix>) -> ConcurrentDirectMap<&'static K, (), 4, Radix> { x }
//! fn identity_12(x: ConcurrentDirectMap<usize, &'static u8, 4, Radix>) -> ConcurrentDirectMap<usize, &'static u8, 4, Radix> { x }
//! # }
//! ```
//!
//! ```compile_fail
//! # #[cfg(feature = "concurrent")] {
//! use paged_collections::concurrent::ConcurrentHive;
//! fn shorten<'a>(x: ConcurrentHive<&'static u8>) -> ConcurrentHive<&'a u8> { x }
//! # }
//! ```
//!
//! ```compile_fail
//! # #[cfg(feature = "concurrent")] {
//! use paged_collections::concurrent::ConcurrentAppendHive;
//! fn shorten<'a>(x: ConcurrentAppendHive<&'static u8>) -> ConcurrentAppendHive<&'a u8> { x }
//! # }
//! ```
//!
//! ```compile_fail
//! # #[cfg(feature = "concurrent")] {
//! use paged_collections::concurrent::ConcurrentPagedVec;
//! fn shorten<'a>(x: ConcurrentPagedVec<&'static u8>) -> ConcurrentPagedVec<&'a u8> { x }
//! # }
//! ```
//!
//! ```compile_fail
//! # #[cfg(feature = "concurrent")] {
//! use paged_collections::concurrent::ConcurrentSparseSet;
//! use paged_collections::page::Flat;
//! use paged_collections::key::Key;
//! #[derive(PartialEq, Eq)]
//! struct K(usize);
//! impl Key for &K { fn index(self) -> usize { self.0 } }
//! fn shorten<'a>(x: ConcurrentSparseSet<&'static K, 4, Flat>) -> ConcurrentSparseSet<&'a K, 4, Flat> { x }
//! # }
//! ```
//!
//! ```compile_fail
//! # #[cfg(feature = "concurrent")] {
//! use paged_collections::concurrent::ConcurrentSparseMap;
//! use paged_collections::page::Flat;
//! use paged_collections::key::Key;
//! #[derive(PartialEq, Eq)]
//! struct K(usize);
//! impl Key for &K { fn index(self) -> usize { self.0 } }
//! fn shorten<'a>(x: ConcurrentSparseMap<&'static K, (), 4, Flat>) -> ConcurrentSparseMap<&'a K, (), 4, Flat> { x }
//! # }
//! ```
//!
//! ```compile_fail
//! # #[cfg(feature = "concurrent")] {
//! use paged_collections::concurrent::ConcurrentSparseMap;
//! use paged_collections::page::Flat;
//! fn shorten<'a>(x: ConcurrentSparseMap<usize, &'static u8, 4, Flat>) -> ConcurrentSparseMap<usize, &'a u8, 4, Flat> { x }
//! # }
//! ```
//!
//! ```compile_fail
//! # #[cfg(feature = "concurrent")] {
//! use paged_collections::concurrent::ConcurrentDirectMap;
//! use paged_collections::page::Flat;
//! use paged_collections::key::{DirectKey, Key};
//! #[derive(PartialEq, Eq)]
//! struct K(usize);
//! impl Key for &K { fn index(self) -> usize { self.0 } }
//! impl DirectKey for &K { type Tag = Self; fn tag(self) -> Self { self } fn from_parts(_: usize, k: Self) -> Self { k } }
//! fn shorten<'a>(x: ConcurrentDirectMap<&'static K, (), 4, Flat>) -> ConcurrentDirectMap<&'a K, (), 4, Flat> { x }
//! # }
//! ```
//!
//! ```compile_fail
//! # #[cfg(feature = "concurrent")] {
//! use paged_collections::concurrent::ConcurrentDirectMap;
//! use paged_collections::page::Flat;
//! fn shorten<'a>(x: ConcurrentDirectMap<usize, &'static u8, 4, Flat>) -> ConcurrentDirectMap<usize, &'a u8, 4, Flat> { x }
//! # }
//! ```
//!
//! ```compile_fail
//! # #[cfg(feature = "concurrent")] {
//! use paged_collections::concurrent::ConcurrentSparseSet;
//! use paged_collections::page::Radix;
//! use paged_collections::key::Key;
//! #[derive(PartialEq, Eq)]
//! struct K(usize);
//! impl Key for &K { fn index(self) -> usize { self.0 } }
//! fn shorten<'a>(x: ConcurrentSparseSet<&'static K, 4, Radix>) -> ConcurrentSparseSet<&'a K, 4, Radix> { x }
//! # }
//! ```
//!
//! ```compile_fail
//! # #[cfg(feature = "concurrent")] {
//! use paged_collections::concurrent::ConcurrentSparseMap;
//! use paged_collections::page::Radix;
//! use paged_collections::key::Key;
//! #[derive(PartialEq, Eq)]
//! struct K(usize);
//! impl Key for &K { fn index(self) -> usize { self.0 } }
//! fn shorten<'a>(x: ConcurrentSparseMap<&'static K, (), 4, Radix>) -> ConcurrentSparseMap<&'a K, (), 4, Radix> { x }
//! # }
//! ```
//!
//! ```compile_fail
//! # #[cfg(feature = "concurrent")] {
//! use paged_collections::concurrent::ConcurrentSparseMap;
//! use paged_collections::page::Radix;
//! fn shorten<'a>(x: ConcurrentSparseMap<usize, &'static u8, 4, Radix>) -> ConcurrentSparseMap<usize, &'a u8, 4, Radix> { x }
//! # }
//! ```
//!
//! ```compile_fail
//! # #[cfg(feature = "concurrent")] {
//! use paged_collections::concurrent::ConcurrentDirectMap;
//! use paged_collections::page::Radix;
//! use paged_collections::key::{DirectKey, Key};
//! #[derive(PartialEq, Eq)]
//! struct K(usize);
//! impl Key for &K { fn index(self) -> usize { self.0 } }
//! impl DirectKey for &K { type Tag = Self; fn tag(self) -> Self { self } fn from_parts(_: usize, k: Self) -> Self { k } }
//! fn shorten<'a>(x: ConcurrentDirectMap<&'static K, (), 4, Radix>) -> ConcurrentDirectMap<&'a K, (), 4, Radix> { x }
//! # }
//! ```
//!
//! ```compile_fail
//! # #[cfg(feature = "concurrent")] {
//! use paged_collections::concurrent::ConcurrentDirectMap;
//! use paged_collections::page::Radix;
//! fn shorten<'a>(x: ConcurrentDirectMap<usize, &'static u8, 4, Radix>) -> ConcurrentDirectMap<usize, &'a u8, 4, Radix> { x }
//! # }
//! ```
//!
pub use ;
pub use ;
pub use ;
pub use SharedDirectory;
pub use ;
pub use ConcurrentIdAllocator;
pub use ;
pub use ConcurrentPagedVec;