From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: from localhost (localhost [127.0.0.1]) by arlo.cworth.org (Postfix) with ESMTP id 3C8946DE0291 for ; Mon, 30 Jul 2018 15:46:20 -0700 (PDT) X-Virus-Scanned: Debian amavisd-new at cworth.org X-Spam-Flag: NO X-Spam-Score: 0 X-Spam-Level: X-Spam-Status: No, score=0 tagged_above=-999 required=5 tests=[AWL=0.011, SPF_PASS=-0.001, T_RP_MATCHES_RCVD=-0.01] autolearn=disabled Received: from arlo.cworth.org ([127.0.0.1]) by localhost (arlo.cworth.org [127.0.0.1]) (amavisd-new, port 10024) with ESMTP id UcgloWUCigW3 for ; Mon, 30 Jul 2018 15:46:19 -0700 (PDT) Received: from fethera.tethera.net (fethera.tethera.net [198.245.60.197]) by arlo.cworth.org (Postfix) with ESMTPS id 0602E6DE02CB for ; Mon, 30 Jul 2018 15:46:17 -0700 (PDT) Received: from remotemail by fethera.tethera.net with local (Exim 4.89) (envelope-from ) id 1fkGvg-0002BY-65; Mon, 30 Jul 2018 18:46:16 -0400 Received: (nullmailer pid 28726 invoked by uid 1000); Mon, 30 Jul 2018 22:45:56 -0000 From: David Bremner To: notmuch@notmuchmail.org Subject: [PATCH 04/15] lib/thread: sort child messages by date Date: Tue, 31 Jul 2018 06:45:44 +0800 Message-Id: <20180730224555.26047-5-david@tethera.net> X-Mailer: git-send-email 2.18.0 In-Reply-To: <20180730224555.26047-1-david@tethera.net> References: <20180730224555.26047-1-david@tethera.net> X-BeenThere: notmuch@notmuchmail.org X-Mailman-Version: 2.1.26 Precedence: list List-Id: "Use and development of the notmuch mail system." List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , X-List-Received-Date: Mon, 30 Jul 2018 22:46:20 -0000 This will not should anything currently, as the child messages are already added in date order. In the future we will add some messages in a second pass out of order and the sorting will be useful. --- lib/message.cc | 41 +++++++++++++++++++++++++++++++++++++++++ lib/notmuch-private.h | 3 +++ lib/thread.cc | 9 +++++++++ 3 files changed, 53 insertions(+) diff --git a/lib/message.cc b/lib/message.cc index 153e4bed..107dcf35 100644 --- a/lib/message.cc +++ b/lib/message.cc @@ -588,6 +588,47 @@ _notmuch_message_add_reply (notmuch_message_t *message, _notmuch_message_list_add_message (message->replies, reply); } +static int +_cmpmsg (const void *pa, const void *pb) +{ + notmuch_message_t **a = (notmuch_message_t **) pa; + notmuch_message_t **b = (notmuch_message_t **) pb; + time_t time_a = notmuch_message_get_date (*a); + time_t time_b = notmuch_message_get_date (*b); + + return (int) difftime (time_a, time_b); +} + +void +_notmuch_message_sort_subtree (notmuch_message_t *root) +{ + size_t child_count = 0; + size_t child_capacity = 16; + notmuch_message_t **children = talloc_zero_array (root, notmuch_message_t *, child_capacity); + + for (notmuch_messages_t *messages = _notmuch_messages_create (root->replies); + notmuch_messages_valid (messages); + notmuch_messages_move_to_next (messages)) { + notmuch_message_t *child = notmuch_messages_get (messages); + if (child_count >= child_capacity) { + child_capacity *= 2; + children = talloc_realloc (root, children, notmuch_message_t *, child_capacity); + } + children[child_count++] = child; + _notmuch_message_sort_subtree (child); + } + + notmuch_message_list_t *new_replies = _notmuch_message_list_create (root); + + qsort (children, child_count, sizeof (notmuch_message_t *), _cmpmsg); + for (size_t i=0; ireplies); + root->replies = new_replies; + talloc_free (children); +} + notmuch_messages_t * notmuch_message_get_replies (notmuch_message_t *message) { diff --git a/lib/notmuch-private.h b/lib/notmuch-private.h index 499d73d4..10bb7040 100644 --- a/lib/notmuch-private.h +++ b/lib/notmuch-private.h @@ -539,6 +539,9 @@ _notmuch_message_remove_unprefixed_terms (notmuch_message_t *message); const char * _notmuch_message_get_thread_id_only(notmuch_message_t *message); +void +_notmuch_message_sort_subtree (notmuch_message_t *message); + /* sha1.c */ char * diff --git a/lib/thread.cc b/lib/thread.cc index e961c76b..db592a3a 100644 --- a/lib/thread.cc +++ b/lib/thread.cc @@ -429,6 +429,15 @@ _resolve_thread_relationships (notmuch_thread_t *thread) _notmuch_message_list_add_message (thread->toplevel_list, message); } + for (notmuch_messages_t *messages = _notmuch_messages_create (thread->toplevel_list); + notmuch_messages_valid (messages); + notmuch_messages_move_to_next (messages)) + { + notmuch_message_t *message = notmuch_messages_get (messages); + _notmuch_message_sort_subtree (message); + } + + /* XXX: After scanning through the entire list looking for parents * via "In-Reply-To", we should do a second pass that looks at the * list of messages IDs in the "References" header instead. (And -- 2.18.0