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
#[allow(unused_macros)]
#[macro_export]

///Print macro like C++ cout.
/// # Examples
/// let x=6;
/// echo!(x,"is my favorite number.")
macro_rules! echo {
   ($($e:ident),*) => {
       $(
           (print!("{}", $e));
       )*
       print!("\n");
   };
  
   ($($e:expr),*) => {
       $(
           (print!("{}", $e));
       )*
       print!("\n");
   };      
}

#[allow(unused_macros)]
///Scan macro like C++ cin.
/// Ignores whitespace.
/// # Examples
/// let x:i32;
/// let y=String::new();
/// scan!(x,y);
/// # Panics
/// If failed to translate into desired type,
/// this macro calls panic!() macro.
#[macro_export]
macro_rules! scan {
   ($($e:expr),*) => {
       $(
           ($e=zoozle::oft::read_into());
       )*
   };     
}
 
pub fn safe_scan<T>(default:T) -> T
   where T:std::str::FromStr
{
   let mut buff = String::new();
   std::io::stdin().read_line(&mut buff).ok();
   buff.trim().parse().unwrap_or(default)
}

#[allow(unused_macros)]
#[macro_export]
///Scan macro like C++ cin.
/// Divided by whitespace.
/// # Examples
/// let x:i32;
/// let y=String::new();
/// space_scan!(x,y);   /// foo bar
/// # Panics
/// If failed to translate into desired type,
/// this macro calls panic!() macro.
macro_rules!  space_scan {
   ($($e:expr),*) => {
       let mut temp=String::new();
       std::io::stdin().read_line(&mut temp);
       let tempvec:Vec<&str>=temp.split_whitespace().collect();
       let mut count=0;
       $(
           $e=tempvec[count].parse().unwrap();
           count+=1;
       )*
   };
}

///Scan macro like C++ cin.
/// Ignores whitespace.
/// Never call panic!().
/// If failed to translate into desired type,
/// the default value will be assigned.
/// # Examples
/// let x:i32; 
/// x=safe_scan!(9999);
#[allow(unused_macros)]
#[macro_export]
macro_rules!  safe_scan {
    ($e:expr) => {
        zoozle::oft::safe_scan_func($e)
    };
      
    ($e:ident) => {
        zoozle::oft::safe_scan_func($e)
    }  
 }

#[allow(unused_macros)]
#[macro_export]
///Simple swap macro.
///You don't need &mut before variable.
macro_rules!  swap {
    ($e:ident,$m:ident) => {
        zoozle::oft::swap_func(&mut $e,&mut $m);
    }  
}

pub fn swap_func<T>(first:&mut T,second:&mut T)
   where T:Clone
{
   let temp=first.clone();
   *first=second.clone();
   *second=temp;
}

 
pub fn safe_scan_func<T>(default:T) -> T
    where T:std::str::FromStr
{
    let mut buff = String::new();
    std::io::stdin().read_line(&mut buff).ok();
    buff.trim().parse().unwrap_or(default)
 }
 


pub fn goutln<T:std::fmt::Debug>(target:T){
    println!("{:?}",target);
 }
  
 pub fn gout<T:std::fmt::Debug>(target:T){
    print!("{:?}",target);
 }
  
 pub fn read_to_str()->String{
    let mut buff=String::new();
    match std::io::stdin().read_line(&mut buff){
        Ok(_content)=>{
            buff
        },
        Err(_no)=>{
            "Failed".to_string()
        },
    }
 }
 
 pub fn read_into<T>() -> T
   where T:std::str::FromStr
{
   let mut s = String::new();
   std::io::stdin().read_line(&mut s).ok();
   s.trim().parse().ok().unwrap()
}