From patchwork Thu Jun 24 21:33:30 2010 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Luiz Capitulino X-Patchwork-Id: 56856 Return-Path: X-Original-To: incoming@patchwork.ozlabs.org Delivered-To: patchwork-incoming@bilbo.ozlabs.org Received: from lists.gnu.org (lists.gnu.org [199.232.76.165]) (using TLSv1 with cipher DHE-RSA-AES256-SHA (256/256 bits)) (Client did not present a certificate) by ozlabs.org (Postfix) with ESMTPS id 4120AB6F29 for ; Fri, 25 Jun 2010 07:51:48 +1000 (EST) Received: from localhost ([127.0.0.1]:58804 helo=lists.gnu.org) by lists.gnu.org with esmtp (Exim 4.43) id 1ORuKn-0006s9-NG for incoming@patchwork.ozlabs.org; Thu, 24 Jun 2010 17:51:45 -0400 Received: from [140.186.70.92] (port=51712 helo=eggs.gnu.org) by lists.gnu.org with esmtp (Exim 4.43) id 1ORu3c-0000fg-3R for qemu-devel@nongnu.org; Thu, 24 Jun 2010 17:34:01 -0400 Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.69) (envelope-from ) id 1ORu3a-0001b5-QZ for qemu-devel@nongnu.org; Thu, 24 Jun 2010 17:33:59 -0400 Received: from mx1.redhat.com ([209.132.183.28]:18055) by eggs.gnu.org with esmtp (Exim 4.69) (envelope-from ) id 1ORu3a-0001az-Fd for qemu-devel@nongnu.org; Thu, 24 Jun 2010 17:33:58 -0400 Received: from int-mx02.intmail.prod.int.phx2.redhat.com (int-mx02.intmail.prod.int.phx2.redhat.com [10.5.11.12]) by mx1.redhat.com (8.13.8/8.13.8) with ESMTP id o5OLXvuT007961 (version=TLSv1/SSLv3 cipher=DHE-RSA-AES256-SHA bits=256 verify=OK) for ; Thu, 24 Jun 2010 17:33:57 -0400 Received: from localhost (vpn-9-104.rdu.redhat.com [10.11.9.104]) by int-mx02.intmail.prod.int.phx2.redhat.com (8.13.8/8.13.8) with ESMTP id o5OLXuEa021466; Thu, 24 Jun 2010 17:33:57 -0400 From: Luiz Capitulino To: qemu-devel@nongnu.org Date: Thu, 24 Jun 2010 18:33:30 -0300 Message-Id: <1277415220-17810-5-git-send-email-lcapitulino@redhat.com> In-Reply-To: <1277415220-17810-1-git-send-email-lcapitulino@redhat.com> References: <1277415220-17810-1-git-send-email-lcapitulino@redhat.com> X-Scanned-By: MIMEDefang 2.67 on 10.5.11.12 X-detected-operating-system: by eggs.gnu.org: Genre and OS details not recognized. Cc: armbru@redhat.com Subject: [Qemu-devel] [PATCH 04/14] QDict: Introduce new iteration API X-BeenThere: qemu-devel@nongnu.org X-Mailman-Version: 2.1.5 Precedence: list List-Id: qemu-devel.nongnu.org List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Sender: qemu-devel-bounces+incoming=patchwork.ozlabs.org@nongnu.org Errors-To: qemu-devel-bounces+incoming=patchwork.ozlabs.org@nongnu.org It's composed of functions qdict_first() and qdict_next(), plus functions to access QDictEntry values. This API was suggested by Markus Armbruster 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 --- qdict.c | 37 +++++++++++++++++++++++++++++++++++++ qdict.h | 2 ++ 2 files changed, 39 insertions(+), 0 deletions(-) diff --git a/qdict.c b/qdict.c index c467763..a28a0a9 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 */ diff --git a/qdict.h b/qdict.h index 0c8de3c..0e7a43f 100644 --- a/qdict.h +++ b/qdict.h @@ -45,6 +45,8 @@ QDict *qobject_to_qdict(const QObject *obj); void qdict_iter(const QDict *qdict, void (*iter)(const char *key, QObject *obj, void *opaque), void *opaque); +const QDictEntry *qdict_first(const QDict *qdict); +const QDictEntry *qdict_next(const QDict *qdict, const QDictEntry *entry); /* Helper to qdict_put_obj(), accepts any object */ #define qdict_put(qdict, key, obj) \