1 package frost.collections
2
3 ====================================================================================================
4 A read/write random-access list of elements.
5
6 @see Array
7 ====================================================================================================
8 interface List<T> : ListView<T>, ListWriter<T>, Collection<T> {
9 ================================================================================================
10 Replaces a range of elements in this list with elements from another list.
11
12 For example,
13
14 -- testcase ListRangeAssignment(Simple)
15 def list:List<Object> := [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
16 list[0 .. 5] := ["Red", "Green", "Blue"]
17 Console.printLine(list)
18
19 This displays `[Red, Green, Blue, 6, 7, 8, 9, 10]`.
20 ================================================================================================
21 @default
22 @pre(range.min <= range.max)
23 method []:=(range:Range<Int>, list:ListView<T>) {
24 -- FIXME performance, doing this the slow and stupid way for now
25 var max := range.max
26 if !range.inclusive {
27 max -= 1
28 }
29 for i in max ... range.min by -1 {
30 removeIndex(i)
31 }
32 var index := range.min
33 for v in list {
34 insert(index, v)
35 index += 1
36 }
37 }
38
39 ================================================================================================
40 Replaces a range of elements in this list with elements from another list.
41
42 For example,
43
44 -- testcase ListNullableRangeAssignment(Simple)
45 def list:List<Object> := [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
46 list[3..] := ["Red", "Green", "Blue"]
47 Console.printLine(list)
48
49 This displays `[1, 2, 3, Red, Green, Blue]`.
50 ================================================================================================
51 @default
52 method []:=(range:Range<Int?>, list:ListView<T>) {
53 var min := range.min
54 if min == null {
55 min := 0
56 }
57
58 var max := range.max
59 if max == null {
60 max := count
61 }
62
63 self[Range<Int>(min, max, range.inclusive)] := list
64 }
65
66 @override
67 @default
68 method filterInPlace(test:(T)=>(Bit)) {
69 for i in count - 1 ... 0 by -1 {
70 if !test(self[i]) {
71 removeIndex(i)
72 }
73 }
74 }
75
76 @override
77 @default
78 method mapInPlace(f:(T)=>(T)) {
79 for i in 0 .. count {
80 self[i] := f(self[i])
81 }
82 }
83
84 ================================================================================================
85 As [sort], but performs the sort in-place rather than returning a new list.
86 ================================================================================================
87 @default
88 method sortInPlace(greater:(T, T)=>(Bit)) {
89 MergeSort.sort(self, greater)
90 }
91 }
92