o3 0.62.0

shared-nothing primitives
Documentation
use std::mem;

use crate::depot::{
    heap,
    map::{
        self,
        priority::sealed::{Core, Node},
    },
    queue::links,
};

/// A live stable slot under the exclusive loan of its priority map.
pub struct Occupied<'a, K, P: Ord, V> {
    pub(super) core: &'a mut Core<K, V, P>,
    pub(super) index: u32,
}

impl<K, V, P: Ord> Occupied<'_, K, P, V> {
    pub fn index(&self) -> links::Index {
        // SAFETY: every live slot is below the priority plan's non-sentinel capacity.
        unsafe { links::Index::new_raw(self.index) }
    }

    fn node(&self) -> &Node<K, V, P> {
        // SAFETY: construction retained the initialized slot and its exclusive owner.
        unsafe { self.core.node(self.index) }
    }

    pub fn key(&self) -> &K {
        &self.node().key
    }

    pub fn get(&self) -> &V {
        &self.node().value
    }

    pub fn priority(&self) -> &P {
        &self.node().priority
    }

    pub fn get_mut(&mut self) -> &mut V {
        &mut unsafe { self.core.node_mut(self.index) }.value
    }

    pub fn set(&mut self, priority: P) {
        // SAFETY: a live slot has its final position from heap publication.
        let node = unsafe { self.core.node_mut(self.index) };
        let position = unsafe { node.position.assume_init() } as usize;
        let _old = mem::replace(&mut node.priority, priority);
        // SAFETY: replacement preserves the slot identity. The old priority outlives repair.
        unsafe { heap::raw::Source::replace::<false>(self.core, position, self.index) };
    }

    pub fn remove(self) -> V {
        self.remove_parts().1
    }

    pub fn remove_parts(self) -> (K, V, P) {
        let index = self.index;
        let node = self.node();
        // SAFETY: the occupied loan retains both reverse positions until removal.
        let position = unsafe { node.position.assume_init() } as usize;
        let bucket = unsafe { map::raw::Occupied::new(node.bucket as usize) };
        let len = self.core.len;
        let tail = len - 1;
        self.core.len = tail;
        if len > self.core.retired {
            self.core.retired = len;
        }
        if position != tail as usize {
            // SAFETY: the former tail is initialized and leaves the heap. Exchange it for the
            // retired slot, then place the former tail's live resident in the shortened heap.
            let last = unsafe { self.core.indices.add(tail as usize).replace(index) };
            unsafe { heap::raw::Source::replace::<false>(self.core, position, last) };
        }
        let hole = unsafe { self.core.table.directory.vacate(bucket) };
        unsafe { map::raw::close(self.core, hole) };
        // SAFETY: this row left both indexes and ownership transfers exactly once.
        let node = unsafe { self.core.nodes.add(index as usize).read() };
        (node.key, node.value, node.priority)
    }
}

/// A capacity-admitted slot. An unused reservation leaves both indices untouched.
pub struct Vacant<'a, K, P: Ord, V> {
    pub(super) core: &'a mut Core<K, V, P>,
    pub(super) bucket: map::raw::Vacant,
    pub(super) hash: u64,
    pub(super) index: u32,
    pub(super) key: K,
}

impl<K, V, P: Ord> Vacant<'_, K, P, V> {
    pub fn index(&self) -> links::Index {
        // SAFETY: this reservation is below the admitted capacity.
        unsafe { links::Index::new_raw(self.index) }
    }

    pub fn insert(self, priority: P, value: V) {
        let Self {
            core,
            bucket,
            hash,
            index,
            key,
        } = self;
        let node = Node {
            key,
            value,
            priority,
            bucket: bucket.index() as u32,
            position: mem::MaybeUninit::uninit(),
        };
        // SAFETY: the exclusive reservation supplies either a fresh row or the top recycled index.
        // Append installs its final position before publishing the key.
        unsafe {
            core.nodes.add(index as usize).write(node);
            heap::raw::Source::append(core, index);
            core.table.insert(bucket, hash, index);
        }
    }
}