aboutsummaryrefslogtreecommitdiff
path: root/qdict.c
diff options
context:
space:
mode:
authorLuiz Capitulino <lcapitulino@redhat.com>2010-06-07 16:07:29 -0300
committerLuiz Capitulino <lcapitulino@redhat.com>2010-07-01 14:27:13 -0300
commitf2b07f35d2ee1c6a501d1e3c68c0db42c70751fd (patch)
tree765bea2ee1cbe966fedcf962e00e48aedf343d19 /qdict.c
parent0d078b2adec72fd74b00defb260724b33e87e184 (diff)
QDict: Introduce new iteration API
It's composed of functions qdict_first() and qdict_next(), plus functions to access QDictEntry values. This API was suggested by Markus Armbruster <armbru@redhat.com> and it offers full control over the iteration process. The usage is simple, the following example prints all keys in 'qdict' (it's hopefully better than any English description): QDict *qdict; const QDictEntry *ent; [...] for (ent = qdict_first(qdict); ent; ent = qdict_next(qdict, ent)) { printf("%s ", qdict_entry_key(ent)); } Signed-off-by: Luiz Capitulino <lcapitulino@redhat.com>
Diffstat (limited to 'qdict.c')
-rw-r--r--qdict.c37
1 files changed, 37 insertions, 0 deletions
diff --git a/qdict.c b/qdict.c
index c4677636e3..a28a0a9f97 100644
--- a/qdict.c
+++ b/qdict.c
@@ -345,6 +345,43 @@ void qdict_iter(const QDict *qdict,
}
}
+static QDictEntry *qdict_next_entry(const QDict *qdict, int first_bucket)
+{
+ int i;
+
+ for (i = first_bucket; i < QDICT_BUCKET_MAX; i++) {
+ if (!QLIST_EMPTY(&qdict->table[i])) {
+ return QLIST_FIRST(&qdict->table[i]);
+ }
+ }
+
+ return NULL;
+}
+
+/**
+ * qdict_first(): Return first qdict entry for iteration.
+ */
+const QDictEntry *qdict_first(const QDict *qdict)
+{
+ return qdict_next_entry(qdict, 0);
+}
+
+/**
+ * qdict_next(): Return next qdict entry in an iteration.
+ */
+const QDictEntry *qdict_next(const QDict *qdict, const QDictEntry *entry)
+{
+ QDictEntry *ret;
+
+ ret = QLIST_NEXT(entry, next);
+ if (!ret) {
+ unsigned int bucket = tdb_hash(entry->key) % QDICT_BUCKET_MAX;
+ ret = qdict_next_entry(qdict, bucket + 1);
+ }
+
+ return ret;
+}
+
/**
* qentry_destroy(): Free all the memory allocated by a QDictEntry
*/