mirror of
https://github.com/rkd77/elinks.git
synced 2025-02-02 15:09:23 -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:
parent
1a26f492ea
commit
d501d762ea
@ -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);
|
||||
|
@ -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;
|
||||
|
||||
@ -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
|
||||
@ -121,7 +127,6 @@ delete_map2(void *m)
|
||||
free_hash(m);
|
||||
}
|
||||
|
||||
|
||||
void
|
||||
delete_map_rev(void *m)
|
||||
{
|
||||
@ -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;
|
||||
|
@ -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
|
||||
|
@ -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;
|
||||
@ -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) {
|
||||
|
Loading…
x
Reference in New Issue
Block a user