Module
pooka/bytes_tree
src/pooka/bytes_tree.pookaKeep the pieces until somebody asks for the whole parcel. Appending makes a branch in constant time. Rendering walks an explicit work list, preserving chunk order without recursive calls down a deep left spine. Partial final bytes are preserved; this tree accepts arbitrary bitstrings.
assert bytes_tree.to_bit_array(bytes_tree.from_bit_array(<<1, 2>>)) == <<1, 2>>
API reference
class BytesTree
An immutable tree. Only the private leaf and branch classes construct its values.
new() -> BytesTree
fn() -> pooka/bytes_tree.BytesTreeStart without any chunks.
import pooka/bytes_tree
assert bytes_tree.is_empty(bytes_tree.new())
from_bit_array(value: BitArray) -> BytesTree
fn(BitArray) -> pooka/bytes_tree.BytesTreeKeep a chunk without copying its contents.
import pooka/bytes_tree
assert bytes_tree.to_bit_array(bytes_tree.from_bit_array(<<1, 2>>)) == <<1, 2>>
append(tree: BytesTree, value: BitArray) -> BytesTree
fn(pooka/bytes_tree.BytesTree, BitArray) -> pooka/bytes_tree.BytesTreeAttach one chunk in constant time.
assert bytes_tree.to_bit_array(bytes_tree.append(bytes_tree.new(), <<1, 2>>)) == <<1, 2>>
append_tree(left: BytesTree, right: BytesTree) -> BytesTree
Attach another tree without walking either side.
import pooka/bytes_tree
let left = bytes_tree.from_bit_array(<<1:1>>)
let right = bytes_tree.from_bit_array(<<0:1>>)
assert bytes_tree.to_bit_array(bytes_tree.append_tree(left, right)) == <<2:2>>
prepend(tree: BytesTree, value: BitArray) -> BytesTree
fn(pooka/bytes_tree.BytesTree, BitArray) -> pooka/bytes_tree.BytesTreePlace one chunk before the earlier tree.
import pooka/bytes_tree
let tree = bytes_tree.from_bit_array(<<2>>)
assert bytes_tree.to_bit_array(bytes_tree.prepend(tree, <<1>>)) == <<1, 2>>
concat(trees: List(BytesTree)) -> BytesTree
fn(List(pooka/bytes_tree.BytesTree)) -> pooka/bytes_tree.BytesTreeJoin trees in input order without rendering their chunks.
import pooka/bytes_tree
let trees = [bytes_tree.from_bit_array(<<1>>), bytes_tree.from_bit_array(<<2>>)]
assert bytes_tree.to_bit_array(bytes_tree.concat(trees)) == <<1, 2>>
from_bit_arrays(values: List(BitArray)) -> BytesTree
fn(List(BitArray)) -> pooka/bytes_tree.BytesTreeKeep each supplied chunk as a leaf.
import pooka/bytes_tree
assert bytes_tree.to_bit_array(bytes_tree.from_bit_arrays([<<1>>, <<2>>])) == <<1, 2>>
to_bit_array(tree: BytesTree) -> BitArray
fn(pooka/bytes_tree.BytesTree) -> BitArrayRender once, preserving every chunk's order.
import pooka/bytes_tree
assert bytes_tree.to_bit_array(bytes_tree.new()) == <<>>
byte_size(tree: BytesTree) -> Int
fn(pooka/bytes_tree.BytesTree) -> IntByte-oriented size rounds a partial final byte up after rendering.
import pooka/bytes_tree
let tree = bytes_tree.from_bit_arrays([<<1:1>>, <<0:1>>])
assert bytes_tree.byte_size(tree) == 1
bit_size(tree: BytesTree) -> Int
fn(pooka/bytes_tree.BytesTree) -> IntCount every bit without rendering the tree.
import pooka/bytes_tree
assert bytes_tree.bit_size(bytes_tree.from_bit_array(<<1:1>>)) == 1
is_empty(tree: BytesTree) -> Bool
fn(pooka/bytes_tree.BytesTree) -> BoolAsk whether rendering would produce an empty value.
import pooka/bytes_tree
assert bytes_tree.is_empty(bytes_tree.from_bit_array(<<>>))