#include "dom.h" #include "hash.h" union { char buf[8]; usize value; } static const attr_map = { .buf = "attrmap_" }; pdom_attribute_map_t pdom_attribute_map(void) { return (pdom_attribute_map_t) {}; } void pd_attr_put(pdom_attribute_map_t map[static const 1], pcow_t key, pcow_t value) { usize hash = phash(key.c_str, key.length, attr_map.value); if (map->buckets[hash % 32]) { struct pdom_attribute_bucket_t *stretchy bucket = map->buckets[hash % 32]; psb_foreach(bucket, key_value) { if (key_value->hash == hash && pcow_equal(key_value->key, key)) { key_value->value = value; return; } } psb_pushback(bucket, ((struct pdom_attribute_bucket_t){ .hash = hash, .key = key, .value = value })); map->buckets[hash % 32] = bucket; } else { psb_pushback(map->buckets[hash % 32], ((struct pdom_attribute_bucket_t){ .hash = hash, .key = key, .value = value })); } } pcow_t pd_attr_get(const pdom_attribute_map_t map[static const 1], pcow_t key) { usize hash = phash(key.c_str, key.length, attr_map.value); if (map->buckets[hash % 32]) { struct pdom_attribute_bucket_t *stretchy bucket = map->buckets[hash % 32]; psb_foreach(bucket, key_value) { if (key_value->hash == hash && pcow_equal(key_value->key, key)) { return key_value->value; } } } return pcow("", 0); } pdom_attribute_iter_t pd_attr_iter(const pdom_attribute_map_t map[static const 1]) { if (!map->num_items) return (pdom_attribute_iter_t){}; for (usize bucket_index = 0; bucket_index < 32; bucket_index++) { struct pdom_attribute_bucket_t *current = map->buckets[bucket_index]; if (!current) continue; return (pdom_attribute_iter_t) { PDOM_ATTR_ITER_SOME, bucket_index, 0, current->key, current->value }; } // shouldn't be possible, if we have 0 items we early exit return (pdom_attribute_iter_t){}; } bool pd_attr_iter_next(const pdom_attribute_map_t map[static const 1], pdom_attribute_iter_t it[static const 1]) { if (!map->num_items) return false; if (it->kind == PDOM_ATTR_ITER_NONE) return false; bool using_next_bucket = false; for (usize bucket_index = it->bucket_index; bucket_index < 32; bucket_index++) { struct pdom_attribute_bucket_t *current = map->buckets[bucket_index]; if (!current) { using_next_bucket = true; continue; } if (using_next_bucket) { *it = (pdom_attribute_iter_t) { PDOM_ATTR_ITER_SOME, bucket_index, 0, current->key, current->value }; return true; } else if (it->index + 1 < psb_size(current)) { usize new_index = it->index + 1; *it = (pdom_attribute_iter_t) { PDOM_ATTR_ITER_SOME, bucket_index, new_index, current[new_index].key, current[new_index].value }; return true; } else { using_next_bucket = true; } } return false; } pdom_node_t pdom_text_node(pcow_t text) { return (pdom_node_t) { .kind = PDOM_NODE_TEXT, .text = text }; } pdom_node_t pdom_element_node(pcow_t tag_name, pdom_attribute_map_t attr, pdom_node_t *stretchy children) { return (pdom_node_t) { .kind = PDOM_NODE_ELEMENT, .children = children, .element = { tag_name, attr } }; } void pdom_debug_print_node_tabulated(pdom_node_t *node, usize num_tabs) { for (usize i = 0; i < num_tabs; i++) { printf(" "); } switch (node->kind) { case PDOM_NODE_TEXT: printf("TEXT_NODE: \"%*.*s\"\n", (int)node->text.length, (int)node->text.length, node->text.c_str ); break; case PDOM_NODE_ELEMENT: printf("ELEMENT_NODE: %*.*s\n", (int)node->element.tag_name.length, (int)node->element.tag_name.length, node->element.tag_name.c_str ); for (usize i = 0; i < num_tabs; i++) { printf(" "); } printf("attributes:\n"); for (pdom_attribute_iter_t it = pd_attr_iter(&node->element.attr); it.kind == PDOM_ATTR_ITER_SOME; pd_attr_iter_next(&node->element.attr, &it)) { for (usize i = 0; i < num_tabs; i++) { printf(" "); } printf("%*.*s: %*.*s\n", (int)it.key.length, (int)it.key.length, it.key.c_str, (int)it.value.length, (int)it.value.length, it.value.c_str ); } for (usize i = 0; i < num_tabs; i++) { printf(" "); } printf("children:\n"); break; } psb_foreach(node->children, child) { pdom_debug_print_node_tabulated(child, num_tabs); } } void pdom_debug_print_node(pdom_node_t *node) { pdom_debug_print_node_tabulated(node, 0); }