X-Git-Url: https://git.notmuchmail.org/git?p=notmuch;a=blobdiff_plain;f=lib%2Ftags.c;h=c58924f87b56de56a7799dd23aadcef35056d15d;hp=85507e91fc35da13b52b1b82b2bcbe2a71af8f83;hb=75a05526334b721d719d6c8a8098ff64573e6c1e;hpb=c979fc5b05a541d8488b77cbbc590ed3955690df diff --git a/lib/tags.c b/lib/tags.c index 85507e91..c58924f8 100644 --- a/lib/tags.c +++ b/lib/tags.c @@ -20,30 +20,18 @@ #include "notmuch-private.h" -#include /* GList */ - struct _notmuch_tags { - int sorted; - GList *tags; - GList *iterator; + notmuch_string_node_t *iterator; }; -/* XXX: Should write some talloc-friendly list to avoid the need for - * this. */ -static int -_notmuch_tags_destructor (notmuch_tags_t *tags) -{ - g_list_free (tags->tags); - - return 0; -} - /* Create a new notmuch_tags_t object, with 'ctx' as its talloc owner. + * The returned iterator will talloc_steal the 'list', since the list + * is almost always transient. * * This function can return NULL in case of out-of-memory. */ notmuch_tags_t * -_notmuch_tags_create (void *ctx) +_notmuch_tags_create (const void *ctx, notmuch_string_list_t *list) { notmuch_tags_t *tags; @@ -51,45 +39,14 @@ _notmuch_tags_create (void *ctx) if (unlikely (tags == NULL)) return NULL; - talloc_set_destructor (tags, _notmuch_tags_destructor); - - tags->sorted = 1; - tags->tags = NULL; - tags->iterator = NULL; + tags->iterator = list->head; + talloc_steal (tags, list); return tags; } -/* Add a new tag to 'tags'. The tags object will create its own copy - * of the string. - * - * Note: The tags object will not do anything to prevent duplicate - * tags being stored, so the caller really shouldn't pass - * duplicates. */ -void -_notmuch_tags_add_tag (notmuch_tags_t *tags, const char *tag) -{ - tags->tags = g_list_prepend (tags->tags, talloc_strdup (tags, tag)); - tags->sorted = 0; -} - -/* Prepare 'tag' for iteration. - * - * The internal creator of 'tags' should call this function before - * returning 'tags' to the user to call the public functions such as - * notmuch_tags_has_more, notmuch_tags_get, and notmuch_tags_advance. */ -void -_notmuch_tags_prepare_iterator (notmuch_tags_t *tags) -{ - if (! tags->sorted) - tags->tags = g_list_sort (tags->tags, (GCompareFunc) strcmp); - tags->sorted = 1; - - tags->iterator = tags->tags; -} - notmuch_bool_t -notmuch_tags_has_more (notmuch_tags_t *tags) +notmuch_tags_valid (notmuch_tags_t *tags) { return tags->iterator != NULL; } @@ -100,11 +57,11 @@ notmuch_tags_get (notmuch_tags_t *tags) if (tags->iterator == NULL) return NULL; - return (char *) tags->iterator->data; + return (char *) tags->iterator->string; } void -notmuch_tags_advance (notmuch_tags_t *tags) +notmuch_tags_move_to_next (notmuch_tags_t *tags) { if (tags->iterator == NULL) return;