Module

pooka/bytes_tree

src/pooka/bytes_tree.pooka

Keep 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.

Pooka
assert bytes_tree.to_bit_array(bytes_tree.from_bit_array(<<1, 2>>)) == <<1, 2>>

API reference

class

BytesTree

src/pooka/bytes_tree.pooka:22
class BytesTree

An immutable tree. Only the private leaf and branch classes construct its values.

function

new

src/pooka/bytes_tree.pooka:37
new() -> BytesTree

Start without any chunks.

Pooka
import pooka/bytes_tree

assert bytes_tree.is_empty(bytes_tree.new())
function

from_bit_array

src/pooka/bytes_tree.pooka:48
from_bit_array(value: BitArray) -> BytesTree
INFERREDfn(BitArray) -> pooka/bytes_tree.BytesTree

Keep a chunk without copying its contents.

Pooka
import pooka/bytes_tree

assert bytes_tree.to_bit_array(bytes_tree.from_bit_array(<<1, 2>>)) == <<1, 2>>
function

append

src/pooka/bytes_tree.pooka:57
append(tree: BytesTree, value: BitArray) -> BytesTree

Attach one chunk in constant time.

Pooka
assert bytes_tree.to_bit_array(bytes_tree.append(bytes_tree.new(), <<1, 2>>)) == <<1, 2>>
function

append_tree

src/pooka/bytes_tree.pooka:70
append_tree(left: BytesTree, right: BytesTree) -> BytesTree

Attach another tree without walking either side.

Pooka
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>>
function

prepend

src/pooka/bytes_tree.pooka:82
prepend(tree: BytesTree, value: BitArray) -> BytesTree

Place one chunk before the earlier tree.

Pooka
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>>
function

concat

src/pooka/bytes_tree.pooka:94
concat(trees: List(BytesTree)) -> BytesTree

Join trees in input order without rendering their chunks.

Pooka
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>>
function

from_bit_arrays

src/pooka/bytes_tree.pooka:105
from_bit_arrays(values: List(BitArray)) -> BytesTree
INFERREDfn(List(BitArray)) -> pooka/bytes_tree.BytesTree

Keep each supplied chunk as a leaf.

Pooka
import pooka/bytes_tree

assert bytes_tree.to_bit_array(bytes_tree.from_bit_arrays([<<1>>, <<2>>])) == <<1, 2>>
function

to_bit_array

src/pooka/bytes_tree.pooka:116
to_bit_array(tree: BytesTree) -> BitArray
INFERREDfn(pooka/bytes_tree.BytesTree) -> BitArray

Render once, preserving every chunk's order.

Pooka
import pooka/bytes_tree

assert bytes_tree.to_bit_array(bytes_tree.new()) == <<>>
function

byte_size

src/pooka/bytes_tree.pooka:128
byte_size(tree: BytesTree) -> Int
INFERREDfn(pooka/bytes_tree.BytesTree) -> Int

Byte-oriented size rounds a partial final byte up after rendering.

Pooka
import pooka/bytes_tree

let tree = bytes_tree.from_bit_arrays([<<1:1>>, <<0:1>>])
assert bytes_tree.byte_size(tree) == 1
function

bit_size

src/pooka/bytes_tree.pooka:139
bit_size(tree: BytesTree) -> Int
INFERREDfn(pooka/bytes_tree.BytesTree) -> Int

Count every bit without rendering the tree.

Pooka
import pooka/bytes_tree

assert bytes_tree.bit_size(bytes_tree.from_bit_array(<<1:1>>)) == 1
function

is_empty

src/pooka/bytes_tree.pooka:152
is_empty(tree: BytesTree) -> Bool
INFERREDfn(pooka/bytes_tree.BytesTree) -> Bool

Ask whether rendering would produce an empty value.

Pooka
import pooka/bytes_tree

assert bytes_tree.is_empty(bytes_tree.from_bit_array(<<>>))