From c2c25f68cfeceec0eabe88a0e23b9f78deae7d1f Mon Sep 17 00:00:00 2001 From: Tobie Morgan Hitchcock Date: Thu, 24 Mar 2022 13:03:15 +0000 Subject: [PATCH] Change array manipulation function workings --- lib/src/sql/array.rs | 101 ++++++++++++++++++++----------------------- 1 file changed, 47 insertions(+), 54 deletions(-) diff --git a/lib/src/sql/array.rs b/lib/src/sql/array.rs index 0914819e..c8d4f955 100644 --- a/lib/src/sql/array.rs +++ b/lib/src/sql/array.rs @@ -244,38 +244,6 @@ impl Abolish for Vec { // ------------------------------ -pub trait Uniq { - fn uniq(self) -> Vec; -} - -impl Uniq for Vec { - fn uniq(mut self) -> Vec { - 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 { - fn union(self, other: Vec) -> Vec; -} - -impl Union for Vec { - fn union(mut self, mut other: Vec) -> Vec { - self.append(&mut other); - self.uniq() - } -} - -// ------------------------------ - pub trait Combine { fn combine(self, other: Vec) -> Vec>; } @@ -285,9 +253,7 @@ impl Combine for Vec { let mut out = Vec::new(); for a in self.iter() { for b in other.iter() { - if a != b { - out.push(vec![a.clone(), b.clone()]); - } + out.push(vec![a.clone(), b.clone()]); } } out @@ -297,17 +263,34 @@ impl Combine for Vec { // ------------------------------ pub trait Concat { - fn concat(self, other: Vec) -> Vec>; + fn concat(self, other: Vec) -> Vec; } -impl Concat for Vec { - fn concat(self, other: Vec) -> Vec> { +impl Concat for Vec { + fn concat(mut self, mut other: Vec) -> Vec { + self.append(&mut other); + self + } +} + +// ------------------------------ + +pub trait Difference { + fn difference(self, other: Vec) -> Vec; +} + +impl Difference for Vec { + fn difference(self, other: Vec) -> Vec { let mut out = Vec::new(); - for a in self.iter() { - for b in other.iter() { - out.push(vec![a.clone(), b.clone()]); + let mut other: Vec<_> = other.into_iter().collect(); + 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 } } @@ -334,23 +317,33 @@ impl Intersect for Vec { // ------------------------------ -pub trait Difference { - fn difference(self, other: Vec) -> Vec; +pub trait Union { + fn union(self, other: Vec) -> Vec; } -impl Difference for Vec { - fn difference(self, other: Vec) -> Vec { - let mut out = Vec::new(); - let mut other: Vec<_> = other.into_iter().collect(); - for a in self.into_iter() { - if let Some(pos) = other.iter().position(|b| a == *b) { - other.remove(pos); - } else { - out.push(a); +impl Union for Vec { + fn union(mut self, mut other: Vec) -> Vec { + self.append(&mut other); + self.uniq() + } +} + +// ------------------------------ + +pub trait Uniq { + fn uniq(self) -> Vec; +} + +impl Uniq for Vec { + fn uniq(mut self) -> Vec { + for x in (0..self.len()).rev() { + for y in (x + 1..self.len()).rev() { + if self[x] == self[y] { + self.remove(y); + } } } - out.append(&mut other); - out + self } }