Orbiter 2022
Combinatorial Objects
sorting.cpp
Go to the documentation of this file.
1// sorting.cpp
2//
3// Anton Betten
4//
5// moved out of util.cpp: 11/12/07
6
7
8
9
10#include "foundations.h"
11
12using namespace std;
13
14
15namespace orbiter {
16namespace layer1_foundations {
17namespace data_structures {
18
19
20static void int_vec_partition(int *v,
21 int (*compare_func)(int a, int b),
22 int left, int right, int *middle);
23static void lint_vec_partition(long int *v,
24 int (*compare_func)(long int a, long int b), int left, int right, int *middle);
25static void partition(void **v, int *perm,
26 int (*compare_func)(void *a, void *b, void *data), void *data,
27 int left, int right, int *middle);
28static void quicksort(void **v, int *perm,
29 int (*compare_func)(void *a, void *b, void *data), void *data,
30 int left, int right);
31
32static int compare_increasingly_int(int a, int b);
33static int compare_decreasingly_int(int a, int b);
34static int compare_increasingly_lint(long int a, long int b);
35static int compare_decreasingly_lint(long int a, long int b);
36
37
39{
40
41}
42
44{
45
46}
47
49 int *v, int len, int *A, int A_sz, int *Idx)
50{
51 int i;
52
53 for (i = 0; i < A_sz; i++) {
54 if (!int_vec_search(v, len, A[i], Idx[i])) {
55 cout << "sorting::int_vec_search_vec did not find entry" << endl;
56 exit(1);
57 }
58 }
59}
60
62 long int *v, int len, long int *A, int A_sz, long int *Idx)
63{
64 int i, idx;
65
66 for (i = 0; i < A_sz; i++) {
67 if (!lint_vec_search(v, len, A[i], idx, 0)) {
68 cout << "sorting::int_vec_search_vec did not find entry" << endl;
69 exit(1);
70 }
71 Idx[i] = idx;
72 }
73}
74
76 int *v, int len, int *A, int A_sz, int *Idx)
77{
78 int i;
79
80 for (i = 0; i < A_sz; i++) {
81 if (!int_vec_search_linear(v, len, A[i], Idx[i])) {
82 cout << "int_vec_search_vec did not find entry" << endl;
83 exit(1);
84 }
85 }
86}
87
89 long int *v, int len, long int *A, int A_sz, long int *Idx)
90{
91 int i, idx;
92
93 for (i = 0; i < A_sz; i++) {
94 if (!lint_vec_search_linear(v, len, A[i], idx)) {
95 cout << "lint_vec_search_vec did not find entry" << endl;
96 exit(1);
97 }
98 Idx[i] = idx;
99 }
100}
101
103 int *set, int sz, int *big_set, int big_set_sz)
104{
105 int i, j, a;
106
107 j = 0;
108 for (i = 0; i < sz; i++) {
109 a = set[i];
110 while (big_set[j] < a && j < big_set_sz) {
111 j++;
112 }
113 if (j == big_set_sz) {
114 return FALSE;
115 }
116 if (big_set[j] == a) {
117 j++;
118 continue;
119 }
120 return FALSE;
121 }
122 return TRUE;
123}
124
126 int *set, int sz, long int *big_set, int big_set_sz, int verbose_level)
127{
128 long int i, j, a;
129 int f_v = (verbose_level >= 1);
130
131 j = 0;
132 for (i = 0; i < sz; i++) {
133 a = set[i];
134 while (big_set[j] < a && j < big_set_sz) {
135 j++;
136 }
137 if (j == big_set_sz) {
138 return FALSE;
139 }
140 if (big_set[j] == a) {
141 j++;
142 if (f_v) {
143 cout << "element " << a << " has been found" << endl;
144 }
145 continue;
146 }
147 return FALSE;
148 }
149 return TRUE;
150}
151
153 int *list, int *list_inv, int idx1, int idx2)
154{
155 int p1, p2;
156
157 if (idx1 == idx2) {
158 return;
159 }
160 p1 = list[idx1];
161 p2 = list[idx2];
162 list[idx1] = p2;
163 list[idx2] = p1;
164 list_inv[p1] = idx2;
165 list_inv[p2] = idx1;
166}
167
168int sorting::int_vec_is_sorted(int *v, int len)
169{
170 int i;
171
172 for (i = 1; i < len; i++) {
173 if (v[i - 1] > v[i]) {
174 return FALSE;
175 }
176 }
177 return TRUE;
178}
179
181{
182 int i, j;
183
184 int_vec_heapsort(v, len);
185 for (i = len - 1; i > 0; i--) {
186 if (v[i] == v[i - 1]) {
187 for (j = i + 1; j < len; j++) {
188 v[j - 1] = v[j];
189 }
190 len--;
191 }
192 }
193}
194
196{
197 int i, j;
198
199 lint_vec_heapsort(v, len);
200 for (i = len - 1; i > 0; i--) {
201 if (v[i] == v[i - 1]) {
202 for (j = i + 1; j < len; j++) {
203 v[j - 1] = v[j];
204 }
205 len--;
206 }
207 }
208}
209
211 int *v1, int len1, int *v2, int len2)
212{
213 int i, j;
214
215 int_vec_heapsort(v1, len1);
216 int_vec_heapsort(v2, len2);
217 for (i = 0, j = 0; i < len1; ) {
218 if (j == len2) {
219 return FALSE;
220 }
221 if (v1[i] == v2[j]) {
222 i++;
223 j++;
224 }
225 else if (v1[i] > v2[j]) {
226 j++;
227 }
228 else if (v1[i] < v2[j]) {
229 return FALSE;
230 }
231 }
232 return TRUE;
233}
234
236 long int *v1, int len1, long int *v2, int len2)
237{
238 int i, j;
239
240 lint_vec_heapsort(v1, len1);
241 lint_vec_heapsort(v2, len2);
242 for (i = 0, j = 0; i < len1; ) {
243 if (j == len2) {
244 return FALSE;
245 }
246 if (v1[i] == v2[j]) {
247 i++;
248 j++;
249 }
250 else if (v1[i] > v2[j]) {
251 j++;
252 }
253 else if (v1[i] < v2[j]) {
254 return FALSE;
255 }
256 }
257 return TRUE;
258}
259
260int sorting::int_vecs_are_disjoint(int *v1, int len1, int *v2, int len2)
261{
262 int i, j;
263
264 i = 0;
265 j = 0;
266 while (TRUE) {
267 if (i == len1) {
268 break;
269 }
270 if (j == len2) {
271 break;
272 }
273 if (v1[i] == v2[j]) {
274 return FALSE;
275 }
276 if (v1[i] < v2[j]) {
277 i++;
278 }
279 else if (v1[i] > v2[j]) {
280 j++;
281 }
282 }
283 return TRUE;
284}
285
287 int *v2, int len2, int &idx1, int &idx2)
288{
289 int i, j;
290
291 i = 0;
292 j = 0;
293 while (TRUE) {
294 if (i == len1) {
295 break;
296 }
297 if (j == len2) {
298 break;
299 }
300 if (v1[i] == v2[j]) {
301 idx1 = i;
302 idx2 = j;
303 return TRUE;
304 }
305 if (v1[i] < v2[j]) {
306 i++;
307 }
308 else if (v1[i] > v2[j]) {
309 j++;
310 }
311 }
312 return FALSE;
313}
314
315int sorting::lint_vecs_find_common_element(long int *v1, int len1,
316 long int *v2, int len2, int &idx1, int &idx2)
317{
318 int i, j;
319
320 i = 0;
321 j = 0;
322 while (TRUE) {
323 if (i == len1) {
324 break;
325 }
326 if (j == len2) {
327 break;
328 }
329 if (v1[i] == v2[j]) {
330 idx1 = i;
331 idx2 = j;
332 return TRUE;
333 }
334 if (v1[i] < v2[j]) {
335 i++;
336 }
337 else if (v1[i] > v2[j]) {
338 j++;
339 }
340 }
341 return FALSE;
342}
343
345 int *&vec, int &used_length,
346 int &alloc_length, int a,
347 int verbose_level)
348{
349 int f_v = (verbose_level >= 1);
350 int f_vv = (verbose_level >= 2);
351 int idx, t;
352
353 if (f_v) {
354 cout << "int_vec_insert_and_reallocate_if_necessary" << endl;
355 }
356 if (int_vec_search(vec, used_length, a, idx)) {
357 if (f_vv) {
358 cout << "int_vec_insert_and_reallocate_if_necessary "
359 "element " << a << " is already in the list" << endl;
360 }
361 }
362 else {
363 if (used_length == alloc_length) {
364 int *C;
365 int new_alloc_length;
366
367 new_alloc_length = 2 * alloc_length;
368 cout << "reallocating to length " << new_alloc_length << endl;
369 C = NEW_int(new_alloc_length);
370 for (t = 0; t < used_length; t++) {
371 C[t] = vec[t];
372 }
373 FREE_int(vec);
374 vec = C;
375 alloc_length = new_alloc_length;
376 }
377 for (t = used_length; t > idx; t--) {
378 vec[t] = vec[t - 1];
379 }
380 vec[idx] = a;
381 used_length++;
382 if (FALSE) {
383 cout << "element " << a << " has been added to the "
384 "list at position " << idx << " n e w length = "
385 << used_length << endl;
386 }
387 if (f_v) {
388 if ((used_length & (1024 - 1)) == 0) {
389 cout << "used_length = " << used_length << endl;
390 }
391 }
392 }
393 if (f_v) {
394 cout << "int_vec_insert_and_reallocate_if_necessary done" << endl;
395 }
396}
397
399 int &used_length, int &alloc_length, int a,
400 int verbose_level)
401{
402 int f_v = (verbose_level >= 1);
403 //int f_vv = (verbose_level >= 2);
404 int t;
405
406 if (f_v) {
407 cout << "int_vec_append_and_reallocate_if_necessary" << endl;
408 }
409 if (used_length == alloc_length) {
410 int *C;
411 int new_alloc_length;
412
413 new_alloc_length = 2 * alloc_length;
414 cout << "reallocating to length " << new_alloc_length << endl;
415 C = NEW_int(new_alloc_length);
416 for (t = 0; t < used_length; t++) {
417 C[t] = vec[t];
418 }
419 FREE_int(vec);
420 vec = C;
421 alloc_length = new_alloc_length;
422 }
423 vec[used_length] = a;
424 used_length++;
425 if (FALSE) {
426 cout << "element " << a << " has been appended to the list "
427 "at position " << used_length - 1 << " n e w "
428 "length = " << used_length << endl;
429 }
430 if (f_v) {
431 if ((used_length & (1024 - 1)) == 0) {
432 cout << "used_length = " << used_length << endl;
433 }
434 }
435 if (f_v) {
436 cout << "int_vec_append_and_reallocate_if_necessary "
437 "done" << endl;
438 }
439}
440
442 int &used_length, int &alloc_length, long int a,
443 int verbose_level)
444{
445 int f_v = (verbose_level >= 1);
446 //int f_vv = (verbose_level >= 2);
447 int t;
448
449 if (f_v) {
450 cout << "lint_vec_append_and_reallocate_if_necessary" << endl;
451 }
452 if (used_length == alloc_length) {
453 long int *C;
454 int new_alloc_length;
455
456 new_alloc_length = 2 * alloc_length;
457 cout << "reallocating to length " << new_alloc_length << endl;
458 C = NEW_lint(new_alloc_length);
459 for (t = 0; t < used_length; t++) {
460 C[t] = vec[t];
461 }
462 FREE_lint(vec);
463 vec = C;
464 alloc_length = new_alloc_length;
465 }
466 vec[used_length] = a;
467 used_length++;
468 if (FALSE) {
469 cout << "element " << a << " has been appended to the list "
470 "at position " << used_length - 1 << " n e w "
471 "length = " << used_length << endl;
472 }
473 if (f_v) {
474 if ((used_length & (1024 - 1)) == 0) {
475 cout << "used_length = " << used_length << endl;
476 }
477 }
478 if (f_v) {
479 cout << "lint_vec_append_and_reallocate_if_necessary "
480 "done" << endl;
481 }
482}
483
484int sorting::int_vec_is_zero(int *v, int len)
485{
486 int i;
487
488 for (i = 0; i < len; i++) {
489 if (v[i]) {
490 return FALSE;
491 }
492 }
493 return TRUE;
494}
495
496int sorting::test_if_sets_are_equal(int *set1, int *set2, int set_size)
497{
498 int *S1, *S2;
499 int i;
500
501 S1 = NEW_int(set_size);
502 S2 = NEW_int(set_size);
503 Int_vec_copy(set1, S1, set_size);
504 Int_vec_copy(set2, S2, set_size);
505 int_vec_heapsort(S1, set_size);
506 int_vec_heapsort(S2, set_size);
507 for (i = 0; i < set_size; i++) {
508 if (S1[i] != S2[i]) {
509 FREE_int(S1);
510 FREE_int(S2);
511 return FALSE;
512 }
513 }
514 FREE_int(S1);
515 FREE_int(S2);
516 return TRUE;
517}
518
519int sorting::test_if_sets_are_disjoint(long int *set1, int sz1, long int *set2, int sz2)
520{
521 long int *S1, *S2;
522 int i, idx;
523 long int a;
524
525 S1 = NEW_lint(sz1);
526 S2 = NEW_lint(sz2);
527 Lint_vec_copy(set1, S1, sz1);
528 Lint_vec_copy(set2, S2, sz2);
529 lint_vec_heapsort(S1, sz1);
530 lint_vec_heapsort(S2, sz2);
531 for (i = 0; i < sz1; i++) {
532 a = set1[i];
533 if (lint_vec_search(S2, sz2, a, idx, 0 /*verbose_level*/)) {
534 FREE_lint(S1);
535 FREE_lint(S2);
536 return FALSE;
537 }
538 }
539 FREE_lint(S1);
540 FREE_lint(S2);
541 return TRUE;
542}
543
544void sorting::test_if_set(int *set, int set_size)
545{
546 int *S;
547 int i;
548
549 S = NEW_int(set_size);
550 for (i = 0; i < set_size; i++) {
551 S[i] = set[i];
552 }
553 int_vec_heapsort(S, set_size);
554 for (i = 0; i < set_size - 1; i++) {
555 if (S[i] == S[i + 1]) {
556 cout << "the set is not a set: the element "
557 << S[i] << " is repeated" << endl;
558 exit(1);
559 }
560 }
561 FREE_int(S);
562}
563
564int sorting::test_if_set_with_return_value(int *set, int set_size)
565{
566 int *S;
567 int i;
568
569 S = NEW_int(set_size);
570 for (i = 0; i < set_size; i++) {
571 S[i] = set[i];
572 }
573 int_vec_heapsort(S, set_size);
574 for (i = 0; i < set_size - 1; i++) {
575 if (S[i] == S[i + 1]) {
576 cout << "the set is not a set: the element "
577 << S[i] << " is repeated" << endl;
578 FREE_int(S);
579 return FALSE;
580 }
581 }
582 FREE_int(S);
583 return TRUE;
584}
585
586int sorting::test_if_set_with_return_value_lint(long int *set, int set_size)
587{
588 int *S;
589 int i;
590
591 S = NEW_int(set_size);
592 for (i = 0; i < set_size; i++) {
593 S[i] = set[i];
594 }
595 int_vec_heapsort(S, set_size);
596 for (i = 0; i < set_size - 1; i++) {
597 if (S[i] == S[i + 1]) {
598 cout << "the set is not a set: the element "
599 << S[i] << " is repeated" << endl;
600 FREE_int(S);
601 return FALSE;
602 }
603 }
604 FREE_int(S);
605 return TRUE;
606}
607
609 int *set, int *subset, int *rearranged_set,
610 int verbose_level)
611{
612 int f_v = (verbose_level >= 1);
613 int i, j = 0;
614
615 for (i = 0; i < n; i++) {
616 if (j < k && subset[j] == i) {
617 rearranged_set[j] = set[subset[j]];
618 j++;
619 }
620 else {
621 rearranged_set[k + i - j] = set[i];
622 }
623 }
624 if (f_v) {
625 cout << "rearrange_subset ";
626 Int_vec_print(cout, rearranged_set, n);
627 cout << endl;
628#if 0
629 cout << "rearrange_subset subset=";
630 int_vec_print(cout, set, n);
631 cout << " : ";
632 int_vec_print(cout, subset, k);
633 cout << " : ";
634 int_vec_print(cout, rearranged_set, n);
635 cout << endl;
636#endif
637 }
638}
639
641 long int *set, int *subset, long int *rearranged_set,
642 int verbose_level)
643{
644 int f_v = (verbose_level >= 1);
645 int i, j = 0;
646
647 for (i = 0; i < n; i++) {
648 if (j < k && subset[j] == i) {
649 rearranged_set[j] = set[subset[j]];
650 j++;
651 }
652 else {
653 rearranged_set[k + i - j] = set[i];
654 }
655 }
656 if (f_v) {
657 cout << "rearrange_subset ";
658 Lint_vec_print(cout, rearranged_set, n);
659 cout << endl;
660 }
661}
662
664 long int *set, long int *subset, long int *rearranged_set,
665 int verbose_level)
666{
667 int f_v = (verbose_level >= 1);
668 int i, j = 0;
669
670 for (i = 0; i < n; i++) {
671 if (j < k && subset[j] == i) {
672 rearranged_set[j] = set[subset[j]];
673 j++;
674 }
675 else {
676 rearranged_set[k + i - j] = set[i];
677 }
678 }
679 if (f_v) {
680 cout << "rearrange_subset ";
681 Lint_vec_print(cout, rearranged_set, n);
682 cout << endl;
683 }
684}
685
686int sorting::int_vec_search_linear(int *v, int len, int a, int &idx)
687{
688 int i;
689
690 for (i = 0; i < len; i++) {
691 if (v[i] == a) {
692 idx = i;
693 return TRUE;
694 }
695 }
696 return FALSE;
697}
698
699int sorting::lint_vec_search_linear(long int *v, int len, long int a, int &idx)
700{
701 int i;
702
703 for (i = 0; i < len; i++) {
704 if (v[i] == a) {
705 idx = i;
706 return TRUE;
707 }
708 }
709 return FALSE;
710}
711
712void sorting::int_vec_intersect(int *v1, int len1,
713 int *v2, int len2, int *&v3, int &len3)
714{
715 int *V1, *V2;
716 int i, a, idx;
717
718 V1 = NEW_int(len1);
719 V2 = NEW_int(len2);
720 for (i = 0; i < len1; i++) {
721 V1[i] = v1[i];
722 }
723 for (i = 0; i < len2; i++) {
724 V2[i] = v2[i];
725 }
726 int_vec_heapsort(V1, len1);
727 int_vec_heapsort(V2, len2);
728 v3 = NEW_int(MAXIMUM(len1, len2));
729 len3 = 0;
730 for (i = 0; i < len1; i++) {
731 a = V1[i];
732 if (int_vec_search(V2, len2, a, idx)) {
733 v3[len3++] = a;
734 }
735 }
736
737 FREE_int(V1);
738 FREE_int(V2);
739}
740
741void sorting::vec_intersect(long int *v1, int len1,
742 long int *v2, int len2, long int *&v3, int &len3)
743{
744 long int *V1, *V2;
745 long int i, a;
746 int idx;
747
748 V1 = NEW_lint(len1);
749 V2 = NEW_lint(len2);
750 for (i = 0; i < len1; i++) {
751 V1[i] = v1[i];
752 }
753 for (i = 0; i < len2; i++) {
754 V2[i] = v2[i];
755 }
756 lint_vec_heapsort(V1, len1);
757 lint_vec_heapsort(V2, len2);
758 v3 = NEW_lint(MAXIMUM(len1, len2));
759 len3 = 0;
760 for (i = 0; i < len1; i++) {
761 a = V1[i];
762 if (lint_vec_search(V2, len2, a, idx, 0 /* verbose_level */)) {
763 v3[len3++] = a;
764 }
765 }
766
767 FREE_lint(V1);
768 FREE_lint(V2);
769}
770
772 int *v2, int len2, int *v3, int &len3)
773{
774 int i, j, a, b;
775
776 len3 = 0;
777 i = 0;
778 j = 0;
779 while (TRUE) {
780 if (i >= len1 || j >= len2) {
781 break;
782 }
783 a = v1[i];
784 b = v2[j];
785
786 if (a == b) {
787 v3[len3++] = a;
788 i++;
789 j++;
790 }
791 else if (a < b) {
792 i++;
793 }
794 else {
795 j++;
796 }
797 }
798}
799
801 long int *v2, int len2, long int *v3, int &len3)
802{
803 int i, j, a, b;
804
805 len3 = 0;
806 i = 0;
807 j = 0;
808 while (TRUE) {
809 if (i >= len1 || j >= len2) {
810 break;
811 }
812 a = v1[i];
813 b = v2[j];
814
815 if (a == b) {
816 v3[len3++] = a;
817 i++;
818 j++;
819 }
820 else if (a < b) {
821 i++;
822 }
823 else {
824 j++;
825 }
826 }
827}
828
829
831 int *perm, int *perm_inv, int f_increasingly)
832// perm and perm_inv must be allocated to len elements
833{
834#if 0
835 int i;
836 int *pairs;
837 pint *V;
838
839 pairs = NEW_int(len * 2);
840 V = NEW_pint(len);
841 for (i = 0; i < len; i++) {
842 pairs[i * 2 + 0] = v[i];
843 pairs[i * 2 + 1] = i;
844 V[i] = pairs + i * 2;
845 }
846 if (f_increasingly) {
847 quicksort_array(len, (void **)V, int_compare_increasingly, NULL);
848 }
849 else {
850 quicksort_array(len, (void **)V, int_compare_decreasingly, NULL);
851 }
852 for (i = 0; i < len; i++) {
853 perm_inv[i] = V[i][1];
854 }
855 perm_inverse(perm_inv, perm, len);
856
857 FREE_int(V);
858 FREE_pint(pairs);
859#else
860 int i;
862
863 for (i = 0; i < len; i++) {
864 perm_inv[i] = i;
865 }
866 int_vec_heapsort_with_log(v, perm_inv, len);
867 if (!f_increasingly) {
868 int n2 = len >> 1;
869 int a;
870
871 for (i = 0; i < n2; i++) {
872 a = v[i];
873 v[i] = v[len - 1 - i];
874 v[len - 1 - i] = a;
875 a = perm_inv[i];
876 perm_inv[i] = perm_inv[len - 1 - i];
877 perm_inv[len - 1 - i] = a;
878 }
879 }
880 Combi.perm_inverse(perm_inv, perm, len);
881#endif
882}
883
885 int (*compare_func)(int a, int b), int left, int right)
886{
887 int middle;
888
889 if (left < right) {
890 int_vec_partition(v, compare_func, left, right, &middle);
891 int_vec_quicksort(v, compare_func, left, middle - 1);
892 int_vec_quicksort(v, compare_func, middle + 1, right);
893 }
894}
895
897 int (*compare_func)(long int a, long int b), int left, int right)
898{
899 int middle;
900
901 if (left < right) {
902 lint_vec_partition(v, compare_func, left, right, &middle);
903 lint_vec_quicksort(v, compare_func, left, middle - 1);
904 lint_vec_quicksort(v, compare_func, middle + 1, right);
905 }
906}
907
909{
910 int_vec_quicksort(v, compare_increasingly_int, 0, len - 1);
911}
912
914{
915 int_vec_quicksort(v, compare_decreasingly_int, 0, len - 1);
916}
917
919{
920 lint_vec_quicksort(v, compare_increasingly_lint, 0, len - 1);
921}
922
924{
925 lint_vec_quicksort(v, compare_decreasingly_lint, 0, len - 1);
926}
927
928
929void sorting::quicksort_array(int len, void **v,
930 int (*compare_func)(void *a, void *b, void *data), void *data)
931{
932 if (len <= 1)
933 return;
934 quicksort(v, NULL, compare_func, data, 0, len - 1);
935}
936
937void sorting::quicksort_array_with_perm(int len, void **v, int *perm,
938 int (*compare_func)(void *a, void *b, void *data), void *data)
939{
940 if (len <= 1)
941 return;
942 quicksort(v, perm, compare_func, data, 0, len - 1);
943}
944
946 int (*compare_func)(void *a, void *b, void *data),
947 void *data_for_compare,
948 int len, void *a, int &idx, int verbose_level)
949{
950 int l, r, m, res;
951 int f_found = FALSE;
952 int f_v = (verbose_level >= 1);
953
954 if (f_v) {
955 cout << "sorting::vec_search len=" << len << endl;
956 }
957 idx = 0;
958 if (len == 0) {
959 idx = 0;
960 return FALSE;
961 }
962 l = 0;
963 r = len;
964 // invariant:
965 // v[i] <= a for i < l;
966 // v[i] > a for i >= r;
967 // r - l is the length of the area to search in.
968 while (l < r) {
969 if (f_v) {
970 cout << "sorting::vec_search l=" << l << " r=" << r << endl;
971 }
972 m = (l + r) >> 1;
973 // if the length of the search area is even
974 // we examine the element above the middle
975 res = (*compare_func)(a, v[m], data_for_compare);
976 if (f_v) {
977 cout << "sorting::vec_search m=" << m << " res=" << res << endl;
978 }
979 //res = v[m] - a;
980 //cout << "search l=" << l << " m=" << m << " r="
981 // << r << "a=" << a << " v[m]=" << v[m] << " res=" << res << endl;
982 if (res <= 0) {
983 l = m + 1;
984 if (res == 0) {
985 f_found = TRUE;
986 }
987 }
988 else {
989 r = m;
990 }
991 }
992 // now: l == r;
993 // and f_found is set accordingly */
994 if (f_found) {
995 l--;
996 }
997 idx = l;
998 if (f_v) {
999 cout << "sorting::vec_search done, "
1000 "f_found=" << f_found << " idx=" << idx << endl;
1001 }
1002 return f_found;
1003}
1004
1006 int (*compare_func)(void *vec, void *a, int b, void *data_for_compare),
1007 void *data_for_compare,
1008 int len, void *a, int &idx, int verbose_level)
1009{
1010 int l, r, m, res;
1011 int f_found = FALSE;
1012 int f_v = (verbose_level >= 1);
1013
1014 if (f_v) {
1015 cout << "vec_search_general len=" << len << endl;
1016 }
1017 if (len == 0) {
1018 idx = 0;
1019 return FALSE;
1020 }
1021 l = 0;
1022 r = len;
1023 // invariant:
1024 // v[i] <= a for i < l;
1025 // v[i] > a for i >= r;
1026 // r - l is the length of the area to search in.
1027 while (l < r) {
1028 if (f_v) {
1029 cout << "vec_search_general l=" << l << " r=" << r << endl;
1030 }
1031 m = (l + r) >> 1;
1032 // if the length of the search area is even
1033 // we examine the element above the middle
1034 res = (*compare_func)(vec, a, m, data_for_compare);
1035 if (f_v) {
1036 cout << "m=" << m << " res=" << res << endl;
1037 }
1038 //res = v[m] - a;
1039 //cout << "vec_search_general l=" << l << " m=" << m << " r="
1040 // << r << "a=" << a << " v[m]=" << v[m] << " res=" << res << endl;
1041 if (res <= 0) {
1042 l = m + 1;
1043 if (res == 0) {
1044 f_found = TRUE;
1045 }
1046 }
1047 else {
1048 r = m;
1049 }
1050 }
1051 // now: l == r;
1052 // and f_found is set accordingly */
1053 if (f_found) {
1054 l--;
1055 }
1056 idx = l;
1057 return f_found;
1058}
1059
1061{
1062 int idx, t;
1063
1064 if (!int_vec_search(v, len, a, idx)) {
1065 for (t = len - 1; t >= idx; t--) {
1066 v[t + 1] = v[t];
1067 }
1068 v[idx] = a;
1069 len++;
1070 return TRUE;
1071 }
1072 else {
1073 return FALSE;
1074 }
1075}
1076
1078{
1079 int idx, t;
1080
1081 if (int_vec_search(v, len, a, idx)) {
1082 for (t = idx; t < len - 1; t++) {
1083 v[t] = v[t + 1];
1084 }
1085 len--;
1086 return TRUE;
1087 }
1088 else {
1089 return FALSE;
1090 }
1091}
1092
1093
1094int sorting::int_vec_search(int *v, int len, int a, int &idx)
1095// This function finds the last occurrence of the element a.
1096// If a is not found, it returns in idx the position
1097// where it should be inserted if
1098// the vector is assumed to be in increasing order.
1099
1100{
1101 int l, r, m, res;
1102 int f_found = FALSE;
1103 int f_v = FALSE;
1104
1105 if (len == 0) {
1106 idx = 0;
1107 return FALSE;
1108 }
1109 l = 0;
1110 r = len;
1111 // invariant:
1112 // v[i] <= a for i < l;
1113 // v[i] > a for i >= r;
1114 // r - l is the length of the area to search in.
1115 while (l < r) {
1116 m = (l + r) >> 1;
1117 // if the length of the search area is even
1118 // we examine the element above the middle
1119 res = v[m] - a;
1120 if (f_v) {
1121 cout << "l=" << l << " r=" << r<< " m=" << m
1122 << " v[m]=" << v[m] << " res=" << res << endl;
1123 }
1124 //cout << "search l=" << l << " m=" << m << " r="
1125 // << r << "a=" << a << " v[m]=" << v[m] << " res=" << res << endl;
1126 // so, res is
1127 // positive if v[m] > a,
1128 // zero if v[m] == a,
1129 // negative if v[m] < a
1130 if (res <= 0) {
1131 l = m + 1;
1132 if (f_v) {
1133 cout << "moving to the right" << endl;
1134 }
1135 if (res == 0) {
1136 f_found = TRUE;
1137 }
1138 }
1139 else {
1140 if (f_v) {
1141 cout << "moving to the left" << endl;
1142 }
1143 r = m;
1144 }
1145 }
1146 // now: l == r;
1147 // and f_found is set accordingly */
1148#if 1
1149 if (f_found) {
1150 l--;
1151 }
1152#endif
1153 idx = l;
1154 return f_found;
1155}
1156
1157int sorting::lint_vec_search(long int *v, int len,
1158 long int a, int &idx, int verbose_level)
1159// This function finds the last occurrence of the element a.
1160// If a is not found, it returns in idx the position
1161// where it should be inserted if
1162// the vector is assumed to be in increasing order.
1163
1164{
1165 int f_v = (verbose_level >= 1);
1166 int l, r, m;
1167 long int res;
1168 int f_found = FALSE;
1169
1170 if (f_v) {
1171 cout << "sorting::lint_vec_search len=" << len << ", searching for " << a << endl;
1172 }
1173 if (len == 0) {
1174 idx = 0;
1175 return FALSE;
1176 }
1177 l = 0;
1178 r = len;
1179 // invariant:
1180 // v[i] <= a for i < l;
1181 // v[i] > a for i >= r;
1182 // r - l is the length of the area to search in.
1183 while (l < r) {
1184 m = (l + r) >> 1;
1185 if (f_v) {
1186 cout << "sorting::lint_vec_search l=" << l << " m=" << m << " r=" << r << endl;
1187 }
1188 // if the length of the search area is even
1189 // we examine the element above the middle
1190 res = v[m] - a;
1191 if (f_v) {
1192 cout << "sorting::lint_vec_search v[m]=" << v[m] << " a=" << a << endl;
1193 }
1194 //cout << "search l=" << l << " m=" << m << " r="
1195 // << r << "a=" << a << " v[m]=" << v[m] << " res=" << res << endl;
1196 // so, res is
1197 // positive if v[m] > a,
1198 // zero if v[m] == a,
1199 // negative if v[m] < a
1200 if (res <= 0) {
1201 l = m + 1;
1202 if (f_v) {
1203 cout << "moving to the right" << endl;
1204 }
1205 if (res == 0) {
1206 if (f_v) {
1207 cout << "f_found = TRUE" << endl;
1208 }
1209 f_found = TRUE;
1210 }
1211 }
1212 else {
1213 if (f_v) {
1214 cout << "moving to the left" << endl;
1215 }
1216 r = m;
1217 }
1218 }
1219 // now: l == r;
1220 // and f_found is set accordingly */
1221#if 1
1222 if (f_found) {
1223 l--;
1224 }
1225#endif
1226 idx = l;
1227 if (f_v) {
1228 cout << "sorting::lint_vec_search len=" << len << ", searching "
1229 "for " << a << " done, f_found=" << f_found
1230 << " idx=" << idx << endl;
1231 }
1232 return f_found;
1233}
1234
1235int sorting::vector_lint_search(std::vector<long int> &v,
1236 long int a, int &idx, int verbose_level)
1237// This function finds the last occurence of the element a.
1238// If a is not found, it returns in idx the position
1239// where it should be inserted if
1240// the vector is assumed to be in increasing order.
1241
1242{
1243 int f_v = (verbose_level >= 1);
1244 int len;
1245 int l, r, m;
1246 long int res;
1247 int f_found = FALSE;
1248
1249 len = v.size();
1250 if (f_v) {
1251 cout << "sorting::vector_lint_search len=" << len << ", searching for " << a << endl;
1252 }
1253 if (len == 0) {
1254 idx = 0;
1255 return FALSE;
1256 }
1257 l = 0;
1258 r = len;
1259 // invariant:
1260 // v[i] <= a for i < l;
1261 // v[i] > a for i >= r;
1262 // r - l is the length of the area to search in.
1263 while (l < r) {
1264 m = (l + r) >> 1;
1265 if (f_v) {
1266 cout << "sorting::vector_lint_search l=" << l << " m=" << m << " r=" << r << endl;
1267 }
1268 // if the length of the search area is even
1269 // we examine the element above the middle
1270 res = v[m] - a;
1271 if (f_v) {
1272 cout << "sorting::vector_lint_search v[m]=" << v[m] << " a=" << a << endl;
1273 }
1274 //cout << "search l=" << l << " m=" << m << " r="
1275 // << r << "a=" << a << " v[m]=" << v[m] << " res=" << res << endl;
1276 // so, res is
1277 // positive if v[m] > a,
1278 // zero if v[m] == a,
1279 // negative if v[m] < a
1280 if (res <= 0) {
1281 l = m + 1;
1282 if (f_v) {
1283 cout << "moving to the right" << endl;
1284 }
1285 if (res == 0) {
1286 if (f_v) {
1287 cout << "f_found = TRUE" << endl;
1288 }
1289 f_found = TRUE;
1290 }
1291 }
1292 else {
1293 if (f_v) {
1294 cout << "moving to the left" << endl;
1295 }
1296 r = m;
1297 }
1298 }
1299 // now: l == r;
1300 // and f_found is set accordingly */
1301#if 1
1302 if (f_found) {
1303 l--;
1304 }
1305#endif
1306 idx = l;
1307 if (f_v) {
1308 cout << "sorting::vector_lint_search len=" << len << ", searching "
1309 "for " << a << " done, f_found=" << f_found
1310 << " idx=" << idx << endl;
1311 }
1312 return f_found;
1313}
1315 int len, int a, int &idx,
1316 int verbose_level)
1317// This function finds the first occurence of the element a.
1318{
1319 int l, r, m; //, res;
1320 int f_found = FALSE;
1321 int f_v = (verbose_level >= 1);
1322
1323 if (f_v) {
1324 cout << "int_vec_search_first_occurence searching for " << a
1325 << " len=" << len << endl;
1326 }
1327 if (len == 0) {
1328 idx = 0;
1329 return FALSE;
1330 }
1331 l = 0;
1332 r = len;
1333 if (f_v) {
1334 cout << "int_vec_search_first_occurence searching for "
1335 << a << " l=" << l << " r=" << r << endl;
1336 }
1337 // invariant:
1338 // v[i] < a for i < l;
1339 // v[i] >= a for i >= r;
1340 // r - l is the length of the area to search in.
1341 while (l < r) {
1342 m = (l + r) >> 1;
1343 // if the length of the search area is even
1344 // we examine the element above the middle
1345 //res = v[m] - a;
1346 if (f_v) {
1347 cout << "int_vec_search_first_occurence l=" << l
1348 << " r=" << r<< " m=" << m << " v[m]=" << v[m] << endl;
1349 //<< " res=" << res << endl;
1350 }
1351 //cout << "search l=" << l << " m=" << m << " r="
1352 // << r << "a=" << a << " v[m]=" << v[m] << " res=" << res << endl;
1353 // so, res is
1354 // positive if v[m] > a,
1355 // zero if v[m] == a,
1356 // negative if v[m] < a
1357 if (v[m] < a /*res < 0*/) {
1358 l = m + 1;
1359 if (f_v) {
1360 cout << "int_vec_search_first_occurence "
1361 "moving to the right" << endl;
1362 }
1363 }
1364 else {
1365 r = m;
1366 if (f_v) {
1367 cout << "int_vec_search_first_occurence "
1368 "moving to the left" << endl;
1369 }
1370 if (v[m] == a /*res == 0*/) {
1371 if (f_v) {
1372 cout << "int_vec_search_first_occurence "
1373 "we found the element" << endl;
1374 }
1375 f_found = TRUE;
1376 }
1377 }
1378 }
1379 // now: l == r;
1380 // and f_found is set accordingly */
1381#if 0
1382 if (f_found) {
1383 l--;
1384 }
1385#endif
1386 idx = l;
1387 if (f_v) {
1388 cout << "int_vec_search_first_occurence done "
1389 "f_found=" << f_found << " idx=" << idx << endl;
1390 }
1391 return f_found;
1392}
1393
1396{
1397 int l, r, m, res;
1398 int f_found = FALSE;
1400
1401 if (len == 0) {
1402 idx = 0;
1403 return FALSE;
1404 }
1405 l = 0;
1406 r = len;
1407 // invariant:
1408 // v[i] <= a for i < l;
1409 // v[i] > a for i >= r;
1410 // r - l is the length of the area to search in.
1411 while (l < r) {
1412 m = (l + r) >> 1;
1413 // if the length of the search area is even
1414 // we examine the element above the middle
1415 res = D.compare(v[m], a);
1416 // so, res is
1417 // positive if v[m] > a,
1418 // zero if v[m] == a,
1419 // negative if v[m] < a
1420
1421 //res = v[m] - a;
1422 //cout << "search l=" << l << " m=" << m << " r="
1423 // << r << "a=" << a << " v[m]=" << v[m] << " res=" << res << endl;
1424 if (res <= 0) {
1425 l = m + 1;
1426 if (res == 0) {
1427 f_found = TRUE;
1428 }
1429 }
1430 else {
1431 r = m;
1432 }
1433 }
1434 // now: l == r;
1435 // and f_found is set accordingly */
1436 if (f_found) {
1437 l--;
1438 }
1439 idx = l;
1440 return f_found;
1441}
1442
1443void sorting::int_vec_classify_and_print(ostream &ost, int *v, int l)
1444{
1445 tally C;
1446 int f_backwards = TRUE;
1447
1448 C.init(v, l, FALSE, 0);
1449 C.print_file(ost, f_backwards);
1450}
1451
1452void sorting::int_vec_values(int *v, int l, int *&w, int &w_len)
1453{
1454 tally C;
1455 //int f_backwards = TRUE;
1456 int i, f, a;
1457
1458 C.init(v, l, FALSE, 0);
1459 w_len = C.nb_types;
1460 w = NEW_int(w_len);
1461 for (i = 0; i < w_len; i++) {
1462 f = C.type_first[i];
1463 a = C.data_sorted[f];
1464 w[i] = a;
1465 }
1466}
1467
1469 int *&w, int &w_len)
1470{
1471 tally C;
1472 //int f_backwards = TRUE;
1473 int i;
1474
1475 C.init(v, l, FALSE, 0);
1476 w_len = C.nb_types;
1477 w = NEW_int(w_len);
1478 for (i = 0; i < w_len; i++) {
1479 w[i] = C.type_len[i];
1480 }
1481}
1482
1484 int *&val, int *&mult, int &nb_values)
1485{
1486 tally C;
1487 //int f_backwards = TRUE;
1488 int i, f, len, a;
1489
1490 C.init(v, l, FALSE, 0);
1491 nb_values = C.nb_types;
1492 val = NEW_int(nb_values);
1493 mult = NEW_int(nb_values);
1494 for (i = 0; i < nb_values; i++) {
1495 f = C.type_first[i];
1496 len = C.type_len[i];
1497 a = C.data_sorted[f];
1498 val[i] = a;
1499 mult[i] = len;
1500 }
1501}
1502
1504 int *the_vec, int *&the_vec_sorted,
1505 int *&sorting_perm, int *&sorting_perm_inv,
1506 int &nb_types, int *&type_first, int *&type_len)
1507{
1508
1509#if 0
1510 if (length == 0) {
1511 cout << "int_vec_classify length is zero" << endl;
1512 exit(1);
1513 }
1514#endif
1515 the_vec_sorted = NEW_int(length);
1516 sorting_perm = NEW_int(length);
1517 sorting_perm_inv = NEW_int(length);
1518 type_first = NEW_int(length);
1519 type_len = NEW_int(length);
1520
1521 int_vec_classify_with_arrays(length, the_vec, the_vec_sorted,
1522 sorting_perm, sorting_perm_inv,
1523 nb_types, type_first, type_len);
1524
1525}
1526
1528 int *the_vec, int *the_vec_sorted,
1529 int *sorting_perm, int *sorting_perm_inv,
1530 int &nb_types, int *type_first, int *type_len)
1531{
1532 int i;
1533
1534 for (i = 0; i < length; i++) {
1535 the_vec_sorted[i] = the_vec[i];
1536 }
1537 int_vec_sorting_permutation(the_vec_sorted,
1538 length, sorting_perm, sorting_perm_inv,
1539 TRUE /* f_increasingly */);
1540 for (i = 0; i < length; i++) {
1541 the_vec_sorted[sorting_perm[i]] = the_vec[i];
1542 }
1543
1544 int_vec_sorted_collect_types(length, the_vec_sorted,
1545 nb_types, type_first, type_len);
1546
1547#if 0
1548 nb_types = 0;
1549 type_first[0] = 0;
1550 type_len[0] = 1;
1551 for (i = 1; i < length; i++) {
1552 if (the_vec_sorted[i] == the_vec_sorted[i - 1]) {
1553 type_len[nb_types]++;
1554 }
1555 else {
1556 type_first[nb_types + 1] =
1557 type_first[nb_types] + type_len[nb_types];
1558 nb_types++;
1559 type_len[nb_types] = 1;
1560 }
1561 }
1562 nb_types++;
1563#endif
1564}
1565
1567 int *the_vec_sorted,
1568 int &nb_types, int *type_first, int *type_len)
1569{
1570 int i;
1571
1572 nb_types = 0;
1573 type_first[0] = 0;
1574 type_len[0] = 0;
1575 if (length == 0) {
1576 return;
1577 }
1578 type_len[0] = 1;
1579 for (i = 1; i < length; i++) {
1580 if (the_vec_sorted[i] == the_vec_sorted[i - 1]) {
1581 type_len[nb_types]++;
1582 }
1583 else {
1584 type_first[nb_types + 1] =
1585 type_first[nb_types] + type_len[nb_types];
1586 nb_types++;
1587 type_len[nb_types] = 1;
1588 }
1589 }
1590 nb_types++;
1591}
1592
1593void sorting::int_vec_print_classified(ostream &ost, int *vec, int len)
1594{
1595 int *the_vec_sorted;
1596 int *sorting_perm;
1597 int *sorting_perm_inv;
1598 int *type_first;
1599 int *type_len;
1600 int nb_types;
1601 //int i, f, l, a;
1602
1603
1604 int_vec_classify(len, vec, the_vec_sorted,
1605 sorting_perm, sorting_perm_inv,
1606 nb_types, type_first, type_len);
1607#if 0
1608 ost << "( ";
1609 for (i = 0; i < nb_types; i++) {
1610 f = type_first[i];
1611 l = type_len[i];
1612 a = the_vec_sorted[f];
1613 ost << a << "^" << l;
1614 if (i < nb_types - 1)
1615 ost << ", ";
1616 }
1617 ost << " )";
1618#endif
1619 int_vec_print_types(ost, FALSE /* f_backwards */, the_vec_sorted,
1620 nb_types, type_first, type_len);
1621 FREE_int(the_vec_sorted);
1622 FREE_int(sorting_perm);
1623 FREE_int(sorting_perm_inv);
1624 FREE_int(type_first);
1625 FREE_int(type_len);
1626}
1627
1629 int f_backwards, int *the_vec_sorted,
1630 int nb_types, int *type_first, int *type_len)
1631{
1632 ost << "( ";
1634 f_backwards, the_vec_sorted, nb_types, type_first, type_len);
1635 ost << " )";
1636}
1637
1639 int f_backwards, int *the_vec_sorted,
1640 int nb_types, int *type_first, int *type_len)
1641{
1642 int i, f, l, a;
1643
1644 if (f_backwards) {
1645 for (i = nb_types - 1; i >= 0; i--) {
1646 f = type_first[i];
1647 l = type_len[i];
1648 a = the_vec_sorted[f];
1649 sstr << a;
1650 if (l > 1) {
1651 sstr << "^{" << l << "}";
1652 }
1653 if (i)
1654 sstr << ", ";
1655 }
1656 }
1657 else {
1658 for (i = 0; i < nb_types; i++) {
1659 f = type_first[i];
1660 l = type_len[i];
1661 a = the_vec_sorted[f];
1662 sstr << a;
1663 if (l > 1) {
1664 sstr << "^{" << l << "}";
1665 }
1666 if (i < nb_types - 1)
1667 sstr << ", ";
1668 }
1669 }
1670}
1671
1673 int f_backwards, int *the_vec_sorted,
1674 int nb_types, int *type_first, int *type_len)
1675{
1676 int i, f, l, a;
1677
1678 if (f_backwards) {
1679 for (i = nb_types - 1; i >= 0; i--) {
1680 f = type_first[i];
1681 l = type_len[i];
1682 a = the_vec_sorted[f];
1683 ost << a;
1684 if (l > 1) {
1685 ost << "^" << l;
1686 }
1687 if (i)
1688 ost << ", ";
1689 }
1690 }
1691 else {
1692 for (i = 0; i < nb_types; i++) {
1693 f = type_first[i];
1694 l = type_len[i];
1695 a = the_vec_sorted[f];
1696 ost << a;
1697 if (l > 1) {
1698 ost << "^" << l;
1699 }
1700 if (i < nb_types - 1)
1701 ost << ", ";
1702 }
1703 }
1704}
1705
1707 int f_backwards, int *the_vec_sorted,
1708 int nb_types, int *type_first, int *type_len)
1709{
1710 int i, f, l, a;
1711
1712 if (f_backwards) {
1713 for (i = nb_types - 1; i >= 0; i--) {
1714 f = type_first[i];
1715 l = type_len[i];
1716 a = the_vec_sorted[f];
1717 ost << "$" << a;
1718 if (l > 9) {
1719 ost << "^{" << l << "}";
1720 }
1721 else if (l > 1) {
1722 ost << "^" << l;
1723 }
1724 if (i)
1725 ost << ",\\,";
1726 ost << "$ ";
1727 }
1728 }
1729 else {
1730 for (i = 0; i < nb_types; i++) {
1731 f = type_first[i];
1732 l = type_len[i];
1733 a = the_vec_sorted[f];
1734 ost << "$" << a;
1735 if (l > 9) {
1736 ost << "^{" << l << "}";
1737 }
1738 else if (l > 1) {
1739 ost << "^" << l;
1740 }
1741 if (i < nb_types - 1)
1742 ost << ",\\,";
1743 ost << "$ ";
1744 }
1745 }
1746}
1747
1749 int f_backwards, int *the_vec_sorted,
1750 int nb_types, int *type_first, int *type_len)
1751{
1752 int i, f, l, a;
1753
1754 if (f_backwards) {
1755 for (i = nb_types - 1; i >= 0; i--) {
1756 f = type_first[i];
1757 l = type_len[i];
1758 a = the_vec_sorted[f];
1759 ost << a;
1760 if (l > 9) {
1761 ost << "^{" << l << "}";
1762 }
1763 else if (l > 1) {
1764 ost << "^" << l;
1765 }
1766 if (i)
1767 ost << ",\\,";
1768 ost << " ";
1769 }
1770 }
1771 else {
1772 for (i = 0; i < nb_types; i++) {
1773 f = type_first[i];
1774 l = type_len[i];
1775 a = the_vec_sorted[f];
1776 ost << a;
1777 if (l > 9) {
1778 ost << "^{" << l << "}";
1779 }
1780 else if (l > 1) {
1781 ost << "^" << l;
1782 }
1783 if (i < nb_types - 1)
1784 ost << ",\\,";
1785 ost << " ";
1786 }
1787 }
1788}
1789
1790void sorting::Heapsort(void *v, int len, int entry_size_in_chars,
1791 int (*compare_func)(void *v1, void *v2))
1792{
1793 int end;
1794
1795 //cout << "Heapsort len=" << len << endl;
1796 Heapsort_make_heap(v, len,
1797 entry_size_in_chars, compare_func);
1798 for (end = len - 1; end > 0; ) {
1799 Heapsort_swap(v, 0, end, entry_size_in_chars);
1800 end--;
1801 Heapsort_sift_down(v, 0, end,
1802 entry_size_in_chars, compare_func);
1803 }
1804}
1805
1806void sorting::Heapsort_general(void *data, int len,
1807 int (*compare_func)(void *data,
1808 int i, int j, void *extra_data),
1809 void (*swap_func)(void *data,
1810 int i, int j, void *extra_data),
1811 void *extra_data)
1812{
1813 int end;
1814
1815 //cout << "Heapsort_general len=" << len << endl;
1817 compare_func, swap_func, extra_data);
1818 for (end = len - 1; end > 0; ) {
1819 (*swap_func)(data, 0, end, extra_data);
1820 //Heapsort_general_swap(v, 0, end);
1821 end--;
1822 Heapsort_general_sift_down(data, 0, end,
1823 compare_func, swap_func, extra_data);
1824 }
1825}
1826
1827void sorting::Heapsort_general_with_log(void *data, int *w, int len,
1828 int (*compare_func)(void *data,
1829 int i, int j, void *extra_data),
1830 void (*swap_func)(void *data,
1831 int i, int j, void *extra_data),
1832 void *extra_data)
1833{
1834 int end;
1835 algorithms Algo;
1836
1837 //cout << "Heapsort_general len=" << len << endl;
1839 compare_func, swap_func, extra_data);
1840 for (end = len - 1; end > 0; ) {
1841 (*swap_func)(data, 0, end, extra_data);
1842 Algo.int_swap(w[0], w[end]);
1843 //Heapsort_general_swap(v, 0, end);
1844 end--;
1846 compare_func, swap_func, extra_data);
1847 }
1848}
1849
1850
1851
1852int sorting::search_general(void *data, int len,
1853 void *search_object, int &idx,
1854 int (*compare_func)(void *data, int i,
1855 void *search_object, void *extra_data),
1856 void *extra_data, int verbose_level)
1857// This function finds the last occurrence of the element a.
1858// If a is not found, it returns in idx the
1859// position where it should be inserted if
1860// the vector is assumed to be in increasing order.
1861
1862{
1863 int f_v = (verbose_level >= 1);
1864 int l, r, m, res;
1865 int f_found = FALSE;
1866
1867 if (f_v) {
1868 cout << "search_general len = " << len << endl;
1869 }
1870
1871 if (len == 0) {
1872 idx = 0;
1873 return FALSE;
1874 }
1875 l = 0;
1876 r = len;
1877 // invariant:
1878 // v[i] <= a for i < l;
1879 // v[i] > a for i >= r;
1880 // r - l is the length of the area to search in.
1881 while (l < r) {
1882 m = (l + r) >> 1;
1883 // if the length of the search area is even
1884 // we examine the element above the middle
1885
1886
1887 if (f_v) {
1888 cout << "search_general l=" << l << " m=" << m
1889 << " r=" << r << endl;
1890 }
1891 res = (*compare_func)(data, m, search_object, extra_data);
1892 if (f_v) {
1893 cout << "search_general l=" << l << " m=" << m
1894 << " r=" << r << " res=" << res << endl;
1895 }
1896 //res = - res;
1897 //if (c < 0 /*v[root] < v[child] */)
1898
1899
1900 //res = v[m] - a;
1901 if (f_v) {
1902 cout << "l=" << l << " r=" << r<< " m=" << m
1903 << " res=" << res << endl;
1904 }
1905 //cout << "search l=" << l << " m=" << m << " r="
1906 // << r << "a=" << a << " v[m]=" << v[m]
1907 // << " res=" << res << endl;
1908 // so, res is
1909 // positive if v[m] > a,
1910 // zero if v[m] == a,
1911 // negative if v[m] < a
1912 if (res <= 0) {
1913 l = m + 1;
1914 if (f_v) {
1915 cout << "moving to the right" << endl;
1916 }
1917 if (res == 0) {
1918 f_found = TRUE;
1919 }
1920 }
1921 else {
1922 if (f_v) {
1923 cout << "moving to the left" << endl;
1924 }
1925 r = m;
1926 }
1927 }
1928 // now: l == r;
1929 // and f_found is set accordingly */
1930#if 1
1931 if (f_found) {
1932 l--;
1933 }
1934#endif
1935 idx = l;
1936 return f_found;
1937}
1938
1939
1940
1941void sorting::int_vec_heapsort(int *v, int len)
1942{
1943 int end;
1944
1945 heapsort_make_heap(v, len);
1946 for (end = len - 1; end > 0; ) {
1947 heapsort_swap(v, 0, end);
1948 end--;
1949 heapsort_sift_down(v, 0, end);
1950 }
1951
1952}
1953
1954void sorting::lint_vec_heapsort(long int *v, int len)
1955{
1956 int end;
1957
1959 for (end = len - 1; end > 0; ) {
1960 lint_heapsort_swap(v, 0, end);
1961 end--;
1962 lint_heapsort_sift_down(v, 0, end);
1963 }
1964
1965}
1966
1967void sorting::int_vec_heapsort_with_log(int *v, int *w, int len)
1968{
1969 int end;
1970
1971 heapsort_make_heap_with_log(v, w, len);
1972 for (end = len - 1; end > 0; ) {
1973 heapsort_swap(v, 0, end);
1974 heapsort_swap(w, 0, end);
1975 end--;
1976 heapsort_sift_down_with_log(v, w, 0, end);
1977 }
1978
1979}
1980
1981void sorting::lint_vec_heapsort_with_log(long int *v, long int *w, int len)
1982{
1983 int end;
1984
1986 for (end = len - 1; end > 0; ) {
1987 lint_heapsort_swap(v, 0, end);
1988 lint_heapsort_swap(w, 0, end);
1989 end--;
1991 }
1992
1993}
1994
1995void sorting::heapsort_make_heap(int *v, int len)
1996{
1997 int start;
1998
1999 for (start = (len - 2) >> 1 ; start >= 0; start--) {
2000 heapsort_sift_down(v, start, len - 1);
2001 }
2002}
2003
2004void sorting::lint_heapsort_make_heap(long int *v, int len)
2005{
2006 int start;
2007
2008 for (start = (len - 2) >> 1 ; start >= 0; start--) {
2009 lint_heapsort_sift_down(v, start, len - 1);
2010 }
2011}
2012
2013void sorting::heapsort_make_heap_with_log(int *v, int *w, int len)
2014{
2015 int start;
2016
2017 for (start = (len - 2) >> 1 ; start >= 0; start--) {
2018 heapsort_sift_down_with_log(v, w, start, len - 1);
2019 }
2020}
2021
2022void sorting::lint_heapsort_make_heap_with_log(long int *v, long int *w, int len)
2023{
2024 int start;
2025
2026 for (start = (len - 2) >> 1 ; start >= 0; start--) {
2027 lint_heapsort_sift_down_with_log(v, w, start, len - 1);
2028 }
2029}
2030
2031void sorting::Heapsort_make_heap(void *v, int len, int entry_size_in_chars,
2032 int (*compare_func)(void *v1, void *v2))
2033{
2034 int start;
2035
2036 //cout << "Heapsort_make_heap len=" << len << endl;
2037 for (start = (len - 2) >> 1 ; start >= 0; start--) {
2038 Heapsort_sift_down(v, start, len - 1,
2039 entry_size_in_chars, compare_func);
2040 }
2041}
2042
2044 int (*compare_func)(void *data, int i, int j, void *extra_data),
2045 void (*swap_func)(void *data, int i, int j, void *extra_data),
2046 void *extra_data)
2047{
2048 int start;
2049
2050 //cout << "Heapsort_general_make_heap len=" << len << endl;
2051 for (start = (len - 2) >> 1 ; start >= 0; start--) {
2052 Heapsort_general_sift_down(data, start, len - 1,
2053 compare_func, swap_func, extra_data);
2054 }
2055}
2056
2057void sorting::Heapsort_general_make_heap_with_log(void *data, int *w, int len,
2058 int (*compare_func)(void *data, int i, int j, void *extra_data),
2059 void (*swap_func)(void *data, int i, int j, void *extra_data),
2060 void *extra_data)
2061{
2062 int start;
2063
2064 //cout << "Heapsort_general_make_heap len=" << len << endl;
2065 for (start = (len - 2) >> 1 ; start >= 0; start--) {
2066 Heapsort_general_sift_down_with_log(data, w, start, len - 1,
2067 compare_func, swap_func, extra_data);
2068 }
2069}
2070
2071void sorting::heapsort_sift_down(int *v, int start, int end)
2072{
2073 int root, child;
2074
2075 root = start;
2076 while (2 * root + 1 <= end) {
2077 child = 2 * root + 1; // left child
2078 if (child + 1 <= end && v[child] < v[child + 1]) {
2079 child++;
2080 }
2081 if (v[root] < v[child]) {
2082 heapsort_swap(v, root, child);
2083 root = child;
2084 }
2085 else {
2086 return;
2087 }
2088 }
2089}
2090
2091void sorting::lint_heapsort_sift_down(long int *v, int start, int end)
2092{
2093 int root, child;
2094
2095 root = start;
2096 while (2 * root + 1 <= end) {
2097 child = 2 * root + 1; // left child
2098 if (child + 1 <= end && v[child] < v[child + 1]) {
2099 child++;
2100 }
2101 if (v[root] < v[child]) {
2102 lint_heapsort_swap(v, root, child);
2103 root = child;
2104 }
2105 else {
2106 return;
2107 }
2108 }
2109}
2110
2112 int *v, int *w, int start, int end)
2113{
2114 int root, child;
2115
2116 root = start;
2117 while (2 * root + 1 <= end) {
2118 child = 2 * root + 1; // left child
2119 if (child + 1 <= end && v[child] < v[child + 1]) {
2120 child++;
2121 }
2122 if (v[root] < v[child]) {
2123 heapsort_swap(v, root, child);
2124 heapsort_swap(w, root, child);
2125 root = child;
2126 }
2127 else {
2128 return;
2129 }
2130 }
2131}
2132
2134 long int *v, long int *w, int start, int end)
2135{
2136 long int root, child;
2137
2138 root = start;
2139 while (2 * root + 1 <= end) {
2140 child = 2 * root + 1; // left child
2141 if (child + 1 <= end && v[child] < v[child + 1]) {
2142 child++;
2143 }
2144 if (v[root] < v[child]) {
2145 lint_heapsort_swap(v, root, child);
2146 lint_heapsort_swap(w, root, child);
2147 root = child;
2148 }
2149 else {
2150 return;
2151 }
2152 }
2153}
2154
2156 void *v, int start, int end, int entry_size_in_chars,
2157 int (*compare_func)(void *v1, void *v2))
2158{
2159 char *V = (char *) v;
2160 int root, child, c;
2161
2162 //cout << "Heapsort_sift_down " << start << " : " << end << endl;
2163 root = start;
2164 while (2 * root + 1 <= end) {
2165 child = 2 * root + 1; // left child
2166 if (child + 1 <= end) {
2167 //cout << "compare " << child << " : " << child + 1 << endl;
2168 c = (*compare_func)(
2169 V + child * entry_size_in_chars,
2170 V + (child + 1) * entry_size_in_chars);
2171 if (c < 0 /*v[child] < v[child + 1]*/) {
2172 child++;
2173 }
2174 }
2175 //cout << "compare " << root << " : " << child << endl;
2176 c = (*compare_func)(
2177 V + root * entry_size_in_chars,
2178 V + child * entry_size_in_chars);
2179 if (c < 0 /*v[root] < v[child] */) {
2180 Heapsort_swap(v, root, child, entry_size_in_chars);
2181 root = child;
2182 }
2183 else {
2184 return;
2185 }
2186 }
2187}
2188
2189void sorting::Heapsort_general_sift_down(void *data, int start, int end,
2190 int (*compare_func)(void *data, int i, int j, void *extra_data),
2191 void (*swap_func)(void *data, int i, int j, void *extra_data),
2192 void *extra_data)
2193{
2194 int root, child, c;
2195
2196 //cout << "Heapsort_general_sift_down " << start << " : " << end << endl;
2197 root = start;
2198 while (2 * root + 1 <= end) {
2199 child = 2 * root + 1; // left child
2200 if (child + 1 <= end) {
2201 //cout << "compare " << child << " : " << child + 1 << endl;
2202 c = (*compare_func)(data, child, child + 1, extra_data);
2203 if (c < 0 /*v[child] < v[child + 1]*/) {
2204 child++;
2205 }
2206 }
2207 //cout << "compare " << root << " : " << child << endl;
2208 c = (*compare_func)(data, root, child, extra_data);
2209 if (c < 0 /*v[root] < v[child] */) {
2210 (*swap_func)(data, root, child, extra_data);
2211 //Heapsort_swap(v, root, child, entry_size_in_chars);
2212 root = child;
2213 }
2214 else {
2215 return;
2216 }
2217 }
2218}
2219
2220void sorting::Heapsort_general_sift_down_with_log(void *data, int *w, int start, int end,
2221 int (*compare_func)(void *data, int i, int j, void *extra_data),
2222 void (*swap_func)(void *data, int i, int j, void *extra_data),
2223 void *extra_data)
2224{
2225 int root, child, c;
2226 algorithms Algo;
2227
2228 //cout << "Heapsort_general_sift_down " << start << " : " << end << endl;
2229 root = start;
2230 while (2 * root + 1 <= end) {
2231 child = 2 * root + 1; // left child
2232 if (child + 1 <= end) {
2233 //cout << "compare " << child << " : " << child + 1 << endl;
2234 c = (*compare_func)(data, child, child + 1, extra_data);
2235 if (c < 0 /*v[child] < v[child + 1]*/) {
2236 child++;
2237 }
2238 }
2239 //cout << "compare " << root << " : " << child << endl;
2240 c = (*compare_func)(data, root, child, extra_data);
2241 if (c < 0 /*v[root] < v[child] */) {
2242 (*swap_func)(data, root, child, extra_data);
2243 Algo.int_swap(w[root], w[child]);
2244 //Heapsort_swap(v, root, child, entry_size_in_chars);
2245 root = child;
2246 }
2247 else {
2248 return;
2249 }
2250 }
2251}
2252
2253
2254void sorting::heapsort_swap(int *v, int i, int j)
2255{
2256 int a;
2257
2258 a = v[i];
2259 v[i] = v[j];
2260 v[j] = a;
2261}
2262
2263void sorting::lint_heapsort_swap(long int *v, int i, int j)
2264{
2265 long int a;
2266
2267 a = v[i];
2268 v[i] = v[j];
2269 v[j] = a;
2270}
2271
2272void sorting::Heapsort_swap(void *v, int i, int j, int entry_size_in_chars)
2273{
2274 int a, h, I, J;
2275 char *V;
2276
2277 I = i * entry_size_in_chars;
2278 J = j * entry_size_in_chars;
2279 V = (char *)v;
2280 for (h = 0; h < entry_size_in_chars; h++) {
2281 a = V[I + h];
2282 V[I + h] = V[J + h];
2283 V[J + h] = a;
2284 }
2285}
2286
2287
2289 int *data, int data_sz, int multiplicity,
2290 int *&pts, int &nb_pts)
2291{
2292 tally C;
2293 C.init(data, data_sz, FALSE, 0);
2294 C.get_data_by_multiplicity(pts, nb_pts, multiplicity, 0 /* verbose_level */);
2295}
2296
2298{
2299 int i, j, a;
2300 for (i = 0; i < len; i++) {
2301 for (j = i + 1; j < len; j++) {
2302 if (p[i] > p[j]) {
2303 a = p[i];
2304 p[i] = p[j];
2305 p[j] = a;
2306 }
2307 }
2308 }
2309}
2310
2311
2312
2313int sorting::integer_vec_compare(int *p, int *q, int len)
2314{
2315 int i;
2316
2317 for (i = 0; i < len; i++) {
2318 if (p[i] < q[i])
2319 return -1;
2320 if (p[i] > q[i])
2321 return 1;
2322 }
2323 return 0;
2324}
2325
2326int sorting::lint_vec_compare(long int *p, long int *q, int len)
2327{
2328 int i;
2329
2330 for (i = 0; i < len; i++) {
2331 if (p[i] < q[i])
2332 return -1;
2333 if (p[i] > q[i])
2334 return 1;
2335 }
2336 return 0;
2337}
2338
2340 int n, int *pts, int *prev, int f_prev_is_point_index, int *pts_inv,
2341 int *&depth, int *&ancestor, int verbose_level)
2342{
2343 int f_v = (verbose_level >= 1);
2344 int i;
2345
2346 if (f_v) {
2347 cout << "sorting::schreier_vector_compute_depth_and_ancestor" << endl;
2348 }
2349 depth = NEW_int(n);
2350 ancestor = NEW_int(n);
2351
2352 for (i = 0; i < n; i++) {
2353 depth[i] = -1;
2354 ancestor[i] = -1;
2355 }
2356 for (i = 0; i < n; i++) {
2357 if (f_v) {
2358 cout << "sorting::schreier_vector_compute_depth_and_ancestor i=" << i << " / " << n << endl;
2359 }
2361 pts, prev, f_prev_is_point_index, pts_inv, depth, ancestor, i);
2362 }
2363 if (f_v) {
2364 cout << "sorting::schreier_vector_compute_depth_and_ancestor done" << endl;
2365 }
2366
2367}
2368
2370 int n, int *pts, int *prev, int f_use_pts_inv, int *pts_inv,
2371 int *depth, int *ancestor, int pos)
2372{
2373 int pt, pt_loc, d;
2374
2375 if (f_use_pts_inv) {
2376 pt = prev[pos];
2377 if (pt == -1) {
2378 depth[pos] = 0;
2379 ancestor[pos] = pts[pos];
2380 return 0;
2381 }
2382 pt_loc = pts_inv[pt];
2383 }
2384 else {
2385 pt = prev[pos];
2386 if (pt == -1) {
2387 depth[pos] = 0;
2388 ancestor[pos] = pts[pos];
2389 return 0;
2390 }
2391 if (!int_vec_search(pts, n, pt, pt_loc)) {
2392 int i;
2393
2394 cout << "schreier_vector_determine_depth_recursion, "
2395 "fatal: did not find pt" << endl;
2396 cout << "pt = " << pt << endl;
2397 cout << "vector of length " << n << endl;
2398 Int_vec_print(cout, pts, n);
2399 cout << endl;
2400 cout << "i : pts[i] : prev[i] : depth[i] : ancestor[i]" << endl;
2401 for (i = 0; i < n; i++) {
2402 cout
2403 << setw(5) << i << " : "
2404 << setw(5) << pts[i] << " : "
2405 << setw(5) << prev[i] << " : "
2406 //<< setw(5) << label[i] << " : "
2407 << setw(5) << depth[i] << " : "
2408 << setw(5) << ancestor[i]
2409 << endl;
2410 }
2411 exit(1);
2412 }
2413 }
2414 d = depth[pt_loc];
2415 if (d >= 0) {
2416 d++;
2417 }
2418 else {
2420 pts, prev, f_use_pts_inv, pts_inv,
2421 depth, ancestor, pt_loc) + 1;
2422 }
2423 depth[pos] = d;
2424 ancestor[pos] = ancestor[pt_loc];
2425 return d;
2426}
2427
2429 int n, int *pts, int *prev, int f_use_pts_inv, int *pts_inv,
2430 std::string &fname_base,
2431 graphics::layered_graph_draw_options *LG_Draw_options,
2433 int verbose_level)
2434{
2435 int f_v = (verbose_level >= 1);
2436 int i, r;
2437 int *depth;
2438 int *ancestor;
2439
2440 if (f_v) {
2441 cout << "sorting::schreier_vector_tree, n=" << n << " f_use_pts_inv=" << f_use_pts_inv << endl;
2442 }
2443
2444 if (f_v) {
2445 if (f_use_pts_inv) {
2446 cout << "i : pts[i] : pts_inv[i] : prev[i]" << endl;
2447 for (i = 0; i < n; i++) {
2448 cout << i << " : " << pts[i] << " : " << pts_inv[i] << " : " << prev[i] << endl;
2449 }
2450 }
2451 else {
2452 cout << "i : pts[i] : prev[i]" << endl;
2453 for (i = 0; i < n; i++) {
2454 cout << i << " : " << pts[i] << " : " << prev[i] << endl;
2455 }
2456
2457 }
2458 }
2459
2460 if (f_v) {
2461 cout << "sorting::schreier_vector_tree before schreier_vector_compute_depth_and_ancestor" << endl;
2462 }
2464 n, pts, prev, f_use_pts_inv, pts_inv,
2465 depth, ancestor, verbose_level - 2);
2466 if (f_v) {
2467 cout << "sorting::schreier_vector_tree after schreier_vector_compute_depth_and_ancestor" << endl;
2468 }
2469
2470 if (f_v) {
2471 cout << "i : pts[i] : prev[i] : depth[i]" << endl;
2472 for (i = 0; i < n; i++) {
2473 cout << i << " : " << pts[i] << " : " << prev[i] << " : " << depth[i] << endl;
2474 }
2475 }
2476
2477
2478 for (i = 0; i < n; i++) {
2479 if (i == 0) {
2480 r = ancestor[0];
2481 }
2482 else {
2483 if (ancestor[i] != r) {
2484 cout << "sorting::schreier_vector_tree the tree is not a tree but a forest" << endl;
2485 exit(1);
2486 }
2487 }
2488 }
2489 set_of_sets *SoS;
2490 tally C;
2491 //int f, a, t;
2492
2493 SoS = NEW_OBJECT(set_of_sets);
2494 C.init(depth, n, FALSE, 0);
2495
2496 int *types;
2497 int nb_types;
2498
2499 SoS = C.get_set_partition_and_types(types,
2500 nb_types, verbose_level);
2501 SoS->sort_all(verbose_level - 2);
2502
2503 if (f_v) {
2504 cout << "sorting::schreier_vector_tree SoS=" << endl;
2505 SoS->print_table();
2506 }
2507
2509 int *Sz;
2510
2511 Sz = NEW_int(C.nb_types);
2512 for (i = 0; i < C.nb_types; i++) {
2513 Sz[i] = SoS->Set_size[i];
2514 }
2515
2516 LG->init(C.nb_types /* nb_layers */, Sz /* int *Nb_nodes_layer */,
2517 fname_base, verbose_level);
2518
2519 FREE_int(Sz);
2520
2521
2522 int pos1, pos2, d1, d2, n1, n2;
2523
2524 for (i = 0; i < n; i++) {
2525
2526 if (f_v) {
2527 cout << "sorting::schreier_vector_tree i=" << i << " / " << n << endl;
2528 }
2529 pos2 = i;
2530 if (depth[i] == 0) {
2531 continue;
2532 }
2533 if (f_v) {
2534 cout << "sorting::schreier_vector_tree i=" << i << " / " << n << " pos2=" << pos2 << endl;
2535 }
2536
2537 if (f_use_pts_inv) {
2538 int pt;
2539
2540 pt = prev[i];
2541 pos1 = pts_inv[pt];
2542 }
2543 else {
2544 int pt;
2545
2546 pt = prev[i];
2547 if (!int_vec_search(pts, n, pt, pos1)) {
2548 cout << "sorting::schreier_vector_tree cannot find point pt" << endl;
2549 exit(1);
2550 }
2551 }
2552 if (f_v) {
2553 cout << "sorting::schreier_vector_tree i=" << i << " / " << n << " pos2=" << pos2 << " pos1=" << pos1 << endl;
2554 }
2555 d1 = depth[pos1];
2556 d2 = depth[pos2];
2557 if (!lint_vec_search(SoS->Sets[d1], SoS->Set_size[d1], pos1, n1, 0)) {
2558 cout << "sorting::schreier_vector_tree cannot find point pos1" << endl;
2559 exit(1);
2560 }
2561 if (!lint_vec_search(SoS->Sets[d2], SoS->Set_size[d2], pos2, n2, 0)) {
2562 cout << "sorting::schreier_vector_tree cannot find point pos2" << endl;
2563 exit(1);
2564 }
2565 LG->add_edge(d1, n1, d2, n2, 0 /*verbose_level*/);
2566 }
2567
2568 for (i = 0; i < n; i++) {
2569 pos1 = i;
2570 d1 = depth[pos1];
2571 if (!lint_vec_search(SoS->Sets[d1], SoS->Set_size[d1], pos1, n1, 0)) {
2572 cout << "sorting::schreier_vector_tree cannot find point pos1" << endl;
2573 exit(1);
2574 }
2575 LG->add_node_data1(d1, n1, pts[pos1], 0/*verbose_level*/);
2576 }
2577
2578 FREE_int(depth);
2579 FREE_int(ancestor);
2580 FREE_OBJECT(SoS);
2581
2582 if (f_v) {
2583 cout << "sorting::schreier_vector_tree before LG->place" << endl;
2584 }
2585 LG->place_with_y_stretch(0.5, verbose_level);
2586 if (f_v) {
2587 cout << "sorting::schreier_vector_tree after LG->place" << endl;
2588 }
2589 if (f_v) {
2590 cout << "sorting::schreier_vector_tree before LG->create_spanning_tree" << endl;
2591 }
2593 TRUE /* f_place_x */, verbose_level);
2594 if (f_v) {
2595 cout << "sorting::schreier_vector_tree after LG->create_spanning_tree" << endl;
2596 }
2597
2598
2599 string fname;
2600
2601 fname.assign(fname_base);
2602 fname.append(".layered_graph");
2603
2604
2605
2606 LG->write_file(fname, 0 /*verbose_level*/);
2607 LG->draw_with_options(fname_base, LG_Draw_options,
2608 0 /* verbose_level */);
2609
2610
2611 if (f_v) {
2612 cout << "sorting::schreier_vector_tree done" << endl;
2613 }
2614
2615}
2616
2617int sorting::compare_sets(int *set1, int *set2, int sz1, int sz2)
2618{
2619 int *S1, *S2;
2620 int u, ret;
2621
2622 S1 = NEW_int(sz1);
2623 S2 = NEW_int(sz2);
2624 Int_vec_copy(set1, S1, sz1);
2625 Int_vec_copy(set2, S2, sz2);
2626 int_vec_heapsort(S1, sz1);
2627 int_vec_heapsort(S2, sz2);
2628 for ( u = 0; u < sz1 + sz2; u++) {
2629 if (u < sz1 && u < sz2) {
2630 if (S1[u] < S2[u]) {
2631 ret = -1;
2632 goto finish;
2633 }
2634 else if (S1[u] > S2[u]) {
2635 ret = 1;
2636 goto finish;
2637 }
2638 }
2639 if (u == sz1) {
2640 if (sz2 > sz1) {
2641 ret = -1;
2642 }
2643 else {
2644 ret = 0;
2645 }
2646 goto finish;
2647 }
2648 else if (u == sz2) {
2649 ret = 1;
2650 goto finish;
2651 }
2652 }
2653 ret = 0;
2654finish:
2655 FREE_int(S1);
2656 FREE_int(S2);
2657 return ret;
2658}
2659
2660int sorting::compare_sets_lint(long int *set1, long int *set2, int sz1, int sz2)
2661{
2662 long int *S1, *S2;
2663 int u, ret;
2664
2665 S1 = NEW_lint(sz1);
2666 S2 = NEW_lint(sz2);
2667 Lint_vec_copy(set1, S1, sz1);
2668 Lint_vec_copy(set2, S2, sz2);
2669 lint_vec_heapsort(S1, sz1);
2670 lint_vec_heapsort(S2, sz2);
2671 for (u = 0; u < sz1 + sz2; u++) {
2672 if (u < sz1 && u < sz2) {
2673 if (S1[u] < S2[u]) {
2674 ret = -1;
2675 goto finish;
2676 }
2677 else if (S1[u] > S2[u]) {
2678 ret = 1;
2679 goto finish;
2680 }
2681 }
2682 if (u == sz1) {
2683 if (sz2 > sz1) {
2684 ret = -1;
2685 }
2686 else {
2687 ret = 0;
2688 }
2689 goto finish;
2690 }
2691 else if (u == sz2) {
2692 ret = 1;
2693 goto finish;
2694 }
2695 }
2696 ret = 0;
2697finish:
2698 FREE_lint(S1);
2699 FREE_lint(S2);
2700 return ret;
2701}
2702
2703int sorting::test_if_sets_are_disjoint(long int *set1, long int *set2, int sz1, int sz2)
2704{
2705 int u, v;
2706
2707 u = v = 0;
2708 while (u < sz1 && v < sz2) {
2709 if (set1[u] == set2[v]) {
2710 return FALSE;
2711 }
2712 else if (set1[u] < set2[v]) {
2713 u++;
2714 }
2715 else {
2716 // now set1[u] > set2[v]
2717 v++;
2718 }
2719 }
2720 return TRUE;
2721}
2722
2723void sorting::d_partition(double *v, int left, int right, int *middle)
2724{
2725 int l, r, m, len, m1, pivot;
2726 double vv;
2727
2728 //cout << "partition: from " << left << " to " << right << endl;
2729 // pivot strategy: take the element in the middle:
2730 len = right + 1 - left;
2731 m1 = len >> 1;
2732 pivot = left;
2733 if (m1) {
2734 vv = v[pivot];
2735 v[pivot] = v[left + m1];
2736 v[left + m1] = vv;
2737 }
2738 l = left;
2739 r = right;
2740 while (l < r) {
2741 while (TRUE) {
2742 if (l > right)
2743 break;
2744 if (v[l] < v[pivot])
2745 break;
2746 l++;
2747 }
2748 while (TRUE) {
2749 if (r < left)
2750 break;
2751 if (v[r] >= v[pivot])
2752 break;
2753 r--;
2754 }
2755 // now v[l] > v[pivot] and v[r] <= v[pivot]
2756 if (l < r) {
2757 vv = v[l];
2758 v[l] = v[r];
2759 v[r] = vv;
2760 }
2761 }
2762 m = r;
2763 if (left != m) {
2764 vv = v[left];
2765 v[left] = v[m];
2766 v[m] = vv;
2767 }
2768 *middle = m;
2769}
2770
2771void sorting::d_quicksort(double *v, int left, int right)
2772{
2773 int middle;
2774
2775 if (left < right) {
2776 d_partition(v, left, right, &middle);
2777 d_quicksort(v, left, middle - 1);
2778 d_quicksort(v, middle + 1, right);
2779 }
2780}
2781
2782void sorting::d_quicksort_array(int len, double *v)
2783{
2784 if (len <= 1)
2785 return;
2786 d_quicksort(v, 0, len - 1);
2787}
2788
2789
2790int sorting::test_if_sets_are_disjoint_assuming_sorted(int *set1, int *set2, int sz1, int sz2)
2791{
2792 int sz;
2793 int *p, *q;
2794 int u, v;
2795
2796 sz = sz1 + sz2;
2797 u = v = 0;
2798 p = set1;
2799 q = set2;
2800 while (u + v < sz) {
2801 if (p[u] == q[v]) {
2802 return FALSE;
2803 }
2804 if (u == sz1) {
2805 v++;
2806 }
2807 else if (v == sz2) {
2808 u++;
2809 }
2810 else if (p[u] < q[v]) {
2811 u++;
2812 }
2813 else {
2814 v++;
2815 }
2816 }
2817 return TRUE;
2818}
2819
2821 long int *set1, long int *set2, int sz1, int sz2)
2822{
2823 int sz;
2824 long int *p, *q;
2825 int u, v;
2826
2827 sz = sz1 + sz2;
2828 u = v = 0;
2829 p = set1;
2830 q = set2;
2831 while (u + v < sz) {
2832 if (p[u] == q[v]) {
2833 return FALSE;
2834 }
2835 if (u == sz1) {
2836 v++;
2837 }
2838 else if (v == sz2) {
2839 u++;
2840 }
2841 else if (p[u] < q[v]) {
2842 u++;
2843 }
2844 else {
2845 v++;
2846 }
2847 }
2848 return TRUE;
2849}
2850
2852{
2853 int i;
2854
2855 for (i = 0; i < len; i++) {
2856 if (p[i] < q[i])
2857 return -1;
2858 if (p[i] > q[i])
2859 return 1;
2860 }
2861 return 0;
2862}
2863
2865{
2866 int i, a, idx;
2867 sorting Sorting;
2868
2869 for (i = 0; i < len; i++) {
2870 a = v[i];
2871 if (lint_vec_search_linear(w, len, a, idx)) {
2872 return FALSE;
2873 }
2874 }
2875 return TRUE;
2876}
2877
2878int sorting::int_vec_compare(int *p, int *q, int len)
2879{
2880 int i;
2881
2882 for (i = 0; i < len; i++) {
2883 if (p[i] < q[i])
2884 return -1;
2885 if (p[i] > q[i])
2886 return 1;
2887 }
2888 return 0;
2889}
2890
2891int sorting::int_vec_compare_stride(int *p, int *q, int len, int stride)
2892{
2893 int i;
2894
2895 for (i = 0; i < len; i++) {
2896 if (p[i * stride] < q[i * stride])
2897 return -1;
2898 if (p[i * stride] > q[i * stride])
2899 return 1;
2900 }
2901 return 0;
2902}
2903
2905 int *class_first, int *class_len, int &nb_classes)
2906// we assume that the vector v is sorted.
2907{
2908 int i;
2909
2910 nb_classes = 0;
2911 class_first[0] = 0;
2912 class_len[0] = 1;
2913 for (i = 1; i < len; i++) {
2914 if (v[i] == v[i - 1]) {
2915 class_len[nb_classes]++;
2916 }
2917 else {
2918 nb_classes++;
2919 class_first[nb_classes] =
2920 class_first[nb_classes - 1] + class_len[nb_classes - 1];
2921 class_len[nb_classes] = 1;
2922 }
2923 }
2924 nb_classes++;
2925}
2926
2927//##############################################################################
2928// global functions:
2929//##############################################################################
2930
2931static void int_vec_partition(int *v,
2932 int (*compare_func)(int a, int b), int left, int right, int *middle)
2933{
2934 int l, r, m, len, m1, res, pivot;
2935 int vv;
2936
2937 //cout << "partition: from " << left << " to " << right << endl;
2938 // pivot strategy: take the element in the middle:
2939 len = right + 1 - left;
2940 m1 = len >> 1;
2941 pivot = left;
2942 if (m1) {
2943 vv = v[pivot];
2944 v[pivot] = v[left + m1];
2945 v[left + m1] = vv;
2946 }
2947 l = left;
2948 r = right;
2949 while (l < r) {
2950 while (TRUE) {
2951 if (l > right)
2952 break;
2953 res = (*compare_func)(v[l], v[pivot]);
2954 if (res > 0)
2955 break;
2956 l++;
2957 }
2958 while (TRUE) {
2959 if (r < left)
2960 break;
2961 res = (*compare_func)(v[r], v[pivot]);
2962 if (res <= 0)
2963 break;
2964 r--;
2965 }
2966 // now v[l] > v[pivot] and v[r] <= v[pivot]
2967 if (l < r) {
2968 vv = v[l];
2969 v[l] = v[r];
2970 v[r] = vv;
2971 }
2972 }
2973 m = r;
2974 if (left != m) {
2975 vv = v[left];
2976 v[left] = v[m];
2977 v[m] = vv;
2978 }
2979 *middle = m;
2980}
2981
2982static void lint_vec_partition(long int *v,
2983 int (*compare_func)(long int a, long int b), int left, int right, int *middle)
2984{
2985 int l, r, m;
2986 int len, m1, res, pivot;
2987 long int vv;
2988
2989 //cout << "partition: from " << left << " to " << right << endl;
2990 // pivot strategy: take the element in the middle:
2991 len = right + 1 - left;
2992 m1 = len >> 1;
2993 pivot = left;
2994 if (m1) {
2995 vv = v[pivot];
2996 v[pivot] = v[left + m1];
2997 v[left + m1] = vv;
2998 }
2999 l = left;
3000 r = right;
3001 while (l < r) {
3002 while (TRUE) {
3003 if (l > right)
3004 break;
3005 res = (*compare_func)(v[l], v[pivot]);
3006 if (res > 0)
3007 break;
3008 l++;
3009 }
3010 while (TRUE) {
3011 if (r < left)
3012 break;
3013 res = (*compare_func)(v[r], v[pivot]);
3014 if (res <= 0)
3015 break;
3016 r--;
3017 }
3018 // now v[l] > v[pivot] and v[r] <= v[pivot]
3019 if (l < r) {
3020 vv = v[l];
3021 v[l] = v[r];
3022 v[r] = vv;
3023 }
3024 }
3025 m = r;
3026 if (left != m) {
3027 vv = v[left];
3028 v[left] = v[m];
3029 v[m] = vv;
3030 }
3031 *middle = m;
3032}
3033
3034static void partition(void **v, int *perm,
3035 int (*compare_func)(void *a, void *b, void *data), void *data,
3036 int left, int right, int *middle)
3037{
3038 int l, r, m, len, m1, res, pivot, tmp;
3039 void *vv;
3040
3041 //cout << "partition: from " << left << " to " << right << endl;
3042 // pivot strategy: take the element in the middle:
3043 len = right + 1 - left;
3044 m1 = len >> 1;
3045 pivot = left;
3046 if (m1) {
3047 vv = v[pivot];
3048 v[pivot] = v[left + m1];
3049 v[left + m1] = vv;
3050
3051 if (perm) {
3052 tmp = perm[pivot];
3053 perm[pivot] = perm[left + m1];
3054 perm[left + m1] = tmp;
3055 }
3056 }
3057 l = left;
3058 r = right;
3059 while (l < r) {
3060 while (TRUE) {
3061 if (l > right)
3062 break;
3063 res = (*compare_func)(v[l], v[pivot], data);
3064 if (res > 0)
3065 break;
3066 l++;
3067 }
3068 while (TRUE) {
3069 if (r < left)
3070 break;
3071 res = (*compare_func)(v[r], v[pivot], data);
3072 if (res <= 0)
3073 break;
3074 r--;
3075 }
3076 // now v[l] > v[pivot] and v[r] <= v[pivot]
3077 if (l < r) {
3078 vv = v[l];
3079 v[l] = v[r];
3080 v[r] = vv;
3081 if (perm) {
3082 tmp = perm[l];
3083 perm[l] = perm[r];
3084 perm[r] = tmp;
3085 }
3086 }
3087 }
3088 m = r;
3089 if (left != m) {
3090 vv = v[left];
3091 v[left] = v[m];
3092 v[m] = vv;
3093 if (perm) {
3094 tmp = perm[left];
3095 perm[left] = perm[m];
3096 perm[m] = tmp;
3097 }
3098 }
3099 *middle = m;
3100}
3101
3102static void quicksort(void **v, int *perm,
3103 int (*compare_func)(void *a, void *b, void *data), void *data,
3104 int left, int right)
3105{
3106 int middle;
3107
3108 if (left < right) {
3109 partition(v, perm, compare_func, data, left, right, &middle);
3110 quicksort(v, perm, compare_func, data, left, middle - 1);
3111 quicksort(v, perm, compare_func, data, middle + 1, right);
3112 }
3113}
3114
3115static int compare_increasingly_int(int a, int b)
3116{
3117 if (a < b)
3118 return -1;
3119 if (a > b)
3120 return 1;
3121 return 0;
3122}
3123
3124static int compare_decreasingly_int(int a, int b)
3125{
3126 if (a > b)
3127 return -1;
3128 if (a < b)
3129 return 1;
3130 return 0;
3131}
3132
3133static int compare_increasingly_lint(long int a, long int b)
3134{
3135 if (a < b)
3136 return -1;
3137 if (a > b)
3138 return 1;
3139 return 0;
3140}
3141
3142static int compare_decreasingly_lint(long int a, long int b)
3143{
3144 if (a > b)
3145 return -1;
3146 if (a < b)
3147 return 1;
3148 return 0;
3149}
3150
3151
3152
3153}}}
3154
3155
a collection of functions related to sorted vectors
int int_vec_search_first_occurence(int *v, int len, int a, int &idx, int verbose_level)
Definition: sorting.cpp:1314
void int_vec_values_and_multiplicities(int *v, int l, int *&val, int *&mult, int &nb_values)
Definition: sorting.cpp:1483
int int_vec_compare_stride(int *p, int *q, int len, int stride)
Definition: sorting.cpp:2891
void lint_vec_search_vec_linear(long int *v, int len, long int *A, int A_sz, long int *Idx)
Definition: sorting.cpp:88
void lint_vec_quicksort_increasingly(long int *v, int len)
Definition: sorting.cpp:918
int int_vec_search(int *v, int len, int a, int &idx)
Definition: sorting.cpp:1094
void heapsort_sift_down_with_log(int *v, int *w, int start, int end)
Definition: sorting.cpp:2111
void Heapsort_general_sift_down_with_log(void *data, int *w, int start, int end, int(*compare_func)(void *data, int i, int j, void *extra_data), void(*swap_func)(void *data, int i, int j, void *extra_data), void *extra_data)
Definition: sorting.cpp:2220
int lint_vec_search_linear(long int *v, int len, long int a, int &idx)
Definition: sorting.cpp:699
void lint_vec_intersect_sorted_vectors(long int *v1, int len1, long int *v2, int len2, long int *v3, int &len3)
Definition: sorting.cpp:800
void heapsort_sift_down(int *v, int start, int end)
Definition: sorting.cpp:2071
int vec_search_general(void *vec, int(*compare_func)(void *vec, void *a, int b, void *data_for_compare), void *data_for_compare, int len, void *a, int &idx, int verbose_level)
Definition: sorting.cpp:1005
void int_vec_intersect(int *v1, int len1, int *v2, int len2, int *&v3, int &len3)
Definition: sorting.cpp:712
int vector_lint_search(std::vector< long int > &v, long int a, int &idx, int verbose_level)
Definition: sorting.cpp:1235
void int_vec_sorting_permutation(int *v, int len, int *perm, int *perm_inv, int f_increasingly)
Definition: sorting.cpp:830
int test_if_sets_are_disjoint_assuming_sorted_lint(long int *set1, long int *set2, int sz1, int sz2)
Definition: sorting.cpp:2820
void sorted_vec_get_first_and_length(int *v, int len, int *class_first, int *class_len, int &nb_classes)
Definition: sorting.cpp:2904
int integer_vec_compare(int *p, int *q, int len)
Definition: sorting.cpp:2313
int test_if_set_with_return_value_lint(long int *set, int set_size)
Definition: sorting.cpp:586
void Heapsort_general_make_heap(void *data, int len, int(*compare_func)(void *data, int i, int j, void *extra_data), void(*swap_func)(void *data, int i, int j, void *extra_data), void *extra_data)
Definition: sorting.cpp:2043
int schreier_vector_determine_depth_recursion(int n, int *pts, int *prev, int f_use_pts_inv, int *pts_inv, int *depth, int *ancestor, int pos)
Definition: sorting.cpp:2369
int test_if_sets_are_disjoint(long int *set1, int sz1, long int *set2, int sz2)
Definition: sorting.cpp:519
int int_vec_sort_and_test_if_contained(int *v1, int len1, int *v2, int len2)
Definition: sorting.cpp:210
void int_vec_print_classified(std::ostream &ost, int *vec, int len)
Definition: sorting.cpp:1593
void Heapsort_sift_down(void *v, int start, int end, int entry_size_in_chars, int(*compare_func)(void *v1, void *v2))
Definition: sorting.cpp:2155
int lint_vec_compare(long int *p, long int *q, int len)
Definition: sorting.cpp:2326
void int_vec_search_vec(int *v, int len, int *A, int A_sz, int *Idx)
Definition: sorting.cpp:48
void int_vec_swap_points(int *list, int *list_inv, int idx1, int idx2)
Definition: sorting.cpp:152
int lint_vecs_find_common_element(long int *v1, int len1, long int *v2, int len2, int &idx1, int &idx2)
Definition: sorting.cpp:315
int lint_vec_sort_and_test_if_contained(long int *v1, int len1, long int *v2, int len2)
Definition: sorting.cpp:235
void int_vec_heapsort_with_log(int *v, int *w, int len)
Definition: sorting.cpp:1967
void rearrange_subset_lint(int n, int k, long int *set, int *subset, long int *rearranged_set, int verbose_level)
Definition: sorting.cpp:640
int int_vecs_find_common_element(int *v1, int len1, int *v2, int len2, int &idx1, int &idx2)
Definition: sorting.cpp:286
void int_vec_classify(int length, int *the_vec, int *&the_vec_sorted, int *&sorting_perm, int *&sorting_perm_inv, int &nb_types, int *&type_first, int *&type_len)
Definition: sorting.cpp:1503
void Heapsort_general_sift_down(void *data, int start, int end, int(*compare_func)(void *data, int i, int j, void *extra_data), void(*swap_func)(void *data, int i, int j, void *extra_data), void *extra_data)
Definition: sorting.cpp:2189
void int_vec_print_types_naked_tex_we_are_in_math_mode(std::ostream &ost, int f_backwards, int *the_vec_sorted, int nb_types, int *type_first, int *type_len)
Definition: sorting.cpp:1748
void Heapsort_general(void *data, int len, int(*compare_func)(void *data, int i, int j, void *extra_data), void(*swap_func)(void *data, int i, int j, void *extra_data), void *extra_data)
Definition: sorting.cpp:1806
int int_vecs_are_disjoint(int *v1, int len1, int *v2, int len2)
Definition: sorting.cpp:260
int test_if_set_with_return_value(int *set, int set_size)
Definition: sorting.cpp:564
void int_vec_classify_and_print(std::ostream &ost, int *v, int l)
Definition: sorting.cpp:1443
int uchar_vec_compare(uchar *p, uchar *q, int len)
Definition: sorting.cpp:2851
void int_vec_print_types_naked_stringstream(std::stringstream &sstr, int f_backwards, int *the_vec_sorted, int nb_types, int *type_first, int *type_len)
Definition: sorting.cpp:1638
void heapsort_make_heap_with_log(int *v, int *w, int len)
Definition: sorting.cpp:2013
void int_vec_classify_with_arrays(int length, int *the_vec, int *the_vec_sorted, int *sorting_perm, int *sorting_perm_inv, int &nb_types, int *type_first, int *type_len)
Definition: sorting.cpp:1527
void int_vec_quicksort(int *v, int(*compare_func)(int a, int b), int left, int right)
Definition: sorting.cpp:884
void int_vec_print_types(std::ostream &ost, int f_backwards, int *the_vec_sorted, int nb_types, int *type_first, int *type_len)
Definition: sorting.cpp:1628
void lint_vec_heapsort_with_log(long int *v, long int *w, int len)
Definition: sorting.cpp:1981
void d_quicksort(double *v, int left, int right)
Definition: sorting.cpp:2771
void Heapsort_general_with_log(void *data, int *w, int len, int(*compare_func)(void *data, int i, int j, void *extra_data), void(*swap_func)(void *data, int i, int j, void *extra_data), void *extra_data)
Definition: sorting.cpp:1827
int int_vec_is_subset_of(int *set, int sz, int *big_set, int big_set_sz)
Definition: sorting.cpp:102
void d_partition(double *v, int left, int right, int *middle)
Definition: sorting.cpp:2723
int int_vec_search_linear(int *v, int len, int a, int &idx)
Definition: sorting.cpp:686
void Heapsort(void *v, int len, int entry_size_in_chars, int(*compare_func)(void *v1, void *v2))
Definition: sorting.cpp:1790
int longinteger_vec_search(ring_theory::longinteger_object *v, int len, ring_theory::longinteger_object &a, int &idx)
Definition: sorting.cpp:1394
int int_vec_search_and_remove_if_found(int *v, int &len, int a)
Definition: sorting.cpp:1077
void quicksort_array(int len, void **v, int(*compare_func)(void *a, void *b, void *data), void *data)
Definition: sorting.cpp:929
void int_vec_append_and_reallocate_if_necessary(int *&vec, int &used_length, int &alloc_length, int a, int verbose_level)
Definition: sorting.cpp:398
void lint_vec_sort_and_remove_duplicates(long int *v, int &len)
Definition: sorting.cpp:195
void Heapsort_general_make_heap_with_log(void *data, int *w, int len, int(*compare_func)(void *data, int i, int j, void *extra_data), void(*swap_func)(void *data, int i, int j, void *extra_data), void *extra_data)
Definition: sorting.cpp:2057
void int_vec_search_vec_linear(int *v, int len, int *A, int A_sz, int *Idx)
Definition: sorting.cpp:75
void vec_intersect(long int *v1, int len1, long int *v2, int len2, long int *&v3, int &len3)
Definition: sorting.cpp:741
void rearrange_subset_lint_all(int n, int k, long int *set, long int *subset, long int *rearranged_set, int verbose_level)
Definition: sorting.cpp:663
void int_vec_multiplicities(int *v, int l, int *&w, int &w_len)
Definition: sorting.cpp:1468
void lint_vec_quicksort(long int *v, int(*compare_func)(long int a, long int b), int left, int right)
Definition: sorting.cpp:896
void schreier_vector_compute_depth_and_ancestor(int n, int *pts, int *prev, int f_use_pts_inv, int *pts_inv, int *&depth, int *&ancestor, int verbose_level)
Definition: sorting.cpp:2339
void quicksort_array_with_perm(int len, void **v, int *perm, int(*compare_func)(void *a, void *b, void *data), void *data)
Definition: sorting.cpp:937
void int_vec_values(int *v, int l, int *&w, int &w_len)
Definition: sorting.cpp:1452
int vec_search(void **v, int(*compare_func)(void *a, void *b, void *data), void *data_for_compare, int len, void *a, int &idx, int verbose_level)
Definition: sorting.cpp:945
void lint_heapsort_swap(long int *v, int i, int j)
Definition: sorting.cpp:2263
void lint_vec_search_vec(long int *v, int len, long int *A, int A_sz, long int *Idx)
Definition: sorting.cpp:61
int lint_vec_search(long int *v, int len, long int a, int &idx, int verbose_level)
Definition: sorting.cpp:1157
void find_points_by_multiplicity(int *data, int data_sz, int multiplicity, int *&pts, int &nb_pts)
Definition: sorting.cpp:2288
void Heapsort_swap(void *v, int i, int j, int entry_size_in_chars)
Definition: sorting.cpp:2272
int test_if_sets_are_equal(int *set1, int *set2, int set_size)
Definition: sorting.cpp:496
void lint_vec_quicksort_decreasingly(long int *v, int len)
Definition: sorting.cpp:923
int search_general(void *data, int len, void *search_object, int &idx, int(*compare_func)(void *data, int i, void *search_object, void *extra_data), void *extra_data, int verbose_level)
Definition: sorting.cpp:1852
int test_if_sets_are_disjoint_assuming_sorted(int *set1, int *set2, int sz1, int sz2)
Definition: sorting.cpp:2790
void lint_heapsort_sift_down(long int *v, int start, int end)
Definition: sorting.cpp:2091
int int_vec_search_and_insert_if_necessary(int *v, int &len, int a)
Definition: sorting.cpp:1060
int test_if_sets_are_disjoint_not_assuming_sorted(long int *v, long int *w, int len)
Definition: sorting.cpp:2864
void rearrange_subset(int n, int k, int *set, int *subset, int *rearranged_set, int verbose_level)
Definition: sorting.cpp:608
void int_vec_intersect_sorted_vectors(int *v1, int len1, int *v2, int len2, int *v3, int &len3)
Definition: sorting.cpp:771
void int_vec_print_types_naked_tex(std::ostream &ost, int f_backwards, int *the_vec_sorted, int nb_types, int *type_first, int *type_len)
Definition: sorting.cpp:1706
void lint_heapsort_make_heap_with_log(long int *v, long int *w, int len)
Definition: sorting.cpp:2022
void schreier_vector_tree(int n, int *pts, int *prev, int f_use_pts_inv, int *pts_inv, std::string &fname_base, graphics::layered_graph_draw_options *LG_Draw_options, graph_theory::layered_graph *&LG, int verbose_level)
Definition: sorting.cpp:2428
int compare_sets_lint(long int *set1, long int *set2, int sz1, int sz2)
Definition: sorting.cpp:2660
void int_vec_insert_and_reallocate_if_necessary(int *&vec, int &used_length, int &alloc_length, int a, int verbose_level)
Definition: sorting.cpp:344
void lint_vec_append_and_reallocate_if_necessary(long int *&vec, int &used_length, int &alloc_length, long int a, int verbose_level)
Definition: sorting.cpp:441
int compare_sets(int *set1, int *set2, int sz1, int sz2)
Definition: sorting.cpp:2617
void lint_heapsort_sift_down_with_log(long int *v, long int *w, int start, int end)
Definition: sorting.cpp:2133
int lint_vec_is_subset_of(int *set, int sz, long int *big_set, int big_set_sz, int verbose_level)
Definition: sorting.cpp:125
void int_vec_print_types_naked(std::ostream &ost, int f_backwards, int *the_vec_sorted, int nb_types, int *type_first, int *type_len)
Definition: sorting.cpp:1672
void int_vec_sorted_collect_types(int length, int *the_vec_sorted, int &nb_types, int *type_first, int *type_len)
Definition: sorting.cpp:1566
void Heapsort_make_heap(void *v, int len, int entry_size_in_chars, int(*compare_func)(void *v1, void *v2))
Definition: sorting.cpp:2031
a statistical analysis of data consisting of single integers
void init(int *data, int data_length, int f_second, int verbose_level)
Definition: tally.cpp:72
data_structures::set_of_sets * get_set_partition_and_types(int *&types, int &nb_types, int verbose_level)
Definition: tally.cpp:702
void print_file(std::ostream &ost, int f_backwards)
Definition: tally.cpp:322
void get_data_by_multiplicity(int *&Pts, int &nb_pts, int multiplicity, int verbose_level)
Definition: tally.cpp:557
a data structure to store layered graphs or Hasse diagrams
Definition: graph_theory.h:654
void draw_with_options(std::string &fname, graphics::layered_graph_draw_options *O, int verbose_level)
void write_file(std::string &fname, int verbose_level)
void add_node_data1(int l, int n, int data, int verbose_level)
void init(int nb_layers, int *Nb_nodes_layer, std::string &fname_base, int verbose_level)
void create_spanning_tree(int f_place_x, int verbose_level)
void place_with_y_stretch(double y_stretch, int verbose_level)
void add_edge(int l1, int n1, int l2, int n2, int verbose_level)
options for drawing an object of type layered_graph
Definition: graphics.h:457
domain to compute with objects of type longinteger
Definition: ring_theory.h:240
int compare(longinteger_object &a, longinteger_object &b)
a class to represent arbitrary precision integers
Definition: ring_theory.h:366
#define Lint_vec_copy(A, B, C)
Definition: foundations.h:694
#define FREE_int(p)
Definition: foundations.h:640
unsigned char uchar
Definition: foundations.h:204
#define NEW_pint(n)
Definition: foundations.h:627
#define NEW_OBJECT(type)
Definition: foundations.h:638
#define Lint_vec_print(A, B, C)
Definition: foundations.h:686
#define FREE_OBJECT(p)
Definition: foundations.h:651
int * pint
Definition: foundations.h:197
#define NEW_int(n)
Definition: foundations.h:625
#define TRUE
Definition: foundations.h:231
#define FALSE
Definition: foundations.h:234
#define Int_vec_copy(A, B, C)
Definition: foundations.h:693
#define FREE_lint(p)
Definition: foundations.h:642
#define NEW_lint(n)
Definition: foundations.h:628
#define FREE_pint(p)
Definition: foundations.h:641
#define Int_vec_print(A, B, C)
Definition: foundations.h:685
#define MAXIMUM(x, y)
Definition: foundations.h:217
the orbiter library for the classification of combinatorial objects