1
0
mirror of https://github.com/rkd77/elinks.git synced 2024-11-04 08:17:17 -05:00

[libdom] find_offset similar to find_in_map

Also with bsearch, but this time elements are explicitly sorted.
This commit is contained in:
Witold Filipczyk 2024-08-25 12:46:33 +02:00
parent 1a26f492ea
commit d501d762ea
4 changed files with 49 additions and 9 deletions

View File

@ -437,15 +437,11 @@ done_document(struct document *document)
free_document(document->dom); free_document(document->dom);
if (document->element_map) { if (document->element_map) {
void *mapa = document->element_map; delete_map(document->element_map);
delete_map(mapa);
} }
if (document->element_map_rev) { if (document->element_map_rev) {
void *mapa = document->element_map_rev; delete_map(document->element_map_rev);
delete_map_rev(&mapa);
} }
#endif #endif
free_list(document->tags); free_list(document->tags);

View File

@ -52,6 +52,12 @@ save_in_map2(void *m, void *node, int length)
void void
save_offset_in_map(void *m, void *node, int offset) 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; struct hash *mapa = (struct hash *)m;
@ -94,7 +100,7 @@ create_new_element_map2(void)
void * void *
create_new_element_map_rev(void) create_new_element_map_rev(void)
{ {
return (void *)init_hash8(); return create_new_element_map();
} }
void void
@ -121,7 +127,6 @@ delete_map2(void *m)
free_hash(m); free_hash(m);
} }
void void
delete_map_rev(void *m) delete_map_rev(void *m)
{ {
@ -179,9 +184,45 @@ find_in_map2(void *m, int offset)
return item->value; 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 int
find_offset(void *m, void *node) 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 *mapa = (struct hash *)m;
struct hash_item *item; struct hash_item *item;

View File

@ -31,6 +31,8 @@ void *find_in_map2(void *m, int offset);
int find_offset(void *m, void *node); int find_offset(void *m, void *node);
void sort_nodes(void *m);
#ifdef __cplusplus #ifdef __cplusplus
} }
#endif #endif

View File

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