Skip to content

Commit 5baa869

Browse files
committed
feat: Impl bulk delete and undelete
1 parent 04fe104 commit 5baa869

File tree

1 file changed

+36
-0
lines changed

1 file changed

+36
-0
lines changed

src/id_list.ts

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -570,6 +570,23 @@ export class IdList {
570570
*/
571571
delete(id: ElementId, count = 1) {
572572
checkCount(count);
573+
if (count === 0) return this;
574+
575+
const located = this.locate(id);
576+
if (located === null) return this;
577+
578+
const leaf = located[0].node;
579+
// Check if all ids are in the same leaf, then they can be bulk deleted
580+
if (
581+
leaf.bunchId === id.bunchId &&
582+
id.counter >= leaf.startCounter &&
583+
id.counter + count <= leaf.startCounter + leaf.count
584+
) {
585+
const newPresent = leaf.present.clone();
586+
newPresent.delete(id.counter, count);
587+
588+
return this.replaceLeaf(located, { ...leaf, present: newPresent });
589+
}
573590

574591
// eslint-disable-next-line @typescript-eslint/no-this-alias
575592
let ans: IdList = this;
@@ -624,6 +641,25 @@ export class IdList {
624641
*/
625642
undelete(id: ElementId, count = 1) {
626643
checkCount(count);
644+
if (count === 0) return this;
645+
646+
const located = this.locate(id);
647+
if (located === null) {
648+
throw new Error("id is not known");
649+
}
650+
651+
const leaf = located[0].node;
652+
// Check if all ids are in the same leaf, then they can be bulk undeleted
653+
if (
654+
leaf.bunchId === id.bunchId &&
655+
id.counter >= leaf.startCounter &&
656+
id.counter + count <= leaf.startCounter + leaf.count
657+
) {
658+
const newPresent = leaf.present.clone();
659+
newPresent.set(id.counter, count);
660+
661+
return this.replaceLeaf(located, { ...leaf, present: newPresent });
662+
}
627663

628664
// eslint-disable-next-line @typescript-eslint/no-this-alias
629665
let ans: IdList = this;

0 commit comments

Comments
 (0)