o3 0.61.0

shared-nothing primitives
Documentation
use std::cmp;

use super::Source;

/// # Safety
/// The caller retains this root's initialized-prefix/reverse-map bijection.
/// It admits the absent value and one spare position below u32::MAX.
pub unsafe fn append<S: Source>(owner: &mut S, value: S::Value) -> usize {
    let position = owner.len() as usize;
    unsafe { *owner.len_mut() = position as u32 + 1 };
    let mut hole = Hole {
        owner,
        value,
        position,
    };
    unsafe { hole.up(|owner, position, value| owner.compare(position, value)) };
    let position = hole.position;
    unsafe { hole.commit::<true>() };
    position
}

/// # Safety
/// The caller retains this root's initialized-prefix/reverse-map bijection and
/// this member. The replacement retains the same reverse-map entry; UPDATE also
/// preserves its encoded identity and ancillary records.
pub unsafe fn replace<const UPDATE: bool, S: Source>(
    owner: &mut S,
    position: usize,
    value: S::Value,
) -> (usize, S::Value) {
    let direction = if UPDATE {
        unsafe { owner.direction(position, &value) }
    } else {
        cmp::Ordering::Equal
    };
    let old = unsafe { owner.read::<false>(position) };
    let len = owner.len() as usize;
    let mut hole = Hole {
        owner,
        value,
        position,
    };
    if UPDATE {
        match direction {
            cmp::Ordering::Less => unsafe {
                hole.up(|owner, position, value| owner.compare(position, value))
            },
            cmp::Ordering::Greater => unsafe {
                hole.down(len, |owner, position, value| owner.compare(position, value))
            },
            cmp::Ordering::Equal => {}
        }
    } else {
        unsafe { hole.repair(len, |owner, position, value| owner.compare(position, value)) };
    }
    let final_position = hole.position;
    if !UPDATE || final_position != position {
        unsafe { hole.commit::<true>() };
    } else {
        unsafe { hole.commit::<false>() };
    }
    (final_position, old)
}

/// # Safety
/// The caller retains this root's initialized-prefix/reverse-map bijection and
/// this position is its initialized heap member.
pub unsafe fn remove<S: Source>(owner: &mut S, position: usize) -> S::Value {
    let tail = (owner.len() - 1) as usize;
    let value = unsafe { owner.read::<true>(position) };
    if position == tail {
        unsafe { *owner.len_mut() = tail as u32 };
        return value;
    }
    let mut hole = Hole {
        owner,
        value,
        position,
    };
    // The tail stays initialized outside the active tree until its final move.
    unsafe {
        hole.repair(tail, |owner, position, _| owner.compare_at(tail, position));
        hole.shift(tail);
        *hole.owner.len_mut() = tail as u32;
    }
    hole.value
}

pub(in crate::depot) struct Hole<'a, S: Source> {
    pub(in crate::depot) owner: &'a mut S,
    pub(in crate::depot) value: S::Value,
    pub(in crate::depot) position: usize,
}

impl<S: Source> Hole<'_, S> {
    pub(in crate::depot) unsafe fn commit<const PLACE: bool>(self) {
        unsafe { self.owner.write::<PLACE>(self.position, self.value) };
    }

    unsafe fn shift(&mut self, source: usize) {
        unsafe { self.owner.shift(source, self.position) };
        self.position = source;
    }

    unsafe fn up(&mut self, mut compare: impl FnMut(&S, usize, &S::Value) -> cmp::Ordering) {
        while self.position != 0 {
            let parent = (self.position - 1) / 2;
            if !compare(self.owner, parent, &self.value).is_lt() {
                break;
            }
            unsafe { self.shift(parent) };
        }
    }

    unsafe fn down(
        &mut self,
        len: usize,
        mut compare: impl FnMut(&S, usize, &S::Value) -> cmp::Ordering,
    ) {
        while len >= 2 && self.position <= (len - 2) / 2 {
            let left = self.position * 2 + 1;
            let right = left + 1;
            let child = left
                + usize::from(right < len && unsafe { self.owner.compare_at(right, left) }.is_lt());
            if !compare(self.owner, child, &self.value).is_gt() {
                break;
            }
            unsafe { self.shift(child) };
        }
    }

    /// The comparison only borrows initialized candidates and preserves this hole.
    pub(in crate::depot) unsafe fn repair(
        &mut self,
        len: usize,
        mut compare: impl FnMut(&S, usize, &S::Value) -> cmp::Ordering,
    ) {
        if self.position != 0 {
            let parent = (self.position - 1) / 2;
            if compare(self.owner, parent, &self.value).is_lt() {
                unsafe {
                    self.shift(parent);
                    self.up(compare);
                }
                return;
            }
        }
        unsafe { self.down(len, compare) };
    }
}