Compaction
Compaction is a way to efficiently represent a set of A5 cells by replacing groups of sibling cells with their parent cell. This reduces the number of cells needed to represent a region while maintaining complete coverage.
For example, if you have all 4 children of a cell, you can represent them with just their parent cell. The compact() function performs this optimization, and uncompact() reverses it by expanding parent cells back into their children.
compact
Compacts a set of A5 cells by replacing complete groups of sibling cells with their parent cells.
function compact(cells: bigint[] | BigUint64Array): BigUint64Array;
Parameters
cells(bigint[] | BigUint64Array) Array of A5 cell identifiers to compact
Return value
- (BigUint64Array) Compacted array of cell identifiers (typically smaller than input)
Example
import { compact, cellToChildren } from 'a5-js';
// Get 4 sibling cells at resolution 3
const parent = 0x6a80000000000000n; // A cell at resolution 2
const children = cellToChildren(parent);
console.log(children.length); // 4
// Compact them back to the parent
const compacted = compact(children);
console.log(compacted.length); // 1
console.log(compacted[0] === parent); // true
Notes
- The compaction process is recursive - if compacting cells creates complete sibling groups at coarser resolutions, those will also be compacted
- Duplicate cells in the input are automatically removed
- The output is always sorted
- For optimal performance with large datasets, consider using
BigUint64Arrayas input
uncompact
Expands a set of A5 cells to a target resolution by generating all descendant cells.
function uncompact(cells: bigint[] | BigUint64Array, targetResolution: number): BigUint64Array;
Parameters
cells(bigint[] | BigUint64Array) Array of A5 cell identifiers to uncompacttargetResolution(number) The target resolution level for all output cells
Return value
- (BigUint64Array) Array of cell identifiers, all at the target resolution
Example
import { uncompact, getResolution } from 'a5-js';
// Start with a cell at resolution 2
const cell = 0x6a80000000000000n;
// Uncompact to resolution 5 (3 levels finer)
const expanded = uncompact([cell], 5);
console.log(expanded.length); // 64 (4^3)
// All cells are at resolution 5
console.log(getResolution(expanded[0])); // 5
Notes
- All output cells will be at exactly the target resolution
- Cells already at the target resolution are passed through unchanged
- Attempting to uncompact to a coarser resolution throws an error
- The expansion is complete - every descendant cell at the target resolution is included