@@ -8,7 +8,9 @@ import { isEqual, Result$Error, Result$Ok } from "./gleam.mjs";
88// -- HASH --------------------------------------------------------------------
99
1010const referenceMap = /* @__PURE__ */ new WeakMap ( ) ;
11- const tempDataView = /* @__PURE__ */ new DataView ( /* @__PURE__ */ new ArrayBuffer ( 8 ) ) ;
11+ const tempDataView = /* @__PURE__ */ new DataView (
12+ /* @__PURE__ */ new ArrayBuffer ( 8 ) ,
13+ ) ;
1214let referenceUID = 0 ;
1315/**
1416 * hash the object by reference using a weak map and incrementing uid
@@ -299,7 +301,7 @@ export function make() {
299301export function from ( iterable ) {
300302 let transient = toTransient ( emptyDict ) ;
301303 for ( const [ key , value ] of iterable ) {
302- transient = put ( key , value , transient ) ;
304+ transient = transientInsert ( key , value , transient ) ;
303305 }
304306 return fromTransient ( transient ) ;
305307}
@@ -432,7 +434,7 @@ export function insert(dict, key, value) {
432434 globalTransient . generation = nextGeneration ( dict ) ;
433435 globalTransient . size = dict . size ;
434436
435- const root = doPut ( globalTransient , dict . root , key , value , getHash ( key ) , 0 ) ;
437+ const root = doInsert ( globalTransient , dict . root , key , value , getHash ( key ) , 0 ) ;
436438 if ( root === dict . root ) {
437439 return dict ;
438440 }
@@ -446,13 +448,30 @@ export function insert(dict, key, value) {
446448 *
447449 * Returns a new transient.
448450 */
449- export function put ( key , value , transient ) {
451+ export function transientInsert ( key , value , transient ) {
450452 const hash = getHash ( key ) ;
451- transient . root = doPut ( transient , transient . root , key , value , hash , 0 ) ;
453+ transient . root = doInsert ( transient , transient . root , key , value , hash , 0 ) ;
452454 return transient ;
453455}
454456
455- function doPut ( transient , node , key , value , hash , shift ) {
457+ /**
458+ * Consume a transient, writing a new key/value pair if the key doesn't exist or updating
459+ * the existing value with a function if it does.
460+ *
461+ * Returns a new transient.
462+ */
463+ export function transientUpdateWith ( key , fun , value , transient ) {
464+ const hash = getHash ( key ) ;
465+
466+ const existing = lookup ( transient . root , key , hash ) ;
467+ if ( existing !== noElementMarker ) {
468+ value = fun ( existing ) ;
469+ }
470+ transient . root = doInsert ( transient , transient . root , key , value , hash , 0 ) ;
471+ return transient ;
472+ }
473+
474+ function doInsert ( transient , node , key , value , hash , shift ) {
456475 const data = node . data ;
457476 const generation = transient . generation ;
458477
@@ -476,7 +495,7 @@ function doPut(transient, node, key, value, hash, shift) {
476495 if ( node . nodemap & bit ) {
477496 const nodeidx = data . length - 1 - index ( node . nodemap , bit ) ;
478497 const child = data [ nodeidx ] ;
479- const newChild = doPut ( transient , child , key , value , hash , shift + bits ) ;
498+ const newChild = doInsert ( transient , child , key , value , hash , shift + bits ) ;
480499 return copyAndSet ( node , generation , nodeidx , newChild ) ;
481500 }
482501
@@ -503,8 +522,8 @@ function doPut(transient, node, key, value, hash, shift) {
503522 const childShift = shift + bits ;
504523
505524 let child = emptyNode ;
506- child = doPut ( transient , child , key , value , hash , childShift ) ;
507- child = doPut ( transient , child , otherKey , otherVal , otherHash , childShift ) ;
525+ child = doInsert ( transient , child , key , value , hash , childShift ) ;
526+ child = doInsert ( transient , child , otherKey , otherVal , otherHash , childShift ) ;
508527
509528 // we inserted 2 elements, but implicitely deleted the one we pushed down from the datamap.
510529 transient . size -= 1 ;
@@ -529,16 +548,17 @@ function doPut(transient, node, key, value, hash, shift) {
529548
530549 return makeNode ( generation , node . datamap ^ bit , node . nodemap | bit , newData ) ;
531550}
551+
532552/**
533553 * Consume a transient, removing a key if it exists.
534554 * Returns a new transient.
535555 */
536- export function remove ( key , transient ) {
537- transient . root = doRemove ( transient , transient . root , key , getHash ( key ) , 0 ) ;
556+ export function transientDelete ( key , transient ) {
557+ transient . root = doDelete ( transient , transient . root , key , getHash ( key ) , 0 ) ;
538558 return transient ;
539559}
540560
541- function doRemove ( transient , node , key , hash , shift ) {
561+ function doDelete ( transient , node , key , hash , shift ) {
542562 const data = node . data ;
543563 const generation = transient . generation ;
544564
@@ -564,7 +584,7 @@ function doRemove(transient, node, key, hash, shift) {
564584 const nodeidx = data . length - 1 - index ( node . nodemap , bit ) ;
565585
566586 const oldChild = data [ nodeidx ] ;
567- const newChild = doRemove ( transient , oldChild , key , hash , shift + bits ) ;
587+ const newChild = doDelete ( transient , oldChild , key , hash , shift + bits ) ;
568588
569589 // the node did change, so let's copy to incorporate that change.
570590 if ( newChild . nodemap !== 0 || newChild . data . length > 2 ) {
@@ -602,18 +622,6 @@ function doRemove(transient, node, key, hash, shift) {
602622 return copyAndRemovePair ( node , generation , bit , dataidx ) ;
603623}
604624
605- export function update_with ( key , fun , value , transient ) {
606- const hash = getHash ( key ) ;
607-
608- const existing = lookup ( transient . root , key , hash ) ;
609- if ( existing !== noElementMarker ) {
610- value = fun ( existing ) ;
611- }
612-
613- transient . root = doPut ( transient , transient . root , key , value , hash , 0 ) ;
614- return transient ;
615- }
616-
617625export function map ( dict , fun ) {
618626 // map can never modify the structure, so we can walk the dictionary directly,
619627 // but still move to a new generation to make sure we get a new copy of every node.
0 commit comments