Skip to content

Commit cc93239

Browse files
committed
Use constructor instead of putNextLink in LinkedValue and Links
1 parent 5a70f34 commit cc93239

File tree

4 files changed

+15
-20
lines changed

4 files changed

+15
-20
lines changed

kotlinx-collections-immutable/src/main/kotlin/kotlinx/collections/immutable/implementations/persistentOrderedMap/PersistentOrderedMap.kt

Lines changed: 6 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -25,20 +25,17 @@ import kotlinx.collections.immutable.mutate
2525
internal object EndOfChain
2626

2727
internal class LinkedValue<V>(val value: V, val previous: Any?, val next: Any?) {
28+
/** Constructs LinkedValue for a new single entry */
2829
constructor(value: V) : this(value, EndOfChain, EndOfChain)
30+
/** Constructs LinkedValue for a new last entry */
31+
constructor(value: V, previous: Any?) : this(value, previous, EndOfChain)
2932

3033
fun withValue(newValue: V) = LinkedValue(newValue, previous, next)
3134
fun withPrevious(newPrevious: Any?) = LinkedValue(value, newPrevious, next)
3235
fun withNext(newNext: Any?) = LinkedValue(value, previous, newNext)
3336

3437
val hasNext get() = next !== EndOfChain
3538
val hasPrevious get() = previous !== EndOfChain
36-
37-
// TODO: it doesn't use 'this', can be made static
38-
fun putNextLink(value: V, previous: Any?): LinkedValue<V> {
39-
// assert(next === EndOfChain)
40-
return LinkedValue(value, previous, EndOfChain)
41-
}
4239
}
4340

4441
internal class PersistentOrderedMap<K, V>(
@@ -95,11 +92,11 @@ internal class PersistentOrderedMap<K, V>(
9592

9693
@Suppress("UNCHECKED_CAST")
9794
val lastKey = lastKey as K
98-
val lastLink = hashMap[lastKey]!!
95+
val lastLinks = hashMap[lastKey]!!
9996
// assert(!lastLink.hasNext)
10097
val newMap = hashMap
101-
.put(lastKey, lastLink.withNext(key))
102-
.put(key, lastLink.putNextLink(value, lastKey))
98+
.put(lastKey, lastLinks.withNext(key))
99+
.put(key, LinkedValue(value, previous = lastKey))
103100
return PersistentOrderedMap(firstKey, key, newMap)
104101
}
105102

kotlinx-collections-immutable/src/main/kotlin/kotlinx/collections/immutable/implementations/persistentOrderedMap/PersistentOrderedMapBuilder.kt

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -77,11 +77,11 @@ internal class PersistentOrderedMapBuilder<K, V>(private var map: PersistentOrde
7777
}
7878
@Suppress("UNCHECKED_CAST")
7979
val lastKey = lastKey as K
80-
val lastLink = hashMapBuilder[lastKey]!!
81-
assert(!lastLink.hasNext)
80+
val lastLinks = hashMapBuilder[lastKey]!!
81+
assert(!lastLinks.hasNext)
8282

83-
hashMapBuilder[lastKey] = lastLink.withNext(key)
84-
hashMapBuilder[key] = lastLink.putNextLink(value, lastKey)
83+
hashMapBuilder[lastKey] = lastLinks.withNext(key)
84+
hashMapBuilder[key] = LinkedValue(value, previous = lastKey)
8585
this.lastKey = key
8686
return null
8787
}

kotlinx-collections-immutable/src/main/kotlin/kotlinx/collections/immutable/implementations/persistentOrderedSet/PersistentOrderedSet.kt

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -22,18 +22,16 @@ import kotlinx.collections.immutable.implementations.persistentOrderedMap.EndOfC
2222
import kotlinx.collections.immutable.mutate
2323

2424
internal class Links(val previous: Any?, val next: Any?) {
25+
/** Constructs Links for a new single element */
2526
constructor() : this(EndOfChain, EndOfChain)
27+
/** Constructs Links for a new last element */
28+
constructor(previous: Any?) : this(previous, EndOfChain)
2629

2730
fun withNext(newNext: Any?) = Links(previous, newNext)
2831
fun withPrevious(newPrevious: Any?) = Links(newPrevious, next)
2932

3033
val hasNext get() = next !== EndOfChain
3134
val hasPrevious get() = previous !== EndOfChain
32-
33-
fun putNextLink(previous: Any?): Links {
34-
// assert(next === EndOfChain)
35-
return Links(previous, next)
36-
}
3735
}
3836

3937
internal class PersistentOrderedSet<E>(
@@ -61,7 +59,7 @@ internal class PersistentOrderedSet<E>(
6159

6260
val newMap = hashMap
6361
.put(lastElement, lastLinks.withNext(element))
64-
.put(element, lastLinks.putNextLink(lastElement))
62+
.put(element, Links(previous = lastElement))
6563
return PersistentOrderedSet(firstElement, element, newMap)
6664
}
6765

kotlinx-collections-immutable/src/main/kotlin/kotlinx/collections/immutable/implementations/persistentOrderedSet/PersistentOrderedSetBuilder.kt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,7 @@ internal class PersistentOrderedSetBuilder<E>(private var set: PersistentOrdered
5757
val lastLinks = hashMapBuilder[lastElement]!!
5858
// assert(lastLinks.next === EndOfLink)
5959
hashMapBuilder[lastElement as E] = lastLinks.withNext(element)
60-
hashMapBuilder[element] = lastLinks.putNextLink(lastElement)
60+
hashMapBuilder[element] = Links(previous = lastElement)
6161
lastElement = element
6262

6363
return true

0 commit comments

Comments
 (0)