@@ -199,30 +199,35 @@ export class IndexedNode<K, V> {
199199 const bit = toBitmap ( frag )
200200 const indx = fromBitmap ( mask , bit )
201201 const exists = mask & bit
202+ const canEdit = canEditNode ( edit , this )
203+
204+ if ( ! exists ) {
205+ const _newChild = new Empty < K , V > ( ) . modify ( edit , shift + SIZE , f , hash , key , size )
206+ if ( ! _newChild ) return this
207+ return children . length >= MAX_INDEX_NODE
208+ ? expand ( edit , frag , _newChild , mask , children )
209+ : new IndexedNode (
210+ edit ,
211+ mask | bit ,
212+ arraySpliceIn ( canEdit , indx , _newChild , children )
213+ )
214+ }
215+
202216 const current = exists ? children [ indx ] ! : new Empty < K , V > ( )
203217 const child = current . modify ( edit , shift + SIZE , f , hash , key , size )
204218
205219 if ( current === child ) return this
206220
207- const canEdit = canEditNode ( edit , this )
208221 let bitmap = mask
209222 let newChildren
210- if ( exists && isEmptyNode ( child ) ) {
223+ if ( isEmptyNode ( child ) ) {
211224 // remove
212225 bitmap &= ~ bit
213226 if ( ! bitmap ) return new Empty ( )
214227 if ( children . length <= 2 && isLeaf ( children [ indx ^ 1 ] ! ) )
215228 return children [ indx ^ 1 ] ! // collapse
216229
217230 newChildren = arraySpliceOut ( canEdit , indx , children )
218- } else if ( ! exists && ! isEmptyNode ( child ) ) {
219- // add
220- if ( children . length >= MAX_INDEX_NODE ) {
221- return expand ( edit , frag , child , mask , children )
222- }
223-
224- bitmap |= bit
225- newChildren = arraySpliceIn ( canEdit , indx , child , children )
226231 } else {
227232 // modify
228233 newChildren = arrayUpdate ( canEdit , indx , child , children )
@@ -233,6 +238,7 @@ export class IndexedNode<K, V> {
233238 this . children = newChildren
234239 return this
235240 }
241+
236242 return new IndexedNode ( edit , bitmap , newChildren )
237243 }
238244}
0 commit comments