diem_proptest_helpers/growing_subset.rs
1// Copyright (c) The Diem Core Contributors
2// SPDX-License-Identifier: Apache-2.0
3
4// Copyright 2021 Conflux Foundation. All rights reserved.
5// Conflux is free software and distributed under GNU General Public License.
6// See http://www.gnu.org/licenses/
7
8use proptest::sample::Index;
9use std::iter::FromIterator;
10
11/// A set of elements, each with an associated key, that grows over time.
12///
13/// This is called `GrowingSubset` because the universal set the subset grows
14/// from is provided upfront. At any time, the items included form the *current
15/// subset*.
16///
17/// `GrowingSubset` integrates with `proptest` through the
18/// [`pick_item`][GrowingSubset::pick_item] and [`pick_value`][GrowingSubset::
19/// pick_value] methods.
20///
21/// # Examples
22///
23/// ```
24/// use diem_proptest_helpers::GrowingSubset;
25/// let items = vec![(1, "a"), (3, "c"), (2, "b"), (2, "d")];
26/// let mut subset: GrowingSubset<_, _> = items.into_iter().collect();
27///
28/// assert_eq!(subset.total_len(), 4);
29/// assert_eq!(subset.len(), 0);
30/// assert_eq!(subset.current(), &[]);
31///
32/// subset.advance_to(&2);
33/// assert_eq!(subset.len(), 1);
34/// assert_eq!(subset.current(), &[(1, "a")]);
35///
36/// subset.advance_to(&4);
37/// assert_eq!(subset.len(), 4);
38/// assert_eq!(subset.current(), &[(1, "a"), (2, "b"), (2, "d"), (3, "c")]);
39///
40/// subset.advance_to(&5);
41/// assert_eq!(subset.len(), 4);
42/// assert_eq!(subset.current(), &[(1, "a"), (2, "b"), (2, "d"), (3, "c")]);
43/// ```
44#[derive(Clone, Debug)]
45pub struct GrowingSubset<Ix, T> {
46 // `items` needs to be in ascending order by index -- constructors of
47 // `GrowingSubset` should sort the list of items.
48 items: Vec<(Ix, T)>,
49 current_pos: usize,
50}
51
52/// Constructs a `GrowingSubset` from an iterator of (index, value) pairs.
53///
54/// The input does not need to be pre-sorted.
55impl<Ix, T> FromIterator<(Ix, T)> for GrowingSubset<Ix, T>
56where Ix: Ord
57{
58 fn from_iter<I>(iter: I) -> Self
59 where I: IntoIterator<Item = (Ix, T)> {
60 let mut items: Vec<_> = iter.into_iter().collect();
61 // Sorting is required to construct a subset correctly.
62 items.sort_by(|x, y| x.0.cmp(&y.0));
63 Self {
64 items,
65 current_pos: 0,
66 }
67 }
68}
69
70impl<Ix, T> GrowingSubset<Ix, T>
71where Ix: Ord
72{
73 /// Returns the number of elements in the *current subset*.
74 ///
75 /// See [`total_len`](GrowingSubset::total_len) for the length of the
76 /// universal set.
77 pub fn len(&self) -> usize { self.current_pos }
78
79 /// Returns `true` if the *current subset* contains no elements.
80 pub fn is_empty(&self) -> bool { self.len() == 0 }
81
82 /// Returns the total number of elements in the universal set.
83 ///
84 /// This remains fixed once the `GrowingSubset` has been created.
85 pub fn total_len(&self) -> usize { self.items.len() }
86
87 /// Returns a slice containing the items in the *current subset*.
88 pub fn current(&self) -> &[(Ix, T)] { &self.items[0..self.current_pos] }
89
90 /// Chooses an (index, value) pair from the *current subset* using the
91 /// provided [`Index`](proptest::sample::Index) instance as the source
92 /// of randomness.
93 pub fn pick_item(&self, index: &Index) -> &(Ix, T) {
94 index.get(self.current())
95 }
96
97 /// Chooses a value from the *current subset* using the provided
98 /// [`Index`](proptest::sample::Index) instance as the source of randomness.
99 pub fn pick_value(&self, index: &Index) -> &T { &self.pick_item(index).1 }
100
101 /// Advances the valid subset to the provided index. After the end of this,
102 /// the *current subset* will contain all elements where the index is
103 /// *less than* `to_idx`.
104 ///
105 /// If duplicate indexes exist, `advance_to` will cause all of the
106 /// corresponding items to be included.
107 ///
108 /// It is expected that `advance_to` will be called with larger indexes over
109 /// time.
110 pub fn advance_to(&mut self, to_idx: &Ix) {
111 let len = self.items.len();
112 while self.current_pos < len {
113 let (idx, _) = &self.items[self.current_pos];
114 if idx >= to_idx {
115 break;
116 }
117 self.current_pos += 1;
118 }
119 }
120}