]> 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)
committerKevin McCarthy <kevin@8t8.us>
Fri, 10 Feb 2017 21:01:21 +0000 (13:01 -0800)
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().

alias.c
group.c
hash.c
hash.h
headers.c
imap/message.c
init.c
mh.c
mx.c
pop.c
thread.c

diff --git a/alias.c b/alias.c
index d9b52fa828197b8064bdb1436cb0690cffa9633f..458461df597c91654cbf11175b326a695994a169 100644 (file)
--- a/alias.c
+++ b/alias.c
@@ -465,7 +465,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 f48329756ac99c5dee25a95a5f2e8b3888922af6..2b1dc23675418becba5b42db605dbd5870af2c8a 100644 (file)
--- a/group.c
+++ b/group.c
@@ -47,7 +47,7 @@ group_t *mutt_pattern_group (const char *k)
     dprint (2, (debugfile, "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 40afd0b1b28e765da98bee149864d8a84288f509..3747dee5f1e839be71fa2384fbdd17dc1bf60aef 100644 (file)
--- a/hash.c
+++ b/hash.c
@@ -102,14 +102,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;
 }
 
@@ -118,7 +122,7 @@ HASH *int_hash_create (int nelem)
  * 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;
@@ -128,7 +132,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;
@@ -158,18 +162,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 1b6c731e5b7961baf7d2172860f0553b46feec9c..1ce378421fe9b4348d72055ba718b1fb99fa95bc 100644 (file)
--- a/hash.h
+++ b/hash.h
@@ -35,7 +35,8 @@ struct hash_elem
 typedef struct
 {
   int nelem;
-  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,12 +46,13 @@ 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 int_hash_insert (HASH *table, unsigned int 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);
 
 void *hash_find (const HASH *table, const char *key);
 struct hash_elem *hash_find_elem (const HASH *table, const char *strkey);
index 0b4372d7901751dbac70382385a1e73e9cba21b6..920bae542bbc0239237f419d8536a949f5874d5a 100644 (file)
--- a/headers.c
+++ b/headers.c
@@ -242,7 +242,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 625b994dec6d6ecffe191f6987976a2561e878f5..bd03bd49c25b5ec4a182dab8f101b934a1a65d7e 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 935013bb9909def00236ae5da49d16b153d048b5..7c0f179a0c4c4865bad66e7d2b77afd3a5683010 100644 (file)
--- a/init.c
+++ b/init.c
@@ -3249,7 +3249,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);
   
   mutt_menu_init ();
   mutt_srandom ();
diff --git a/mh.c b/mh.c
index fe4f7ebdfa93d32a93c26d5d2e7b0c42a5eaa387..545fa40486ab005111ca46c30ef52e151cca7f68 100644 (file)
--- a/mh.c
+++ b/mh.c
@@ -2141,7 +2141,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 */
@@ -2291,7 +2291,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 5e94d92b5bb27f11e30c383574174c53bc8239d6..5e61798cff49bc618b45e3047d19bd641125b5b9 100644 (file)
--- a/mx.c
+++ b/mx.c
@@ -1420,9 +1420,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)) 
diff --git a/pop.c b/pop.c
index c81c414f8d0dd9b8a5d97d3b6c0ff57fd4acf660..8e5d8feafbc67052f562f207b73c0b5044d65a10 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 27ce5160402ab179f202b08f7c7adbef67b13fcd..4c4c7b04a4fa7b532bf9a9a42246981b053af6c8 100644 (file)
--- a/thread.c
+++ b/thread.c
@@ -762,7 +762,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
@@ -834,7 +834,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)
        {
@@ -921,7 +921,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
       {
@@ -1337,7 +1337,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;
@@ -1349,13 +1349,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;