| 510 | } |
| 511 | |
| 512 | set( |
| 513 | edit: number, |
| 514 | shift: number, |
| 515 | hash: number, |
| 516 | key: K, |
| 517 | value: V, |
| 518 | added: { value: boolean } |
| 519 | ): Node<K, V> { |
| 520 | if (this.canEdit(edit)) this._hash = undefined |
| 521 | const bit = bitpos(hash, shift) |
| 522 | const idx = index(this.bitmap, bit) |
| 523 | |
| 524 | if ((this.bitmap & bit) !== 0) { |
| 525 | // Existing child - update it |
| 526 | const child = this.children[idx] |
| 527 | const newChild = child.set(edit, shift + SHIFT, hash, key, value, added) |
| 528 | if (child === newChild) { |
| 529 | return this |
| 530 | } |
| 531 | |
| 532 | if (this.canEdit(edit)) { |
| 533 | this.children[idx] = newChild |
| 534 | return this |
| 535 | } |
| 536 | |
| 537 | const newChildren = this.children.slice() |
| 538 | newChildren[idx] = newChild |
| 539 | return new IndexedNode(edit, this.bitmap, newChildren) |
| 540 | } else { |
| 541 | // New child - insert |
| 542 | added.value = true |
| 543 | const newChild = new LeafNode(edit, hash, key, value) |
| 544 | const newBitmap = this.bitmap | bit |
| 545 | |
| 546 | if (this.canEdit(edit)) { |
| 547 | this.children.splice(idx, 0, newChild) |
| 548 | this.bitmap = newBitmap |
| 549 | this._size = undefined |
| 550 | |
| 551 | if (this.children.length > MAX_INDEX_NODE) { |
| 552 | return this.expand(edit, newBitmap, this.children) |
| 553 | } |
| 554 | return this |
| 555 | } |
| 556 | |
| 557 | const newChildren = insertAt(this.children, idx, newChild) |
| 558 | |
| 559 | if (newChildren.length > MAX_INDEX_NODE) { |
| 560 | return this.expand(edit, newBitmap, newChildren) |
| 561 | } |
| 562 | |
| 563 | return new IndexedNode(edit, newBitmap, newChildren) |
| 564 | } |
| 565 | } |
| 566 | |
| 567 | remove( |
| 568 | edit: number, |