pub struct ImmutableVector<T> { /* private fields */ }Expand description
An immutable vector implementation with efficient edits/clones.
Internally, the vector is represented like a finger tree. Each interal node holds up to 32 children, and each leaf node holds up to 32 values. Most operations take O(log32 n) time, but log32 grows really slowly, so in pracice they are basically O(1).
§Example
use rust_dsa::ImmutableVector;
let vector = ImmutableVector::new();
// Each time we push a value, the original vector doesn't change.
let vector1 = vector.push(1);
let vector2 = vector1.push(2);
let vector3 = vector2.push(3);
assert_eq!(vector, ImmutableVector::from([]));
assert_eq!(vector1, ImmutableVector::from([1]));
assert_eq!(vector2, ImmutableVector::from([1, 2]));
assert_eq!(vector3, ImmutableVector::from([1, 2, 3]));
// We can also remove elements.
let vector4 = vector3.remove(1);
assert_eq!(vector4, ImmutableVector::from([1, 3]));
// And join two vectors.
let vector5 = vector4.join(&vector2);
assert_eq!(vector5, ImmutableVector::from([1, 3, 1, 2]));
// We can iterate over vectors.
for x in vector5 {
// x has type Rc<i32>
println!("{x}");
}
for x in &vector4 {
// x has type &i32
println!("{x}");
}
// And create vectors from iterators.
let vector6: ImmutableVector<_> = ('a'..='z').collect();
assert_eq!(vector6.len(), 26);§Runtime complexity
| Operation | Runtime Complexity |
|---|---|
ImmutableVector::push | O(log32 n) |
ImmutableVector::remove | O(log32 n) |
ImmutableVector::replace | O(log32 n) |
ImmutableVector::join | O(1) |
ImmutableVector::clone | O(1) |
Implementations§
Source§impl<T> ImmutableVector<T>
impl<T> ImmutableVector<T>
Sourcepub fn new() -> ImmutableVector<T>
pub fn new() -> ImmutableVector<T>
Creates an empty vector.
Sourcepub fn get(&self, index: usize) -> Option<&T>
pub fn get(&self, index: usize) -> Option<&T>
Returns a reference to the element at index or None if out of bounds.
§Example
use rust_dsa::ImmutableVector;
let vector = ImmutableVector::from([1, 2, 3, 4, 5, 6, 7]);
assert_eq!(vector.get(3), Some(&4));
assert_eq!(vector.get(10), None);Sourcepub fn range<R>(&self, range: R) -> ImmutableVector<T>where
R: RangeBounds<usize>,
pub fn range<R>(&self, range: R) -> ImmutableVector<T>where
R: RangeBounds<usize>,
Returns a new vector that contains the elements in the specified range.
§Panics
Panics if the starting point is greater than the end point or if the end point is greater than the length of the vector.
§Example
use rust_dsa::ImmutableVector;
let racecar: ImmutableVector<_> = "racecar".chars().collect();
let car: ImmutableVector<_> = "car".chars().collect();
assert_eq!(racecar.range(4..), car);Sourcepub fn push(&self, value: T) -> ImmutableVector<T>
pub fn push(&self, value: T) -> ImmutableVector<T>
Returns a new vector with the value appended.
§Example
use rust_dsa::ImmutableVector;
let vector = ImmutableVector::new();
let vector = vector.push(1);
let vector = vector.push(2);
let vector = vector.push(3);
assert_eq!(vector, ImmutableVector::from([1, 2, 3]));Sourcepub fn remove(&self, index: usize) -> ImmutableVector<T>
pub fn remove(&self, index: usize) -> ImmutableVector<T>
Sourcepub fn replace(&self, index: usize, value: T) -> ImmutableVector<T>
pub fn replace(&self, index: usize, value: T) -> ImmutableVector<T>
Returns a new vector with value at position index.
§Panics
Panics if index >= len.
§Example
use rust_dsa::ImmutableVector;
let unseasonably: ImmutableVector<_> = "unseasonably".chars().collect();
let unreasonably: ImmutableVector<_> = "unreasonably".chars().collect();
assert_eq!(
unseasonably.replace(2, 'r'),
unreasonably
);Sourcepub fn join(&self, other: &ImmutableVector<T>) -> ImmutableVector<T>
pub fn join(&self, other: &ImmutableVector<T>) -> ImmutableVector<T>
Returns a new vector that contains the elements in self
followed by the elements in other.
§Example
use rust_dsa::ImmutableVector;
let fruits = ImmutableVector::from(["apple", "banana"]);
let veggies = ImmutableVector::from(["asparagus", "broccoli"]);
let both = fruits.join(&veggies);
assert_eq!(
both,
ImmutableVector::from(["apple", "banana", "asparagus", "broccoli"])
);Trait Implementations§
Source§impl<T: Clone> Clone for ImmutableVector<T>
impl<T: Clone> Clone for ImmutableVector<T>
Source§fn clone(&self) -> ImmutableVector<T>
fn clone(&self) -> ImmutableVector<T>
1.0.0 · Source§fn clone_from(&mut self, source: &Self)
fn clone_from(&mut self, source: &Self)
source. Read more