Skip to main content

SortedVec

Struct SortedVec 

Source
pub struct SortedVec<'a, T: Pod + FixedLayout + Ord> { /* private fields */ }
Expand description

Sorted bounded dynamic array – zero-copy with O(log n) binary search.

Elements are kept in ascending order. Insert is O(n) (shift right), remove is O(n) (shift left), search is O(log n).

Implementations§

Source§

impl<'a, T: Pod + FixedLayout + Ord> SortedVec<'a, T>

Source

pub fn from_bytes(data: &'a mut [u8]) -> Result<Self, ProgramError>

Overlay a SortedVec on a mutable byte slice.

Source

pub fn len(&self) -> usize

Current number of elements.

Source

pub fn capacity(&self) -> usize

Maximum capacity.

Source

pub fn is_empty(&self) -> bool

Is the vec empty?

Source

pub fn is_full(&self) -> bool

Is the vec at capacity?

Binary search for a value. Returns Ok(index) if found, or Err(insert_index) where insert_index is where the value would go.

Source

pub fn contains(&self, target: &T) -> bool

Check if the value exists in the vec. O(log n).

Source

pub fn get(&self, index: usize) -> Result<T, ProgramError>

Get element at index (bounds-checked).

Source

pub fn insert(&mut self, value: T) -> Result<usize, ProgramError>

Insert a value in sorted position. O(n) due to shift. Returns the index where it was inserted.

Source

pub fn insert_unique(&mut self, value: T) -> Result<usize, usize>

Insert a value only if it doesn’t already exist. Returns Ok(index) on successful insert, or Err(existing_index) if duplicate.

Source

pub fn remove(&mut self, index: usize) -> Result<T, ProgramError>

Remove value at index (shift left). O(n).

Source

pub fn remove_value(&mut self, value: &T) -> Result<T, ProgramError>

Remove a specific value by searching for it first. O(n).

Source

pub fn min(&self) -> Result<T, ProgramError>

Returns the minimum element (first). O(1).

Source

pub fn max(&self) -> Result<T, ProgramError>

Returns the maximum element (last). O(1).

Auto Trait Implementations§

§

impl<'a, T> Freeze for SortedVec<'a, T>

§

impl<'a, T> RefUnwindSafe for SortedVec<'a, T>
where T: RefUnwindSafe,

§

impl<'a, T> Send for SortedVec<'a, T>
where T: Send,

§

impl<'a, T> Sync for SortedVec<'a, T>
where T: Sync,

§

impl<'a, T> Unpin for SortedVec<'a, T>
where T: Unpin,

§

impl<'a, T> UnsafeUnpin for SortedVec<'a, T>

§

impl<'a, T> !UnwindSafe for SortedVec<'a, T>

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.