Skip to content

Commit c7aceab

Browse files
committed
Merge branch 'develop': new persistent collection implementations and interfaces
2 parents 48c7c62 + b42fd6b commit c7aceab

File tree

72 files changed

+8189
-409
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

72 files changed

+8189
-409
lines changed

kotlinx-collections-immutable/build.gradle

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
buildscript {
2-
ext.kotlin_version = "1.1.0"
2+
ext.kotlin_version = "1.3.11"
33

44
repositories {
55
jcenter()
@@ -44,6 +44,7 @@ dependencies {
4444
project('tests') {
4545
dependencies {
4646
compile project(path: ':kotlinx-collections-immutable', configuration: 'shadow')
47+
testCompile 'com.google.guava:guava-testlib:18.0'
4748
}
4849
}
4950

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

Lines changed: 21 additions & 54 deletions
Original file line numberDiff line numberDiff line change
@@ -16,10 +16,12 @@
1616

1717
package kotlinx.collections.immutable
1818

19+
import kotlinx.collections.immutable.adapters.ImmutableCollectionAdapter
20+
import kotlinx.collections.immutable.adapters.ImmutableSetAdapter
1921
import org.pcollections.PMap
2022
import java.util.ConcurrentModificationException
2123

22-
internal abstract class AbstractImmutableMap<K, out V> protected constructor(protected val impl: PMap<K, @UnsafeVariance V>) : ImmutableMap<K, V> {
24+
internal abstract class AbstractImmutableMap<K, out V> protected constructor(protected val impl: PMap<K, @UnsafeVariance V>) : PersistentMap<K, V> {
2325

2426
abstract class AbstractImmutableEntry<out K, out V> : Map.Entry<K, V> {
2527
override fun equals(other: Any?): Boolean = other is Map.Entry<*,*> && other.key == key && other.value == value
@@ -40,31 +42,31 @@ internal abstract class AbstractImmutableMap<K, out V> protected constructor(pro
4042

4143

4244
// should it be immutable set/collection or just read-only?
43-
private var _keys: Set<K>? = null
44-
final override val keys: Set<K> get() = _keys ?: createKeys().apply { _keys = this }
45-
protected open fun createKeys(): Set<K> = impl.keys
46-
47-
private var _values: Collection<V>? = null
48-
final override val values: Collection<V> get() = _values ?: createValues().apply { _values = this }
49-
protected open fun createValues(): Collection<V> = impl.values
50-
51-
private var _entries: Set<Map.Entry<K, V>>? = null
52-
final override val entries: Set<Map.Entry<K, V>> get() = _entries ?: createEntries().apply { _entries = this }
53-
protected open fun createEntries(): Set<Map.Entry<K, V>> = impl.entries
54-
55-
override fun put(key: K, value: @UnsafeVariance V): ImmutableMap<K, V> = wrap(impl.plus(key, value))
56-
override fun putAll(m: Map<out K, @UnsafeVariance V>): ImmutableMap<K, V> = wrap(impl.plusAll(m))
57-
override fun remove(key: K): ImmutableMap<K, V> = wrap(impl.minus(key))
58-
override fun remove(key: K, value: @UnsafeVariance V): ImmutableMap<K, V>
45+
private var _keys: ImmutableSet<K>? = null
46+
final override val keys: ImmutableSet<K> get() = _keys ?: createKeys().apply { _keys = this }
47+
protected open fun createKeys(): ImmutableSet<K> = ImmutableSetAdapter(impl.keys)
48+
49+
private var _values: ImmutableCollection<V>? = null
50+
final override val values: ImmutableCollection<V> get() = _values ?: createValues().apply { _values = this }
51+
protected open fun createValues(): ImmutableCollection<V> = ImmutableCollectionAdapter(impl.values)
52+
53+
private var _entries: ImmutableSet<Map.Entry<K, V>>? = null
54+
final override val entries: ImmutableSet<Map.Entry<K, V>> get() = _entries ?: createEntries().apply { _entries = this }
55+
protected open fun createEntries(): ImmutableSet<Map.Entry<K, V>> = ImmutableSetAdapter(impl.entries)
56+
57+
override fun put(key: K, value: @UnsafeVariance V): PersistentMap<K, V> = wrap(impl.plus(key, value))
58+
override fun putAll(m: Map<out K, @UnsafeVariance V>): PersistentMap<K, V> = wrap(impl.plusAll(m))
59+
override fun remove(key: K): PersistentMap<K, V> = wrap(impl.minus(key))
60+
override fun remove(key: K, value: @UnsafeVariance V): PersistentMap<K, V>
5961
= if (!impl.contains(key, value)) this else wrap(impl.minus(key))
6062

6163
override abstract fun builder(): Builder<K, @UnsafeVariance V>
6264

6365
protected abstract fun wrap(impl: PMap<K, @UnsafeVariance V>): AbstractImmutableMap<K, V>
6466

6567

66-
abstract class Builder<K, V> protected constructor(protected var value: AbstractImmutableMap<K, V>, protected var impl: PMap<K, V>) : ImmutableMap.Builder<K, V>, AbstractMutableMap<K, V>() {
67-
override fun build(): ImmutableMap<K, V> = value.wrap(impl).apply { value = this }
68+
abstract class Builder<K, V> protected constructor(protected var value: AbstractImmutableMap<K, V>, protected var impl: PMap<K, V>) : PersistentMap.Builder<K, V>, AbstractMutableMap<K, V>() {
69+
override fun build(): AbstractImmutableMap<K, V> = value.wrap(impl).apply { value = this }
6870

6971
override val size: Int get() = impl.size
7072
override fun isEmpty(): Boolean = impl.isEmpty()
@@ -164,38 +166,3 @@ internal abstract class AbstractImmutableMap<K, out V> protected constructor(pro
164166
internal fun <K, V> PMap<K, V>.contains(key: K, value: @UnsafeVariance V): Boolean
165167
= this[key]?.let { candidate -> candidate == value } ?: (value == null && containsKey(key))
166168

167-
168-
/*
169-
private open class ImmutableCollectionWrapper<E>(protected val impl: Collection<E>) : ImmutableCollection<E> {
170-
override val size: Int get() = impl.size
171-
override fun isEmpty(): Boolean = impl.isEmpty()
172-
override fun contains(element: @UnsafeVariance E): Boolean = impl.contains(element)
173-
override fun containsAll(elements: Collection<@UnsafeVariance E>): Boolean = impl.containsAll(elements)
174-
override fun iterator(): Iterator<E> = impl.iterator()
175-
176-
override fun equals(other: Any?): Boolean = impl.equals(other)
177-
override fun hashCode(): Int = impl.hashCode()
178-
override fun toString(): String = impl.toString()
179-
180-
override fun builder(): ImmutableCollection.Builder<E> = ImmutableVectorList.emptyOf<E>().builder().apply { addAll(impl) }
181-
182-
override fun add(element: E): ImmutableCollection<E> = builder().apply { add(element) }.build()
183-
override fun addAll(elements: Collection<E>): ImmutableCollection<E> = builder().apply { addAll(elements) }.build()
184-
override fun remove(element: E): ImmutableCollection<E> = builder().apply { remove(element) }.build()
185-
override fun removeAll(elements: Collection<E>): ImmutableCollection<E> = builder().apply { removeAll(elements) }.build()
186-
override fun removeAll(predicate: (E) -> Boolean): ImmutableCollection<E> = builder().apply { removeAll(predicate) }.build()
187-
override fun clear(): ImmutableCollection<E> = immutableListOf()
188-
}
189-
190-
private class ImmutableSetWrapper<E>(impl: Set<E>) : ImmutableSet<E>, ImmutableCollectionWrapper<E>(impl) {
191-
192-
override fun builder(): ImmutableSet.Builder<E> = ImmutableOrderedSet.emptyOf<E>().builder().apply { addAll(impl) }
193-
194-
override fun add(element: E): ImmutableSet<E> = super.add(element) as ImmutableSet
195-
override fun addAll(elements: Collection<E>): ImmutableSet<E> = super.addAll(elements) as ImmutableSet
196-
override fun remove(element: E): ImmutableSet<E> = super.remove(element) as ImmutableSet
197-
override fun removeAll(elements: Collection<E>): ImmutableSet<E> = super.removeAll(elements) as ImmutableSet
198-
override fun removeAll(predicate: (E) -> Boolean): ImmutableSet<E> = super.removeAll(predicate) as ImmutableSet
199-
override fun clear(): ImmutableSet<E> = immutableSetOf()
200-
}
201-
*/

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

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ package kotlinx.collections.immutable
1919
import org.pcollections.PSet
2020
import java.util.ConcurrentModificationException
2121

22-
internal abstract class AbstractImmutableSet<out E> protected constructor(protected val impl: PSet<@UnsafeVariance E>) : ImmutableSet<E> {
22+
internal abstract class AbstractImmutableSet<out E> protected constructor(protected val impl: PSet<@UnsafeVariance E>) : PersistentSet<E> {
2323

2424
override val size: Int get() = impl.size
2525
override fun isEmpty(): Boolean = impl.isEmpty()
@@ -31,23 +31,23 @@ internal abstract class AbstractImmutableSet<out E> protected constructor(protec
3131
override fun hashCode(): Int = impl.hashCode()
3232
override fun toString(): String = impl.toString()
3333

34-
override fun add(element: @UnsafeVariance E): ImmutableSet<E> = wrap(impl.plus(element))
34+
override fun add(element: @UnsafeVariance E): PersistentSet<E> = wrap(impl.plus(element))
3535

36-
override fun addAll(elements: Collection<@UnsafeVariance E>): ImmutableSet<E> = wrap(impl.plusAll(elements))
36+
override fun addAll(elements: Collection<@UnsafeVariance E>): PersistentSet<E> = wrap(impl.plusAll(elements))
3737

38-
override fun remove(element: @UnsafeVariance E): ImmutableSet<E> = wrap(impl.minus(element))
38+
override fun remove(element: @UnsafeVariance E): PersistentSet<E> = wrap(impl.minus(element))
3939

40-
override fun removeAll(elements: Collection<@UnsafeVariance E>): ImmutableSet<E> = mutate { it.removeAll(elements) }
40+
override fun removeAll(elements: Collection<@UnsafeVariance E>): PersistentSet<E> = mutate { it.removeAll(elements) }
4141

42-
override fun removeAll(predicate: (E) -> Boolean): ImmutableSet<E> = mutate { it.removeAll(predicate) }
42+
override fun removeAll(predicate: (E) -> Boolean): PersistentSet<E> = mutate { it.removeAll(predicate) }
4343

4444
override abstract fun clear(): AbstractImmutableSet<E>
4545

4646
override abstract fun builder(): Builder<@UnsafeVariance E>
4747

4848
protected abstract fun wrap(impl: PSet<@UnsafeVariance E>): AbstractImmutableSet<E>
4949

50-
abstract class Builder<E> internal constructor(protected var value: AbstractImmutableSet<E>, protected var impl: PSet<E>) : AbstractMutableSet<E>(), ImmutableSet.Builder<E> {
50+
abstract class Builder<E> internal constructor(protected var value: AbstractImmutableSet<E>, protected var impl: PSet<E>) : AbstractMutableSet<E>(), PersistentSet.Builder<E> {
5151
override fun build(): AbstractImmutableSet<E> = value.wrap(impl).apply { value = this }
5252
// delegating to impl
5353
override val size: Int get() = impl.size

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

Lines changed: 10 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -16,21 +16,23 @@
1616

1717
package kotlinx.collections.immutable
1818

19-
public interface ImmutableCollection<out E>: Collection<E> {
20-
fun add(element: @UnsafeVariance E): ImmutableCollection<E>
19+
public interface ImmutableCollection<out E>: Collection<E>
2120

22-
fun addAll(elements: Collection<@UnsafeVariance E>): ImmutableCollection<E>
21+
public interface PersistentCollection<out E> : ImmutableCollection<E> {
22+
fun add(element: @UnsafeVariance E): PersistentCollection<E>
2323

24-
fun remove(element: @UnsafeVariance E): ImmutableCollection<E>
24+
fun addAll(elements: Collection<@UnsafeVariance E>): PersistentCollection<E>
2525

26-
fun removeAll(elements: Collection<@UnsafeVariance E>): ImmutableCollection<E>
26+
fun remove(element: @UnsafeVariance E): PersistentCollection<E>
2727

28-
fun removeAll(predicate: (E) -> Boolean): ImmutableCollection<E>
28+
fun removeAll(elements: Collection<@UnsafeVariance E>): PersistentCollection<E>
2929

30-
fun clear(): ImmutableCollection<E>
30+
fun removeAll(predicate: (E) -> Boolean): PersistentCollection<E>
31+
32+
fun clear(): PersistentCollection<E>
3133

3234
interface Builder<E>: MutableCollection<E> {
33-
fun build(): ImmutableCollection<E>
35+
fun build(): PersistentCollection<E>
3436
}
3537

3638
fun builder(): Builder<@UnsafeVariance E>

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ internal class ImmutableHashMap<K, out V> private constructor(impl: PMap<K, V>)
2323
override fun wrap(impl: PMap<K, @UnsafeVariance V>): ImmutableHashMap<K, V>
2424
= if (this.impl === impl) this else ImmutableHashMap(impl)
2525

26-
override fun clear(): ImmutableMap<K, V> = emptyOf()
26+
override fun clear(): PersistentMap<K, V> = emptyOf()
2727

2828
override fun builder(): Builder<K, @UnsafeVariance V> = Builder(this, impl)
2929

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

Lines changed: 43 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -16,36 +16,63 @@
1616

1717
package kotlinx.collections.immutable
1818

19-
public interface ImmutableList<out E>: List<E>, ImmutableCollection<E> {
20-
override fun add(element: @UnsafeVariance E): ImmutableList<E>
19+
import kotlinx.collections.immutable.internal.ListImplementation
2120

22-
override fun addAll(elements: Collection<@UnsafeVariance E>): ImmutableList<E> // = super<ImmutableCollection>.addAll(elements) as ImmutableList
21+
public interface ImmutableList<out E> : List<E>, ImmutableCollection<E> {
2322

24-
override fun remove(element: @UnsafeVariance E): ImmutableList<E>
23+
override fun subList(fromIndex: Int, toIndex: Int): ImmutableList<E> = SubList(this, fromIndex, toIndex)
2524

26-
override fun removeAll(elements: Collection<@UnsafeVariance E>): ImmutableList<E>
25+
public class SubList<E>(private val source: ImmutableList<E>, private val fromIndex: Int, private val toIndex: Int) : ImmutableList<E>, AbstractList<E>() {
26+
private var _size: Int = 0
2727

28-
override fun removeAll(predicate: (E) -> Boolean): ImmutableList<E>
28+
init {
29+
ListImplementation.checkRangeIndexes(fromIndex, toIndex, source.size)
30+
this._size = toIndex - fromIndex
31+
}
2932

30-
override fun clear(): ImmutableList<E>
33+
override fun get(index: Int): E {
34+
ListImplementation.checkElementIndex(index, _size)
3135

36+
return source[fromIndex + index]
37+
}
3238

33-
fun addAll(index: Int, c: Collection<@UnsafeVariance E>): ImmutableList<E> // = builder().apply { addAll(index, c.toList()) }.build()
39+
override val size: Int get() = _size
3440

35-
fun set(index: Int, element: @UnsafeVariance E): ImmutableList<E>
41+
override fun subList(fromIndex: Int, toIndex: Int): ImmutableList<E> {
42+
ListImplementation.checkRangeIndexes(fromIndex, toIndex, this._size)
43+
return SubList(source, this.fromIndex + fromIndex, this.fromIndex + toIndex)
44+
}
45+
46+
}
47+
}
48+
49+
public interface PersistentList<out E> : ImmutableList<E>, PersistentCollection<E> {
50+
override fun add(element: @UnsafeVariance E): PersistentList<E>
51+
52+
override fun addAll(elements: Collection<@UnsafeVariance E>): PersistentList<E> // = super<ImmutableCollection>.addAll(elements) as ImmutableList
53+
54+
override fun remove(element: @UnsafeVariance E): PersistentList<E>
55+
56+
override fun removeAll(elements: Collection<@UnsafeVariance E>): PersistentList<E>
57+
58+
override fun removeAll(predicate: (E) -> Boolean): PersistentList<E>
59+
60+
override fun clear(): PersistentList<E>
61+
62+
63+
fun addAll(index: Int, c: Collection<@UnsafeVariance E>): PersistentList<E> // = builder().apply { addAll(index, c.toList()) }.build()
64+
65+
fun set(index: Int, element: @UnsafeVariance E): PersistentList<E>
3666

3767
/**
3868
* Inserts an element into the list at the specified [index].
3969
*/
40-
fun add(index: Int, element: @UnsafeVariance E): ImmutableList<E>
41-
42-
fun removeAt(index: Int): ImmutableList<E>
43-
70+
fun add(index: Int, element: @UnsafeVariance E): PersistentList<E>
4471

45-
override fun subList(fromIndex: Int, toIndex: Int): ImmutableList<E>
72+
fun removeAt(index: Int): PersistentList<E>
4673

47-
interface Builder<E>: MutableList<E>, ImmutableCollection.Builder<E> {
48-
override fun build(): ImmutableList<E>
74+
interface Builder<E>: MutableList<E>, PersistentCollection.Builder<E> {
75+
override fun build(): PersistentList<E>
4976
}
5077

5178
override fun builder(): Builder<@UnsafeVariance E>

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

Lines changed: 13 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -19,28 +19,29 @@ package kotlinx.collections.immutable
1919

2020
public interface ImmutableMap<K, out V>: Map<K, V> {
2121

22-
override val keys: Set<K>
22+
override val keys: ImmutableSet<K>
23+
24+
override val values: ImmutableCollection<V>
25+
26+
override val entries: ImmutableSet<Map.Entry<K, V>>
27+
}
2328

24-
override val values: Collection<V>
2529

26-
override val entries: Set<Map.Entry<K, V>>
2730

28-
fun put(key: K, value: @UnsafeVariance V): ImmutableMap<K, V>
31+
public interface PersistentMap<K, out V> : ImmutableMap<K, V> {
32+
fun put(key: K, value: @UnsafeVariance V): PersistentMap<K, V>
2933

30-
fun remove(key: K): ImmutableMap<K, V>
34+
fun remove(key: K): PersistentMap<K, V>
3135

32-
fun remove(key: K, value: @UnsafeVariance V): ImmutableMap<K, V>
36+
fun remove(key: K, value: @UnsafeVariance V): PersistentMap<K, V>
3337

34-
fun putAll(m: Map<out K, @UnsafeVariance V>): ImmutableMap<K, V> // m: Iterable<Map.Entry<K, V>> or Map<out K,V> or Iterable<Pair<K, V>>
38+
fun putAll(m: Map<out K, @UnsafeVariance V>): PersistentMap<K, V> // m: Iterable<Map.Entry<K, V>> or Map<out K,V> or Iterable<Pair<K, V>>
3539

36-
fun clear(): ImmutableMap<K, V>
40+
fun clear(): PersistentMap<K, V>
3741

3842
interface Builder<K, V>: MutableMap<K, V> {
39-
fun build(): ImmutableMap<K, V>
43+
fun build(): PersistentMap<K, V>
4044
}
4145

4246
fun builder(): Builder<K, @UnsafeVariance V>
4347
}
44-
45-
46-

0 commit comments

Comments
 (0)