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

[css] Modified find_in_map. Refs #317

Instead of hash, array which can be binary searched.
Performance is not as good as original css, but is tolerable.
This commit is contained in:
Witold Filipczyk 2024-08-24 19:22:35 +02:00
parent 0c28f1b7cc
commit cdada4b04d
4 changed files with 103 additions and 3 deletions

View File

@ -439,7 +439,7 @@ done_document(struct document *document)
if (document->element_map) {
void *mapa = document->element_map;
delete_map(&mapa);
delete_map(mapa);
}
if (document->element_map_rev) {

View File

@ -14,6 +14,30 @@
void
save_in_map(void *m, void *node, int length)
{
struct el_mapa *mapa = (struct el_mapa *)m;
if (!mapa) {
return;
}
if (mapa->size == mapa->allocated) {
struct el_node_elem *tmp = mem_realloc(mapa->table, mapa->size * 2 * sizeof(*tmp));
if (!tmp) {
return;
}
mapa->table = tmp;
mapa->allocated = mapa->size * 2;
}
mapa->table[mapa->size].offset = length;
mapa->table[mapa->size].node = node;
mapa->size++;
}
void
save_in_map2(void *m, void *node, int length)
{
struct hash *mapa = (struct hash *)m;
@ -42,6 +66,27 @@ save_offset_in_map(void *m, void *node, int offset)
void *
create_new_element_map(void)
{
struct el_mapa *mapa = (struct el_mapa *)mem_calloc(1, sizeof(*mapa));
if (!mapa) {
return NULL;
}
struct el_node_elem *tmp = mem_calloc(256, sizeof(*tmp));
if (!tmp) {
mem_free(mapa);
return NULL;
}
mapa->table = tmp;
mapa->size = 0;
mapa->allocated = 256;
return mapa;
}
void *
create_new_element_map2(void)
{
return (void *)init_hash8();
}
@ -54,6 +99,17 @@ create_new_element_map_rev(void)
void
delete_map(void *m)
{
struct el_mapa *mapa = (struct el_mapa *)m;
if (mapa) {
mem_free(mapa->table);
mem_free(mapa);
}
}
void
delete_map2(void *m)
{
struct hash *hash = (struct hash *)(*(struct hash **)m);
struct hash_item *item;
@ -65,14 +121,41 @@ delete_map(void *m)
free_hash(m);
}
void
delete_map_rev(void *m)
{
delete_map(m);
delete_map2(m);
}
static int
compare(const void *a, const void *b)
{
int offa = ((struct el_node_elem *)a)->offset;
int offb = ((struct el_node_elem *)b)->offset;
return offa - offb;
}
void *
find_in_map(void *m, int offset)
{
struct el_mapa *mapa = (struct el_mapa *)m;
if (!mapa) {
return;
}
struct el_node_elem key = { .offset = offset, .node = NULL };
struct el_node_elem *item = (struct el_node_elem *)bsearch(&key, mapa->table, mapa->size, sizeof(*item), compare);
if (!item) {
return NULL;
}
return item->node;
}
void *
find_in_map2(void *m, int offset)
{
struct hash *mapa = (struct hash *)m;
struct hash_item *item;
@ -96,6 +179,7 @@ find_in_map(void *m, int offset)
return item->value;
}
int
find_offset(void *m, void *node)
{

View File

@ -5,7 +5,20 @@
extern "C" {
#endif
struct el_node_elem {
int offset;
void *node;
};
struct el_mapa {
unsigned int size;
unsigned int allocated;
struct el_node_elem *table;
};
void save_in_map(void *m, void *node, int length);
void save_in_map2(void *m, void *node, int length);
void save_offset_in_map(void *m, void *node, int offset);
void *create_new_element_map(void);
void *create_new_element_map_rev(void);
@ -13,6 +26,9 @@ void delete_map(void *m);
void delete_map_rev(void *m);
void *find_in_map(void *m, int offset);
void *find_in_map2(void *m, int offset);
int find_offset(void *m, void *node);
#ifdef __cplusplus

View File

@ -557,7 +557,7 @@ dump_xhtml(struct cache_entry *cached, struct document *document, int parse)
mapa = document->element_map;
if (mapa) {
delete_map(&mapa);
delete_map(mapa);
}
mapa = create_new_element_map();
document->element_map = (void *)mapa;