From 39148b1b6dc6c7a10f7e713ece084b6d5cf27537 Mon Sep 17 00:00:00 2001 From: Tom Smeding Date: Sun, 16 Feb 2025 20:03:40 +0100 Subject: Initial --- tree.h | 39 +++++++++++++++++++++++++++++++++++++++ 1 file changed, 39 insertions(+) create mode 100644 tree.h (limited to 'tree.h') diff --git a/tree.h b/tree.h new file mode 100644 index 0000000..86a9521 --- /dev/null +++ b/tree.h @@ -0,0 +1,39 @@ +#pragma once + +#include +#include + + +// The data field is a compact representation of a +// [(string, struct tree_node*)] list. The strings are all non-empty. +// Representation: for every list element, first a null-terminated sequence of +// chars (the string), then sizeof(void*) bytes (the pointer, potentially +// NULL). After the last list element, an additional 0 byte, which could be +// interpreted as an empty string, but instead means the end of the list. +struct tree_node { + size_t total_files; + size_t total_dirs; + size_t total_size; // sum of on-disk sizes (in bytes) + uint8_t *data; +}; + +// A single element in a child list. +struct tree_pair { + const char *name; // ownership is retained by the producer of this tree_pair + struct tree_node *sub; // can be NULL +}; + +// The names are copied out of 'pairs'. +struct tree_node* tree_make(const struct tree_pair *pairs, size_t npairs, size_t disk_size); + +// Frees recursively. +void tree_free(struct tree_node *node); + +size_t tree_num_children(const struct tree_node *node); + +struct tree_pair tree_index(struct tree_node *node, size_t index); + +struct tree_iter { struct tree_node *node; size_t off; }; +struct tree_iter tree_iter_start(struct tree_node *node); +// Returns whether there was another child +bool tree_iter_next(struct tree_iter *iter, struct tree_pair *dst); -- cgit v1.2.3-70-g09d2