Struct diem_proptest_helpers::RepeatVec
source · pub struct RepeatVec<T> { /* private fields */ }
Expand description
An efficient representation of a vector with repeated elements inserted.
Internally, this data structure stores one copy of each inserted element, along with data about how many times each element is repeated.
This data structure does not do any sort of deduplication, so it isn’t any sort of set (or multiset).
This is useful for presenting a large logical vector for picking proptest
indexes from.
Examples
use diem_proptest_helpers::RepeatVec;
let mut repeat_vec = RepeatVec::new();
repeat_vec.extend("a", 10); // logically, insert "a" 10 times
repeat_vec.extend("b", 20); // logically, insert "b" 20 times
assert_eq!(repeat_vec.get(0), Some((&"a", 0))); // returns the "a" at logical position 0
assert_eq!(repeat_vec.get(5), Some((&"a", 5))); // returns the "a" at logical position 5
assert_eq!(repeat_vec.get(10), Some((&"b", 0))); // returns the "b" (offset 0) at logical position 10
assert_eq!(repeat_vec.get(20), Some((&"b", 10))); // returns the "b" (offset 10) at logical position 20
assert_eq!(repeat_vec.get(30), None); // past the end of the logical array
The data structure doesn’t care about whether the inserted items are equal or not.
use diem_proptest_helpers::RepeatVec;
let mut repeat_vec = RepeatVec::new();
repeat_vec.extend("a", 10); // logically, insert "a" 10 times
repeat_vec.extend("a", 20); // logically, insert "a" 20 times
assert_eq!(repeat_vec.get(0), Some((&"a", 0)));
assert_eq!(repeat_vec.get(5), Some((&"a", 5)));
assert_eq!(repeat_vec.get(10), Some((&"a", 0))); // This refers to the second "a".
Implementations§
source§impl<T> RepeatVec<T>
impl<T> RepeatVec<T>
sourcepub fn with_capacity(capacity: usize) -> Self
pub fn with_capacity(capacity: usize) -> Self
Creates a new, empty RepeatVec
with the specified capacity to store physical elements.
sourcepub fn is_empty(&self) -> bool
pub fn is_empty(&self) -> bool
Returns true
if this RepeatVec
has no logical elements.
Examples
use diem_proptest_helpers::RepeatVec;
let mut repeat_vec = RepeatVec::new();
// There are no elements in this RepeatVec.
assert!(repeat_vec.is_empty());
// Adding 0 logical copies of an element still means it's empty.
repeat_vec.extend("a", 0);
assert!(repeat_vec.is_empty());
// Adding non-zero logical copies makes this vector not empty.
repeat_vec.extend("b", 1);
assert!(!repeat_vec.is_empty());
sourcepub fn extend(&mut self, item: T, size: usize)
pub fn extend(&mut self, item: T, size: usize)
Extends this RepeatVec
by logically adding size
copies of item
to the end of it.
sourcepub fn remove(&mut self, index: usize)
pub fn remove(&mut self, index: usize)
Removes the item specified by the given logical index, shifting all elements after it to the left by updating start positions.
Out of bounds indexes have no effect.
sourcepub fn remove_all(&mut self, logical_indexes: impl IntoIterator<Item = usize>)
pub fn remove_all(&mut self, logical_indexes: impl IntoIterator<Item = usize>)
Removes the items specified by the given logical indexes, shifting all elements after them to the left by updating start positions.
Ignores any out of bounds indexes.
sourcepub fn get(&self, at: usize) -> Option<(&T, usize)>
pub fn get(&self, at: usize) -> Option<(&T, usize)>
Returns the item at location at
. The return value is a reference to the stored item, plus
the offset from the start (logically, which copy of the item is being returned).