From 9c66094ed08b1d6bc8bcc79be2ba013a370b9fa3 Mon Sep 17 00:00:00 2001 From: Pietro Cerutti Date: Mon, 19 Aug 2019 12:10:34 +0000 Subject: [PATCH] Convert thread sorting to rxi-vec --- mutt_thread.c | 35 ++++++++++++++++++----------------- 1 file changed, 18 insertions(+), 17 deletions(-) diff --git a/mutt_thread.c b/mutt_thread.c index a6e43d200..8892c6e13 100644 --- a/mutt_thread.c +++ b/mutt_thread.c @@ -43,6 +43,7 @@ #include "mx.h" #include "protos.h" #include "sort.h" +#include "rxi-vec/vec.h" /* These Config Variables are only used in mutt_thread.c */ bool C_DuplicateThreads; ///< Config: Highlight messages with duplicated message IDs @@ -646,6 +647,8 @@ static int compare_threads(const void *a, const void *b) } } +typedef vec_t(struct MuttThread*) ThreadVector; + /** * mutt_sort_subthreads - Sort the children of a thread * @param thread Thread to start at @@ -654,9 +657,10 @@ static int compare_threads(const void *a, const void *b) */ struct MuttThread *mutt_sort_subthreads(struct MuttThread *thread, bool init) { - struct MuttThread **array = NULL, *sort_key = NULL, *top = NULL, *tmp = NULL; + ThreadVector array; + struct MuttThread *sort_key = NULL, *top = NULL, *tmp = NULL; struct Email *oldsort_key = NULL; - int i, array_size, sort_top = 0; + int i, sort_top = 0; /* we put things into the array backwards to save some cycles, * but we want to have to move less stuff around if we're @@ -668,8 +672,8 @@ struct MuttThread *mutt_sort_subthreads(struct MuttThread *thread, bool init) top = thread; - array_size = 256; - array = mutt_mem_calloc(array_size, sizeof(struct MuttThread *)); + vec_init(&array); + vec_reserve(&array, 256); while (true) { if (init || !thread->sort_key) @@ -704,31 +708,28 @@ struct MuttThread *mutt_sort_subthreads(struct MuttThread *thread, bool init) /* if it has siblings and needs to be sorted, sort it... */ if (thread->prev && (thread->parent ? thread->parent->sort_children : sort_top)) { + vec_clear(&array); /* put them into the array */ for (i = 0; thread; i++, thread = thread->prev) { - if (i >= array_size) - mutt_mem_realloc(&array, (array_size *= 2) * sizeof(struct MuttThread *)); - - array[i] = thread; + vec_push(&array, thread); } - - qsort((void *) array, i, sizeof(struct MuttThread *), *compare_threads); + vec_sort(&array, *compare_threads); /* attach them back together. make thread the last sibling. */ - thread = array[0]; + thread = vec_first(&array); thread->next = NULL; - array[i - 1]->prev = NULL; + vec_last(&array)->prev = NULL; if (thread->parent) - thread->parent->child = array[i - 1]; + thread->parent->child = vec_last(&array); else - top = array[i - 1]; + top = vec_last(&array); while (--i) { - array[i - 1]->prev = array[i]; - array[i]->next = array[i - 1]; + array.data[i - 1]->prev = array.data[i]; + array.data[i]->next = array.data[i - 1]; } } @@ -775,7 +776,7 @@ struct MuttThread *mutt_sort_subthreads(struct MuttThread *thread, bool init) else { C_Sort ^= SORT_REVERSE; - FREE(&array); + vec_deinit(&array); return top; } } -- 2.40.0