Change array manipulation function workings

This commit is contained in:
Tobie Morgan Hitchcock 2022-03-24 13:03:15 +00:00
parent 7e4bf91a48
commit c2c25f68cf

View file

@ -244,38 +244,6 @@ impl<T> Abolish<T> for Vec<T> {
// ------------------------------ // ------------------------------
pub trait Uniq<T> {
fn uniq(self) -> Vec<T>;
}
impl<T: PartialEq> Uniq<T> for Vec<T> {
fn uniq(mut self) -> Vec<T> {
for x in (0..self.len()).rev() {
for y in (x + 1..self.len()).rev() {
if self[x] == self[y] {
self.remove(y);
}
}
}
self
}
}
// ------------------------------
pub trait Union<T> {
fn union(self, other: Vec<T>) -> Vec<T>;
}
impl<T: PartialEq> Union<T> for Vec<T> {
fn union(mut self, mut other: Vec<T>) -> Vec<T> {
self.append(&mut other);
self.uniq()
}
}
// ------------------------------
pub trait Combine<T> { pub trait Combine<T> {
fn combine(self, other: Vec<T>) -> Vec<Vec<T>>; fn combine(self, other: Vec<T>) -> Vec<Vec<T>>;
} }
@ -285,11 +253,9 @@ impl<T: PartialEq + Clone> Combine<T> for Vec<T> {
let mut out = Vec::new(); let mut out = Vec::new();
for a in self.iter() { for a in self.iter() {
for b in other.iter() { for b in other.iter() {
if a != b {
out.push(vec![a.clone(), b.clone()]); out.push(vec![a.clone(), b.clone()]);
} }
} }
}
out out
} }
} }
@ -297,17 +263,34 @@ impl<T: PartialEq + Clone> Combine<T> for Vec<T> {
// ------------------------------ // ------------------------------
pub trait Concat<T> { pub trait Concat<T> {
fn concat(self, other: Vec<T>) -> Vec<Vec<T>>; fn concat(self, other: Vec<T>) -> Vec<T>;
} }
impl<T: PartialEq + Clone> Concat<T> for Vec<T> { impl<T: PartialEq> Concat<T> for Vec<T> {
fn concat(self, other: Vec<T>) -> Vec<Vec<T>> { fn concat(mut self, mut other: Vec<T>) -> Vec<T> {
self.append(&mut other);
self
}
}
// ------------------------------
pub trait Difference<T> {
fn difference(self, other: Vec<T>) -> Vec<T>;
}
impl<T: PartialEq> Difference<T> for Vec<T> {
fn difference(self, other: Vec<T>) -> Vec<T> {
let mut out = Vec::new(); let mut out = Vec::new();
for a in self.iter() { let mut other: Vec<_> = other.into_iter().collect();
for b in other.iter() { for a in self.into_iter() {
out.push(vec![a.clone(), b.clone()]); if let Some(pos) = other.iter().position(|b| a == *b) {
other.remove(pos);
} else {
out.push(a);
} }
} }
out.append(&mut other);
out out
} }
} }
@ -334,23 +317,33 @@ impl<T: PartialEq> Intersect<T> for Vec<T> {
// ------------------------------ // ------------------------------
pub trait Difference<T> { pub trait Union<T> {
fn difference(self, other: Vec<T>) -> Vec<T>; fn union(self, other: Vec<T>) -> Vec<T>;
} }
impl<T: PartialEq> Difference<T> for Vec<T> { impl<T: PartialEq> Union<T> for Vec<T> {
fn difference(self, other: Vec<T>) -> Vec<T> { fn union(mut self, mut other: Vec<T>) -> Vec<T> {
let mut out = Vec::new(); self.append(&mut other);
let mut other: Vec<_> = other.into_iter().collect(); self.uniq()
for a in self.into_iter() {
if let Some(pos) = other.iter().position(|b| a == *b) {
other.remove(pos);
} else {
out.push(a);
} }
} }
out.append(&mut other);
out // ------------------------------
pub trait Uniq<T> {
fn uniq(self) -> Vec<T>;
}
impl<T: PartialEq> Uniq<T> for Vec<T> {
fn uniq(mut self) -> Vec<T> {
for x in (0..self.len()).rev() {
for y in (x + 1..self.len()).rev() {
if self[x] == self[y] {
self.remove(y);
}
}
}
self
} }
} }