/
List.mo
7098 lines (6374 loc) · 192 KB
/
List.mo
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 OpenModelica.
*
* Copyright (c) 1998-2014, Open Source Modelica Consortium (OSMC),
* c/o Linköpings universitet, Department of Computer and Information Science,
* SE-58183 Linköping, Sweden.
*
* All rights reserved.
*
* THIS PROGRAM IS PROVIDED UNDER THE TERMS OF GPL VERSION 3 LICENSE OR
* THIS OSMC PUBLIC LICENSE (OSMC-PL) VERSION 1.2.
* ANY USE, REPRODUCTION OR DISTRIBUTION OF THIS PROGRAM CONSTITUTES
* RECIPIENT'S ACCEPTANCE OF THE OSMC PUBLIC LICENSE OR THE GPL VERSION 3,
* ACCORDING TO RECIPIENTS CHOICE.
*
* The OpenModelica software and the Open Source Modelica
* Consortium (OSMC) Public License (OSMC-PL) are obtained
* from OSMC, either from the above address,
* from the URLs: http://www.ida.liu.se/projects/OpenModelica or
* http://www.openmodelica.org, and in the OpenModelica distribution.
* GNU version 3 is obtained from: http://www.gnu.org/copyleft/gpl.html.
*
* This program is distributed WITHOUT ANY WARRANTY; without
* even the implied warranty of MERCHANTABILITY or FITNESS
* FOR A PARTICULAR PURPOSE, EXCEPT AS EXPRESSLY SET FORTH
* IN THE BY RECIPIENT SELECTED SUBSIDIARY LICENSE CONDITIONS OF OSMC-PL.
*
* See the full OSMC Public License conditions for more details.
*
*/
encapsulated package List
" file: List.mo
package: List
description: List functions
This package contains all functions that operate on the List type, such as
mapping and filtering functions.
Most of the functions in this package follows a naming convention that looks
like (? means zero or one, + means one or more, * means zero or more):
(operation(n)?(_m)?(prefix)*)+
operation: The operation that the function does, i.e. mapping, folding, etc.
n: The number of extra arguments that the function takes.
m: The number of lists created.
prefix: One of the following prefixes:
AllValue: Checks that all elements of the list matches a given value.
Bool: Returns true or false, instead of succeeding or failing.
Elt: Takes a single element instead of a list.
F: Will fail instead of returning the input list when
appropriate.
Flat: An operator function that would normally return an
element, such as in map, will return a list instead. The
returned lists are flattened into a single list.
IntN: A special version for integers between 1 and N.
Last: Operates on the tail of the list.
List: Operates on a list of lists.
N: Returns a list of N elements.
OnBool: Decides which operation to do based on a given boolean value.
OnSuccess: Takes an operation function that succeeds or fails.
OnTrue: Takes an operation function that returns true or false.
Option: Operates on options.
r: Takes an operation function with the arguments reversed.
Reverse: Returns the processed list in reverse order.
Sorted: Expects the given list(s) to be sorted.
Tuple: Operates on tuples, either by expecting tuple types as
input or by returning tuples instead of multiple lists.
All operator functions has the same parameter order as the types defined
below, i.e. ValueType before ElementType, and so on. Some types are
bidirectional, in which case they appear commented out in the outputs list
below just to show the order. The r prefix changes this order by either moving
FoldType to the top if FoldType is used, otherwise moving the ElementType to the
bottom.
The n and m numbers define the number of extra arguments or lists created, and
is only used when they deviate from the expected values. I.e. map should be
called map_1 according to the convention, since it creates one list. But this
is the expected number of lists, so the _1 is omitted.
An example of this convention:
fold2 is a fold function, and as such it takes at least a list, a fold
function and a fold argument, and returns the updated fold argument. The 2
after it's name means that it also takes two extra arguments. Following the
ordering of the types below we get that the order of it's signature is:
(elementlist, fold function, extra arg 1, extra arg 2, fold arg) -> fold arg
and the signature of the fold function that it takes is:
(element, extra arg 1, extra arg 2, fold arg) -> fold arg
"
protected
import MetaModelica.Dangerous.{listReverseInPlace, arrayGetNoBoundsChecking, arrayUpdateNoBoundsChecking, arrayCreateNoInit};
import MetaModelica.Dangerous;
import DoubleEndedList;
import GC;
public function create<T>
"Creates a list from an element."
input T inElement;
output list<T> outList = {inElement};
end create;
public function create2<T>
"Creates a list from two elements."
input T inElement1;
input T inElement2;
output list<T> outList = {inElement1, inElement2};
end create2;
public function fill<T>
"Returns a list of n element.
Example: fill(2, 3) => {2, 2, 2}"
input T inElement;
input Integer inCount;
output list<T> outList = {};
protected
Integer i = 0;
algorithm
while i < inCount loop
outList := inElement :: outList;
i := i + 1;
end while;
end fill;
public function intRange
"Returns a list of n integers from 1 to inStop.
Example: listIntRange(3) => {1,2,3}"
input Integer inStop;
output list<Integer> outRange = {};
protected
Integer i = inStop;
algorithm
while i > 0 loop
outRange := i :: outRange;
i := i - 1;
end while;
end intRange;
public function intRange2
"Returns a list of integers from inStart to inStop.
Example listIntRange2(3,5) => {3,4,5}"
input Integer inStart;
input Integer inStop;
output list<Integer> outRange = {};
protected
Integer i = inStop;
algorithm
if inStart < inStop then
while i >= inStart loop
outRange := i :: outRange;
i := i - 1;
end while;
else
while i <= inStart loop
outRange := i :: outRange;
i := i + 1;
end while;
end if;
end intRange2;
public function intRange3
"Returns a list of integers from inStart to inStop with step inStep.
Example: listIntRange2(3,2,9) => {3,5,7,9}"
input Integer inStart;
input Integer inStep;
input Integer inStop;
output list<Integer> outRange;
algorithm
if inStep == 0 then fail(); end if;
outRange := list(i for i in inStart:inStep:inStop);
end intRange3;
public function toOption<T>
"Returns an option of the element in a list if the list contains exactly one
element, NONE() if the list is empty and fails if the list contains more than
one element."
input list<T> inList;
output Option<T> outOption;
algorithm
outOption := match(inList)
local
T e;
case {} then NONE();
case {e} then SOME(e);
end match;
end toOption;
public function fromOption<T>
"Returns an empty list for NONE() and a list containing the element for
SOME(element)."
input Option<T> inElement;
output list<T> outList;
algorithm
outList := match(inElement)
local
T e;
case SOME(e) then {e};
else {};
end match;
end fromOption;
public function assertIsEmpty<T>
"Fails if the given list is not empty."
input list<T> inList;
algorithm
{} := inList;
end assertIsEmpty;
public function isEqual<T>
"Checks if two lists are equal. If inEqualLength is true the lists are assumed
to be of equal length, and if it is false they can be of different lengths (in
which case only the overlapping parts of the lists are checked)."
input list<T> inList1;
input list<T> inList2;
input Boolean inEqualLength;
output Boolean outIsEqual;
algorithm
outIsEqual := match(inList1, inList2, inEqualLength)
local
T e1, e2;
list<T> rest1, rest2;
case (e1 :: rest1, e2 :: rest2, _) guard(valueEq(e1, e2))
then isEqual(rest1, rest2, inEqualLength);
case ({}, {}, _) then true;
case ({}, _, false) then true;
case (_, {}, false) then true;
else false;
end match;
end isEqual;
public function isEqualOnTrue<T1, T2>
"Takes two lists and an equality function, and returns whether the lists are
equal or not."
input list<T1> inList1;
input list<T2> inList2;
input CompFunc inCompFunc;
output Boolean outIsEqual;
partial function CompFunc
input T1 inElement1;
input T2 inElement2;
output Boolean outIsEqual;
end CompFunc;
algorithm
outIsEqual := match(inList1, inList2)
local
T1 e1;
T2 e2;
list<T1> rest1;
list<T2> rest2;
case (e1 :: rest1, e2 :: rest2) guard(inCompFunc(e1, e2))
then isEqualOnTrue(rest1, rest2, inCompFunc);
case ({}, {}) then true;
else false;
end match;
end isEqualOnTrue;
public function isPrefixOnTrue<T1, T2>
"Checks if the first list is a prefix of the second list, i.e. that all
elements in the first list is equal to the corresponding elements in the
second list."
input list<T1> inList1;
input list<T2> inList2;
input CompFunc inCompFunc;
output Boolean outIsPrefix;
partial function CompFunc
input T1 inElement1;
input T2 inElement2;
output Boolean outIsEqual;
end CompFunc;
algorithm
outIsPrefix := match(inList1, inList2)
local
T1 e1;
list<T1> rest1;
T2 e2;
list<T2> rest2;
case (e1 :: rest1, e2 :: rest2) guard(inCompFunc(e1, e2))
then isPrefixOnTrue(rest1, rest2, inCompFunc);
case ({}, _) then true;
else false;
end match;
end isPrefixOnTrue;
public function consr<T>
"The same as the builtin cons operator, but with the order of the arguments
swapped."
input list<T> inList;
input T inElement;
output list<T> outList;
algorithm
outList := inElement :: inList;
end consr;
public function consOnTrue<T>
"Adds the element to the front of the list if the condition is true."
input Boolean inCondition;
input T inElement;
input list<T> inList;
output list<T> outList;
algorithm
outList := if inCondition then inElement :: inList else inList;
end consOnTrue;
public function consOnSuccess<T>
"Adds the element to the front of the list if the predicate succeeds.
Prefer using consOnTrue instead of this function, it's more efficient."
input T inElement;
input list<T> inList;
input Predicate inPredicate;
output list<T> outList;
partial function Predicate
input T inElement;
end Predicate;
algorithm
try
inPredicate(inElement);
outList := inElement :: inList;
else
outList := inList;
end try;
end consOnSuccess;
public function consOption<T>
"Adds an optional element to the front of the list, or returns the list if the
element is none."
input Option<T> inElement;
input list<T> inList;
output list<T> outList;
algorithm
outList := match(inElement)
local
T e;
case SOME(e) then e :: inList;
else inList;
end match;
end consOption;
public function consOnBool<T>
"Adds an element to one of two lists, depending on the given boolean value."
input Boolean inValue;
input T inElement;
input output list<T> trueList;
input output list<T> falseList;
algorithm
if inValue then
trueList := inElement :: trueList;
else
falseList := inElement :: falseList;
end if;
end consOnBool;
public function consN<T>
"concate n time inElement to the list:
n = 5, inElement=1, list={1,2} -> list={1,1,1,1,1,1,2}"
input Integer size;
input T inElement;
input output list<T> inList;
algorithm
for i in 1:size loop
inList := inElement :: inList;
end for;
end consN;
public function append_reverse<T>
"Appends the elements from list1 in reverse order to list2."
input list<T> inList1;
input list<T> inList2;
output list<T> outList=inList2;
algorithm
// Do not optimize the case listEmpty(inList2) and listLength(inList1)==1
// since we use listReverseInPlace together with this function.
// An alternative would be to keep both (and rename this append_reverse_always_copy)
for e in inList1 loop
outList := e::outList;
end for;
end append_reverse;
public function appendr<T>
"Appends two lists in reverse order compared to listAppend."
input list<T> inList1;
input list<T> inList2;
output list<T> outList;
algorithm
outList := listAppend(inList2, inList1);
end appendr;
public function appendElt<T>
"Appends an element to the end of the list. Note that this is very
inefficient, so try to avoid using this function."
input T inElement;
input list<T> inList;
output list<T> outList;
algorithm
outList := listAppend(inList, {inElement});
end appendElt;
public function appendLastList<T>
"Appends a list to the last list in a list of lists."
input list<list<T>> inListList;
input list<T> inList;
output list<list<T>> outListList;
algorithm
outListList := match(inListList, inList)
local
list<T> l;
list<list<T>> ll;
list<list<T>> ol = {};
case ({}, _) then {inList};
case ({l}, _)
then {listAppend(l, inList)};
case (l :: ll, _)
algorithm
while not listEmpty(ll) loop
ol := l::ol;
l::ll := ll;
end while;
ol := listAppend(l, inList) :: ol;
ol := listReverseInPlace(ol);
then ol;
end match;
end appendLastList;
public function insert<T>
"Inserts an element at a position
example: insert({2,1,4,2},2,3) => {2,3,1,4,2} "
input list<T> inList;
input Integer inN;
input T inElement;
output list<T> outList;
protected
list<T> lst1, lst2;
algorithm
true := (inN > 0);
(lst1, lst2) := splitr(inList, inN-1);
outList := append_reverse(lst1,inElement::lst2);
end insert;
public function insertListSorted<T>
"Inserts an sorted list into another sorted list. O(n)
example: insertListSorted({1,2,4,5},{3,4,8},intGt) => {1,2,3,4,4,5,8}"
input list<T> inList;
input list<T> inList2;
input CompareFunc inCompFunc;
output list<T> outList;
partial function CompareFunc
input T inElement1;
input T inElement2;
output Boolean inRes;
end CompareFunc;
algorithm
outList := listReverseInPlace(insertListSorted1(inList, inList2, inCompFunc, {}));
end insertListSorted;
protected function insertListSorted1<T>
"Iterate over the first given list and add it to the result list if the comparison function with the head of the second list returns true.
The result is a sorted list in reverse order."
input list<T> inList;
input list<T> inList2;
input CompareFunc inCompFunc;
input list<T> inResultList;
output list<T> outResultList;
partial function CompareFunc
input T inElement1;
input T inElement2;
output Boolean inRes;
end CompareFunc;
protected
list<T> listRest, listRest2, tmpResultList;
T listHead, listHead2;
T elem;
algorithm
outResultList := match(inList, inList2, inCompFunc, inResultList)
case({},{},_,_)
then inResultList;
case({},_,_,_)
then append_reverse(inList2, inResultList);
case(_,{},_,_)
then append_reverse(inList, inResultList);
case(listHead::listRest, listHead2::listRest2,_,_)
equation
if(inCompFunc(listHead, listHead2)) then
tmpResultList = listHead::inResultList;
tmpResultList = insertListSorted1(listRest, inList2, inCompFunc, tmpResultList);
else
tmpResultList = listHead2::inResultList;
tmpResultList = insertListSorted1(inList, listRest2, inCompFunc, tmpResultList);
end if;
then tmpResultList;
end match;
end insertListSorted1;
public function set<T>
"set an element at a position
example: set({2,1,4,2},2,3) => {2,3,4,2} "
input list<T> inList;
input Integer inN;
input T inElement;
output list<T> outList;
protected
list<T> lst1, lst2;
algorithm
true := (inN > 0);
(lst1, lst2) := splitr(inList, inN-1);
lst2 := stripFirst(lst2);
outList := append_reverse(lst1,inElement::lst2);
end set;
public function first<T>
"Returns the first element of a list. Fails if the list is empty."
input list<T> inList;
output T out;
algorithm
out := match(inList)
local
T e;
case e :: _ then e;
end match;
end first;
public function firstOrEmpty<T>
"Returns the first element of a list as a list, or an empty list if the given
list is empty."
input list<T> inList;
output list<T> outList;
algorithm
outList := match(inList)
local
T e;
case e :: _ then {e};
else {};
end match;
end firstOrEmpty;
public function second<T>
"Returns the second element of a list. Fails if the list is empty."
input list<T> inList;
output T outSecond;
algorithm
outSecond := listGet(inList, 2);
end second;
public function last<T>
"Returns the last element of a list. Fails if the list is empty."
input list<T> inList;
output T outLast;
algorithm
outLast := match(inList)
local
T e;
list<T> rest;
case (e :: rest)
algorithm
while not listEmpty(rest) loop
e::rest := rest;
end while;
then e;
end match;
end last;
public function lastElement<T>
"Returns the last cons-cell of a list. Fails if the list is empty. Also returns the list length."
input list<T> inList;
output list<T> lst;
output Integer listLength=0;
protected
list<T> rest=inList;
algorithm
false := listEmpty(rest);
while not listEmpty(rest) loop
(lst as (_::rest)) := rest;
listLength := listLength+1;
end while;
end lastElement;
public function lastListOrEmpty<T>
"Returns the last element(list) of a list of lists. Returns empty list
if the outer list is empty."
input list<list<T>> inListList;
output list<T> outLastList = {};
protected
list<list<T>> rest = inListList;
algorithm
while not listEmpty(rest) loop
outLastList :: rest := rest;
end while;
end lastListOrEmpty;
public function secondLast<T>
"Returns the second last element of a list, or fails if such an element does
not exist."
input list<T> inList;
output T outSecondLast;
algorithm
_ :: outSecondLast :: _ := listReverse(inList);
end secondLast;
public function lastN<T>
"Returns the last N elements of a list."
input list<T> inList;
input Integer inN;
output list<T> outList;
protected
Integer len;
algorithm
true := inN >= 0;
len := listLength(inList);
outList := stripN(inList, len - inN);
end lastN;
public function rest<T>
"Returns all elements except for the first in a list."
input list<T> inList;
output list<T> outList;
algorithm
_ :: outList := inList;
end rest;
public function restCond<T>
"Returns all elements except for the first in a list."
input Boolean cond;
input list<T> inList;
output list<T> outList;
algorithm
outList := if cond then listRest(inList) else inList;
end restCond;
public function restOrEmpty<T>
"Returns all elements except for the first in a list, or the empty list of the
list is empty."
input list<T> inList;
output list<T> outList;
algorithm
outList := if listEmpty(inList) then inList else listRest(inList);
end restOrEmpty;
public function getIndexFirst<T>
input Integer index;
input list<T> inList;
output T element;
algorithm
element := listGet(inList, index);
end getIndexFirst;
public function firstN<T>
"Returns the first N elements of a list, or fails if there are not enough
elements in the list."
input list<T> inList;
input Integer inN;
output list<T> outList = {};
protected
T e;
list<T> rest;
algorithm
true := (inN >= 0);
rest := inList;
for i in 1:inN loop
e :: rest := rest;
outList := e :: outList;
end for;
outList := listReverseInPlace(outList);
end firstN;
public function stripFirst<T>
"Removes the first element of a list, but returns the empty list if the given
list is empty."
input list<T> inList;
output list<T> outList;
algorithm
if listEmpty(inList) then
outList := {};
else
_::outList := inList;
end if;
end stripFirst;
public function stripLast<T>
"Removes the last element of a list. If the list is the empty list, the
function returns the empty list."
input list<T> inList;
output list<T> outList;
algorithm
if listEmpty(inList) then
outList := {};
else
_ :: outList := listReverse(inList);
outList := listReverseInPlace(outList);
end if;
end stripLast;
public function stripN<T>
"Strips the N first elements from a list. Fails if the list contains less than
N elements, or if N is negative."
input list<T> inList;
input Integer inN;
output list<T> outList = inList;
algorithm
true := inN >= 0;
for i in 1:inN loop
_ :: outList := outList;
end for;
end stripN;
public function sort<T>
"Sorts a list given an ordering function with the mergesort algorithm.
Example:
sort({2, 1, 3}, intGt) => {1, 2, 3}
sort({2, 1, 3}, intLt) => {3, 2, 1}"
input list<T> inList;
input CompareFunc inCompFunc;
output list<T> outList= {};
partial function CompareFunc
input T inElement1;
input T inElement2;
output Boolean inRes;
end CompareFunc;
protected
list<T> rest = inList;
T e1, e2;
list<T> left, right;
Integer middle;
algorithm
if not listEmpty(rest) then
e1 :: rest := rest;
if listEmpty(rest) then
outList := inList;
else
e2 :: rest := rest;
if listEmpty(rest) then
outList := if inCompFunc(e2, e1) then inList else e2::{e1};
else
middle := intDiv(listLength(inList), 2);
(left, right) := split(inList, middle);
left := sort(left, inCompFunc);
right := sort(right, inCompFunc);
outList := merge(left, right, inCompFunc, {});
end if;
end if;
end if;
end sort;
public function sortedDuplicates<T>
"Returns a list of all duplicates in a sorted list, using the given comparison
function to check for equality."
input list<T> inList;
input CompareFunc inCompFunc "Equality comparator";
output list<T> outDuplicates = {};
partial function CompareFunc
input T inElement1;
input T inElement2;
output Boolean outEqual;
end CompareFunc;
protected
T e;
list<T> rest = inList;
algorithm
while not listEmpty(rest) loop
e :: rest := rest;
if not listEmpty(rest) and inCompFunc(e, listHead(rest)) then
outDuplicates := e :: outDuplicates;
end if;
end while;
outDuplicates := listReverseInPlace(outDuplicates);
end sortedDuplicates;
public function sortedListAllUnique<T>
"The input is a sorted list. The functions checks if all elements are unique."
input list<T> lst;
input CompareFunc compare;
output Boolean allUnique = false;
partial function CompareFunc
input T inElement1;
input T inElement2;
output Boolean outEqual;
end CompareFunc;
protected
T e;
list<T> rest = lst;
algorithm
while not listEmpty(rest) loop
rest := match rest
local
T e1,e2;
case {_} then {};
case e1::(rest as e2::_)
algorithm
if compare(e1,e2) then
return;
end if;
then rest;
end match;
end while;
allUnique := true;
end sortedListAllUnique;
public function sortedUnique<T>
"Returns a list of unique elements in a sorted list, using the given
comparison function to check for equality."
input list<T> inList;
input CompareFunc inCompFunc;
output list<T> outUniqueElements = {};
partial function CompareFunc
input T inElement1;
input T inElement2;
output Boolean outEqual;
end CompareFunc;
protected
T e;
list<T> rest = inList;
algorithm
while not listEmpty(rest) loop
e :: rest := rest;
if listEmpty(rest) or not inCompFunc(e, listHead(rest)) then
outUniqueElements := e :: outUniqueElements;
end if;
end while;
outUniqueElements := listReverseInPlace(outUniqueElements);
end sortedUnique;
public function sortedUniqueAndDuplicates<T>
"Returns a list with all duplicate elements removed, as well as a list of the
removed elements, using the given comparison function to check for equality."
input list<T> inList;
input CompareFunc inCompFunc;
output list<T> outUniqueElements = {};
output list<T> outDuplicateElements = {};
partial function CompareFunc
input T inElement1;
input T inElement2;
output Boolean outEqual;
end CompareFunc;
protected
T e;
list<T> rest = inList;
algorithm
while not listEmpty(rest) loop
e :: rest := rest;
if not listEmpty(rest) and inCompFunc(e, listHead(rest)) then
outDuplicateElements := e :: outDuplicateElements;
else
outUniqueElements := e :: outUniqueElements;
end if;
end while;
outUniqueElements := listReverseInPlace(outUniqueElements);
outDuplicateElements := listReverseInPlace(outDuplicateElements);
end sortedUniqueAndDuplicates;
public function sortedUniqueOnlyDuplicates<T>
"Returns a list with all duplicate elements removed, as well as a list of the
removed elements, using the given comparison function to check for equality."
input list<T> inList;
input CompareFunc inCompFunc;
output list<T> outDuplicateElements = {};
partial function CompareFunc
input T inElement1;
input T inElement2;
output Boolean outEqual;
end CompareFunc;
protected
T e;
list<T> rest = inList;
algorithm
while not listEmpty(rest) loop
e :: rest := rest;
if not listEmpty(rest) and inCompFunc(e, listHead(rest)) then
outDuplicateElements := e :: outDuplicateElements;
end if;
end while;
outDuplicateElements := listReverseInPlace(outDuplicateElements);
end sortedUniqueOnlyDuplicates;
protected function merge<T>
"Helper function to sort, merges two sorted lists."
input list<T> inLeft;
input list<T> inRight;
input CompareFunc inCompFunc;
input list<T> acc;
output list<T> outList;
partial function CompareFunc
input T inElement1;
input T inElement2;
output Boolean outRes;
end CompareFunc;
algorithm
outList := match (inLeft, inRight)
local
Boolean b;
T l, r, el;
list<T> l_rest, r_rest, res;
/* Tail recursive version */
case (l :: l_rest, r :: r_rest)
algorithm
if inCompFunc(r, l) then
r_rest := inRight;
el := l;
else
l_rest := inLeft;
el := r;
end if;
then
merge(l_rest, r_rest, inCompFunc, el :: acc);
case ({}, {}) then listReverseInPlace(acc);
case ({}, _) then append_reverse(acc,inRight);
case (_, {}) then append_reverse(acc,inLeft);
end match;
end merge;
public function mergeSorted<T>
"This function merges two sorted lists into one sorted list. It takes a
comparison function that defines a strict weak ordering of the elements, i.e.
that returns true if the first element should be placed before the second
element in the sorted list."
input list<T> inList1;
input list<T> inList2;
input CompFunc inCompFunc;
output list<T> outList = {};
partial function CompFunc
input T inElement1;
input T inElement2;
output Boolean outIsEqual;
end CompFunc;
protected
list<T> l1, l2;
T e1, e2;
algorithm
l1 := inList1;
l2 := inList2;
// While both lists contain elements.
while not listEmpty(l1) and not listEmpty(l2) loop
e1 :: _ := l1;
e2 :: _ := l2;
// Move the smallest head from either list to accumulator.
if inCompFunc(e1, e2) then
outList := e1 :: outList;
_ :: l1 := l1;
else
outList := e2 :: outList;
_ :: l2 := l2;
end if;
end while;
// Reverse accumulator and append the remaining elements.
l1 := if listEmpty(l1) then l2 else l1;
outList := append_reverse(outList, l1);
end mergeSorted;
public function sortIntN
"Provides same functionality as sort, but for integer values between 1
and N. The complexity in this case is O(n)"
input list<Integer> inList;