pub struct Vec2d<T>(/* private fields */);
Expand description
Compact representation of a Vec<Vec<T>>
Implementations§
Source§impl<T> Vec2d<T>
impl<T> Vec2d<T>
Sourcepub fn new() -> Self
pub fn new() -> Self
Create a new empty Vec2d
.
This is equivalent to
Self::with_capacity(0, 0)
.
Sourcepub fn with_capacity(vectors: usize, elements_sum: usize) -> Self
pub fn with_capacity(vectors: usize, elements_sum: usize) -> Self
Create a new empty Vec2d
with at least the specified capacities.
A Vec2d
internally contains two Vec
s, one for the elements of all
the inner vectors and one for the indices where these inner vectors
begin. vectors
is the capacity for the latter and elements_sum
is
the capacity for the former Vec
.
Sourcepub fn reserve_vectors(&mut self, additional: usize)
pub fn reserve_vectors(&mut self, additional: usize)
Reserve space for at least additional
more inner vectors. Note that
this will not reserve space for elements of the inner vectors. To that
end, use Self::reserve_elements()
.
This is essentially a wrapper around Vec::reserve()
, so the
documentation there provides more details.
Sourcepub fn reserve_elements(&mut self, additional: usize)
pub fn reserve_elements(&mut self, additional: usize)
Reserve space for at least additional
more elements in the inner
vectors. The space is shared between the inner vectors: After reserving
space for n
additional elements, pushing, e.g., two vectors with k1 + k2 <= n
elements will not lead to a reallocation.
This is essentially a wrapper around Vec::reserve()
, so the
documentation there provides more details.
Sourcepub fn is_empty(&self) -> bool
pub fn is_empty(&self) -> bool
true
iff there are no inner vectors
Equivalent to self.len() == 0
Sourcepub fn get_mut(&mut self, index: usize) -> Option<&mut [T]>
pub fn get_mut(&mut self, index: usize) -> Option<&mut [T]>
Get the vector at index
if there is one
Sourcepub fn push_vec(&mut self)
pub fn push_vec(&mut self)
Add an empty inner vector
Elements can be added to that vector using Self::push_element()
Sourcepub fn pop_vec(&mut self) -> bool
pub fn pop_vec(&mut self) -> bool
Remove the last vector (if there is one)
Returns true iff the outer vector was non-empty before removal.
Sourcepub fn truncate(&mut self, len: usize)
pub fn truncate(&mut self, len: usize)
Truncate the outer vector to len
inner vectors
If len
is greater or equal to self.len()
, this is a
no-op.
Sourcepub fn push_element(&mut self, element: T)
pub fn push_element(&mut self, element: T)
Push an element to the last vector
The vector list must be non-empty.
Sourcepub fn push_elements(&mut self, iter: impl IntoIterator<Item = T>)
pub fn push_elements(&mut self, iter: impl IntoIterator<Item = T>)
Extend the last vector by the elements from iter
There must be at least one inner vector (i.e.,
!self.is_empty()
).
Sourcepub fn all_elements(&self) -> &[T]
pub fn all_elements(&self) -> &[T]
Get a slice containing all elements, ignoring the boundaries of the inner vectors
Sourcepub fn all_elements_mut(&mut self) -> &mut [T]
pub fn all_elements_mut(&mut self) -> &mut [T]
Get a slice containing all elements, ignoring the boundaries of the inner vectors
Source§impl<T: Clone> Vec2d<T>
impl<T: Clone> Vec2d<T>
Sourcepub fn resize_last(&mut self, new_len: usize, value: T)
pub fn resize_last(&mut self, new_len: usize, value: T)
Resize the last vector in-place such that its length is equal to
new_len
If new_len
is greater than the current length, the vector is extended
by the difference, with each additional slot filled with value
. If
new_len
is less than len
, the Vec
is simply truncated.
Trait Implementations§
Source§impl<'a, T: Copy> Extend<&'a [T]> for Vec2d<T>
impl<'a, T: Copy> Extend<&'a [T]> for Vec2d<T>
Source§fn extend<I: IntoIterator<Item = &'a [T]>>(&mut self, iter: I)
fn extend<I: IntoIterator<Item = &'a [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
)