| 465 | } |
| 466 | |
| 467 | set( |
| 468 | edit: number, |
| 469 | shift: number, |
| 470 | hash: number, |
| 471 | key: K, |
| 472 | value: V, |
| 473 | added: { value: boolean } |
| 474 | ): Node<K, V> { |
| 475 | const bit = bitpos(hash, shift) |
| 476 | const idx = index(this.bitmap, bit) |
| 477 | |
| 478 | if ((this.bitmap & bit) !== 0) { |
| 479 | // Existing child - update it |
| 480 | const child = this.children[idx] |
| 481 | const newChild = child.set(edit, shift + SHIFT, hash, key, value, added) |
| 482 | if (child === newChild) { |
| 483 | return this |
| 484 | } |
| 485 | |
| 486 | if (this.canEdit(edit)) { |
| 487 | this.children[idx] = newChild |
| 488 | return this |
| 489 | } |
| 490 | |
| 491 | const newChildren = [...this.children] |
| 492 | newChildren[idx] = newChild |
| 493 | return new IndexedNode(edit, this.bitmap, newChildren) |
| 494 | } else { |
| 495 | // New child - insert |
| 496 | added.value = true |
| 497 | const newChild = new LeafNode(edit, hash, key, value) |
| 498 | const newBitmap = this.bitmap | bit |
| 499 | |
| 500 | if (this.canEdit(edit)) { |
| 501 | this.children.splice(idx, 0, newChild) |
| 502 | this.bitmap = newBitmap |
| 503 | this._size = undefined |
| 504 | |
| 505 | if (this.children.length > MAX_INDEX_NODE) { |
| 506 | return this.expand(edit, newBitmap, this.children) |
| 507 | } |
| 508 | return this |
| 509 | } |
| 510 | |
| 511 | const newChildren = [...this.children] |
| 512 | newChildren.splice(idx, 0, newChild) |
| 513 | |
| 514 | if (newChildren.length > MAX_INDEX_NODE) { |
| 515 | return this.expand(edit, newBitmap, newChildren) |
| 516 | } |
| 517 | |
| 518 | return new IndexedNode(edit, newBitmap, newChildren) |
| 519 | } |
| 520 | } |
| 521 | |
| 522 | remove( |
| 523 | edit: number, |