itacart.serialization.tree_blob
TreeBlob: dense binary encoding of a compositional index as a cell set.
Canonical prefix-index form, content-addressable. A resolution-13 leaf occupies exactly 10 bytes.
Formal properties: determinism, decode(encode(x)) == recompose(x),
encode(x) == encode(recompose(x)), idempotent recomposition,
bit-exact round trip, content-addressability, 10 bytes at resolution 13,
and monotone prefix_at_resolution_binary().
The encoding depends only on the index being prefix-structured with a known per-level alphabet, so it ports to other DGGS with little change.
Wire format
A tree blob encodes a whole compositional tree as one bytes:
[magic = 0xC7] 1 byte
[version (4 bits) | flags (4 bits)] 1 byte
[quadrant (2 bits) | reserved (6)] 1 byte
[resolution-1 child count, 16 bits] 2 bytes
[bit-packed children, pre-order] variable
Packing is big-endian, MSB-first within every byte. Each node carries a child-count field whose width follows the resolution of its children; a count of zero marks a leaf.
A node blob encodes one root-to-cell path and is the form returned by
iter_leaves():
[0xA0 | resolution] 1 byte
[quadrant + column/row + refinements] variable
Index space
Columns and rows are written as they appear in the index, without
offset. Column 0 is the meridian triangle and exists only in the
eastern quadrants; column 2004 is a prefix that spells the fifth
child of a trapezoidal cell in the last column, and is never a
resolution-1 cell of its own. The refinement ratio of a level is not the
number of children a node has: a trapezoidal parent yields one more than
the ratio, at every depth, so the count field is bounded by its own
width and not by the ratio.
Row 1000 holds the two polar caps, NE(0000/1000) and
SE(0000/1000). The grid has them; this version of the binary codecs
does not serialize them. That is a restriction of the codecs and not a
property of the grid, so a row-1000 index is refused with a message that
says so, while a row past it is refused as outside the grid, which it is.
The GeometryBlob reads its vertices through the same parser and refuses
the caps with the same message.
Module constants
Greatest column that is a resolution-1 cell in its own right. |
|
Greatest column the grammar admits. |
|
Greatest row this version of the codecs writes. |
|
The row of the two polar caps, one past |
|
Magic, version and the quadrant-group count. |
|
Resolution of the synthetic root that holds one node per quadrant. |
|
Quadrants holding the meridian triangle. |
Functions
Count leaves in a blob without materialising them. |
|
Decode a standalone node. |
|
Decode a TreeBlob into a compositional index. |
|
Decode a blob to the index it stores. |
|
Encode one atomic index as a standalone node. |
|
Encode a compositional index as a TreeBlob. |
|
Test ancestry directly on encoded nodes. |
|
Stream encoded leaf nodes without decoding to text. |
|
Truncate an encoded node to a coarser resolution. |
|
Rewrite an index in canonical prefix form. |
|
Resolution of an encoded node. |
|
Encode an index, compacting first by default. |
|
Validate blob structure, raising on the first problem found. |