From patchwork Thu Jul 1 19:21:40 2010 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Luiz Capitulino X-Patchwork-Id: 57580 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 47855B70B8 for ; Fri, 2 Jul 2010 05:36:10 +1000 (EST) Received: from localhost ([127.0.0.1]:43306 helo=lists.gnu.org) by lists.gnu.org with esmtp (Exim 4.43) id 1OUPYL-000714-6A for incoming@patchwork.ozlabs.org; Thu, 01 Jul 2010 15:36:05 -0400 Received: from [140.186.70.92] (port=37711 helo=eggs.gnu.org) by lists.gnu.org with esmtp (Exim 4.43) id 1OUPLI-0006aZ-S3 for qemu-devel@nongnu.org; Thu, 01 Jul 2010 15:22:56 -0400 Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.69) (envelope-from ) id 1OUPL8-0007MC-39 for qemu-devel@nongnu.org; Thu, 01 Jul 2010 15:22:36 -0400 Received: from mx1.redhat.com ([209.132.183.28]:48009) by eggs.gnu.org with esmtp (Exim 4.69) (envelope-from ) id 1OUPL7-0007M6-Ol for qemu-devel@nongnu.org; Thu, 01 Jul 2010 15:22:26 -0400 Received: from int-mx04.intmail.prod.int.phx2.redhat.com (int-mx04.intmail.prod.int.phx2.redhat.com [10.5.11.17]) by mx1.redhat.com (8.13.8/8.13.8) with ESMTP id o61JMP8j022243 (version=TLSv1/SSLv3 cipher=DHE-RSA-AES256-SHA bits=256 verify=OK); Thu, 1 Jul 2010 15:22:25 -0400 Received: from localhost (vpn-10-238.rdu.redhat.com [10.11.10.238]) by int-mx04.intmail.prod.int.phx2.redhat.com (8.13.8/8.13.8) with ESMTP id o61JMN7t020284; Thu, 1 Jul 2010 15:22:24 -0400 From: Luiz Capitulino To: aliguori@us.ibm.com Date: Thu, 1 Jul 2010 16:21:40 -0300 Message-Id: <1278012111-26227-13-git-send-email-lcapitulino@redhat.com> In-Reply-To: <1278012111-26227-1-git-send-email-lcapitulino@redhat.com> References: <1278012111-26227-1-git-send-email-lcapitulino@redhat.com> X-Scanned-By: MIMEDefang 2.67 on 10.5.11.17 X-detected-operating-system: by eggs.gnu.org: Genre and OS details not recognized. Cc: qemu-devel@nongnu.org Subject: [Qemu-devel] [PATCH 12/23] 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) \