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: i32Implementations§
Source§impl<T> OnceBiVec<T>
impl<T> OnceBiVec<T>
Sourcepub fn from_vec(min_degree: i32, data: Vec<T>) -> Self
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 vectordata: 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);Sourcepub fn from_bivec(data: BiVec<T>) -> Self
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: Thebivec::BiVecto convert from
Sourcepub const fn min_degree(&self) -> i32
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);Sourcepub fn max_degree(&self) -> i32
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);Sourcepub fn len(&self) -> i32
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);pub fn is_empty(&self) -> bool
pub fn push_checked(&self, value: T, index: i32)
pub fn push(&self, value: T) -> i32
pub fn ooo_elements(&self) -> Vec<i32>
Sourcepub fn get(&self, index: i32) -> Option<&T>
pub fn get(&self, index: i32) -> Option<&T>
Returns whether the OnceBiVec has remaining out-of-order elements
pub fn get_or_insert(&self, index: i32, to_insert: impl FnOnce() -> T) -> &T
pub fn range(&self) -> Range<i32> ⓘ
Sourcepub fn extend(&self, new_max: i32, f: impl FnMut(i32) -> T)
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 withf(i)at theith 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);
}pub fn last(&self) -> Option<&T>
Sourcepub fn lock(&self) -> MutexGuard<'_, OooTracker>
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;
pub fn values(&self) -> impl Iterator<Item = &T>
pub fn iter(&self) -> OnceBiVecIter<'_, T> ⓘ
Source§impl<T: Send + Sync> OnceBiVec<T>
impl<T: Send + Sync> OnceBiVec<T>
Sourcepub fn maybe_par_extend(&self, new_max: i32, f: impl Fn(i32) -> T + Send + Sync)
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);
}pub fn maybe_par_iter( &self, ) -> impl MaybeParallelIterator<Item = (i32, &T)> + MaybeIndexedParallelIterator
Trait Implementations§
Source§impl<'a, T> IntoIterator for &'a OnceBiVec<T>
impl<'a, T> IntoIterator for &'a OnceBiVec<T>
impl<T: Eq> Eq for OnceBiVec<T>
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> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
Source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Source§impl<T> CloneToUninit for Twhere
T: Clone,
impl<T> CloneToUninit for Twhere
T: Clone,
Source§impl<T> IntoEither for T
impl<T> IntoEither for T
Source§fn into_either(self, into_left: bool) -> Either<Self, Self> ⓘ
fn into_either(self, into_left: bool) -> Either<Self, Self> ⓘ
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 moreSource§fn into_either_with<F>(self, into_left: F) -> Either<Self, Self> ⓘ
fn into_either_with<F>(self, into_left: F) -> Either<Self, Self> ⓘ
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