All files / src/internal/views tree-set-views.ts

100% Statements 15/15
100% Branches 2/2
100% Functions 12/12
100% Lines 14/14

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79                                131857x       164165x       212x       9x       3622x       57x                               124972x 124972x       93647x       231x         20x 20x       45x       41x      
// The live views of a sorted set: a range view shows the members between two bounds; a reversed
// view shows a set or a range view in the opposite order. Both work on the set's own tree and share
// its change counter, so changes show everywhere and open iterators of any of them fail fast.
 
import type { Comparator } from '../../functions/comparators'
import type { SortedSet } from '../../interfaces/sorted-set'
import { type OrderedTree, reversing } from '../engines/ordered-tree'
import { AbstractSortedSet, type Bound } from '../skeletons/abstract-sorted-set'
 
/**
 * A live view of the members of a sorted set between two bounds, in ascending order.
 *
 * @internal
 */
export class SubSetView<E> extends AbstractSortedSet<E> {
  constructor(tree: OrderedTree<E>, low: Bound<E> | undefined, high: Bound<E> | undefined) {
    super(tree, low, high, false)
  }
 
  get [Symbol.toStringTag](): string {
    return 'TreeSubSet'
  }
 
  get owner(): SortedSet<E> {
    return this
  }
 
  comparator(): Comparator<E> | undefined {
    return this.tree.order
  }
 
  reversed(): SortedSet<E> {
    return new ReversedView(this)
  }
 
  protected openRange(low: Bound<E> | undefined, high: Bound<E> | undefined): SortedSet<E> {
    return new SubSetView(this.tree, low, high)
  }
}
 
/**
 * A live view of an ascending sorted set or range view in descending order. Reversing it returns
 * the set or view it reverses; a range taken from it is the reversed view of the ascending range.
 *
 * @internal
 */
export class ReversedView<E> extends AbstractSortedSet<E> {
  private readonly source: AbstractSortedSet<E>
  private reversing: Comparator<E> | undefined
 
  /** @param source - The ascending set or range view to reverse. */
  constructor(source: AbstractSortedSet<E>) {
    super(source.tree, source.low, source.high, true)
    this.source = source
  }
 
  get [Symbol.toStringTag](): string {
    return 'ReversedTreeSet'
  }
 
  get owner(): SortedSet<E> {
    return this
  }
 
  /** A function that orders in reverse, the same one on every call to this view. */
  comparator(): Comparator<E> {
    if (this.reversing === undefined) this.reversing = reversing(this.tree.order)
    return this.reversing
  }
 
  reversed(): SortedSet<E> {
    return this.source.owner
  }
 
  protected openRange(low: Bound<E> | undefined, high: Bound<E> | undefined): SortedSet<E> {
    return new ReversedView(new SubSetView(this.tree, low, high))
  }
}