From 51239e43fd4a3c2440b4574379e8525d75646b3e Mon Sep 17 00:00:00 2001 From: Pierre Habouzit Date: Wed, 14 Mar 2007 00:46:38 +0100 Subject: [PATCH] simplify hashes. --- init.c | 4 ++-- lib-lib/hash.c | 29 +++++++++++++++-------------- lib-lib/hash.h | 10 ++++++---- lib-mx/mh.c | 8 ++++---- lib-mx/mx.c | 4 ++-- nntp/newsrc.c | 11 ++++------- nntp/nntp.c | 6 +++--- thread.c | 14 +++++++------- 8 files changed, 43 insertions(+), 43 deletions(-) diff --git a/init.c b/init.c index e153736..45355c7 100644 --- a/init.c +++ b/init.c @@ -2200,9 +2200,9 @@ void mutt_init (int skip_sys_rc, string_list_t * commands) err.data = error; err.dsize = sizeof(error); - ConfigOptions = hash_create (sizeof(MuttVars) * 2); + ConfigOptions = hash_create (sizeof(MuttVars) * 2, 0); for (i = 0; MuttVars[i].option; i++) { - hash_insert (ConfigOptions, MuttVars[i].option, &MuttVars[i], 0); + hash_insert (ConfigOptions, MuttVars[i].option, &MuttVars[i]); } /* diff --git a/lib-lib/hash.c b/lib-lib/hash.c index eea6cd6..2bfc65a 100644 --- a/lib-lib/hash.c +++ b/lib-lib/hash.c @@ -39,39 +39,40 @@ int hash_string(const unsigned char *s, int n) return (h % n); } -HASH *hash_create(int nelem) +HASH *hash_create(int nelem, int allow_dup) { HASH *table = p_new(HASH, 1); - if (nelem == 0) - nelem = 2; - - table->nelem = nelem; + table->dupes = allow_dup; + table->nelem = MIN(nelem, 2); table->curnelem = 0; - table->table = p_new(struct hash_elem *, nelem); + table->table = p_new(struct hash_elem *, table->nelem); + return table; } -HASH *hash_resize(HASH *ptr, int nelem) +void hash_resize(HASH *ptr, int nelem) { HASH *table; struct hash_elem *elem, *tmp; int i; - table = hash_create (nelem); + /* XXX hack: we know the has was correct, no dupe checks is fater */ + table = hash_create(nelem, 1); for (i = 0; i < ptr->nelem; i++) { for (elem = ptr->table[i]; elem;) { tmp = elem; elem = elem->next; - hash_insert(table, tmp->key, tmp->data, 1); + hash_insert(table, tmp->key, tmp->data); p_delete(&tmp); } } - p_delete(&ptr->table); - p_delete(&ptr); - return table; + p_delete(&ptr->table); + ptr->nelem = table->nelem; + ptr->table = table->table; + p_delete(&table); } /* table hash table to update @@ -79,7 +80,7 @@ HASH *hash_resize(HASH *ptr, int nelem) * data data to associate with `key' * allow_dup if nonzero, duplicate keys are allowed in the table */ -int hash_insert(HASH *table, const char *key, void *data, int allow_dup) +int hash_insert(HASH *table, const char *key, void *data) { struct hash_elem *ptr; int h; @@ -89,7 +90,7 @@ int hash_insert(HASH *table, const char *key, void *data, int allow_dup) ptr->key = key; ptr->data = data; - if (allow_dup) { + if (table->dupes) { ptr->next = table->table[h]; table->table[h] = ptr; table->curnelem++; diff --git a/lib-lib/hash.h b/lib-lib/hash.h index 3fdba21..546b665 100644 --- a/lib-lib/hash.h +++ b/lib-lib/hash.h @@ -32,7 +32,9 @@ struct hash_elem { }; typedef struct { - int nelem, curnelem; + unsigned dupes : 1; + int nelem; + int curnelem; struct hash_elem **table; } HASH; @@ -42,10 +44,10 @@ typedef struct { #define hash_delete(table,key,data,destroy) \ hash_delete_hash(table, hash_string((unsigned char *)key, table->nelem), key, data, destroy) -HASH *hash_create(int nelem); +HASH *hash_create(int nelem, int allow_dup); int hash_string(const unsigned char *s, int n); -int hash_insert(HASH *table, const char *key, void *data, int allow_dup); -HASH *hash_resize(HASH *table, int nelem); +int hash_insert(HASH *table, const char *key, void *data); +void hash_resize(HASH *table, int nelem); void *hash_find_hash(const HASH *table, int hash, const char *key); void hash_delete_hash(HASH *table, int hash, const char *key, const void *data, void (*destroy)(void *)); diff --git a/lib-mx/mh.c b/lib-mx/mh.c index 7225ccc..e1cf230 100644 --- a/lib-mx/mh.c +++ b/lib-mx/mh.c @@ -1472,12 +1472,12 @@ static int maildir_check_mailbox (CONTEXT * ctx, int *index_hint, int unused __a * of each message we scanned. This is used in the loop over the * existing messages below to do some correlation. */ - fnames = hash_create (1031); + fnames = hash_create (1031, 0); for (p = md; p; p = p->next) { maildir_canon_filename (buf, p->h->path, sizeof (buf)); p->canon_fname = m_strdup(buf); - hash_insert (fnames, p->canon_fname, p, 0); + hash_insert (fnames, p->canon_fname, p); } /* check for modifications and adjust flags */ @@ -1610,10 +1610,10 @@ static int mh_check_mailbox (CONTEXT * ctx, int *index_hint, int unused __attrib mhs_free_sequences (&mhs); /* check for modifications and adjust flags */ - fnames = hash_create (1031); + fnames = hash_create (1031, 0); for (p = md; p; p = p->next) - hash_insert (fnames, p->h->path, p, 0); + hash_insert (fnames, p->h->path, p); for (i = 0; i < ctx->msgcount; i++) { ctx->hdrs[i]->active = 0; diff --git a/lib-mx/mx.c b/lib-mx/mx.c index 1d6ebf1..d13a00f 100644 --- a/lib-mx/mx.c +++ b/lib-mx/mx.c @@ -1269,10 +1269,10 @@ void mx_update_context (CONTEXT * ctx, int new_messages) /* add this message to the hash tables */ if (ctx->id_hash && h->env->message_id) - hash_insert (ctx->id_hash, h->env->message_id, h, 0); + hash_insert (ctx->id_hash, h->env->message_id, h); if (!ctx->counting) { if (ctx->subj_hash && h->env->real_subj) - hash_insert (ctx->subj_hash, h->env->real_subj, h, 1); + hash_insert (ctx->subj_hash, h->env->real_subj, h); if (option (OPTSCORE)) mutt_score_message (ctx, h, 0); diff --git a/nntp/newsrc.c b/nntp/newsrc.c index 6d5fad5..4e897f3 100644 --- a/nntp/newsrc.c +++ b/nntp/newsrc.c @@ -64,9 +64,8 @@ static int nntp_parse_newsrc_line (NNTP_SERVER * news, char *line) data->nserv = news; data->deleted = 1; if (news->newsgroups->nelem < news->newsgroups->curnelem * 2) - news->newsgroups = hash_resize (news->newsgroups, news->newsgroups->nelem * 2); - hash_insert (news->newsgroups, data->group, data, 0); + hash_insert (news->newsgroups, data->group, data); nntp_add_to_list (news, data); } else @@ -215,9 +214,8 @@ static int nntp_parse_cacheindex (NNTP_SERVER * news) data->nserv = news; data->deleted = 1; if (news->newsgroups->nelem < news->newsgroups->curnelem * 2) - news->newsgroups = hash_resize (news->newsgroups, news->newsgroups->nelem * 2); - hash_insert (news->newsgroups, data->group, data, 0); + hash_insert (news->newsgroups, data->group, data); nntp_add_to_list (news, data); } data->cache = m_strdup(file); @@ -380,7 +378,7 @@ NNTP_SERVER *mutt_select_newsserver (char *server) serv = p_new(NNTP_SERVER, 1); serv->conn = conn; serv->newsrc = m_strdup(file); - serv->newsgroups = hash_create (1009); + serv->newsgroups = hash_create(1009, false); slurp_newsrc (serv); /* load .newsrc */ nntp_parse_cacheindex (serv); /* load .index */ if (option (OPTNEWSCACHE) && serv->cache && nntp_get_cache_all (serv) >= 0) @@ -963,9 +961,8 @@ NNTP_DATA *mutt_newsgroup_subscribe (NNTP_SERVER * news, char *group) data->nserv = news; data->deleted = 1; if (news->newsgroups->nelem < news->newsgroups->curnelem * 2) - news->newsgroups = hash_resize (news->newsgroups, news->newsgroups->nelem * 2); - hash_insert (news->newsgroups, data->group, data, 0); + hash_insert (news->newsgroups, data->group, data); nntp_add_to_list (news, data); } if (!data->subscribed) { diff --git a/nntp/nntp.c b/nntp/nntp.c index d5b72f7..520ed7f 100644 --- a/nntp/nntp.c +++ b/nntp/nntp.c @@ -831,7 +831,7 @@ static int nntp_open_mailbox (CONTEXT * ctx) nntp_data = xmalloc(sizeof(NNTP_DATA) + m_strlen(buf) + 1); nntp_data->group = (char *) nntp_data + sizeof (NNTP_DATA); strcpy (nntp_data->group, buf); - hash_insert (serv->newsgroups, nntp_data->group, nntp_data, 0); + hash_insert (serv->newsgroups, nntp_data->group, nntp_data); nntp_add_to_list (serv, nntp_data); } ctx->data = nntp_data; @@ -1233,8 +1233,8 @@ static int add_group (char *buf, void *serv) strcpy (nntp_data->group, group); nntp_data->nserv = s; if (s->newsgroups->nelem < s->newsgroups->curnelem * 2) - s->newsgroups = hash_resize (s->newsgroups, s->newsgroups->nelem * 2); - hash_insert (s->newsgroups, nntp_data->group, nntp_data, 0); + hash_resize (s->newsgroups, s->newsgroups->nelem * 2); + hash_insert (s->newsgroups, nntp_data->group, nntp_data); nntp_add_to_list (s, nntp_data); } nntp_data->deleted = 0; diff --git a/thread.c b/thread.c index e34c543..f66afba 100644 --- a/thread.c +++ b/thread.c @@ -689,7 +689,7 @@ void mutt_sort_threads (CONTEXT * ctx, int init) init = 1; if (init) - ctx->thread_hash = hash_create (ctx->msgcount * 2); + ctx->thread_hash = hash_create (ctx->msgcount * 2, 1); /* we want a quick way to see if things are actually attached to the top of the * thread tree or if they're just dangling, so we attach everything to a top @@ -754,7 +754,7 @@ void mutt_sort_threads (CONTEXT * ctx, int init) cur->thread = thread; hash_insert (ctx->thread_hash, cur->env->message_id ? cur->env->message_id : "", - thread, 1); + thread); if (new) { if (new->duplicate_thread) @@ -830,7 +830,7 @@ void mutt_sort_threads (CONTEXT * ctx, int init) if ((new = hash_find (ctx->thread_hash, ref->data)) == NULL) { new = p_new(THREAD, 1); - hash_insert (ctx->thread_hash, ref->data, new, 1); + hash_insert (ctx->thread_hash, ref->data, new); } else { if (new->duplicate_thread) @@ -1196,12 +1196,12 @@ HASH *mutt_make_id_hash (CONTEXT * ctx) HEADER *hdr; HASH *hash; - hash = hash_create (ctx->msgcount * 2); + hash = hash_create (ctx->msgcount * 2, 0); for (i = 0; i < ctx->msgcount; i++) { hdr = ctx->hdrs[i]; if (hdr->env->message_id) - hash_insert (hash, hdr->env->message_id, hdr, 0); + hash_insert (hash, hdr->env->message_id, hdr); } return hash; @@ -1213,12 +1213,12 @@ HASH *mutt_make_subj_hash (CONTEXT * ctx) HEADER *hdr; HASH *hash; - hash = hash_create (ctx->msgcount * 2); + hash = hash_create (ctx->msgcount * 2, 1); for (i = 0; i < ctx->msgcount; i++) { hdr = ctx->hdrs[i]; if (hdr->env->real_subj) - hash_insert (hash, hdr->env->real_subj, hdr, 1); + hash_insert(hash, hdr->env->real_subj, hdr); } return hash; -- 2.20.1