]> granicus.if.org Git - neomutt/commitdiff
Change "allow_dups" into a flag at hash creation.
authorKevin McCarthy <kevin@8t8.us>
Fri, 10 Feb 2017 21:01:21 +0000 (13:01 -0800)
committerRichard Russon <rich@flatcap.org>
Mon, 20 Feb 2017 16:49:41 +0000 (16:49 +0000)
Instead of having an "allow_dups" parameter for hash_insert(), add a
flag, MUTT_HASH_ALLOW_DUPS, to hash_create().

Currently ReverseAlias, subj_hash, and thread_hash allow duplicate
keys.  Change those hashes to pass the flag at creation, and remove
the last parameter from all callers of hash_insert().

13 files changed:
alias.c
group.c
hash.c
hash.h
headers.c
imap/message.c
init.c
mh.c
mx.c
newsrc.c
nntp.c
pop.c
thread.c

diff --git a/alias.c b/alias.c
index c7806537f038d3ce37142087d78ee60769c81235..fd59bb689535262b6f36862ca531ae54d0557feb 100644 (file)
--- a/alias.c
+++ b/alias.c
@@ -469,7 +469,7 @@ void mutt_alias_add_reverse (ALIAS *t)
   for (ap = t->addr; ap; ap = ap->next)
   {
     if (!ap->group && ap->mailbox)
-      hash_insert (ReverseAlias, ap->mailbox, ap, 1);
+      hash_insert (ReverseAlias, ap->mailbox, ap);
   }
 }
 
diff --git a/group.c b/group.c
index bd10da841f74219fe038a835a9155acfd8e09336..656ff982284465d910e711404e489f74d34203d4 100644 (file)
--- a/group.c
+++ b/group.c
@@ -47,7 +47,7 @@ group_t *mutt_pattern_group (const char *k)
     mutt_debug (2, "mutt_pattern_group: Creating group %s.\n", k);
     p = safe_calloc (1, sizeof (group_t));
     p->name = safe_strdup (k);
-    hash_insert (Groups, p->name, p, 0);
+    hash_insert (Groups, p->name, p);
   }
 
   return p;
diff --git a/hash.c b/hash.c
index e0f4dff68bb87910e9227fc6d741561253df96ac..ae6d1c91a7a6d6614ec0fdd105566dec11e853a5 100644 (file)
--- a/hash.c
+++ b/hash.c
@@ -103,14 +103,18 @@ HASH *hash_create (int nelem, int flags)
   }
   if (flags & MUTT_HASH_STRDUP_KEYS)
     table->strdup_keys = 1;
+  if (flags & MUTT_HASH_ALLOW_DUPS)
+    table->allow_dups = 1;
   return table;
 }
 
-HASH *int_hash_create (int nelem)
+HASH *int_hash_create (int nelem, int flags)
 {
   HASH *table = new_hash (nelem);
   table->gen_hash = gen_int_hash;
   table->cmp_key = cmp_int_key;
+  if (flags & MUTT_HASH_ALLOW_DUPS)
+    table->allow_dups = 1;
   return table;
 }
 
@@ -128,7 +132,7 @@ HASH *hash_resize (HASH *ptr, int nelem, int lower)
     {
       tmp = elem;
       elem = elem->next;
-      hash_insert (table, tmp->key.strkey, tmp->data, 1);
+      hash_insert (table, tmp->key.strkey, tmp->data);
       FREE (&tmp);
     }
   }
@@ -142,7 +146,7 @@ HASH *hash_resize (HASH *ptr, int nelem, int lower)
  * data         data to associate with `key'
  * allow_dup    if nonzero, duplicate keys are allowed in the table 
  */
-static int union_hash_insert (HASH * table, union hash_key key, void *data, int allow_dup)
+static int union_hash_insert (HASH * table, union hash_key key, void *data)
 {
   struct hash_elem *ptr;
   unsigned int h;
@@ -152,7 +156,7 @@ static int union_hash_insert (HASH * table, union hash_key key, void *data, int
   ptr->key = key;
   ptr->data = data;
 
-  if (allow_dup)
+  if (table->allow_dups)
   {
     ptr->next = table->table[h];
     table->table[h] = ptr;
@@ -184,18 +188,18 @@ static int union_hash_insert (HASH * table, union hash_key key, void *data, int
   return h;
 }
 
-int hash_insert (HASH * table, const char *strkey, void *data, int allow_dup)
+int hash_insert (HASH * table, const char *strkey, void *data)
 {
   union hash_key key;
   key.strkey = table->strdup_keys ? safe_strdup (strkey) : strkey;
-  return union_hash_insert (table, key, data, allow_dup);
+  return union_hash_insert (table, key, data);
 }
 
-int int_hash_insert (HASH * table, unsigned int intkey, void *data, int allow_dup)
+int int_hash_insert (HASH * table, unsigned int intkey, void *data)
 {
   union hash_key key;
   key.intkey = intkey;
-  return union_hash_insert (table, key, data, allow_dup);
+  return union_hash_insert (table, key, data);
 }
 
 static struct hash_elem *union_hash_find_elem (const HASH *table, union hash_key key)
diff --git a/hash.h b/hash.h
index b29e097ea8f1d40fe0e3073a2698d1bc1d09a748..e2f7ace93a83f5bc7405c3da8b7143325725db6e 100644 (file)
--- a/hash.h
+++ b/hash.h
@@ -35,7 +35,8 @@ struct hash_elem
 typedef struct
 {
   int nelem, curnelem;
-  int strdup_keys;      /* if set, the key->strkey is strdup'ed */
+  unsigned int strdup_keys : 1;      /* if set, the key->strkey is strdup'ed */
+  unsigned int allow_dups : 1;       /* if set, duplicate keys are allowed */
   struct hash_elem **table;
   unsigned int (*gen_hash)(union hash_key, unsigned int);
   int (*cmp_key)(union hash_key, union hash_key);
@@ -45,13 +46,14 @@ HASH;
 /* flags for hash_create() */
 #define MUTT_HASH_STRCASECMP   (1<<0)   /* use strcasecmp() to compare keys */
 #define MUTT_HASH_STRDUP_KEYS  (1<<1)   /* make a copy of the keys */
+#define MUTT_HASH_ALLOW_DUPS   (1<<2)   /* allow duplicate keys to be inserted */
 
-HASH *hash_create (int nelem, int lower);
-HASH *int_hash_create (int nelem);
+HASH *hash_create (int nelem, int flags);
+HASH *int_hash_create (int nelem, int flags);
 
-int hash_insert (HASH * table, const char *key, void *data, int allow_dup);
+int hash_insert (HASH * table, const char *key, void *data);
+int int_hash_insert (HASH *table, unsigned int key, void *data);
 HASH *hash_resize (HASH * table, int nelem, int lower);
-int int_hash_insert (HASH *table, unsigned int key, void *data, int allow_dup);
 
 void *hash_find (const HASH *table, const char *key);
 struct hash_elem *hash_find_elem (const HASH *table, const char *strkey);
index ffba7a18f70d3607af6a938f58f496aacc7dffe8..1f671dde29badc039ca6b3df3a4fd43bfd4b98f3 100644 (file)
--- a/headers.c
+++ b/headers.c
@@ -245,7 +245,7 @@ static void label_ref_inc(CONTEXT *ctx, char *label)
   if (!elem)
   {
     count = 1;
-    hash_insert(ctx->label_hash, label, (void *)count, 0);
+    hash_insert(ctx->label_hash, label, (void *)count);
     return;
   }
 
index f912dcc722268885def7821106a236ef9e54a7b3..0e9a1b3bbf86dedb2ccbb331cdf22d3cb85a4908 100644 (file)
@@ -60,12 +60,12 @@ static void imap_update_context (IMAP_DATA *idata, int oldmsgcount)
 
   ctx = idata->ctx;
   if (!idata->uid_hash)
-    idata->uid_hash = int_hash_create (MAX (6 * ctx->msgcount / 5, 30));
+    idata->uid_hash = int_hash_create (MAX (6 * ctx->msgcount / 5, 30), 0);
 
   for (msgno = oldmsgcount; msgno < ctx->msgcount; msgno++)
   {
     h = ctx->hdrs[msgno];
-    int_hash_insert (idata->uid_hash, HEADER_DATA(h)->uid, h, 0);
+    int_hash_insert (idata->uid_hash, HEADER_DATA(h)->uid, h);
   }
 }
 
diff --git a/init.c b/init.c
index 1b361f75cead4ec38a3434d0702ca717a0b8a9e9..66547ca5e4ed4a16969de4bb4efdf3dcffbd605d 100644 (file)
--- a/init.c
+++ b/init.c
@@ -3670,7 +3670,8 @@ void mutt_init (int skip_sys_rc, LIST *commands)
 
   Groups = hash_create (1031, 0);
   /* reverse alias keys need to be strdup'ed because of idna conversions */
-  ReverseAlias = hash_create (1031, MUTT_HASH_STRCASECMP | MUTT_HASH_STRDUP_KEYS);
+  ReverseAlias = hash_create (1031, MUTT_HASH_STRCASECMP | MUTT_HASH_STRDUP_KEYS |
+                              MUTT_HASH_ALLOW_DUPS);
 #ifdef USE_NOTMUCH
   TagTransforms = hash_create (64, 1);
   TagFormats = hash_create (64, 0);
@@ -4086,7 +4087,7 @@ int parse_tag_transforms (BUFFER *b, BUFFER *s, unsigned long data, BUFFER *err)
       continue;
     }
 
-    hash_insert(TagTransforms, tag, transform, 0);
+    hash_insert(TagTransforms, tag, transform);
   }
   return 0;
 }
@@ -4117,7 +4118,7 @@ int parse_tag_formats (BUFFER *b, BUFFER *s, unsigned long data, BUFFER *err)
       continue;
     }
 
-    hash_insert(TagFormats, format, tag, 0);
+    hash_insert(TagFormats, format, tag);
   }
   return 0;
 }
diff --git a/mh.c b/mh.c
index ece6818aff28160199ba4d98b06cbde646bb00cb..2018d70c966fac700a6e0e7b0a9f36661632ad7b 100644 (file)
--- a/mh.c
+++ b/mh.c
@@ -2224,7 +2224,7 @@ static int maildir_check_mailbox (CONTEXT * ctx, int *index_hint)
   {
     maildir_canon_filename (buf, p->h->path, sizeof (buf));
     p->canon_fname = safe_strdup (buf);
-    hash_insert (fnames, p->canon_fname, p, 0);
+    hash_insert (fnames, p->canon_fname, p);
   }
 
   /* check for modifications and adjust flags */
@@ -2374,7 +2374,7 @@ static int mh_check_mailbox (CONTEXT * ctx, int *index_hint)
   {
     /* the hash key must survive past the header, which is freed below. */
     p->canon_fname = safe_strdup (p->h->path);
-    hash_insert (fnames, p->canon_fname, p, 0);
+    hash_insert (fnames, p->canon_fname, p);
   }
 
   for (i = 0; i < ctx->msgcount; i++)
diff --git a/mx.c b/mx.c
index 0c16f0b5f179caadbba264bc8f36912ce13aadb5..7624c5a66ea81405cc2320370ad8dd322a6954f8 100644 (file)
--- a/mx.c
+++ b/mx.c
@@ -1518,9 +1518,9 @@ 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->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);
     mutt_label_hash_add (ctx, h);
 
     if (option (OPTSCORE)) 
index 036bbb8d732a9bf148bdb490b9c6bf3811b8eeaf..228a5e892266d985cd438fbdf3488496b7a2744f 100644 (file)
--- a/newsrc.c
+++ b/newsrc.c
@@ -64,7 +64,7 @@ static NNTP_DATA *nntp_data_find (NNTP_SERVER *nserv, const char *group)
     if (nserv->groups_hash->nelem < nserv->groups_hash->curnelem * 2)
       nserv->groups_hash = hash_resize (nserv->groups_hash,
                           nserv->groups_hash->nelem * 2, 0);
-    hash_insert (nserv->groups_hash, nntp_data->group, nntp_data, 0);
+    hash_insert (nserv->groups_hash, nntp_data->group, nntp_data);
 
     /* add NNTP_DATA to list */
     if (nserv->groups_num >= nserv->groups_max)
diff --git a/nntp.c b/nntp.c
index 17a95b784f60b2d62854c131c7767e3e99ea54e9..c25c1b2b40282e25581f9bc420c218dedc0d0284 100644 (file)
--- a/nntp.c
+++ b/nntp.c
@@ -1645,9 +1645,9 @@ int nntp_fetch_message (CONTEXT *ctx, MESSAGE *msg, int msgno)
   hdr->env = mutt_read_rfc822_header (msg->fp, hdr, 0, 0);
 
   if (ctx->id_hash && hdr->env->message_id)
-    hash_insert (ctx->id_hash, hdr->env->message_id, hdr, 0);
+    hash_insert (ctx->id_hash, hdr->env->message_id, hdr);
   if (ctx->subj_hash && hdr->env->real_subj)
-    hash_insert (ctx->subj_hash, hdr->env->real_subj, hdr, 1);
+    hash_insert (ctx->subj_hash, hdr->env->real_subj, hdr);
 
   /* fix content length */
   fseek (msg->fp, 0, SEEK_END);
diff --git a/pop.c b/pop.c
index faf11bc582976a35a63d19f6323715e23d8c1b7b..6a50fcb26ac26368c9a6195eb040544185183e9b 100644 (file)
--- a/pop.c
+++ b/pop.c
@@ -636,7 +636,7 @@ static int pop_fetch_message (CONTEXT* ctx, MESSAGE* msg, int msgno)
   mutt_free_envelope (&h->env);
   h->env = mutt_read_rfc822_header (msg->fp, h, 0, 0);
   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);
   mutt_label_hash_add (ctx, h);
 
   h->data = uidl;
index 3130e0856f2a1fa192eee01d770c8b8efac3805f..b6da4d354eb4c2516e36e8db02bb7d04fb3a53a9 100644 (file)
--- a/thread.c
+++ b/thread.c
@@ -765,7 +765,7 @@ void mutt_sort_threads (CONTEXT *ctx, int init)
     init = 1;
 
   if (init)
-    ctx->thread_hash = hash_create (ctx->msgcount * 2, 0);
+    ctx->thread_hash = hash_create (ctx->msgcount * 2, MUTT_HASH_ALLOW_DUPS);
 
   /* 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
@@ -837,7 +837,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)
        {
@@ -924,7 +924,7 @@ void mutt_sort_threads (CONTEXT *ctx, int init)
       if ((new = hash_find (ctx->thread_hash, ref->data)) == NULL)
       {
        new = safe_calloc (1, sizeof (THREAD));
-       hash_insert (ctx->thread_hash, ref->data, new, 1);
+       hash_insert (ctx->thread_hash, ref->data, new);
       }
       else
       {
@@ -1340,7 +1340,7 @@ HASH *mutt_make_id_hash (CONTEXT *ctx)
   {
     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;
@@ -1352,13 +1352,13 @@ HASH *mutt_make_subj_hash (CONTEXT *ctx)
   HEADER *hdr;
   HASH *hash;
 
-  hash = hash_create (ctx->msgcount * 2, 0);
+  hash = hash_create (ctx->msgcount * 2, MUTT_HASH_ALLOW_DUPS);
 
   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;