-
Notifications
You must be signed in to change notification settings - Fork 338
/
Copy pathlist.c
1660 lines (1380 loc) · 48.3 KB
/
list.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
/**************************************************************/
/* ********************************************************** */
/* * * */
/* * LISTS * */
/* * * */
/* * $Module: LIST * */
/* * * */
/* * Copyright (C) 1996, 1997, 1998, 1999, 2000, 2001 * */
/* * MPI fuer Informatik * */
/* * * */
/* * This program is free software; you can redistribute * */
/* * it and/or modify it under the terms of the GNU * */
/* * General Public License as published by the Free * */
/* * Software Foundation; either version 2 of the License, * */
/* * or (at your option) any later version. * */
/* * * */
/* * This program 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 General Public * */
/* * License for more details. * */
/* * * */
/* * You should have received a copy of the GNU General * */
/* * Public License along with this program; if not, write * */
/* * to the Free Software Foundation, Inc., 59 Temple * */
/* * Place, Suite 330, Boston, MA 02111-1307 USA * */
/* * * */
/* * * */
/* $Revision$ * */
/* $State$ * */
/* $Date$ * */
/* $Author$ * */
/* * * */
/* * Contact: * */
/* * Christoph Weidenbach * */
/* * MPI fuer Informatik * */
/* * Stuhlsatzenhausweg 85 * */
/* * 66123 Saarbruecken * */
/* * Email: weidenb@mpi-sb.mpg.de * */
/* * Germany * */
/* * * */
/* ********************************************************** */
/**************************************************************/
/* $RCSfile$ */
#include "list.h"
/**************************************************************/
/* ********************************************************** */
/* * * */
/* * MEMORY MANAGEMENT * */
/* * * */
/* ********************************************************** */
/**************************************************************/
LIST list_Copy(const LIST List)
/**************************************************************
INPUT: A List.
RETURNS: The copy of the list.
CAUTION: The entries of the list are NOT copied !
the function needs time O(n), where <n> is the length
of the list.
***************************************************************/
{
LIST Copy;
LIST Scan1,Scan2;
if (list_Empty(List))
return list_Nil();
Copy = list_List(list_Car(List));
Scan1 = Copy;
Scan2 = list_Cdr(List);
while (!list_Empty(Scan2)) {
list_Rplacd(Scan1, list_List(list_Car(Scan2)));
Scan1 = list_Cdr(Scan1);
Scan2 = list_Cdr(Scan2);
}
return Copy;
}
LIST list_CopyWithElement(const LIST List, POINTER (*CopyElement)(POINTER))
/**************************************************************
INPUT: A List and a copy function for the elements.
RETURNS: The copy of the list.
CAUTION: The entries of the list are NOT copied !
The function needs time O(n*c), where <n> is the length
of the list and <c> is the time for a call of the
element copy function.
***************************************************************/
{
LIST Copy;
LIST Scan1,Scan2;
if (list_Empty(List))
return list_Nil();
Copy = list_List(CopyElement(list_Car(List)));
Scan1 = Copy;
Scan2 = list_Cdr(List);
while (!list_Empty(Scan2)) {
list_Rplacd(Scan1, list_List(CopyElement(list_Car(Scan2))));
Scan1 = list_Cdr(Scan1);
Scan2 = list_Cdr(Scan2);
}
return Copy;
}
void list_InsertNext(LIST List, POINTER Pointer)
/**************************************************************
INPUT: A list and a pointer to anything.
RETURNS: A list with Pointer being added at the position that
follows List.
SUMMARY: We enqueue the element at position list_Cdr(List);
The function needs time O(1).
***************************************************************/
{
#ifdef CHECK
if (Pointer == NULL) {
misc_StartErrorReport();
misc_ErrorReport("\n In list_InsertNext: NULL Pointer. ");
misc_FinishErrorReport();
}
#endif
list_Rplacd(List, list_Cons(Pointer, list_Cdr(List)));
}
void list_NMapCar(LIST List, POINTER (*ElementFunc)(POINTER))
/**************************************************************
INPUT: A List and a function for the elements.
RETURNS: The List where all elements are replaced by the result of
the function calls of <ElementFunc> to the elements
CAUTION: The List is not copied !
The function needs time O(n*f), where <n> is the length
of the list and <f> is the time for a call of the
element function.
***************************************************************/
{
LIST Scan;
for (Scan = List; !list_Empty(Scan); Scan = list_Cdr(Scan))
list_Rplaca(Scan, ElementFunc(list_Car(Scan)));
}
void list_Apply(void (*Function)(POINTER), LIST List)
/**************************************************************
INPUT: A non-resulting function and a list.
SUMMARY: Apply the function to all members of the list.
The function needs time O(n*f), where <n> is the length
of the list and <f> is the time for a call of the
element function.
***************************************************************/
{
while (!list_Empty(List)) {
Function(list_Car(List));
List = list_Cdr(List);
}
}
LIST list_Reverse(const LIST List)
/**************************************************************
INPUT: A list.
RETURNS: A new list where the order of the elements is reversed.
EFFECT: The function needs time O(n), where <n> is the length
of the list.
***************************************************************/
{
LIST ReverseList;
LIST Scan;
ReverseList = list_Nil();
for (Scan=List;!list_Empty(Scan);Scan=list_Cdr(Scan))
ReverseList = list_Cons(list_Car(Scan), ReverseList);
return ReverseList;
}
LIST list_NReverse(LIST List)
/**************************************************************
INPUT: A list
RETURNS: The same list with reversed order of items.
CAUTION: Destructive.
The function needs time O(n), where <n> is the length
of the list.
***************************************************************/
{
LIST ReverseList;
LIST Scan1;
LIST Scan2;
ReverseList = list_Nil();
for (Scan1=List; !list_Empty(Scan1); Scan1=list_Cdr(Scan1))
ReverseList = list_Cons(list_Car(Scan1),ReverseList);
for (Scan1=List, Scan2=ReverseList;
!list_Empty(Scan1);
Scan1=list_Cdr(Scan1), Scan2=list_Cdr(Scan2))
list_Rplaca(Scan1, list_Car(Scan2));
list_Delete(ReverseList);
return List;
}
static __inline__ BOOL list_PointerLower (POINTER A, POINTER B)
{
return (NAT) A < (NAT) B;
}
LIST list_PointerSort(LIST List)
/**************************************************************
INPUT: A list
RETURNS: The same list where the elements are sorted as pointers.
EFFECT: The function needs time O(n log n), where <n> is the length
of the list.
CAUTION: Destructive.
***************************************************************/
{
return list_Sort(List, list_PointerLower);
}
BOOL list_SortedInOrder(LIST L, BOOL (*Test)(POINTER, POINTER))
/**************************************************************
INPUT: A list, and an ordering function.
RETURNS: TRUE, if the list is ordered with respect to the
ordering function, FALSE otherwise.
EFFECT: The function needs time O(n), where <n> is the
length of the list.
***************************************************************/
{
LIST Scan1, Scan2;
if (!(list_Empty(L) || list_Empty(list_Cdr(L)))) {
Scan1 = L;
Scan2 = list_Cdr(Scan1);
/* Scan the list. */
do {
/* If all elements are ordered, then every element */
/* is <= its successor with respect to the ordering */
/* function. */
/* We might have a strictly ordering Test function, */
/* which implements < instead of <=, so let's test */
/* for equality first. */
if (!Test(list_Car(Scan1), list_Car(Scan2)) &&
Test(list_Car(Scan2), list_Car(Scan1)))
/* It is really strictly greater, so return FALSE. */
return FALSE;
Scan1 = list_Cdr(Scan1);
Scan2 = list_Cdr(Scan1);
} while (!list_Empty(Scan2));
}
return TRUE;
}
LIST list_Merge(LIST List1, LIST List2, BOOL (*Test)(POINTER, POINTER))
/**************************************************************
INPUT: Two sorted lists List1 and List2, and an ordering function.
RETURNS: The merged list ordered with respect to the ordering function.
EFFECT: The function needs time O(n), where <n> is the length of the list.
***************************************************************/
{
LIST Scan1, Scan2, Result, ResultStart;
#ifdef CHECK
if (!list_SortedInOrder(List1, Test)) {
/* print an error message and exit */
misc_StartErrorReport();
misc_ErrorReport("\n In list_Merge: First argument is not sorted.");
misc_FinishErrorReport();
}
else if (!list_SortedInOrder (List2, Test)) {
/* print an error message and exit */
misc_StartErrorReport();
misc_ErrorReport("\n In list_Merge: Second argument is not sorted.");
misc_FinishErrorReport();
}
#endif
if (list_Empty(List1))
return List2;
if (list_Empty(List2))
return List1;
/* This version is derived from list_NNumberMerge, but it doesn't need */
/* to allocate and deallocate memory, so it should be more efficient. */
/* Use the list with the least element as result list. */
if (Test(list_Car(List1), list_Car(List2))) {
ResultStart = List1;
Scan1 = list_Cdr(List1);
Scan2 = List2;
}
else {
ResultStart = List2;
Scan1 = List1;
Scan2 = list_Cdr(List2);
}
/* Result is the last element of the merged list. */
Result = ResultStart;
while (!list_Empty(Scan1) && !list_Empty(Scan2)) {
/* This function doesn't implement stable merging. */
/* Add another test if you need it. */
if (Test(list_Car(Scan1), list_Car(Scan2))) {
list_Rplacd(Result,Scan1);
Scan1 = list_Cdr(Scan1);
}
else {
list_Rplacd(Result,Scan2);
Scan2 = list_Cdr(Scan2);
}
Result = list_Cdr(Result);
}
if (list_Empty(Scan1))
list_Rplacd(Result, Scan2);
else
list_Rplacd(Result, Scan1);
return ResultStart;
}
void list_Split(LIST L, LIST *Half1, LIST *Half2)
/**************************************************************
INPUT: A list, and two pointers to lists.
RETURNS: Nothing.
EFFECT: The input list is split in two. <Half1> and
<Half2> point to the resulting halves.
The input list is destructively changed!
If the list length is odd, <Half2> is assigned the
bigger part.
The function needs time O(n), where <n> is the
length of the input list.
***************************************************************/
{
/* Adapted code from proofcheck ... MergeSort. */
LIST SingleStep, DoubleStep, Prev;
if (list_Empty(L) || list_Empty(list_Cdr(L))) {
*Half1 = list_Nil();
*Half2 = L;
}
else {
/* divide list in two halves */
Prev = L;
SingleStep = list_Cdr(L);
DoubleStep = list_Cdr(SingleStep);
while (!list_Empty(DoubleStep) && !list_Empty(list_Cdr(DoubleStep))) {
Prev = SingleStep;
SingleStep = list_Cdr(SingleStep);
DoubleStep = list_Cdr(list_Cdr(DoubleStep));
}
*Half1 = L;
*Half2 = SingleStep;
list_Rplacd(Prev, list_Nil());
}
}
LIST list_MergeSort (LIST L, BOOL (*Test) (POINTER, POINTER))
/**************************************************************
INPUT: A list, and an ordering function.
RETURNS: The list sorted with respect to the ordering function.
EFFECT: The function needs time O((n log n) * t), where
<n> is the length of the input list and <t> is the
execution time of the ordering function.
***************************************************************/
{
LIST Result;
#ifdef CHECK
NAT originallength;
originallength = list_Length(L);
#endif
/* Only sort if list has more than one element */
if (!list_Empty(L) && !list_Empty(list_Cdr(L))) {
LIST lowerhalf;
LIST greaterhalf;
LIST *lowerhalfptr;
LIST *greaterhalfptr;
lowerhalfptr = &lowerhalf;
greaterhalfptr = &greaterhalf;
list_Split(L, lowerhalfptr, greaterhalfptr);
#ifdef CHECK
if((list_Length(lowerhalf) + list_Length(greaterhalf))
!= originallength) {
/* output an error message and exit */
misc_StartErrorReport();
misc_ErrorReport("\n In list_MergeSort: Split lists' total sizes");
misc_ErrorReport("\n don't match original list's size.");
misc_FinishErrorReport();
}
#endif
lowerhalf = list_MergeSort(lowerhalf, Test);
greaterhalf = list_MergeSort(greaterhalf, Test);
#ifdef CHECK
if((list_Length(lowerhalf) + list_Length(greaterhalf))
!= originallength) {
/* output an error message and exit */
misc_StartErrorReport();
misc_ErrorReport("\n In list_MergeSort: Mergesorted lists' total sizes");
misc_ErrorReport("\n don't match original list's size.");
misc_FinishErrorReport();
}
#endif
Result = list_Merge(lowerhalf, greaterhalf, Test);
#ifdef CHECK
if(list_Length(Result) != originallength) {
/* output an error message and exit */
misc_StartErrorReport();
misc_ErrorReport("\n In list_MergeSort: Merged list's size doesn't match ");
misc_ErrorReport("\n original list's size.");
misc_FinishErrorReport();
}
#endif
}
else {
Result = L;
}
return Result;
}
LIST list_InsertionSort(LIST List, BOOL (*Test)(POINTER, POINTER))
/**************************************************************
INPUT: A list and a 'less' function on the elements.
RETURNS: The same list where the elements are sorted with
respect to Test.
EFFECT: The function needs time O(n^2*t), where <n> is the
length of the list and <t> is the time for the test
function.
CAUTION: Destructive.
***************************************************************/
{
LIST Scan1,Scan2,Min;
POINTER Exchange;
for (Scan1=List; !list_Empty(Scan1); Scan1=list_Cdr(Scan1)) {
Min = Scan1;
for (Scan2 = list_Cdr(Scan1); !list_Empty(Scan2); Scan2 = list_Cdr(Scan2))
if (Test(list_Car(Scan2), list_Car(Min))) {
Exchange = list_Car(Min);
list_Rplaca(Min, list_Car(Scan2));
list_Rplaca(Scan2, Exchange);
}
}
return List;
}
LIST list_Sort(LIST List, BOOL (*Test)(POINTER, POINTER))
/**************************************************************
INPUT: A list and a 'less' function on the elements.
RETURNS: The same list where the elements are sorted with
respect to Test.
EFFECT: The function needs time O((n log n) *t), where <n>
is the length of the list and <t> is the time for
the test function.
CAUTION: Destructive.
***************************************************************/
{
LIST Result;
#ifdef CHECK
NAT originallength;
originallength = list_Length(List);
#endif
Result = list_MergeSort(List, Test);
#ifdef CHECK
if (!list_SortedInOrder(Result, Test)) {
misc_StartErrorReport();
misc_ErrorReport("\n In list_Sort: list_MergeSort did not sort properly.");
misc_FinishErrorReport();
}
if (list_Length(Result) != originallength) {
misc_StartErrorReport();
misc_ErrorReport("\n In list_Sort: list_MergeSort lost elements. ");
misc_FinishErrorReport();
}
#endif
return Result;
}
/* Help Variable used to store a pointer to the numbering function to use
in element comparisons.
*/
static NAT (*NumberFunction)(POINTER) = NULL;
static __inline__ BOOL list_PointerNumberedLower(POINTER A, POINTER B)
{
return (BOOL) (NumberFunction(A) < NumberFunction(B));
}
static __inline__ BOOL list_PointerNumberedLowerOrEqual(POINTER A, POINTER B)
{
return (BOOL) (NumberFunction(A) <= NumberFunction(B));
}
static __inline__ BOOL list_PointerNumberedGreater(POINTER A, POINTER B)
{
return (BOOL) (NumberFunction(A) > NumberFunction(B));
}
LIST list_NumberSort(LIST List, NAT (*Number)(POINTER))
/**************************************************************
INPUT: A list and function mapping elements to numbers.
RETURNS: The same list where the elements are sorted with
respect to < and the Number function.
EFFECT: The function needs time O((n log n) * f), where <n>
is the length of the list and <f> is the time for a
call of the <Number> function.
CAUTION: Destructive.
***************************************************************/
{
/* Use number function as temporary variable. It is used as
an implicit parameter in list_PointerLower.
We can't make it an explicit parameter, because of the
prototype of list_Sort.
*/
NumberFunction = Number;
return list_Sort(List, list_PointerNumberedLower);
}
LIST list_GreaterNumberSort(LIST List, NAT (*Number)(POINTER))
/**************************************************************
INPUT: A list and function mapping elements to numbers.
RETURNS: The same list where the elements are sorted with
respect to > and the Number function.
EFFECT: The function needs time O((n log n) * f), where <n>
is the length of the list and <f> is the time for a
call of the <Number> function.
CAUTION: Destructive.
***************************************************************/
{
/* Use number function as temporary variable. It is used as
an implicit parameter in list_PointerLower.
We can't make it an explicit parameter, because of the
prototype of list_Sort.
*/
NumberFunction = Number;
return list_Sort(List, list_PointerNumberedGreater);
}
LIST list_NNumberMerge(LIST List1, LIST List2, NAT (*Number)(POINTER))
/**************************************************************
INPUT: Two sorted lists and function mapping elements to
numbers.
RETURNS: The merge of the lists where the elements are sorted
with respect to < and the Number function.
CAUTION: Destructive on both lists.
***************************************************************/
{
NumberFunction = Number;
return list_Merge(List1, List2, list_PointerNumberedLowerOrEqual);
}
POINTER list_DequeueNext(LIST List)
/**************************************************************
INPUT: A list
RETURNS: A pointer to a dequeued element.
SUMMARY: We dequeue the element pointed to by list_Cdr(List).
The function needs time O(1).
***************************************************************/
{
POINTER Pointer;
LIST Memo;
if (list_Empty(List))
return NULL;
Memo = list_Cdr(List);
if (list_Empty(Memo))
return NULL;
Pointer = list_Car(Memo);
list_Rplacd(List, Memo->cdr);
list_Free(Memo);
return Pointer;
}
POINTER list_NthElement(LIST List, NAT Number)
/**************************************************************
INPUT: A List and a natural number.
RETURNS: The <Number>th element of the list, NULL otherwise.
EFFECT: The function needs time O(Number).
***************************************************************/
{
while (!list_Empty(List) && --Number > 0)
List = list_Cdr(List);
if (list_Empty(List))
return NULL;
else
return list_Car(List);
}
void list_DeleteWithElement(LIST List, void (*ElementDelete)(POINTER))
/**************************************************************
INPUT: A list and a delete function for the elements.
RETURNS: Nothing.
EFFECT: The list and all its elements are deleted.
The function needs time O(n*d), where <n> is the length
of the list and <d> is the time for the delete function.
***************************************************************/
{
LIST Scan;
while (!list_Empty(List)) {
Scan = list_Cdr(List);
ElementDelete(list_Car(List));
list_Free(List);
List = Scan;
}
}
NAT list_DeleteWithElementCount(LIST List, void (*ElementDelete)(POINTER))
/**************************************************************
INPUT: A List and a delete function for the elements.
RETURNS: The number of deleted elements.
EFFECT: The List and all its elements are deleted.
The function needs time O(n*d), where <n> is the length
of the list and <d> is the time for the delete function.
***************************************************************/
{
int Result;
LIST Scan;
Result = 0;
while (!list_Empty(List)) {
Scan = list_Cdr(List);
ElementDelete(list_Car(List));
list_Free(List);
List = Scan;
Result++;
}
return Result;
}
LIST list_DeleteElement(LIST List, POINTER Element, BOOL (*Test)(POINTER, POINTER))
/**************************************************************
INPUT: A list, an element pointer, an equality test for
elements
RETURNS: The list where Element is deleted from List with
respect to Test.
EFFECTS: If List contains Element with respect to EqualityTest,
Element is deleted from List
CAUTION: Destructive. Be careful, the first element of a
list cannot be changed destructively by call by
reference.
***************************************************************/
{
LIST Scan1,Scan2;
while (!list_Empty(List) && Test(Element, list_Car(List))) {
Scan1 = list_Cdr(List);
list_Free(List);
List = Scan1;
}
if (list_Empty(List))
return list_Nil();
Scan2 = List;
Scan1 = list_Cdr(List);
while (!list_Empty(Scan1)) {
if (Test(Element, list_Car(Scan1))) {
list_Rplacd(Scan2, list_Cdr(Scan1));
list_Free(Scan1);
Scan1 = list_Cdr(Scan2);
}
else {
Scan2 = Scan1;
Scan1 = list_Cdr(Scan1);
}
}
return List;
}
LIST list_DeleteElementIf(LIST List, BOOL (*Test)(POINTER))
/**************************************************************
INPUT: A list and a test for elements.
RETURNS: The list where an element is deleted if <Test> on it
succeeds.
CAUTION: Destructive. Be careful, the first element of a list
cannot be changed destructively by call by
reference.
***************************************************************/
{
LIST Scan1,Scan2;
while (!list_Empty(List) && Test(list_Car(List))) {
Scan1 = list_Cdr(List);
list_Free(List);
List = Scan1;
}
if (list_Empty(List))
return list_Nil();
Scan2 = List;
Scan1 = list_Cdr(List);
while (!list_Empty(Scan1)) {
if (Test(list_Car(Scan1))) {
list_Rplacd(Scan2, list_Cdr(Scan1));
list_Free(Scan1);
Scan1 = list_Cdr(Scan2);
}
else {
Scan2 = Scan1;
Scan1 = list_Cdr(Scan1);
}
}
return List;
}
LIST list_DeleteElementIfFree(LIST List, BOOL (*Test)(POINTER),
void (*Delete)(POINTER))
/**************************************************************
INPUT: A list, a test for elements and a delete function
for elements.
RETURNS: The list where an element is deleted if <Test> on it
succeeds.
The element is deleted with <Delete>.
CAUTION: Destructive. Be careful, the first element of a list
cannot be changed destructively by call by reference.
***************************************************************/
{
LIST Scan1,Scan2;
while (!list_Empty(List) && Test(list_Car(List))) {
Scan1 = list_Cdr(List);
Delete(list_Car(List));
list_Free(List);
List = Scan1;
}
if (list_Empty(List))
return list_Nil();
Scan2 = List;
Scan1 = list_Cdr(List);
while (!list_Empty(Scan1)) {
if (Test(list_Car(Scan1))) {
Delete(list_Car(Scan1));
list_Rplacd(Scan2, list_Cdr(Scan1));
list_Free(Scan1);
Scan1 = list_Cdr(Scan2);
}
else {
Scan2 = Scan1;
Scan1 = list_Cdr(Scan1);
}
}
return List;
}
LIST list_DeleteElementFree(LIST List, POINTER Element,
BOOL (*Test)(POINTER, POINTER),
void (*Free)(POINTER))
/**************************************************************
INPUT: A list, an element pointer, an equality test for
elements and a free function for elements.
RETURNS: The list where Element is deleted from List with
respect to Test.
EFFECTS: If the list contains <Element> with respect to <Test>,
<Element> is deleted from the list and freed.
CAUTION: Destructive. Be careful, the first element of a list
cannot be changed destructively by call by reference.
***************************************************************/
{
LIST Scan1,Scan2;
while (!list_Empty(List) && Test(Element, list_Car(List))) {
Scan1 = list_Cdr(List);
Free(list_Car(List));
list_Free(List);
List = Scan1;
}
if (list_Empty(List))
return list_Nil();
Scan2 = List;
Scan1 = list_Cdr(List);
while (!list_Empty(Scan1)) {
if (Test(Element, list_Car(Scan1))) {
list_Rplacd(Scan2, list_Cdr(Scan1));
Free(list_Car(Scan1));
list_Free(Scan1);
Scan1 = list_Cdr(Scan2);
}
else {
Scan2 = Scan1;
Scan1 = list_Cdr(Scan1);
}
}
return List;
}
LIST list_DeleteOneElement(LIST List, POINTER Element, BOOL (*Test)(POINTER, POINTER))
/**************************************************************
INPUT: A list, an element pointer and an equality test for
elements.
RETURNS: The list where at most one element was deleted from
<List> if the Test between <Element> and the element
succeeds.
EFFECT: The function needs time O(n*t) in the worst case, and
time O(t) in the best case, where <n> is the length of
the list and t is the time for a call of the test function.
CAUTION: Destructive. Be careful, the first element of a list
cannot be changed destructively by call by
reference.
The memory of the deleted element is not freed.
***************************************************************/
{
LIST scan1, scan2;
if (list_Empty(List))
return List;
else {
if (Test(Element, list_Car(List)))
return list_Pop(List);
}
for (scan2 = List, scan1 = list_Cdr(List); !list_Empty(scan1);
scan2 = scan1, scan1 = list_Cdr(scan1)) {
if (Test(Element, list_Car(scan1))) {
list_Rplacd(scan2, list_Cdr(scan1));
list_Free(scan1);
scan1 = list_Cdr(scan2);
return List;
}
}
return List;
}
LIST list_PointerDeleteElement(LIST List, POINTER Element)
/**************************************************************
INPUT: A list and an element pointer
RETURNS: The list where Element is deleted from List with respect to
pointer equality.
EFFECTS: If <List> contains <Element> with respect to pointer equality,
<Element> is deleted from <List>.
This function needs time O(n), where <n> is the length of the list.
CAUTION: Destructive. Be careful, the first element of a list cannot
be changed destructively by call by reference.
***************************************************************/
{
LIST Scan1,Scan2;
while (!list_Empty(List) && Element == list_Car(List)) {
Scan1 = list_Cdr(List);
list_Free(List);
List = Scan1;
}
if (list_Empty(List))
return list_Nil();
Scan2 = List;
Scan1 = list_Cdr(List);
while (!list_Empty(Scan1)) {
if (Element == list_Car(Scan1)) {
list_Rplacd(Scan2, list_Cdr(Scan1));
list_Free(Scan1);
Scan1 = list_Cdr(Scan2);
}
else {
Scan2 = Scan1;
Scan1 = list_Cdr(Scan1);
}
}
return List;
}
LIST list_PointerDeleteElementFree(LIST List, POINTER Element,
void (*Free)(POINTER))
/**************************************************************
INPUT: A list, an element pointer and a free function for
elements.
RETURNS: The list where Element is deleted from List with
respect to pointer equality and freed.
EFFECTS: If List contains Element with respect to pointer
equality, Element is deleted from List
CAUTION: Destructive. Be careful, the first element of a list
cannot be changed destructively by call by
reference.
***************************************************************/
{
LIST Scan1,Scan2;
while (!list_Empty(List) && Element == list_Car(List)) {
Scan1 = list_Cdr(List);
Free(list_Car(List));
list_Free(List);
List = Scan1;
}
if (list_Empty(List))
return list_Nil();
Scan2 = List;
Scan1 = list_Cdr(List);
while (!list_Empty(Scan1)) {
if (Element == list_Car(Scan1)) {
list_Rplacd(Scan2, list_Cdr(Scan1));
Free(list_Car(Scan1));
list_Free(Scan1);
Scan1 = list_Cdr(Scan2);
}
else {
Scan2 = Scan1;
Scan1 = list_Cdr(Scan1);
}
}
return List;
}
LIST list_PointerDeleteOneElement(LIST List, POINTER Element)
/**************************************************************
INPUT: A list and an element pointer.
RETURNS: The list where one occurrence of Element is deleted from List
with respect to pointer equality.
EFFECTS: If List contains Element with respect to pointer equality,
Element is deleted from List.
CAUTION: Destructive. Be careful, the first element of a list cannot