Skip to content

Commit 30c1ba3

Browse files
author
Abduqodiri Qurbonzoda
committed
Change toPersistentMap/Set to return ordered persistent set/map
1 parent bce06da commit 30c1ba3

File tree

3 files changed

+28
-11
lines changed

3 files changed

+28
-11
lines changed

core/commonMain/src/extensions.kt

+9-7
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,9 @@ import kotlinx.collections.immutable.implementations.immutableMap.PersistentHash
1313
import kotlinx.collections.immutable.implementations.immutableSet.PersistentHashSet
1414
import kotlinx.collections.immutable.implementations.immutableSet.PersistentHashSetBuilder
1515
import kotlinx.collections.immutable.implementations.persistentOrderedMap.PersistentOrderedMap
16+
import kotlinx.collections.immutable.implementations.persistentOrderedMap.PersistentOrderedMapBuilder
1617
import kotlinx.collections.immutable.implementations.persistentOrderedSet.PersistentOrderedSet
18+
import kotlinx.collections.immutable.implementations.persistentOrderedSet.PersistentOrderedSetBuilder
1719

1820
//@Suppress("INVISIBLE_MEMBER", "INVISIBLE_REFERENCE")
1921
//inline fun <T> @kotlin.internal.Exact ImmutableCollection<T>.mutate(mutator: (MutableCollection<T>) -> Unit): ImmutableCollection<T> = builder().apply(mutator).build()
@@ -593,9 +595,9 @@ fun <T> Iterable<T>.toImmutableSet(): ImmutableSet<T> =
593595
* Elements of the returned set are iterated in the same order as in this collection
594596
*/
595597
fun <T> Iterable<T>.toPersistentSet(): PersistentSet<T> =
596-
this as? PersistentSet<T>
597-
?: (this as? PersistentSet.Builder)?.build()
598-
?: persistentSetOf<T>() + this
598+
this as? PersistentOrderedSet<T>
599+
?: (this as? PersistentOrderedSetBuilder)?.build()
600+
?: PersistentOrderedSet.emptyOf<T>() + this
599601

600602
/**
601603
* Returns a persistent set containing all elements from this set.
@@ -621,7 +623,7 @@ fun <T> Set<T>.toPersistentHashSet(): PersistentSet<T>
621623
fun <K, V> Map<K, V>.toImmutableMap(): ImmutableMap<K, V>
622624
= this as? ImmutableMap
623625
?: (this as? PersistentMap.Builder)?.build()
624-
?: PersistentOrderedMap.emptyOf<K, V>().putAll(this)
626+
?: persistentMapOf<K, V>().putAll(this)
625627

626628
/**
627629
* Returns a persistent map containing all entries from this map.
@@ -632,8 +634,8 @@ fun <K, V> Map<K, V>.toImmutableMap(): ImmutableMap<K, V>
632634
* Entries of the returned map are iterated in the same order as in this map.
633635
*/
634636
fun <K, V> Map<K, V>.toPersistentMap(): PersistentMap<K, V>
635-
= this as? PersistentMap<K, V>
636-
?: (this as? PersistentMap.Builder<K, V>)?.build()
637+
= this as? PersistentOrderedMap<K, V>
638+
?: (this as? PersistentOrderedMapBuilder<K, V>)?.build()
637639
?: PersistentOrderedMap.emptyOf<K, V>().putAll(this)
638640

639641
/**
@@ -645,6 +647,6 @@ fun <K, V> Map<K, V>.toPersistentMap(): PersistentMap<K, V>
645647
* Order of the entries in the returned map is unspecified.
646648
*/
647649
fun <K, V> Map<K, V>.toPersistentHashMap(): PersistentMap<K, V>
648-
= this as? PersistentMap
650+
= this as? PersistentHashMap
649651
?: (this as? PersistentHashMapBuilder<K, V>)?.build()
650652
?: PersistentHashMap.emptyOf<K, V>().putAll(this)

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

+9-1
Original file line numberDiff line numberDiff line change
@@ -14,10 +14,16 @@ import kotlin.test.*
1414

1515
class ImmutableHashMapTest : ImmutableMapTest() {
1616
override fun <K, V> immutableMapOf(vararg pairs: Pair<K, V>): PersistentMap<K, V> = persistentHashMapOf(*pairs)
17+
override fun <K, V> testBuilderToPersistentMap(builder: PersistentMap.Builder<K, V>) {
18+
assertNotSame(builder.build(), builder.toPersistentMap(), "toPersistent shouldn't call build()")
19+
}
1720
}
1821
class ImmutableOrderedMapTest : ImmutableMapTest() {
1922
override fun <K, V> immutableMapOf(vararg pairs: Pair<K, V>): PersistentMap<K, V> = persistentMapOf(*pairs)
2023
override fun <K, V> compareMaps(expected: Map<K, V>, actual: Map<K, V>) = compare(expected, actual) { mapBehavior(ordered = true) }
24+
override fun <K, V> testBuilderToPersistentMap(builder: PersistentMap.Builder<K, V>) {
25+
assertSame(builder.build(), builder.toPersistentMap(), "toPersistent should call build()")
26+
}
2127

2228
@Test fun iterationOrder() {
2329
var map = immutableMapOf("x" to null, "y" to 1).toPersistentMap()
@@ -35,8 +41,8 @@ class ImmutableOrderedMapTest : ImmutableMapTest() {
3541
}
3642

3743
abstract class ImmutableMapTest {
38-
3944
abstract fun <K, V> immutableMapOf(vararg pairs: Pair<K, V>): PersistentMap<K, V>
45+
abstract fun <K, V> testBuilderToPersistentMap(builder: PersistentMap.Builder<K, V>)
4046

4147
open fun <K, V> compareMaps(expected: Map<K, V>, actual: Map<K, V>) = compareMapsUnordered(expected, actual)
4248
fun <K, V> compareMapsUnordered(expected: Map<K, V>, actual: Map<K, V>) = compare(expected, actual) { mapBehavior(ordered = false) }
@@ -154,6 +160,8 @@ abstract class ImmutableMapTest {
154160
val map2 = builder.toImmutableMap()
155161
assertSame(map2, map, "toImmutable calls build()")
156162

163+
testBuilderToPersistentMap(builder)
164+
157165
with(map) {
158166
testMutation { put('K', null) }
159167
testMutation { putAll("kotlin".associate { it to 0 }) }

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

+10-3
Original file line numberDiff line numberDiff line change
@@ -14,15 +14,22 @@ import kotlin.test.*
1414

1515
class ImmutableHashSetTest : ImmutableSetTestBase() {
1616
override fun <T> immutableSetOf(vararg elements: T) = persistentHashSetOf(*elements)
17+
override fun <T> testBuilderToPersistentSet(builder: PersistentSet.Builder<T>) {
18+
assertNotSame(builder.build(), builder.toPersistentSet(), "toPersistent shouldn't call build()")
19+
}
1720
}
1821
class ImmutableOrderedSetTest : ImmutableSetTestBase() {
1922
override fun <T> immutableSetOf(vararg elements: T) = persistentSetOf(*elements)
2023
override fun <T> compareSets(expected: Set<T>, actual: Set<T>) = compare(expected, actual) { setBehavior(ordered = true) }
24+
override fun <T> testBuilderToPersistentSet(builder: PersistentSet.Builder<T>) {
25+
assertSame(builder.build(), builder.toPersistentSet(), "toPersistent should call build()")
26+
}
2127
}
2228

2329
abstract class ImmutableSetTestBase {
24-
2530
abstract fun <T> immutableSetOf(vararg elements: T): PersistentSet<T>
31+
abstract fun <T> testBuilderToPersistentSet(builder: PersistentSet.Builder<T>)
32+
2633
fun <T> immutableSetOf(elements: Collection<T>) = immutableSetOf<T>() + elements
2734

2835
open fun <T> compareSets(expected: Set<T>, actual: Set<T>) = compareSetsUnordered(expected, actual)
@@ -103,8 +110,8 @@ abstract class ImmutableSetTestBase {
103110

104111
val set2 = builder.toImmutableSet()
105112
assertTrue(set2 === set, "toImmutable calls build()")
106-
val set3 = builder.toPersistentSet()
107-
assertTrue(set3 === set, "toPersistent calls build()")
113+
114+
testBuilderToPersistentSet(builder)
108115

109116
with(set) {
110117
testMutation { add('K') }

0 commit comments

Comments
 (0)