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

MAX_CELL_COLUMN

Greatest column that is a resolution-1 cell in its own right.

MAX_PREFIX_COLUMN

Greatest column the grammar admits.

MAX_ROW

Greatest row this version of the codecs writes.

POLAR_CAP_ROW

The row of the two polar caps, one past MAX_ROW.

HEADER_SIZE_BYTES

Magic, version and the quadrant-group count.

GLOBE_RESOLUTION

Resolution of the synthetic root that holds one node per quadrant.

EASTERN_QUADRANTS

Quadrants holding the meridian triangle.

Functions

count_vertices

Count leaves in a blob without materialising them.

decode_node

Decode a standalone node.

decode_tree

Decode a TreeBlob into a compositional index.

deserialize_from_blob

Decode a blob to the index it stores.

encode_node

Encode one atomic index as a standalone node.

encode_tree

Encode a compositional index as a TreeBlob.

is_ancestor_binary

Test ancestry directly on encoded nodes.

iter_leaves

Stream encoded leaf nodes without decoding to text.

prefix_at_resolution_binary

Truncate an encoded node to a coarser resolution.

recompose_to_prefix_form

Rewrite an index in canonical prefix form.

resolution_of_binary

Resolution of an encoded node.

serialize_to_blob

Encode an index, compacting first by default.

validate_tree

Validate blob structure, raising on the first problem found.