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
use crate::{
memory::{assert_in_bounds, Vector},
output::unreachable,
};
use std::{
iter::FromIterator,
ops::{Index, IndexMut},
};
#[derive(Debug, PartialEq, Clone)]
pub enum SmallVector<T> {
Empty,
One(T),
Many(Vector<T>),
}
impl<T> SmallVector<T> {
pub fn len(&self) -> usize {
match self {
SmallVector::Empty => 0,
SmallVector::One(_value) => 1,
SmallVector::Many(vector) => vector.len(),
}
}
pub fn is_empty(&self) -> bool {
match &self {
SmallVector::Empty => true,
SmallVector::One(_value) => false,
SmallVector::Many(vector) => vector.is_empty(),
}
}
}
impl<T: Copy + Default> Default for SmallVector<T> {
fn default() -> SmallVector<T> {
SmallVector::Empty
}
}
impl<T: Copy + Default> SmallVector<T> {
#[allow(dead_code)]
pub fn singleton(value: T) -> SmallVector<T> {
SmallVector::One(value)
}
pub fn from_vector(vector: Vector<T>) -> SmallVector<T> {
SmallVector::Many(vector)
}
pub fn first(&self) -> Option<T> {
match self {
SmallVector::Empty => None,
&SmallVector::One(value) => Some(value),
SmallVector::Many(vector) => vector.get(0).cloned(),
}
}
pub fn push(&mut self, new_value: T) {
if let SmallVector::Empty = self {
*self = SmallVector::One(new_value);
return;
}
if let SmallVector::One(value) = *self {
*self = SmallVector::Many(vector!(value));
}
if let SmallVector::Many(vector) = self {
vector.push(new_value);
return;
}
unreachable();
}
pub fn swap_remove(&mut self, index: usize) -> T {
requires!(index == 0);
if let SmallVector::One(value) = *self {
*self = SmallVector::Empty;
value
} else if let SmallVector::Many(vector) = self {
vector.swap_remove(0)
} else {
unreachable()
}
}
}
impl<T: Copy + Default> FromIterator<T> for SmallVector<T> {
fn from_iter<I: IntoIterator<Item = T>>(iter: I) -> SmallVector<T> {
SmallVector::from_vector(Vector::from_iter(iter))
}
}
impl<T> Index<usize> for SmallVector<T> {
type Output = T;
fn index(&self, index: usize) -> &Self::Output {
assert_in_bounds(0..self.len(), index);
match self {
SmallVector::Empty => unreachable(),
SmallVector::One(value) => value,
SmallVector::Many(vector) => &vector[index],
}
}
}
impl<T> IndexMut<usize> for SmallVector<T> {
fn index_mut(&mut self, index: usize) -> &mut Self::Output {
assert_in_bounds(0..self.len(), index);
match self {
SmallVector::Empty => unreachable(),
SmallVector::One(value) => value,
SmallVector::Many(vector) => &mut vector[index],
}
}
}