| 520 | } |
| 521 | |
| 522 | remove( |
| 523 | edit: number, |
| 524 | shift: number, |
| 525 | hash: number, |
| 526 | key: K, |
| 527 | removed: { value: boolean } |
| 528 | ): Node<K, V> | undefined { |
| 529 | const bit = bitpos(hash, shift) |
| 530 | if ((this.bitmap & bit) === 0) { |
| 531 | return this |
| 532 | } |
| 533 | |
| 534 | const idx = index(this.bitmap, bit) |
| 535 | const child = this.children[idx] |
| 536 | const newChild = child.remove(edit, shift + SHIFT, hash, key, removed) |
| 537 | |
| 538 | if (!removed.value) { |
| 539 | return this |
| 540 | } |
| 541 | |
| 542 | if (newChild === undefined) { |
| 543 | const newBitmap = this.bitmap ^ bit |
| 544 | if (newBitmap === 0) { |
| 545 | return undefined |
| 546 | } |
| 547 | |
| 548 | if (this.children.length === 2) { |
| 549 | const remaining = this.children[idx === 0 ? 1 : 0] |
| 550 | if (remaining._tag === "LeafNode") { |
| 551 | return remaining |
| 552 | } |
| 553 | } |
| 554 | |
| 555 | if (this.canEdit(edit)) { |
| 556 | this.children.splice(idx, 1) |
| 557 | this.bitmap = newBitmap |
| 558 | this._size = undefined |
| 559 | return this |
| 560 | } |
| 561 | |
| 562 | const newChildren = [...this.children] |
| 563 | newChildren.splice(idx, 1) |
| 564 | return new IndexedNode(edit, newBitmap, newChildren) |
| 565 | } |
| 566 | |
| 567 | if (child === newChild) { |
| 568 | return this |
| 569 | } |
| 570 | |
| 571 | if (this.canEdit(edit)) { |
| 572 | this.children[idx] = newChild |
| 573 | return this |
| 574 | } |
| 575 | |
| 576 | const newChildren = [...this.children] |
| 577 | newChildren[idx] = newChild |
| 578 | return new IndexedNode(edit, this.bitmap, newChildren) |
| 579 | } |