SortedIndexBuffer

Struct SortedIndexBuffer 

Source
pub struct SortedIndexBuffer<T> { /* private fields */ }
Expand description

A buffer indexed by a u64 sequence number.

This behaves idential to a BTreeMap<u64, T>, but is optimized for the case where the indices are mostly contiguous, with occasional gaps.

It will have large memory overhead if the indices are very sparse, so it should not be used as a general-purpose sorted map.

§Internals

The underlying storage is a contiguous buffer of Option<T> of twice the size of the key range, rounded up to the next power of two. We track min and max.

The buffer is a Vec<Option<T>>, which can have some additional unused capacity.

|x_xxxx|________| max-min=6, page size 8, buffer fits in first page ^ min ^ max |_x|xxxx| max-min=6, page size 8, buffer spans two pages ^ min ^ max

Access is O(1). Insertion and removal is usually O(1), but will occasionally move contents around to resize the buffer, which is O(n). Moving will only happen every O(n.next_power_of_two()) operations, so amortized complexity is still O(1).

Implementations§

Source§

impl<T> SortedIndexBuffer<T>

Source

pub fn with_capacity(capacity: usize) -> Self

Create a new SortedIndexBuffer with the given initial capacity.

Source

pub fn new() -> Self

Create a new, empty SortedIndexBuffer.

Source

pub fn is_empty(&self) -> bool

Returns true if the buffer contains no elements.

Source

pub fn contains_key(&self, index: u64) -> bool

Returns true if the buffer contains an element at the given index.

Source

pub fn keys_range<R: RangeBounds<u64>>( &self, range: R, ) -> impl DoubleEndedIterator<Item = u64> + '_

Iterate over all keys in the given index range in ascending order.

Source

pub fn values_range<R: RangeBounds<u64>>( &self, range: R, ) -> impl DoubleEndedIterator<Item = &T> + '_

Iterate over all values in the given index range in ascending order of their keys.

Source

pub fn values_range_mut<R: RangeBounds<u64>>( &mut self, range: R, ) -> impl DoubleEndedIterator<Item = &mut T> + '_

Iterate over all values in the given index range in ascending order of their keys.

Source

pub fn iter_range<R: RangeBounds<u64>>( &self, range: R, ) -> impl DoubleEndedIterator<Item = (u64, &T)> + '_

Iterate over all (index, value) pairs in the given index range in ascending order of their keys.

Source

pub fn retain(&mut self, f: impl FnMut(u64, &mut T) -> bool)

Source

pub fn retain_range<R: RangeBounds<u64>>(&mut self, range: R)

Retain only the elements in the given index range.

Source

pub fn keys(&self) -> impl DoubleEndedIterator<Item = u64> + '_

Iterate over all keys in the buffer in ascending order.

Source

pub fn values(&self) -> impl DoubleEndedIterator<Item = &T> + '_

Iterate over all values in the buffer in ascending order of their keys.

Source

pub fn values_mut(&mut self) -> impl DoubleEndedIterator<Item = &mut T> + '_

Iterate over all values in the buffer in ascending order of their keys.

Source

pub fn iter(&self) -> impl DoubleEndedIterator<Item = (u64, &T)> + '_

Iterate over all (index, value) pairs in the buffer in ascending order of their keys.

Values are returned by reference.

Source

pub fn into_iter(self) -> impl DoubleEndedIterator<Item = (u64, T)>

Turn into an iterator over all (index, value) pairs in the buffer in ascending order of their keys.

This is an explicit method instead of implementing IntoIterator, so we can return a DoubleEndedIterator without having to name the iterator type.

Source

pub fn get(&self, index: u64) -> Option<&T>

Get a reference to the value at the given index, if it exists.

Source

pub fn insert(&mut self, index: u64, value: T)

Insert value at index.

Source

pub fn remove(&mut self, index: u64) -> Option<T>

Remove value at index.

Trait Implementations§

Source§

impl<T: Clone> Clone for SortedIndexBuffer<T>

Source§

fn clone(&self) -> SortedIndexBuffer<T>

Returns a duplicate of the value. Read more
1.0.0 · Source§

fn clone_from(&mut self, source: &Self)

Performs copy-assignment from source. Read more
Source§

impl<T: Debug> Debug for SortedIndexBuffer<T>

Source§

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

Formats the value using the given formatter. Read more
Source§

impl<T> Default for SortedIndexBuffer<T>

Source§

fn default() -> Self

Returns the “default value” for a type. Read more

Auto Trait Implementations§

§

impl<T> Freeze for SortedIndexBuffer<T>

§

impl<T> RefUnwindSafe for SortedIndexBuffer<T>
where T: RefUnwindSafe,

§

impl<T> Send for SortedIndexBuffer<T>
where T: Send,

§

impl<T> Sync for SortedIndexBuffer<T>
where T: Sync,

§

impl<T> Unpin for SortedIndexBuffer<T>
where T: Unpin,

§

impl<T> UnwindSafe for SortedIndexBuffer<T>
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> CloneToUninit for T
where T: Clone,

Source§

unsafe fn clone_to_uninit(&self, dest: *mut u8)

🔬This is a nightly-only experimental API. (clone_to_uninit)
Performs copy-assignment from self to dest. 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> ToOwned for T
where T: Clone,

Source§

type Owned = T

The resulting type after obtaining ownership.
Source§

fn to_owned(&self) -> T

Creates owned data from borrowed data, usually by cloning. Read more
Source§

fn clone_into(&self, target: &mut T)

Uses borrowed data to replace owned data, usually by cloning. Read more
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.