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
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
#![cfg_attr(test, feature(plugin))]
#![cfg_attr(test, plugin(clippy))]
#![cfg_attr(feature = "nightly", feature(never_type))]
#[cfg(feature = "nightly")]
type Nothing = !;
#[cfg(not(feature = "nightly"))]
pub enum Nothing {}
use std::collections::{BTreeMap, BTreeSet, BinaryHeap, HashMap, HashSet, LinkedList, VecDeque};
use std::hash::Hash;
pub trait Push<T> {
type PushedOut;
fn push(&mut self, val: T) -> Option<Self::PushedOut>;
}
pub trait PushFront<T>: Push<T> {
fn push_front(&mut self, val: T) -> Option<Self::PushedOut>;
}
pub trait PushBack<T>: Push<T> {
fn push_back(&mut self, val: T) -> Option<Self::PushedOut>;
}
pub trait PushRef<T: ?Sized> {
type PushedOut;
fn push_ref(&mut self, val: &T) -> Option<Self::PushedOut>;
}
pub trait PushRefFront<T: ?Sized>: PushRef<T> {
fn push_ref_front(&mut self, val: &T) -> Option<Self::PushedOut>;
}
pub trait PushRefBack<T: ?Sized>: PushRef<T> {
fn push_ref_back(&mut self, val: &T) -> Option<Self::PushedOut>;
}
pub struct PushedVal;
impl<K: Ord, V> Push<(K, V)> for BTreeMap<K, V> {
type PushedOut = V;
fn push(&mut self, (key, val): (K, V)) -> Option<V> {
BTreeMap::insert(self, key, val)
}
}
impl<T: Ord> Push<T> for BTreeSet<T> {
type PushedOut = PushedVal;
fn push(&mut self, val: T) -> Option<PushedVal> {
if BTreeSet::insert(self, val) {
None
} else {
Some(PushedVal)
}
}
}
impl<T: Ord> Push<T> for BinaryHeap<T> {
type PushedOut = Nothing;
fn push(&mut self, val: T) -> Option<Nothing> {
BinaryHeap::push(self, val);
None
}
}
impl<K: Hash + Eq, V> Push<(K, V)> for HashMap<K, V> {
type PushedOut = V;
fn push(&mut self, (key, val): (K, V)) -> Option<V> {
HashMap::insert(self, key, val)
}
}
impl<T: Hash + Eq> Push<T> for HashSet<T> {
type PushedOut = PushedVal;
fn push(&mut self, val: T) -> Option<PushedVal> {
if HashSet::insert(self, val) {
None
} else {
Some(PushedVal)
}
}
}
impl<T> Push<T> for LinkedList<T> {
type PushedOut = Nothing;
fn push(&mut self, val: T) -> Option<Nothing> {
LinkedList::push_back(self, val);
None
}
}
impl<T> Push<LinkedList<T>> for LinkedList<T> {
type PushedOut = Nothing;
fn push(&mut self, mut val: LinkedList<T>) -> Option<Nothing> {
LinkedList::append(self, &mut val);
None
}
}
impl Push<char> for String {
type PushedOut = Nothing;
fn push(&mut self, val: char) -> Option<Nothing> {
String::push(self, val);
None
}
}
impl PushRef<str> for String {
type PushedOut = Nothing;
fn push_ref(&mut self, val: &str) -> Option<Nothing> {
String::push_str(self, val);
None
}
}
impl<T> Push<T> for Vec<T> {
type PushedOut = Nothing;
fn push(&mut self, val: T) -> Option<Nothing> {
Vec::push(self, val);
None
}
}
impl<T: Clone> PushRef<[T]> for Vec<T> {
type PushedOut = Nothing;
fn push_ref(&mut self, val: &[T]) -> Option<Nothing> {
Vec::extend_from_slice(self, val);
None
}
}
impl<T> Push<T> for VecDeque<T> {
type PushedOut = Nothing;
fn push(&mut self, val: T) -> Option<Nothing> {
VecDeque::push_back(self, val);
None
}
}
impl<T> PushFront<T> for LinkedList<T> {
fn push_front(&mut self, val: T) -> Option<Nothing> {
LinkedList::push_front(self, val);
None
}
}
impl<T> PushBack<T> for LinkedList<T> {
fn push_back(&mut self, val: T) -> Option<Nothing> {
LinkedList::push_back(self, val);
None
}
}
impl<T> PushFront<LinkedList<T>> for LinkedList<T> {
fn push_front(&mut self, mut val: LinkedList<T>) -> Option<Nothing> {
LinkedList::append(&mut val, self);
*self = val;
None
}
}
impl<T> PushBack<LinkedList<T>> for LinkedList<T> {
fn push_back(&mut self, mut val: LinkedList<T>) -> Option<Nothing> {
LinkedList::append(self, &mut val);
None
}
}
impl PushBack<char> for String {
fn push_back(&mut self, val: char) -> Option<Nothing> {
String::push(self, val);
None
}
}
impl PushRefBack<str> for String {
fn push_ref_back(&mut self, val: &str) -> Option<Nothing> {
String::push_str(self, val);
None
}
}
impl<T> PushBack<T> for Vec<T> {
fn push_back(&mut self, val: T) -> Option<Nothing> {
Vec::push(self, val);
None
}
}
impl<T: Clone> PushRefBack<[T]> for Vec<T> {
fn push_ref_back(&mut self, val: &[T]) -> Option<Nothing> {
Vec::extend_from_slice(self, val);
None
}
}
impl<T> PushFront<T> for VecDeque<T> {
fn push_front(&mut self, val: T) -> Option<Nothing> {
VecDeque::push_front(self, val);
None
}
}
impl<T> PushBack<T> for VecDeque<T> {
fn push_back(&mut self, val: T) -> Option<Nothing> {
VecDeque::push_back(self, val);
None
}
}