SegmentedVec

Struct SegmentedVec 

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

A segmented vector with stable pointers.

Implementations§

Source§

impl<T> SegmentedVec<T>

Source

pub const fn new() -> Self

Creates a new empty SegmentedVec.

§Example
use segmented_vec::SegmentedVec;
let vec: SegmentedVec<i32> = SegmentedVec::new();
assert!(vec.is_empty());
Source

pub const fn len(&self) -> usize

Returns the number of elements in the vector.

Source

pub const fn is_empty(&self) -> bool

Returns true if the vector contains no elements.

Source

pub fn capacity(&self) -> usize

Returns the current capacity of the vector.

Source

pub fn push(&mut self, value: T)

Appends an element to the back of the vector.

§Panics

Panics if allocation fails.

§Example
use segmented_vec::SegmentedVec;
let mut vec: SegmentedVec<i32> = SegmentedVec::new();
vec.push(1);
vec.push(2);
assert_eq!(vec.len(), 2);
Source

pub fn pop(&mut self) -> Option<T>

Removes the last element from the vector and returns it, or None if empty.

§Example
use segmented_vec::SegmentedVec;
let mut vec: SegmentedVec<i32> = SegmentedVec::new();
vec.push(1);
vec.push(2);
assert_eq!(vec.pop(), Some(2));
assert_eq!(vec.pop(), Some(1));
assert_eq!(vec.pop(), None);
Source

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

Returns a reference to the element at the given index.

Returns None if the index is out of bounds.

Source

pub fn get_mut(&mut self, index: usize) -> Option<&mut T>

Returns a mutable reference to the element at the given index.

Returns None if the index is out of bounds.

Source

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

Returns a reference to the first element, or None if empty.

Source

pub fn first_mut(&mut self) -> Option<&mut T>

Returns a mutable reference to the first element, or None if empty.

Source

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

Returns a reference to the last element, or None if empty.

Source

pub fn last_mut(&mut self) -> Option<&mut T>

Returns a mutable reference to the last element, or None if empty.

Source

pub fn contains(&self, x: &T) -> bool
where T: PartialEq,

Returns true if the slice contains an element with the given value.

Source

pub fn starts_with(&self, needle: &[T]) -> bool
where T: PartialEq,

Returns true if needle is a prefix of the vector.

Source

pub fn ends_with(&self, needle: &[T]) -> bool
where T: PartialEq,

Returns true if needle is a suffix of the vector.

Binary searches this vector for a given element.

If the value is found, returns Ok(index). If not found, returns Err(index) where index is the position where the element could be inserted.

Source

pub fn binary_search_by<F>(&self, f: F) -> Result<usize, usize>
where F: FnMut(&T) -> Ordering,

Binary searches this vector with a comparator function.

Source

pub fn binary_search_by_key<B, F>(&self, b: &B, f: F) -> Result<usize, usize>
where F: FnMut(&T) -> B, B: Ord,

Binary searches this vector with a key extraction function.

Source

pub fn swap(&mut self, a: usize, b: usize)

Swaps two elements in the vector.

§Panics

Panics if a or b are out of bounds.

Source

pub fn reverse(&mut self)

Reverses the order of elements in the vector.

Source

pub fn fill(&mut self, value: T)
where T: Clone,

Fills the vector with the given value.

Source

pub fn fill_with<F>(&mut self, f: F)
where F: FnMut() -> T,

Fills the vector with values produced by a function.

Source

pub fn clear(&mut self)

Clears the vector, removing all elements.

This does not deallocate the dynamic segments.

Source

pub fn truncate(&mut self, new_len: usize)

Shortens the vector, keeping the first new_len elements.

If new_len is greater than or equal to the current length, this has no effect.

Source

pub fn reserve(&mut self, additional: usize)

Reserves capacity for at least additional more elements.

Source

pub fn shrink_to_fit(&mut self)

Shrinks the capacity to match the current length.

This deallocates unused dynamic segments.

Source

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

Returns an iterator over references to the elements.

Source

pub fn iter_mut(&mut self) -> IterMut<'_, T>

Returns an iterator over mutable references to the elements.

Source

pub fn as_slice(&self) -> SegmentedSlice<'_, T>

Returns an immutable slice view of the entire vector.

§Example
use segmented_vec::SegmentedVec;

let mut vec: SegmentedVec<i32> = SegmentedVec::new();
vec.extend(0..10);

let slice = vec.as_slice();
assert_eq!(slice.len(), 10);
assert_eq!(slice[0], 0);
Source

pub fn as_mut_slice(&mut self) -> SegmentedSliceMut<'_, T>

Returns a mutable slice view of the entire vector.

§Example
use segmented_vec::SegmentedVec;

let mut vec: SegmentedVec<i32> = SegmentedVec::new();
vec.extend(0..10);

let mut slice = vec.as_mut_slice();
slice[0] = 100;
assert_eq!(vec[0], 100);
Source

pub fn slice(&self, range: Range<usize>) -> SegmentedSlice<'_, T>

Returns an immutable slice view of a range of the vector.

§Panics

Panics if the range is out of bounds.

§Example
use segmented_vec::SegmentedVec;

let mut vec: SegmentedVec<i32> = SegmentedVec::new();
vec.extend(0..10);

let slice = vec.slice(2..5);
assert_eq!(slice.len(), 3);
assert_eq!(slice[0], 2);
Source

pub fn slice_mut(&mut self, range: Range<usize>) -> SegmentedSliceMut<'_, T>

Returns a mutable slice view of a range of the vector.

§Panics

Panics if the range is out of bounds.

§Example
use segmented_vec::SegmentedVec;

let mut vec: SegmentedVec<i32> = SegmentedVec::new();
vec.extend(0..10);

let mut slice = vec.slice_mut(2..5);
slice[0] = 100;
assert_eq!(vec[2], 100);
Source

pub fn extend_from_slice(&mut self, other: &[T])
where T: Clone,

Extends the vector by cloning elements from a slice.

Source

pub fn extend_from_copy_slice(&mut self, other: &[T])
where T: Copy,

Extends the vector by copying elements from a slice (for Copy types).

This is more efficient than extend_from_slice for Copy types as it uses bulk memory copy operations.

Source

pub fn sort(&mut self)
where T: Ord,

Sorts the vector in place using a stable sorting algorithm.

This sort is stable (i.e., does not reorder equal elements) and O(n * log(n)) worst-case.

The algorithm is a merge sort adapted from the Rust standard library.

§Examples
use segmented_vec::SegmentedVec;

let mut vec: SegmentedVec<i32> = SegmentedVec::new();
vec.extend([3, 1, 4, 1, 5, 9, 2, 6]);
vec.sort();
assert_eq!(vec.iter().copied().collect::<Vec<_>>(), vec![1, 1, 2, 3, 4, 5, 6, 9]);
Source

pub fn sort_by<F>(&mut self, compare: F)
where F: FnMut(&T, &T) -> Ordering,

Sorts the vector in place with a comparator function.

This sort is stable (i.e., does not reorder equal elements) and O(n * log(n)) worst-case.

The comparator function must define a total ordering for the elements.

§Examples
use segmented_vec::SegmentedVec;

let mut vec: SegmentedVec<i32> = SegmentedVec::new();
vec.extend([3, 1, 4, 1, 5, 9, 2, 6]);
vec.sort_by(|a, b| b.cmp(a)); // reverse order
assert_eq!(vec.iter().copied().collect::<Vec<_>>(), vec![9, 6, 5, 4, 3, 2, 1, 1]);
Source

pub fn sort_by_key<K, F>(&mut self, f: F)
where F: FnMut(&T) -> K, K: Ord,

Sorts the vector in place with a key extraction function.

This sort is stable (i.e., does not reorder equal elements) and O(m * n * log(n)) worst-case, where the key function is O(m).

§Examples
use segmented_vec::SegmentedVec;

let mut vec: SegmentedVec<i32> = SegmentedVec::new();
vec.extend([-3, 1, -4, 1, 5, -9, 2, 6]);
vec.sort_by_key(|k| k.abs());
assert_eq!(vec.iter().copied().collect::<Vec<_>>(), vec![1, 1, 2, -3, -4, 5, 6, -9]);
Source

pub fn sort_unstable(&mut self)
where T: Ord,

Sorts the vector in place using an unstable sorting algorithm.

This sort is unstable (i.e., may reorder equal elements), in-place, and O(n * log(n)) worst-case.

The algorithm is a quicksort with heapsort fallback, adapted from the Rust standard library.

§Examples
use segmented_vec::SegmentedVec;

let mut vec: SegmentedVec<i32> = SegmentedVec::new();
vec.extend([3, 1, 4, 1, 5, 9, 2, 6]);
vec.sort_unstable();
assert_eq!(vec.iter().copied().collect::<Vec<_>>(), vec![1, 1, 2, 3, 4, 5, 6, 9]);
Source

pub fn sort_unstable_by<F>(&mut self, compare: F)
where F: FnMut(&T, &T) -> Ordering,

Sorts the vector in place with a comparator function using an unstable sorting algorithm.

This sort is unstable (i.e., may reorder equal elements), in-place, and O(n * log(n)) worst-case.

The comparator function must define a total ordering for the elements.

§Examples
use segmented_vec::SegmentedVec;

let mut vec: SegmentedVec<i32> = SegmentedVec::new();
vec.extend([3, 1, 4, 1, 5, 9, 2, 6]);
vec.sort_unstable_by(|a, b| b.cmp(a)); // reverse order
assert_eq!(vec.iter().copied().collect::<Vec<_>>(), vec![9, 6, 5, 4, 3, 2, 1, 1]);
Source

pub fn sort_unstable_by_key<K, F>(&mut self, f: F)
where F: FnMut(&T) -> K, K: Ord,

Sorts the vector in place with a key extraction function using an unstable sorting algorithm.

This sort is unstable (i.e., may reorder equal elements), in-place, and O(n * log(n)) worst-case, where the key function is O(m).

§Examples
use segmented_vec::SegmentedVec;

let mut vec: SegmentedVec<i32> = SegmentedVec::new();
vec.extend([-3, 1, -4, 1, 5, -9, 2, 6]);
vec.sort_unstable_by_key(|k| k.abs());
// Note: unstable sort may reorder equal elements, so we just check it's sorted by abs value
let result: Vec<i32> = vec.iter().copied().collect();
for i in 1..result.len() {
    assert!(result[i-1].abs() <= result[i].abs());
}
Source

pub fn is_sorted(&self) -> bool
where T: PartialOrd,

Checks if the elements of this vector are sorted.

Source

pub fn is_sorted_by<F>(&self, compare: F) -> bool
where F: FnMut(&T, &T) -> bool,

Checks if the elements of this vector are sorted using the given comparator function.

Source

pub fn is_sorted_by_key<K, F>(&self, f: F) -> bool
where F: FnMut(&T) -> K, K: PartialOrd,

Checks if the elements of this vector are sorted using the given key extraction function.

Source

pub fn partition_point<P>(&self, pred: P) -> usize
where P: FnMut(&T) -> bool,

Returns the index of the partition point according to the given predicate.

Source

pub fn rotate_left(&mut self, mid: usize)

Rotates the vector in-place such that the first mid elements move to the end.

Source

pub fn rotate_right(&mut self, k: usize)

Rotates the vector in-place such that the last k elements move to the front.

Source

pub fn with_capacity(capacity: usize) -> Self

Creates a new SegmentedVec with at least the specified capacity.

Source

pub fn insert(&mut self, index: usize, element: T)

Inserts an element at position index, shifting all elements after it to the right.

§Panics

Panics if index > len.

Source

pub fn remove(&mut self, index: usize) -> T

Removes and returns the element at position index, shifting all elements after it to the left.

§Panics

Panics if index >= len.

Source

pub fn swap_remove(&mut self, index: usize) -> T

Removes an element from the vector and returns it.

The removed element is replaced by the last element of the vector. This does not preserve ordering, but is O(1).

§Panics

Panics if index >= len.

Source

pub fn retain<F>(&mut self, f: F)
where F: FnMut(&T) -> bool,

Retains only the elements specified by the predicate.

Removes all elements for which f(&element) returns false.

Source

pub fn retain_mut<F>(&mut self, f: F)
where F: FnMut(&mut T) -> bool,

Retains only the elements specified by the predicate, passing a mutable reference.

Source

pub fn dedup(&mut self)
where T: PartialEq,

Removes consecutive duplicate elements.

If the vector is sorted, this removes all duplicates.

Source

pub fn dedup_by<F>(&mut self, same_bucket: F)
where F: FnMut(&mut T, &mut T) -> bool,

Removes consecutive elements that satisfy the given equality relation.

Source

pub fn dedup_by_key<K, F>(&mut self, key: F)
where F: FnMut(&mut T) -> K, K: PartialEq,

Removes consecutive elements that map to the same key.

Source

pub fn resize(&mut self, new_len: usize, value: T)
where T: Clone,

Resizes the vector in-place so that len is equal to new_len.

If new_len is greater than len, the vector is extended by the difference, with each additional slot filled with value. If new_len is less than len, the vector is simply truncated.

Source

pub fn resize_with<F>(&mut self, new_len: usize, f: F)
where F: FnMut() -> T,

Resizes the vector in-place so that len is equal to new_len.

If new_len is greater than len, the vector is extended by the difference, with each additional slot filled with the result of calling the closure f.

Source

pub fn append(&mut self, other: &mut Self)

Moves all the elements of other into self, leaving other empty.

Source

pub fn split_off(&mut self, at: usize) -> Self

Splits the vector into two at the given index.

Returns a newly allocated vector containing the elements in the range [at, len). After the call, the original vector will contain elements [0, at).

§Panics

Panics if at > len.

Source

pub fn chunks(&self, chunk_size: usize) -> Chunks<'_, T>

Returns an iterator over chunk_size elements of the vector at a time.

§Panics

Panics if chunk_size is 0.

Source

pub fn windows(&self, size: usize) -> Windows<'_, T>

Returns an iterator over all contiguous windows of length size.

§Panics

Panics if size is 0.

Source

pub fn rchunks(&self, chunk_size: usize) -> RChunks<'_, T>

Returns an iterator over chunk_size elements at a time, starting from the end.

§Panics

Panics if chunk_size is 0.

Source

pub fn chunks_exact(&self, chunk_size: usize) -> ChunksExact<'_, T>

Returns an iterator over exactly chunk_size elements at a time.

§Panics

Panics if chunk_size is 0.

Source

pub fn drain(&mut self, range: Range<usize>) -> Drain<'_, T>

Creates a draining iterator that removes the specified range and yields the removed items.

§Panics

Panics if the starting point is greater than the end point or if the end point is greater than the length.

Source

pub fn to_vec(&self) -> Vec<T>
where T: Clone,

Copies the elements to a new Vec<T>.

Trait Implementations§

Source§

impl<T: Clone> Clone for SegmentedVec<T>

Source§

fn clone(&self) -> Self

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 SegmentedVec<T>

Source§

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

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

impl<T> Default for SegmentedVec<T>

Source§

fn default() -> Self

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

impl<T> Drop for SegmentedVec<T>

Source§

fn drop(&mut self)

Executes the destructor for this type. Read more
Source§

impl<T> Extend<T> for SegmentedVec<T>

Source§

fn extend<I: IntoIterator<Item = T>>(&mut self, iter: I)

Extends a collection with the contents of an iterator. Read more
Source§

fn extend_one(&mut self, item: A)

🔬This is a nightly-only experimental API. (extend_one)
Extends a collection with exactly one element.
Source§

fn extend_reserve(&mut self, additional: usize)

🔬This is a nightly-only experimental API. (extend_one)
Reserves capacity in a collection for the given number of additional elements. Read more
Source§

impl<T> FromIterator<T> for SegmentedVec<T>

Source§

fn from_iter<I: IntoIterator<Item = T>>(iter: I) -> Self

Creates a value from an iterator. Read more
Source§

impl<T> Index<usize> for SegmentedVec<T>

Source§

type Output = T

The returned type after indexing.
Source§

fn index(&self, index: usize) -> &Self::Output

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

impl<T> IndexMut<usize> for SegmentedVec<T>

Source§

fn index_mut(&mut self, index: usize) -> &mut Self::Output

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

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

Source§

type Item = &'a T

The type of the elements being iterated over.
Source§

type IntoIter = Iter<'a, T>

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

fn into_iter(self) -> Self::IntoIter

Creates an iterator from a value. Read more
Source§

impl<'a, T> IntoIterator for &'a mut SegmentedVec<T>

Source§

type Item = &'a mut T

The type of the elements being iterated over.
Source§

type IntoIter = IterMut<'a, T>

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

fn into_iter(self) -> Self::IntoIter

Creates an iterator from a value. Read more
Source§

impl<T> IntoIterator for SegmentedVec<T>

Source§

type Item = T

The type of the elements being iterated over.
Source§

type IntoIter = IntoIter<T>

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

fn into_iter(self) -> Self::IntoIter

Creates an iterator from a value. Read more
Source§

impl<T: PartialEq> PartialEq for SegmentedVec<T>

Source§

fn eq(&self, other: &Self) -> 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 SegmentedVec<T>

Auto Trait Implementations§

§

impl<T> Freeze for SegmentedVec<T>

§

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

§

impl<T> !Send for SegmentedVec<T>

§

impl<T> !Sync for SegmentedVec<T>

§

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

§

impl<T> UnwindSafe for SegmentedVec<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> 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.