forked from gap-system/gap
-
Notifications
You must be signed in to change notification settings - Fork 0
/
listfunc.c
1582 lines (1371 loc) · 46 KB
/
listfunc.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/****************************************************************************
**
** This file is part of GAP, a system for computational discrete algebra.
**
** Copyright of GAP belongs to its developers, whose names are too numerous
** to list here. Please refer to the COPYRIGHT file for details.
**
** SPDX-License-Identifier: GPL-2.0-or-later
**
** This file contains the functions for generic lists.
*/
#include "listfunc.h"
#include "ariths.h"
#include "blister.h"
#include "bool.h"
#include "calls.h"
#include "error.h"
#include "io.h"
#include "lists.h"
#include "modules.h"
#include "opers.h"
#include "permutat.h"
#include "plist.h"
#include "pperm.h"
#include "set.h"
#include "stringobj.h"
#include "sysfiles.h"
#include "trans.h"
#ifdef HPCGAP
#include "hpc/aobjects.h"
#endif
/****************************************************************************
**
*F AddList(<list>,<obj>) . . . . . . . . add an object to the end of a list
**
** 'AddList' adds the object <obj> to the end of the list <list>, i.e.,
** it is equivalent to the assignment '<list>[ Length(<list>)+1 ] := <obj>'.
** The list is automatically extended to make room for the new element.
** 'AddList' returns nothing, it is called only for its side effect.
*/
static void AddList3(Obj list, Obj obj, Int pos)
{
Int len;
Int i;
len = LEN_LIST(list);
if (pos == (Int) -1)
pos = len + 1;
for (i = len +1; i > pos; i--)
ASS_LIST(list, i, ELM_LIST(list, i-1));
ASS_LIST( list, pos, obj );
}
void AddList (
Obj list,
Obj obj)
{
AddList3(list, obj, -1);
}
static void AddPlist3(Obj list, Obj obj, Int pos)
{
UInt len;
if ( ! IS_PLIST_MUTABLE(list) ) {
ErrorMayQuit("List Assignment: <list> must be a mutable list", 0, 0);
}
// in order to be optimistic when building list call assignment
len = LEN_PLIST( list );
if (pos == (Int)-1)
pos = len + 1;
if ( len == 0) {
AssPlistEmpty( list, pos, obj );
return;
}
if (pos <= len) {
GROW_PLIST(list, len+1);
SET_LEN_PLIST(list, len+1);
Obj * ptr = ADDR_OBJ(list) + pos;
SyMemmove(ptr + 1, ptr, sizeof(Obj) * (len - pos + 1));
}
ASS_LIST(list, pos, obj);
}
void AddPlist (
Obj list,
Obj obj)
{
AddPlist3(list, obj, -1);
}
static Obj AddListOper;
static Obj FuncADD_LIST3(Obj self, Obj list, Obj obj, Obj pos)
{
// dispatch
Int ipos;
if (pos == (Obj)0)
ipos = -1;
else if (IS_POS_INTOBJ(pos))
ipos = INT_INTOBJ(pos);
else {
DoOperation3Args( self, list, obj, pos);
return (Obj) 0;
}
UInt tnum = TNUM_OBJ(list);
if ( IS_PLIST( list ) ) {
AddPlist3( list, obj, ipos );
} else if ( FIRST_LIST_TNUM <= tnum && tnum <= LAST_LIST_TNUM ) {
AddList3( list, obj, ipos );
#ifdef HPCGAP
// Only support adding to end of atomic lists
} else if ( tnum == T_ALIST && pos == (Obj)0 ) {
AddAList( list, obj );
#endif
} else {
if (pos == 0)
DoOperation2Args( self, list, obj );
else
DoOperation3Args( self, list, obj, pos);
}
return 0;
}
static Obj FuncADD_LIST(Obj self, Obj list, Obj obj)
{
FuncADD_LIST3(self, list, obj, (Obj)0);
return (Obj) 0;
}
/****************************************************************************
**
*F RemList(<list>) . . . . . . . . remove an object from the end of a list
**
** 'RemList' removes the last object <obj> from the end of the list <list>,
** and returns it.
*/
static Obj RemList(Obj list)
{
Int pos;
Obj result;
pos = LEN_LIST( list ) ;
if ( pos == 0 ) {
ErrorMayQuit("Remove: <list> must not be empty", 0, 0);
}
result = ELM_LIST(list, pos);
UNB_LIST(list, pos);
return result;
}
static Obj RemPlist(Obj list)
{
Int pos;
Obj removed;
if ( ! IS_PLIST_MUTABLE(list) ) {
ErrorMayQuit("Remove: <list> must be a mutable list", 0, 0);
}
pos = LEN_PLIST( list );
if ( pos == 0 ) {
ErrorMayQuit("Remove: <list> must not be empty", 0, 0);
}
removed = ELM_PLIST(list, pos);
SET_ELM_PLIST(list, pos, 0);
pos--;
while ( 1 <= pos && ELM_PLIST( list, pos ) == 0 ) { pos--; }
SET_LEN_PLIST(list, pos);
if ( pos == 0 ) {
RetypeBag(list, T_PLIST_EMPTY);
}
if (4*pos*sizeof(Obj) < 3*SIZE_BAG(list))
SHRINK_PLIST(list, pos);
return removed;
}
static Obj RemListOper;
static Obj FuncREM_LIST(Obj self, Obj list)
{
// dispatch
if ( IS_PLIST( list ) ) {
return RemPlist( list);
}
else if ( TNUM_OBJ( list ) < FIRST_EXTERNAL_TNUM ) {
return RemList( list);
}
else {
return DoOperation1Args( self, list);
}
}
/****************************************************************************
**
*F FuncAPPEND_LIST_INTR(<list1>,<list2>) . . . . . append elements to a list
**
** 'FuncAPPEND_LIST_INTR' implements the function 'Append'.
**
** 'Append(<list1>,<list2>)'
**
** 'Append' adds (see "Add") the elements of the list <list2> to the end
** of the list <list1>. It is allowed that <list2> contains empty positions,
** in which case the corresponding positions will be left empty in <list1>.
** 'Append' returns nothing, it is called only for its side effect.
*/
static Obj FuncAPPEND_LIST_INTR(Obj self, Obj list1, Obj list2)
{
UInt len1; // length of the first list
UInt len2; // length of the second list
Obj elm; // one element of the second list
Int i; // loop variable
RequireMutable(SELF_NAME, list1, "list");
RequireSmallList(SELF_NAME, list1);
RequireSmallList(SELF_NAME, list2);
// handle the case of strings now
if (IS_STRING_REP(list1) && IS_STRING(list2)) {
if (!IS_STRING_REP(list2)) {
list2 = ImmutableString(list2);
}
AppendString(list1, list2);
return 0;
}
// check the type of the first argument
if ( TNUM_OBJ( list1 ) != T_PLIST ) {
if ( ! IS_PLIST( list1 ) ) {
PLAIN_LIST( list1 );
}
RetypeBag( list1, T_PLIST );
}
len1 = LEN_PLIST( list1 );
// check the type of the second argument
if ( ! IS_PLIST( list2 ) ) {
len2 = LEN_LIST( list2 );
}
else {
len2 = LEN_PLIST( list2 );
}
// if the list has no room at the end, enlarge it
if ( 0 < len2 ) {
GROW_PLIST( list1, len1+len2 );
SET_LEN_PLIST( list1, len1+len2 );
}
// add the elements
if ( IS_PLIST(list2) ) {
// note that the two memory regions can never overlap, even
// if list1 and list2 are identical
memcpy(ADDR_OBJ(list1) + 1 + len1, CONST_ADDR_OBJ(list2) + 1,
len2 * sizeof(Obj));
CHANGED_BAG( list1 );
}
else {
for ( i = 1; i <= len2; i++ ) {
elm = ELMV0_LIST( list2, i );
SET_ELM_PLIST( list1, i+len1, elm );
CHANGED_BAG( list1 );
}
}
return 0;
}
static Obj AppendListOper;
static Obj FuncAPPEND_LIST(Obj self, Obj list, Obj obj)
{
// dispatch
if ( TNUM_OBJ( list ) < FIRST_EXTERNAL_TNUM ) {
FuncAPPEND_LIST_INTR( 0, list, obj );
}
else {
DoOperation2Args( self, list, obj );
}
return 0;
}
/****************************************************************************
**
*F POSITION_SORTED_LIST(<list>,<obj>) . . . . find an object in a sorted list
*F PositionSortedDensePlist(<list>,<obj>) . find an object in a sorted list
**
** 'POSITION_SORTED_LIST' returns the position of the object <obj>, which may
** be an object of any type, with respect to the sorted list <list>.
**
** 'POSITION_SORTED_LIST' returns <pos> such that '<list>[<pos>-1] < <obj>'
** and '<obj> <= <list>[<pos>]'. That means if <obj> appears once in <list>
** its position is returned. If <obj> appears several times in <list>, the
** position of the first occurrence is returned. If <obj> is not an element
** of <list>, the index where <obj> must be inserted to keep the list sorted
** is returned.
*/
static UInt POSITION_SORTED_LIST(Obj list, Obj obj)
{
UInt l; // low
UInt h; // high
UInt m; // mid
Obj v; // one element of the list
// perform the binary search to find the position
l = 0; h = LEN_LIST( list ) + 1;
while ( l+1 < h ) { // list[l] < obj && obj <= list[h]
m = (l + h) / 2; // l < m < h
v = ELMV_LIST( list, m );
if ( LT( v, obj ) ) { l = m; }
else { h = m; }
}
// return the position
return h;
}
UInt PositionSortedDensePlist (
Obj list,
Obj obj )
{
UInt l; // low
UInt h; // high
UInt m; // mid
Obj v; // one element of the list
// perform the binary search to find the position
l = 0; h = LEN_PLIST( list ) + 1;
while ( l+1 < h ) { // list[l] < obj && obj <= list[h]
m = (l + h) / 2; // l < m < h
v = ELM_PLIST( list, m );
if ( LT( v, obj ) ) { l = m; }
else { h = m; }
}
// return the position
return h;
}
static Obj FuncPOSITION_SORTED_LIST(Obj self, Obj list, Obj obj)
{
RequireSmallList(SELF_NAME, list);
UInt h;
if ( IS_DENSE_PLIST(list) ) {
h = PositionSortedDensePlist( list, obj );
}
else {
h = POSITION_SORTED_LIST( list, obj );
}
return INTOBJ_INT( h );
}
/****************************************************************************
**
*F POSITION_SORTED_LISTComp(<list>,<obj>,<func>) . . find an object in a list
*F PositionSortedDensePlistComp(<list>,<obj>,<func>)find an object in a list
**
** 'POSITION_SORTED_LISTComp' returns the position of the object <obj>, which
** may be an object of any type, with respect to the list <list>, which is
** sorted with respect to the comparison function <func>.
**
** 'POSITION_SORTED_LISTComp' returns <pos> such that '<list>[<pos>-1] < <obj>'
** and '<obj> <= <list>[<pos>]'. That means if <obj> appears once in <list>
** its position is returned. If <obj> appears several times in <list>, the
** position of the first occurrence is returned. If <obj> is not an element
** of <list>, the index where <obj> must be inserted to keep the list sorted
** is returned.
*/
static UInt POSITION_SORTED_LISTComp(Obj list, Obj obj, Obj func)
{
UInt l; // low
UInt h; // high
UInt m; // mid
Obj v; // one element of the list
// perform the binary search to find the position
l = 0; h = LEN_LIST( list ) + 1;
while ( l+1 < h ) { // list[l] < obj && obj <= list[h]
m = (l + h) / 2; // l < m < h
v = ELMV_LIST( list, m );
if ( CALL_2ARGS( func, v, obj ) == True ) { l = m; }
else { h = m; }
}
// return the position
return h;
}
static UInt PositionSortedDensePlistComp(Obj list, Obj obj, Obj func)
{
UInt l; // low
UInt h; // high
UInt m; // mid
Obj v; // one element of the list
// perform the binary search to find the position
l = 0; h = LEN_PLIST( list ) + 1;
while ( l+1 < h ) { // list[l] < obj && obj <= list[h]
m = (l + h) / 2; // l < m < h
v = ELM_PLIST( list, m );
if ( CALL_2ARGS( func, v, obj ) == True ) { l = m; }
else { h = m; }
}
// return the position
return h;
}
static Obj
FuncPOSITION_SORTED_LIST_COMP(Obj self, Obj list, Obj obj, Obj func)
{
RequireSmallList(SELF_NAME, list);
RequireFunction(SELF_NAME, func);
UInt h;
if ( IS_DENSE_PLIST(list) ) {
h = PositionSortedDensePlistComp( list, obj, func );
}
else {
h = POSITION_SORTED_LISTComp( list, obj, func );
}
return INTOBJ_INT( h );
}
/****************************************************************************
**
** Low-level implementations of PositionSortedBy for dense Plists and lists.
*/
static Obj FuncPOSITION_SORTED_BY(Obj self, Obj list, Obj val, Obj func)
{
RequirePlainList(SELF_NAME, list);
RequireFunction(SELF_NAME, func);
// perform the binary search to find the position
UInt l = 0;
UInt h = LEN_PLIST(list) + 1;
while (l + 1 < h) { // list[l] < val && val <= list[h]
UInt m = (l + h) / 2; // l < m < h
Obj v = CALL_1ARGS(func, ELM_PLIST(list, m));
if (LT(v, val)) {
l = m;
}
else {
h = m;
}
}
return INTOBJ_INT(h);
}
/****************************************************************************
**
*F SORT_LIST( <list> ) . . . . . . . . . . . . . . . . . . . . sort a list
*F SortDensePlist( <list> ) . . . . . . . . . . . . . . . . . . sort a list
**
** 'SORT_LIST' sorts the list <list> in increasing order.
*/
// See sortbase.h for a description of these macros.
// We put these first, as they are the same for the next 4 functions so
// we do not have to repeat them
#define SORT_CREATE_TEMP_BUFFER(len) NEW_PLIST( T_PLIST, len + 1000);
#define SORT_ASS_BUF_TO_LOCAL(buffer, t, i) t = ELM_PLIST(buffer, i);
#define SORT_ASS_LOCAL_TO_BUF(buffer, i, j) \
SET_ELM_PLIST(buffer, i, j); \
CHANGED_BAG(buffer);
#define SORT_FUNC_NAME SORT_LIST
#define SORT_FUNC_ARGS Obj list
#define SORT_ARGS list
#define SORT_CREATE_LOCAL(name) Obj name ;
#define SORT_LEN_LIST() LEN_LIST(list)
#define SORT_ASS_LIST_TO_LOCAL(t, i) t = ELMV_LIST(list, i)
#define SORT_ASS_LOCAL_TO_LIST(i, j) ASS_LIST(list, i, j)
#define SORT_COMP(v, w) LT(v, w)
#define SORT_FILTER_CHECKS() \
if(IS_PLIST(list)) \
RESET_FILT_LIST(list, FN_IS_NSORT);
#include "sortbase.h"
#define SORT_FUNC_NAME SortDensePlist
#define SORT_FUNC_ARGS Obj list
#define SORT_ARGS list
#define SORT_CREATE_LOCAL(name) Obj name ;
#define SORT_LEN_LIST() LEN_PLIST(list)
#define SORT_ASS_LIST_TO_LOCAL(t, i) t = ELM_PLIST(list, i)
#define SORT_ASS_LOCAL_TO_LIST(i, j) \
SET_ELM_PLIST(list, i, j); \
CHANGED_BAG(list);
#define SORT_COMP(v, w) LT(v, w)
#define SORT_FILTER_CHECKS() \
RESET_FILT_LIST(list, FN_IS_NSORT);
#include "sortbase.h"
// This is a variant of SortDensePlist, which sorts plists by
// Obj pointer. It works on non-dense plists, and can be
// used to efficiently sort lists of small integers.
#define SORT_FUNC_NAME SortPlistByRawObj
#define SORT_FUNC_ARGS Obj list
#define SORT_ARGS list
#define SORT_CREATE_LOCAL(name) Obj name;
#define SORT_LEN_LIST() LEN_PLIST(list)
#define SORT_ASS_LIST_TO_LOCAL(t, i) t = ELM_PLIST(list, i)
#define SORT_ASS_LOCAL_TO_LIST(i, j) SET_ELM_PLIST(list, i, j);
#define SORT_COMP(v, w) ((v) < (w))
#define SORT_FILTER_CHECKS() \
RESET_FILT_LIST(list, FN_IS_NSORT); \
RESET_FILT_LIST(list, FN_IS_SSORT);
#include "sortbase.h"
/****************************************************************************
**
*F SORT_LISTComp(<list>,<func>) . . . . . . . . . . . . . . . . sort a list
*F SortDensePlistComp(<list>,<func>) . . . . . . . . . . . . . . sort a list
**
** 'SORT_LISTComp' sorts the list <list> in increasing order, with respect to
** comparison function <func>.
*/
#define SORT_FUNC_NAME SORT_LISTComp
#define SORT_FUNC_ARGS Obj list, Obj func
#define SORT_ARGS list, func
#define SORT_CREATE_LOCAL(name) Obj name ;
#define SORT_LEN_LIST() LEN_LIST(list)
#define SORT_ASS_LIST_TO_LOCAL(t, i) t = ELMV_LIST(list, i)
#define SORT_ASS_LOCAL_TO_LIST(i, j) ASS_LIST(list, i, j)
#define SORT_COMP(v, w) CALL_2ARGS(func, v, w) == True
// list is not necc. sorted wrt. \< (any longer)
#define SORT_FILTER_CHECKS() \
RESET_FILT_LIST(list, FN_IS_SSORT); \
RESET_FILT_LIST(list, FN_IS_NSORT);
#include "sortbase.h"
#define SORT_FUNC_NAME SortDensePlistComp
#define SORT_FUNC_ARGS Obj list, Obj func
#define SORT_ARGS list, func
#define SORT_CREATE_LOCAL(name) Obj name ;
#define SORT_LEN_LIST() LEN_PLIST(list)
#define SORT_ASS_LIST_TO_LOCAL(t, i) t = ELM_PLIST(list, i)
#define SORT_ASS_LOCAL_TO_LIST(i, j) \
SET_ELM_PLIST(list, i, j); \
CHANGED_BAG(list);
#define SORT_COMP(v, w) CALL_2ARGS(func, v, w) == True
// list is not necc. sorted wrt. \< (any longer)
#define SORT_FILTER_CHECKS() \
RESET_FILT_LIST(list, FN_IS_SSORT); \
RESET_FILT_LIST(list, FN_IS_NSORT);
#include "sortbase.h"
/****************************************************************************
**
*F SORT_PARA_LIST( <list> ) . . . . . . . . . . . sort a lists with shadow
*F SortParaDensePlistPara( <list> ) . . . . . . . sort a lists with shadow
*F SORT_PARA_LISTComp(<list>,<func>) . . . . . . . sort a lists with shadow
*F SortParaDensePlistComp(<list>,<func>) . . . . . sort a lists with shadow
**
** The following suite of functions mirrors the sort functions above. They
** sort the first list given and perform the same operations on the second
** list, the shadow list. All functions assume that shadow list has (at
** least) the length of the first list.
**
** The code here is a duplication of the code above with the operations on
** the second list added in.
*/
// Through this section, code of the form (void)(varname); stops
// various compilers warning about unused variables.
// These 3 macros are the same for all 4 of the following functions.
#undef SORT_CREATE_TEMP_BUFFER
#undef SORT_ASS_BUF_TO_LOCAL
#undef SORT_ASS_LOCAL_TO_BUF
#define SORT_CREATE_TEMP_BUFFER(len) NEW_PLIST( T_PLIST, len * 2 + 1000);
#define SORT_ASS_BUF_TO_LOCAL(buffer, t, i) \
t = ELM_PLIST(buffer, 2*(i)); \
t##s = ELM_PLIST(buffer, 2*(i)-1); (void)(t##s)
#define SORT_ASS_LOCAL_TO_BUF(buffer, i, j) \
SET_ELM_PLIST(buffer, 2*(i), j); \
SET_ELM_PLIST(buffer, 2*(i)-1, j##s); \
CHANGED_BAG(buffer);
#define SORT_FUNC_NAME SORT_PARA_LIST
#define SORT_FUNC_ARGS Obj list, Obj shadow
#define SORT_ARGS list, shadow
#define SORT_CREATE_LOCAL(name) Obj name ; Obj name##s ; (void)(name##s) ;
#define SORT_LEN_LIST() LEN_LIST(list)
#define SORT_ASS_LIST_TO_LOCAL(t, i) \
t = ELMV_LIST(list, i); \
t##s = ELMV_LIST(shadow, i);
#define SORT_ASS_LOCAL_TO_LIST(i, t) \
ASS_LIST(list, i, t); \
ASS_LIST(shadow, i, t##s);
#define SORT_COMP(v, w) LT( v, w )
/* if list was ssorted, then it still will be,
but, we don't know anything else any more */
#define SORT_FILTER_CHECKS() \
RESET_FILT_LIST(list, FN_IS_NSORT); \
RESET_FILT_LIST(shadow, FN_IS_SSORT); \
RESET_FILT_LIST(shadow, FN_IS_NSORT);
#include "sortbase.h"
#define SORT_FUNC_NAME SortParaDensePlist
#define SORT_FUNC_ARGS Obj list, Obj shadow
#define SORT_ARGS list, shadow
#define SORT_CREATE_LOCAL(name) Obj name ; Obj name##s ; (void)(name##s) ;
#define SORT_LEN_LIST() LEN_PLIST(list)
#define SORT_ASS_LIST_TO_LOCAL(t, i) \
t = ELM_PLIST(list, i); \
t##s = ELM_PLIST(shadow, i);
#define SORT_ASS_LOCAL_TO_LIST(i, t) \
SET_ELM_PLIST(list, i, t); \
SET_ELM_PLIST(shadow, i, t##s); \
CHANGED_BAG(list); \
CHANGED_BAG(shadow);
#define SORT_COMP(v, w) LT( v, w )
/* if list was ssorted, then it still will be,
but, we don't know anything else any more */
#define SORT_FILTER_CHECKS() \
RESET_FILT_LIST(list, FN_IS_NSORT); \
RESET_FILT_LIST(shadow, FN_IS_SSORT); \
RESET_FILT_LIST(shadow, FN_IS_NSORT);
#include "sortbase.h"
#define SORT_FUNC_NAME SORT_PARA_LISTComp
#define SORT_FUNC_ARGS Obj list, Obj shadow, Obj func
#define SORT_ARGS list, shadow, func
#define SORT_CREATE_LOCAL(name) Obj name ; Obj name##s ; (void)(name##s) ;
#define SORT_LEN_LIST() LEN_LIST(list)
#define SORT_ASS_LIST_TO_LOCAL(t, i) \
t = ELMV_LIST(list, i); \
t##s = ELMV_LIST(shadow, i);
#define SORT_ASS_LOCAL_TO_LIST(i, t) \
ASS_LIST(list, i, t); \
ASS_LIST(shadow, i, t##s);
#define SORT_COMP(v, w) CALL_2ARGS( func, v, w ) == True
// list is not necc. sorted wrt. \< (any longer)
#define SORT_FILTER_CHECKS() \
RESET_FILT_LIST(list, FN_IS_SSORT); \
RESET_FILT_LIST(list, FN_IS_NSORT); \
RESET_FILT_LIST(shadow, FN_IS_NSORT); \
RESET_FILT_LIST(shadow, FN_IS_SSORT);
#include "sortbase.h"
#define SORT_FUNC_NAME SortParaDensePlistComp
#define SORT_FUNC_ARGS Obj list, Obj shadow, Obj func
#define SORT_ARGS list, shadow, func
#define SORT_CREATE_LOCAL(name) Obj name ; Obj name##s ; (void)(name##s) ;
#define SORT_LEN_LIST() LEN_PLIST(list)
#define SORT_ASS_LIST_TO_LOCAL(t, i) \
t = ELM_PLIST(list, i); \
t##s = ELM_PLIST(shadow, i);
#define SORT_ASS_LOCAL_TO_LIST(i, t) \
SET_ELM_PLIST(list, i, t); \
SET_ELM_PLIST(shadow, i, t##s); \
CHANGED_BAG(list); \
CHANGED_BAG(shadow);
#define SORT_COMP(v, w) CALL_2ARGS( func, v, w ) == True
// list is not necc. sorted wrt. \< (any longer)
#define SORT_FILTER_CHECKS() \
RESET_FILT_LIST(list, FN_IS_SSORT); \
RESET_FILT_LIST(list, FN_IS_NSORT); \
RESET_FILT_LIST(shadow, FN_IS_NSORT); \
RESET_FILT_LIST(shadow, FN_IS_SSORT);
#include "sortbase.h"
/****************************************************************************
**
*F RemoveDupsDensePlist(<list>) . . . . remove duplicates from a plain list
**
** 'RemoveDupsDensePlist' removes duplicate elements from the dense
** plain list <list>. <list> must be sorted. 'RemoveDupsDensePlist'
** returns 0 if <list> contains mutable elements, 1 if not and 2 if
** the list contains immutable elements all lying in the same family.
*/
UInt RemoveDupsDensePlist (
Obj list )
{
UInt mutable; // the elements are mutable
UInt homog; // the elements all lie in the same family
Int len; // length of the list
Obj v, w; // two elements of the list
UInt l, i; // loop variables
Obj fam;
// get the length, nothing to be done for empty lists
len = LEN_PLIST( list );
if ( len == 0 ) { return 0; }
// select the first element as the first representative
l = 1;
v = ELM_PLIST( list, l );
mutable = IS_MUTABLE_OBJ(v);
homog = 1;
fam = FAMILY_OBJ(v);
// loop over the other elements, compare them with the current rep.
for ( i = 2; i <= len; i++ ) {
w = ELM_PLIST( list, i );
mutable = (mutable || IS_MUTABLE_OBJ(w));
if ( ! EQ( v, w ) ) {
if ( l+1 != i ) {
SET_ELM_PLIST( list, l+1, w );
SET_ELM_PLIST( list, i, (Obj)0 );
}
l += 1;
v = w;
homog = (!mutable && homog && fam == FAMILY_OBJ(w));
}
}
// the list may be shorter now
SET_LEN_PLIST( list, l );
SHRINK_PLIST( list, l );
// Set appropriate filters
if (!mutable)
{
if (!homog)
SET_FILT_LIST(list, FN_IS_NHOMOG);
else
SET_FILT_LIST(list, FN_IS_HOMOG);
SET_FILT_LIST(list, FN_IS_SSORT);
}
// return whether the list contains mutable elements
if (mutable)
return 0;
if (!homog)
return 1;
else
return 2;
}
/****************************************************************************
**
*F * * * * * * * * * * * * * * GAP level functions * * * * * * * * * * * * *
*/
/****************************************************************************
**
*F FuncSORT_LIST( <self>, <list> ) . . . . . . . . . . . . . . . sort a list
*/
static Obj FuncSORT_LIST(Obj self, Obj list)
{
RequireSmallList(SELF_NAME, list);
if ( IS_DENSE_PLIST(list) ) {
SortDensePlist( list );
}
else {
SORT_LIST( list );
}
IS_SSORT_LIST(list);
return 0;
}
static Obj FuncSTABLE_SORT_LIST(Obj self, Obj list)
{
RequireSmallList(SELF_NAME, list);
if ( IS_DENSE_PLIST(list) ) {
SortDensePlistMerge( list );
}
else {
SORT_LISTMerge( list );
}
IS_SSORT_LIST(list);
return 0;
}
/****************************************************************************
**
*F FuncSORT_LIST_COMP( <self>, <list>, <func> ) . . . . . . . . sort a list
*/
static Obj FuncSORT_LIST_COMP(Obj self, Obj list, Obj func)
{
RequireSmallList(SELF_NAME, list);
RequireFunction(SELF_NAME, func);
if ( IS_DENSE_PLIST(list) ) {
SortDensePlistComp( list, func );
}
else {
SORT_LISTComp( list, func );
}
return 0;
}
static Obj FuncSTABLE_SORT_LIST_COMP(Obj self, Obj list, Obj func)
{
RequireSmallList(SELF_NAME, list);
RequireFunction(SELF_NAME, func);
if ( IS_DENSE_PLIST(list) ) {
SortDensePlistCompMerge( list, func );
}
else {
SORT_LISTCompMerge( list, func );
}
return 0;
}
/****************************************************************************
**
*F FuncSORT_PARA_LIST( <self>, <list> ) . . . . . . sort a list with shadow
*/
static Obj FuncSORT_PARA_LIST(Obj self, Obj list, Obj shadow)
{
RequireSmallList(SELF_NAME, list);
RequireSmallList(SELF_NAME, shadow);
RequireSameLength(SELF_NAME, list, shadow);
if ( IS_DENSE_PLIST(list) && IS_DENSE_PLIST(shadow) ) {
SortParaDensePlist( list, shadow );
}
else {
SORT_PARA_LIST( list, shadow );
}
IS_SSORT_LIST(list);
return 0;
}
static Obj FuncSTABLE_SORT_PARA_LIST(Obj self, Obj list, Obj shadow)
{
RequireSmallList(SELF_NAME, list);
RequireSmallList(SELF_NAME, shadow);
RequireSameLength(SELF_NAME, list, shadow);
if ( IS_DENSE_PLIST(list) && IS_DENSE_PLIST(shadow) ) {
SortParaDensePlistMerge( list, shadow );
}
else {
SORT_PARA_LISTMerge( list, shadow );
}
IS_SSORT_LIST(list);
return 0;
}
/****************************************************************************
**
*F FuncSORT_LIST_COMP( <self>, <list>, <func> ) . . . . . . . . sort a list
*/
static Obj FuncSORT_PARA_LIST_COMP(Obj self, Obj list, Obj shadow, Obj func)
{
RequireSmallList(SELF_NAME, list);
RequireSmallList(SELF_NAME, shadow);
RequireSameLength(SELF_NAME, list, shadow);
RequireFunction(SELF_NAME, func);
if ( IS_DENSE_PLIST(list) && IS_DENSE_PLIST(shadow) ) {
SortParaDensePlistComp( list, shadow, func );
}
else {
SORT_PARA_LISTComp( list, shadow, func );
}
return 0;
}
static Obj
FuncSTABLE_SORT_PARA_LIST_COMP(Obj self, Obj list, Obj shadow, Obj func)
{
RequireSmallList(SELF_NAME, list);
RequireSmallList(SELF_NAME, shadow);
RequireSameLength(SELF_NAME, list, shadow);
RequireFunction(SELF_NAME, func);
if ( IS_DENSE_PLIST(list) && IS_DENSE_PLIST(shadow) ) {
SortParaDensePlistCompMerge( list, shadow, func );
}
else {
SORT_PARA_LISTCompMerge( list, shadow, func );
}
return 0;
}
/****************************************************************************
**
*F FuncOnPoints( <self>, <point>, <elm> ) . . . . . . . operation on points
**
** 'FuncOnPoints' implements the internal function 'OnPoints'.
**
** 'OnPoints( <point>, <elm> )'
**
** specifies the canonical default operation. Passing this function is
** equivalent to specifying no operation. This function exists because
** there are places where the operation in not an option.
*/
static Obj FuncOnPoints(Obj self, Obj point, Obj elm)
{
return POW( point, elm );
}
/****************************************************************************
**
*F FuncOnPairs( <self>, <pair>, <elm> ) . . . operation on pairs of points
**
** 'FuncOnPairs' implements the internal function 'OnPairs'.
**
** 'OnPairs( <pair>, <elm> )'
**
** specifies the componentwise operation of group elements on pairs of
** points, which are represented by lists of length 2.
*/
static Obj FuncOnPairs(Obj self, Obj pair, Obj elm)
{
Obj img; // image, result
Obj tmp; // temporary
RequireSmallList(SELF_NAME, pair);
if (LEN_LIST(pair) != 2) {
ErrorMayQuit("OnPairs: <pair> must have length 2, not length %d",
LEN_LIST(pair), 0);
}
// create a new bag for the result
img = NEW_PLIST_WITH_MUTABILITY( IS_MUTABLE_OBJ(pair), T_PLIST, 2 );
SET_LEN_PLIST( img, 2 );
// and enter the images of the points into the result bag
tmp = POW( ELMV_LIST( pair, 1 ), elm );
SET_ELM_PLIST( img, 1, tmp );
CHANGED_BAG( img );
tmp = POW( ELMV_LIST( pair, 2 ), elm );
SET_ELM_PLIST( img, 2, tmp );
CHANGED_BAG( img );
return img;
}
/****************************************************************************
**
*F FuncOnTuples( <self>, <tuple>, <elm> ) . . operation on tuples of points
**
** 'FuncOnTuples' implements the internal function 'OnTuples'.
**
** 'OnTuples( <tuple>, <elm> )'
**
** specifies the componentwise operation of group elements on tuples of
** points, which are represented by lists. 'OnPairs' is the special case of
** 'OnTuples' for tuples with two elements.
*/
static Obj FuncOnTuples(Obj self, Obj tuple, Obj elm)
{
Obj img; // image, result
Obj tmp; // temporary
UInt i; // loop variable
RequireSmallList(SELF_NAME, tuple);
// special case for the empty list
if (LEN_LIST(tuple) == 0) {
if (IS_MUTABLE_OBJ(tuple)) {