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 SegVec with one element, a segment of length 1 is allocated.
  • For a SegVec with 2 elements, two segments of length 1 are allocated.
  • For a SegVec with 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, a SegVec always has a capacity that is a power of 2, with the special case of the empty SegVec with 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);

Create a new SegVec with a length of 0 and a capacity large enough to hold the given number of elements.

let v: SegVec<i32> = SegVec::with_capacity(5);
assert_eq!(v.capacity(), 8);

Panics

  • If the required capacity overflows usize

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);

Pushes a new value onto the end of the SegVec, resizing if necessary.

let mut v: SegVec<i32> = SegVec::new();
v.push(1);
assert_eq!(v[0], 1);

Panics

  • If the required capacity overflows usize

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.

Returns a Slice over the given range in the SegVec.

let mut v: SegVec<i32> = SegVec::new();
v.push(1);
v.push(2);
let s = v.slice(1..);
assert_eq!(s[0], 2);

Panics

  • If the end index is greater than self.len()
  • If the start index is greater than the end index.

Returns a SliceMut over the given range in the SegVec.

let mut v: SegVec<i32> = SegVec::new();
v.push(1);
v.push(2);
let mut s = v.slice_mut(..1);
s[0] = 100;
assert_eq!(v[0], 100);

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)

Sort the SegVec in ascending order (unstable) using the given comparison function

Trait Implementations

Returns a copy of the value. Read more

Performs copy-assignment from source. Read more

Formats the value using the given formatter. Read more

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

🔬 This is a nightly-only experimental API. (extend_one)

Extends a collection with exactly one element.

🔬 This is a nightly-only experimental API. (extend_one)

Reserves capacity in a collection for the given number of additional elements. Read more

Creates a value from an iterator. Read more

Feeds this value into the given Hasher. Read more

Feeds a slice of this type into the given Hasher. Read more

The returned type after indexing.

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

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

Which kind of iterator are we turning this into?

The type of the elements being iterated over.

Creates an iterator from a value. Read more

This method tests for self and other values to be equal, and is used by ==. Read more

This method tests for !=.

Auto Trait Implementations

Blanket Implementations

Gets the TypeId of self. Read more

Immutably borrows from an owned value. Read more

Mutably borrows from an owned value. Read more

Performs the conversion.

Performs the conversion.

The resulting type after obtaining ownership.

Creates owned data from borrowed data, usually by cloning. Read more

🔬 This is a nightly-only experimental API. (toowned_clone_into)

recently added

Uses borrowed data to replace owned data, usually by cloning. Read more

The type returned in the event of a conversion error.

Performs the conversion.

The type returned in the event of a conversion error.

Performs the conversion.