Skip to content

Commit

Permalink
[libdom] find_offset similar to find_in_map
Browse files Browse the repository at this point in the history
Also with bsearch, but this time elements are explicitly sorted.
  • Loading branch information
rkd77 committed Aug 25, 2024
1 parent 1a26f49 commit d501d76
Show file tree
Hide file tree
Showing 4 changed files with 49 additions and 9 deletions.
8 changes: 2 additions & 6 deletions src/document/document.c
Original file line number Diff line number Diff line change
Expand Up @@ -437,15 +437,11 @@ done_document(struct document *document)
free_document(document->dom);

if (document->element_map) {
void *mapa = document->element_map;

delete_map(mapa);
delete_map(document->element_map);
}

if (document->element_map_rev) {
void *mapa = document->element_map_rev;

delete_map_rev(&mapa);
delete_map(document->element_map_rev);
}
#endif
free_list(document->tags);
Expand Down
45 changes: 43 additions & 2 deletions src/document/libdom/mapa.c
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,12 @@ save_in_map2(void *m, void *node, int length)

void
save_offset_in_map(void *m, void *node, int offset)
{
save_in_map(m, node, offset);
}

void
save_offset_in_map2(void *m, void *node, int offset)
{
struct hash *mapa = (struct hash *)m;

Expand Down Expand Up @@ -94,7 +100,7 @@ create_new_element_map2(void)
void *
create_new_element_map_rev(void)
{
return (void *)init_hash8();
return create_new_element_map();
}

void
Expand All @@ -121,7 +127,6 @@ delete_map2(void *m)
free_hash(m);
}


void
delete_map_rev(void *m)
{
Expand Down Expand Up @@ -179,9 +184,45 @@ find_in_map2(void *m, int offset)
return item->value;
}

static int
compare_nodes(const void *a, const void *b)
{
void *nodea = ((struct el_node_elem *)a)->node;
void *nodeb = ((struct el_node_elem *)b)->node;

return nodea - nodeb;
}

int
find_offset(void *m, void *node)
{
struct el_mapa *mapa = (struct el_mapa *)m;

if (!mapa) {
return -1;
}
struct el_node_elem key = { .offset = -1, .node = node };
struct el_node_elem *item = (struct el_node_elem *)bsearch(&key, mapa->table, mapa->size, sizeof(*item), compare_nodes);

if (!item) {
return -1;
}
return item->offset;
}

void
sort_nodes(void *m)
{
struct el_mapa *mapa = (struct el_mapa *)m;

if (!mapa) {
return;
}
qsort(mapa->table, mapa->size, sizeof(struct el_node_elem), compare_nodes);
}

int
find_offset2(void *m, void *node)
{
struct hash *mapa = (struct hash *)m;
struct hash_item *item;
Expand Down
2 changes: 2 additions & 0 deletions src/document/libdom/mapa.h
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,8 @@ void *find_in_map2(void *m, int offset);

int find_offset(void *m, void *node);

void sort_nodes(void *m);

#ifdef __cplusplus
}
#endif
Expand Down
3 changes: 2 additions & 1 deletion src/document/libdom/renderer2.c
Original file line number Diff line number Diff line change
Expand Up @@ -564,7 +564,7 @@ dump_xhtml(struct cache_entry *cached, struct document *document, int parse)
mapa_rev = document->element_map_rev;

if (mapa_rev) {
delete_map_rev(&mapa_rev);
delete_map(mapa_rev);
}
mapa_rev = create_new_element_map_rev();
document->element_map_rev = (void *)mapa_rev;
Expand All @@ -575,6 +575,7 @@ dump_xhtml(struct cache_entry *cached, struct document *document, int parse)
//dom_node_unref(doc);
return;
}
sort_nodes(mapa_rev);
dom_node_unref(root);

if (parse) {
Expand Down

0 comments on commit d501d76

Please sign in to comment.