Struct rle_vec::RleVec
[−]
[src]
pub struct RleVec<T> { /* fields omitted */ }The RleVec struct handles like a normal vector and supports a subset from the Vec methods.
Not all methods implemented on Vec are implemented for RleVec. All methods returning a slice
cannot work for RleVec.
Examples:
let mut rle = RleVec::new(); rle.push(10); rle.push(10); rle.push(11); assert_eq!(rle[1], 10); assert_eq!(rle[2], 11); rle.insert(1, 10); assert_eq!(rle.runs_len(), 2); rle.set(0, 1); assert_eq!(rle.runs_len(), 3);
RleVec can be constructed from Iterators and be iterated over just like a Vec.
let v = vec![0,0,0,1,1,1,1,2,2,3,4,5,4,4,4]; let mut rle: RleVec<_> = v.into_iter().collect(); assert_eq!(rle.len(), 15); assert_eq!(rle.runs_len(), 7); assert_eq!(rle.iter().nth(10), Some(&4));
An RleVec can be indexed like a regular vector, but not mutated. Use RleVec::set to change the
value at an index.
let v = vec![0,0,0,1,1,1,1,2,2,3]; let mut rle: RleVec<_> = v.into_iter().collect(); rle.set(1,2); rle.insert(4,4); assert_eq!(rle.iter().cloned().collect::<Vec<_>>(), vec![0,2,0,1,4,1,1,1,2,2,3]);
RleVec::set and RleVec::insert require T: Clone.
Indexing
The RleVec type allows to access values by index, because it implements the
Index trait. An example will be more explicit:
let v = vec![0, 2, 4, 6]; let rle: RleVec<_> = v.into_iter().collect(); println!("{}", rle[1]); // it will display '2'
However be careful: if you try to access an index which isn't in the RleVec,
your software will panic! You cannot do this:
let v = vec![0, 2, 4, 6]; let rle: RleVec<_> = v.into_iter().collect(); println!("{}", v[6]); // it will panic!
In conclusion: always check if the index you want to get really exists before doing it.
Capacity and reallocation
The capacity of an RleVec is the amount of space allocated for any future runs that will be
required for the RleVec. This is not to be confused with the length, which specifies the
number of actual elements that can be indexed from the RleVec. If a a run needs to be
added to the RleVec and the number of runs exceeds its capacity, its capacity will
automatically be increased, but its runs will have to be reallocated.
For example, an RleVec with capacity 10 and length 0 would be an empty vector with space
for 10 more runs. Pushing 10 or fewer consecutively different elements onto the vector will
not change its capacity or cause reallocation to occur. However, if the RleVec's length is
increased to 11, it will have to reallocate, which can be slow. For this reason, if you can
predict the number of runs required in your RleVec, it is recommended to use
RleVec::with_capacity whenever possible to specify how many runs the RleVec is expected
to store.
Methods
impl<T> RleVec<T>[src]
fn new() -> RleVec<T>[src]
Constructs a new empty RleVec<T>.
The rle_vector will not allocate until elements are pushed onto it.
Examples
let rle = RleVec::<i32>::new();
fn with_capacity(capacity: usize) -> RleVec<T>[src]
Constructs a new empty RleVec<T> with capacity for the number of runs.
Choosing this value requires knowledge about the composition of the data that is going to be inserted.
Example
let mut rle = RleVec::with_capacity(10); // The rle_vector contains no items, even though it has capacity for more assert_eq!(rle.len(), 0); // These are all done without reallocating... for i in 0..10 { rle.push(i); } // The rle_vector contains 10 runs and 10 elements too... assert_eq!(rle.len(), 10); assert_eq!(rle.runs_len(), 10); // this definitely won't reallocate the runs rle.push(10); // while this may make the rle_vector reallocate rle.push(11);
fn len(&self) -> usize[src]
Returns the number of elements in the rle_vector.
Example
let mut rle = RleVec::new(); rle.push(1); rle.push(1); rle.push(2); assert_eq!(rle.len(), 3);
fn is_empty(&self) -> bool[src]
Returns true if the rle_vector contains no elements.
Example
let mut rle = RleVec::new(); assert!(rle.is_empty()); rle.push(1); assert!(!rle.is_empty());
fn clear(&mut self)[src]
Clears the vector, removing all values.
Note that this method has no effect on the allocated capacity of the vector.
Examples
let mut rle = RleVec::from(&[1, 1, 1, 1, 2, 2, 3][..]); rle.clear(); assert!(rle.is_empty());
fn last(&self) -> Option<&T>[src]
Returns the last value, or None if it is empty.
Example
let rle = RleVec::from(&[10, 10, 40, 40, 30][..]); assert_eq!(rle.last(), Some(&30)); let rle = RleVec::<i32>::new(); assert_eq!(rle.last(), None);
fn last_run(&self) -> Option<Run<&T>>[src]
Returns the last run, or None if it is empty.
Example
let mut rle = RleVec::new(); assert_eq!(rle.last_run(), None); rle.push(1); rle.push(1); rle.push(1); rle.push(1); assert_eq!(rle.last_run(), Some(Run{ len: 4, value: &1 })); rle.push(2); rle.push(2); rle.push(3); assert_eq!(rle.last_run(), Some(Run{ len: 1, value: &3 }));
fn runs_len(&self) -> usize[src]
Returns the number of runs
Example
let mut rle = RleVec::new(); assert_eq!(rle.runs_len(), 0); rle.push(1); rle.push(1); assert_eq!(rle.runs_len(), 1); rle.push(2); rle.push(3); assert_eq!(rle.runs_len(), 3);
fn starts(&self) -> Vec<usize>[src]
Returns the 0-based start coordinates of the runs
Example
let mut rle = RleVec::new(); rle.push(1); rle.push(1); rle.push(2); rle.push(2); rle.push(3); let starts = rle.starts(); assert_eq!(starts, vec![0, 2, 4]);
fn ends(&self) -> Vec<usize>[src]
Returns the 0-based end coordinates of the runs
fn iter(&self) -> Iter<T>[src]
Returns an iterator over values. Comparable to a Vec iterator.
Example
let mut rle = RleVec::new(); rle.push(1); rle.push(1); rle.push(2); rle.push(3); let mut iterator = rle.iter(); assert_eq!(iterator.next(), Some(&1)); assert_eq!(iterator.next(), Some(&1)); assert_eq!(iterator.next(), Some(&2)); assert_eq!(iterator.next(), Some(&3)); assert_eq!(iterator.next(), None);
fn runs(&self) -> Runs<T>[src]
Returns an iterator that can be used to iterate over the runs.
Example
let mut rle = RleVec::new(); rle.push(1); rle.push(1); rle.push(2); rle.push(3); let mut iterator = rle.runs(); assert_eq!(iterator.next(), Some(Run{ len: 2, value: &1 })); assert_eq!(iterator.next(), Some(Run{ len: 1, value: &2 })); assert_eq!(iterator.next(), Some(Run{ len: 1, value: &3 })); assert_eq!(iterator.next(), None);
impl<T: Eq> RleVec<T>[src]
fn push(&mut self, value: T)[src]
Appends an element to the back of this rle_vector.
Panics
Panics if the number of elements in the vector overflows a usize.
Example
let mut rle = RleVec::new(); rle.push(1); assert_eq!(rle[0], 1);
fn push_n(&mut self, n: usize, value: T)[src]
impl<T: Clone> RleVec<T>[src]
fn to_vec(&self) -> Vec<T>[src]
Construct a Vec<T> from this RleVec.
The values of the RleVec are cloned to produce the final Vec.
This can be usefull for debugging.
Example
let slice = &[0, 0, 0, 1, 1, 99, 9]; let rle = RleVec::from(&slice[..]); let vec = rle.to_vec(); assert_eq!(vec.as_slice(), slice);
impl<T: Eq + Clone> RleVec<T>[src]
fn set(&mut self, index: usize, value: T)[src]
Modify the value at given index.
This can result in the breaking of a run and therefore be an expensive operation. If the value is equal to the value currently present the complexity is O(log n). But if the run needs to be broken the complexity increases to a worst case of O((log n) + n).
Example
let mut rle = RleVec::from(&[1, 1, 1, 1, 2, 2, 3][..]); assert_eq!(rle[2], 1); assert_eq!(rle.len(), 7); assert_eq!(rle.runs_len(), 3); rle.set(2, 3); assert_eq!(rle[2], 3); assert_eq!(rle.len(), 7); assert_eq!(rle.runs_len(), 5);
fn remove(&mut self, index: usize) -> T[src]
Removes and returns the element at position index, shifting all elements after it to the left.
Panics
Panics if index is out of bounds.
Examples
let mut rle = RleVec::from(&[1, 1, 1, 1, 2, 1, 1, 4, 4][..]); assert_eq!(rle.remove(4), 2); assert_eq!(rle.runs_len(), 2); assert_eq!(rle.to_vec(), vec![1, 1, 1, 1, 1, 1, 4, 4]);
fn insert(&mut self, index: usize, value: T)[src]
Insert a value at the given index.
Because the positions of the values after the inserted value need to be changed, the complexity of this function is O((log n) + 2n).
Example
let mut rle = RleVec::from(&[1, 1, 1, 1, 2, 2, 3][..]); assert_eq!(rle[2], 1); assert_eq!(rle.runs_len(), 3); rle.insert(2, 3); assert_eq!(rle[2], 3); assert_eq!(rle.runs_len(), 5);
Trait Implementations
impl<T: Debug> Debug for RleVec<T>[src]
impl<T: Clone> Clone for RleVec<T>[src]
fn clone(&self) -> RleVec<T>[src]
Returns a copy of the value. Read more
fn clone_from(&mut self, source: &Self)1.0.0[src]
Performs copy-assignment from source. Read more
impl<T: Eq> Eq for RleVec<T>[src]
impl<T: PartialEq> PartialEq for RleVec<T>[src]
fn eq(&self, __arg_0: &RleVec<T>) -> bool[src]
This method tests for self and other values to be equal, and is used by ==. Read more
fn ne(&self, __arg_0: &RleVec<T>) -> bool[src]
This method tests for !=.
impl<T: Ord> Ord for RleVec<T>[src]
fn cmp(&self, __arg_0: &RleVec<T>) -> Ordering[src]
This method returns an Ordering between self and other. Read more
fn max(self, other: Self) -> Self1.22.0[src]
Compares and returns the maximum of two values. Read more
fn min(self, other: Self) -> Self1.22.0[src]
Compares and returns the minimum of two values. Read more
impl<T: PartialOrd> PartialOrd for RleVec<T>[src]
fn partial_cmp(&self, __arg_0: &RleVec<T>) -> Option<Ordering>[src]
This method returns an ordering between self and other values if one exists. Read more
fn lt(&self, __arg_0: &RleVec<T>) -> bool[src]
This method tests less than (for self and other) and is used by the < operator. Read more
fn le(&self, __arg_0: &RleVec<T>) -> bool[src]
This method tests less than or equal to (for self and other) and is used by the <= operator. Read more
fn gt(&self, __arg_0: &RleVec<T>) -> bool[src]
This method tests greater than (for self and other) and is used by the > operator. Read more
fn ge(&self, __arg_0: &RleVec<T>) -> bool[src]
This method tests greater than or equal to (for self and other) and is used by the >= operator. Read more
impl<T: Hash> Hash for RleVec<T>[src]
fn hash<__HT: Hasher>(&self, __arg_0: &mut __HT)[src]
Feeds this value into the given [Hasher]. Read more
fn hash_slice<H>(data: &[Self], state: &mut H) where
H: Hasher, 1.3.0[src]
H: Hasher,
Feeds a slice of this type into the given [Hasher]. Read more
impl<T> Index<usize> for RleVec<T>[src]
type Output = T
The returned type after indexing.
fn index(&self, index: usize) -> &T[src]
Performs the indexing (container[index]) operation.
impl<T: Clone> Into<Vec<T>> for RleVec<T>[src]
impl<'a, T: Eq + Clone> From<&'a [T]> for RleVec<T>[src]
impl<T: Eq> FromIterator<T> for RleVec<T>[src]
fn from_iter<I>(iter: I) -> Self where
I: IntoIterator<Item = T>, [src]
I: IntoIterator<Item = T>,
Creates a value from an iterator. Read more
impl<T: Eq> FromIterator<Run<T>> for RleVec<T>[src]
fn from_iter<I>(iter: I) -> Self where
I: IntoIterator<Item = Run<T>>, [src]
I: IntoIterator<Item = Run<T>>,
Creates a value from an iterator. Read more
impl<T> Default for RleVec<T>[src]
impl<T: Eq> Extend<T> for RleVec<T>[src]
fn extend<I>(&mut self, iter: I) where
I: IntoIterator<Item = T>, [src]
I: IntoIterator<Item = T>,
Extends a collection with the contents of an iterator. Read more
impl<T: Eq> Extend<Run<T>> for RleVec<T>[src]
fn extend<I>(&mut self, iter: I) where
I: IntoIterator<Item = Run<T>>, [src]
I: IntoIterator<Item = Run<T>>,
Extends a collection with the contents of an iterator. Read more
impl Write for RleVec<u8>[src]
fn write(&mut self, buf: &[u8]) -> Result<usize>[src]
Write a buffer into this object, returning how many bytes were written. Read more
fn write_all(&mut self, buf: &[u8]) -> Result<()>[src]
Attempts to write an entire buffer into this write. Read more
fn flush(&mut self) -> Result<()>[src]
Flush this output stream, ensuring that all intermediately buffered contents reach their destination. Read more
fn write_fmt(&mut self, fmt: Arguments) -> Result<(), Error>1.0.0[src]
Writes a formatted string into this writer, returning any error encountered. Read more
fn by_ref(&mut self) -> &mut Self1.0.0[src]
Creates a "by reference" adaptor for this instance of Write. Read more