aboutsummaryrefslogtreecommitdiff
path: root/lib/string-map.c
diff options
context:
space:
mode:
authorDavid Bremner <david@tethera.net>2016-06-12 22:05:51 -0300
committerDavid Bremner <david@tethera.net>2016-09-21 18:14:24 -0300
commitb846bdb48233c907d1ecaff495fbf6bc578910d6 (patch)
tree6a25b8219edc583ac098c789414ef3154ac9a96b /lib/string-map.c
parentb8bb6d796458732622f80464dd808b3e02f57d9d (diff)
lib: extend private string map API with iterators
Support for prefix based iterators is perhaps overengineering, but I wanted to mimic the existing database_config API.
Diffstat (limited to 'lib/string-map.c')
-rw-r--r--lib/string-map.c72
1 files changed, 72 insertions, 0 deletions
diff --git a/lib/string-map.c b/lib/string-map.c
index 0491a10b..591ff6d5 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);
+}