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>
impl<'a, T: Pod + FixedLayout + Ord> SortedVec<'a, T>
Sourcepub fn from_bytes(data: &'a mut [u8]) -> Result<Self, ProgramError>
pub fn from_bytes(data: &'a mut [u8]) -> Result<Self, ProgramError>
Overlay a SortedVec on a mutable byte slice.
Sourcepub fn binary_search(&self, target: &T) -> Result<usize, usize>
pub fn binary_search(&self, target: &T) -> Result<usize, usize>
Binary search for a value. Returns Ok(index) if found, or
Err(insert_index) where insert_index is where the value would go.
Sourcepub fn get(&self, index: usize) -> Result<T, ProgramError>
pub fn get(&self, index: usize) -> Result<T, ProgramError>
Get element at index (bounds-checked).
Sourcepub fn insert(&mut self, value: T) -> Result<usize, ProgramError>
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.
Sourcepub fn insert_unique(&mut self, value: T) -> Result<usize, usize>
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.
Sourcepub fn remove(&mut self, index: usize) -> Result<T, ProgramError>
pub fn remove(&mut self, index: usize) -> Result<T, ProgramError>
Remove value at index (shift left). O(n).
Sourcepub fn remove_value(&mut self, value: &T) -> Result<T, ProgramError>
pub fn remove_value(&mut self, value: &T) -> Result<T, ProgramError>
Remove a specific value by searching for it first. O(n).
Sourcepub fn min(&self) -> Result<T, ProgramError>
pub fn min(&self) -> Result<T, ProgramError>
Returns the minimum element (first). O(1).
Sourcepub fn max(&self) -> Result<T, ProgramError>
pub fn max(&self) -> Result<T, ProgramError>
Returns the maximum element (last). O(1).