SlidingRing

Struct SlidingRing 

Source
pub struct SlidingRing<T: Copy, const DEPTH: u8> { /* private fields */ }
Expand description

Ring that slides over signed coordinates while keeping slot access O(1).

Implementations§

Source§

impl<T: Copy, const DEPTH: u8> SlidingRing<T, DEPTH>

Source

pub const DEPTH: u8 = DEPTH

Compile-time depth parameter exposed for downstream consumers.

Source

pub fn new(zero: T) -> Self

Create a new ring seeded with zero. The anchor index starts at 0.

Source

pub fn borrow_anchor(&self) -> &T

Borrow the slot at the anchor.

Source

pub fn borrow_anchor_mut(&mut self) -> &mut T

Borrow the slot at the anchor mutably.

Source

pub fn slot(&self, offset: RingIndex) -> &T

Immutable access to the slot for offset (measured from the anchor).

Source

pub fn slot_mut(&mut self, offset: RingIndex) -> &mut T

Mutable access to the slot for offset (measured from the anchor).

Source

pub fn shift_by(&mut self, delta: i128)

Shift the anchor by delta, clearing only the slots that newly enter the window.

Source

pub fn shift_by_keep_anchor(&mut self, delta: i128)

Shift by delta while preserving the new anchor slot on backward moves.

Source

pub fn slot_by_index(&self, index: RingIndex) -> &T

Immutable access by raw ring index (0-255).

Source

pub fn slot_by_index_mut(&mut self, index: RingIndex) -> &mut T

Mutable access by raw ring index (0-255).

Source

pub fn clear_all(&mut self)

Clear every slot and reset the anchor index to zero.

Source

pub fn as_slice(&self) -> &[T; 256]

Expose immutable slice reference for advanced use-cases.

Source

pub fn as_mut_slice(&mut self) -> &mut [T; 256]

Expose mutable slice reference for advanced use-cases.

Source

pub fn slices_from_anchor(&self) -> (&[T], &[T])

Returns the contiguous slices that cover the ring starting at the anchor.

The first slice begins at the anchor slot; the second slice wraps around to index 0 (and may be empty). This is ideal for SIMD scans.

const DEPTH: u8 = 16;
let mut ring = SlidingRing::<u64, DEPTH>::new(0);
*ring.slot_mut(0) = 42;
let (tail, head) = ring.slices_from_anchor();
assert_eq!(tail.first(), Some(&42));
assert!(head.is_empty());
Source

pub fn slices_from_anchor_mut(&mut self) -> (&mut [T], &mut [T])

Mutable variant of SlidingRing::slices_from_anchor.

const DEPTH: u8 = 16;
let mut ring = SlidingRing::<u64, DEPTH>::new(0);
{
    let (tail, head) = ring.slices_from_anchor_mut();
    tail[0] = 7;
    assert!(head.is_empty());
}
assert_eq!(*ring.slot(0), 7);
Source

pub fn iter(&self) -> Slots<'_, T>

Iterate over slots in storage order using raw pointers.

Source

pub fn iter_mut(&mut self) -> SlotsMut<'_, T>

Mutable iterator over slots in storage order using raw pointers.

Source

pub fn iter_from_anchor(&self, direction: Direction) -> AnchorIter<'_, T, DEPTH>

Iterate starting at the anchor in the desired direction (wrapping once).

const DEPTH: u8 = 16;
let mut ring = SlidingRing::<u64, DEPTH>::new(0);
*ring.slot_mut(0) = 1;
*ring.slot_mut(1) = 2;
let mut iter = ring.iter_from_anchor(Direction::Forward);
assert_eq!(iter.next().unwrap().1, &1);
assert_eq!(iter.next().unwrap().0, 1);
Source

pub fn iter_from_anchor_mut( &mut self, direction: Direction, ) -> AnchorIterMut<'_, T, DEPTH>

Mutable iteration starting at the anchor in the desired direction.

const DEPTH: u8 = 16;
let mut ring = SlidingRing::<u64, DEPTH>::new(0);
for (_, slot) in ring.iter_from_anchor_mut(Direction::Forward).take(2) {
    *slot = 5;
}
assert_eq!(*ring.slot(0), 5);
assert_eq!(*ring.slot(1), 5);

Trait Implementations§

Source§

impl<T: Copy, const DEPTH: u8> Debug for SlidingRing<T, DEPTH>

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more

Auto Trait Implementations§

§

impl<T, const DEPTH: u8> Freeze for SlidingRing<T, DEPTH>
where T: Freeze,

§

impl<T, const DEPTH: u8> RefUnwindSafe for SlidingRing<T, DEPTH>
where T: RefUnwindSafe,

§

impl<T, const DEPTH: u8> Send for SlidingRing<T, DEPTH>
where T: Send,

§

impl<T, const DEPTH: u8> Sync for SlidingRing<T, DEPTH>
where T: Sync,

§

impl<T, const DEPTH: u8> Unpin for SlidingRing<T, DEPTH>
where T: Unpin,

§

impl<T, const DEPTH: u8> UnwindSafe for SlidingRing<T, DEPTH>
where T: UnwindSafe,

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.