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
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
use crate::memory::{HeapSpace, Vector};
use rate_macros::HeapSpace;
use std::{
ops::{Index, IndexMut},
slice,
};
#[derive(Debug, Clone, HeapSpace, PartialEq, Default)]
pub struct BoundedVector<T>
where
T: HeapSpace,
{
vector: Vector<T>,
}
impl<T: HeapSpace> BoundedVector<T> {
pub fn with_capacity(capacity: usize) -> BoundedVector<T> {
BoundedVector {
vector: Vector::with_capacity(capacity),
}
}
pub fn push(&mut self, value: T) {
self.vector.push_no_grow(value)
}
pub fn len(&self) -> usize {
self.vector.len()
}
pub fn is_empty(&self) -> bool {
self.vector.is_empty()
}
pub fn capacity(&self) -> usize {
self.vector.capacity()
}
pub fn pop(&mut self) -> Option<T> {
self.vector.pop()
}
pub fn first(&self) -> &T {
self.vector.first()
}
pub fn last(&self) -> &T {
self.vector.last()
}
pub fn iter(&self) -> slice::Iter<T> {
self.vector.iter()
}
pub fn as_ptr(&mut self) -> *const T {
self.vector.as_ptr()
}
pub fn mut_ptr(&mut self) -> *mut T {
self.vector.mut_ptr()
}
pub fn truncate(&mut self, new_length: usize) {
self.vector.truncate(new_length)
}
pub fn clear(&mut self) {
self.vector.clear()
}
}
impl<T: HeapSpace + Clone + Default> BoundedVector<T> {
pub fn resize(&mut self, new_length: usize) {
self.vector.resize(new_length)
}
}
impl<T: HeapSpace + Ord> BoundedVector<T> {
pub fn sort_unstable(&mut self) {
self.vector.sort_unstable()
}
}
impl<T: HeapSpace> BoundedVector<T> {
pub fn sort_unstable_by_key<F, K>(&mut self, f: F)
where
F: FnMut(&T) -> K,
K: Ord,
{
self.vector.sort_unstable_by_key(f)
}
}
impl<T: HeapSpace> Index<usize> for BoundedVector<T> {
type Output = T;
fn index(&self, offset: usize) -> &T {
self.vector.index(offset)
}
}
impl<T: HeapSpace> IndexMut<usize> for BoundedVector<T> {
fn index_mut(&mut self, offset: usize) -> &mut T {
self.vector.index_mut(offset)
}
}
impl<'a, T: HeapSpace> IntoIterator for &'a BoundedVector<T> {
type Item = &'a T;
type IntoIter = slice::Iter<'a, T>;
fn into_iter(self) -> Self::IntoIter {
self.vector.iter()
}
}