-
Notifications
You must be signed in to change notification settings - Fork 82
/
Copy pathset_from_vec.rs
58 lines (50 loc) · 1.14 KB
/
set_from_vec.rs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
use vstd::prelude::*;
verus! {
struct VecSet {
vt: Vec<u64>,
}
impl VecSet {
pub closed spec fn view(&self) -> Set<u64> {
[email protected]_set()
}
pub fn new() -> (s: Self)
ensures
s@ =~= Set::<u64>::empty(),
{
VecSet { vt: Vec::new() }
}
pub fn insert(&mut self, v: u64)
ensures
self@ =~= old(self)@.insert(v),
{
self.vt.push(v);
proof {
broadcast use vstd::seq_lib::group_seq_properties;
}
assert(self.vt@ =~= old(self).vt@ + seq![v]);
}
pub fn contains(&self, v: u64) -> (contained: bool)
ensures
contained == [email protected](v),
{
for i in iter: 0..self.vt.len()
invariant
forall|j: nat| j < i ==> self.vt[j as int] != v,
{
if self.vt[i] == v {
return true;
}
}
false
}
}
fn main() {
let mut vs: VecSet = VecSet::new();
assert(vs@ =~= set![]);
vs.insert(3);
vs.insert(5);
let contains2 = vs.contains(2);
assert(!contains2);
assert(vs@ =~= set![3, 5]);
}
} // verus!