--- /dev/null
+Return-Path: <jrollins@finestructure.net>\r
+X-Original-To: notmuch@notmuchmail.org\r
+Delivered-To: notmuch@notmuchmail.org\r
+Received: from localhost (localhost [127.0.0.1])\r
+ by olra.theworths.org (Postfix) with ESMTP id 8E451431FAE\r
+ for <notmuch@notmuchmail.org>; Sun, 19 Aug 2012 18:53:10 -0700 (PDT)\r
+X-Virus-Scanned: Debian amavisd-new at olra.theworths.org\r
+X-Spam-Flag: NO\r
+X-Spam-Score: -2.3\r
+X-Spam-Level: \r
+X-Spam-Status: No, score=-2.3 tagged_above=-999 required=5\r
+ tests=[RCVD_IN_DNSWL_MED=-2.3] autolearn=disabled\r
+Received: from olra.theworths.org ([127.0.0.1])\r
+ by localhost (olra.theworths.org [127.0.0.1]) (amavisd-new, port 10024)\r
+ with ESMTP id d21aE45cpBHE for <notmuch@notmuchmail.org>;\r
+ Sun, 19 Aug 2012 18:53:08 -0700 (PDT)\r
+Received: from outgoing-mail.its.caltech.edu (outgoing-mail.its.caltech.edu\r
+ [131.215.239.19])\r
+ by olra.theworths.org (Postfix) with ESMTP id BA23A431FB6\r
+ for <notmuch@notmuchmail.org>; Sun, 19 Aug 2012 18:53:06 -0700 (PDT)\r
+Received: from earth-doxen.imss.caltech.edu (localhost [127.0.0.1])\r
+ by earth-doxen-postvirus (Postfix) with ESMTP id 7292966E0016\r
+ for <notmuch@notmuchmail.org>; Sun, 19 Aug 2012 18:53:06 -0700 (PDT)\r
+X-Spam-Scanned: at Caltech-IMSS on earth-doxen by amavisd-new\r
+Received: from finestructure.net (unknown [76.89.192.57])\r
+ (Authenticated sender: jrollins)\r
+ by earth-doxen-submit (Postfix) with ESMTP id 4AA8966E00D1\r
+ for <notmuch@notmuchmail.org>; Sun, 19 Aug 2012 18:53:04 -0700 (PDT)\r
+Received: by finestructure.net (Postfix, from userid 1000)\r
+ id C7DE12C3; Sun, 19 Aug 2012 18:53:03 -0700 (PDT)\r
+From: Jameson Graef Rollins <jrollins@finestructure.net>\r
+To: Notmuch Mail <notmuch@notmuchmail.org>\r
+Subject: [PATCH 01/11] lib: new thread addresses structure\r
+Date: Sun, 19 Aug 2012 18:52:40 -0700\r
+Message-Id: <1345427570-26518-2-git-send-email-jrollins@finestructure.net>\r
+X-Mailer: git-send-email 1.7.10.4\r
+In-Reply-To: <1345427570-26518-1-git-send-email-jrollins@finestructure.net>\r
+References: <1345427570-26518-1-git-send-email-jrollins@finestructure.net>\r
+X-BeenThere: notmuch@notmuchmail.org\r
+X-Mailman-Version: 2.1.13\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: Mon, 20 Aug 2012 01:53:11 -0000\r
+\r
+This new structure holds addresses associated with a thread, both\r
+matched and unmatched. Initially this will be used to replace the\r
+existing infrastructure for storing the addresses of thread authors.\r
+Further patches will use it to store the addresses of threads\r
+recipients.\r
+\r
+Init and destructor functions are included, as well as a function to\r
+add addresses to a struct, either "matched" or not.\r
+---\r
+ lib/notmuch.h | 1 +\r
+ lib/thread.cc | 116 +++++++++++++++++++++++++++++++++++++++++++++++++++++++++\r
+ 2 files changed, 117 insertions(+)\r
+\r
+diff --git a/lib/notmuch.h b/lib/notmuch.h\r
+index 3633bed..6acd38d 100644\r
+--- a/lib/notmuch.h\r
++++ b/lib/notmuch.h\r
+@@ -118,6 +118,7 @@ typedef struct _notmuch_database notmuch_database_t;\r
+ typedef struct _notmuch_query notmuch_query_t;\r
+ typedef struct _notmuch_threads notmuch_threads_t;\r
+ typedef struct _notmuch_thread notmuch_thread_t;\r
++typedef struct _notmuch_thread_addresses notmuch_thread_addresses_t;\r
+ typedef struct _notmuch_messages notmuch_messages_t;\r
+ typedef struct _notmuch_message notmuch_message_t;\r
+ typedef struct _notmuch_tags notmuch_tags_t;\r
+diff --git a/lib/thread.cc b/lib/thread.cc\r
+index e976d64..7af9eeb 100644\r
+--- a/lib/thread.cc\r
++++ b/lib/thread.cc\r
+@@ -24,6 +24,14 @@\r
+ #include <gmime/gmime.h>\r
+ #include <glib.h> /* GHashTable */\r
+ \r
++struct visible _notmuch_thread_addresses {\r
++ GHashTable *matched_hash;\r
++ GPtrArray *matched_array;\r
++ GHashTable *unmatched_hash;\r
++ GPtrArray *unmatched_array;\r
++ char *string;\r
++};\r
++\r
+ struct visible _notmuch_thread {\r
+ notmuch_database_t *notmuch;\r
+ char *thread_id;\r
+@@ -44,6 +52,18 @@ struct visible _notmuch_thread {\r
+ };\r
+ \r
+ static int\r
++_notmuch_thread_addresses_destructor (notmuch_thread_addresses_t *addresses)\r
++{\r
++ g_hash_table_unref (addresses->matched_hash);\r
++ g_hash_table_unref (addresses->unmatched_hash);\r
++ g_ptr_array_free (addresses->matched_array, TRUE);\r
++ g_ptr_array_free (addresses->unmatched_array, TRUE);\r
++ addresses->matched_array = NULL;\r
++ addresses->unmatched_array = NULL;\r
++ return 0;\r
++}\r
++\r
++static int\r
+ _notmuch_thread_destructor (notmuch_thread_t *thread)\r
+ {\r
+ g_hash_table_unref (thread->authors_hash);\r
+@@ -64,6 +84,81 @@ _notmuch_thread_destructor (notmuch_thread_t *thread)\r
+ return 0;\r
+ }\r
+ \r
++/* Add address to a thread addresses struct. If matched is TRUE, then\r
++ * the address will be added to the matched list.*/\r
++static void\r
++_thread_add_address (notmuch_thread_addresses_t *addresses,\r
++ const char *address,\r
++ notmuch_bool_t matched)\r
++{\r
++ char *address_copy;\r
++ GHashTable *hash;\r
++ GPtrArray *array;\r
++\r
++ if (matched) {\r
++ hash = addresses->matched_hash;\r
++ array = addresses->matched_array;\r
++ } else {\r
++ hash = addresses->unmatched_hash;\r
++ array = addresses->unmatched_array;\r
++ }\r
++\r
++ if (address == NULL)\r
++ return;\r
++\r
++ if (g_hash_table_lookup_extended (hash, address, NULL, NULL))\r
++ return;\r
++\r
++ address_copy = talloc_strdup (addresses, address);\r
++\r
++ g_hash_table_insert (hash, address_copy, NULL);\r
++\r
++ g_ptr_array_add (array, address_copy);\r
++}\r
++\r
++/* Construct an addresses string from matched and unmatched addresses\r
++ * in notmuch_thread_addresses_t. The string contains matched\r
++ * addresses first, then non-matched addresses (with the two groups\r
++ * separated by '|'). Within each group, addresses are listed in date\r
++ * order. */\r
++static void\r
++_resolve_thread_addresses_string (notmuch_thread_addresses_t *addresses)\r
++{\r
++ unsigned int i;\r
++ char *address;\r
++ int first_non_matched_address = 1;\r
++\r
++ /* First, list all matched addressses in date order. */\r
++ for (i = 0; i < addresses->matched_array->len; i++) {\r
++ address = (char *) g_ptr_array_index (addresses->matched_array, i);\r
++ if (addresses->string)\r
++ addresses->string = talloc_asprintf (addresses, "%s, %s",\r
++ addresses->string,\r
++ address);\r
++ else\r
++ addresses->string = address;\r
++ }\r
++\r
++ /* Next, append any non-matched addresses that haven't already appeared. */\r
++ for (i = 0; i < addresses->unmatched_array->len; i++) {\r
++ address = (char *) g_ptr_array_index (addresses->unmatched_array, i);\r
++ if (g_hash_table_lookup_extended (addresses->matched_hash,\r
++ address, NULL, NULL))\r
++ continue;\r
++ if (first_non_matched_address) {\r
++ addresses->string = talloc_asprintf (addresses, "%s| %s",\r
++ addresses->string,\r
++ address);\r
++ } else {\r
++ addresses->string = talloc_asprintf (addresses, "%s, %s",\r
++ addresses->string,\r
++ address);\r
++ }\r
++\r
++ first_non_matched_address = 0;\r
++ }\r
++}\r
++\r
+ /* Add each author of the thread to the thread's authors_hash and to\r
+ * the thread's authors_array. */\r
+ static void\r
+@@ -382,6 +477,27 @@ _resolve_thread_relationships (unused (notmuch_thread_t *thread))\r
+ */\r
+ }\r
+ \r
++/* Initialize a thread addresses struct. */\r
++notmuch_thread_addresses_t *\r
++_thread_addresses_init (const void *ctx)\r
++{\r
++ notmuch_thread_addresses_t *addresses;\r
++\r
++ addresses = talloc (ctx, notmuch_thread_addresses_t);\r
++ if (unlikely (addresses == NULL))\r
++ return NULL;\r
++\r
++ addresses->matched_hash = g_hash_table_new_full (g_str_hash, g_str_equal,\r
++ NULL, NULL);\r
++ addresses->matched_array = g_ptr_array_new ();\r
++ addresses->unmatched_hash = g_hash_table_new_full (g_str_hash, g_str_equal,\r
++ NULL, NULL);\r
++ addresses->unmatched_array = g_ptr_array_new ();\r
++ addresses->string = NULL;\r
++\r
++ return addresses;\r
++}\r
++\r
+ /* Create a new notmuch_thread_t object by finding the thread\r
+ * containing the message with the given doc ID, treating any messages\r
+ * contained in match_set as "matched". Remove all messages in the\r
+-- \r
+1.7.10.4\r
+\r