Skip to content

Commit 9b6cf0d

Browse files
author
Abduqodiri Qurbonzoda
committed
Support building project with JDK_6
1 parent a53f7c9 commit 9b6cf0d

File tree

4 files changed

+60
-4
lines changed

4 files changed

+60
-4
lines changed

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

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -75,6 +75,13 @@ internal class PersistentHashMapBuilder<K, V>(private var map: PersistentHashMap
7575
return operationResult
7676
}
7777

78+
fun remove(key: K, value: V): Boolean {
79+
val oldSize = size
80+
val keyHash = key?.hashCode() ?: NULL_HASH_CODE
81+
node = node.mutableRemove(keyHash, key, value, 0, this) ?: TrieNode.EMPTY as TrieNode<K, V>
82+
return oldSize != size
83+
}
84+
7885
override fun clear() {
7986
node = TrieNode.EMPTY as TrieNode<K, V>
8087
size = 0

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

Lines changed: 40 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -194,6 +194,7 @@ internal class TrieNode<K, V>(var dataMap: Int,
194194

195195
private fun mutableRemoveDataAtIndex(keyIndex: Int, position: Int, mutator: PersistentHashMapBuilder<K, V>): TrieNode<K, V>? {
196196
// assert(hasDataAt(position))
197+
mutator.size--
197198
mutator.operationResult = buffer[keyIndex + 1] as V
198199
if (buffer.size == 2) { return null }
199200

@@ -214,6 +215,7 @@ internal class TrieNode<K, V>(var dataMap: Int,
214215
}
215216

216217
private fun mutableCollisionRemoveDataAtIndex(i: Int, mutator: PersistentHashMapBuilder<K, V>): TrieNode<K, V>? {
218+
mutator.size--
217219
mutator.operationResult = buffer[i + 1] as V
218220
if (buffer.size == 2) { return null }
219221

@@ -315,7 +317,6 @@ internal class TrieNode<K, V>(var dataMap: Int,
315317
private fun mutableCollisionRemove(key: K, mutator: PersistentHashMapBuilder<K, V>): TrieNode<K, V>? {
316318
for (i in 0 until buffer.size step ENTRY_SIZE) {
317319
if (key == buffer[i]) {
318-
mutator.size--
319320
return mutableCollisionRemoveDataAtIndex(i, mutator)
320321
}
321322
}
@@ -331,6 +332,15 @@ internal class TrieNode<K, V>(var dataMap: Int,
331332
return this
332333
}
333334

335+
private fun mutableCollisionRemove(key: K, value: V, mutator: PersistentHashMapBuilder<K, V>): TrieNode<K, V>? {
336+
for (i in 0 until buffer.size step ENTRY_SIZE) {
337+
if (key == buffer[i] && value == buffer[i + 1]) {
338+
return mutableCollisionRemoveDataAtIndex(i, mutator)
339+
}
340+
}
341+
return this
342+
}
343+
334344
fun containsKey(keyHash: Int, key: K, shift: Int): Boolean {
335345
val keyPosition = 1 shl ((keyHash shr shift) and MAX_BRANCHING_FACTOR_MINUS_ONE)
336346

@@ -472,7 +482,6 @@ internal class TrieNode<K, V>(var dataMap: Int,
472482
val keyIndex = keyDataIndex(keyPosition)
473483

474484
if (key == keyAtIndex(keyIndex)) {
475-
mutator.size--
476485
return mutableRemoveDataAtIndex(keyIndex, keyPosition, mutator)
477486
}
478487
return this
@@ -524,6 +533,35 @@ internal class TrieNode<K, V>(var dataMap: Int,
524533
return this
525534
}
526535

536+
fun mutableRemove(keyHash: Int, key: K, value: @UnsafeVariance V, shift: Int, mutator: PersistentHashMapBuilder<K, V>): TrieNode<K, V>? {
537+
val keyPosition = 1 shl ((keyHash shr shift) and MAX_BRANCHING_FACTOR_MINUS_ONE)
538+
539+
if (hasDataAt(keyPosition)) { // key is directly in buffer
540+
val keyIndex = keyDataIndex(keyPosition)
541+
542+
if (key == keyAtIndex(keyIndex) && value == valueAtKeyIndex(keyIndex)) {
543+
return mutableRemoveDataAtIndex(keyIndex, keyPosition, mutator)
544+
}
545+
return this
546+
}
547+
if (hasNodeAt(keyPosition)) { // key is in node
548+
val nodeIndex = keyNodeIndex(keyPosition)
549+
550+
val targetNode = nodeAtIndex(nodeIndex)
551+
val newNode = if (shift == MAX_SHIFT) {
552+
targetNode.mutableCollisionRemove(key, value, mutator)
553+
} else {
554+
targetNode.mutableRemove(keyHash, key, value, shift + LOG_MAX_BRANCHING_FACTOR, mutator)
555+
}
556+
if (targetNode === newNode) { return this }
557+
if (newNode == null) { return mutableRemoveNodeAtIndex(nodeIndex, keyPosition, mutator.marker) }
558+
return mutableUpdateNodeAtIndex(nodeIndex, newNode, mutator.marker)
559+
}
560+
561+
// key is absent
562+
return this
563+
}
564+
527565
internal companion object {
528566
internal val EMPTY = TrieNode<Nothing, Nothing>(0, 0, emptyArray())
529567
}

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

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -105,6 +105,17 @@ internal class PersistentOrderedMapBuilder<K, V>(private var map: PersistentOrde
105105
return links.value
106106
}
107107

108+
fun remove(key: K, value: V): Boolean {
109+
val links = mapBuilder[key] ?: return false
110+
111+
return if (links.value != value) {
112+
false
113+
} else {
114+
remove(key)
115+
true
116+
}
117+
}
118+
108119
override fun clear() {
109120
mapBuilder.clear()
110121
firstKey = null

kotlinx-collections-immutable/tests/src/test/kotlin/kotlinx.collections.immutable/stressTests/WrapperGenerator.kt

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -32,14 +32,14 @@ class WrapperGenerator<K: Comparable<K>>(private val hashCodeUpperBound: Int) {
3232
val wrapper = ObjectWrapper(element, hashCode)
3333
elementMap[element] = wrapper
3434

35-
val wrappers = hashCodeMap.getOrDefault(hashCode, mutableListOf())
35+
val wrappers = hashCodeMap[hashCode] ?: mutableListOf()
3636
wrappers.add(wrapper)
3737
hashCodeMap[hashCode] = wrappers
3838

3939
return wrapper
4040
}
4141

4242
fun wrappersByHashCode(hashCode: Int): List<ObjectWrapper<K>> {
43-
return hashCodeMap.getOrDefault(hashCode, mutableListOf())
43+
return hashCodeMap[hashCode] ?: mutableListOf()
4444
}
4545
}

0 commit comments

Comments
 (0)