Skip to content

Commit 7036128

Browse files
committed
Replace direct buffer access with keyAtIndex and valueAtKeyIndex
1 parent 782d866 commit 7036128

File tree

1 file changed

+12
-15
lines changed
  • kotlinx-collections-immutable/src/main/kotlin/kotlinx/collections/immutable/implementations/immutableMap

1 file changed

+12
-15
lines changed

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

Lines changed: 12 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -270,7 +270,7 @@ internal class TrieNode<K, V>(
270270
private fun mutableRemoveEntryAtIndex(keyIndex: Int, positionMask: Int, mutator: PersistentHashMapBuilder<K, V>): TrieNode<K, V>? {
271271
// assert(hasEntryAt(positionMask))
272272
mutator.size--
273-
mutator.operationResult = buffer[keyIndex + 1] as V
273+
mutator.operationResult = valueAtKeyIndex(keyIndex)
274274
if (buffer.size == ENTRY_SIZE) return null
275275

276276
if (marker === mutator.marker) {
@@ -291,8 +291,7 @@ internal class TrieNode<K, V>(
291291

292292
private fun mutableCollisionRemoveEntryAtIndex(i: Int, mutator: PersistentHashMapBuilder<K, V>): TrieNode<K, V>? {
293293
mutator.size--
294-
@Suppress("UNCHECKED_CAST")
295-
mutator.operationResult = buffer[i + 1] as V
294+
mutator.operationResult = valueAtKeyIndex(i)
296295
if (buffer.size == ENTRY_SIZE) return null
297296

298297
if (marker === mutator.marker) {
@@ -333,18 +332,17 @@ internal class TrieNode<K, V>(
333332

334333
private fun collisionGet(key: K): V? {
335334
for (i in 0 until buffer.size step ENTRY_SIZE) {
336-
if (key == buffer[i]) {
337-
@Suppress("UNCHECKED_CAST")
338-
return buffer[i + 1] as V
335+
if (key == keyAtIndex(i)) {
336+
return valueAtKeyIndex(i)
339337
}
340338
}
341339
return null
342340
}
343341

344342
private fun collisionPut(key: K, value: V): ModificationResult<K, V>? {
345343
for (i in 0 until buffer.size step ENTRY_SIZE) {
346-
if (key == buffer[i]) {
347-
if (value === buffer[i + 1]) {
344+
if (key == keyAtIndex(i)) {
345+
if (value === valueAtKeyIndex(i)) {
348346
return null
349347
}
350348
val newBuffer = buffer.copyOf()
@@ -359,9 +357,8 @@ internal class TrieNode<K, V>(
359357
private fun mutableCollisionPut(key: K, value: V, mutator: PersistentHashMapBuilder<K, V>): TrieNode<K, V> {
360358
// Check if there is an entry with the specified key.
361359
for (i in 0 until buffer.size step ENTRY_SIZE) {
362-
if (key == buffer[i]) { // found entry with the specified key
363-
@Suppress("UNCHECKED_CAST")
364-
mutator.operationResult = buffer[i + 1] as V
360+
if (key == keyAtIndex(i)) { // found entry with the specified key
361+
mutator.operationResult = valueAtKeyIndex(i)
365362

366363
// If the [mutator] is exclusive owner of this node, update value of the entry in-place.
367364
if (marker === mutator.marker) {
@@ -385,7 +382,7 @@ internal class TrieNode<K, V>(
385382

386383
private fun collisionRemove(key: K): TrieNode<K, V>? {
387384
for (i in 0 until buffer.size step ENTRY_SIZE) {
388-
if (key == buffer[i]) {
385+
if (key == keyAtIndex(i)) {
389386
return collisionRemoveEntryAtIndex(i)
390387
}
391388
}
@@ -394,7 +391,7 @@ internal class TrieNode<K, V>(
394391

395392
private fun mutableCollisionRemove(key: K, mutator: PersistentHashMapBuilder<K, V>): TrieNode<K, V>? {
396393
for (i in 0 until buffer.size step ENTRY_SIZE) {
397-
if (key == buffer[i]) {
394+
if (key == keyAtIndex(i)) {
398395
return mutableCollisionRemoveEntryAtIndex(i, mutator)
399396
}
400397
}
@@ -403,7 +400,7 @@ internal class TrieNode<K, V>(
403400

404401
private fun collisionRemove(key: K, value: V): TrieNode<K, V>? {
405402
for (i in 0 until buffer.size step ENTRY_SIZE) {
406-
if (key == buffer[i] && value == buffer[i + 1]) {
403+
if (key == keyAtIndex(i) && value == valueAtKeyIndex(i)) {
407404
return collisionRemoveEntryAtIndex(i)
408405
}
409406
}
@@ -412,7 +409,7 @@ internal class TrieNode<K, V>(
412409

413410
private fun mutableCollisionRemove(key: K, value: V, mutator: PersistentHashMapBuilder<K, V>): TrieNode<K, V>? {
414411
for (i in 0 until buffer.size step ENTRY_SIZE) {
415-
if (key == buffer[i] && value == buffer[i + 1]) {
412+
if (key == keyAtIndex(i) && value == valueAtKeyIndex(i)) {
416413
return mutableCollisionRemoveEntryAtIndex(i, mutator)
417414
}
418415
}

0 commit comments

Comments
 (0)