o3 0.24.1

shared-nothing primitives
Documentation
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
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
//! Growable payloads with independent indexed FIFO memberships.

use std::{array, iter, mem, num::NonZero, ops};

pub use entry::Entry;
pub use error::Error;

use crate::{
    alloc::{self, BoxSliceExt as _, VecExt as _, raw::VecExt as _},
    base::marker::ThreadBound,
    depot::{
        capacity,
        heap::indexed,
        queue::{
            indexed::{lanes::runtime, planes},
            links,
        },
    },
};

pub mod bound;
mod entry;
pub mod groups;
pub mod resident;
pub use state::State;

mod error;
pub mod map;
pub mod phase;

/// Statically builds one payload from its exact topology index.
pub trait Seed<T> {
    fn seed(&self, index: links::Index) -> T;
}

impl<T: Default> Seed<T> for () {
    fn seed(&self, _: links::Index) -> T {
        T::default()
    }
}

/// Coupled minimum priorities and their exact payload domain.
pub trait Priority<T, P: Ord, const PLANES: usize, C: alloc::raw::Extent>: Sized {
    fn try_seed(len: C, seed: impl Seed<T>) -> Result<Self, Error<C>>;
    fn try_repeat(len: C, priority: P) -> Result<Self, Error<C>>
    where
        T: Default,
        P: Copy;
    fn reset<'a>(&'a mut self) -> impl Iterator<Item = &'a mut T>
    where
        T: 'a;
    fn heap(&self) -> &indexed::Min<P, C>;
    fn select(&mut self, index: usize) -> Option<(indexed::Entry<'_, P, C>, Entry<'_, T, PLANES>)>;
    fn first(&mut self) -> Option<(indexed::Occupied<'_, P, C>, Entry<'_, T, PLANES>)>;
    fn try_grow_to(&mut self, len: C, seed: impl Seed<T>) -> Result<ops::Range<usize>, Error<C>>;
}

mod state;

#[repr(C)]
struct Node<T, const PLANES: usize> {
    value: T,
    hooks: [links::Hook; PLANES],
}

impl<T, const PLANES: usize> Node<T, PLANES> {
    fn new(index: links::Index, value: T) -> Self {
        Self {
            value,
            hooks: array::from_fn(|_| links::Hook::new(index)),
        }
    }
}

struct Hooks<'a, T, const PLANES: usize, const PLANE: usize>(&'a [Node<T, PLANES>]);

// SAFETY: each index maps to its one co-located hook in the const-selected
// plane, entries never change logical index, and vector growth cannot
// invalidate index links.
unsafe impl<T, const PLANES: usize, const PLANE: usize> crate::depot::raw::Hooks
    for Hooks<'_, T, PLANES, PLANE>
{
    unsafe fn hook_raw(&self, index: links::Index) -> &'_ links::Hook {
        const { assert!(PLANE < PLANES, "plane") };
        // SAFETY: HookStore's caller proves this compact index is represented.
        let entry = unsafe { self.0.get_unchecked(index.get()) };
        &entry.hooks[PLANE]
    }
}

/// Growable indexed values with independent FIFO membership planes.
pub struct Set<T, const PLANES: usize, A = ()> {
    core: Core<T, PLANES>,
    index: A,
}

struct Core<T, const PLANES: usize> {
    entries: Vec<Node<T, PLANES>>,
    queues: [links::Chain<u32>; PLANES],
    _thread: ThreadBound,
}

/// Exclusive access to one validated payload and all of its queue hooks.
impl<T, const PLANES: usize> Set<T, PLANES> {
    pub const ENTRY_BYTES: usize = mem::size_of::<Node<T, PLANES>>();

    pub fn try_seed<S: planes::Seed<T>>(len: usize, seed: S) -> Result<Self, alloc::Error> {
        let count = capacity::Count::try_count(len, alloc::Error::Overflow)?;
        let len = count.get();
        let entries = Box::try_from_fn(len, |index| {
            // SAFETY: the admitted capacity covers every initialized compact index.
            let index = unsafe { links::Index::new_usize(index) };
            Node::new(index, seed.seed(index))
        })?
        .into_vec();
        Ok(Self {
            core: Core {
                entries,
                queues: [const { links::Chain::new() }; PLANES],
                _thread: ThreadBound::NEW,
            },
            index: (),
        })
    }
}

impl<T, const PLANES: usize, A> Set<T, PLANES, A> {
    /// Exclusively borrows the payload and hooks at `index`.
    pub fn entry(&mut self, index: usize) -> Option<Entry<'_, T, PLANES>> {
        if index >= self.len() {
            return None;
        }
        // SAFETY: Set construction caps its length and `index` is in bounds.
        let index = unsafe { links::Index::new_usize(index) };
        Some(Entry::new(&mut self.core, index))
    }

    /// Borrows one plane in FIFO order without changing its membership.
    pub fn iter<const PLANE: usize>(&self) -> impl Iterator<Item = (usize, &T)> + '_ {
        self.core
            .iter::<PLANE>()
            .map(|(index, value)| (index.get(), value))
    }

    /// Exclusively binds the oldest payload to its exact membership proof.
    pub fn front_entry<const PLANE: usize>(
        &mut self,
    ) -> Option<Entry<'_, T, PLANES, phase::Front<PLANE>>> {
        self.core.front_entry::<PLANE>()
    }

    pub fn len_in<const PLANE: usize>(&self) -> usize {
        const { assert!(PLANE < PLANES) };
        self.core.queues[PLANE].len()
    }

    pub fn clear<const PLANE: usize>(&mut self) {
        self.core.clear::<PLANE>();
    }
}

impl<T, const PLANES: usize> Set<T, PLANES> {
    /// Grows to `len`, preserving every existing index and membership.
    /// Allocation failure leaves the original logical length and topology unchanged.
    pub fn try_grow_to<S: planes::Seed<T>>(
        &mut self,
        len: usize,
        seed: S,
    ) -> Result<ops::Range<usize>, crate::alloc::Error> {
        let count = capacity::Count::try_count(len, alloc::Error::Overflow)?;
        let old = self.core.entries.len();
        let core = self.core.grow(count.raw(), seed)?;
        Ok(old..core.entries.len())
    }
}

impl<T, const PLANES: usize> Core<T, PLANES> {
    fn grow<S: planes::Seed<T>>(
        &mut self,
        len: u32,
        seed: S,
    ) -> Result<&mut Self, crate::alloc::Error> {
        let len = len as usize;
        let original = self.entries.len();
        if len <= original {
            return Ok(self);
        }
        self.entries.try_grow(len - original)?;
        for index in original..len {
            // SAFETY: admitted capacity covers this compact index and its next vacant slot.
            unsafe {
                let compact = links::Index::new_usize(index);
                let value = seed.seed(compact);
                self.entries.vacancy().insert(Node::new(compact, value));
            }
        }
        Ok(self)
    }
}

impl<T, const PLANES: usize, A> Set<T, PLANES, A> {
    /// Selects a payload and its admitted compact identity in one lookup.
    pub fn resolve(&self, index: usize) -> Option<(NonZero<u32>, &T)> {
        self.core
            .resolve(index)
            .map(|(index, value)| (index.position(), value))
    }

    pub fn get(&self, index: usize) -> Option<&T> {
        self.core.entries.get(index).map(|node| &node.value)
    }
    pub fn iter_mut(&mut self) -> impl ExactSizeIterator<Item = &mut T> + DoubleEndedIterator {
        self.core.entries.iter_mut().map(|node| &mut node.value)
    }
    /// Returns retained physical rows, including rows outside the active prefix.
    pub fn len(&self) -> usize {
        self.core.entries.len()
    }

    pub fn is_empty(&self) -> bool {
        self.core.entries.is_empty()
    }
}

const _: () = {
    assert!(mem::size_of::<Entry<'static, (), 1>>() == 2 * mem::size_of::<usize>());
    assert!(
        mem::size_of::<Entry<'static, (), 1, phase::Front<0>>>() == 2 * mem::size_of::<usize>()
    );
};

impl<T, const PLANES: usize> Core<T, PLANES> {
    fn resolve(&self, index: usize) -> Option<(links::Index, &T)> {
        let entry = self.entries.get(index)?;
        // SAFETY: construction excludes the sentinel and this index is represented.
        let index = unsafe { links::Index::new_usize(index) };
        Some((index, &entry.value))
    }

    fn iter<const PLANE: usize>(&self) -> impl Iterator<Item = (links::Index, &T)> + '_ {
        const { assert!(PLANE < PLANES) };
        let mut next = self.queues[PLANE].first();
        iter::from_fn(move || {
            let index = next?;
            // SAFETY: this private queue contains only this owner's initialized nodes.
            let entry = unsafe { self.entries.get_unchecked(index.get()) };
            next = entry.hooks[PLANE].next_index();
            Some((index, &entry.value))
        })
    }

    fn front_entry<const PLANE: usize>(
        &mut self,
    ) -> Option<Entry<'_, T, PLANES, phase::Front<PLANE>>> {
        const { assert!(PLANE < PLANES) };
        let index = self.queues[PLANE].first()?;
        Some(Entry::new(self, index))
    }

    fn clear<const PLANE: usize>(&mut self) {
        while let Some(front) = self.front_entry::<PLANE>() {
            front.pop();
        }
    }

    fn first<'a, P: Ord, C: alloc::raw::Extent>(
        &'a mut self,
        heap: &'a mut indexed::Min<P, C>,
    ) -> Option<(indexed::Occupied<'a, P, C>, Entry<'a, T, PLANES>)> {
        let timer = heap.first_entry()?;
        // SAFETY: this coupled owner retains a payload for every priority slot.
        let index = unsafe { links::Index::new_usize(timer.index()) };
        Some((timer, Entry::new(self, index)))
    }

    fn select<'a, P: Ord, C: alloc::raw::Extent>(
        &'a mut self,
        heap: &'a mut indexed::Min<P, C>,
        index: links::Index,
    ) -> (indexed::Entry<'a, P, C>, Entry<'a, T, PLANES>) {
        let entry = Entry::new(self, index);
        // SAFETY: callers retain both disjoint projections of the same coupled owner.
        let live = unsafe { crate::depot::raw::Proof::new(((heap, entry), index.position())) };
        indexed::Slot::<_, _, _>::from_source(live).entry()
    }
    fn grow_with<P: Ord, C: alloc::raw::Extent>(
        &mut self,
        heap: &mut indexed::Min<P, C>,
        len: C,
        seed: impl planes::Seed<T>,
    ) -> Result<&mut Self, planes::Error<C>> {
        let old = self.entries.len();
        if len.len() <= old {
            return Ok(self);
        }
        let mut next = indexed::Min::try_new(len).map_err(planes::Error::Heap)?;
        let core = self
            .grow(len.len() as u32, seed)
            .map_err(|error| planes::Error::Values {
                capacity: len,
                error,
            })?;
        while let Some(timer) = heap.first_entry() {
            // SAFETY: the coupled owner retains one payload for every old priority.
            let index = unsafe { links::Index::new_usize(timer.index()) };
            let entry = Entry::<T, PLANES>::new(core, index);
            let priority = timer.take();
            // SAFETY: the new heap covers this payload, and each old priority moves once.
            let live =
                unsafe { crate::depot::raw::Proof::new(((&mut next, entry), index.position())) };
            let slot = indexed::Slot::<_, _, _, indexed::Empty>::from_source(live);
            let (vacant, _) = slot.entry();
            vacant.set(priority);
        }
        *heap = next;
        Ok(core)
    }

    /// The admitted prefix is represented and every selected-plane hook is vacant.
    unsafe fn link_prefix<const PLANE: usize>(&mut self, len: usize) {
        const { assert!(PLANE < PLANES) };
        for index in 0..len {
            // SAFETY: the caller retains this exact prefix and its vacant hook phase.
            let index = unsafe { links::Index::new_usize(index) };
            Entry::<_, PLANES, phase::Vacant<PLANE>>::new(self, index).link_back();
        }
    }
}

impl<T, P: Ord, const PLANES: usize, C: alloc::raw::Extent> planes::Priority<T, P, PLANES, C>
    for Set<T, PLANES, indexed::Min<P, C>>
{
    fn try_seed(len: C, seed: impl planes::Seed<T>) -> Result<Self, planes::Error<C>> {
        let index = indexed::Min::try_new(len).map_err(planes::Error::Heap)?;
        let values = planes::Set::<T, PLANES>::try_seed(len.len(), seed).map_err(|error| {
            planes::Error::Values {
                capacity: len,
                error,
            }
        })?;
        Ok(Self {
            core: values.core,
            index,
        })
    }

    fn try_repeat(len: C, priority: P) -> Result<Self, planes::Error<C>>
    where
        T: Default,
        P: Copy,
    {
        let index = indexed::Min::try_repeat(len, priority).map_err(planes::Error::Heap)?;
        let values = planes::Set::<T, PLANES>::try_seed(len.len(), ()).map_err(|error| {
            planes::Error::Values {
                capacity: len,
                error,
            }
        })?;
        Ok(Self {
            core: values.core,
            index,
        })
    }

    /// Removes priorities and lends their payloads, preserving queue membership.
    fn reset<'a>(&'a mut self) -> impl Iterator<Item = &'a mut T>
    where
        T: 'a,
    {
        self.index.clear();
        self.iter_mut()
    }

    fn heap(&self) -> &indexed::Min<P, C> {
        &self.index
    }

    /// Selects a payload and its exact priority slot with one domain check.
    fn select(
        &mut self,
        index: usize,
    ) -> Option<(indexed::Entry<'_, P, C>, planes::Entry<'_, T, PLANES>)> {
        if index >= self.len() {
            return None;
        }
        // SAFETY: the coupled constructor and growth retain equal domains.
        let index = unsafe { links::Index::new_usize(index) };
        Some(self.core.select(&mut self.index, index))
    }

    /// Selects the minimum priority together with its original payload owner.
    fn first(&mut self) -> Option<(indexed::Occupied<'_, P, C>, planes::Entry<'_, T, PLANES>)> {
        self.core.first(&mut self.index)
    }

    /// Grows both domains atomically, retaining every priority and queue link.
    fn try_grow_to(
        &mut self,
        len: C,
        seed: impl planes::Seed<T>,
    ) -> Result<ops::Range<usize>, planes::Error<C>> {
        let old = self.core.entries.len();
        let core = self.core.grow_with(&mut self.index, len, seed)?;
        Ok(old..core.entries.len())
    }
}

const _: () = {
    assert!(
        mem::size_of::<planes::Set<(), 3, indexed::Min<u64>>>()
            == mem::size_of::<planes::Set<(), 3>>() + mem::size_of::<indexed::Min<u64>>()
    );
    assert!(mem::size_of::<planes::Entry<'static, (), 3>>() == 2 * mem::size_of::<usize>());
};

impl<T, P: Ord, const PLANES: usize, const MAX: usize>
    Set<T, PLANES, indexed::Min<P, capacity::Limit<MAX>>>
{
    /// Creates a fresh domain and links its admitted prefix into one plane.
    pub fn try_seed_in<const PLANE: usize>(
        capacity: capacity::Limit<MAX>,
        seed: impl Seed<T>,
    ) -> Result<Self, Error<capacity::Limit<MAX>>> {
        let mut values = <Self as Priority<T, P, PLANES, _>>::try_seed(capacity, seed)?;
        // SAFETY: construction admits the entire fresh prefix; all its hooks are vacant.
        unsafe { values.core.link_prefix::<PLANE>(capacity.get()) };
        Ok(values)
    }
}

impl<T, P: Ord, R, const PLANES: usize, const MAX: usize, const N: usize, const MULTIPLIER: usize>
    TryFrom<Set<T, PLANES, indexed::Min<P, capacity::Limit<MAX>>>>
    for Set<T, PLANES, groups::Index<P, capacity::Limit<MAX>, R, N, MULTIPLIER>>
{
    type Error = alloc::Error;

    fn try_from(
        source: Set<T, PLANES, indexed::Min<P, capacity::Limit<MAX>>>,
    ) -> Result<Self, Self::Error> {
        let records = groups::Index::<P, _, R, N, MULTIPLIER>::reserve(source.index.capacity())?;
        Ok(Self {
            core: source.core,
            index: groups::Index {
                priority: source.index,
                records,
            },
        })
    }
}

impl<T, P: Ord, R, const PLANES: usize, const MAX: usize, const N: usize, const MULTIPLIER: usize>
    Set<T, PLANES, groups::Index<P, capacity::Limit<MAX>, R, N, MULTIPLIER>>
{
    /// Prepares a new prefix, retaining an exclusive loan until companion commit.
    /// Dropping the commit retains grown storage but preserves the active prefix and memberships.
    pub fn try_replace<const PLANE: usize>(
        &mut self,
        records: runtime::Fifo<
            R,
            groups::Rows<capacity::Limit<MAX>, N>,
            groups::Buckets<capacity::Limit<MAX>, MULTIPLIER>,
            links::List,
        >,
        seed: impl Seed<T>,
    ) -> Result<impl FnOnce() + '_, Error<capacity::Limit<MAX>>> {
        let capacity = records.capacity().0;
        let target = &mut self.index.records;
        let core = self
            .core
            .grow_with(&mut self.index.priority, capacity, seed)?;
        Ok(move || {
            core.clear::<PLANE>();
            // SAFETY: successful growth retains this owner and its represented prefix.
            // Clearing the same owner detached every selected-plane hook.
            unsafe { core.link_prefix::<PLANE>(capacity.get()) };
            *target = records;
        })
    }

    /// Returns the active prefix within the retained physical rows.
    pub fn limit(&self) -> capacity::Limit<MAX> {
        self.index.records.capacity().0
    }

    pub fn heap(&self) -> &indexed::Min<P, capacity::Limit<MAX>> {
        &self.index.priority
    }

    pub fn first(
        &mut self,
    ) -> Option<(
        indexed::Occupied<'_, P, capacity::Limit<MAX>>,
        Entry<'_, T, PLANES>,
    )> {
        self.core.first(&mut self.index.priority)
    }
}