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
use std::fmt::Display;
/// Unzip a vector of tuples into multiple vectors.
/// Requires `use paste;`
///
/// # Examples
/// ```rust
/// use lib::unzip;
/// use lib::util::io::*;
///
/// let ab = vec![(1, 2), (3, 4), (5, 6)];
/// unzip!((a, b), ab);
/// a; // [1, 3, 5]
/// b; // [2, 4, 6]
/// ```
#[macro_export]
macro_rules! unzip {
(($($a:ident),*), $ab:expr) => {
$(let mut $a = Vec::new();)*
let it = $ab.into_iter();
for e in it {
paste::item!{
let ($([<elem_ $a>]),*) = e;
}
paste::item!{
$(
$a.push([<elem_ $a>]);
)*
}
}
};
}
/// b ? "Yes" : "No"
#[allow(non_snake_case)]
pub fn Yes(b: bool) {
println!("{}", if b { "Yes" } else { "No" });
}
/// b ? "yes" : "no"
pub fn yes(b: bool) {
println!("{}", if b { "yes" } else { "no" });
}
/// b ? "Possible" : "Impossible"
#[allow(non_snake_case)]
pub fn Possible(b: bool) {
println!("{}", if b { "possible" } else { "impossible" });
}
/// b ? "possible" : "impossible"
pub fn possible(b: bool) {
println!("{}", if b { "possible" } else { "impossible" });
}
pub trait OutFormatter {
fn out_format(&self) -> String;
}
impl<T: Display> OutFormatter for Vec<T> {
fn out_format(&self) -> String {
self.iter()
.map(|item| item.to_string())
.collect::<Vec<_>>()
.join(" ")
}
}
impl OutFormatter for usize {
fn out_format(&self) -> String {
self.to_string()
}
}
impl OutFormatter for f64 {
fn out_format(&self) -> String {
format!("{:.10}", self)
}
}
impl OutFormatter for String {
fn out_format(&self) -> String {
self.clone()
}
}
impl OutFormatter for char {
fn out_format(&self) -> String {
self.to_string()
}
}
impl OutFormatter for &str {
fn out_format(&self) -> String {
self.to_string()
}
}
/// Print given values separated by spaces to stdout.
/// Requires `use lib::util::io::*`
///
/// # Examples
/// ```rust
/// use lib::out;
/// use lib::util::io::*;
/// let v = vec![9, 9, 8];
/// let s = "string";
/// let n = 998;
/// out!(v, s, n); // "9 9 8 string 998"
/// ```
#[macro_export]
macro_rules! out {
($($arg:expr),*) => {
println!("{}", vec![$($arg.out_format()),*].join(" "));
};
}
/// Print given values separated by spaces to stderr.
/// Requires `use lib::util::io::*`
///
/// # Examples
/// ```rust
/// use lib::deb;
/// use lib::util::io::*;
/// let v = vec![9, 9, 8];
/// let s = "string";
/// let n = 998;
/// deb!(v, s, n); // "9 9 8 string 998"
/// ```
#[macro_export]
macro_rules! deb {
($($arg:expr),*) => {
eprintln!("{}", vec![$($arg.out_format()),*].join(" "));
};
}
#[cfg(test)]
mod tests {
#[test]
fn test_unzip_two() {
let ab = vec![
(1, "a".to_string()),
(3, "b".to_string()),
(5, "c".to_string()),
];
unzip!((a, b), ab);
assert_eq!(a, vec![1, 3, 5]);
assert_eq!(b, vec!["a".to_string(), "b".to_string(), "c".to_string()]);
}
#[test]
fn test_unzip_three() {
let ab = vec![(1, 2, "a"), (3, 4, "b"), (5, 6, "c")];
unzip!((a, b, c), ab);
assert_eq!(a, vec![1, 3, 5]);
assert_eq!(b, vec![2, 4, 6]);
assert_eq!(c, vec!["a", "b", "c"]);
}
}