1use superslice::Ext;
6
7pub fn distance<T: Eq>(x1: &[T], x2: &[T]) -> usize {
14 assert_eq!(x1.len(), x2.len());
15 let mut dist = 0;
16 for i in 0..x1.len() {
17 if x1[i] != x2[i] {
18 dist += 1;
19 }
20 }
21 dist
22}
23
24pub fn reverse_sort<T: Ord>(x: &mut Vec<T>) {
31 x.sort_by(|a, b| b.cmp(a));
32}
33
34pub fn unique_sort<T: Ord>(x: &mut Vec<T>) {
37 x.sort();
38 x.dedup();
39}
40
41pub fn contains_at<T: Eq>(s: &[T], t: &[T], p: usize) -> bool {
48 if p + t.len() > s.len() {
49 return false;
50 }
51 for i in 0..t.len() {
52 if s[p + i] != t[i] {
53 return false;
54 }
55 }
56 true
57}
58
59pub fn contains<T: Eq>(x: &[T], y: &[T]) -> bool {
62 if y.len() > x.len() {
63 return false;
64 }
65 for i in 0..=x.len() - y.len() {
66 let mut matches = true;
67 for j in 0..y.len() {
68 if x[i + j] != y[j] {
69 matches = false;
70 break;
71 }
72 }
73 if matches {
74 return true;
75 }
76 }
77 false
78}
79
80pub trait VecUtils<'a> {
85 fn ilen(&self) -> isize;
86
87 fn solo(&self) -> bool;
88
89 fn duo(&self) -> bool;
90}
91
92impl<'a, T> VecUtils<'a> for [T] {
93 fn ilen(&self) -> isize {
94 self.len() as isize
95 }
96
97 fn solo(&self) -> bool {
98 self.len() == 1
99 }
100
101 fn duo(&self) -> bool {
102 self.len() == 2
103 }
104}
105
106pub fn erase_if<T>(x: &mut Vec<T>, to_delete: &[bool]) {
114 let mut count = 0;
115 for (j, &delete) in to_delete.iter().take(x.len()).enumerate() {
116 if !delete {
117 if j != count {
118 x.swap(j, count);
119 }
120 count += 1;
121 }
122 }
123 x.truncate(count);
124}
125
126pub fn meet<T: Ord>(x: &[T], y: &[T]) -> bool {
133 let mut i = 0;
134 let mut j = 0;
135 while i < x.len() && j < y.len() {
136 if x[i] < y[j] {
137 i += 1;
138 } else if y[j] < x[i] {
139 j += 1;
140 } else {
141 return true;
142 }
143 }
144 false
145}
146
147pub fn meet_size<T: Ord>(x: &[T], y: &[T]) -> usize {
152 let mut i = 0;
153 let mut j = 0;
154 let mut count = 0;
155 while i < x.len() && j < y.len() {
156 if x[i] < y[j] {
157 i += 1;
158 } else if y[j] < x[i] {
159 j += 1;
160 } else {
161 count += 1;
162 i += 1;
163 j += 1;
164 }
165 }
166 count
167}
168
169pub fn intersection<T: Ord + Clone>(x: &[T], y: &[T], z: &mut Vec<T>) {
172 z.clear();
173 let (mut ix, mut iy) = (0, 0);
174 while ix < x.len() && iy < y.len() {
175 if x[ix] < y[iy] {
176 ix += 1;
177 } else if y[iy] < x[ix] {
178 iy += 1;
179 } else {
180 z.push(x[ix].clone());
181 ix += 1;
182 iy += 1;
183 }
184 }
185}
186
187pub fn make_freq<T: Ord + Clone>(x: &[T], freq: &mut Vec<(u32, T)>) {
195 freq.clear();
196 let mut j = 0;
197 loop {
198 if j == x.len() {
199 break;
200 }
201 let mut k = j + 1;
202 loop {
203 if k == x.len() || x[k] != x[j] {
204 break;
205 }
206 k += 1;
207 }
208 let t = x[j].clone();
209 freq.push(((k - j) as u32, t));
210 j = k;
211 }
212 freq.sort_by(|a, b| b.cmp(a)); }
214
215pub fn bin_member<T: Ord>(x: &[T], d: &T) -> bool {
222 x.binary_search(d).is_ok()
223}
224
225pub fn position<T: Ord>(x: &[T], d: &T) -> i32 {
229 for (i, y) in x.iter().enumerate() {
230 if y == d {
231 return i as i32;
232 }
233 }
234 -1_i32
235}
236
237pub fn bin_position<T: Ord>(x: &[T], d: &T) -> i32 {
241 match x.binary_search(d) {
242 Ok(p) => p as i32,
243 Err(_e) => -1,
244 }
245}
246
247pub fn bin_position1_2<S: Ord, T: Ord>(x: &[(S, T)], d: &S) -> i32 {
248 match x.binary_search_by_key(&d, |&(ref a, ref _b)| a) {
249 Ok(p) => p as i32,
250 Err(_e) => -1,
251 }
252}
253
254pub fn bin_position1_3<S: Ord, T: Ord, U: Ord>(x: &[(S, T, U)], d: &S) -> i32 {
255 match x.binary_search_by_key(&d, |&(ref a, ref _b, ref _c)| a) {
256 Ok(p) => p as i32,
257 Err(_e) => -1,
258 }
259}
260
261pub fn bin_position1_4<S: Ord, T: Ord, U: Ord, V: Ord>(x: &[(S, T, U, V)], d: &S) -> i32 {
262 match x.binary_search_by_key(&d, |&(ref a, ref _b, ref _c, ref _d)| a) {
263 Ok(p) => p as i32,
264 Err(_e) => -1,
265 }
266}
267
268pub fn bin_position1_5<S: Ord, T: Ord, U: Ord, V: Ord, W: Ord>(
269 x: &[(S, T, U, V, W)],
270 d: &S,
271) -> i32 {
272 match x.binary_search_by_key(&d, |&(ref a, ref _b, ref _c, ref _d, ref _e)| a) {
273 Ok(p) => p as i32,
274 Err(_e) => -1,
275 }
276}
277
278pub fn lower_bound<T: Ord>(x: &[T], d: &T) -> i64 {
281 x.lower_bound(d) as i64
282}
283
284pub fn upper_bound<T: Ord>(x: &[T], d: &T) -> i64 {
285 x.upper_bound(d) as i64
286}
287
288pub fn lower_bound1_2<S: Ord, T: Ord>(x: &[(S, T)], d: &S) -> i64 {
289 x.lower_bound_by_key(&d, |(a, _b)| a) as i64
290}
291
292pub fn upper_bound1_2<S: Ord, T: Ord>(x: &[(S, T)], d: &S) -> i64 {
293 x.upper_bound_by_key(&d, |(a, _b)| a) as i64
294}
295
296pub fn lower_bound1_3<S: Ord, T: Ord, U: Ord>(x: &[(S, T, U)], d: &S) -> i64 {
297 x.lower_bound_by_key(&d, |(a, _b, _c)| a) as i64
298}
299
300pub fn upper_bound1_3<S: Ord, T: Ord, U: Ord>(x: &[(S, T, U)], d: &S) -> i64 {
301 x.upper_bound_by_key(&d, |(a, _b, _c)| a) as i64
302}
303
304pub fn count_instances<T: Ord>(x: &[T], d: &T) -> i32 {
307 (x.upper_bound(d) - x.lower_bound(d)) as i32
308}
309
310pub fn next_diff<T: Eq>(x: &[T], i: usize) -> usize {
318 let mut j = i + 1;
319 loop {
320 if j == x.len() || x[j] != x[i] {
321 return j;
322 }
323 j += 1;
324 }
325}
326
327pub fn next_diff1_2<T: Eq, U: Eq>(x: &[(T, U)], i: i32) -> i32 {
328 let mut j: i32 = i + 1;
329 loop {
330 if j == x.len() as i32 || x[j as usize].0 != x[i as usize].0 {
331 return j;
332 }
333 j += 1;
334 }
335}
336
337pub fn next_diff1_3<T: Eq, U: Eq, V: Eq>(x: &[(T, U, V)], i: i32) -> i32 {
338 let mut j: i32 = i + 1;
339 loop {
340 if j == x.len() as i32 || x[j as usize].0 != x[i as usize].0 {
341 return j;
342 }
343 j += 1;
344 }
345}
346
347pub fn next_diff1_4<T: Eq, U: Eq, V: Eq, W: Eq>(x: &[(T, U, V, W)], i: i32) -> i32 {
348 let mut j: i32 = i + 1;
349 loop {
350 if j == x.len() as i32 || x[j as usize].0 != x[i as usize].0 {
351 return j;
352 }
353 j += 1;
354 }
355}
356
357pub fn next_diff12_3<T: Eq, U: Eq, V: Eq>(x: &[(T, U, V)], i: i32) -> i32 {
358 let mut j: i32 = i + 1;
359 loop {
360 if j == x.len() as i32
361 || x[j as usize].0 != x[i as usize].0
362 || x[j as usize].1 != x[i as usize].1
363 {
364 return j;
365 }
366 j += 1;
367 }
368}
369
370pub fn next_diff12_4<T: Eq, U: Eq, V: Eq, W: Eq>(x: &[(T, U, V, W)], i: i32) -> i32 {
371 let mut j: i32 = i + 1;
372 loop {
373 if j == x.len() as i32
374 || x[j as usize].0 != x[i as usize].0
375 || x[j as usize].1 != x[i as usize].1
376 {
377 return j;
378 }
379 j += 1;
380 }
381}
382
383#[allow(clippy::type_complexity)]
384pub fn next_diff12_8<S: Eq, T: Eq, U: Eq, V: Eq, W: Eq, X: Eq, Y: Eq, Z: Eq>(
385 x: &[(S, T, U, V, W, X, Y, Z)],
386 i: i32,
387) -> i32 {
388 let mut j: i32 = i + 1;
389 loop {
390 if j == x.len() as i32
391 || x[j as usize].0 != x[i as usize].0
392 || x[j as usize].1 != x[i as usize].1
393 {
394 return j;
395 }
396 j += 1;
397 }
398}
399
400pub fn next_diff1_5<T: Eq, U: Eq, V: Eq, W: Eq, X: Eq>(x: &[(T, U, V, W, X)], i: i32) -> i32 {
401 let mut j: i32 = i + 1;
402 loop {
403 if j == x.len() as i32 || x[j as usize].0 != x[i as usize].0 {
404 return j;
405 }
406 j += 1;
407 }
408}
409
410pub fn next_diff1_6<T: Eq, U: Eq, V: Eq, W: Eq, X: Eq, Y: Eq>(
411 x: &[(T, U, V, W, X, Y)],
412 i: i32,
413) -> i32 {
414 let mut j: i32 = i + 1;
415 loop {
416 if j == x.len() as i32 || x[j as usize].0 != x[i as usize].0 {
417 return j;
418 }
419 j += 1;
420 }
421}
422
423pub fn next_diff1_7<T: Eq, U: Eq, V: Eq, W: Eq, X: Eq, Y: Eq, Z: Eq>(
424 x: &[(T, U, V, W, X, Y, Z)],
425 i: i32,
426) -> i32 {
427 let mut j: i32 = i + 1;
428 loop {
429 if j == x.len() as i32 || x[j as usize].0 != x[i as usize].0 {
430 return j;
431 }
432 j += 1;
433 }
434}
435
436#[allow(clippy::type_complexity)]
437pub fn next_diff1_8<S: Eq, T: Eq, U: Eq, V: Eq, W: Eq, X: Eq, Y: Eq, Z: Eq>(
438 x: &[(S, T, U, V, W, X, Y, Z)],
439 i: i32,
440) -> i32 {
441 let mut j: i32 = i + 1;
442 loop {
443 if j == x.len() as i32 || x[j as usize].0 != x[i as usize].0 {
444 return j;
445 }
446 j += 1;
447 }
448}
449
450pub unsafe fn resize_without_setting<T>(x: &mut Vec<T>, n: usize) {
463 x.clear();
464 x.reserve(n);
465 x.set_len(n); }
467
468pub fn sort_sync2<T: Ord + Clone, S1: Ord + Clone>(t: &mut Vec<T>, s1: &mut Vec<S1>) {
473 let permutation = permutation::sort(&t[..]);
474 *t = permutation.apply_slice(&t[..]);
475 *s1 = permutation.apply_slice(&s1[..]);
476}
477
478pub fn sort_sync3<T: Ord + Clone, S1: Ord + Clone, S2: Ord + Clone>(
479 t: &mut Vec<T>,
480 s1: &mut Vec<S1>,
481 s2: &mut Vec<S2>,
482) {
483 let permutation = permutation::sort(&t[..]);
484 *t = permutation.apply_slice(&t[..]);
485 *s1 = permutation.apply_slice(&s1[..]);
486 *s2 = permutation.apply_slice(&s2[..]);
487}
488
489pub fn sort_sync4<T: Ord + Clone, S1: Ord + Clone, S2: Ord + Clone, S3: Ord + Clone>(
490 t: &mut Vec<T>,
491 s1: &mut Vec<S1>,
492 s2: &mut Vec<S2>,
493 s3: &mut Vec<S3>,
494) {
495 let permutation = permutation::sort(&t[..]);
496 *t = permutation.apply_slice(&t[..]);
497 *s1 = permutation.apply_slice(&s1[..]);
498 *s2 = permutation.apply_slice(&s2[..]);
499 *s3 = permutation.apply_slice(&s3[..]);
500}
501
502pub fn sort_sync5<
503 T: Ord + Clone,
504 S1: Ord + Clone,
505 S2: Ord + Clone,
506 S3: Ord + Clone,
507 S4: Ord + Clone,
508>(
509 t: &mut Vec<T>,
510 s1: &mut Vec<S1>,
511 s2: &mut Vec<S2>,
512 s3: &mut Vec<S3>,
513 s4: &mut Vec<S4>,
514) {
515 let permutation = permutation::sort(&t[..]);
516 *t = permutation.apply_slice(&t[..]);
517 *s1 = permutation.apply_slice(&s1[..]);
518 *s2 = permutation.apply_slice(&s2[..]);
519 *s3 = permutation.apply_slice(&s3[..]);
520 *s4 = permutation.apply_slice(&s4[..]);
521}
522
523pub fn sort_sync6<
524 T: Ord + Clone,
525 S1: Ord + Clone,
526 S2: Ord + Clone,
527 S3: Ord + Clone,
528 S4: Ord + Clone,
529 S5: Ord + Clone,
530>(
531 t: &mut Vec<T>,
532 s1: &mut Vec<S1>,
533 s2: &mut Vec<S2>,
534 s3: &mut Vec<S3>,
535 s4: &mut Vec<S4>,
536 s5: &mut Vec<S5>,
537) {
538 let permutation = permutation::sort(&t[..]);
539 *t = permutation.apply_slice(&t[..]);
540 *s1 = permutation.apply_slice(&s1[..]);
541 *s2 = permutation.apply_slice(&s2[..]);
542 *s3 = permutation.apply_slice(&s3[..]);
543 *s4 = permutation.apply_slice(&s4[..]);
544 *s5 = permutation.apply_slice(&s5[..]);
545}
546
547pub fn sort_sync7<
548 T: Ord + Clone,
549 S1: Ord + Clone,
550 S2: Ord + Clone,
551 S3: Ord + Clone,
552 S4: Ord + Clone,
553 S5: Ord + Clone,
554 S6: Ord + Clone,
555>(
556 t: &mut Vec<T>,
557 s1: &mut Vec<S1>,
558 s2: &mut Vec<S2>,
559 s3: &mut Vec<S3>,
560 s4: &mut Vec<S4>,
561 s5: &mut Vec<S5>,
562 s6: &mut Vec<S6>,
563) {
564 let permutation = permutation::sort(&t[..]);
565 *t = permutation.apply_slice(&t[..]);
566 *s1 = permutation.apply_slice(&s1[..]);
567 *s2 = permutation.apply_slice(&s2[..]);
568 *s3 = permutation.apply_slice(&s3[..]);
569 *s4 = permutation.apply_slice(&s4[..]);
570 *s5 = permutation.apply_slice(&s5[..]);
571 *s6 = permutation.apply_slice(&s6[..]);
572}