Skip to content

Commit 02f38a9

Browse files
author
Abduqodiri Qurbonzoda
committed
Check for collision only the leaf node
1 parent 44f49db commit 02f38a9

File tree

2 files changed

+51
-54
lines changed

2 files changed

+51
-54
lines changed

core/commonMain/src/implementations/immutableMap/TrieNode.kt

Lines changed: 34 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -76,7 +76,8 @@ internal class TrieNode<K, V>(
7676
internal var buffer: Array<Any?> = buffer
7777
private set
7878

79-
internal fun isCollision(): Boolean {
79+
@Suppress("NOTHING_TO_INLINE")
80+
internal inline fun isCollision(): Boolean {
8081
return dataMap == 0 && nodeMap == 0 && buffer.isNotEmpty()
8182
}
8283

@@ -487,10 +488,6 @@ internal class TrieNode<K, V>(
487488
}
488489

489490
fun containsKey(keyHash: Int, key: K, shift: Int): Boolean {
490-
if (isCollision()) {
491-
return collisionContainsKey(key)
492-
}
493-
494491
val keyPositionMask = 1 shl indexSegment(keyHash, shift)
495492

496493
if (hasEntryAt(keyPositionMask)) { // key is directly in buffer
@@ -501,15 +498,15 @@ internal class TrieNode<K, V>(
501498
return targetNode.containsKey(keyHash, key, shift + LOG_MAX_BRANCHING_FACTOR)
502499
}
503500

501+
if (isCollision()) {
502+
return collisionContainsKey(key)
503+
}
504+
504505
// key is absent
505506
return false
506507
}
507508

508509
fun get(keyHash: Int, key: K, shift: Int): V? {
509-
if (isCollision()) {
510-
return collisionGet(key)
511-
}
512-
513510
val keyPositionMask = 1 shl indexSegment(keyHash, shift)
514511

515512
if (hasEntryAt(keyPositionMask)) { // key is directly in buffer
@@ -525,15 +522,15 @@ internal class TrieNode<K, V>(
525522
return targetNode.get(keyHash, key, shift + LOG_MAX_BRANCHING_FACTOR)
526523
}
527524

525+
if (isCollision()) {
526+
return collisionGet(key)
527+
}
528+
528529
// key is absent
529530
return null
530531
}
531532

532533
fun put(keyHash: Int, key: K, value: @UnsafeVariance V, shift: Int): ModificationResult<K, V>? {
533-
if (isCollision()) {
534-
return collisionPut(key, value, shift)
535-
}
536-
537534
val keyPositionMask = 1 shl indexSegment(keyHash, shift)
538535

539536
if (hasEntryAt(keyPositionMask)) { // key is directly in buffer
@@ -554,15 +551,15 @@ internal class TrieNode<K, V>(
554551
return putResult.replaceNode { node -> updateNodeAtIndex(nodeIndex, node) }
555552
}
556553

554+
if (isCollision()) {
555+
return collisionPut(key, value, shift)
556+
}
557+
557558
// no entry at this key hash segment
558559
return insertEntryAt(keyPositionMask, key, value).asInsertResult()
559560
}
560561

561562
fun mutablePut(keyHash: Int, key: K, value: @UnsafeVariance V, shift: Int, mutator: PersistentHashMapBuilder<K, V>): TrieNode<K, V> {
562-
if (isCollision()) {
563-
return mutableCollisionPut(key, value, shift, mutator)
564-
}
565-
566563
val keyPositionMask = 1 shl indexSegment(keyHash, shift)
567564

568565
if (hasEntryAt(keyPositionMask)) { // key is directly in buffer
@@ -590,16 +587,16 @@ internal class TrieNode<K, V>(
590587
return mutableUpdateNodeAtIndex(nodeIndex, newNode, mutator.ownership)
591588
}
592589

590+
if (isCollision()) {
591+
return mutableCollisionPut(key, value, shift, mutator)
592+
}
593+
593594
// key is absent
594595
mutator.size++
595596
return mutableInsertEntryAt(keyPositionMask, key, value, mutator.ownership)
596597
}
597598

598599
fun remove(keyHash: Int, key: K, shift: Int): TrieNode<K, V>? {
599-
if (isCollision()) {
600-
return collisionRemove(key)
601-
}
602-
603600
val keyPositionMask = 1 shl indexSegment(keyHash, shift)
604601

605602
if (hasEntryAt(keyPositionMask)) { // key is directly in buffer
@@ -622,15 +619,15 @@ internal class TrieNode<K, V>(
622619
}
623620
}
624621

622+
if (isCollision()) {
623+
return collisionRemove(key)
624+
}
625+
625626
// key is absent
626627
return this
627628
}
628629

629630
fun mutableRemove(keyHash: Int, key: K, shift: Int, mutator: PersistentHashMapBuilder<K, V>): TrieNode<K, V>? {
630-
if (isCollision()) {
631-
return mutableCollisionRemove(key, mutator)
632-
}
633-
634631
val keyPositionMask = 1 shl indexSegment(keyHash, shift)
635632

636633
if (hasEntryAt(keyPositionMask)) { // key is directly in buffer
@@ -653,15 +650,15 @@ internal class TrieNode<K, V>(
653650
}
654651
}
655652

653+
if (isCollision()) {
654+
return mutableCollisionRemove(key, mutator)
655+
}
656+
656657
// key is absent
657658
return this
658659
}
659660

660661
fun remove(keyHash: Int, key: K, value: @UnsafeVariance V, shift: Int): TrieNode<K, V>? {
661-
if (isCollision()) {
662-
return collisionRemove(key, value)
663-
}
664-
665662
val keyPositionMask = 1 shl indexSegment(keyHash, shift)
666663

667664
if (hasEntryAt(keyPositionMask)) { // key is directly in buffer
@@ -684,15 +681,15 @@ internal class TrieNode<K, V>(
684681
}
685682
}
686683

684+
if (isCollision()) {
685+
return collisionRemove(key, value)
686+
}
687+
687688
// key is absent
688689
return this
689690
}
690691

691692
fun mutableRemove(keyHash: Int, key: K, value: @UnsafeVariance V, shift: Int, mutator: PersistentHashMapBuilder<K, V>): TrieNode<K, V>? {
692-
if (isCollision()) {
693-
return mutableCollisionRemove(key, value, mutator)
694-
}
695-
696693
val keyPositionMask = 1 shl indexSegment(keyHash, shift)
697694

698695
if (hasEntryAt(keyPositionMask)) { // key is directly in buffer
@@ -715,6 +712,10 @@ internal class TrieNode<K, V>(
715712
}
716713
}
717714

715+
if (isCollision()) {
716+
return mutableCollisionRemove(key, value, mutator)
717+
}
718+
718719
// key is absent
719720
return this
720721
}

core/commonMain/src/implementations/immutableSet/TrieNode.kt

Lines changed: 17 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -268,18 +268,18 @@ internal class TrieNode<E>(
268268
return makeCollisionNode(buffer.removeCellAtIndex(index), mutator.ownership)
269269
}
270270

271-
fun isCollision(): Boolean {
271+
@Suppress("NOTHING_TO_INLINE")
272+
inline fun isCollision(): Boolean {
272273
return bitmap == 0 && buffer.isNotEmpty()
273274
}
274275

275276
fun contains(elementHash: Int, element: E, shift: Int): Boolean {
276-
if (isCollision()) {
277-
return collisionContains(elementHash, element)
278-
}
279-
280277
val cellPositionMask = 1 shl indexSegment(elementHash, shift)
281278

282279
if (hasNoCellAt(cellPositionMask)) { // element is absent
280+
if (isCollision()) {
281+
return collisionContains(elementHash, element)
282+
}
283283
return false
284284
}
285285

@@ -293,13 +293,12 @@ internal class TrieNode<E>(
293293
}
294294

295295
fun add(elementHash: Int, element: E, shift: Int): TrieNode<E> {
296-
if (isCollision()) {
297-
return collisionAdd(elementHash, element, shift)
298-
}
299-
300296
val cellPositionMask = 1 shl indexSegment(elementHash, shift)
301297

302298
if (hasNoCellAt(cellPositionMask)) { // element is absent
299+
if (isCollision()) {
300+
return collisionAdd(elementHash, element, shift)
301+
}
303302
return addElementAt(cellPositionMask, element)
304303
}
305304

@@ -316,13 +315,12 @@ internal class TrieNode<E>(
316315
}
317316

318317
fun mutableAdd(elementHash: Int, element: E, shift: Int, mutator: PersistentHashSetBuilder<*>): TrieNode<E> {
319-
if (isCollision()) {
320-
return collisionMutableAdd(elementHash, element, shift, mutator)
321-
}
322-
323318
val cellPosition = 1 shl indexSegment(elementHash, shift)
324319

325320
if (hasNoCellAt(cellPosition)) { // element is absent
321+
if (isCollision()) {
322+
return collisionMutableAdd(elementHash, element, shift, mutator)
323+
}
326324
mutator.size++
327325
return mutableAddElementAt(cellPosition, element, mutator.ownership)
328326
}
@@ -341,13 +339,12 @@ internal class TrieNode<E>(
341339
}
342340

343341
fun remove(elementHash: Int, element: E, shift: Int): TrieNode<E>? {
344-
if (isCollision()) {
345-
return collisionRemove(elementHash, element)
346-
}
347-
348342
val cellPositionMask = 1 shl indexSegment(elementHash, shift)
349343

350344
if (hasNoCellAt(cellPositionMask)) { // element is absent
345+
if (isCollision()) {
346+
return collisionRemove(elementHash, element)
347+
}
351348
return this
352349
}
353350

@@ -369,13 +366,12 @@ internal class TrieNode<E>(
369366
}
370367

371368
fun mutableRemove(elementHash: Int, element: E, shift: Int, mutator: PersistentHashSetBuilder<*>): TrieNode<E>? {
372-
if (isCollision()) {
373-
return collisionMutableRemove(elementHash, element, mutator)
374-
}
375-
376369
val cellPositionMask = 1 shl indexSegment(elementHash, shift)
377370

378371
if (hasNoCellAt(cellPositionMask)) { // element is absent
372+
if (isCollision()) {
373+
return collisionMutableRemove(elementHash, element, mutator)
374+
}
379375
return this
380376
}
381377

0 commit comments

Comments
 (0)