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
#[macro_export]
macro_rules! map {
( $($k:expr => $v:expr),* $(,)* ) => {{
let mut m = HashMap::new();
$(
m.insert($k, $v);
)*
m
}};
}
#[macro_export]
macro_rules! set {
[ $($v:expr),* $(,)* ] => {{
let mut m = HashSet::new();
$(
m.insert($v);
)*
m
}};
}
const STACK_SZ: usize = 32;
#[derive(Default)]
pub struct Stack<T> {
elems: [T; STACK_SZ],
top: u8,
}
impl<T: Copy + Default> Stack<T> {
pub fn new() -> Stack<T> {
Default::default()
}
pub fn push(&mut self, v: T) {
if (self.top as usize) >= self.elems.len() {
panic!("Stack overflow.");
}
self.elems[self.top as usize] = v;
self.top += 1;
}
pub fn pop(&mut self) -> Option<T> {
if self.top == 0 {
return None;
}
self.top -= 1;
Some(self.elems[self.top as usize])
}
pub fn len(&self) -> usize {
self.top as usize
}
pub fn is_empty(&self) -> bool {
self.len() == 0
}
pub fn iter(&self) -> impl Iterator<Item = &T> {
self.elems[..(self.top as usize)].iter().rev()
}
}
#[macro_export]
macro_rules! count_args {
() => { 0 };
( $arg:ident ) => { 1 };
( $arg:ident, $($tail:ident),* ) => {
1 + count_args!($($tail),*)
}
}
macro_rules! from_many {
( $to:ty |$v:ident| { $($from:ty => $action:block),+ } ) => {
$(
impl<'a> From<$from> for $to {
fn from($v: $from) -> $to {
$action
}
}
)+
}
}
pub fn is_sorted<T>(xs: &[T]) -> bool
where T: Ord {
xs.iter()
.zip(xs.iter().skip(1))
.all(|(u, v)| u <= v)
}
#[macro_export]
macro_rules! minto {
($($v:expr),+) => {
&[$($v.into()),+]
};
}