From mboxrd@z Thu Jan 1 00:00:00 1970 Path: news.gmane.org!not-for-mail From: David Thompson Newsgroups: gmane.lisp.guile.devel Subject: Re: [PATCH] Add procedures to convert alists into hash tables Date: Sun, 20 Oct 2013 10:28:33 -0400 Message-ID: <5263E891.3030801@worcester.edu> References: <5263E55A.6040307@worcester.edu> NNTP-Posting-Host: plane.gmane.org Mime-Version: 1.0 Content-Type: multipart/mixed; boundary="------------010401040402040408080001" X-Trace: ger.gmane.org 1382279330 14479 80.91.229.3 (20 Oct 2013 14:28:50 GMT) X-Complaints-To: usenet@ger.gmane.org NNTP-Posting-Date: Sun, 20 Oct 2013 14:28:50 +0000 (UTC) To: guile-devel Original-X-From: guile-devel-bounces+guile-devel=m.gmane.org@gnu.org Sun Oct 20 16:28:52 2013 Return-path: Envelope-to: guile-devel@m.gmane.org Original-Received: from lists.gnu.org ([208.118.235.17]) by plane.gmane.org with esmtp (Exim 4.69) (envelope-from ) id 1VXtzv-0004Hq-IT for guile-devel@m.gmane.org; Sun, 20 Oct 2013 16:28:51 +0200 Original-Received: from localhost ([::1]:36321 helo=lists.gnu.org) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1VXtzv-0007YO-5I for guile-devel@m.gmane.org; Sun, 20 Oct 2013 10:28:51 -0400 Original-Received: from eggs.gnu.org ([2001:4830:134:3::10]:55311) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1VXtzk-0007QJ-Ft for guile-devel@gnu.org; Sun, 20 Oct 2013 10:28:47 -0400 Original-Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1VXtzd-00083V-F5 for guile-devel@gnu.org; Sun, 20 Oct 2013 10:28:40 -0400 Original-Received: from na3sys009aog109.obsmtp.com ([74.125.149.201]:35640) by eggs.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1VXtzd-00082Z-47 for guile-devel@gnu.org; Sun, 20 Oct 2013 10:28:33 -0400 Original-Received: from mail-qe0-f44.google.com ([209.85.128.44]) (using TLSv1) by na3sys009aob109.postini.com ([74.125.148.12]) with SMTP ID DSNKUmPoj5uqyUTor6Yd8om71FaWTdC9z1Mh@postini.com; Sun, 20 Oct 2013 07:28:32 PDT Original-Received: by mail-qe0-f44.google.com with SMTP id 6so3175364qeb.17 for ; Sun, 20 Oct 2013 07:28:31 -0700 (PDT) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20130820; h=x-gm-message-state:message-id:date:from:user-agent:mime-version:to :subject:references:in-reply-to:content-type; bh=Vb7jfpNYx+gBJix4vwLyTBmrJMePh+f5hDza4GBxVAQ=; b=TXgHY0fPCrhBmGQbPqNkvHv9obAqzhK7T2nXcZ++NwMtVs5uw2K+D4AhTcjNDxBcmH pNOss7iMe/nRs3oHtzoRfusPYiq7gdlO75I2d0faH6aTkJx4rwQyqd3XUGfETluwM8eP YDt6ks8BpTnUKD6nashzVwIbUjgQQ4MA34gDBPjKplVVOwF4kW8BotFXhRKNyhAIV8E+ ztss7ZIIYsM8RI49hcn8uv1C0eG4ZlvwveHoyLeDV6LzHw/WU7+oGq4/HViWiEpUHFrE I4xVpC+WgzmnWIBF+VspEjKk6sKFkrGJ4gUhJvexc8F58HvppVgyHCMtM7k2tjLsnfOn d8Dg== X-Gm-Message-State: ALoCoQmYyReKBHGwTxNYaj5Hdjz8PWPCobhD1TfUoeOMtglfR69sfvIjPQVuv0drk2wNel5VGe3T5tsPHYdlNXG84zGiXy7c1WW5a6+S2QKQqQr8Y8S1wJns9LydgE36qhDRGW8vhe2xStPbn2UON/Y+oJkqaZV1mA== X-Received: by 10.224.51.131 with SMTP id d3mr17806986qag.0.1382279311328; Sun, 20 Oct 2013 07:28:31 -0700 (PDT) X-Received: by 10.224.51.131 with SMTP id d3mr17806979qag.0.1382279311254; Sun, 20 Oct 2013 07:28:31 -0700 (PDT) Original-Received: from [192.168.1.124] (209-6-40-86.c3-0.smr-ubr1.sbo-smr.ma.cable.rcn.com. [209.6.40.86]) by mx.google.com with ESMTPSA id fx6sm22621009qeb.1.2013.10.20.07.28.30 for (version=TLSv1 cipher=ECDHE-RSA-RC4-SHA bits=128/128); Sun, 20 Oct 2013 07:28:30 -0700 (PDT) User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:17.0) Gecko/20131005 Icedove/17.0.9 In-Reply-To: <5263E55A.6040307@worcester.edu> X-detected-operating-system: by eggs.gnu.org: GNU/Linux 2.4.x X-Received-From: 74.125.149.201 X-BeenThere: guile-devel@gnu.org X-Mailman-Version: 2.1.14 Precedence: list List-Id: "Developers list for Guile, the GNU extensibility library" List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: guile-devel-bounces+guile-devel=m.gmane.org@gnu.org Original-Sender: guile-devel-bounces+guile-devel=m.gmane.org@gnu.org Xref: news.gmane.org gmane.lisp.guile.devel:16686 Archived-At: This is a multi-part message in MIME format. --------------010401040402040408080001 Content-Type: text/plain; charset=ISO-8859-1; format=flowed Content-Transfer-Encoding: 7bit On 10/20/2013 10:14 AM, David Thompson wrote: > Hello all, > > When looking through the different hash table implementations > available (Guile, SRFI-69, and RNRS) I found a useful SRFI-69 > procedure that had no equivalent in Guile's native hash table API: > alist->hash-table. > > This patch is an attempt to add that. It works for all four types of > hash tables: equal?, eq?, eqv? and custom. > > - Dave Found an inconsistency in the docs. Updated patch attached. - Dave --------------010401040402040408080001 Content-Type: text/x-patch; name="0001-Add-procedures-to-convert-alists-into-hash-tables.patch" Content-Transfer-Encoding: 7bit Content-Disposition: attachment; filename*0="0001-Add-procedures-to-convert-alists-into-hash-tables.patch" >From 46b7905727ad2efed2dc1d1aca4d4ad00d8f48c5 Mon Sep 17 00:00:00 2001 From: David Thompson Date: Sat, 19 Oct 2013 22:43:37 -0400 Subject: [PATCH] Add procedures to convert alists into hash tables. * libguile/hashtab.c (scm_alist_to_hash_table, scm_alist_to_hashq_table, scm_alist_to_hashv_table, scm_alist_to_hashx_table): Add the equivalent of SRFI-69's alist->hash-table procedure for the native hash table implementation. * test-suite/tests/hash.test ("alist->hash-table"): Add tests. * doc/ref/api-compound.texi (alist->hash-table): Add docs. --- doc/ref/api-compound.texi | 18 ++++++++++++++ libguile/hashtab.c | 61 ++++++++++++++++++++++++++++++++++++++++++++++ libguile/hashtab.h | 6 +++++ test-suite/tests/hash.test | 27 ++++++++++++++++++++ 4 files changed, 112 insertions(+) diff --git a/doc/ref/api-compound.texi b/doc/ref/api-compound.texi index 94e0145..06115be 100644 --- a/doc/ref/api-compound.texi +++ b/doc/ref/api-compound.texi @@ -3829,6 +3829,24 @@ then it can use @var{size} to avoid rehashing when initial entries are added. @end deffn +@deffn {Scheme Procedure} alist->hash-table alist [size] +@deffnx {Scheme Procedure} alist->hashq-table alist [size] +@deffnx {Scheme Procedure} alist->hashv-table alist [size] +@deffnx {Scheme Procedure} alist->hashx-table hash assoc alist [size] +@deffnx {C Function} scm_alist_to_hash_table (alist, size) +@deffnx {C Function} scm_alist_to_hashq_table (alist, size) +@deffnx {C Function} scm_alist_to_hashv_table (alist, size) +@deffnx {C Function} scm_alist_to_hashx_table (hash, assoc, alist, size) +Convert @var{alist} into a hash table with minimum number of buckets +@var{size}. When keys are repeated in @var{alist}, the leftmost +association takes precedence. + +@example +(alist->hash-table '((foo . 1) (bar . 2))) +@end example + +@end deffn + @deffn {Scheme Procedure} hash-table? obj @deffnx {C Function} scm_hash_table_p (obj) Return @code{#t} if @var{obj} is a abstract hash table object. diff --git a/libguile/hashtab.c b/libguile/hashtab.c index 88cb199..c215269 100644 --- a/libguile/hashtab.c +++ b/libguile/hashtab.c @@ -423,6 +423,67 @@ SCM_DEFINE (scm_make_hash_table, "make-hash-table", 0, 1, 0, } #undef FUNC_NAME +#define SCM_ALIST_TO_HASH_TABLE(alist, n, hash_set_fn) \ + SCM hash_table; \ + SCM_VALIDATE_LIST (1, alist); \ + hash_table = scm_make_hash_table (n); \ + while (!scm_is_null (alist)) { \ + SCM pair = SCM_CAR (alist); \ + hash_set_fn (hash_table, scm_car (pair), scm_cdr (pair)); \ + alist = SCM_CDR (alist); \ + } \ + return hash_table; + +SCM_DEFINE (scm_alist_to_hash_table, "alist->hash-table", 1, 1, 0, + (SCM alist, SCM n), + "Convert @var{alist} into a hash table with minimum number of " + "buckets @var{n}.") +#define FUNC_NAME s_scm_alist_to_hash_table +{ + SCM_ALIST_TO_HASH_TABLE (alist, n, scm_hash_set_x); +} +#undef FUNC_NAME + +SCM_DEFINE (scm_alist_to_hashq_table, "alist->hashq-table", 1, 1, 0, + (SCM alist, SCM n), + "Convert @var{alist} into a hash table with minimum number of " + "buckets @var{n}.") +#define FUNC_NAME s_scm_alist_to_hashq_table +{ + SCM_ALIST_TO_HASH_TABLE (alist, n, scm_hashq_set_x); +} +#undef FUNC_NAME + +SCM_DEFINE (scm_alist_to_hashv_table, "alist->hashv-table", 1, 1, 0, + (SCM alist, SCM n), + "Convert @var{alist} into a hash table with minimum number of " + "buckets @var{n}.") +#define FUNC_NAME s_scm_alist_to_hashv_table +{ + SCM_ALIST_TO_HASH_TABLE (alist, n, scm_hashv_set_x); +} +#undef FUNC_NAME + +SCM_DEFINE (scm_alist_to_hashx_table, "alist->hashx-table", 3, 1, 0, + (SCM hash, SCM assoc, SCM alist, SCM n), + "Convert @var{alist} into a hash table with minimum number of " + "buckets @var{n}.") +#define FUNC_NAME s_scm_alist_to_hashx_table +{ + SCM hash_table; + SCM_VALIDATE_LIST (3, alist); + hash_table = scm_make_hash_table (n); + + while (!scm_is_null (alist)) { + SCM pair = SCM_CAR (alist); + scm_hashx_set_x (hash, assoc, hash_table, scm_car (pair), scm_cdr (pair)); + alist = SCM_CDR (alist); + } + + return hash_table; +} +#undef FUNC_NAME + /* The before-gc C hook only runs if GC_set_start_callback is available, so if not, fall back on a finalizer-based implementation. */ static int diff --git a/libguile/hashtab.h b/libguile/hashtab.h index dcebcb8..da4f28c 100644 --- a/libguile/hashtab.h +++ b/libguile/hashtab.h @@ -101,6 +101,12 @@ SCM_API SCM scm_make_weak_key_hash_table (SCM k); SCM_API SCM scm_make_weak_value_hash_table (SCM k); SCM_API SCM scm_make_doubly_weak_hash_table (SCM k); +SCM_API SCM scm_alist_to_hash_table (SCM alist, SCM n); +SCM_API SCM scm_alist_to_hashq_table (SCM alist, SCM n); +SCM_API SCM scm_alist_to_hashv_table (SCM alist, SCM n); +SCM_API SCM scm_alist_to_hashx_table (SCM hash, SCM assoc, + SCM alist, SCM n); + SCM_API SCM scm_hash_table_p (SCM h); SCM_API SCM scm_weak_key_hash_table_p (SCM h); SCM_API SCM scm_weak_value_hash_table_p (SCM h); diff --git a/test-suite/tests/hash.test b/test-suite/tests/hash.test index 3bd4004..b09c0b8 100644 --- a/test-suite/tests/hash.test +++ b/test-suite/tests/hash.test @@ -81,6 +81,33 @@ (write (make-hash-table 100))))))) ;;; +;;; alist->hash-table +;;; + +(with-test-prefix + "alist->hash-table" + + ;; equal? hash table + (pass-if (let ((table (alist->hash-table '(("foo" . 1) ("bar" . 2))))) + (and (= (hash-ref table "foo") 1) + (= (hash-ref table "bar") 2)))) + + ;; eq? hash table + (pass-if (let ((table (alist->hashq-table '((foo . 1) (bar . 2))))) + (and (= (hashq-ref table 'foo) 1) + (= (hashq-ref table 'bar) 2)))) + + ;; eqv? hash table + (pass-if (let ((table (alist->hashv-table '((1 . 1) (2 . 2))))) + (and (= (hashv-ref table 1) 1) + (= (hashv-ref table 2) 2)))) + + ;; custom hash table + (pass-if (let ((table (alist->hashx-table hash assoc '((foo . 1) (bar . 2))))) + (and (= (hashx-ref hash assoc table 'foo) 1) + (= (hashx-ref hash assoc table 'bar) 2))))) + +;;; ;;; usual set and reference ;;; -- 1.8.4.rc3 --------------010401040402040408080001--