1use vstd::prelude::*;
8
9use crate::connectives::accumulator::Accumulator as AccumulatorCarrier;
10use crate::connectives::buffer::Buffer as BufferCarrier;
11use crate::connectives::counter::Counter as CounterCarrier;
12use crate::connectives::marker::Marker as MarkerCarrier;
13use crate::value_eq::ValueEq;
14
15verus! {
16
17pub struct Accumulator<T: Copy> {
33 inner: AccumulatorCarrier<T>,
34}
35
36impl<T: Copy> Accumulator<T> {
37 pub closed spec fn well_formed(&self) -> bool {
39 self.inner.well_formed()
40 }
41
42 pub closed spec fn complete(&self) -> bool {
44 self.inner.pending@.len() == 0
45 }
46
47 pub closed spec fn total_len(&self) -> nat {
49 self.inner.accumulated@.len() + self.inner.pending@.len()
50 }
51
52 pub fn new(values: Vec<T>) -> (accumulator: Self)
54 ensures accumulator.well_formed(),
55 {
56 Self { inner: AccumulatorCarrier::new(values) }
57 }
58
59 pub fn from_accumulated(values: Vec<T>) -> (accumulator: Self)
61 ensures
62 accumulator.well_formed(),
63 accumulator.complete(),
64 {
65 Self { inner: AccumulatorCarrier::from_accumulated(values) }
66 }
67
68 pub fn checked_len(&self) -> Option<usize> {
70 self.inner
71 .accumulated_len()
72 .checked_add(self.inner.pending_len())
73 }
74
75 pub fn is_empty(&self) -> bool {
77 self.inner.accumulated_len() == 0 && self.inner.pending_len() == 0
78 }
79
80 pub fn accumulated_len(&self) -> usize { self.inner.accumulated_len() }
82
83 pub fn pending_len(&self) -> usize { self.inner.pending_len() }
85
86 pub fn is_complete(&self) -> (complete: bool)
88 ensures complete == self.complete(),
89 {
90 self.inner.is_complete()
91 }
92
93 pub fn accumulated(&self, index: usize) -> Option<T> { self.inner.accumulated(index) }
95
96 pub fn pending(&self, index: usize) -> Option<T> { self.inner.pending(index) }
98
99 pub fn advance(&mut self) -> (value: Option<T>)
101 requires old(self).well_formed(),
102 ensures final(self).well_formed(),
103 {
104 self.inner.advance()
105 }
106
107 pub fn try_append(&mut self, value: T) -> (result: Result<(), T>)
113 requires old(self).well_formed(),
114 ensures final(self).well_formed(),
115 {
116 if !self.inner.is_complete() { return Err(value); }
117 self.inner.append(value);
118 Ok(())
119 }
120}
121
122pub struct Buffer<T> {
139 inner: BufferCarrier<T>,
140}
141
142impl<T> Buffer<T> {
143 pub closed spec fn retained(&self) -> Seq<T> {
145 self.inner.values@
146 }
147
148 pub closed spec fn admitted_capacity(&self) -> nat {
150 self.inner.capacity as nat
151 }
152
153 pub closed spec fn contains_retained(&self, value: T) -> bool {
155 crate::connectives::buffer::contains_value(self.inner.values@, value)
156 }
157
158 pub closed spec fn well_formed(&self) -> bool {
160 self.inner.well_formed()
161 }
162
163 pub closed spec fn distinct(&self) -> bool {
165 crate::connectives::buffer::all_distinct(self.inner.values@)
166 }
167
168 pub fn new(capacity: usize) -> (buffer: Self)
170 ensures
171 buffer.well_formed(),
172 buffer.distinct(),
173 buffer.admitted_capacity() == capacity as nat,
174 buffer.retained() == Seq::<T>::empty(),
175 forall|value: T| !buffer.contains_retained(value),
176 {
177 Self { inner: BufferCarrier::new(capacity) }
178 }
179
180 pub fn capacity(&self) -> (capacity: usize)
182 ensures capacity as nat == self.admitted_capacity(),
183 {
184 self.inner.capacity()
185 }
186
187 pub fn len(&self) -> (length: usize)
189 ensures length as nat == self.retained().len(),
190 {
191 self.inner.len()
192 }
193
194 pub fn is_empty(&self) -> (empty: bool)
196 ensures empty == (self.retained().len() == 0),
197 {
198 self.inner.is_empty()
199 }
200
201 pub fn is_full(&self) -> (full: bool)
203 ensures full == (self.retained().len() == self.admitted_capacity()),
204 {
205 self.inner.is_full()
206 }
207
208 pub fn push(&mut self, value: T) -> (result: Result<(), T>)
214 requires old(self).well_formed(),
215 ensures
216 final(self).well_formed(),
217 final(self).admitted_capacity() == old(self).admitted_capacity(),
218 old(self).retained().len() < old(self).admitted_capacity() ==>
219 final(self).retained() == old(self).retained().push(value),
220 old(self).retained().len() >= old(self).admitted_capacity() ==>
221 final(self).retained() == old(self).retained(),
222 {
223 self.inner.push(value)
224 }
225
226 pub fn pop(&mut self) -> (value: Option<T>)
228 requires old(self).well_formed(),
229 ensures
230 final(self).well_formed(),
231 final(self).admitted_capacity() == old(self).admitted_capacity(),
232 old(self).retained().len() == 0 ==>
233 final(self).retained() == old(self).retained(),
234 old(self).retained().len() > 0 ==>
235 final(self).retained() == old(self).retained().skip(1),
236 old(self).distinct() ==> final(self).distinct(),
237 {
238 self.inner.pop()
239 }
240}
241
242impl<T: ValueEq + Copy> Buffer<T> {
243 pub fn contains(&self, value: T) -> (present: bool)
245 ensures present == self.contains_retained(value),
246 {
247 self.inner.contains(value)
248 }
249
250 #[must_use]
252 pub fn push_unique(&mut self, value: T) -> (accepted: bool)
253 requires old(self).well_formed(), old(self).distinct(),
254 ensures
255 final(self).well_formed(),
256 final(self).distinct(),
257 final(self).admitted_capacity() == old(self).admitted_capacity(),
258 accepted == (old(self).retained().len() < old(self).admitted_capacity()
259 && !old(self).contains_retained(value)),
260 accepted ==> final(self).retained() == old(self).retained().push(value),
261 !accepted ==> final(self).retained() == old(self).retained(),
262 {
263 self.inner.push_unique(value)
264 }
265
266 #[must_use]
268 pub fn remove(&mut self, value: T) -> (removed: bool)
269 requires old(self).well_formed(), old(self).distinct(),
270 ensures
271 final(self).well_formed(),
272 final(self).distinct(),
273 final(self).admitted_capacity() == old(self).admitted_capacity(),
274 removed == old(self).contains_retained(value),
275 forall|candidate: T| #[trigger] final(self).contains_retained(candidate)
276 == (old(self).contains_retained(candidate) && candidate != value),
277 {
278 self.inner.remove_value(value)
279 }
280}
281
282#[derive(Clone, Copy, Debug, Default, Eq, PartialEq)]
294pub struct Counter {
295 inner: CounterCarrier,
296}
297
298impl Counter {
299 pub fn new(value: u64) -> Self { Self { inner: CounterCarrier::new(value) } }
301
302 pub fn value(&self) -> u64 { self.inner.value() }
304
305 #[must_use]
307 pub fn try_increment(&mut self) -> bool { self.inner.try_increment() }
308
309 #[must_use]
311 pub fn try_decrement(&mut self) -> bool { self.inner.try_decrement() }
312}
313
314#[derive(Clone, Copy, Debug, Default, Eq, PartialEq)]
327pub struct Marker {
328 inner: MarkerCarrier,
329}
330
331impl Marker {
332 pub fn new(marked: bool) -> Self { Self { inner: MarkerCarrier::new(marked) } }
334
335 pub fn is_marked(&self) -> bool { self.inner.is_marked() }
337
338 #[must_use]
340 pub fn set(&mut self) -> bool { self.inner.set() }
341
342 #[must_use]
344 pub fn clear(&mut self) -> bool { self.inner.clear() }
345}
346
347pub fn projection_consistent(projected: bool, source: bool) -> (consistent: bool)
358 ensures consistent == crate::connectives::projection::membership_consistent(projected, source),
359{
360 projected == source
361}
362
363pub fn strictly_before(left: usize, right: usize) -> (ordered: bool) {
374 crate::connectives::ordering_pass::is_strictly_before(left, right)
375}
376
377}
378
379impl<T: Copy> Accumulator<T> {
380 pub fn accumulated_iter(&self) -> impl ExactSizeIterator<Item = &T> {
382 self.inner.accumulated.iter()
383 }
384
385 pub fn pending_iter(&self) -> impl ExactSizeIterator<Item = &T> {
387 self.inner.pending.iter()
388 }
389
390 pub fn iter(&self) -> impl Iterator<Item = &T> {
392 self.inner
393 .accumulated
394 .iter()
395 .chain(self.inner.pending.iter())
396 }
397}
398
399impl<T: Copy> Default for Accumulator<T> {
400 fn default() -> Self {
401 Self::new(Vec::new())
402 }
403}
404
405impl<T: Copy + core::fmt::Debug> core::fmt::Debug for Accumulator<T> {
406 fn fmt(&self, formatter: &mut core::fmt::Formatter<'_>) -> core::fmt::Result {
407 formatter
408 .debug_struct("Accumulator")
409 .field("accumulated", &self.inner.accumulated)
410 .field("pending", &self.inner.pending)
411 .finish()
412 }
413}
414
415impl<T> Buffer<T> {
416 pub fn get(&self, index: usize) -> Option<&T> {
418 self.inner.values.get(index)
419 }
420
421 pub fn as_slice(&self) -> &[T] {
423 self.inner.values.as_slice()
424 }
425
426 pub fn iter(&self) -> core::slice::Iter<'_, T> {
428 self.inner.values.iter()
429 }
430}
431
432impl<T> Default for Buffer<T> {
433 fn default() -> Self {
434 Self::new(0)
435 }
436}
437
438impl<T: Clone> Clone for Buffer<T> {
439 fn clone(&self) -> Self {
440 Self {
441 inner: BufferCarrier {
442 capacity: self.inner.capacity,
443 values: self.inner.values.clone(),
444 },
445 }
446 }
447}
448
449impl<T: PartialEq> PartialEq for Buffer<T> {
450 fn eq(&self, other: &Self) -> bool {
451 self.inner.capacity == other.inner.capacity && self.inner.values == other.inner.values
452 }
453}
454
455impl<T: Eq> Eq for Buffer<T> {}
456
457impl<T: core::fmt::Debug> core::fmt::Debug for Buffer<T> {
458 fn fmt(&self, formatter: &mut core::fmt::Formatter<'_>) -> core::fmt::Result {
459 formatter
460 .debug_struct("Buffer")
461 .field("capacity", &self.inner.capacity)
462 .field("values", &self.inner.values)
463 .finish()
464 }
465}
466
467impl<T> AsRef<[T]> for Buffer<T> {
468 fn as_ref(&self) -> &[T] {
469 self.as_slice()
470 }
471}
472
473impl<'a, T> IntoIterator for &'a Buffer<T> {
474 type Item = &'a T;
475 type IntoIter = core::slice::Iter<'a, T>;
476
477 fn into_iter(self) -> Self::IntoIter {
478 self.iter()
479 }
480}
481
482impl<T> IntoIterator for Buffer<T> {
483 type Item = T;
484 type IntoIter = std::vec::IntoIter<T>;
485
486 fn into_iter(self) -> Self::IntoIter {
487 self.inner.values.into_iter()
488 }
489}
490
491impl From<u64> for Counter {
492 fn from(value: u64) -> Self {
493 Self::new(value)
494 }
495}
496
497impl From<Counter> for u64 {
498 fn from(counter: Counter) -> Self {
499 counter.value()
500 }
501}
502
503impl From<bool> for Marker {
504 fn from(marked: bool) -> Self {
505 Self::new(marked)
506 }
507}
508
509impl From<Marker> for bool {
510 fn from(marker: Marker) -> Self {
511 marker.is_marked()
512 }
513}