Create an empty binary indexed tree. Count, add, sum and selection take O(log(capacity + 1)).
Construction and clearing take O(capacity + 1) and counters occupy 4(capacity + 1) bytes.
@throws ― {FrequencyTableInvalidInputError} INVALID_CAPACITY unless capacity is an integer in [0, 2^31 - 1].
Create an empty binary indexed tree. Count, add, sum and selection take O(log(capacity + 1)).
Construction and clearing take O(capacity + 1) and counters occupy 4(capacity + 1) bytes.
@throws ― {FrequencyTableInvalidInputError} INVALID_CAPACITY unless capacity is an integer in [0, 2^31 - 1].
Add delta occurrences of key, defaulting to one. Negative deltas remove occurrences.
@throws ― {FrequencyTableInvalidInputError} INVALID_INDEX for an invalid key, INVALID_DELTA for a delta that is
not a safe integer, COUNT_UNDERFLOW for a negative resulting frequency, or COUNT_OVERFLOW for a total above 2^32 - 1.
A rejected update leaves all counts unchanged.
Add delta occurrences of key, defaulting to one. Negative deltas remove occurrences.
@throws ― {FrequencyTableInvalidInputError} INVALID_INDEX for an invalid key, INVALID_DELTA for a delta that is
not a safe integer, COUNT_UNDERFLOW for a negative resulting frequency, or COUNT_OVERFLOW for a total above 2^32 - 1.
A rejected update leaves all counts unchanged.
Add delta occurrences of key, defaulting to one. Negative deltas remove occurrences.
@throws ― {FrequencyTableInvalidInputError} INVALID_INDEX for an invalid key, INVALID_DELTA for a delta that is
not a safe integer, COUNT_UNDERFLOW for a negative resulting frequency, or COUNT_OVERFLOW for a total above 2^32 - 1.
A rejected update leaves all counts unchanged.
add(9)
constcounts:FrequencyTable
counts.
FrequencyTable.count: (key:number) => number
Read the exact frequency of key. An unobserved key has count zero.
@throws ― {FrequencyTableInvalidInputError} INVALID_INDEX for a key outside the integer domain.
count(2) // 2
// Sorted observations are [2, 2, 7, 9].
constcounts:FrequencyTable
counts.
FrequencyTable.at: (position:number) => number
Read the key at a zero-based position, counting duplicates. Negative positions count from the end.
@throws ― {FrequencyTableInvalidInputError} INVALID_INDEX for a noninteger position or one outside [-size, size).
at(
var Math:Math
An intrinsic object that provides basic mathematics functionality and constants.
Math.
Math.floor(x: number): number
Returns the greatest integer less than or equal to its numeric argument.
@param ― x A numeric expression.
floor((
constcounts:FrequencyTable
counts.
FrequencyTable.size: number
Sum of all frequencies, including repeated occurrences. O(1).
size-1) /2)) // 2, the lower median
constcounts:FrequencyTable
counts.
FrequencyTable.rangeSum: (start:number, end:number) => number
Sum frequencies of keys in [start, end).
@throws ― {FrequencyTableInvalidInputError} INVALID_RANGE for noninteger, reversed, or out-of-domain bounds.
Add delta occurrences of key, defaulting to one. Negative deltas remove occurrences.
@throws ― {FrequencyTableInvalidInputError} INVALID_INDEX for an invalid key, INVALID_DELTA for a delta that is
not a safe integer, COUNT_UNDERFLOW for a negative resulting frequency, or COUNT_OVERFLOW for a total above 2^32 - 1.
A rejected update leaves all counts unchanged.
add(2, -1)
constcounts:FrequencyTable
counts.
FrequencyTable.at: (position:number) => number
Read the key at a zero-based position, counting duplicates. Negative positions count from the end.
@throws ― {FrequencyTableInvalidInputError} INVALID_INDEX for a noninteger position or one outside [-size, size).
The flat layout scans the prefix. Fenwick sums one partial counter per set bit in the prefix length.
Hover or focus a chart for exact values. Each change reruns demo.ts against knotkit/frequency-table in your browser; the defaults reproduce the README figure.
Every error this module throws is FrequencyTableInvalidInputError, with a code from a closed, exported list. Branch on the code; the message is for people.
Use knotkit/frequency-table for this task. If Knotkit is missing, follow https://knotkit.jkomyno.dev/getting-started.md#install-from-source. Import from 'knotkit/frequency-table'.
Exports:
- `fenwickFrequencyTable` → `FrequencyTable`: Binary indexed tree of exact nonnegative integer frequencies.
- `flatFrequencyTable` → `FrequencyTable`: One unsigned counter per key. Range sums and rank selection scan counters.
Pick the layout by these rules:
- `fenwickFrequencyTable` (kind 'fenwick') when prefix sums or rank selections repeatedly span many keys between updates
- `flatFrequencyTable` (kind 'flat') when the domain has few keys or the workload primarily updates and reads individual counts
Use something else when:
- rank keys under a custom comparator or store payloads → knotkit/ordered-map
- membership and set algebra on unique unsigned IDs → knotkit/bitset
- prefix sums over immutable signed values → a prefix-sum array
- estimate per-key frequencies in a stream with bounded memory → knotkit/scalar-sketch
Errors are FrequencyTableInvalidInputError with a `code` property. Branch on `error.code`, never on the message.
Full page: https://knotkit.jkomyno.dev/modules/frequency-table.md
Types: node_modules/knotkit/frequency-table/ and https://knotkit.jkomyno.dev/reference/knotkit/frequency-table/