o3 0.62.0

shared-nothing primitives
Documentation
use std::{marker, mem};

use crate::{
    alloc,
    base::marker::ThreadBound,
    depot::{
        self,
        index::Initialized,
        map::{
            self,
            priority::{Occupied, Plan, Vacant, errors, sealed::Core},
        },
        queue::links,
    },
};

type Entry<'a, K, P, V> = map::Entry<Occupied<'a, K, P, V>, Vacant<'a, K, P, V>>;

/// A fixed-capacity map whose entries are ordered by minimum priority.
pub struct Min<K, P: Ord, V, H> {
    core: Core<K, V, P>,
    hasher: H,
    thread: marker::PhantomData<ThreadBound>,
}

impl<K, P: Ord, V, H> Min<K, P, V, H> {
    pub fn try_new(plan: Plan<K, P, V>, hasher: H) -> Result<Self, alloc::Exhausted> {
        Ok(Self {
            core: Core::try_new(plan)?,
            hasher,
            thread: marker::PhantomData,
        })
    }

    pub fn peek(&self) -> Option<(&K, &P, &V)> {
        let index = self.core.first()?;
        let node = unsafe { self.core.node(index) };
        Some((&node.key, &node.priority, &node.value))
    }

    pub fn peek_mut(&mut self) -> Option<Occupied<'_, K, P, V>> {
        let index = self.core.first()?;
        Some(Occupied {
            core: &mut self.core,
            index,
        })
    }

    pub fn len(&self) -> usize {
        self.core.len as usize
    }

    pub fn is_empty(&self) -> bool {
        self.core.len == 0
    }

    pub(in crate::depot) fn entry_at(
        source: depot::raw::Proof<(&mut Self, links::Index), Initialized<()>>,
    ) -> Occupied<'_, K, P, V> {
        let (owner, index) = source.into_inner();
        Occupied {
            core: &mut owner.core,
            index: index.raw(),
        }
    }

    pub fn pop(&mut self) -> Option<(K, P, V)> {
        let (key, value, priority) = self.peek_mut()?.remove_parts();
        Some((key, priority, value))
    }
}

impl<K: Eq, P: Ord, V, H: map::Hasher<K>> Min<K, P, V, H> {
    pub fn try_entry(
        &mut self,
        key: K,
    ) -> Result<Entry<'_, K, P, V>, crate::mem::Rejected<K, errors::Full>> {
        let hash = self.hasher.hash(&key);
        let core = &mut self.core;
        match core.probe(hash, &key) {
            Some(map::Entry::Occupied(index)) => Ok(map::Entry::Occupied(Occupied { core, index })),
            Some(map::Entry::Vacant(bucket)) if core.len < core.plan.capacity().raw() => {
                let index = if core.len < core.retired {
                    // SAFETY: the initialized recycled suffix begins at len.
                    unsafe { core.indices.add(core.len as usize).read() }
                } else {
                    core.len
                };
                Ok(map::Entry::Vacant(Vacant {
                    core,
                    bucket,
                    hash,
                    index,
                    key,
                }))
            }
            _ => Err(crate::mem::Rejected::new(key, errors::Full)),
        }
    }

    pub fn occupied(&mut self, key: &K) -> Option<Occupied<'_, K, P, V>> {
        let index = self.core.find(self.hasher.hash(key), key)?;
        Some(Occupied {
            core: &mut self.core,
            index,
        })
    }

    pub fn get(&self, key: &K) -> Option<&V> {
        let index = self.core.find(self.hasher.hash(key), key)?;
        Some(&unsafe { self.core.node(index) }.value)
    }

    pub fn contains_key(&self, key: &K) -> bool {
        self.core.find(self.hasher.hash(key), key).is_some()
    }

    pub fn remove(&mut self, key: &K) -> Option<V> {
        Some(self.occupied(key)?.remove())
    }
}

const _: () = {
    assert!(mem::size_of::<Min<(), (), (), ()>>() == mem::size_of::<Core<(), (), ()>>());
    assert!(mem::align_of::<Min<(), (), (), ()>>() == mem::align_of::<Core<(), (), ()>>());
    assert!(
        mem::size_of::<Option<Min<(), (), (), ()>>>() == mem::size_of::<Option<Core<(), (), ()>>>()
    );
    assert!(
        mem::size_of::<crate::depot::heap::raw::Hole<'static, Core<u32, u32, u32>>>()
            == 3 * mem::size_of::<usize>()
    );
};