Skip to content

Commit 16df77e

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

File tree

6 files changed

+135
-0
lines changed

6 files changed

+135
-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: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -766,3 +766,66 @@ public fun <K, V> Map<K, V>.toPersistentHashMap(): PersistentMap<K, V>
766766
= this as? PersistentHashMap
767767
?: (this as? PersistentHashMapBuilder<K, V>)?.build()
768768
?: PersistentHashMap.emptyOf<K, V>().putAll(this)
769+
770+
/**
771+
* Builds a new [PersistentList] by populating a [MutableList] using the given [builderAction]
772+
* and returning an immutable list with the same elements.
773+
*
774+
* The list passed as a receiver to the [builderAction] is valid only inside that function.
775+
* Using it outside the function produces an unspecified behavior.
776+
*/
777+
public inline fun <T> buildPersistentList(builderAction: MutableList<T>.() -> Unit): PersistentList<T> {
778+
return persistentListOf<T>().mutate(builderAction)
779+
}
780+
781+
/**
782+
* Builds a new [PersistentSet] by populating a [MutableSet] using the given [builderAction]
783+
* and returning an immutable set with the same elements.
784+
*
785+
* The set passed as a receiver to the [builderAction] is valid only inside that function.
786+
* Using it outside the function produces an unspecified behavior.
787+
*
788+
* Elements of the set are iterated in the order they were added by the [builderAction].
789+
*/
790+
public inline fun <T> buildPersistentSet(builderAction: MutableSet<T>.() -> Unit): PersistentSet<T> {
791+
return persistentSetOf<T>().mutate(builderAction)
792+
}
793+
794+
/**
795+
* Builds a new [PersistentSet] by populating a [MutableSet] using the given [builderAction]
796+
* and returning an immutable set with the same elements.
797+
*
798+
* The set passed as a receiver to the [builderAction] is valid only inside that function.
799+
* Using it outside the function produces an unspecified behavior.
800+
*
801+
* Order of the elements in the returned set is unspecified.
802+
*/
803+
public inline fun <T> buildPersistentHashSet(builderAction: MutableSet<T>.() -> Unit): PersistentSet<T> {
804+
return persistentHashSetOf<T>().mutate(builderAction)
805+
}
806+
807+
/**
808+
* Builds a new [PersistentMap] by populating a [MutableMap] using the given [builderAction]
809+
* and returning an immutable map with the same key-value pairs.
810+
*
811+
* The map passed as a receiver to the [builderAction] is valid only inside that function.
812+
* Using it outside the function produces an unspecified behavior.
813+
*
814+
* Entries of the map are iterated in the order they were added by the [builderAction].
815+
*/
816+
public inline fun <K, V> buildPersistentMap(builderAction: MutableMap<K, V>.() -> Unit): PersistentMap<K, V> {
817+
return persistentMapOf<K, V>().mutate(builderAction)
818+
}
819+
820+
/**
821+
* Builds a new [PersistentMap] by populating a [MutableMap] using the given [builderAction]
822+
* and returning an immutable map with the same key-value pairs.
823+
*
824+
* The map passed as a receiver to the [builderAction] is valid only inside that function.
825+
* Using it outside the function produces an unspecified behavior.
826+
*
827+
* Order of the entries in the returned map is unspecified.
828+
*/
829+
public inline fun <K, V> buildPersistentHashMap(builderAction: MutableMap<K, V>.() -> Unit): PersistentMap<K, V> {
830+
return persistentHashMapOf<K, V>().mutate(builderAction)
831+
}

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)