UNPKG

@northguild/gmt

Version:

Date and time library for JavaScript and TypeScript, built on TC39 Temporal: time zones, DST, business days, ISO 8601 strings in and out. Never throws.

150 lines • 7.33 kB
import { DutyLogEntry, DutyRun } from "./dutyLog.js"; import { ResolvedPair, StatusSet } from "./dutyRules.js"; /** * Which entries a view of the log counts: the entries in a set, the entries outside one, or the * entries outside one and inside another (`within`; null for any status). */ export type StatusKey = StatusSet | { not: StatusSet; within?: StatusSet | null; }; /** One status test's view of the whole log, built once per call. */ export type RunIndex = { entries: readonly DutyLogEntry[]; entryStarts: readonly bigint[]; /** `matched[i]`: whether `entries[i]` passes the test. */ matched: readonly boolean[]; /** `entryPrefix[i]`: time in the test over `entries[0..i)`; one longer than `entries`. */ entryPrefix: readonly bigint[]; /** The runs of the whole log, as today's `runsOf` gives them. */ runs: readonly DutyRun[]; runStarts: readonly bigint[]; runEnds: readonly bigint[]; /** Per-`(minimum)` and per-`(minimum, maximum)` arrays, built on first use. */ qualifying: Map<bigint, readonly number[]>; events: Map<string, EventList>; interruptions: Map<string, InterruptionStates>; /** Per-`(fullRest, pair)` tables of the whole log's rest pairs, built on first use. */ pairTables: Map<string, PairTable>; }; /** * Every rest pair of the whole log: `seconds[k]` and `firsts[k]` are the run indexes of the k-th * pair, in order, and `previous[i]` is the run a pair ending at run `i` would start from (-1 for * none). Firsts never decrease along the pairs. */ type PairTable = { seconds: readonly number[]; firsts: readonly number[]; previous: readonly number[]; }; /** The runs of the whole log whose length is in a range: their run indexes and starts. */ type EventList = { indexes: readonly number[]; starts: readonly bigint[]; }; /** The log and the indexes built from it, kept for one call. */ export type RunStore = { entries: readonly DutyLogEntry[]; entryStarts: readonly bigint[]; positive: Map<StatusSet, RunIndex>; complement: Map<StatusSet, Map<StatusSet | null, RunIndex>>; /** `nextMatch(set)[i]`: the first entry at or after `i` in the set, or `entries.length`. */ nextMatch: Map<StatusSet, readonly number[]>; }; /** The first `count` runs of an index, the last cut at the instant: the log as it stood there. */ export type RunView = { index: RunIndex; count: number; last: DutyRun | undefined; }; /** * The first position in `[0, count)` where `holds` is true, given it is false and then true; `count` * when it never is. */ export declare function firstWhere(count: number, holds: (position: number) => boolean): number; /** Maximal runs of touching entries passing `matches`, merged as `mergeIntervals` merges them. */ export declare function runsMatching(entries: readonly DutyLogEntry[], matches: (status: string) => boolean): DutyRun[]; /** * A call's store: the entries and, as tests ask for them, an index of each. * * @param entries the parsed log, ordered and separate */ export declare function createRunStore(entries: readonly DutyLogEntry[]): RunStore; /** * The index of a test, built on first use and kept: a set or the complement of one is keyed by its * set object, which the rules hold once per call. */ export declare function runIndex(store: RunStore, test: StatusKey): RunIndex; /** * Time in the test inside `[from, to)` of the log as it stood at `at`: entries starting at or after * `at` are gone and the one straddling it ends there. * * @example timeInBefore(index, 0n, 100n, 10n) // time in the test inside [0, 10) */ export declare function timeInBefore(index: RunIndex, from: bigint, to: bigint, at: bigint): bigint; /** The runs of the log as it stood at `at`: no copy, the last cut there. */ export declare function runsUntil(index: RunIndex, at: bigint): RunView; /** The `position`-th run of a view, the last one cut. */ export declare function viewRun(view: RunView, position: number): DutyRun; /** The first position in a view whose run ends after `t`; the view's count when none does. */ export declare function firstEndingAfter(view: RunView, t: bigint): number; /** The first position in a view whose run starts at or after `t`; the view's count when none. */ export declare function firstStartingAtOrAfter(view: RunView, t: bigint): number; /** * The latest of a view's first `taken` runs that is at least `minimum` long. The view's last run * counts at its cut length. */ export declare function latestAtLeast(view: RunView, taken: number, minimum: bigint): DutyRun | undefined; /** The latest run in a view at least `minimum` long. */ export declare function lastAtLeastUntil(view: RunView, minimum: bigint): DutyRun | undefined; /** How many of a view's first runs end at or before `t`. */ export declare function endingAtOrBefore(view: RunView, t: bigint): number; /** The run of a view that ends at `t`, if one does. */ export declare function runEndingAt(view: RunView, t: bigint): DutyRun | undefined; /** * How many of a view's runs of at least `minimum` and under `maximum` (`null`: no upper bound) * start in `[from, to)` (`null`: no upper bound). The view's last run counts at its cut length, * and not at all when it ends at `skipEndingAt`. */ export declare function countEvents(view: RunView, bounds: { minimum: bigint; maximum: bigint | null; }, range: { from: bigint; to: bigint | null; }, skipEndingAt: bigint | null): number; /** The start of the log's first entry, when it starts before `at`. */ export declare function logStartBefore(store: RunStore, at: bigint): bigint | undefined; /** The log's last entry as it stood at `at`, cut there. */ export declare function lastEntryBefore(store: RunStore, at: bigint): DutyLogEntry | undefined; /** The entry holding the instant `at`, if one does (start ≤ at < end). */ export declare function entryHolding(store: RunStore, at: bigint): DutyLogEntry | undefined; /** * The first entry in `statuses` that starts at or after `from` (`null`: any) and before `at`. */ export declare function firstEntryIn(store: RunStore, statuses: StatusSet, from: bigint | null, at: bigint): DutyLogEntry | undefined; /** Where a required-interruption count stands after each break of the log. */ type InterruptionStates = { resetAt: readonly (bigint | null)[]; part: readonly number[]; }; /** * Where a required-interruption count was reset, taking every break a view holds in order: a break * of `minimum` resets it, and breaks that meet `split` one after another in order reset it too. * * @returns the instant of the last reset, or null when none */ export declare function interruptionResetAt(view: RunView, minimum: bigint, split: readonly bigint[] | null): bigint | null; /** * The latest rest pair among a view's runs from `from` on: what the last of * `restPairs(viewRunsFrom(view, from), fullRest, pair)` is, without walking the runs. Only the * view's last run is cut, so it is tried by itself and every earlier pair comes from the whole-log * table. */ export declare function latestPairFrom(view: RunView, from: number, fullRest: bigint, pair: ResolvedPair): { first: DutyRun; second: DutyRun; } | undefined; /** How many of the ascending values are in `[low, high)`. */ export declare function countInRange(sorted: readonly bigint[], low: bigint, high: bigint): number; export {};