All files / src position.ts

100% Statements 38/38
100% Branches 13/13
100% Functions 7/7
100% Lines 38/38

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 401x 1x 1x 1x 1x 1x 1x 1x 1x 1x 2143107x 2143107x 2143107x 1x 1x 3199469x 3199469x 1x 1x 1772445x 1772445x 1x 1x 46959x 46959x 1x 1x 37118x 37118x 1x 1x 93534x 93534x 1x 1x 35927x 35927x 1x    
// largest value that is a Smi on all V8 builds, larger values (eg. Number.MAX_SAFE_INTEGER) make V8
// store row/col of every Position as boxed heap numbers, costing ~32 bytes per Position
export const MAX_POSITION_VALUE = 2 ** 30 - 1;
 
// first position is (1,1)
export class Position {
  private readonly row: number;
  private readonly col: number;
 
  public constructor(row: number, col: number) {
    this.row = row;
    this.col = col;
  }
 
  public getCol(): number {
    return this.col;
  }
 
  public getRow(): number {
    return this.row;
  }
 
  public isAfter(p: Position): boolean {
    return this.row > p.row || (this.row === p.row && this.col >= p.col);
  }
 
  public equals(p: Position): boolean {
    return this.row === p.getRow() && this.col === p.getCol();
  }
 
  public isBefore(p: Position): boolean {
    return this.row < p.row || (this.row === p.row && this.col < p.col);
  }
 
  public isBetween(p1: Position, p2: Position): boolean {
    return this.isAfter(p1) && this.isBefore(p2);
  }
}