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
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
pub mod example;
#[macro_export]
macro_rules! def_sorted_vec {
( $(#[$attr:meta])* $v:vis struct $name:ident: $val:ty => $key:ty, $keygen:expr ) => {
$(#[$attr])*
$v struct $name {
inner: Vec<$val>,
}
impl $name {
pub fn find(&self, key: &$key) -> Option<&$val> {
self.inner
.binary_search_by(|probe| ($keygen)(probe).cmp(key))
.ok()
.and_then(|idx| self.inner.get(idx))
}
pub fn contains(&self, key: &$key) -> bool {
self.inner
.binary_search_by(|probe| ($keygen)(probe).cmp(key))
.is_ok()
}
pub fn insert(&mut self, val: $val) {
let ref key = ($keygen)(&val);
let search = self
.inner
.binary_search_by(|probe| ($keygen)(probe).cmp(key));
let idx = match search {
Ok(i) | Err(i) => i,
};
self.inner.insert(idx, val);
}
pub fn split_off(&mut self, at: usize) -> Self {
let other_inner = self.inner.split_off(at);
Self {
inner: other_inner,
}
}
pub fn dedup(&mut self) {
self.inner.dedup_by(|a, b| ($keygen)(a) == ($keygen)(b));
}
pub fn pop(&mut self) -> Option<$val> {
self.inner.pop()
}
fn sort(&mut self) {
self.inner.sort_unstable_by(|a, b| {
let lhs = ($keygen)(a);
let rhs = ($keygen)(b);
lhs.cmp(&rhs)
})
}
}
impl std::default::Default for $name {
fn default() -> Self {
Self { inner: std::default::Default::default() }
}
}
impl Extend<$val> for $name {
fn extend<I>(&mut self, iter: I)
where
I: IntoIterator<Item = $val>,
{
self.inner.extend(iter);
self.sort();
}
}
impl std::iter::FromIterator<$val> for $name {
fn from_iter<I: std::iter::IntoIterator<Item=$val>>(iter: I) -> Self {
let inner = Vec::from_iter(iter);
From::from(inner)
}
}
impl Into<Vec<$val>> for $name {
fn into(self) -> Vec<$val> {
self.inner
}
}
impl From<Vec<$val>> for $name {
fn from(vec: Vec<$val>) -> Self {
let mut res = Self { inner: vec };
res.sort();
res
}
}
impl std::ops::Deref for $name {
type Target = Vec<$val>;
fn deref(&self) -> &Self::Target {
&self.inner
}
}
impl std::borrow::Borrow<[$val]> for $name {
fn borrow(&self) -> &[$val] {
&self.inner
}
}
impl AsRef<[$val]> for $name {
fn as_ref(&self) -> &[$val] {
&self.inner
}
}
impl AsRef<Vec<$val>> for $name {
fn as_ref(&self) -> &Vec<$val> {
&self.inner
}
}
}
}
#[cfg(test)]
mod tests {
#![allow(unused_variables)]
#[test]
fn simple() {
def_sorted_vec! {
#[derive(Eq, PartialEq, Ord, PartialOrd, Debug, Clone, Hash)]
pub struct TestVec: u32 => u32, |x| x
}
let sv: TestVec = (0u32..10).collect();
assert!(sv.find(&5) == Some(&5));
assert_eq!(10, sv.len());
let v: Vec<_> = sv.clone().into();
}
}