forked from gap-system/gap
-
Notifications
You must be signed in to change notification settings - Fork 0
/
lists.c
2523 lines (2085 loc) · 76.6 KB
/
lists.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 of the generic list package.
**
** This package provides a uniform interface to the functions that access
** lists and their elements for the other packages in the GAP kernel. For
** example, 'ExecFor' can loop over the elements in a list using 'LEN_LIST'
** and 'ELM_LIST' independently of the type of the list.
**
** This package uses plain lists (of type 'T_PLIST') and assumes that it is
** possible to put values of any type into these. It uses the functions
** 'LEN_PLIST', 'SET_LEN_PLIST', 'ELM_PLIST', and 'SET_ELM_PLIST' exported
** by the plain list package to access and modify plain lists.
*/
#include "lists.h"
#include "ariths.h"
#include "bool.h"
#include "calls.h"
#include "error.h"
#include "gaputils.h"
#include "integer.h"
#include "io.h"
#include "modules.h"
#include "opers.h"
#include "plist.h"
#include "precord.h"
#include "range.h"
#include "records.h"
#include "stringobj.h"
#ifdef HPCGAP
#include "hpc/aobjects.h"
#include "hpc/guards.h"
#endif
/****************************************************************************
**
*F IS_LIST(<obj>) . . . . . . . . . . . . . . . . . . . is an object a list
*V IsListFuncs[<type>] . . . . . . . . . . . . . . . . . table for list test
**
** 'IS_LIST' only calls the function pointed to by 'IsListFuncs[<type>]',
** passing <obj> as argument.
*/
BOOL (*IsListFuncs[LAST_REAL_TNUM + 1])(Obj obj);
static Obj IsListFilt;
static Obj FiltIS_LIST(Obj self, Obj obj)
{
return (IS_LIST( obj ) ? True : False);
}
static BOOL IsListObject(Obj obj)
{
return (DoFilter( IsListFilt, obj ) == True);
}
/****************************************************************************
**
*F IS_SMALL_LIST(<obj>) . . . . . . . . . . . . . . . . . . . is an object a list
*V IsListFuncs[<type>] . . . . . . . . . . . . . . . . . table for list test
**
** 'IS_SMALL_LIST' only calls the function pointed to by 'IsListFuncs[<type>]',
** passing <obj> as argument.
**
** This is, in some sense, a workaround for the not yet implemented features
** below (see LENGTH).
*/
BOOL (*IsSmallListFuncs[LAST_REAL_TNUM + 1])(Obj obj);
static Obj IsSmallListFilt;
static Obj HasIsSmallListFilt;
static Obj LengthAttr;
static Obj SetIsSmallList;
static BOOL IsSmallListObject(Obj obj)
{
Obj len;
if (DoFilter(IsListFilt, obj) != True)
return FALSE;
if (DoFilter(HasIsSmallListFilt, obj) == True)
return DoFilter(IsSmallListFilt, obj) == True;
if (DoTestAttribute(LengthAttr, obj) == True)
{
len = DoAttribute(LengthAttr, obj);
if (IS_INTOBJ(len))
{
CALL_2ARGS(SetIsSmallList, obj, True);
return TRUE;
}
else
{
CALL_2ARGS(SetIsSmallList, obj, False);
return FALSE;
}
}
return 0;
}
/****************************************************************************
**
*F AttrLENGTH( <self>, <list> ) . . . . . . . . . . . 'Length' interface
**
** There are the ``relatively'' easy changes to 'LEN_LIST' to allow it
** return GAP objects instead of small C integers, but then the kernel has
** to be very careful not to assume that the length is small and most of the
** code has to duplicated, namely one large and one small version. So
** instead the following solution has been taken:
**
** - internal lists have always a small length, that means that it is not
** possible to have plain list of length larger than 2^28 (or maybe 2^32)
** on 32-bit machines, 'LEN_LIST' can only be applied to internal objects,
** 'LENGTH' is the GAP interface for all kind of objects
**
** - on the other hand we want ranges to have large start and end points,
** therefore ranges are no longer *internal* objects, they are now
** external objects (NOT YET IMPLEMENTED)
**
** - the for/list assignment has to be careful to catch the special case of
** a range constructor with small integer bounds
**
** - the list access/assignment is a binary operation (NOT YET IMPLEMENTED)
**
** - the conversion/test functions are split into three different functions
** (NOT YET IMPLEMENTED)
**
** - 'ResetFilterObj' and 'SetFilterObj' are implemented using a table for
** internal types (NOT YET IMPLEMENTED)
*/
static Obj AttrLENGTH(Obj self, Obj list)
{
// internal list types
#ifdef HPCGAP
ReadGuard(list);
ImpliedWriteGuard(list);
if ( (FIRST_LIST_TNUM<=TNUM_OBJ(list) && TNUM_OBJ(list)<=LAST_LIST_TNUM)
|| TNUM_OBJ(list) == T_ALIST || TNUM_OBJ(list) == T_FIXALIST) {
return ObjInt_Int( LEN_LIST(list) );
}
#else
if ( FIRST_LIST_TNUM<=TNUM_OBJ(list) && TNUM_OBJ(list)<=LAST_LIST_TNUM) {
return ObjInt_Int( LEN_LIST(list) );
}
#endif
// external types
else {
return DoAttribute( LengthAttr, list );
}
}
/****************************************************************************
**
*F LEN_LIST(<list>) . . . . . . . . . . . . . . . . . . . length of a list
*V LenListFuncs[<type>] . . . . . . . . . . . . . table of length functions
*F LenListError(<list>) . . . . . . . . . . . . . . . error length function
**
** 'LEN_LIST' only calls the function pointed to by 'LenListFuncs[<type>]',
** passing <list> as argument. If <type> is not the type of a list, then
** 'LenListFuncs[<type>]' points to 'LenListError', which just signals an
** error.
**
** At the moment this also handles external types but this is a hack,
** because external lists can have large length or even be infinite. See
** 'AttrLENGTH'.
*/
Int (*LenListFuncs[LAST_REAL_TNUM+1]) ( Obj list );
static Obj FuncLEN_LIST(Obj self, Obj list)
{
// special case for plain lists (avoid conversion back and forth)
if ( IS_PLIST(list) ) {
return INTOBJ_INT( LEN_PLIST( list ) );
}
// generic case (will signal an error if <list> is not a list)
else {
return AttrLENGTH( LengthAttr, list );
}
}
static Int LenListError(Obj list)
{
RequireArgument("Length", list, "must be a list");
}
static Int LenListObject(Obj obj)
{
Obj len;
len = AttrLENGTH( LengthAttr, obj );
if (!IS_NONNEG_INTOBJ(len)) {
RequireArgumentEx("Length", len, 0,
"method must return a non-negative small integer");
}
return INT_INTOBJ( len );
}
/****************************************************************************
**
*F LENGTH(<list>) . . . . . . . . . . . . . . . . . . . length of a list
*V LengthFuncs[<type>] . . . . . . . . . . . . . table of length functions
**
** 'LENGTH' returns the logical length of the list <list> as a GAP object
** An error is signalled if <list> is not a list.
**
** A package implementing a list type <type> must provide such a function
** and install it in 'LengthFuncs[<type>]'.
*/
Obj (*LengthFuncs[LAST_REAL_TNUM+1]) ( Obj list );
static Obj LengthError(Obj list)
{
RequireArgument("Length", list, "must be a list");
}
static Obj LengthObject(Obj obj)
{
return AttrLENGTH( LengthAttr, obj );
}
static Obj LengthInternal(Obj obj)
{
return INTOBJ_INT(LEN_LIST(obj));
}
/****************************************************************************
**
*F ISB_LIST(<list>,<pos>) . . . . . . . . . . test for element from a list
*V IsbListFuncs[<type>] . . . . . . . . . . . . . . table of test functions
**
** 'ISB_LIST' only calls the function pointed to by 'IsbListFuncs[<type>]',
** passing <list> and <pos> as arguments. If <type> is not the type of a
** list, then 'IsbListFuncs[<type>]' points to 'IsbListError', which signals
** the error.
*/
BOOL (*IsbListFuncs[LAST_REAL_TNUM + 1])(Obj list, Int pos);
static Obj IsbListOper;
static Obj FuncISB_LIST(Obj self, Obj list, Obj pos)
{
if (IS_POS_INTOBJ(pos))
return ISB_LIST( list, INT_INTOBJ(pos) ) ? True : False;
else
return ISBB_LIST( list, pos ) ? True : False;
}
static BOOL IsbListError(Obj list, Int pos)
{
RequireArgument("IsBound", list, "must be a list");
}
static BOOL IsbListObject(Obj list, Int pos)
{
return DoOperation2Args( IsbListOper, list, INTOBJ_INT(pos) ) == True;
}
BOOL ISBB_LIST(Obj list, Obj pos)
{
return DoOperation2Args( IsbListOper, list, pos ) == True;
}
BOOL ISB_MAT(Obj mat, Obj row, Obj col)
{
return DoOperation3Args(IsbListOper, mat, row, col) == True;
}
/****************************************************************************
**
*F * * * * * * * * * * * * list access functions * * * * * * * * * * * * * *
*/
/****************************************************************************
**
*V Elm0ListFuncs[ <type> ] . . . . . . . . . . table of selection functions
**
** 'ELM0_LIST' returns the element at the position <pos> in the list <list>,
** or 0 if <list> has no assigned object at position <pos>. An error is
** signalled if <list> is not a list. It is the responsibility of the
** caller to ensure that <pos> is a positive integer.
*/
Obj (*Elm0ListFuncs[LAST_REAL_TNUM+1]) ( Obj list, Int pos );
/****************************************************************************
**
*V ElmDefListFuncs[ <type> ] . . . . . . . . . table of selection functions
**
** 'ELM_DEFAULT_LIST' returns the element at the position <pos> in the list
** <list>, or <default> if <list> has no assigned object at position <pos>.
** An error is signalled if <list> is not a list. It is the responsibility
** of the caller to ensure that <pos> is a positive integer.
*/
Obj (*ElmDefListFuncs[LAST_REAL_TNUM + 1])(Obj list, Int pos, Obj def);
// Default implementation of ELM_DEFAULT_LIST
static Obj ElmDefListDefault(Obj list, Int pos, Obj def)
{
Obj val = ELM0_LIST(list, pos);
if (val) {
return val;
}
else {
return def;
}
}
/****************************************************************************
**
*F ElmDefListObject( <list>, <pos>, <default> )select an element from a list
**
** `ElmDefListObject' is the `ELM_DEFAULT_LIST' function for objects.
**
*/
static Obj ElmDefListOper;
static Obj ElmDefListObject(Obj list, Int pos, Obj def)
{
return DoOperation3Args(ElmDefListOper, list, INTOBJ_INT(pos), def);
}
static Obj FuncELM_DEFAULT_LIST(Obj self, Obj list, Obj pos, Obj def)
{
Int ipos = GetPositiveSmallInt("GetWithDefault", pos);
return ELM_DEFAULT_LIST(list, ipos, def);
}
/****************************************************************************
**
*V Elm0vListFuncs[ <type> ] . . . . . . . . . table of selection functions
**
** 'ELMV0_LIST' does the same as 'ELM0_LIST', but the caller also guarantees
** that <list> is a list and that <pos> is less than or equal to the length
** of <list>.
*/
Obj (*Elm0vListFuncs[LAST_REAL_TNUM+1]) ( Obj list, Int pos );
/****************************************************************************
**
*F Elm0ListError( <list>, <pos> ) . . . . . . . . . . . . . . error message
*/
static Obj Elm0ListError(Obj list, Int pos)
{
RequireArgument("List Element", list, "must be a list");
}
/****************************************************************************
**
*F Elm0ListObject( <list>, <pos> ) . . . . . . select an element from a list
**
** `Elm0ListObject' is the `ELM0_LIST' and `ELMV0_LIST' function for
** objects. The function returns the element at the position <pos> of the
** list object <list>, or 0 if <list> has no assigned object at <pos>. It
** is the responsibility of the caller to ensure that <pos> is a positive
** integer.
*/
static Obj Elm0ListObject(Obj list, Int pos)
{
if (ISB_LIST(list, pos))
return ELM_LIST(list, pos);
else
return 0;
}
/****************************************************************************
**
*V ElmListFuncs[<type>] . . . . . . . . . . . table of selection functions
**
** 'ELM_LIST' returns the element at the position <pos> in the list <list>.
** An error is signalled if <list> is not a list, if <pos> is larger than
** the length of <list>, or if <list> has no assigned object at <pos>. It
** is the responsibility of the caller to ensure that <pos> is a positive
** integer.
**
** 'ELM_LIST' only calls the functions pointed to by 'ElmListFuncs[<type>]'
** passing <list> and <pos> as arguments. If <type> is not the type of a
** list, then 'ElmListFuncs[<type>]' points to 'ElmListError', which signals
** the error.
*/
Obj (*ElmListFuncs[LAST_REAL_TNUM+1]) ( Obj list, Int pos );
/****************************************************************************
**
*V ElmvListFuncs[<type>] . . . . . . . . . . . table of selection functions
**
** 'ELMV_LIST' does the same as 'ELM_LIST', but the caller also guarantees
** that <list> is a list and that <pos> is less than or equal to the length
** of <list>.
*/
Obj (*ElmvListFuncs[LAST_REAL_TNUM+1]) ( Obj list, Int pos );
/****************************************************************************
**
*V ElmwListFuncs[<type>] . . . . . . . . . . . table of selection functions
**
** 'ELMW_LIST' does the same as 'ELMV_LIST', but the caller also guarantees
** that <list> has an assigned object at the position <pos>.
*/
Obj (*ElmwListFuncs[LAST_REAL_TNUM+1]) ( Obj list, Int pos );
/****************************************************************************
**
*F ElmListError( <list>, <pos> ) . . . . . . . . . . . . . . . error message
*/
static Obj ElmListError(Obj list, Int pos)
{
RequireArgument("List Element", list, "must be a list");
}
/****************************************************************************
**
*F ElmListObject( <list>, <pos> . . . . . . . select an element from a list
**
** `ElmListObject' is the `ELM_LIST', `ELMV_LIST', and `ELMW_LIST' function
** for objects. 'ElmListObjects' selects the element at position <pos> of
** list object <list>. It is the responsibility of the caller to ensure
** that <pos> is a positive integer. The methods have to signal an error if
** <pos> is larger than the length of <list> or if the entry is not bound.
*/
static Obj ElmListOper;
static Obj ElmListObject(Obj list, Int pos)
{
return ELMB_LIST( list, INTOBJ_INT(pos) );
}
Obj ELMB_LIST(Obj list, Obj pos)
{
Obj elm;
elm = DoOperation2Args( ElmListOper, list, pos );
if (elm == 0) {
ErrorMayQuit("List access method must return a value", 0, 0);
}
return elm;
}
/****************************************************************************
**
*F FuncELM_MAT( <self>, <mat>, <row>, <col> ) . . . . . operation `ELM_MAT'
*/
static Obj FuncELM_MAT(Obj self, Obj mat, Obj row, Obj col)
{
return ELM_MAT(mat, row, col);
}
static Obj ElmMatOper;
Obj ELM_MAT(Obj mat, Obj row, Obj col)
{
Obj elm;
if (IS_POS_INTOBJ(row) && IS_POS_INTOBJ(col) && IS_PLIST(mat)) {
Int r = INT_INTOBJ(row);
if (r <= LEN_PLIST(mat)) {
Obj rowlist = ELM_PLIST(mat, r);
Int c = INT_INTOBJ(col);
if (!rowlist)
ErrorMayQuit("Matrix Element: <mat>[%d] must have an assigned value",
(Int)r, (Int)c);
if (IS_PLIST(rowlist) && c <= LEN_PLIST(rowlist)) {
elm = ELM_PLIST(rowlist, c);
if (!elm)
ErrorMayQuit("Matrix Element: <mat>[%d,%d] must have an assigned value",
(Int)r, (Int)c);
return elm;
}
// fallback to generic list access code (also triggers error if
// row isn't a list)
return ELM_LIST(rowlist, c);
}
}
elm = DoOperation3Args(ElmMatOper, mat, row, col);
if (elm == 0) {
ErrorMayQuit("Matrix access method must return a value", 0, 0);
}
return elm;
}
/****************************************************************************
**
*F FuncELM_LIST( <self>, <list>, <pos> ) . . . . . . . operation `ELM_LIST'
*/
static Obj FuncELM_LIST(Obj self, Obj list, Obj pos)
{
if (IS_POS_INTOBJ(pos))
return ELM_LIST(list, INT_INTOBJ(pos));
else
return ELMB_LIST(list, pos);
}
/****************************************************************************
**
*V ElmsListFuncs[<type>] . . . . . . . . . . . table of selection functions
**
** 'ELMS_LIST' returns a new list containing the elements at the positions
** given in the list <poss> from the <list>. It is the responsibility of
** the caller to ensure that <poss> is dense and contains only positive
** integers. An error is signalled if an element of <poss> is larger than
** the length of <list>.
**
** 'ELMS_LIST' only calls the function pointed to by
** 'ElmsListFuncs[<type>]', passing <list> and <poss> as arguments. If
** <type> is not the type of a list, then 'ElmsListFuncs[<type>]' points to
** 'ElmsListError', which just signals an error.
*/
Obj (*ElmsListFuncs[LAST_REAL_TNUM+1]) ( Obj list, Obj poss );
/****************************************************************************
**
*F ElmsListError(<list>,<poss>) . . . . . . . . . error selection function
*/
static Obj ElmsListError(Obj list, Obj poss)
{
RequireArgument("List Elements", list, "must be a list");
}
/****************************************************************************
**
*F ElmsListObject( <list>, <pos> ) . . . . . . . select elements from a list
**
** `ElmsListObject' is the `ELMS_LIST' function for objects.
*/
static Obj ElmsListOper;
static Obj ElmsListObject(Obj list, Obj poss)
{
Obj elm;
elm = DoOperation2Args( ElmsListOper, list, poss );
if (elm == 0) {
ErrorMayQuit("List multi-access method must return a value", 0, 0);
}
return elm;
}
/****************************************************************************
**
*F FuncELMS_LIST( <self>, <list>, <poss> ) . . . . . . `ELMS_LIST' operation
*/
static Obj FuncELMS_LIST(Obj self, Obj list, Obj poss)
{
return ElmsListCheck( list, poss );
}
/****************************************************************************
**
*F ElmsListDefault( <list>, <poss> ) . . . default function for `ELMS_LIST'
**
** Create a new plain list as result. <list> must be small.
*/
Obj ElmsListDefault (
Obj list,
Obj poss )
{
Obj elms; // selected sublist, result
Obj elm; // one element from <list>
Int lenPoss; // length of <positions>
Int pos; // <position> as integer
Int inc; // increment in a range
Int i; // loop variable
// select no element
if ( LEN_LIST(poss) == 0 ) {
elms = NewEmptyPlist();
}
// general code
else if ( ! IS_RANGE(poss) ) {
// get the length of <positions>
// OK because all positions lists are small
lenPoss = LEN_LIST( poss );
// make the result list
elms = NEW_PLIST( T_PLIST, lenPoss );
SET_LEN_PLIST( elms, lenPoss );
// loop over the entries of <positions> and select
for ( i = 1; i <= lenPoss; i++ ) {
// get <position>
Obj p = ELMW_LIST(poss, i);
if (!IS_INTOBJ(p)) {
ErrorMayQuit("List Elements: position is too large for "
"this type of list",
0, 0);
}
pos = INT_INTOBJ(p);
// select the element
elm = ELM0_LIST( list, pos );
if ( elm == 0 ) {
ErrorMayQuit(
"List Elements: <list>[%d] must have an assigned value",
(Int)pos, 0);
}
// assign the element into <elms>
SET_ELM_PLIST( elms, i, elm );
// notify Gasman
CHANGED_BAG( elms );
}
}
// special code for ranges
else {
// get the length of <list>
Int lenList = LEN_LIST( list );
// get the length of <positions>, the first elements, and the inc.
lenPoss = GET_LEN_RANGE( poss );
pos = GET_LOW_RANGE( poss );
inc = GET_INC_RANGE( poss );
// check that no <position> is larger than 'LEN_LIST(<list>)'
if ( lenList < pos ) {
ErrorMayQuit(
"List Elements: <list>[%d] must have an assigned value",
(Int)pos, 0);
}
if ( lenList < pos + (lenPoss-1) * inc ) {
ErrorMayQuit(
"List Elements: <list>[%d] must have an assigned value",
(Int)pos + (lenPoss - 1) * inc, 0);
}
// make the result list
elms = NEW_PLIST( T_PLIST, lenPoss );
SET_LEN_PLIST( elms, lenPoss );
// loop over the entries of <positions> and select
for ( i = 1; i <= lenPoss; i++, pos += inc ) {
// select the element
elm = ELMV0_LIST( list, pos );
if ( elm == 0 ) {
ErrorMayQuit(
"List Elements: <list>[%d] must have an assigned value",
(Int)pos, 0);
}
// assign the element to <elms>
SET_ELM_PLIST( elms, i, elm );
// notify Gasman
CHANGED_BAG( elms );
}
}
return elms;
}
/****************************************************************************
**
*F FuncELMS_LIST_DEFAULT( <self>, <list>, <poss> ) . . . . `ElmsListDefault'
*/
static Obj FuncELMS_LIST_DEFAULT(Obj self, Obj list, Obj poss)
{
return ElmsListDefault( list, poss );
}
/****************************************************************************
**
*F ElmsListCheck( <list>, <poss> ) . . . . . . . . . . . . . . . . ELMS_LIST
**
** `ElmsListCheck' checks that <poss> is a possitions lists before calling
** `ELMS_LIST'.
*/
Obj ElmsListCheck (
Obj list,
Obj poss )
{
CheckIsPossList("List Elements", poss);
return ELMS_LIST( list, poss );
}
/****************************************************************************
**
*F ElmsListLevelCheck( <lists>, <poss>, <level> ) . . . . . . ElmsListLevel
**
** `ElmsListLevelCheck' checks that <poss> is a possitions lists before
** calling `ElmsListLevel'.
*/
void ElmsListLevelCheck (
Obj lists,
Obj poss,
Int level )
{
CheckIsPossList("List Elements", poss);
ElmsListLevel( lists, poss, level );
}
/****************************************************************************
**
*F UNB_LIST(<list>,<pos>) . . . . . . . . . . . unbind element from a list
*V UnbListFuncs[<type>] . . . . . . . . . . . . . table of unbind functions
*F UnbListError(<list>,<pos>) . . . . . . . . . . . . error unbind function
**
*/
void (*UnbListFuncs[LAST_REAL_TNUM+1]) ( Obj list, Int pos );
static Obj UnbListOper;
static Obj FuncUNB_LIST(Obj self, Obj list, Obj pos)
{
if (IS_POS_INTOBJ(pos))
UNB_LIST( list, INT_INTOBJ(pos) );
else
UNBB_LIST( list, pos );
return 0;
}
static void UnbListError(Obj list, Int pos)
{
RequireArgument("Unbind", list, "must be a list");
}
static void UnbListObject(Obj list, Int pos)
{
DoOperation2Args( UnbListOper, list, INTOBJ_INT(pos) );
}
void UNBB_LIST (
Obj list,
Obj pos )
{
DoOperation2Args( UnbListOper, list, pos );
}
void UNB_MAT(Obj mat, Obj row, Obj col)
{
DoOperation3Args(UnbListOper, mat, row, col);
}
/****************************************************************************
**
*F ASS_LIST(<list>,<pos>,<obj>) . . . . . . . . assign an element to a list
*V AssListFuncs[<type>] . . . . . . . . . . . table of assignment functions
*F AssListError(<list>,<pos>,<obj>) . . . . . . . error assignment function
**
** 'ASS_LIST' only calls the function pointed to by 'AssListFuncs[<type>]',
** passing <list>, <pos>, and <obj> as arguments. If <type> is not the type
** of a list, then 'AssListFuncs[<type>]' points to 'AssListError', which
** just signals an error.
**
*/
void (*AssListFuncs[LAST_REAL_TNUM+1]) ( Obj list, Int pos, Obj obj );
static Obj AssListOper;
static Obj FuncASS_LIST(Obj self, Obj list, Obj pos, Obj obj)
{
if (IS_POS_INTOBJ(pos))
ASS_LIST(list, INT_INTOBJ(pos), obj);
else
ASSB_LIST(list, pos, obj);
return 0;
}
static void AssListError(Obj list, Int pos, Obj obj)
{
RequireArgument("List Assignments", list, "must be a list");
}
/****************************************************************************
**
*F AssListObject( <list>, <pos>, <obj> ) . . . . . . . assign to list object
*/
void AssListObject (
Obj list,
Int pos,
Obj obj )
{
DoOperation3Args( AssListOper, list, INTOBJ_INT(pos), obj );
}
void ASSB_LIST (
Obj list,
Obj pos,
Obj obj )
{
DoOperation3Args( AssListOper, list, pos, obj );
}
static Obj AssMatOper;
static Obj FuncASS_MAT(Obj self, Obj mat, Obj row, Obj col, Obj obj)
{
ASS_MAT(mat, row, col, obj);
return 0;
}
void ASS_MAT(Obj mat, Obj row, Obj col, Obj obj)
{
RequireMutable("Matrix Assignment", mat, "matrix");
if (IS_POS_INTOBJ(row) && IS_POS_INTOBJ(col) && IS_PLIST(mat)) {
Int r = INT_INTOBJ(row);
if (r <= LEN_PLIST(mat)) {
Obj rowlist = ELM_PLIST(mat, r);
Int c = INT_INTOBJ(col);
if (!rowlist)
ErrorMayQuit("Matrix Assignment: <mat>[%d] must have an assigned value",
(Int)r, (Int)c);
ASS_LIST(rowlist, c, obj);
return;
}
}
DoOperation4Args(AssMatOper, mat, row, col, obj);
}
/****************************************************************************
**
*F ASSS_LIST(<list>,<poss>,<objs>) . . . . assign several elements to a list
*V AsssListFuncs[<type>] . . . . . . . . . . . table of assignment function
*F AsssListError(<list>,<poss>,<objs>) . . . . . . error assignment function
**
** 'ASSS_LIST' only calls the function pointed to by
** 'AsssListFuncs[<type>]', passing <list>, <poss>, and <objs> as arguments.
** If <type> is not the type of a list, then 'AsssListFuncs[<type>]' points
** to 'AsssListError', which just signals an error.
*/
void (*AsssListFuncs[LAST_REAL_TNUM+1]) ( Obj list, Obj poss, Obj objs );
static Obj AsssListOper;
static Obj FuncASSS_LIST(Obj self, Obj list, Obj poss, Obj objs)
{
AsssListCheck( list, poss, objs );
return 0;
}
static void AsssListError(Obj list, Obj poss, Obj objs)
{
RequireArgument("List Assignments", list, "must be a list");
}
void AsssListDefault (
Obj list,
Obj poss,
Obj objs )
{
Int lenPoss; // length of <positions>
Obj p; // <position>
Int pos; // <position> as integer
Int inc; // increment in a range
Obj obj; // one element from <objs>
Int i; // loop variable
CheckIsPossList("List Assignments", poss);
CheckIsDenseList("List Assignments", "rhss", objs);
CheckSameLength("List Assignments", "rhss", "poss", objs, poss);
// general code
if ( ! IS_RANGE(poss) ) {
// get the length of <positions>
lenPoss = LEN_LIST( poss );
// loop over the entries of <positions> and select
for ( i = 1; i <= lenPoss; i++ ) {
// get <position>
p = ELMW_LIST( poss, i );
// select the element
obj = ELMW_LIST( objs, i );
if (IS_INTOBJ(p) )
{
// assign the element into <elms>
ASS_LIST( list, INT_INTOBJ(p), obj );
}
else
ASSB_LIST(list, p, obj);
}
}
// special code for ranges
else {
// get the length of <positions>
lenPoss = GET_LEN_RANGE( poss );
pos = GET_LOW_RANGE( poss );
inc = GET_INC_RANGE( poss );
// loop over the entries of <positions> and select
for ( i = 1; i <= lenPoss; i++, pos += inc ) {
// select the element
obj = ELMW_LIST( objs, i );
// assign the element to <elms>
ASS_LIST( list, pos, obj );
}
}
}
static void AsssListObject(Obj list, Obj poss, Obj objs)
{
DoOperation3Args( AsssListOper, list, poss, objs );
}
static Obj FuncASSS_LIST_DEFAULT(Obj self, Obj list, Obj poss, Obj objs)
{
AsssListDefault( list, poss, objs );
return 0;
}
/****************************************************************************
**
*F IS_DENSE_LIST(<list>) . . . . . . . . . . . . . . . test for dense lists
*V IsDenseListFuncs[<type>] . . . . . . table for dense list test functions
**
** 'IS_DENSE_LIST' only calls the function pointed to by
** 'IsDenseListFuncs[<type>]', passing <list> as argument. If <type> is not
** the type of a list, then 'IsDenseListFuncs[<type>]' points to
** 'AlwaysNo', which just returns 0.
*/
BOOL (*IsDenseListFuncs[LAST_REAL_TNUM + 1])(Obj list);
static Obj IsDenseListFilt;
static Obj FiltIS_DENSE_LIST(Obj self, Obj obj)
{
return (IS_DENSE_LIST( obj ) ? True : False);
}
static BOOL IsDenseListObject(Obj obj)
{
return (DoFilter( IsDenseListFilt, obj ) == True);
}
/****************************************************************************
**
*F IS_HOMOG_LIST(<list>) . . . . . . . . . . . . test for homogeneous lists
*V IsHomogListFuncs[<type>] . . . table for homogeneous list test functions
**