#![warn(missing_docs)]
use crate::{trivindex,SType,Set,MutSetOps};
use indxvec::{Indices,Vecops,Mutops};
impl<T> MutSetOps<T> for Set<T> where T:Copy+PartialOrd+Default {
fn munordered(&mut self) {
match self.stype {
SType::Empty | SType::Unordered => return, SType::Ordered => (), SType::Indexed | SType::Ranked => self.index = Vec::new() }
self.stype = SType::Unordered;
}
fn mordered(&mut self, quantify: impl Copy + Fn(&T) -> f64, asc:bool) {
match self.stype {
SType::Empty => return, SType::Unordered => { self.data.muthashsort(quantify); if !asc { self.data.mutrevs() } },
SType::Ordered => if self.ascending != asc { self.data.mutrevs() },
SType::Indexed => {
self.data = self.index.unindex(&self.data, self.ascending == asc);
self.index = Vec::new(); },
SType::Ranked => {
self.data = self.index.invindex().unindex(&self.data, self.ascending == asc);
self.index = Vec::new(); }
}
self.stype = SType::Ordered; self.ascending = asc; }
fn mindexed(&mut self, quantify: impl Copy + Fn(&T) -> f64, asc:bool) {
match self.stype {
SType::Empty => return, SType::Unordered => {
self.index = self.data.hashsort_indexed(quantify);
if !asc { self.index.mutrevs(); }; },
SType::Ordered => self.index = trivindex(self.ascending == asc,self.data.len()),
SType::Indexed => if self.ascending != asc { self.index.mutrevs() },
SType::Ranked => {
if self.ascending != asc { self.index = self.index.complindex() };
self.index = self.index.invindex(); },
}
self.stype = SType::Indexed; self.ascending = asc; }
fn mranked(&mut self,asc:bool) {
match self.stype {
SType::Empty => return, SType::Unordered => {
self.index = self.data.mergesort_indexed().invindex();
if !asc { self.index.complindex(); }; },
SType::Ordered => self.index = trivindex(self.ascending == asc,self.data.len()),
SType::Indexed => {
if self.ascending != asc { self.index.mutrevs() };
self.index = self.index.invindex(); },
SType::Ranked => if self.ascending != asc { self.index = self.index.complindex() }
}
self.stype = SType::Ranked; self.ascending = asc; }
fn msame(&mut self, s:&mut Self, quantify: impl Copy + Fn(&T) -> f64) {
match self.stype {
SType::Empty => *s = Set::EMPTYSET, SType::Unordered => s.munordered(),
SType::Ordered => s.mordered(quantify, self.ascending),
SType::Indexed => s.mindexed(quantify,self.ascending),
SType::Ranked => s.mranked(self.ascending)
}
}
fn mdelete(&mut self, item:T) -> bool where Self:Sized {
match self.stype {
SType::Empty => false, SType::Unordered => {
if let Some(i) = self.data.member(item,true) {
self.data.swap_remove(i); true }
else { false }
},
SType::Ordered => {
let r = self.data.binsearch(&item);
if r.is_empty() { return false; };
self.data.remove(r.start); true
},
SType::Indexed => {
let r = self.data.binsearch_indexed(&self.index,&item);
if r.is_empty() { return false; };
let datasub = self.index[r.start];
self.data.remove(datasub); self.index.remove(r.start); for idxitem in &mut self.index { if *idxitem > datasub { *idxitem -= 1 };
}
true },
SType::Ranked => {
let mut sortindex = self.index.invindex();
let r = self.data.binsearch_indexed(&sortindex,&item);
if r.is_empty() { return false; };
let datasub = sortindex[r.start];
self.data.remove(datasub); sortindex.remove(r.start); for idxitem in &mut sortindex { if *idxitem > datasub { *idxitem -= 1 };
}
self.index = sortindex.invindex(); true },
}
}
fn mdeleteall(&mut self, item:T) -> usize where Self:Sized {
let mut count = 0_usize;
match self.stype {
SType::Empty => 0, SType::Unordered => {
while let Some(i) = self.data.member(item,true) {
count += 1;
self.data.swap_remove(i);
};
count
},
SType::Ordered => {
let r = self.data.binsearch(&item);
if r.is_empty() { return 0; };
let count = r.len();
self.data.drain(r);
count
},
SType::Indexed => {
let mut ord_data = self.index.unindex(&self.data,self.ascending);
let r = ord_data.binsearch(&item);
if r.is_empty() { return 0; };
let count = r.len();
ord_data.drain(r);
self.data = ord_data;
self.index = trivindex(self.ascending,self.data.len());
count },
SType::Ranked => {
let mut ord_data = self.index.invindex().unindex(&self.data,self.ascending);
let r = ord_data.binsearch(&item);
if r.is_empty() { return 0; };
let count = r.len();
ord_data.drain(r);
self.data = ord_data;
self.index = trivindex(self.ascending,self.data.len());
count }
}
}
fn minsert(&mut self, item:T) {
match self.stype {
SType::Empty => { self.stype = crate::SType::Ordered;
self.data.push(item);
},
SType::Unordered => self.data.push(item),
SType::Ordered => {
let range = self.data.binsearch(&item);
self.data.insert(range.start,item); },
SType::Indexed => {
let irange = self.data.binsearch_indexed(&self.index,&item);
self.data.push(item);
self.index.insert(irange.start,self.data.len()-1);
}
SType::Ranked => {
let irange = self.data.binsearch_indexed(&self.index.invindex(),&item);
self.data.push(item);
self.index.push(irange.start);
}
};
}
fn mreverse(&mut self) {
match self.stype {
SType::Empty => Default::default(), SType::Unordered => self.data.mutrevs(),
SType::Ordered => {
self.ascending = !self.ascending;
self.data.mutrevs();
},
SType::Indexed => {
self.ascending = !self.ascending;
self.index.mutrevs();
},
SType::Ranked => {
self.ascending = !self.ascending;
self.index = self.index.complindex();
}
}
}
fn mnonrepeat(&mut self) {
if self.data.len() < 2 { return }; match self.stype {
SType::Empty => (), SType::Unordered => { self.data = self.data.sortm(true);
self.data.dedup();
},
SType::Ordered => self.data.dedup(),
SType::Indexed => { let mut orddata = self.index.unindex(&self.data,self.ascending);
orddata.dedup();
self.data = orddata; self.index = trivindex(self.ascending, self.data.len());
},
SType::Ranked => { let mut orddata = self.index.invindex().unindex(&self.data,self.ascending);
orddata.dedup();
self.data = orddata; self.index = trivindex(self.ascending, self.data.len());
}
}
}
fn munion(&mut self, s: &Self) {
let mut selford = self.to_ordered(true);
let sord = s.to_ordered(true);
selford.data = selford.data.merge(&sord.data);
*self = self.to_same(&selford); }
fn mintersection(&mut self, s: &Self) {
let mut selford = self.to_ordered(true);
let sord = s.to_ordered(true);
selford.data = selford.data.intersect(&sord.data);
*self = self.to_same(&selford); }
fn mdifference(&mut self, s: &Self) {
let mut selford = self.to_ordered(true);
let sord = s.to_ordered(true);
selford.data = selford.data.diff(&sord.data);
*self = self.to_same(&selford); }
}