X-Git-Url: https://git.notmuchmail.org/git?p=notmuch;a=blobdiff_plain;f=lib%2Fstring-map.c;h=a88404c734ff5a93ab98349bcc7913229bd19e47;hp=0491a10bf6a6be5206a320cef85f92bfebc94dad;hb=9f68919ee7d7c8c9cc7c2231ddc9f970a0ae1aa3;hpb=8b03ee1d5a310f82718281362d105ff09e30148f diff --git a/lib/string-map.c b/lib/string-map.c index 0491a10b..a88404c7 100644 --- a/lib/string-map.c +++ b/lib/string-map.c @@ -14,7 +14,7 @@ * GNU General Public License for more details. * * You should have received a copy of the GNU General Public License - * along with this program. If not, see http://www.gnu.org/licenses/ . + * along with this program. If not, see https://www.gnu.org/licenses/ . * * Author: David Bremner */ @@ -33,11 +33,17 @@ typedef struct _notmuch_string_pair_t { } notmuch_string_pair_t; struct _notmuch_string_map { - notmuch_bool_t sorted; + bool sorted; size_t length; notmuch_string_pair_t *pairs; }; +struct _notmuch_string_map_iterator { + notmuch_string_pair_t *current; + bool exact; + const char *key; +}; + notmuch_string_map_t * _notmuch_string_map_create (const void *ctx) { @@ -49,7 +55,7 @@ _notmuch_string_map_create (const void *ctx) map->length = 0; map->pairs = NULL; - map->sorted = TRUE; + map->sorted = true; return map; } @@ -61,7 +67,7 @@ _notmuch_string_map_append (notmuch_string_map_t *map, { map->length++; - map->sorted = FALSE; + map->sorted = false; if (map->pairs) map->pairs = talloc_realloc (map, map->pairs, notmuch_string_pair_t, map->length + 1); @@ -97,11 +103,11 @@ _notmuch_string_map_sort (notmuch_string_map_t *map) qsort (map->pairs, map->length, sizeof (notmuch_string_pair_t), cmppair); - map->sorted = TRUE; + map->sorted = true; } -static notmuch_bool_t -string_cmp (const char *a, const char *b, notmuch_bool_t exact) +static int +string_cmp (const char *a, const char *b, bool exact) { if (exact) return (strcmp (a, b)); @@ -110,7 +116,7 @@ string_cmp (const char *a, const char *b, notmuch_bool_t exact) } static notmuch_string_pair_t * -bsearch_first (notmuch_string_pair_t *array, size_t len, const char *key, notmuch_bool_t exact) +bsearch_first (notmuch_string_pair_t *array, size_t len, const char *key, bool exact) { size_t first = 0; size_t last = len - 1; @@ -145,9 +151,78 @@ _notmuch_string_map_get (notmuch_string_map_t *map, const char *key) /* this means that calling append invalidates iterators */ _notmuch_string_map_sort (map); - pair = bsearch_first (map->pairs, map->length, key, TRUE); + pair = bsearch_first (map->pairs, map->length, key, true); if (! pair) return NULL; return pair->value; } + +notmuch_string_map_iterator_t * +_notmuch_string_map_iterator_create (notmuch_string_map_t *map, const char *key, + bool 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; + + if (unlikely (talloc_reference (iter, map) == NULL)) + return NULL; + + iter->key = talloc_strdup (iter, key); + iter->exact = exact; + iter->current = bsearch_first (map->pairs, map->length, key, exact); + return iter; +} + +bool +_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); +}