OnceBiVec

Struct OnceBiVec 

Source
pub struct OnceBiVec<T> {
    data: OnceVec<T>,
    min_degree: i32,
}
Expand description

A vector that supports negative indices, built on top of OnceVec.

OnceBiVec extends the functionality of OnceVec by allowing elements to be indexed using negative integers. This is useful for scenarios where you need to represent data that naturally starts from a negative index. Note that we still only support appending elements to the end of the vector, so it’s not possible to insert elements at arbitrarily negative indices.

§Examples

use once::OnceBiVec;

// Create a bidirectional vector with minimum degree -3
let vec = OnceBiVec::<i32>::new(-3);

// Insert elements at various positions
vec.push_ooo(10, -3); // At minimum degree
vec.push_ooo(30, -1);
vec.push_ooo(20, -2);
vec.push_ooo(50, 1);
vec.push_ooo(40, 0);

// Access elements using their indices
assert_eq!(vec[-3], 10);
assert_eq!(vec[-2], 20);
assert_eq!(vec[-1], 30);
assert_eq!(vec[0], 40);
assert_eq!(vec[1], 50);

// Get the range of valid indices
assert_eq!(vec.range(), -3..2);

Fields§

§data: OnceVec<T>§min_degree: i32

Implementations§

Source§

impl<T> OnceBiVec<T>

Source

pub fn new(min_degree: i32) -> Self

Creates a new empty OnceBiVec with the specified minimum degree.

§Parameters
  • min_degree: The minimum degree (lowest index) of the vector
§Examples
use once::OnceBiVec;

let vec = OnceBiVec::<i32>::new(-5);
assert_eq!(vec.min_degree(), -5);
assert_eq!(vec.len(), -5);
assert!(vec.is_empty());
Source

pub fn from_vec(min_degree: i32, data: Vec<T>) -> Self

Creates an OnceBiVec from a Vec with the specified minimum degree.

§Parameters
  • min_degree: The minimum degree (lowest index) of the vector
  • data: The vector of values to initialize with
§Examples
use once::OnceBiVec;

let vec = OnceBiVec::from_vec(-2, vec![10, 20, 30]);
assert_eq!(vec.min_degree(), -2);
assert_eq!(vec.len(), 1); // -2 + 3 = 1
assert_eq!(vec[-2], 10);
assert_eq!(vec[-1], 20);
assert_eq!(vec[0], 30);
Source

pub fn from_bivec(data: BiVec<T>) -> Self

Creates an OnceBiVec from a bivec::BiVec.

This is a convenience method for converting from the bivec crate’s bidirectional vector implementation.

§Parameters
  • data: The bivec::BiVec to convert from
Source

pub const fn min_degree(&self) -> i32

Returns the minimum degree (lowest index) of the vector.

§Examples
use once::OnceBiVec;

let vec = OnceBiVec::<i32>::new(-3);
assert_eq!(vec.min_degree(), -3);
Source

pub fn max_degree(&self) -> i32

This returns the largest degree in the bivector. This is equal to self.len() - 1.

§Example
let v = BiVec::from_vec(-2, vec![3, 4, 6, 8, 2]);
assert_eq!(v.max_degree(), 2);
Source

pub fn len(&self) -> i32

This returns the “length” of the bivector, defined to be the smallest i such that v[i] is not defined.

§Example
let v = BiVec::from_vec(-2, vec![3, 4, 6, 8, 2]);
assert_eq!(v.len(), 3);
Source

pub fn is_empty(&self) -> bool

Source

pub fn push_checked(&self, value: T, index: i32)

Source

pub fn push(&self, value: T) -> i32

Source

pub fn push_ooo(&self, value: T, index: i32) -> Range<i32>

Source

pub fn ooo_elements(&self) -> Vec<i32>

Source

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

Returns whether the OnceBiVec has remaining out-of-order elements

Source

pub fn get_or_insert(&self, index: i32, to_insert: impl FnOnce() -> T) -> &T

Source

pub fn range(&self) -> Range<i32>

Source

pub fn extend(&self, new_max: i32, f: impl FnMut(i32) -> T)

Extend the OnceBiVec to up to index new_max, filling in the entries with the values of f. This takes the lock before calling f, which is useful behaviour if used in conjunction with OnceBiVec::lock.

This is thread-safe and guaranteed to be idempotent. f will only be called once per index.

In case multiple OnceVec’s have to be simultaneously updated, one can use extend on one of them and push_checked into the others within the function.

§Parameters
  • new_max: After calling this function, self[new_max] will be defined.
  • f: We will fill in the vector with f(i) at the ith index.
§Example
let v: OnceBiVec<i32> = OnceBiVec::new(-4);
v.extend(5, |i| i + 5);
assert_eq!(v.len(), 6);
for (i, &n) in v.iter() {
    assert_eq!(n, i + 5);
}
Source

pub fn last(&self) -> Option<&T>

Source

pub fn lock(&self) -> MutexGuard<'_, OooTracker>

Takes a lock on the OnceBiVec. The OnceBiVec cannot be updated while the lock is held. This is useful when used in conjuction with OnceBiVec::extend;

Source

pub fn values(&self) -> impl Iterator<Item = &T>

Source

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

Source§

impl<T: Send + Sync> OnceBiVec<T>

Source

pub fn maybe_par_extend(&self, new_max: i32, f: impl Fn(i32) -> T + Send + Sync)

A parallel version of extend. If the concurrent feature is enabled, the function f will be run for different indices simultaneously using rayon, through the maybe_rayon crate.

§Example
let v: OnceBiVec<i32> = OnceBiVec::new(-4);
v.maybe_par_extend(5, |i| i + 5);
assert_eq!(v.len(), 6);
for (i, &n) in v.iter() {
    assert_eq!(n, i + 5);
}
Source

pub fn maybe_par_iter( &self, ) -> impl MaybeParallelIterator<Item = (i32, &T)> + MaybeIndexedParallelIterator

Trait Implementations§

Source§

impl<T: Clone> Clone for OnceBiVec<T>

Source§

fn clone(&self) -> OnceBiVec<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 OnceBiVec<T>

Source§

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

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

impl<T> Index<i32> for OnceBiVec<T>

Source§

type Output = T

The returned type after indexing.
Source§

fn index(&self, i: i32) -> &T

Performs the indexing (container[index]) operation. Read more
Source§

impl<T> IndexMut<i32> for OnceBiVec<T>

Source§

fn index_mut(&mut self, i: i32) -> &mut T

Performs the mutable indexing (container[index]) operation. Read more
Source§

impl<'a, T> IntoIterator for &'a OnceBiVec<T>

Source§

type IntoIter = OnceBiVecIter<'a, T>

Which kind of iterator are we turning this into?
Source§

type Item = (i32, &'a T)

The type of the elements being iterated over.
Source§

fn into_iter(self) -> Self::IntoIter

Creates an iterator from a value. Read more
Source§

impl<T: PartialEq> PartialEq for OnceBiVec<T>

Source§

fn eq(&self, other: &OnceBiVec<T>) -> bool

Tests for self and other values to be equal, and is used by ==.
1.0.0 · Source§

fn ne(&self, other: &Rhs) -> bool

Tests for !=. The default implementation is almost always sufficient, and should not be overridden without very good reason.
Source§

impl<T: Eq> Eq for OnceBiVec<T>

Source§

impl<T> StructuralPartialEq for OnceBiVec<T>

Auto Trait Implementations§

§

impl<T> !Freeze for OnceBiVec<T>

§

impl<T> RefUnwindSafe for OnceBiVec<T>

§

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

§

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

§

impl<T> Unpin for OnceBiVec<T>

§

impl<T> !UnwindSafe for OnceBiVec<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> 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> IntoEither for T

Source§

fn into_either(self, into_left: bool) -> Either<Self, Self>

Converts self into a Left variant of Either<Self, Self> if into_left is true. Converts self into a Right variant of Either<Self, Self> otherwise. Read more
Source§

fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
where F: FnOnce(&Self) -> bool,

Converts self into a Left variant of Either<Self, Self> if into_left(&self) returns true. Converts self into a Right variant of Either<Self, Self> otherwise. Read more
§

impl<T> Pointable for T

§

const ALIGN: usize

The alignment of pointer.
§

type Init = T

The type for initializers.
§

unsafe fn init(init: <T as Pointable>::Init) -> usize

Initializes a with the given initializer. Read more
§

unsafe fn deref<'a>(ptr: usize) -> &'a T

Dereferences the given pointer. Read more
§

unsafe fn deref_mut<'a>(ptr: usize) -> &'a mut T

Mutably dereferences the given pointer. Read more
§

unsafe fn drop(ptr: usize)

Drops the object pointed to by the given pointer. Read more
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.