pub struct SegmentedVec<T> { /* private fields */ }Expand description
A segmented vector with stable pointers.
Implementations§
Source§impl<T> SegmentedVec<T>
impl<T> SegmentedVec<T>
Sourcepub const fn new() -> Self
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());Sourcepub fn pop(&mut self) -> Option<T>
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);Sourcepub fn get(&self, index: usize) -> Option<&T>
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.
Sourcepub fn get_mut(&mut self, index: usize) -> Option<&mut T>
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.
Sourcepub fn first_mut(&mut self) -> Option<&mut T>
pub fn first_mut(&mut self) -> Option<&mut T>
Returns a mutable reference to the first element, or None if empty.
Sourcepub fn last_mut(&mut self) -> Option<&mut T>
pub fn last_mut(&mut self) -> Option<&mut T>
Returns a mutable reference to the last element, or None if empty.
Sourcepub fn contains(&self, x: &T) -> boolwhere
T: PartialEq,
pub fn contains(&self, x: &T) -> boolwhere
T: PartialEq,
Returns true if the slice contains an element with the given value.
Sourcepub fn starts_with(&self, needle: &[T]) -> boolwhere
T: PartialEq,
pub fn starts_with(&self, needle: &[T]) -> boolwhere
T: PartialEq,
Returns true if needle is a prefix of the vector.
Sourcepub fn ends_with(&self, needle: &[T]) -> boolwhere
T: PartialEq,
pub fn ends_with(&self, needle: &[T]) -> boolwhere
T: PartialEq,
Returns true if needle is a suffix of the vector.
Sourcepub fn binary_search(&self, x: &T) -> Result<usize, usize>where
T: Ord,
pub fn binary_search(&self, x: &T) -> Result<usize, usize>where
T: Ord,
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.
Sourcepub fn binary_search_by<F>(&self, f: F) -> Result<usize, usize>
pub fn binary_search_by<F>(&self, f: F) -> Result<usize, usize>
Binary searches this vector with a comparator function.
Sourcepub fn binary_search_by_key<B, F>(&self, b: &B, f: F) -> Result<usize, usize>
pub fn binary_search_by_key<B, F>(&self, b: &B, f: F) -> Result<usize, usize>
Binary searches this vector with a key extraction function.
Sourcepub fn fill_with<F>(&mut self, f: F)where
F: FnMut() -> T,
pub fn fill_with<F>(&mut self, f: F)where
F: FnMut() -> T,
Fills the vector with values produced by a function.
Sourcepub fn clear(&mut self)
pub fn clear(&mut self)
Clears the vector, removing all elements.
This does not deallocate the dynamic segments.
Sourcepub fn truncate(&mut self, new_len: usize)
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.
Sourcepub fn reserve(&mut self, additional: usize)
pub fn reserve(&mut self, additional: usize)
Reserves capacity for at least additional more elements.
Sourcepub fn shrink_to_fit(&mut self)
pub fn shrink_to_fit(&mut self)
Shrinks the capacity to match the current length.
This deallocates unused dynamic segments.
Sourcepub fn iter_mut(&mut self) -> IterMut<'_, T> ⓘ
pub fn iter_mut(&mut self) -> IterMut<'_, T> ⓘ
Returns an iterator over mutable references to the elements.
Sourcepub fn as_slice(&self) -> SegmentedSlice<'_, T>
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);Sourcepub fn as_mut_slice(&mut self) -> SegmentedSliceMut<'_, T>
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);Sourcepub fn slice(&self, range: Range<usize>) -> SegmentedSlice<'_, T>
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);Sourcepub fn slice_mut(&mut self, range: Range<usize>) -> SegmentedSliceMut<'_, T>
pub fn slice_mut(&mut self, range: Range<usize>) -> SegmentedSliceMut<'_, T>
Sourcepub fn extend_from_slice(&mut self, other: &[T])where
T: Clone,
pub fn extend_from_slice(&mut self, other: &[T])where
T: Clone,
Extends the vector by cloning elements from a slice.
Sourcepub fn extend_from_copy_slice(&mut self, other: &[T])where
T: Copy,
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.
Sourcepub fn sort(&mut self)where
T: Ord,
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]);Sourcepub fn sort_by<F>(&mut self, compare: F)
pub fn sort_by<F>(&mut self, compare: F)
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]);Sourcepub fn sort_by_key<K, F>(&mut self, f: F)
pub fn sort_by_key<K, F>(&mut self, f: F)
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]);Sourcepub fn sort_unstable(&mut self)where
T: Ord,
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]);Sourcepub fn sort_unstable_by<F>(&mut self, compare: F)
pub fn sort_unstable_by<F>(&mut self, compare: F)
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]);Sourcepub fn sort_unstable_by_key<K, F>(&mut self, f: F)
pub fn sort_unstable_by_key<K, F>(&mut self, f: F)
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());
}Sourcepub fn is_sorted(&self) -> boolwhere
T: PartialOrd,
pub fn is_sorted(&self) -> boolwhere
T: PartialOrd,
Checks if the elements of this vector are sorted.
Sourcepub fn is_sorted_by<F>(&self, compare: F) -> bool
pub fn is_sorted_by<F>(&self, compare: F) -> bool
Checks if the elements of this vector are sorted using the given comparator function.
Sourcepub fn is_sorted_by_key<K, F>(&self, f: F) -> bool
pub fn is_sorted_by_key<K, F>(&self, f: F) -> bool
Checks if the elements of this vector are sorted using the given key extraction function.
Sourcepub fn partition_point<P>(&self, pred: P) -> usize
pub fn partition_point<P>(&self, pred: P) -> usize
Returns the index of the partition point according to the given predicate.
Sourcepub fn rotate_left(&mut self, mid: usize)
pub fn rotate_left(&mut self, mid: usize)
Rotates the vector in-place such that the first mid elements move to the end.
Sourcepub fn rotate_right(&mut self, k: usize)
pub fn rotate_right(&mut self, k: usize)
Rotates the vector in-place such that the last k elements move to the front.
Sourcepub fn with_capacity(capacity: usize) -> Self
pub fn with_capacity(capacity: usize) -> Self
Creates a new SegmentedVec with at least the specified capacity.
Sourcepub fn insert(&mut self, index: usize, element: T)
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.
Sourcepub fn remove(&mut self, index: usize) -> T
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.
Sourcepub fn swap_remove(&mut self, index: usize) -> T
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.
Sourcepub fn retain<F>(&mut self, f: F)
pub fn retain<F>(&mut self, f: F)
Retains only the elements specified by the predicate.
Removes all elements for which f(&element) returns false.
Sourcepub fn retain_mut<F>(&mut self, f: F)
pub fn retain_mut<F>(&mut self, f: F)
Retains only the elements specified by the predicate, passing a mutable reference.
Sourcepub fn dedup(&mut self)where
T: PartialEq,
pub fn dedup(&mut self)where
T: PartialEq,
Removes consecutive duplicate elements.
If the vector is sorted, this removes all duplicates.
Sourcepub fn dedup_by<F>(&mut self, same_bucket: F)
pub fn dedup_by<F>(&mut self, same_bucket: F)
Removes consecutive elements that satisfy the given equality relation.
Sourcepub fn dedup_by_key<K, F>(&mut self, key: F)
pub fn dedup_by_key<K, F>(&mut self, key: F)
Removes consecutive elements that map to the same key.
Sourcepub fn resize(&mut self, new_len: usize, value: T)where
T: Clone,
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.
Sourcepub fn resize_with<F>(&mut self, new_len: usize, f: F)where
F: FnMut() -> T,
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.
Sourcepub fn append(&mut self, other: &mut Self)
pub fn append(&mut self, other: &mut Self)
Moves all the elements of other into self, leaving other empty.
Sourcepub fn split_off(&mut self, at: usize) -> Self
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.
Sourcepub fn chunks(&self, chunk_size: usize) -> Chunks<'_, T> ⓘ
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.
Sourcepub fn rchunks(&self, chunk_size: usize) -> RChunks<'_, T> ⓘ
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.
Sourcepub fn chunks_exact(&self, chunk_size: usize) -> ChunksExact<'_, T> ⓘ
pub fn chunks_exact(&self, chunk_size: usize) -> ChunksExact<'_, T> ⓘ
Trait Implementations§
Source§impl<T: Clone> Clone for SegmentedVec<T>
impl<T: Clone> Clone for SegmentedVec<T>
Source§impl<T: Debug> Debug for SegmentedVec<T>
impl<T: Debug> Debug for SegmentedVec<T>
Source§impl<T> Default for SegmentedVec<T>
impl<T> Default for SegmentedVec<T>
Source§impl<T> Drop for SegmentedVec<T>
impl<T> Drop for SegmentedVec<T>
Source§impl<T> Extend<T> for SegmentedVec<T>
impl<T> Extend<T> for SegmentedVec<T>
Source§fn extend<I: IntoIterator<Item = T>>(&mut self, iter: I)
fn extend<I: IntoIterator<Item = T>>(&mut self, iter: I)
Source§fn extend_one(&mut self, item: A)
fn extend_one(&mut self, item: A)
extend_one)Source§fn extend_reserve(&mut self, additional: usize)
fn extend_reserve(&mut self, additional: usize)
extend_one)