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
pub mod vec {
  pub trait Uniq {
    fn uniq(&self, other: Self) -> Self;
    fn unique(&self) -> Self;
    fn is_unique(&self) -> bool;
  }

  impl<T: Clone + PartialEq> Uniq for Vec<T> {
    fn uniq(&self, other: Vec<T>) -> Vec<T> {

      let mut uniq_val = vec![];

      for x in self.to_vec() {
        let mut unique = true;
        for y in other.to_vec() {
          if x == y {
            unique = false
          }
        };
        if unique {
          uniq_val.push(x.clone())
        }
      };
      uniq_val
    }

    fn unique(&self) -> Vec<T> {
      let mut a = self.clone();
      for x in 0..a.len() {
        for y in x+1..a.len() {
          if a[x] == a[y] {
            a.remove(y);
            break;
          }
        }
      }
      a
    }

    fn is_unique(&self) -> bool {
      let mut a = true;
      for x in 0..self.len() {
        for y in x+1..self.len() {
          if self[x] == self[y] {
            a = false;
            break;
          }
        }
      }
      a
    }
  }

  pub trait Empty {
    //#[deprecated(since = "0.2.3", reason = "already implemented with is_empty()")]
    fn empty(&self)-> bool;
  }

  impl<T: PartialEq> Empty for Vec<T> {
    //#[deprecated(since = "0.2.3", reason = "already implemented with is_empty()")]
    fn empty(&self) -> bool {
      self.len() == 0
    }
  }

  pub trait Shift<T> {
    fn shift(&mut self) -> T;
    fn unshift(&mut self, other: T);
  }
  
  impl<T: PartialEq> Shift<T> for Vec<T> {
    fn shift(&mut self) -> T {
      self.remove(0)
    }
    fn unshift(&mut self, other: T) {
      &self.insert(0, other);
    }
  }

  pub trait Intersect {
    fn intersect(&self, Self) -> Self;
  }

  impl<T: PartialEq + Clone> Intersect for Vec<T> {
    fn intersect(&self, other: Vec<T>) -> Vec<T> {
      let mut out = vec![];
      let a = self.unique();
      let length = other.len();
      for x in a {
        for y in 0..length {
          if x == other[y] {
            out.push(x);
            break;
          }
        }
      }
      out
    }
  }

  pub trait Join {
    fn join(&self, joiner: &'static str) -> String;
  }

  impl<T: ToString> Join for Vec<T> {
    fn join(&self, joiner: &'static str) -> String {
      let mut out = String::from("");
      for x in 0..self.len() {
        out.push_str(&self[x].to_string());
        if x < self.len()-1 {
          out.push_str(&joiner)
        }
      }
      out
    }
  }

  pub trait Times {
    fn times(&self, qty: i32) -> Self;
  }

  impl<T: Clone> Times for Vec<T> {
     // TODO: The size of the vector to be made "is known", 
     // re-implement with full length vector and then asign
     // each position
    fn times(&self, qty: i32) -> Vec<T> {
      let mut out = vec![];
      for _ in 0..(qty as usize) {
        for i in 0..self.len() {
          out.push(self[i].clone())
        }
      }
      out
    }
  }
  
  pub trait Union {
    fn union(&self, other: Self) -> Self;
  }
  impl<T: PartialEq + Clone> Union for Vec<T> {
     // TODO: The size of the vector to be made "is known", 
     // re-implement with full length vector and then asign
     // each position
    fn union(&self, other: Vec<T>) -> Vec<T> {
      let mut stack = self.clone();
      for x in other {
        stack.push(x)
      }
      stack.unique()
    }
  }

}

pub fn uniques<T: PartialEq + Clone>(a: Vec<T>, b: Vec<T>) -> Vec<Vec<T>> {
  use self::vec::Uniq;
  vec![a.uniq(b.clone()), b.uniq(a)]
}