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 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352
#![warn(missing_docs)]
use crate::{trivindex,SType,Set,MutSetOps};
use indxvec::{Indices,Vecops,Mutsort};
/// Removes repetitions from explicitly ordered data.
/// Ascending or descending.
pub fn msansrepeat<T>(s:&mut Vec<T>) where T: PartialEq+Copy {
if s.len() < 2 { return };
let mut last:T = s[0];
let mut length = s.len();
let mut i = 1;
while i < length {
if s[i] != last { last = s[i]; i+=1; }
else { s.remove(i); length -= 1; }
}
}
impl<T> MutSetOps<T> for Set<T> where T:Copy+PartialOrd+Default {
/// Deletes an item v of the same end-type from self
/// Returns false if item not found
fn mdelete(&mut self, item:T) -> bool where Self:Sized {
match self.stype {
SType::Empty => Default::default(), // empty set
SType::Unordered => {
if let Some(i) = self.search(item) {
// don't care about order, swap_remove swaps in the last item, fast
self.data.swap_remove(i); true }
else { false }
},
SType::Ordered => {
if let Some(i) = self.search(item) {
self.data.remove(i); true } // preserve ordering
else { false }
},
SType::Indexed => {
let mut rankindex = self.index.invindex();
if let Some(ix) = if self.ascending {
self.data.memsearch_indexed(&self.index,item) }
else { self.data.memsearchdesc_indexed(&self.index,item) }
{
self.data.remove(self.index[ix]);
rankindex.remove(self.index[ix]);
// repare the whole rank index
if self.ascending {
for (j,&val) in self.data.iter().enumerate() {
if val > item { rankindex[j] -= 1 };
}
}
else {
for (j,&val) in self.data.iter().enumerate() {
if val < item { rankindex[j] -= 1 };
}
}
self.index = rankindex.invindex();
true
}
else { false }
},
SType::Ranked => {
let sortindex = self.index.invindex();
if let Some(ix) = if self.ascending {
self.data.memsearch_indexed(&sortindex,item) }
else { self.data.memsearchdesc_indexed(&sortindex,item) }
{ // memsearch(desc) suceeded, finding subscript ix of item
self.data.remove(sortindex[ix]);
// rank index is also in data order
self.index.remove(sortindex[ix]);
// repare the whole rank index
if self.ascending {
for (j,&val) in self.data.iter().enumerate() {
if val > item { self.index[j] -= 1 };
}
}
else {
for (j,&val) in self.data.iter().enumerate() {
if val < item { self.index[j] -= 1 };
}
}
true
}
else { false } // memsearch(desc) failed
}
}
}
/// Inserts an item v of the same end-type to self
fn minsert(&mut self, item:T) {
match self.stype {
SType::Empty => { // initially empty set
self.stype = crate::SType::Ordered;
self.data.push(item);
},
SType::Unordered => self.data.push(item),
SType::Ordered => {
// binsearch finds the right sort position
let i = if self.ascending { self.data.binsearch(item) }
else { self.data.binsearchdesc(item) };
self.data.insert(i,item); // shifts the rest
},
SType::Indexed => {
let ix = if self.ascending { self.data.binsearch_indexed(&self.index,item) }
else { self.data.binsearchdesc_indexed(&self.index,item) };
// simply push the item to the end of unordered data self.data
self.data.push(item);
// and insert its subscipt into the right place ix in the sort index
self.index.insert(ix,self.data.len()-1);
}
SType::Ranked => {
// have to invert the rank index to get the required sort index
let ix = if self.ascending { self.data.binsearch_indexed(&self.index.invindex(),item) }
else { self.data.binsearchdesc_indexed(&self.index.invindex(),item) };
// simply push the new item to the end of unordered data self.data
self.data.push(item);
// and insert its subscipt into the same place in the rank index
self.index.push(ix);
}
};
}
/// Reverses a vec by iterating over only half of its length
/// and swapping the items
fn mreverse(&mut self) {
match self.stype {
SType::Empty => Default::default(), // empty set
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();
}
}
}
/// Deletes any repetitions
fn mnonrepeat(&mut self) {
match self.stype {
SType::Empty => Default::default(), // empty set
SType::Unordered => { // sorts data first
self.data = self.data.sortm(true);
msansrepeat(&mut self.data);
},
SType::Ordered => msansrepeat(&mut self.data),
SType::Indexed => { // spoofed by sorted data and trivial index
let mut orddata = self.index.unindex(&self.data,self.ascending);
msansrepeat(&mut orddata);
self.data = orddata; // resets data to ordered
self.index = trivindex(self.ascending, self.data.len());
},
SType::Ranked => { // spoofed by sorted data and trivial index
let mut orddata = self.index.invindex().unindex(&self.data,self.ascending);
msansrepeat(&mut orddata);
self.data = orddata; // resets data to ordered
self.index = trivindex(self.ascending, self.data.len());
}
}
}
/// sets union
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); // back to original type and order
}
/// Intersection of two unordered sets, assigned to self
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); // back to original type and order
}
/// Complement of s in self (i.e. self -= s)
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); // back to original type and order
}
}
/*
impl<T> MutSetOps<T> for OrderedSet<T> where T:Copy+PartialOrd {
/// Reverses a vec by iterating over only half of its length
/// and swapping the items
fn mreverse(&mut self) {
self.ascending = !self.ascending;
let n = self.data.len();
for i in 0..n/2 { self.swap(i,n-i-1) }
}
/// Deletes any repetitions
fn mnonrepeat(&mut self) { self.data = self.data.sansrepeat() }
/// Ascending union of two ordered sets, reassigned to self
fn munion(&mut self, s: &Self) {
// the result will be always ascending
if !self.ascending { self.ascending = true; self.data = self.data.revs() };
if s.ascending { self.data = self.data.merge(&s.data) }
else { self.data = self.data.merge(&s.data.revs()); };
}
/// Ascending intersection of two sets, assigned to the self
fn mintersection(&mut self, s: &Self) {
// the result will be always ascending
if !self.ascending { self.ascending = true; self.data = self.data.revs() };
if s.ascending { self.data = self.data.intersect(&s.data )}
else { self.data = self.data.intersect(&s.data.revs()) };
}
/// Ascending complement of s in self (i.e. self-s)
fn mdifference(&mut self, s: &Self) {
// the result will be always ascending
if !self.ascending { self.ascending = true; self.data = self.data.revs() };
if s.ascending { self.data = self.data.diff(&s.data) }
else { self.data = self.data.diff(&s.data.revs()) };
}
}
/// These are generally better than OrderedSet(s) for bulky end types, as
/// there is no moving of data around.
impl<T> MutSetOps<T> for IndexedSet<T> where T: Copy+PartialOrd {
/// Union of two IndexedSets reassigned to self.
/// Will be always ascending ordered.
fn munion(&mut self, s: &Self) {
if self.ascending {
if s.ascending { (self.data,self.index) = self.data.merge_indexed(&self.index,&s.data, &s.index) }
else { (self.data,self.index) = self.data.merge_indexed(&self.index, &s.data, &s.index.revindex() ) }
}
else {
self.ascending = true;
if s.ascending { (self.data,self.index) = self.data.merge_indexed( &self.index.revindex(),&s.data,&s.index) }
else { (self.data,self.index) = self.data.merge_indexed(&self.index.revindex(), &s.data, &s.index.revindex()) }
}
}
/// Intersection of two IndexedSets
fn mintersection(&mut self, s: &Self) {
if self.ascending {
if s.ascending { self.data = self.data.indexntersect_indexed(&self.index,&s.data, &s.index) }
else { self.data = self.data.indexntersect_indexed(&self.index,&s.data, &s.index.revindex() ) }
}
else {
self.ascending = true;
if s.ascending { self.data = self.data.indexntersect_indexed(&self.index.revindex(),&s.data,&s.index) }
else { self.data = self.data.indexntersect_indexed(&self.index.revindex(), &s.data, &s.index.revindex()) }
}
// result index will be of the new size but in all cases trivial and ascending
self.index = trivindex(true,self.data.len());
}
/// Complement of s in self (i.e. self-s)
fn mdifference(&mut self, s: &Self) {
if self.ascending {
if s.ascending { self.data = self.data.diff_indexed(&self.index,&s.data, &s.index) }
else { self.data = self.data.diff_indexed(&self.index,&s.data, &s.index.revindex() ) }
}
else {
self.ascending = true;
if s.ascending { self.data = self.data.indexntersect_indexed(&self.index.revindex(),&s.data,&s.index) }
else { self.data = self.data.indexntersect_indexed(&self.index.revindex(), &s.data, &s.index.revindex()) }
}
// result index will be of the new size but in all cases trivial and ascending
self.index = trivindex(true,self.data.len());
}
}
/// The primitive functions from `indxvec` all expect indexed sets,
/// so for now we convert from ranks to sort indices using `.indexnvindex()`.
/// Even though that is a simple operation, for lots of set operations,
/// it will be slightly quicker to work in IndexedSet(s)
/// and only to rank the final result.
impl<T> MutSetOps<T> for RankedSet<T> where T: Copy+PartialOrd {
/// Inserts an item v of the same end-type to self
fn minsert(&mut self, item:T) {
}
/// just make the ranks descending
fn mreverse(&mut self) {
self.ascending = !self.ascending;
self.index = self.index.complindex() // `complindex` reverses the ranks
}
/// deletes repetitions.
fn mnonrepeat(&mut self) {
let clean = self.data.sansrepeat();
self.data = clean.to_vec();
// rebuild the ranks (can do better)
self.index = clean.rank(self.ascending)
}
/// Union of two RankedSets.
/// Converts ranks to sort indices with `invindex`, merges, then converts back to ranks
/// Data `self.data` is simply concatenated
fn munion(&mut self, s: &Self) {
if !self.ascending { self.ascending = true; self.index = self.index.complindex() }
if s.ascending { (self.data,self.index) =
self.data.merge_indexed( &self.index.indexnvindex(), &s.data, &s.index.indexnvindex() ); }
else { (self.data,self.index) =
self.data.merge_indexed( &self.index.indexnvindex(), &s.data, &s.index.indexnvindex().revindex() ); };
self.index = self.index.indexnvindex(); // invert back to ranks index
}
/// Intersection of two RankedSets
fn mintersection(&mut self, s: &Self) {
if self.ascending {
if s.ascending { self.data = self.data.indexntersect_indexed(&self.index.indexnvindex(),&s.data, &s.index.indexnvindex()) }
else { self.data = self.data.indexntersect_indexed( &self.index.indexnvindex(),&s.data, &s.index.indexnvindex().revindex() ) }
}
else {
self.ascending = true;
if s.ascending { self.data =
self.data.indexntersect_indexed( &self.index.indexnvindex().revindex(),&s.data,&s.index.indexnvindex()) }
else { self.data = self.data.indexntersect_indexed( &self.index.indexnvindex().revindex(), &s.data, &s.index.indexnvindex().revindex()) }
}
// result ranks will be of the new size but in all cases trivial and ascending
self.index = trivindex(true,self.data.len());
}
/// Complement of s in self (i.e. self-s)
fn mdifference(&mut self, s: &Self) {
if self.ascending {
if s.ascending { self.data = self.data.diff_indexed(&self.index.indexnvindex(),&s.data, &s.index.indexnvindex()) }
else { self.data = self.data.diff_indexed( &self.index.indexnvindex(),&s.data, &s.index.indexnvindex().revindex() ) }
}
else {
self.ascending = true;
if s.ascending { self.data = self.data.diff_indexed( &self.index.indexnvindex().revindex(),&s.data,&s.index.indexnvindex()) }
else { self.data = self.data.diff_indexed( &self.index.indexnvindex().revindex(), &s.data, &s.index.indexnvindex().revindex()) }
}
// result ranks will be of the new size, trivial and ascending
self.index = trivindex(true,self.data.len());
}
}
*/