--- /dev/null
+Return-Path: <jani@nikula.org>\r
+X-Original-To: notmuch@notmuchmail.org\r
+Delivered-To: notmuch@notmuchmail.org\r
+Received: from localhost (localhost [127.0.0.1])\r
+ by arlo.cworth.org (Postfix) with ESMTP id 4571A6DE15D4\r
+ for <notmuch@notmuchmail.org>; Thu, 3 Sep 2015 12:40:36 -0700 (PDT)\r
+X-Virus-Scanned: Debian amavisd-new at cworth.org\r
+X-Amavis-Alert: BAD HEADER SECTION, Duplicate header field: "References"\r
+X-Spam-Flag: NO\r
+X-Spam-Score: -0.425\r
+X-Spam-Level: \r
+X-Spam-Status: No, score=-0.425 tagged_above=-999 required=5 tests=[AWL=0.295,\r
+ RCVD_IN_DNSWL_LOW=-0.7, RCVD_IN_MSPIKE_H3=-0.01, RCVD_IN_MSPIKE_WL=-0.01]\r
+ autolearn=disabled\r
+Received: from arlo.cworth.org ([127.0.0.1])\r
+ by localhost (arlo.cworth.org [127.0.0.1]) (amavisd-new, port 10024)\r
+ with ESMTP id 4NCdwu0Yw4pW for <notmuch@notmuchmail.org>;\r
+ Thu, 3 Sep 2015 12:40:34 -0700 (PDT)\r
+Received: from mail-wi0-f171.google.com (mail-wi0-f171.google.com\r
+ [209.85.212.171])\r
+ by arlo.cworth.org (Postfix) with ESMTPS id DA4D76DE15E7\r
+ for <notmuch@notmuchmail.org>; Thu, 3 Sep 2015 12:40:26 -0700 (PDT)\r
+Received: by wicge5 with SMTP id ge5so84705833wic.0\r
+ for <notmuch@notmuchmail.org>; Thu, 03 Sep 2015 12:40:25 -0700 (PDT)\r
+X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed;\r
+ d=1e100.net; s=20130820;\r
+ h=x-gm-message-state:from:to:cc:subject:date:message-id:in-reply-to\r
+ :references:in-reply-to:references;\r
+ bh=NlXTuY6QGPqQJwbKZYJuxHESc8MPqbilrd/29Rp56KY=;\r
+ b=ecazczo3DDgja9izKe6j5vSEjzimdT4lIChtohGtJzltidQ/N7gVdhdpsctKCvfn0L\r
+ o8BjfRVm0lpEWr9UaBEAWvDLNIeThvxKQpedMj2kexADMKuz14RVKgA4eGGfrFIrp5R4\r
+ XkWTi2m6QMtb70WM61MxwpcrcAlCtBgU9CLg3C8t/OFEBMh3Gto6krijoMHafKTTZnNw\r
+ dBQxQ1NqKurOCju0PPJTQq0+gv/iHbraoYAtGYttxqKy9wn3sjOsgIHVnxbT6E78Oxqw\r
+ vXpYphUWecIZHWj3+sFoFfD51HIhMRk+AuX9Rz//qvnp9SsYxKG0wKQyThSiKXy9xVA2\r
+ dkhw==\r
+X-Gm-Message-State:\r
+ ALoCoQmLKjgeYYFJb4z06SoBNJroKC0WFBfEf1bmpF0SZFar09Xx49ipH/WxwFH5thPRF0fQ2Ort\r
+X-Received: by 10.194.82.167 with SMTP id j7mr54475903wjy.123.1441309225376;\r
+ Thu, 03 Sep 2015 12:40:25 -0700 (PDT)\r
+Received: from localhost (mobile-access-bcee4f-131.dhcp.inet.fi.\r
+ [188.238.79.131])\r
+ by smtp.gmail.com with ESMTPSA id i7sm625501wib.15.2015.09.03.12.40.24\r
+ (version=TLSv1/SSLv3 cipher=OTHER);\r
+ Thu, 03 Sep 2015 12:40:24 -0700 (PDT)\r
+From: Jani Nikula <jani@nikula.org>\r
+To: notmuch@notmuchmail.org\r
+Subject: [PATCH v2 6/9] cli: change the data structure for notmuch address\r
+ deduplication\r
+Date: Thu, 3 Sep 2015 22:40:02 +0300\r
+Message-Id:\r
+ <d8bb01aff648f9b73ec5f09f0e86e42dac2a21a6.1441308761.git.jani@nikula.org>\r
+X-Mailer: git-send-email 2.1.4\r
+In-Reply-To: <cover.1441308761.git.jani@nikula.org>\r
+References: <cover.1441308761.git.jani@nikula.org>\r
+In-Reply-To: <cover.1441308761.git.jani@nikula.org>\r
+References: <cover.1441308761.git.jani@nikula.org>\r
+X-BeenThere: notmuch@notmuchmail.org\r
+X-Mailman-Version: 2.1.18\r
+Precedence: list\r
+List-Id: "Use and development of the notmuch mail system."\r
+ <notmuch.notmuchmail.org>\r
+List-Unsubscribe: <http://notmuchmail.org/mailman/options/notmuch>,\r
+ <mailto:notmuch-request@notmuchmail.org?subject=unsubscribe>\r
+List-Archive: <http://notmuchmail.org/pipermail/notmuch/>\r
+List-Post: <mailto:notmuch@notmuchmail.org>\r
+List-Help: <mailto:notmuch-request@notmuchmail.org?subject=help>\r
+List-Subscribe: <http://notmuchmail.org/mailman/listinfo/notmuch>,\r
+ <mailto:notmuch-request@notmuchmail.org?subject=subscribe>\r
+X-List-Received-Date: Thu, 03 Sep 2015 19:40:36 -0000\r
+\r
+Currently we key the address hash table with the case sensitive "name\r
+<address>". Switch to case insensitive keying with just address, and\r
+store the case sensitive name and address in linked lists. This will\r
+be helpful in adding support for different deduplication schemes in\r
+the future.\r
+\r
+There will be a slight performance penalty for the current full case\r
+sensitive name + address deduplication, but this is simpler as a whole\r
+when other deduplication schemes are added, and I expect the schemes\r
+to be added to become more popular than the current default.\r
+\r
+Aparet from the possible performance penalty, the only user visible\r
+change should be the change in the output ordering for\r
+--output=count. The order is not guaranteed (and is based on hash\r
+table traversal) currently anyway, so this should be of no\r
+consequence.\r
+---\r
+ notmuch-client.h | 1 +\r
+ notmuch-search.c | 84 ++++++++++++++++++++++++++++++++++++++++++++++----------\r
+ 2 files changed, 70 insertions(+), 15 deletions(-)\r
+\r
+diff --git a/notmuch-client.h b/notmuch-client.h\r
+index 882aa30563df..97d68d1158ac 100644\r
+--- a/notmuch-client.h\r
++++ b/notmuch-client.h\r
+@@ -48,6 +48,7 @@ typedef GMimeCryptoContext notmuch_crypto_context_t;\r
+ #include <dirent.h>\r
+ #include <errno.h>\r
+ #include <signal.h>\r
++#include <ctype.h>\r
+ \r
+ #include "talloc-extra.h"\r
+ \r
+diff --git a/notmuch-search.c b/notmuch-search.c\r
+index 66404b561679..7c51d5df6bd4 100644\r
+--- a/notmuch-search.c\r
++++ b/notmuch-search.c\r
+@@ -265,30 +265,74 @@ static mailbox_t *new_mailbox (void *ctx, const char *name, const char *addr)\r
+ return mailbox;\r
+ }\r
+ \r
++static int mailbox_compare (const void *v1, const void *v2)\r
++{\r
++ const mailbox_t *m1 = v1, *m2 = v2;\r
++ int v;\r
++\r
++ if (m1->name && m2->name)\r
++ v = strcmp (m1->name, m2->name);\r
++ else\r
++ v = !!m1->name - !!m2->name;\r
++\r
++ if (! v)\r
++ v = strcmp (m1->addr, m2->addr);\r
++\r
++ return v;\r
++}\r
++\r
+ /* Returns TRUE iff name and addr is duplicate. If not, stores the\r
+ * name/addr pair in order to detect subsequent duplicates. */\r
+ static notmuch_bool_t\r
+ is_duplicate (const search_context_t *ctx, const char *name, const char *addr)\r
+ {\r
+ char *key;\r
++ GList *list, *l;\r
+ mailbox_t *mailbox;\r
+ \r
+- key = talloc_asprintf (ctx->format, "%s <%s>", name, addr);\r
+- if (! key)\r
+- return FALSE;\r
++ list = g_hash_table_lookup (ctx->addresses, addr);\r
++ if (list) {\r
++ mailbox_t find = {\r
++ .name = name,\r
++ .addr = addr,\r
++ };\r
++\r
++ l = g_list_find_custom (list, &find, mailbox_compare);\r
++ if (l) {\r
++ mailbox = l->data;\r
++ mailbox->count++;\r
++ return TRUE;\r
++ }\r
+ \r
+- mailbox = g_hash_table_lookup (ctx->addresses, key);\r
+- if (mailbox) {\r
+- mailbox->count++;\r
+- talloc_free (key);\r
+- return TRUE;\r
++ mailbox = new_mailbox (ctx->format, name, addr);\r
++ if (! mailbox)\r
++ return FALSE;\r
++\r
++ /*\r
++ * XXX: It would be more efficient to prepend to the list, but\r
++ * then we'd have to store the changed list head back to the\r
++ * hash table. This check is here just to avoid the compiler\r
++ * warning for unused result.\r
++ */\r
++ if (list != g_list_append (list, mailbox))\r
++ INTERNAL_ERROR ("appending to list changed list head\n");\r
++\r
++ return FALSE;\r
+ }\r
+ \r
++ key = talloc_strdup (ctx->format, addr);\r
++ if (! key)\r
++ return FALSE;\r
++\r
+ mailbox = new_mailbox (ctx->format, name, addr);\r
+ if (! mailbox)\r
+ return FALSE;\r
+ \r
+- g_hash_table_insert (ctx->addresses, key, mailbox);\r
++ list = g_list_append (NULL, mailbox);\r
++ if (! list)\r
++ return FALSE;\r
++\r
++ g_hash_table_insert (ctx->addresses, key, list);\r
+ \r
+ return FALSE;\r
+ }\r
+@@ -401,12 +445,21 @@ _talloc_free_for_g_hash (void *ptr)\r
+ }\r
+ \r
+ static void\r
+-print_hash_value (unused (gpointer key), gpointer value, gpointer user_data)\r
++_list_free_for_g_hash (void *ptr)\r
++{\r
++ g_list_free_full (ptr, _talloc_free_for_g_hash);\r
++}\r
++\r
++static void\r
++print_list_value (void *mailbox, void *context)\r
+ {\r
+- const mailbox_t *mailbox = value;\r
+- search_context_t *ctx = user_data;\r
++ print_mailbox (context, mailbox);\r
++}\r
+ \r
+- print_mailbox (ctx, mailbox);\r
++static void\r
++print_hash_value (unused (void *key), void *list, void *context)\r
++{\r
++ g_list_foreach (list, print_list_value, context);\r
+ }\r
+ \r
+ static int\r
+@@ -792,8 +845,9 @@ notmuch_address_command (notmuch_config_t *config, int argc, char *argv[])\r
+ argc - opt_index, argv + opt_index))\r
+ return EXIT_FAILURE;\r
+ \r
+- ctx->addresses = g_hash_table_new_full (g_str_hash, g_str_equal,\r
+- _talloc_free_for_g_hash, _talloc_free_for_g_hash);\r
++ ctx->addresses = g_hash_table_new_full (strcase_hash, strcase_equal,\r
++ _talloc_free_for_g_hash,\r
++ _list_free_for_g_hash);\r
+ \r
+ ret = do_search_messages (ctx);\r
+ \r
+-- \r
+2.1.4\r
+\r