@@ -18,11 +18,29 @@ package kotlinx.collections.immutable
18
18
19
19
import kotlinx.collections.immutable.internal.ListImplementation
20
20
21
+ /* *
22
+ * A generic immutable ordered collection of elements. Methods in this interface support only read-only access to the immutable list.
23
+ *
24
+ * Modification operations are supported through the [PersistentList] interface.
25
+ *
26
+ * Implementors of this interface take responsibility to be immutable.
27
+ * Once constructed they must contain the same elements in the same order.
28
+ *
29
+ * @param E the type of elements contained in the list. The immutable list is covariant on its element type.
30
+ */
21
31
public interface ImmutableList <out E > : List <E >, ImmutableCollection <E > {
22
32
33
+ /* *
34
+ * Returns a view of the portion of this list between the specified [fromIndex] (inclusive) and [toIndex] (exclusive).
35
+ *
36
+ * The returned list is backed by this list.
37
+ *
38
+ * @throws IndexOutOfBoundsException if [fromIndex] is less than zero or [toIndex] is greater than the size of this list.
39
+ * @throws IllegalArgumentException if [fromIndex] is greater than [toIndex].
40
+ */
23
41
override fun subList (fromIndex : Int , toIndex : Int ): ImmutableList <E > = SubList (this , fromIndex, toIndex)
24
42
25
- public class SubList <E >(private val source : ImmutableList <E >, private val fromIndex : Int , private val toIndex : Int ) : ImmutableList<E>, AbstractList<E>() {
43
+ private class SubList <E >(private val source : ImmutableList <E >, private val fromIndex : Int , private val toIndex : Int ) : ImmutableList<E>, AbstractList<E>() {
26
44
private var _size : Int = 0
27
45
28
46
init {
@@ -42,35 +60,111 @@ public interface ImmutableList<out E> : List<E>, ImmutableCollection<E> {
42
60
ListImplementation .checkRangeIndexes(fromIndex, toIndex, this ._size )
43
61
return SubList (source, this .fromIndex + fromIndex, this .fromIndex + toIndex)
44
62
}
45
-
46
63
}
47
64
}
48
65
66
+ /* *
67
+ * A generic persistent ordered collection of elements that supports adding and removing elements.
68
+ *
69
+ * Modification operations return new instances of the persistent list with the modification applied.
70
+ *
71
+ * @param E the type of elements contained in the list. The persistent list is covariant on its element type.
72
+ */
49
73
public interface PersistentList <out E > : ImmutableList <E >, PersistentCollection <E > {
74
+ /* *
75
+ * Returns a new persistent list with the specified [element] appended.
76
+ */
50
77
override fun add (element : @UnsafeVariance E ): PersistentList <E >
51
78
79
+ /* *
80
+ * Returns the result of appending all elements of the specified [elements] collection to this list.
81
+ *
82
+ * The elements are appended in the order they appear in the specified collection.
83
+ *
84
+ * @return a new persistent list with elements of the specified [elements] collection appended;
85
+ * or this instance if the specified collection is empty.
86
+ */
52
87
override fun addAll (elements : Collection <@UnsafeVariance E >): PersistentList <E > // = super <ImmutableCollection >.addAll(elements) as ImmutableList
53
88
89
+ /* *
90
+ * Returns the result of removing the first appearance of the specified [element] from this list.
91
+ *
92
+ * @return a new persistent list with the first appearance of the specified [element] removed;
93
+ * or this instance if there is no such element in this list.
94
+ */
54
95
override fun remove (element : @UnsafeVariance E ): PersistentList <E >
55
96
97
+ /* *
98
+ * Returns the result of removing all elements in this list that are also
99
+ * contained in the specified [elements] collection.
100
+ *
101
+ * @return a new persistent list with elements in this list that are also
102
+ * contained in the specified [elements] collection removed;
103
+ * or this instance if no modifications were made in the result of this operation.
104
+ */
56
105
override fun removeAll (elements : Collection <@UnsafeVariance E >): PersistentList <E >
57
106
107
+ /* *
108
+ * Returns the result of removing all elements in this list that match the specified [predicate].
109
+ *
110
+ * @return a new persistent list with elements matching the specified [predicate] removed;
111
+ * or this instance if no elements match the predicate.
112
+ */
58
113
override fun removeAll (predicate : (E ) -> Boolean ): PersistentList <E >
59
114
115
+ /* *
116
+ * Returns an empty persistent list.
117
+ */
60
118
override fun clear (): PersistentList <E >
61
119
62
120
121
+ /* *
122
+ * Returns the result of inserting the specified [c] collection at the specified [index].
123
+ *
124
+ * @return a new persistent list with the specified [c] collection inserted at the specified [index];
125
+ * or this instance if the specified collection is empty.
126
+ *
127
+ * @throws IndexOutOfBoundsException if [index] is out of bounds of this list.
128
+ */
63
129
fun addAll (index : Int , c : Collection <@UnsafeVariance E >): PersistentList <E > // = builder().apply { addAll(index, c.toList()) }.build()
64
130
131
+ /* *
132
+ * Returns a new persistent list with the element at the specified [index] replaced with the specified [element].
133
+ *
134
+ * @throws IndexOutOfBoundsException if [index] is out of bounds of this list.
135
+ */
65
136
fun set (index : Int , element : @UnsafeVariance E ): PersistentList <E >
66
137
67
138
/* *
68
- * Inserts an element into the list at the specified [index].
139
+ * Returns a new persistent list with the specified [element] inserted at the specified [index].
140
+ *
141
+ * @throws IndexOutOfBoundsException if [index] is out of bounds of this list.
69
142
*/
70
143
fun add (index : Int , element : @UnsafeVariance E ): PersistentList <E >
71
144
145
+ /* *
146
+ * Returns a new persistent list with the element at the specified [index] removed.
147
+ *
148
+ * @throws IndexOutOfBoundsException if [index] is out of bounds of this list.
149
+ */
72
150
fun removeAt (index : Int ): PersistentList <E >
73
151
152
+ /* *
153
+ * A generic builder of the persistent list. Builder exposes its modification operations through the [MutableList] interface.
154
+ *
155
+ * Builders are reusable, that is [build] method can be called multiple times with modifications between these calls.
156
+ * However, modifications applied do not affect previously built persistent list instances.
157
+ *
158
+ * Builder is backed by the same underlying data structure as the persistent list it was created from.
159
+ * Thus, [builder] and [build] methods take constant time consisting of passing the backing storage to the
160
+ * new builder and persistent list instances, respectively.
161
+ *
162
+ * The builder tracks which nodes in the structure are shared with the persistent list,
163
+ * and which are owned by it exclusively. It owns the nodes it copied during modification
164
+ * operations and avoids copying them on subsequent modifications.
165
+ *
166
+ * When [build] is called the builder forgets about all owned nodes it had created.
167
+ */
74
168
interface Builder <E >: MutableList <E >, PersistentCollection .Builder <E > {
75
169
override fun build (): PersistentList <E >
76
170
}
0 commit comments