@@ -434,7 +434,8 @@ export function insert(dict, key, value) {
434434 globalTransient . generation = nextGeneration ( dict ) ;
435435 globalTransient . size = dict . size ;
436436
437- const root = doInsert ( globalTransient , dict . root , key , value , getHash ( key ) , 0 ) ;
437+ const hash = getHash ( key ) ;
438+ const root = insertIntoNode ( globalTransient , dict . root , key , value , hash , 0 ) ;
438439 if ( root === dict . root ) {
439440 return dict ;
440441 }
@@ -450,7 +451,7 @@ export function insert(dict, key, value) {
450451 */
451452export function destructiveTransientInsert ( key , value , transient ) {
452453 const hash = getHash ( key ) ;
453- transient . root = doInsert ( transient , transient . root , key , value , hash , 0 ) ;
454+ transient . root = insertIntoNode ( transient , transient . root , key , value , hash , 0 ) ;
454455 return transient ;
455456}
456457
@@ -467,11 +468,11 @@ export function destructiveTransientUpdateWith(key, fun, value, transient) {
467468 if ( existing !== noElementMarker ) {
468469 value = fun ( existing ) ;
469470 }
470- transient . root = doInsert ( transient , transient . root , key , value , hash , 0 ) ;
471+ transient . root = insertIntoNode ( transient , transient . root , key , value , hash , 0 ) ;
471472 return transient ;
472473}
473474
474- function doInsert ( transient , node , key , value , hash , shift ) {
475+ function insertIntoNode ( transient , node , key , value , hash , shift ) {
475476 const data = node . data ;
476477 const generation = transient . generation ;
477478
@@ -494,9 +495,10 @@ function doInsert(transient, node, key, value, hash, shift) {
494495 // We have to check first if there is already a child node we have to traverse to.
495496 if ( node . nodemap & bit ) {
496497 const nodeidx = data . length - 1 - index ( node . nodemap , bit ) ;
497- const child = data [ nodeidx ] ;
498- const newChild = doInsert ( transient , child , key , value , hash , shift + bits ) ;
499- return copyAndSet ( node , generation , nodeidx , newChild ) ;
498+
499+ let child = data [ nodeidx ] ;
500+ child = insertIntoNode ( transient , child , key , value , hash , shift + bits ) ;
501+ return copyAndSet ( node , generation , nodeidx , child ) ;
500502 }
501503
502504 // 3. New Data Node
@@ -516,14 +518,15 @@ function doInsert(transient, node, key, value, hash, shift) {
516518 // 5. Collision
517519 // There is no child node, but a data node with the same hash, but with a different key.
518520 // To resolve this, we push both nodes down one level.
519- const otherKey = data [ dataidx ] ;
520- const otherVal = data [ dataidx + 1 ] ;
521- const otherHash = getHash ( otherKey ) ;
522521 const childShift = shift + bits ;
523522
524523 let child = emptyNode ;
525- child = doInsert ( transient , child , key , value , hash , childShift ) ;
526- child = doInsert ( transient , child , otherKey , otherVal , otherHash , childShift ) ;
524+ child = insertIntoNode ( transient , emptyNode , key , value , hash , childShift ) ;
525+
526+ const key2 = data [ dataidx ] ;
527+ const value2 = data [ dataidx + 1 ] ;
528+ const hash2 = getHash ( key2 ) ;
529+ child = insertIntoNode ( transient , child , key2 , value2 , hash2 , childShift ) ;
527530
528531 // we inserted 2 elements, but implicitely deleted the one we pushed down from the datamap.
529532 transient . size -= 1 ;
@@ -554,11 +557,12 @@ function doInsert(transient, node, key, value, hash, shift) {
554557 * Returns a new transient.
555558 */
556559export function destructiveTransientDelete ( key , transient ) {
557- transient . root = doDelete ( transient , transient . root , key , getHash ( key ) , 0 ) ;
560+ const hash = getHash ( key ) ;
561+ transient . root = deleteFromNode ( transient , transient . root , key , hash , 0 ) ;
558562 return transient ;
559563}
560564
561- function doDelete ( transient , node , key , hash , shift ) {
565+ function deleteFromNode ( transient , node , key , hash , shift ) {
562566 const data = node . data ;
563567 const generation = transient . generation ;
564568
@@ -583,12 +587,12 @@ function doDelete(transient, node, key, hash, shift) {
583587 if ( ( node . nodemap & bit ) !== 0 ) {
584588 const nodeidx = data . length - 1 - index ( node . nodemap , bit ) ;
585589
586- const oldChild = data [ nodeidx ] ;
587- const newChild = doDelete ( transient , oldChild , key , hash , shift + bits ) ;
590+ let child = data [ nodeidx ] ;
591+ child = deleteFromNode ( transient , child , key , hash , shift + bits ) ;
588592
589593 // the node did change, so let's copy to incorporate that change.
590- if ( newChild . nodemap !== 0 || newChild . data . length > 2 ) {
591- return copyAndSet ( node , generation , nodeidx , newChild ) ;
594+ if ( child . nodemap !== 0 || child . data . length > 2 ) {
595+ return copyAndSet ( node , generation , nodeidx , child ) ;
592596 }
593597
594598 // this node only has a single data (k/v-pair) child.
@@ -602,8 +606,8 @@ function doDelete(transient, node, key, hash, shift) {
602606 let writeIndex = 0 ;
603607
604608 while ( readIndex < dataidx ) newData [ writeIndex ++ ] = data [ readIndex ++ ] ;
605- newData [ writeIndex ++ ] = newChild . data [ 0 ] ;
606- newData [ writeIndex ++ ] = newChild . data [ 1 ] ;
609+ newData [ writeIndex ++ ] = child . data [ 0 ] ;
610+ newData [ writeIndex ++ ] = child . data [ 1 ] ;
607611 while ( readIndex < nodeidx ) newData [ writeIndex ++ ] = data [ readIndex ++ ] ;
608612 readIndex ++ ;
609613 while ( readIndex < length ) newData [ writeIndex ++ ] = data [ readIndex ++ ] ;
0 commit comments