Struct zerovec::flexzerovec::owned::FlexZeroVecOwned
source · pub struct FlexZeroVecOwned(Vec<u8>);
Expand description
The fully-owned variant of FlexZeroVec
. Contains all mutation methods.
Tuple Fields§
§0: Vec<u8>
Implementations§
source§impl FlexZeroVecOwned
impl FlexZeroVecOwned
sourcepub fn new_empty() -> Self
pub fn new_empty() -> Self
Creates a new FlexZeroVecOwned
with zero elements.
sourcepub fn from_slice(other: &FlexZeroSlice) -> FlexZeroVecOwned
pub fn from_slice(other: &FlexZeroSlice) -> FlexZeroVecOwned
Creates a FlexZeroVecOwned
from a FlexZeroSlice
.
sourcepub fn as_slice(&self) -> &FlexZeroSlice
pub fn as_slice(&self) -> &FlexZeroSlice
Obtains this FlexZeroVecOwned
as a FlexZeroSlice
.
sourcepub(crate) fn as_mut_slice(&mut self) -> &mut FlexZeroSlice
pub(crate) fn as_mut_slice(&mut self) -> &mut FlexZeroSlice
Mutably obtains this FlexZeroVecOwned
as a FlexZeroSlice
.
sourcepub fn into_flexzerovec(self) -> FlexZeroVec<'static>
pub fn into_flexzerovec(self) -> FlexZeroVec<'static>
Converts this FlexZeroVecOwned
into a FlexZeroVec::Owned
.
sourcepub fn push(&mut self, item: usize)
pub fn push(&mut self, item: usize)
Appends an item to the end of the vector.
§Panics
Panics if inserting the element would require allocating more than usize::MAX
bytes.
§Examples
use zerovec::vecs::FlexZeroVec;
let mut zv: FlexZeroVec = [22, 44, 66].iter().copied().collect();
zv.to_mut().push(33);
assert_eq!(zv.to_vec(), vec![22, 44, 66, 33]);
sourcepub fn insert(&mut self, index: usize, item: usize)
pub fn insert(&mut self, index: usize, item: usize)
Inserts an element into the middle of the vector.
Caution: Both arguments to this function are of type usize
. Please be careful to pass
the index first followed by the value second.
§Panics
Panics if index > len
.
Panics if inserting the element would require allocating more than usize::MAX
bytes.
§Examples
use zerovec::vecs::FlexZeroVec;
let mut zv: FlexZeroVec = [22, 44, 66].iter().copied().collect();
zv.to_mut().insert(2, 33);
assert_eq!(zv.to_vec(), vec![22, 44, 33, 66]);
sourcepub fn insert_sorted(&mut self, item: usize)
pub fn insert_sorted(&mut self, item: usize)
Inserts an element into an ascending sorted vector at a position that keeps the vector sorted.
§Panics
Panics if inserting the element would require allocating more than usize::MAX
bytes.
§Examples
use zerovec::vecs::FlexZeroVecOwned;
let mut fzv = FlexZeroVecOwned::new_empty();
fzv.insert_sorted(10);
fzv.insert_sorted(5);
fzv.insert_sorted(8);
assert!(Iterator::eq(fzv.iter(), [5, 8, 10].iter().copied()));
sourcepub fn remove(&mut self, index: usize) -> usize
pub fn remove(&mut self, index: usize) -> usize
Removes and returns the element at the specified index.
§Panics
Panics if index >= len
.
§Examples
use zerovec::vecs::FlexZeroVec;
let mut zv: FlexZeroVec = [22, 44, 66].iter().copied().collect();
let removed_item = zv.to_mut().remove(1);
assert_eq!(44, removed_item);
assert_eq!(zv.to_vec(), vec![22, 66]);
sourcepub fn pop_sorted(&mut self) -> usize
pub fn pop_sorted(&mut self) -> usize
Removes and returns the last element from an ascending sorted vector.
If the vector is not sorted, use FlexZeroVecOwned::remove()
instead. Calling this
function would leave the FlexZeroVec in a safe, well-defined state; however, information
may be lost and/or the equality invariant might not hold.
§Panics
Panics if self.is_empty()
.
§Examples
use zerovec::vecs::FlexZeroVec;
let mut zv: FlexZeroVec = [22, 44, 66].iter().copied().collect();
let popped_item = zv.to_mut().pop_sorted();
assert_eq!(66, popped_item);
assert_eq!(zv.to_vec(), vec![22, 44]);
Calling this function on a non-ascending vector could cause surprising results:
use zerovec::vecs::FlexZeroVec;
let mut zv1: FlexZeroVec = [444, 222, 111].iter().copied().collect();
let popped_item = zv1.to_mut().pop_sorted();
assert_eq!(111, popped_item);
// Oops!
assert_eq!(zv1.to_vec(), vec![188, 222]);
Methods from Deref<Target = FlexZeroSlice>§
sourcepub fn as_bytes(&self) -> &[u8] ⓘ
pub fn as_bytes(&self) -> &[u8] ⓘ
Returns this slice as its underlying &[u8]
byte buffer representation.
Useful for serialization.
§Example
use zerovec::vecs::FlexZeroSlice;
let bytes: &[u8] = &[2, 0xD3, 0x00, 0x19, 0x01, 0xA5, 0x01, 0xCD, 0x80];
let fzv = FlexZeroSlice::parse_byte_slice(bytes).expect("valid bytes");
assert_eq!(bytes, fzv.as_bytes());
sourcepub fn as_flexzerovec(&self) -> FlexZeroVec<'_>
pub fn as_flexzerovec(&self) -> FlexZeroVec<'_>
Borrows this FlexZeroSlice
as a FlexZeroVec::Borrowed
.
pub(crate) fn get_width(&self) -> usize
sourcepub fn get(&self, index: usize) -> Option<usize>
pub fn get(&self, index: usize) -> Option<usize>
Gets the element at index
, or None
if index >= self.len()
.
§Examples
use zerovec::vecs::FlexZeroVec;
let fzv: FlexZeroVec = [22, 33].iter().copied().collect();
assert_eq!(fzv.get(0), Some(22));
assert_eq!(fzv.get(1), Some(33));
assert_eq!(fzv.get(2), None);
sourcepub(crate) fn get_chunk(&self, index: usize) -> Option<&[u8]>
pub(crate) fn get_chunk(&self, index: usize) -> Option<&[u8]>
Gets the element at index
as a chunk of bytes, or None
if index >= self.len()
.
sourcepub unsafe fn get_unchecked(&self, index: usize) -> usize
pub unsafe fn get_unchecked(&self, index: usize) -> usize
sourcepub fn first(&self) -> Option<usize>
pub fn first(&self) -> Option<usize>
Gets the first element of the slice, or None
if the slice is empty.
sourcepub fn last(&self) -> Option<usize>
pub fn last(&self) -> Option<usize>
Gets the last element of the slice, or None
if the slice is empty.
sourcepub fn iter(
&self,
) -> impl DoubleEndedIterator<Item = usize> + '_ + ExactSizeIterator<Item = usize>
pub fn iter( &self, ) -> impl DoubleEndedIterator<Item = usize> + '_ + ExactSizeIterator<Item = usize>
Gets an iterator over the elements of the slice as usize
.
sourcepub fn iter_pairs(&self) -> impl Iterator<Item = (usize, Option<usize>)> + '_
pub fn iter_pairs(&self) -> impl Iterator<Item = (usize, Option<usize>)> + '_
Gets an iterator over pairs of elements.
The second element of the final pair is None
.
§Examples
use zerovec::vecs::FlexZeroVec;
let nums: &[usize] = &[211, 281, 421, 461];
let fzv: FlexZeroVec = nums.iter().copied().collect();
let mut pairs_it = fzv.iter_pairs();
assert_eq!(pairs_it.next(), Some((211, Some(281))));
assert_eq!(pairs_it.next(), Some((281, Some(421))));
assert_eq!(pairs_it.next(), Some((421, Some(461))));
assert_eq!(pairs_it.next(), Some((461, None)));
assert_eq!(pairs_it.next(), None);
sourcepub fn to_vec(&self) -> Vec<usize>
pub fn to_vec(&self) -> Vec<usize>
Creates a Vec<usize>
from a FlexZeroSlice
(or FlexZeroVec
).
§Examples
use zerovec::vecs::FlexZeroVec;
let nums: &[usize] = &[211, 281, 421, 461];
let fzv: FlexZeroVec = nums.iter().copied().collect();
let vec: Vec<usize> = fzv.to_vec();
assert_eq!(nums, vec.as_slice());
sourcepub fn binary_search(&self, needle: usize) -> Result<usize, usize>
pub fn binary_search(&self, needle: usize) -> Result<usize, usize>
Binary searches a sorted FlexZeroSlice
for the given usize
value.
§Examples
use zerovec::vecs::FlexZeroVec;
let nums: &[usize] = &[211, 281, 421, 461];
let fzv: FlexZeroVec = nums.iter().copied().collect();
assert_eq!(fzv.binary_search(0), Err(0));
assert_eq!(fzv.binary_search(211), Ok(0));
assert_eq!(fzv.binary_search(250), Err(1));
assert_eq!(fzv.binary_search(281), Ok(1));
assert_eq!(fzv.binary_search(300), Err(2));
assert_eq!(fzv.binary_search(421), Ok(2));
assert_eq!(fzv.binary_search(450), Err(3));
assert_eq!(fzv.binary_search(461), Ok(3));
assert_eq!(fzv.binary_search(462), Err(4));
sourcepub fn binary_search_in_range(
&self,
needle: usize,
range: Range<usize>,
) -> Option<Result<usize, usize>>
pub fn binary_search_in_range( &self, needle: usize, range: Range<usize>, ) -> Option<Result<usize, usize>>
Binary searches a sorted range of a FlexZeroSlice
for the given usize
value.
The indices in the return value are relative to the start of the range.
§Examples
use zerovec::vecs::FlexZeroVec;
// Make a FlexZeroVec with two sorted ranges: 0..3 and 3..5
let nums: &[usize] = &[111, 222, 444, 333, 555];
let fzv: FlexZeroVec = nums.iter().copied().collect();
// Search in the first range:
assert_eq!(fzv.binary_search_in_range(0, 0..3), Some(Err(0)));
assert_eq!(fzv.binary_search_in_range(111, 0..3), Some(Ok(0)));
assert_eq!(fzv.binary_search_in_range(199, 0..3), Some(Err(1)));
assert_eq!(fzv.binary_search_in_range(222, 0..3), Some(Ok(1)));
assert_eq!(fzv.binary_search_in_range(399, 0..3), Some(Err(2)));
assert_eq!(fzv.binary_search_in_range(444, 0..3), Some(Ok(2)));
assert_eq!(fzv.binary_search_in_range(999, 0..3), Some(Err(3)));
// Search in the second range:
assert_eq!(fzv.binary_search_in_range(0, 3..5), Some(Err(0)));
assert_eq!(fzv.binary_search_in_range(333, 3..5), Some(Ok(0)));
assert_eq!(fzv.binary_search_in_range(399, 3..5), Some(Err(1)));
assert_eq!(fzv.binary_search_in_range(555, 3..5), Some(Ok(1)));
assert_eq!(fzv.binary_search_in_range(999, 3..5), Some(Err(2)));
// Out-of-bounds range:
assert_eq!(fzv.binary_search_in_range(0, 4..6), None);
sourcepub fn binary_search_by(
&self,
predicate: impl FnMut(usize) -> Ordering,
) -> Result<usize, usize>
pub fn binary_search_by( &self, predicate: impl FnMut(usize) -> Ordering, ) -> Result<usize, usize>
Binary searches a sorted FlexZeroSlice
according to a predicate function.
sourcepub fn binary_search_in_range_by(
&self,
predicate: impl FnMut(usize) -> Ordering,
range: Range<usize>,
) -> Option<Result<usize, usize>>
pub fn binary_search_in_range_by( &self, predicate: impl FnMut(usize) -> Ordering, range: Range<usize>, ) -> Option<Result<usize, usize>>
Binary searches a sorted range of a FlexZeroSlice
according to a predicate function.
The indices in the return value are relative to the start of the range.
sourcepub fn binary_search_with_index(
&self,
predicate: impl FnMut(usize) -> Ordering,
) -> Result<usize, usize>
pub fn binary_search_with_index( &self, predicate: impl FnMut(usize) -> Ordering, ) -> Result<usize, usize>
Binary searches a FlexZeroSlice
by its indices.
The predicate
function is passed in-bounds indices into the FlexZeroSlice
.
sourcepub fn binary_search_in_range_with_index(
&self,
predicate: impl FnMut(usize) -> Ordering,
range: Range<usize>,
) -> Option<Result<usize, usize>>
pub fn binary_search_in_range_with_index( &self, predicate: impl FnMut(usize) -> Ordering, range: Range<usize>, ) -> Option<Result<usize, usize>>
Binary searches a range of a FlexZeroSlice
by its indices.
The predicate
function is passed in-bounds indices into the FlexZeroSlice
, which are
relative to the start of the entire slice.
The indices in the return value are relative to the start of the range.
sourcefn binary_search_impl(
&self,
predicate: impl FnMut(usize) -> Ordering,
scaled_slice: &[u8],
) -> Result<usize, usize>
fn binary_search_impl( &self, predicate: impl FnMut(usize) -> Ordering, scaled_slice: &[u8], ) -> Result<usize, usize>
§Safety
scaled_slice
must be a subslice of self.data
sourcefn binary_search_with_index_impl(
&self,
predicate: impl FnMut(usize) -> Ordering,
scaled_slice: &[u8],
) -> Result<usize, usize>
fn binary_search_with_index_impl( &self, predicate: impl FnMut(usize) -> Ordering, scaled_slice: &[u8], ) -> Result<usize, usize>
predicate
is passed a valid index as an argument.
§Safety
scaled_slice
must be a subslice of self.data
sourcepub(crate) fn get_insert_info(&self, new_item: usize) -> InsertInfo
pub(crate) fn get_insert_info(&self, new_item: usize) -> InsertInfo
Compute the InsertInfo
for inserting the specified item anywhere into the vector.
§Panics
Panics if inserting the element would require allocating more than usize::MAX
bytes.
sourcepub(crate) fn get_remove_info(&self, remove_index: usize) -> RemoveInfo
pub(crate) fn get_remove_info(&self, remove_index: usize) -> RemoveInfo
Compute the RemoveInfo
for removing the item at the specified index.
sourcepub(crate) fn get_sorted_pop_info(&self) -> RemoveInfo
pub(crate) fn get_sorted_pop_info(&self) -> RemoveInfo
Returns the RemoveInfo
for removing the last element. Should be called
on a slice sorted in ascending order.
This is more efficient than get_remove_info()
because it doesn’t require a
linear traversal of the vector in order to calculate new_width
.
Trait Implementations§
source§impl Clone for FlexZeroVecOwned
impl Clone for FlexZeroVecOwned
source§fn clone(&self) -> FlexZeroVecOwned
fn clone(&self) -> FlexZeroVecOwned
1.0.0 · source§fn clone_from(&mut self, source: &Self)
fn clone_from(&mut self, source: &Self)
source
. Read moresource§impl Debug for FlexZeroVecOwned
impl Debug for FlexZeroVecOwned
source§impl Deref for FlexZeroVecOwned
impl Deref for FlexZeroVecOwned
source§impl From<&FlexZeroSlice> for FlexZeroVecOwned
impl From<&FlexZeroSlice> for FlexZeroVecOwned
source§fn from(other: &FlexZeroSlice) -> Self
fn from(other: &FlexZeroSlice) -> Self
source§impl FromIterator<usize> for FlexZeroVecOwned
impl FromIterator<usize> for FlexZeroVecOwned
source§fn from_iter<I>(iter: I) -> Selfwhere
I: IntoIterator<Item = usize>,
fn from_iter<I>(iter: I) -> Selfwhere
I: IntoIterator<Item = usize>,
Creates a FlexZeroVecOwned
from an iterator of usize
.
source§impl PartialEq for FlexZeroVecOwned
impl PartialEq for FlexZeroVecOwned
source§fn eq(&self, other: &FlexZeroVecOwned) -> bool
fn eq(&self, other: &FlexZeroVecOwned) -> bool
self
and other
values to be equal, and is used
by ==
.