Struct segvec::SegVec [−][src]
pub struct SegVec<T> { /* fields omitted */ }Expand description
A data structure similar to Vec, but that does not copy on re-size and can
release memory when it is truncated.
- Capacity is allocated in “segments”.
- For a
SegVecwith one element, a segment of length 1 is allocated. - For a
SegVecwith 2 elements, two segments of length 1 are allocated. - For a
SegVecwith 3 or 4 elements, two segments of length one, and a segment of length 2 are allocated. - Each allocated segment is the size of the entire capacity prior to the allocation of that
segment.
In other words, each segment allocated doubles the capacity of the
SegVec. As a consequence, aSegVecalways has a capacity that is a power of 2, with the special case of the emptySegVecwith a capacity of 0.
Implementations
Create a new SegVec with a length and capacity of 0
let v: SegVec<i32> = SegVec::new(); assert_eq!(v.capacity(), 0);
The number of elements in the SegVec
let mut v: SegVec<i32> = SegVec::new(); v.push(1); v.push(2); assert_eq!(v.len(), 2);
The capacity of the SegVec
let mut v: SegVec<i32> = SegVec::with_capacity(3); assert_eq!(v.capacity(), 4);
Reserve enough capacity to insert the given number of elements into the
SegVec without allocating. If the capacity is already sufficient,
nothing happens.
let mut v: SegVec<i32> = SegVec::new(); assert_eq!(v.capacity(), 0); v.reserve(3); assert_eq!(v.capacity(), 4);
Panics
- If the required capacity overflows
usize
Returns a reference to the data at the given index in the SegVec, if it
exists.
let mut v: SegVec<i32> = SegVec::new(); assert_eq!(v.get(0), None); v.push(1); assert_eq!(*v.get(0).unwrap(), 1);
Returns a mutable reference to the data at the given index in the SegVec,
if it exists.
let mut v: SegVec<i32> = SegVec::new(); assert_eq!(v.get_mut(0), None); v.push(1); assert_eq!(*v.get_mut(0).unwrap(), 1);
Removes the last value from the SegVec and returns it, or returns None
if it is empty.
let mut v: SegVec<i32> = SegVec::new(); v.push(1); assert_eq!(v[0], 1);
Truncates the SegVec to the given length.
If the given length is larger than the current length, this is a no-op.
Otherwise, the capacity is reduced and any excess elements are dropped.
let mut v: SegVec<i32> = SegVec::new(); v.push(1); v.push(2); v.push(3); assert_eq!(v.len(), 3); assert_eq!(v.capacity(), 4); v.truncate(1); assert_eq!(v.len(), 1); assert_eq!(v.capacity(), 1);
Returns an iterator over immutable references to the elements in the
SegVec.
let mut v: SegVec<i32> = SegVec::new(); v.push(1); v.push(2); v.push(3); let mut i = v.iter(); assert_eq!(*i.next().unwrap(), 1); assert_eq!(*i.next().unwrap(), 2); assert_eq!(*i.next().unwrap(), 3); assert_eq!(i.next(), None);
Insert the given value at the given index in the SegVec.
This operation requires O(N) time due to the fact that the data is segmented -
the new element is pushed onto the end and then shifted backwards into position.
let mut v: SegVec<i32> = SegVec::new(); v.push(1); v.push(2); v.insert(0, 100); assert_eq!(v[0], 100);
Panics
- If the given index is greater than
self.len() - If the required capacity overflows
usize
Removes the value at the given index in the SegVec and returns it.
This operation requires O(N) time due to the fact that the data is segmented -
the element is shifted to the end and then popped.
let mut v: SegVec<i32> = SegVec::new(); v.push(1); v.push(2); assert_eq!(v.remove(1), 2); assert_eq!(v.len(), 1);
Panics
- If the given index is greater than or equal to
self.len()
Returns an iterator that removes and returns values from within the given range of the
SegVec. See Drain for more information.
let mut v: SegVec<i32> = SegVec::new(); v.push(1); v.push(2); v.drain(..).for_each(|v| println!("{}", v)); assert_eq!(v.len(), 0);
Panics
- If the end index is greater than
self.len() - If the start index is greater than the end index.
Reverses the elements in the SegVec.
let mut v: SegVec<i32> = SegVec::new(); v.push(1); v.push(2); v.push(3); v.push(4); v.push(5); v.push(6); v.reverse(); assert_eq!(v.into_iter().collect::<Vec<_>>(), vec![6, 5, 4, 3, 2, 1]);
Sort the SegVec in ascending order (unstable)
Trait Implementations
Extends a collection with the contents of an iterator. Read more
extend_one)Extends a collection with exactly one element.
extend_one)Reserves capacity in a collection for the given number of additional elements. Read more
Creates a value from an iterator. Read more
Auto Trait Implementations
impl<T> RefUnwindSafe for SegVec<T> where
T: RefUnwindSafe, impl<T> UnwindSafe for SegVec<T> where
T: UnwindSafe, Blanket Implementations
Mutably borrows from an owned value. Read more