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 880F06DE02CD for ; Mon, 30 May 2016 04:50:27 -0700 (PDT) X-Virus-Scanned: Debian amavisd-new at cworth.org X-Spam-Flag: NO X-Spam-Score: -0.012 X-Spam-Level: X-Spam-Status: No, score=-0.012 tagged_above=-999 required=5 tests=[AWL=-0.001, 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 CXOw4NKNK3Hq for ; Mon, 30 May 2016 04:50:19 -0700 (PDT) Received: from fethera.tethera.net (fethera.tethera.net [198.245.60.197]) by arlo.cworth.org (Postfix) with ESMTPS id 86A186DE0281 for ; Mon, 30 May 2016 04:50:11 -0700 (PDT) Received: from remotemail by fethera.tethera.net with local (Exim 4.84) (envelope-from ) id 1b7Lhm-0000On-Uc; Mon, 30 May 2016 07:49:58 -0400 Received: (nullmailer pid 14855 invoked by uid 1000); Mon, 30 May 2016 11:50:06 -0000 From: David Bremner To: notmuch@notmuchmail.org Subject: [RFC2 Patch 4/5] lib: extend private string map API with iterators Date: Mon, 30 May 2016 08:49:58 -0300 Message-Id: <1464608999-14774-5-git-send-email-david@tethera.net> X-Mailer: git-send-email 2.8.1 In-Reply-To: <1464608999-14774-1-git-send-email-david@tethera.net> References: <1463927339-5441-1-git-send-email-david@tethera.net> <1464608999-14774-1-git-send-email-david@tethera.net> X-BeenThere: notmuch@notmuchmail.org X-Mailman-Version: 2.1.20 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 May 2016 11:50:27 -0000 Support for prefix based iterators is perhaps overengineering, but I wanted to mimic the existing database_config API. --- lib/notmuch-private.h | 21 ++++++++++++++- lib/string-map.c | 72 +++++++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 92 insertions(+), 1 deletion(-) diff --git a/lib/notmuch-private.h b/lib/notmuch-private.h index 531b82f..98efdaf 100644 --- a/lib/notmuch-private.h +++ b/lib/notmuch-private.h @@ -539,7 +539,7 @@ _notmuch_string_list_sort (notmuch_string_list_t *list); /* string-map.c */ typedef struct _notmuch_string_map notmuch_string_map_t; - +typedef struct _notmuch_string_map_iterator notmuch_string_map_iterator_t; notmuch_string_map_t * _notmuch_string_map_create (const void *ctx); @@ -551,6 +551,25 @@ _notmuch_string_map_append (notmuch_string_map_t *map, const char * _notmuch_string_map_get (notmuch_string_map_t *map, const char *key); +notmuch_string_map_iterator_t * +_notmuch_string_map_iterator_create (notmuch_string_map_t *map, const char *key, + notmuch_bool_t exact); + +notmuch_bool_t +_notmuch_string_map_iterator_valid (notmuch_string_map_iterator_t *iter); + +void +_notmuch_string_map_iterator_move_to_next (notmuch_string_map_iterator_t *iter); + +const char * +_notmuch_string_map_iterator_key (notmuch_string_map_iterator_t *iterator); + +const char * +_notmuch_string_map_iterator_value (notmuch_string_map_iterator_t *iterator); + +void +_notmuch_string_map_iterator_destroy (notmuch_string_map_iterator_t *iterator); + /* tags.c */ notmuch_tags_t * diff --git a/lib/string-map.c b/lib/string-map.c index 0491a10..591ff6d 100644 --- a/lib/string-map.c +++ b/lib/string-map.c @@ -38,6 +38,12 @@ struct _notmuch_string_map { notmuch_string_pair_t *pairs; }; +struct _notmuch_string_map_iterator { + notmuch_string_pair_t *current; + notmuch_bool_t exact; + const char *key; +}; + notmuch_string_map_t * _notmuch_string_map_create (const void *ctx) { @@ -151,3 +157,69 @@ _notmuch_string_map_get (notmuch_string_map_t *map, const char *key) return pair->value; } + +notmuch_string_map_iterator_t * +_notmuch_string_map_iterator_create (notmuch_string_map_t *map, const char *key, + notmuch_bool_t exact) +{ + notmuch_string_map_iterator_t *iter; + + _notmuch_string_map_sort (map); + + iter = talloc (map, notmuch_string_map_iterator_t); + if (unlikely (iter == NULL)) + return NULL; + + iter->key = talloc_strdup (iter, key); + iter->exact = exact; + iter->current = bsearch_first (map->pairs, map->length, key, exact); + return iter; +} + +notmuch_bool_t +_notmuch_string_map_iterator_valid (notmuch_string_map_iterator_t *iterator) +{ + if (iterator->current == NULL) + return FALSE; + + /* sentinel */ + if (iterator->current->key == NULL) + return FALSE; + + return (0 == string_cmp (iterator->key, iterator->current->key, iterator->exact)); + +} + +void +_notmuch_string_map_iterator_move_to_next (notmuch_string_map_iterator_t *iterator) +{ + + if (! _notmuch_string_map_iterator_valid (iterator)) + return; + + (iterator->current)++; +} + +const char * +_notmuch_string_map_iterator_key (notmuch_string_map_iterator_t *iterator) +{ + if (! _notmuch_string_map_iterator_valid (iterator)) + return NULL; + + return iterator->current->key; +} + +const char * +_notmuch_string_map_iterator_value (notmuch_string_map_iterator_t *iterator) +{ + if (! _notmuch_string_map_iterator_valid (iterator)) + return NULL; + + return iterator->current->value; +} + +void +_notmuch_string_map_iterator_destroy (notmuch_string_map_iterator_t *iterator) +{ + talloc_free (iterator); +} -- 2.8.1