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>

source

pub fn new() -> Self

Creates a new, empty RepeatVec.

source

pub fn with_capacity(capacity: usize) -> Self

Creates a new, empty RepeatVec with the specified capacity to store physical elements.

source

pub fn len(&self) -> usize

Returns the logical number of elements in this RepeatVec.

source

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

pub fn extend(&mut self, item: T, size: usize)

Extends this RepeatVec by logically adding size copies of item to the end of it.

source

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.

source

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.

source

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

source

pub fn pick_uniform_indexes(&self, indexes: &[impl AsRef<Index>]) -> Vec<usize>

Picks out indexes uniformly randomly from this RepeatVec, using the provided Index instances as sources of randomness.

source

pub fn pick_uniform(&self, indexes: &[impl AsRef<Index>]) -> Vec<(&T, usize)>

Picks out elements uniformly randomly from this RepeatVec, using the provided Index instances as sources of randomness.

Trait Implementations§

source§

impl<T: Clone> Clone for RepeatVec<T>

source§

fn clone(&self) -> RepeatVec<T>

Returns a copy of the value. Read more
1.0.0 · source§

fn clone_from(&mut self, source: &Self)

Performs copy-assignment from source. Read more
source§

impl<T: Debug> Debug for RepeatVec<T>

source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more
source§

impl<T: Default> Default for RepeatVec<T>

source§

fn default() -> RepeatVec<T>

Returns the “default value” for a type. Read more
source§

impl<T: Hash> Hash for RepeatVec<T>

source§

fn hash<__H: Hasher>(&self, state: &mut __H)

Feeds this value into the given Hasher. Read more
1.3.0 · source§

fn hash_slice<H>(data: &[Self], state: &mut H)where H: Hasher, Self: Sized,

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

impl<T: PartialEq> PartialEq<RepeatVec<T>> for RepeatVec<T>

source§

fn eq(&self, other: &RepeatVec<T>) -> bool

This method tests for self and other values to be equal, and is used by ==.
1.0.0 · source§

fn ne(&self, other: &Rhs) -> bool

This method tests for !=. The default implementation is almost always sufficient, and should not be overridden without very good reason.
source§

impl<T: Eq> Eq for RepeatVec<T>

source§

impl<T> StructuralEq for RepeatVec<T>

source§

impl<T> StructuralPartialEq for RepeatVec<T>

Auto Trait Implementations§

§

impl<T> RefUnwindSafe for RepeatVec<T>where T: RefUnwindSafe,

§

impl<T> Send for RepeatVec<T>where T: Send,

§

impl<T> Sync for RepeatVec<T>where T: Sync,

§

impl<T> Unpin for RepeatVec<T>where T: Unpin,

§

impl<T> UnwindSafe for RepeatVec<T>where T: UnwindSafe,

Blanket Implementations§

source§

impl<T> Any for Twhere T: 'static + ?Sized,

source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
source§

impl<T> Borrow<T> for Twhere T: ?Sized,

const: unstable · source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
source§

impl<T> BorrowMut<T> for Twhere T: ?Sized,

const: unstable · source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
source§

impl<T> From<T> for T

const: unstable · source§

fn from(t: T) -> T

Returns the argument unchanged.

source§

impl<T, U> Into<U> for Twhere U: From<T>,

const: unstable · source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

§

impl<T> Pointable for T

§

const ALIGN: usize = mem::align_of::<T>()

The alignment of pointer.
§

type Init = T

The type for initializers.
§

unsafe fn init(init: <T as Pointable>::Init) -> usize

Initializes a with the given initializer. Read more
§

unsafe fn deref<'a>(ptr: usize) -> &'a T

Dereferences the given pointer. Read more
§

unsafe fn deref_mut<'a>(ptr: usize) -> &'a mut T

Mutably dereferences the given pointer. Read more
§

unsafe fn drop(ptr: usize)

Drops the object pointed to by the given pointer. Read more
source§

impl<T> ToOwned for Twhere T: Clone,

§

type Owned = T

The resulting type after obtaining ownership.
source§

fn to_owned(&self) -> T

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

fn clone_into(&self, target: &mut T)

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

impl<T, U> TryFrom<U> for Twhere U: Into<T>,

§

type Error = Infallible

The type returned in the event of a conversion error.
const: unstable · source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
source§

impl<T, U> TryInto<U> for Twhere U: TryFrom<T>,

§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
const: unstable · source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.
§

impl<V, T> VZip<V> for Twhere V: MultiLane<T>,

§

fn vzip(self) -> V