Skip to content

Commit d0eac5a

Browse files
committed
Add persistent Collection builder functions
1 parent bd5ac10 commit d0eac5a

File tree

6 files changed

+142
-0
lines changed

6 files changed

+142
-0
lines changed

core/api/kotlinx-collections-immutable.api

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,9 @@
11
public final class kotlinx/collections/immutable/ExtensionsKt {
2+
public static final fun buildPersistentHashMap (Lkotlin/jvm/functions/Function1;)Lkotlinx/collections/immutable/PersistentMap;
3+
public static final fun buildPersistentHashSet (Lkotlin/jvm/functions/Function1;)Lkotlinx/collections/immutable/PersistentSet;
4+
public static final fun buildPersistentList (Lkotlin/jvm/functions/Function1;)Lkotlinx/collections/immutable/PersistentList;
5+
public static final fun buildPersistentMap (Lkotlin/jvm/functions/Function1;)Lkotlinx/collections/immutable/PersistentMap;
6+
public static final fun buildPersistentSet (Lkotlin/jvm/functions/Function1;)Lkotlinx/collections/immutable/PersistentSet;
27
public static final fun immutableHashMapOf ([Lkotlin/Pair;)Lkotlinx/collections/immutable/PersistentMap;
38
public static final fun immutableHashSetOf ([Ljava/lang/Object;)Lkotlinx/collections/immutable/PersistentSet;
49
public static final fun immutableListOf ()Lkotlinx/collections/immutable/PersistentList;

core/api/kotlinx-collections-immutable.klib.api

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -242,6 +242,8 @@ final inline fun <#A: kotlin/Any?, #B: kotlin/Any?> (kotlinx.collections.immutab
242242
final inline fun <#A: kotlin/Any?, #B: kotlin/Any?> (kotlinx.collections.immutable/PersistentMap<out #A, #B>).kotlinx.collections.immutable/plus(kotlin.sequences/Sequence<kotlin/Pair<#A, #B>>): kotlinx.collections.immutable/PersistentMap<#A, #B> // kotlinx.collections.immutable/plus|[email protected]<out|0:0,0:1>(kotlin.sequences.Sequence<kotlin.Pair<0:0,0:1>>){0§<kotlin.Any?>;1§<kotlin.Any?>}[0]
243243
final inline fun <#A: kotlin/Any?, #B: kotlin/Any?> (kotlinx.collections.immutable/PersistentMap<out #A, #B>).kotlinx.collections.immutable/plus(kotlin/Array<out kotlin/Pair<#A, #B>>): kotlinx.collections.immutable/PersistentMap<#A, #B> // kotlinx.collections.immutable/plus|[email protected]<out|0:0,0:1>(kotlin.Array<out|kotlin.Pair<0:0,0:1>>){0§<kotlin.Any?>;1§<kotlin.Any?>}[0]
244244
final inline fun <#A: kotlin/Any?, #B: kotlin/Any?> (kotlinx.collections.immutable/PersistentMap<out #A, #B>).kotlinx.collections.immutable/plus(kotlin/Pair<#A, #B>): kotlinx.collections.immutable/PersistentMap<#A, #B> // kotlinx.collections.immutable/plus|[email protected]<out|0:0,0:1>(kotlin.Pair<0:0,0:1>){0§<kotlin.Any?>;1§<kotlin.Any?>}[0]
245+
final inline fun <#A: kotlin/Any?, #B: kotlin/Any?> kotlinx.collections.immutable/buildPersistentHashMap(kotlin/Function1<kotlin.collections/MutableMap<#A, #B>, kotlin/Unit>): kotlinx.collections.immutable/PersistentMap<#A, #B> // kotlinx.collections.immutable/buildPersistentHashMap|buildPersistentHashMap(kotlin.Function1<kotlin.collections.MutableMap<0:0,0:1>,kotlin.Unit>){0§<kotlin.Any?>;1§<kotlin.Any?>}[0]
246+
final inline fun <#A: kotlin/Any?, #B: kotlin/Any?> kotlinx.collections.immutable/buildPersistentMap(kotlin/Function1<kotlin.collections/MutableMap<#A, #B>, kotlin/Unit>): kotlinx.collections.immutable/PersistentMap<#A, #B> // kotlinx.collections.immutable/buildPersistentMap|buildPersistentMap(kotlin.Function1<kotlin.collections.MutableMap<0:0,0:1>,kotlin.Unit>){0§<kotlin.Any?>;1§<kotlin.Any?>}[0]
245247
final inline fun <#A: kotlin/Any?> (kotlinx.collections.immutable/PersistentCollection<#A>).kotlinx.collections.immutable/minus(#A): kotlinx.collections.immutable/PersistentCollection<#A> // kotlinx.collections.immutable/minus|[email protected]<0:0>(0:0){0§<kotlin.Any?>}[0]
246248
final inline fun <#A: kotlin/Any?> (kotlinx.collections.immutable/PersistentCollection<#A>).kotlinx.collections.immutable/plus(#A): kotlinx.collections.immutable/PersistentCollection<#A> // kotlinx.collections.immutable/plus|[email protected]<0:0>(0:0){0§<kotlin.Any?>}[0]
247249
final inline fun <#A: kotlin/Any?> (kotlinx.collections.immutable/PersistentList<#A>).kotlinx.collections.immutable/minus(#A): kotlinx.collections.immutable/PersistentList<#A> // kotlinx.collections.immutable/minus|[email protected]<0:0>(0:0){0§<kotlin.Any?>}[0]
@@ -250,3 +252,6 @@ final inline fun <#A: kotlin/Any?> (kotlinx.collections.immutable/PersistentList
250252
final inline fun <#A: kotlin/Any?> (kotlinx.collections.immutable/PersistentSet<#A>).kotlinx.collections.immutable/minus(#A): kotlinx.collections.immutable/PersistentSet<#A> // kotlinx.collections.immutable/minus|[email protected]<0:0>(0:0){0§<kotlin.Any?>}[0]
251253
final inline fun <#A: kotlin/Any?> (kotlinx.collections.immutable/PersistentSet<#A>).kotlinx.collections.immutable/mutate(kotlin/Function1<kotlin.collections/MutableSet<#A>, kotlin/Unit>): kotlinx.collections.immutable/PersistentSet<#A> // kotlinx.collections.immutable/mutate|[email protected]<0:0>(kotlin.Function1<kotlin.collections.MutableSet<0:0>,kotlin.Unit>){0§<kotlin.Any?>}[0]
252254
final inline fun <#A: kotlin/Any?> (kotlinx.collections.immutable/PersistentSet<#A>).kotlinx.collections.immutable/plus(#A): kotlinx.collections.immutable/PersistentSet<#A> // kotlinx.collections.immutable/plus|[email protected]<0:0>(0:0){0§<kotlin.Any?>}[0]
255+
final inline fun <#A: kotlin/Any?> kotlinx.collections.immutable/buildPersistentHashSet(kotlin/Function1<kotlin.collections/MutableSet<#A>, kotlin/Unit>): kotlinx.collections.immutable/PersistentSet<#A> // kotlinx.collections.immutable/buildPersistentHashSet|buildPersistentHashSet(kotlin.Function1<kotlin.collections.MutableSet<0:0>,kotlin.Unit>){0§<kotlin.Any?>}[0]
256+
final inline fun <#A: kotlin/Any?> kotlinx.collections.immutable/buildPersistentList(kotlin/Function1<kotlin.collections/MutableList<#A>, kotlin/Unit>): kotlinx.collections.immutable/PersistentList<#A> // kotlinx.collections.immutable/buildPersistentList|buildPersistentList(kotlin.Function1<kotlin.collections.MutableList<0:0>,kotlin.Unit>){0§<kotlin.Any?>}[0]
257+
final inline fun <#A: kotlin/Any?> kotlinx.collections.immutable/buildPersistentSet(kotlin/Function1<kotlin.collections/MutableSet<#A>, kotlin/Unit>): kotlinx.collections.immutable/PersistentSet<#A> // kotlinx.collections.immutable/buildPersistentSet|buildPersistentSet(kotlin.Function1<kotlin.collections.MutableSet<0:0>,kotlin.Unit>){0§<kotlin.Any?>}[0]

core/commonMain/src/extensions.kt

Lines changed: 70 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,8 @@ import kotlinx.collections.immutable.implementations.persistentOrderedMap.Persis
1616
import kotlinx.collections.immutable.implementations.persistentOrderedMap.PersistentOrderedMapBuilder
1717
import kotlinx.collections.immutable.implementations.persistentOrderedSet.PersistentOrderedSet
1818
import kotlinx.collections.immutable.implementations.persistentOrderedSet.PersistentOrderedSetBuilder
19+
import kotlin.contracts.ExperimentalContracts
20+
import kotlin.experimental.ExperimentalTypeInference
1921

2022
//@Suppress("INVISIBLE_MEMBER", "INVISIBLE_REFERENCE")
2123
//inline fun <T> @kotlin.internal.Exact ImmutableCollection<T>.mutate(mutator: (MutableCollection<T>) -> Unit): ImmutableCollection<T> = builder().apply(mutator).build()
@@ -766,3 +768,71 @@ public fun <K, V> Map<K, V>.toPersistentHashMap(): PersistentMap<K, V>
766768
= this as? PersistentHashMap
767769
?: (this as? PersistentHashMapBuilder<K, V>)?.build()
768770
?: PersistentHashMap.emptyOf<K, V>().putAll(this)
771+
772+
/**
773+
* Builds a new [PersistentList] by populating a [MutableList] using the given [builderAction]
774+
* and returning an immutable list with the same elements.
775+
*
776+
* The list passed as a receiver to the [builderAction] is valid only inside that function.
777+
* Using it outside the function produces an unspecified behavior.
778+
*/
779+
@OptIn(ExperimentalTypeInference::class, ExperimentalContracts::class)
780+
public inline fun <T> buildPersistentList(builderAction: MutableList<T>.() -> Unit): PersistentList<T> {
781+
return persistentListOf<T>().mutate(builderAction)
782+
}
783+
784+
/**
785+
* Builds a new [PersistentSet] by populating a [MutableSet] using the given [builderAction]
786+
* and returning an immutable set with the same elements.
787+
*
788+
* The set passed as a receiver to the [builderAction] is valid only inside that function.
789+
* Using it outside the function produces an unspecified behavior.
790+
*
791+
* Elements of the set are iterated in the order they were added by the [builderAction].
792+
*/
793+
@OptIn(ExperimentalTypeInference::class, ExperimentalContracts::class)
794+
public inline fun <T> buildPersistentSet(builderAction: MutableSet<T>.() -> Unit): PersistentSet<T> {
795+
return persistentSetOf<T>().mutate(builderAction)
796+
}
797+
798+
/**
799+
* Builds a new [PersistentSet] by populating a [MutableSet] using the given [builderAction]
800+
* and returning an immutable set with the same elements.
801+
*
802+
* The set passed as a receiver to the [builderAction] is valid only inside that function.
803+
* Using it outside the function produces an unspecified behavior.
804+
*
805+
* Order of the elements in the returned set is unspecified.
806+
*/
807+
@OptIn(ExperimentalTypeInference::class, ExperimentalContracts::class)
808+
public inline fun <T> buildPersistentHashSet(builderAction: MutableSet<T>.() -> Unit): PersistentSet<T> {
809+
return persistentHashSetOf<T>().mutate(builderAction)
810+
}
811+
812+
/**
813+
* Builds a new [PersistentMap] by populating a [MutableMap] using the given [builderAction]
814+
* and returning an immutable map with the same key-value pairs.
815+
*
816+
* The map passed as a receiver to the [builderAction] is valid only inside that function.
817+
* Using it outside the function produces an unspecified behavior.
818+
*
819+
* Entries of the map are iterated in the order they were added by the [builderAction].
820+
*/
821+
@OptIn(ExperimentalTypeInference::class, ExperimentalContracts::class)
822+
public inline fun <K, V> buildPersistentMap(builderAction: MutableMap<K, V>.() -> Unit): PersistentMap<K, V> {
823+
return persistentMapOf<K, V>().mutate(builderAction)
824+
}
825+
826+
/**
827+
* Builds a new [PersistentMap] by populating a [MutableMap] using the given [builderAction]
828+
* and returning an immutable map with the same key-value pairs.
829+
*
830+
* The map passed as a receiver to the [builderAction] is valid only inside that function.
831+
* Using it outside the function produces an unspecified behavior.
832+
*
833+
* Order of the entries in the returned map is unspecified.
834+
*/
835+
@OptIn(ExperimentalTypeInference::class, ExperimentalContracts::class)
836+
public inline fun <K, V> buildPersistentHashMap(builderAction: MutableMap<K, V>.() -> Unit): PersistentMap<K, V> {
837+
return persistentHashMapOf<K, V>().mutate(builderAction)
838+
}

core/commonTest/src/contract/list/ImmutableListTest.kt

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -159,6 +159,16 @@ class ImmutableListTest {
159159
}
160160
}
161161

162+
@Test fun buildPersistentList() {
163+
val expected = persistentListOf(1, 2, 3, 4, 5, 6)
164+
val actual = buildPersistentList {
165+
for (i in 1..6) {
166+
add(i)
167+
}
168+
}
169+
assertEquals(expected, actual)
170+
}
171+
162172
@Test fun subListOfBuilder() {
163173
val list = "abcxaxyz12".toImmutableList().toPersistentList()
164174
val builder = list.builder()

core/commonTest/src/contract/map/ImmutableMapTest.kt

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -78,6 +78,21 @@ class ImmutableHashMapTest : ImmutableMapTest() {
7878
compareMaps(expected, builder1.build())
7979
}
8080

81+
@Test fun buildPersistentHashMap() {
82+
val expected = mutableMapOf<String, Int>()
83+
val actual = buildPersistentHashMap {
84+
for (i in 300..400) {
85+
put("$i", i)
86+
expected["$i"] = i
87+
}
88+
for (i in 0..200) {
89+
put("$i", i)
90+
expected["$i"] = i
91+
}
92+
}
93+
compareMapsUnordered(expected, actual)
94+
}
95+
8196
@Test fun regressionGithubIssue109() {
8297
// https://github.com/Kotlin/kotlinx.collections.immutable/issues/109
8398
val map0 = immutableMapOf<Int, Int>().put(0, 0).put(1, 1).put(32, 32)
@@ -139,6 +154,21 @@ class ImmutableOrderedMapTest : ImmutableMapTest() {
139154
changing.add("break iteration")
140155
assertFailsWith<ConcurrentModificationException> { builder.filter { it.key === changing } }
141156
}
157+
158+
@Test fun buildPersistentMap() {
159+
val expected = mutableMapOf<String, Int>()
160+
val actual = buildPersistentMap {
161+
for (i in 300..400) {
162+
put("$i", i)
163+
expected["$i"] = i
164+
}
165+
for (i in 0..200) {
166+
put("$i", i)
167+
expected["$i"] = i
168+
}
169+
}
170+
compareMaps(expected, actual)
171+
}
142172
}
143173

144174
abstract class ImmutableMapTest {

core/commonTest/src/contract/set/ImmutableSetTest.kt

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -195,6 +195,17 @@ class ImmutableHashSetTest : ImmutableSetTestBase() {
195195
compareSets(immutableSetOf<Int>(), left - right)
196196
}
197197
}
198+
199+
@Test fun buildPersistentHashSet() {
200+
val expected = mutableSetOf<Int>()
201+
val actual = buildPersistentHashSet {
202+
for (i in 0..2000) {
203+
add(i)
204+
expected.add(i)
205+
}
206+
}
207+
compareSetsUnordered(expected, actual)
208+
}
198209
}
199210

200211
class ImmutableOrderedSetTest : ImmutableSetTestBase() {
@@ -221,6 +232,17 @@ class ImmutableOrderedSetTest : ImmutableSetTestBase() {
221232
changing.add("break iteration")
222233
assertFailsWith<ConcurrentModificationException> { builder.filter { it === changing } }
223234
}
235+
236+
@Test fun buildPersistentSet() {
237+
val expected = mutableSetOf<Int>()
238+
val actual = buildPersistentSet {
239+
for (i in 0..2000) {
240+
add(i)
241+
expected.add(i)
242+
}
243+
}
244+
compareSets(expected, actual)
245+
}
224246
}
225247

226248
abstract class ImmutableSetTestBase {

0 commit comments

Comments
 (0)