1#![warn(missing_docs)]
2use crate::{trivindex,SType,Set,MutSetOps};
3use indxvec::{Indices,Vecops,Mutops};
4
5impl<T> MutSetOps<T> for Set<T> where T:Copy+PartialOrd+Default {
6
7 fn munordered(&mut self) {
10 match self.stype {
11 SType::Empty | SType::Unordered => return, SType::Ordered => (), SType::Indexed | SType::Ranked => self.index = Vec::new() }
15 self.stype = SType::Unordered;
16 }
18
19 fn mordered(&mut self, quantify: impl Copy + Fn(&T) -> f64, asc:bool) {
21 match self.stype {
22 SType::Empty => return, SType::Unordered => { self.data.muthashsort(quantify); if !asc { self.data.mutrevs() } },
24 SType::Ordered => if self.ascending != asc { self.data.mutrevs() },
25 SType::Indexed => {
26 self.data = self.index.unindex(&self.data, self.ascending == asc);
27 self.index = Vec::new(); },
28 SType::Ranked => {
29 self.data = self.index.invindex().unindex(&self.data, self.ascending == asc);
30 self.index = Vec::new(); }
31 }
32 self.stype = SType::Ordered; self.ascending = asc; }
35
36 fn mindexed(&mut self, quantify: impl Copy + Fn(&T) -> f64, asc:bool) {
38 match self.stype {
39 SType::Empty => return, SType::Unordered => {
41 self.index = self.data.hashsort_indexed(quantify);
42 if !asc { self.index.mutrevs(); }; },
43 SType::Ordered => self.index = trivindex(self.ascending == asc,self.data.len()),
44 SType::Indexed => if self.ascending != asc { self.index.mutrevs() },
45 SType::Ranked => {
46 if self.ascending != asc { self.index = self.index.complindex() };
47 self.index = self.index.invindex(); },
48 }
49 self.stype = SType::Indexed; self.ascending = asc; }
52
53 fn mranked(&mut self,asc:bool) {
55 match self.stype {
56 SType::Empty => return, SType::Unordered => {
58 self.index = self.data.mergesort_indexed().invindex();
59 if !asc { self.index.complindex(); }; },
60 SType::Ordered => self.index = trivindex(self.ascending == asc,self.data.len()),
61 SType::Indexed => {
62 if self.ascending != asc { self.index.mutrevs() };
63 self.index = self.index.invindex(); },
64 SType::Ranked => if self.ascending != asc { self.index = self.index.complindex() }
65 }
66 self.stype = SType::Ranked; self.ascending = asc; }
69
70 fn msame(&mut self, s:&mut Self, quantify: impl Copy + Fn(&T) -> f64) {
73 match self.stype {
74 SType::Empty => *s = Set::EMPTYSET, SType::Unordered => s.munordered(),
76 SType::Ordered => s.mordered(quantify, self.ascending),
77 SType::Indexed => s.mindexed(quantify,self.ascending),
78 SType::Ranked => s.mranked(self.ascending)
79 }
80 }
81
82 fn mdelete(&mut self, item:T) -> bool where Self:Sized {
85 match self.stype {
86 SType::Empty => false, SType::Unordered => {
88 if let Some(i) = self.data.member(item,true) {
89 self.data.swap_remove(i); true }
91 else { false }
92 },
93 SType::Ordered => {
94 let r = self.data.binsearch(&item);
95 if r.is_empty() { return false; };
96 self.data.remove(r.start); true
98 },
99
100 SType::Indexed => {
101 let r = self.data.binsearch_indexed(&self.index,&item);
102 if r.is_empty() { return false; };
103 let datasub = self.index[r.start];
104 self.data.remove(datasub); self.index.remove(r.start); for idxitem in &mut self.index { if *idxitem > datasub { *idxitem -= 1 };
108 }
109 true },
110
111 SType::Ranked => {
112 let mut sortindex = self.index.invindex();
113 let r = self.data.binsearch_indexed(&sortindex,&item);
114 if r.is_empty() { return false; };
115 let datasub = sortindex[r.start];
116 self.data.remove(datasub); sortindex.remove(r.start); for idxitem in &mut sortindex { if *idxitem > datasub { *idxitem -= 1 };
120 }
121 self.index = sortindex.invindex(); true },
123 }
124 }
125
126 fn mdeleteall(&mut self, item:T) -> usize where Self:Sized {
129 let mut count = 0_usize;
130 match self.stype {
131 SType::Empty => 0, SType::Unordered => {
133 while let Some(i) = self.data.member(item,true) {
134 count += 1;
135 self.data.swap_remove(i);
137 };
138 count
139 },
140 SType::Ordered => {
141 let r = self.data.binsearch(&item);
142 if r.is_empty() { return 0; };
143 let count = r.len();
144 self.data.drain(r);
145 count
146 },
147
148 SType::Indexed => {
149 let mut ord_data = self.index.unindex(&self.data,self.ascending);
150 let r = ord_data.binsearch(&item);
151 if r.is_empty() { return 0; };
152 let count = r.len();
153 ord_data.drain(r);
154 self.data = ord_data;
155 self.index = trivindex(self.ascending,self.data.len());
156 count },
157
158 SType::Ranked => {
159 let mut ord_data = self.index.invindex().unindex(&self.data,self.ascending);
160 let r = ord_data.binsearch(&item);
161 if r.is_empty() { return 0; };
162 let count = r.len();
163 ord_data.drain(r);
164 self.data = ord_data;
165 self.index = trivindex(self.ascending,self.data.len());
166 count }
167 }
168 }
169
170 fn minsert(&mut self, item:T) {
172 match self.stype {
173 SType::Empty => { self.stype = crate::SType::Ordered;
175 self.data.push(item);
176 },
177 SType::Unordered => self.data.push(item),
178 SType::Ordered => {
179 let range = self.data.binsearch(&item);
181 self.data.insert(range.start,item); },
183 SType::Indexed => {
184 let irange = self.data.binsearch_indexed(&self.index,&item);
185 self.data.push(item);
187 self.index.insert(irange.start,self.data.len()-1);
189
190 }
191 SType::Ranked => {
192 let irange = self.data.binsearch_indexed(&self.index.invindex(),&item);
194 self.data.push(item);
196 self.index.push(irange.start);
198 }
199 };
200 }
201
202 fn mreverse(&mut self) {
205 match self.stype {
206 SType::Empty => Default::default(), SType::Unordered => self.data.mutrevs(),
208 SType::Ordered => {
209 self.ascending = !self.ascending;
210 self.data.mutrevs();
211 },
212 SType::Indexed => {
213 self.ascending = !self.ascending;
214 self.index.mutrevs();
215 },
216 SType::Ranked => {
217 self.ascending = !self.ascending;
218 self.index = self.index.complindex();
219 }
220 }
221 }
222
223 fn mnonrepeat(&mut self) {
225 if self.data.len() < 2 { return }; match self.stype {
227 SType::Empty => (), SType::Unordered => { self.data = self.data.sortm(true);
230 self.data.dedup();
231 },
232 SType::Ordered => self.data.dedup(),
233 SType::Indexed => { let mut orddata = self.index.unindex(&self.data,self.ascending);
235 orddata.dedup();
236 self.data = orddata; self.index = trivindex(self.ascending, self.data.len());
238 },
239 SType::Ranked => { let mut orddata = self.index.invindex().unindex(&self.data,self.ascending);
241 orddata.dedup();
242 self.data = orddata; self.index = trivindex(self.ascending, self.data.len());
244 }
245 }
246 }
247
248 fn munion(&mut self, s: &Self) {
250 let mut selford = self.to_ordered(true);
251 let sord = s.to_ordered(true);
252 selford.data = selford.data.merge(&sord.data);
253 *self = self.to_same(&selford); }
255
256 fn mintersection(&mut self, s: &Self) {
258 let mut selford = self.to_ordered(true);
259 let sord = s.to_ordered(true);
260 selford.data = selford.data.intersect(&sord.data);
261 *self = self.to_same(&selford); }
263
264 fn mdifference(&mut self, s: &Self) {
266 let mut selford = self.to_ordered(true);
267 let sord = s.to_ordered(true);
268 selford.data = selford.data.diff(&sord.data);
269 *self = self.to_same(&selford); }
271}