Skip to content

Commit 68b28bf

Browse files
author
Abduqodiri Qurbonzoda
committed
Make new implementations default persistent collections implementations
1 parent 92abb70 commit 68b28bf

File tree

10 files changed

+61
-56
lines changed

10 files changed

+61
-56
lines changed

kotlinx-collections-immutable/src/main/kotlin/kotlinx/collections/immutable/extensions.kt

Lines changed: 16 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,12 @@
1818

1919
package kotlinx.collections.immutable
2020

21+
import kotlinx.collections.immutable.implementations.immutableList.persistentVectorOf
22+
import kotlinx.collections.immutable.implementations.immutableMap.PersistentHashMap
23+
import kotlinx.collections.immutable.implementations.immutableMap.PersistentHashMapBuilder
24+
import kotlinx.collections.immutable.implementations.immutableSet.PersistentHashSet
25+
import kotlinx.collections.immutable.implementations.immutableSet.PersistentHashSetBuilder
26+
2127
//@Suppress("INVISIBLE_MEMBER", "INVISIBLE_REFERENCE")
2228
//inline fun <T> @kotlin.internal.Exact ImmutableCollection<T>.mutate(mutator: (MutableCollection<T>) -> Unit): ImmutableCollection<T> = builder().apply(mutator).build()
2329
// it or this?
@@ -120,16 +126,16 @@ public operator fun <K, V> PersistentMap<out K, V>.minus(keys: Sequence<K>): Per
120126
= mutate { it.minusAssign(keys) }
121127

122128

123-
fun <E> persistentListOf(vararg elements: E): PersistentList<E> = ImmutableVectorList.emptyOf<E>().addAll(elements.asList())
124-
fun <E> persistentListOf(): PersistentList<E> = ImmutableVectorList.emptyOf<E>()
129+
fun <E> persistentListOf(vararg elements: E): PersistentList<E> = persistentVectorOf<E>().addAll(elements.asList())
130+
fun <E> persistentListOf(): PersistentList<E> = persistentVectorOf()
125131

126132
fun <E> persistentSetOf(vararg elements: E): PersistentSet<E> = ImmutableOrderedSet.emptyOf<E>().addAll(elements.asList())
127133
fun <E> persistentSetOf(): PersistentSet<E> = ImmutableOrderedSet.emptyOf<E>()
128134

129-
fun <E> persistentHashSetOf(vararg elements: E): PersistentSet<E> = ImmutableHashSet.emptyOf<E>().addAll(elements.asList())
135+
fun <E> persistentHashSetOf(vararg elements: E): PersistentSet<E> = PersistentHashSet.emptyOf<E>().addAll(elements.asList())
130136

131137
fun <K, V> persistentMapOf(vararg pairs: Pair<K, V>): PersistentMap<K, V> = ImmutableOrderedMap.emptyOf<K,V>().mutate { it += pairs }
132-
fun <K, V> persistentHashMapOf(vararg pairs: Pair<K, V>): PersistentMap<K, V> = ImmutableHashMap.emptyOf<K,V>().mutate { it += pairs }
138+
fun <K, V> persistentHashMapOf(vararg pairs: Pair<K, V>): PersistentMap<K, V> = PersistentHashMap.emptyOf<K,V>().mutate { it += pairs }
133139

134140
@Deprecated("Use persistentListOf instead.", ReplaceWith("persistentListOf(*elements)"))
135141
fun <E> immutableListOf(vararg elements: E): PersistentList<E> = persistentListOf(*elements)
@@ -153,13 +159,12 @@ fun <K, V> immutableHashMapOf(vararg pairs: Pair<K, V>): PersistentMap<K, V> = p
153159

154160
fun <T> Iterable<T>.toImmutableList(): ImmutableList<T> =
155161
this as? ImmutableList
156-
?: (this as? PersistentList.Builder)?.build()
157-
?: persistentListOf<T>() + this
162+
?: this.toPersistentList()
158163

159164
fun <T> Iterable<T>.toPersistentList(): PersistentList<T> =
160165
this as? PersistentList
161166
?: (this as? PersistentList.Builder)?.build()
162-
?: ImmutableVectorList.emptyOf<T>() + this
167+
?: persistentListOf<T>() + this
163168

164169

165170
// fun <T> Array<T>.toImmutableList(): ImmutableList<T> = immutableListOf<T>() + this.asList()
@@ -185,8 +190,8 @@ fun <T> Iterable<T>.toPersistentSet(): PersistentSet<T> =
185190

186191
fun <T> Set<T>.toPersistentHashSet(): PersistentSet<T>
187192
= this as? ImmutableHashSet
188-
?: (this as? ImmutableHashSet.Builder)?.build()
189-
?: ImmutableHashSet.emptyOf<T>() + this
193+
?: (this as? PersistentHashSetBuilder<T>)?.build()
194+
?: PersistentHashSet.emptyOf<T>() + this
190195

191196

192197
fun <K, V> Map<K, V>.toImmutableMap(): ImmutableMap<K, V>
@@ -202,5 +207,5 @@ fun <K, V> Map<K, V>.toPersistentMap(): PersistentMap<K, V>
202207

203208
fun <K, V> Map<K, V>.toPersistentHashMap(): PersistentMap<K, V>
204209
= this as? PersistentMap
205-
?: (this as? ImmutableHashMap.Builder)?.build()
206-
?: ImmutableHashMap.emptyOf<K, V>().putAll(this)
210+
?: (this as? PersistentHashMapBuilder<K, V>)?.build()
211+
?: PersistentHashMap.emptyOf<K, V>().putAll(this)

kotlinx-collections-immutable/src/main/kotlin/kotlinx/collections/immutable/implementations/immutableList/Utils.kt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,6 @@ internal const val MAX_BUFFER_SIZE_PlUS_ONE = MAX_BUFFER_SIZE + 1
2525

2626
internal class ObjectWrapper(var value: Any?)
2727

28-
fun <E> persistentVectorOf(): PersistentList<E> {
28+
internal fun <E> persistentVectorOf(): PersistentList<E> {
2929
return SmallPersistentVector.EMPTY
3030
}

kotlinx-collections-immutable/src/main/kotlin/kotlinx/collections/immutable/implementations/immutableMap/PersistentHashMap.kt

Lines changed: 5 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -75,15 +75,15 @@ internal class PersistentHashMap<K, V>(val node: TrieNode<K, V>,
7575
val keyHash = key?.hashCode() ?: NULL_HASH_CODE
7676
val newNode = node.remove(keyHash, key, 0)
7777
if (node === newNode) { return this }
78-
if (newNode == null) { return persistentHashMapOf() }
78+
if (newNode == null) { return PersistentHashMap.emptyOf() }
7979
return PersistentHashMap(newNode, size - 1)
8080
}
8181

8282
override fun remove(key: K, value: @UnsafeVariance V): PersistentMap<K, V> {
8383
val keyHash = key?.hashCode() ?: NULL_HASH_CODE
8484
val newNode = node.remove(keyHash, key, value, 0)
8585
if (node === newNode) { return this }
86-
if (newNode == null) { return persistentHashMapOf() }
86+
if (newNode == null) { return PersistentHashMap.emptyOf() }
8787
return PersistentHashMap(newNode, size - 1)
8888
}
8989

@@ -92,18 +92,15 @@ internal class PersistentHashMap<K, V>(val node: TrieNode<K, V>,
9292
}
9393

9494
override fun clear(): PersistentMap<K, V> {
95-
return persistentHashMapOf()
95+
return PersistentHashMap.emptyOf()
9696
}
9797

9898
override fun builder(): PersistentMap.Builder<K, @UnsafeVariance V> {
9999
return PersistentHashMapBuilder(node, size)
100100
}
101101

102102
internal companion object {
103-
internal val EMPTY = PersistentHashMap(TrieNode.EMPTY, 0)
103+
private val EMPTY = PersistentHashMap(TrieNode.EMPTY, 0)
104+
internal fun <K, V> emptyOf(): PersistentMap<K, V> = EMPTY as PersistentMap<K, V>
104105
}
105-
}
106-
107-
fun <K, V> persistentHashMapOf(): PersistentMap<K, V> {
108-
return PersistentHashMap.EMPTY as PersistentHashMap<K, V>
109106
}

kotlinx-collections-immutable/src/main/kotlin/kotlinx/collections/immutable/implementations/immutableSet/PersistentHashSet.kt

Lines changed: 4 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@ internal class PersistentHashSet<E>(private val node: TrieNode<E>,
4141
val hashCode = element?.hashCode() ?: NULL_HASH_CODE
4242
val newNode = node.remove(hashCode, element, 0)
4343
if (node === newNode) { return this }
44-
if (newNode == null) { return persistentHashSetOf() }
44+
if (newNode == null) { return PersistentHashSet.emptyOf() }
4545
return PersistentHashSet(newNode, size - 1)
4646
}
4747

@@ -54,7 +54,7 @@ internal class PersistentHashSet<E>(private val node: TrieNode<E>,
5454
}
5555

5656
override fun clear(): PersistentSet<E> {
57-
return persistentHashSetOf()
57+
return PersistentHashSet.emptyOf()
5858
}
5959

6060
override fun iterator(): Iterator<E> {
@@ -66,10 +66,7 @@ internal class PersistentHashSet<E>(private val node: TrieNode<E>,
6666
}
6767

6868
internal companion object {
69-
internal val EMPTY = PersistentHashSet(TrieNode.EMPTY, 0)
69+
private val EMPTY = PersistentHashSet(TrieNode.EMPTY, 0)
70+
internal fun <E> emptyOf(): PersistentSet<E> = PersistentHashSet.EMPTY
7071
}
7172
}
72-
73-
fun <E> persistentHashSetOf(): PersistentSet<E> {
74-
return PersistentHashSet.EMPTY
75-
}
Lines changed: 15 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -16,16 +16,17 @@
1616

1717
package kotlinx.collections.immutable.implementations.immutableList
1818

19+
import kotlinx.collections.immutable.persistentListOf
1920
import org.junit.Test
2021
import org.junit.Assert.*
2122
import java.util.*
2223
import kotlin.test.assertFailsWith
2324

24-
class PersistentVectorBuilderTest {
25+
class PersistentListBuilderTest {
2526

2627
@Test
2728
fun isEmptyTests() {
28-
val builder = persistentVectorOf<String>().builder()
29+
val builder = persistentListOf<String>().builder()
2930

3031
assertTrue(builder.isEmpty())
3132

@@ -44,7 +45,7 @@ class PersistentVectorBuilderTest {
4445

4546
@Test
4647
fun sizeTests() {
47-
val builder = persistentVectorOf<Int>().builder()
48+
val builder = persistentListOf<Int>().builder()
4849

4950
assertTrue(builder.size == 0)
5051

@@ -62,7 +63,7 @@ class PersistentVectorBuilderTest {
6263

6364
@Test
6465
fun firstTests() {
65-
val builder = persistentVectorOf<Int>().builder()
66+
val builder = persistentListOf<Int>().builder()
6667

6768
assertNull(builder.firstOrNull())
6869

@@ -80,7 +81,7 @@ class PersistentVectorBuilderTest {
8081

8182
@Test
8283
fun lastTests() {
83-
val builder = persistentVectorOf<Int>().builder()
84+
val builder = persistentListOf<Int>().builder()
8485

8586
assertNull(builder.lastOrNull())
8687

@@ -98,7 +99,7 @@ class PersistentVectorBuilderTest {
9899

99100
@Test
100101
fun toListTest() {
101-
val builder = persistentVectorOf<Int>().builder()
102+
val builder = persistentListOf<Int>().builder()
102103

103104
assertEquals(emptyList<Int>(), builder)
104105

@@ -114,7 +115,7 @@ class PersistentVectorBuilderTest {
114115

115116
@Test
116117
fun addFirstTests() {
117-
val builder = persistentVectorOf<Int>().builder()
118+
val builder = persistentListOf<Int>().builder()
118119

119120
assertNull(builder.firstOrNull())
120121

@@ -131,7 +132,7 @@ class PersistentVectorBuilderTest {
131132

132133
@Test
133134
fun addLastTests() {
134-
val builder = persistentVectorOf<Int>().builder()
135+
val builder = persistentListOf<Int>().builder()
135136

136137
val elementsToAdd = 10000
137138
repeat(times = elementsToAdd) { index ->
@@ -147,7 +148,7 @@ class PersistentVectorBuilderTest {
147148

148149
@Test
149150
fun removeFirstTests() {
150-
val builder = persistentVectorOf<Int>().builder()
151+
val builder = persistentListOf<Int>().builder()
151152

152153
assertFailsWith<IndexOutOfBoundsException> { builder.removeAt(0) }
153154

@@ -166,7 +167,7 @@ class PersistentVectorBuilderTest {
166167

167168
@Test
168169
fun removeLastTests() {
169-
val builder = persistentVectorOf<Int>().builder()
170+
val builder = persistentListOf<Int>().builder()
170171

171172
assertFailsWith<IndexOutOfBoundsException> {
172173
builder.removeAt(builder.size - 1)
@@ -200,7 +201,7 @@ class PersistentVectorBuilderTest {
200201

201202
@Test
202203
fun getTests() {
203-
val builder = persistentVectorOf<Int>().builder()
204+
val builder = persistentListOf<Int>().builder()
204205

205206
assertFailsWith<IndexOutOfBoundsException> {
206207
builder[0]
@@ -225,7 +226,7 @@ class PersistentVectorBuilderTest {
225226

226227
@Test
227228
fun setTests() {
228-
val builder = persistentVectorOf<Int>().builder()
229+
val builder = persistentListOf<Int>().builder()
229230

230231
assertFailsWith<IndexOutOfBoundsException> {
231232
builder[0] = 0
@@ -256,7 +257,7 @@ class PersistentVectorBuilderTest {
256257

257258
@Test
258259
fun subListTests() {
259-
val builder = persistentVectorOf<Int>().builder()
260+
val builder = persistentListOf<Int>().builder()
260261

261262
val elementsToAdd = 10000
262263
repeat(times = elementsToAdd) { index ->
@@ -287,7 +288,7 @@ class PersistentVectorBuilderTest {
287288

288289
@Test
289290
fun randomOperationsTests() {
290-
val vectorGen = mutableListOf(List(20) { persistentVectorOf<Int>() })
291+
val vectorGen = mutableListOf(List(20) { persistentListOf<Int>() })
291292
val actual = mutableListOf(List(20) { listOf<Int>() })
292293

293294
repeat(times = 10) {

0 commit comments

Comments
 (0)