@@ -18,11 +18,29 @@ package kotlinx.collections.immutable
1818
1919import kotlinx.collections.immutable.internal.ListImplementation
2020
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+ */
2131public interface ImmutableList <out E > : List <E >, ImmutableCollection <E > {
2232
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+ */
2341 override fun subList (fromIndex : Int , toIndex : Int ): ImmutableList <E > = SubList (this , fromIndex, toIndex)
2442
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>() {
2644 private var _size : Int = 0
2745
2846 init {
@@ -42,35 +60,111 @@ public interface ImmutableList<out E> : List<E>, ImmutableCollection<E> {
4260 ListImplementation .checkRangeIndexes(fromIndex, toIndex, this ._size )
4361 return SubList (source, this .fromIndex + fromIndex, this .fromIndex + toIndex)
4462 }
45-
4663 }
4764}
4865
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+ */
4973public interface PersistentList <out E > : ImmutableList <E >, PersistentCollection <E > {
74+ /* *
75+ * Returns a new persistent list with the specified [element] appended.
76+ */
5077 override fun add (element : @UnsafeVariance E ): PersistentList <E >
5178
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+ */
5287 override fun addAll (elements : Collection <@UnsafeVariance E >): PersistentList <E > // = super <ImmutableCollection >.addAll(elements) as ImmutableList
5388
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+ */
5495 override fun remove (element : @UnsafeVariance E ): PersistentList <E >
5596
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+ */
56105 override fun removeAll (elements : Collection <@UnsafeVariance E >): PersistentList <E >
57106
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+ */
58113 override fun removeAll (predicate : (E ) -> Boolean ): PersistentList <E >
59114
115+ /* *
116+ * Returns an empty persistent list.
117+ */
60118 override fun clear (): PersistentList <E >
61119
62120
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+ */
63129 fun addAll (index : Int , c : Collection <@UnsafeVariance E >): PersistentList <E > // = builder().apply { addAll(index, c.toList()) }.build()
64130
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+ */
65136 fun set (index : Int , element : @UnsafeVariance E ): PersistentList <E >
66137
67138 /* *
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.
69142 */
70143 fun add (index : Int , element : @UnsafeVariance E ): PersistentList <E >
71144
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+ */
72150 fun removeAt (index : Int ): PersistentList <E >
73151
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+ */
74168 interface Builder <E >: MutableList <E >, PersistentCollection .Builder <E > {
75169 override fun build (): PersistentList <E >
76170 }
0 commit comments