* [NOTICE]: Performance-sensitive for large data.
(
dims: DimensionIndex[],
cb: FilterCb
)
| 604 | * [NOTICE]: Performance-sensitive for large data. |
| 605 | */ |
| 606 | filter( |
| 607 | dims: DimensionIndex[], |
| 608 | cb: FilterCb |
| 609 | ): DataStore { |
| 610 | if (!this._count) { |
| 611 | return this; |
| 612 | } |
| 613 | |
| 614 | const newStore = this.clone(); |
| 615 | |
| 616 | const count = newStore.count(); |
| 617 | const Ctor = getIndicesCtor(newStore._rawCount); |
| 618 | const newIndices = new Ctor(count); |
| 619 | const value = []; |
| 620 | const dimSize = dims.length; |
| 621 | |
| 622 | let offset = 0; |
| 623 | const dim0 = dims[0]; |
| 624 | const chunks = newStore._chunks; |
| 625 | |
| 626 | for (let i = 0; i < count; i++) { |
| 627 | let keep; |
| 628 | const rawIdx = newStore.getRawIndex(i); |
| 629 | // Simple optimization |
| 630 | if (dimSize === 0) { |
| 631 | keep = (cb as FilterCb0)(i); |
| 632 | } |
| 633 | else if (dimSize === 1) { |
| 634 | const val = chunks[dim0][rawIdx]; |
| 635 | keep = (cb as FilterCb1)(val, i); |
| 636 | } |
| 637 | else { |
| 638 | let k = 0; |
| 639 | for (; k < dimSize; k++) { |
| 640 | value[k] = chunks[dims[k]][rawIdx]; |
| 641 | } |
| 642 | value[k] = i; |
| 643 | keep = (cb as FilterCb).apply(null, value); |
| 644 | } |
| 645 | if (keep) { |
| 646 | newIndices[offset++] = rawIdx; |
| 647 | } |
| 648 | } |
| 649 | |
| 650 | // Set indices after filtered. |
| 651 | if (offset < count) { |
| 652 | newStore._indices = newIndices; |
| 653 | } |
| 654 | newStore._count = offset; |
| 655 | // Reset data extent |
| 656 | newStore._extent = []; |
| 657 | |
| 658 | newStore._updateGetRawIdx(); |
| 659 | |
| 660 | return newStore; |
| 661 | } |
| 662 | |
| 663 | /** |
no test coverage detected