1
0
mirror of https://github.com/rkd77/elinks.git synced 2024-12-04 14:46:47 -05:00

[map] Reimplemented map as hash

Maybe it is slower, but C only.
This commit is contained in:
Witold Filipczyk 2023-11-28 19:43:03 +01:00
parent e0909362eb
commit a5d2119dbf
8 changed files with 138 additions and 124 deletions

View File

@ -444,15 +444,13 @@ done_document(struct document *document)
if (document->element_map) {
void *mapa = document->element_map;
clear_map(mapa);
delete_map(mapa);
delete_map(&mapa);
}
if (document->element_map_rev) {
void *mapa = document->element_map_rev;
clear_map_rev(mapa);
delete_map_rev(mapa);
delete_map_rev(&mapa);
}
#endif
free_list(document->tags);

View File

@ -1,6 +1,6 @@
top_builddir=../../..
include $(top_builddir)/Makefile.config
OBJS = corestrings.o css.o doc.o mapa.obj renderer.o renderer2.o
OBJS = corestrings.o css.o doc.o mapa.o renderer.o renderer2.o
include $(top_srcdir)/Makefile.lib

122
src/document/libdom/mapa.c Normal file
View File

@ -0,0 +1,122 @@
/* map temporary file */
#ifdef HAVE_CONFIG_H
#include "config.h"
#endif
#include <stdio.h>
#include "elinks.h"
#include "document/libdom/mapa.h"
#include "util/hash.h"
#include "util/string.h"
void
save_in_map(void *m, void *node, int length)
{
struct hash *mapa = (struct hash *)m;
if (mapa) {
char *key = memacpy((const char *)&length, sizeof(length));
if (key) {
add_hash_item(mapa, key, sizeof(length), node);
}
}
}
void
save_offset_in_map(void *m, void *node, int offset)
{
struct hash *mapa = (struct hash *)m;
if (mapa) {
char *key = memacpy((const char *)node, sizeof(node));
if (key) {
add_hash_item(mapa, key, sizeof(node), (void *)offset);
}
}
}
void *
create_new_element_map(void)
{
return (void *)init_hash8();
}
void *
create_new_element_map_rev(void)
{
return (void *)init_hash8();
}
void
delete_map(void *m)
{
struct hash *hash = (struct hash *)(*(struct hash **)m);
struct hash_item *item;
int i;
foreach_hash_item(item, *hash, i) {
mem_free_set(&item->key, NULL);
}
free_hash(m);
}
void
delete_map_rev(void *m)
{
delete_map(m);
}
void *
find_in_map(void *m, int offset)
{
struct hash *mapa = (struct hash *)m;
struct hash_item *item;
char *key;
if (!mapa) {
return NULL;
}
key = memacpy((const char *)&offset, sizeof(offset));
if (!key) {
return NULL;
}
item = get_hash_item(mapa, key, sizeof(offset));
mem_free(key);
if (!item) {
return NULL;
}
return item->value;
}
int
find_offset(void *m, void *node)
{
struct hash *mapa = (struct hash *)m;
struct hash_item *item;
char *key;
if (!mapa) {
return -1;
}
key = memacpy((const char *)node, sizeof(node));
if (!key) {
return -1;
}
item = get_hash_item(mapa, key, sizeof(node));
mem_free(key);
if (!item) {
return -1;
}
return (int)(item->value);
}

View File

@ -1,100 +0,0 @@
/* map temporary file */
#ifdef HAVE_CONFIG_H
#include "config.h"
#endif
#include <cstddef>
#include <map>
#include "document/libdom/mapa.h"
void
save_in_map(void *m, void *node, int length)
{
std::map<int, void *> *mapa = static_cast<std::map<int, void *> *>(m);
(*mapa)[length] = node;
}
void
save_offset_in_map(void *m, void *node, int offset)
{
std::map<void *, int> *mapa = static_cast<std::map<void *, int> *>(m);
(*mapa)[node] = offset;
}
void *
create_new_element_map(void)
{
std::map<int, void *> *mapa = new std::map<int, void *>;
return (void *)mapa;
}
void *
create_new_element_map_rev(void)
{
std::map<void *, int> *mapa = new std::map<void *, int>;
return (void *)mapa;
}
void
clear_map(void *m)
{
std::map<int, void *> *mapa = static_cast<std::map<int, void *> *>(m);
mapa->clear();
}
void
clear_map_rev(void *m)
{
std::map<void *, int> *mapa = static_cast<std::map<void *, int> *>(m);
mapa->clear();
}
void
delete_map(void *m)
{
std::map<int, void *> *mapa = static_cast<std::map<int, void *> *>(m);
delete mapa;
}
void
delete_map_rev(void *m)
{
std::map<void *, int> *mapa = static_cast<std::map<void *, int> *>(m);
delete mapa;
}
void *
find_in_map(void *m, int offset)
{
std::map<int, void *> *mapa = static_cast<std::map<int, void *> *>(m);
if (!mapa) {
return NULL;
}
auto element = (*mapa).find(offset);
if (element == (*mapa).end()) {
return NULL;
}
return (void *)element->second;
}
int
find_offset(void *m, void *node)
{
std::map<void *, int> *mapa = static_cast<std::map<void *, int> *>(m);
if (!mapa) {
return -1;
}
auto element = (*mapa).find(node);
if (element == (*mapa).end()) {
return -1;
}
return (int)element->second;
}

View File

@ -9,9 +9,7 @@ void save_in_map(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);
void clear_map(void *m);
void delete_map(void *m);
void clear_map_rev(void *m);
void delete_map_rev(void *m);
void *find_in_map(void *m, int offset);

View File

@ -1 +1 @@
srcs += files('corestrings.c', 'css.c', 'doc.c', 'mapa.cpp', 'renderer.c', 'renderer2.c')
srcs += files('corestrings.c', 'css.c', 'doc.c', 'mapa.c', 'renderer.c', 'renderer2.c')

View File

@ -274,13 +274,11 @@ render_source_document_cxx(struct cache_entry *cached, struct document *document
}
}
mapa = document->element_map;
if (!mapa) {
mapa = create_new_element_map();
document->element_map = (void *)mapa;
} else {
clear_map(mapa);
if (mapa) {
delete_map(&mapa);
}
mapa = create_new_element_map();
document->element_map = (void *)mapa;
if (walk_tree(mapa, &document->text, root, true, 0) == false) {
fprintf(stderr, "Failed to complete DOM structure dump.\n");

View File

@ -326,20 +326,18 @@ dump_xhtml(struct cache_entry *cached, struct document *document, int parse)
}
mapa = document->element_map;
if (!mapa) {
mapa = create_new_element_map();
document->element_map = (void *)mapa;
} else {
clear_map(mapa);
if (mapa) {
delete_map(&mapa);
}
mapa = create_new_element_map();
document->element_map = (void *)mapa;
mapa_rev = document->element_map_rev;
if (!mapa_rev) {
mapa_rev = create_new_element_map_rev();
document->element_map_rev = (void *)mapa_rev;
} else {
clear_map(mapa_rev);
if (mapa_rev) {
delete_map_rev(&mapa_rev);
}
mapa_rev = create_new_element_map_rev();
document->element_map_rev = (void *)mapa_rev;
if (walk_tree(mapa, mapa_rev, &document->text, root, true, 0) == false) {
fprintf(stderr, "Failed to complete DOM structure dump.\n");