-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlistfunc.c
915 lines (827 loc) · 17.7 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
/*
* listfunc - list handling routines
*
* Copyright (C) 1999-2007 David I. Bell
*
* Calc is open software; you can redistribute it and/or modify it under
* the terms of the version 2.1 of the GNU Lesser General Public License
* as published by the Free Software Foundation.
*
* Calc is distributed in the hope that it will be useful, but WITHOUT
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
* or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General
* Public License for more details.
*
* A copy of version 2.1 of the GNU Lesser General Public License is
* distributed with calc under the filename COPYING-LGPL. You should have
* received a copy with calc; if not, write to Free Software Foundation, Inc.
* 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
*
* Under source code control: 1990/02/15 01:48:18
* File existed as early as: before 1990
*
* Share and enjoy! :-) http://www.isthe.com/chongo/tech/comp/calc/
*/
/*
* List handling routines.
* Lists can be composed of any types of values, mixed if desired.
* Lists are doubly linked so that elements can be inserted or
* deleted efficiently at any point in the list. A pointer is
* kept to the most recently indexed element so that sequential
* accesses are fast.
*/
#include "value.h"
#include "zrand.h"
E_FUNC long irand(long s);
S_FUNC LISTELEM *elemalloc(void);
S_FUNC void elemfree(LISTELEM *ep);
S_FUNC void removelistelement(LIST *lp, LISTELEM *ep);
/*
* Insert an element before the first element of a list.
*
* given:
* lp list to put element onto
* vp value to be inserted
*/
void
insertlistfirst(LIST *lp, VALUE *vp)
{
LISTELEM *ep; /* list element */
ep = elemalloc();
copyvalue(vp, &ep->e_value);
if (lp->l_count == 0) {
lp->l_last = ep;
} else {
lp->l_cacheindex++;
lp->l_first->e_prev = ep;
ep->e_next = lp->l_first;
}
lp->l_first = ep;
lp->l_count++;
}
/*
* Insert an element after the last element of a list.
*
* given:
* lp list to put element onto
* vp value to be inserted
*/
void
insertlistlast(LIST *lp, VALUE *vp)
{
LISTELEM *ep; /* list element */
ep = elemalloc();
copyvalue(vp, &ep->e_value);
if (lp->l_count == 0) {
lp->l_first = ep;
} else {
lp->l_last->e_next = ep;
ep->e_prev = lp->l_last;
}
lp->l_last = ep;
lp->l_count++;
}
/*
* Insert an element into the middle of list at the given index (zero based).
* The specified index will select the new element, so existing elements
* at or beyond the index will be shifted down one position. It is legal
* to specify an index which is right at the end of the list, in which
* case the element is appended to the list.
*
* given:
* lp list to put element onto
* index element number to insert in front of
* vp value to be inserted
*/
void
insertlistmiddle(LIST *lp, long index, VALUE *vp)
{
LISTELEM *ep; /* list element */
LISTELEM *oldep; /* old list element at desired index */
if (index == 0) {
insertlistfirst(lp, vp);
return;
}
if (index == lp->l_count) {
insertlistlast(lp, vp);
return;
}
oldep = NULL;
if ((index >= 0) && (index < lp->l_count))
oldep = listelement(lp, index);
if (oldep == NULL) {
math_error("Index out of bounds for list insertion");
/*NOTREACHED*/
}
ep = elemalloc();
copyvalue(vp, &ep->e_value);
ep->e_next = oldep;
ep->e_prev = oldep->e_prev;
ep->e_prev->e_next = ep;
oldep->e_prev = ep;
lp->l_cache = ep;
lp->l_cacheindex = index;
lp->l_count++;
}
/*
* Remove the first element from a list, returning its value.
* Returns the null value if no more elements exist.
*
* given:
* lp list to have element removed
* vp location of the value
*/
void
removelistfirst(LIST *lp, VALUE *vp)
{
if (lp->l_count == 0) {
vp->v_type = V_NULL;
vp->v_subtype = V_NOSUBTYPE;
return;
}
*vp = lp->l_first->e_value;
lp->l_first->e_value.v_type = V_NULL;
lp->l_first->e_value.v_type = V_NOSUBTYPE;
removelistelement(lp, lp->l_first);
}
/*
* Remove the last element from a list, returning its value.
* Returns the null value if no more elements exist.
*
* given:
* lp list to have element removed
* vp location of the value
*/
void
removelistlast(LIST *lp, VALUE *vp)
{
if (lp->l_count == 0) {
vp->v_type = V_NULL;
vp->v_subtype = V_NOSUBTYPE;
return;
}
*vp = lp->l_last->e_value;
lp->l_last->e_value.v_type = V_NULL;
lp->l_last->e_value.v_subtype = V_NOSUBTYPE;
removelistelement(lp, lp->l_last);
}
/*
* Remove the element with the given index from a list, returning its value.
*
* given:
* lp list to have element removed
* index list element to be removed
* vp location of the value
*/
void
removelistmiddle(LIST *lp, long index, VALUE *vp)
{
LISTELEM *ep; /* element being removed */
ep = NULL;
if ((index >= 0) && (index < lp->l_count))
ep = listelement(lp, index);
if (ep == NULL) {
math_error("Index out of bounds for list deletion");
/*NOTREACHED*/
}
*vp = ep->e_value;
ep->e_value.v_type = V_NULL;
ep->e_value.v_subtype = V_NOSUBTYPE;
removelistelement(lp, ep);
}
/*
* Remove an arbitrary element from a list.
* The value contained in the element is freed.
*
* given:
* lp list header
* ep list element to remove
*/
S_FUNC void
removelistelement(LIST *lp, LISTELEM *ep)
{
if ((ep == lp->l_cache) || ((ep != lp->l_first) && (ep != lp->l_last)))
lp->l_cache = NULL;
if (ep->e_next)
ep->e_next->e_prev = ep->e_prev;
if (ep->e_prev)
ep->e_prev->e_next = ep->e_next;
if (ep == lp->l_first) {
lp->l_first = ep->e_next;
lp->l_cacheindex--;
}
if (ep == lp->l_last)
lp->l_last = ep->e_prev;
lp->l_count--;
elemfree(ep);
}
LIST *
listsegment(LIST *lp, long n1, long n2)
{
LIST *newlp;
LISTELEM *ep;
long i;
newlp = listalloc();
if ((n1 >= lp->l_count && n2 >= lp->l_count) || (n1 < 0 && n2 < 0))
return newlp;
if (n1 >= lp->l_count)
n1 = lp->l_count - 1;
if (n2 >= lp->l_count)
n2 = lp->l_count - 1;
if (n1 < 0)
n1 = 0;
if (n2 < 0)
n2 = 0;
ep = lp->l_first;
if (n1 <= n2) {
i = n2 - n1 + 1;
while(n1-- > 0 && ep)
ep = ep->e_next;
while(i-- > 0 && ep) {
insertlistlast(newlp, &ep->e_value);
ep = ep->e_next;
}
} else {
i = n1 - n2 + 1;
while(n2-- > 0 && ep)
ep = ep->e_next;
while(i-- > 0 && ep) {
insertlistfirst(newlp, &ep->e_value);
ep = ep->e_next;
}
}
return newlp;
}
/*
* Search a list for the specified value starting at the specified index.
* Returns 0 and stores the element number (zero based) if the value is
* found, otherwise returns 1.
*/
int
listsearch(LIST *lp, VALUE *vp, long i, long j, ZVALUE *index)
{
register LISTELEM *ep;
if (i < 0 || j > lp->l_count) {
math_error("This should not happen in call to listsearch");
/*NOTREACHED*/
}
ep = listelement(lp, i);
while (i < j) {
if (!ep) {
math_error("This should not happen in listsearch");
/*NOTREACHED*/
}
if (acceptvalue(&ep->e_value, vp)) {
lp->l_cache = ep;
lp->l_cacheindex = i;
utoz(i, index);
return 0;
}
ep = ep->e_next;
i++;
}
return 1;
}
/*
* Search a list backwards for the specified value starting at the
* specified index. Returns 0 and stores i if the value is found at
* index i; otherwise returns 1.
*/
int
listrsearch(LIST *lp, VALUE *vp, long i, long j, ZVALUE *index)
{
register LISTELEM *ep;
if (i < 0 || j > lp->l_count) {
math_error("This should not happen in call to listrsearch");
/*NOTREACHED*/
}
ep = listelement(lp, --j);
while (j >= i) {
if (!ep) {
math_error("This should not happen in listsearch");
/*NOTREACHED*/
}
if (acceptvalue(&ep->e_value, vp)) {
lp->l_cache = ep;
lp->l_cacheindex = j;
utoz(j, index);
return 0;
}
ep = ep->e_prev;
j--;
}
return 1;
}
/*
* Index into a list and return the address for the value corresponding
* to that index. Returns NULL if the element does not exist.
*
* given:
* lp list to index into
* index index of desired element
*/
VALUE *
listfindex(LIST *lp, long index)
{
LISTELEM *ep;
ep = listelement(lp, index);
if (ep == NULL)
return NULL;
return &ep->e_value;
}
/*
* Return the element at a specified index number of a list.
* The list is indexed starting at zero, and negative indices
* indicate to index from the end of the list. This routine finds
* the element by chaining through the list from the closest one
* of the first, last, and cached elements. Returns NULL if the
* element does not exist.
*
* given:
* lp list to index into
* index index of desired element
*/
LISTELEM *
listelement(LIST *lp, long index)
{
register LISTELEM *ep; /* current list element */
long dist; /* distance to element */
long temp; /* temporary distance */
BOOL forward; /* TRUE if need to walk forwards */
if (index < 0)
index += lp->l_count;
if ((index < 0) || (index >= lp->l_count))
return NULL;
/*
* Check quick special cases first.
*/
if (index == 0)
return lp->l_first;
if (index == 1)
return lp->l_first->e_next;
if (index == lp->l_count - 1)
return lp->l_last;
if ((index == lp->l_cacheindex) && lp->l_cache)
return lp->l_cache;
/*
* Calculate whether it is better to go forwards from
* the first element or backwards from the last element.
*/
forward = ((index * 2) <= lp->l_count);
if (forward) {
dist = index;
ep = lp->l_first;
} else {
dist = (lp->l_count - 1) - index;
ep = lp->l_last;
}
/*
* Now see if we have a cached element and if so, whether or
* not the distance from it is better than the above distance.
*/
if (lp->l_cache) {
temp = index - lp->l_cacheindex;
if ((temp >= 0) && (temp < dist)) {
dist = temp;
ep = lp->l_cache;
forward = TRUE;
}
if ((temp < 0) && (-temp < dist)) {
dist = -temp;
ep = lp->l_cache;
forward = FALSE;
}
}
/*
* Now walk forwards or backwards from the selected element
* until we reach the correct element. Cache the location of
* the found element for future use.
*/
if (forward) {
while (dist-- > 0)
ep = ep->e_next;
} else {
while (dist-- > 0)
ep = ep->e_prev;
}
lp->l_cache = ep;
lp->l_cacheindex = index;
return ep;
}
/*
* Compare two lists to see if they are identical.
* Returns TRUE if they are different.
*/
BOOL
listcmp(LIST *lp1, LIST *lp2)
{
LISTELEM *e1, *e2;
long count;
if (lp1 == lp2)
return FALSE;
if (lp1->l_count != lp2->l_count)
return TRUE;
e1 = lp1->l_first;
e2 = lp2->l_first;
count = lp1->l_count;
while (count-- > 0) {
if (comparevalue(&e1->e_value, &e2->e_value))
return TRUE;
e1 = e1->e_next;
e2 = e2->e_next;
}
return FALSE;
}
/*
* Copy a list
*/
LIST *
listcopy(LIST *oldlp)
{
LIST *lp;
LISTELEM *oldep;
lp = listalloc();
oldep = oldlp->l_first;
while (oldep) {
insertlistlast(lp, &oldep->e_value);
oldep = oldep->e_next;
}
return lp;
}
/*
* Round elements of a list to a specified number of decimal digits
*/
LIST *
listround(LIST *oldlp, VALUE *v2, VALUE *v3)
{
LIST *lp;
LISTELEM *oldep, *ep, *eq;
lp = listalloc();
oldep = oldlp->l_first;
lp->l_count = oldlp->l_count;
if (oldep) {
ep = elemalloc();
lp->l_first = ep;
for (;;) {
roundvalue(&oldep->e_value, v2, v3, &ep->e_value);
oldep = oldep->e_next;
if (!oldep)
break;
eq = elemalloc();
ep->e_next = eq;
eq->e_prev = ep;
ep = eq;
}
lp->l_last = ep;
}
return lp;
}
/*
* Round elements of a list to a specified number of binary digits
*/
LIST *
listbround(LIST *oldlp, VALUE *v2, VALUE *v3)
{
LIST *lp;
LISTELEM *oldep, *ep, *eq;
lp = listalloc();
oldep = oldlp->l_first;
lp->l_count = oldlp->l_count;
if (oldep) {
ep = elemalloc();
lp->l_first = ep;
for (;;) {
broundvalue(&oldep->e_value, v2, v3, &ep->e_value);
oldep = oldep->e_next;
if (!oldep)
break;
eq = elemalloc();
ep->e_next = eq;
eq->e_prev = ep;
ep = eq;
}
lp->l_last = ep;
}
return lp;
}
/*
* Approximate a list by approximating elements by multiples of v2,
* type of rounding determined by v3.
*/
LIST *
listappr(LIST *oldlp, VALUE *v2, VALUE *v3)
{
LIST *lp;
LISTELEM *oldep, *ep, *eq;
lp = listalloc();
oldep = oldlp->l_first;
lp->l_count = oldlp->l_count;
if (oldep) {
ep = elemalloc();
lp->l_first = ep;
for (;;) {
apprvalue(&oldep->e_value, v2, v3, &ep->e_value);
oldep = oldep->e_next;
if (!oldep)
break;
eq = elemalloc();
ep->e_next = eq;
eq->e_prev = ep;
ep = eq;
}
lp->l_last = ep;
}
return lp;
}
/*
* Construct a list whose elements are integer quotients of the elements
* of a specified list by a specified number.
*/
LIST *
listquo(LIST *oldlp, VALUE *v2, VALUE *v3)
{
LIST *lp;
LISTELEM *oldep, *ep, *eq;
lp = listalloc();
oldep = oldlp->l_first;
lp->l_count = oldlp->l_count;
if (oldep) {
ep = elemalloc();
lp->l_first = ep;
for (;;) {
quovalue(&oldep->e_value, v2, v3, &ep->e_value);
oldep = oldep->e_next;
if (!oldep)
break;
eq = elemalloc();
ep->e_next = eq;
eq->e_prev = ep;
ep = eq;
}
lp->l_last = ep;
}
return lp;
}
/*
* Construct a list whose elements are the remainders after integral
* division of the elements of a specified list by a specified number.
*/
LIST *
listmod(LIST *oldlp, VALUE *v2, VALUE *v3)
{
LIST *lp;
LISTELEM *oldep, *ep, *eq;
lp = listalloc();
oldep = oldlp->l_first;
lp->l_count = oldlp->l_count;
if (oldep) {
ep = elemalloc();
lp->l_first = ep;
for (;;) {
modvalue(&oldep->e_value, v2, v3, &ep->e_value);
oldep = oldep->e_next;
if (!oldep)
break;
eq = elemalloc();
ep->e_next = eq;
eq->e_prev = ep;
ep = eq;
}
lp->l_last = ep;
}
return lp;
}
void
listreverse(LIST *lp)
{
LISTELEM *e1, *e2;
VALUE tmp;
long s;
s = lp->l_count/2;
e1 = lp->l_first;
e2 = lp->l_last;
lp->l_cache = NULL;
while (s-- > 0) {
tmp = e1->e_value;
e1->e_value = e2->e_value;
e2->e_value = tmp;
e1 = e1->e_next;
e2 = e2->e_prev;
}
}
void
listsort(LIST *lp)
{
LISTELEM *start;
LISTELEM *last, *a, *a1, *b, *next;
LISTELEM *S[LONG_BITS+1];
long len[LONG_BITS+1];
long i, j, k;
if (lp->l_count < 2)
return;
lp->l_cache = NULL;
start = elemalloc();
next = lp->l_first;
last = start;
start->e_next = next;
for (k = 0; next && k < LONG_BITS; k++) {
next->e_prev = last;
last = next;
S[k] = next;
next = next->e_next;
len[k] = 1;
while (k > 0 && (!next || len[k] >= len[k - 1])) {/* merging */
j = len[k];
b = S[k--];
i = len[k];
a = S[k];
a1 = b->e_prev;
len[k] = i + j;
if (precvalue(&b->e_value, &a->e_value)) {
S[k] = b;
a->e_prev->e_next = b;
b->e_prev = a->e_prev;
j--;
while (j > 0) {
b = b->e_next;
if (!precvalue(&b->e_value,
&a->e_value))
break;
j--;
}
if (j == 0) {
b->e_next = a;
a->e_prev = b;
last = a1;
continue;
}
b->e_prev->e_next = a;
a->e_prev = b->e_prev;
}
do {
i--;
while (i > 0) {
a = a->e_next;
if (precvalue(&b->e_value,
&a->e_value))
break;
i--;
}
if (i == 0)
break;
a->e_prev->e_next = b;
b->e_prev = a->e_prev;
j--;
while (j > 0) {
b = b->e_next;
if (!precvalue(&b->e_value,
&a->e_value))
break;
j--;
}
if (j != 0) {
b->e_prev->e_next = a;
a->e_prev = b->e_prev;
}
} while (j != 0);
if (i == 0) {
a->e_next = b;
b->e_prev = a;
} else if (j == 0) {
b->e_next = a;
a->e_prev = b;
last = a1;
}
}
}
if (k >= LONG_BITS) {
/* this should never happen */
math_error("impossible k overflow in listsort!");
/*NOTREACHED*/
}
lp->l_first = start->e_next;
lp->l_first->e_prev = NULL;
lp->l_last = last;
lp->l_last->e_next = NULL;
elemfree(start);
}
void
listrandperm(LIST *lp)
{
LISTELEM *ep, *eq;
long i, s;
VALUE val;
s = lp->l_count;
for (ep = lp->l_last; s > 1; ep = ep->e_prev) {
i = irand(s--);
if (i < s) {
eq = listelement(lp, i);
val = eq->e_value;
eq->e_value = ep->e_value;
ep->e_value = val;
}
}
}
/*
* Allocate an element for a list.
*/
S_FUNC LISTELEM *
elemalloc(void)
{
LISTELEM *ep;
ep = (LISTELEM *) malloc(sizeof(LISTELEM));
if (ep == NULL) {
math_error("Cannot allocate list element");
/*NOTREACHED*/
}
ep->e_next = NULL;
ep->e_prev = NULL;
ep->e_value.v_type = V_NULL;
ep->e_value.v_subtype = V_NOSUBTYPE;
return ep;
}
/*
* Free a list element, along with any contained value.
*/
S_FUNC void
elemfree(LISTELEM *ep)
{
if (ep->e_value.v_type != V_NULL)
freevalue(&ep->e_value);
free(ep);
}
/*
* Allocate a new list header.
*/
LIST *
listalloc(void)
{
register LIST *lp;
lp = (LIST *) malloc(sizeof(LIST));
if (lp == NULL) {
math_error("Cannot allocate list header");
/*NOTREACHED*/
}
lp->l_first = NULL;
lp->l_last = NULL;
lp->l_cache = NULL;
lp->l_cacheindex = 0;
lp->l_count = 0;
return lp;
}
/*
* Free a list header, along with all of its list elements.
*/
void
listfree(LIST *lp)
{
register LISTELEM *ep;
while (lp->l_count-- > 0) {
ep = lp->l_first;
lp->l_first = ep->e_next;
elemfree(ep);
}
free(lp);
}
/*
* Print out a list along with the specified number of its elements.
* The elements are printed out in shortened form.
*/
void
listprint(LIST *lp, long max_print)
{
long count;
long index;
LISTELEM *ep;
if (max_print > lp->l_count)
max_print = lp->l_count;
count = 0;
ep = lp->l_first;
index = lp->l_count;
while (index-- > 0) {
if ((ep->e_value.v_type != V_NUM) ||
(!qiszero(ep->e_value.v_num)))
count++;
ep = ep->e_next;
}
if (max_print > 0)
math_str("\n");
math_fmt("list (%ld element%s, %ld nonzero)", lp->l_count,
((lp->l_count == 1) ? "" : "s"), count);
if (max_print <= 0)
return;
/*
* Walk through the first few list elements, printing their
* value in short and unambiguous format.
*/
math_str(":\n");
ep = lp->l_first;
for (index = 0; index < max_print; index++) {
math_fmt("\t[[%ld]] = ", index);
printvalue(&ep->e_value, PRINT_SHORT | PRINT_UNAMBIG);
math_str("\n");
ep = ep->e_next;
}
if (max_print < lp->l_count)
math_str(" ...\n");
}