unofficial mirror of guile-devel@gnu.org 
 help / color / mirror / Atom feed
* [PATCH] Add-native-hashtable-helper-functions
@ 2013-03-26 10:40 Nala Ginrut
  2013-03-26 12:54 ` Noah Lavine
  2013-03-26 21:58 ` Ludovic Courtès
  0 siblings, 2 replies; 12+ messages in thread
From: Nala Ginrut @ 2013-03-26 10:40 UTC (permalink / raw)
  To: guile-devel

[-- Attachment #1: Type: text/plain, Size: 285 bytes --]

Added three helper functions, they're so explicit that don't need any
docs in the manual I think. Docstring is enough.

* hash-items: get the amount of items in the hash table
* hash-size: return the size of hash table
* hash-keys: return all the keys of hash table as a list

Thanks!

[-- Attachment #2: 0001-Add-native-hashtable-helper-functions.patch --]
[-- Type: text/x-patch, Size: 2423 bytes --]

From 167e82e93ee59ac9a0244006ae9664d68877b4c8 Mon Sep 17 00:00:00 2001
From: Nala Ginrut <nalaginrut@gmail.com>
Date: Tue, 26 Mar 2013 16:00:54 +0800
Subject: [PATCH] Add native hashtable helper functions.

libguile/hashtab.c: Add hash-items and hash-size
libguile/hashtab.h

module/ice-9/boot-9.scm: Add hash-keys
---
 libguile/hashtab.c      |   19 +++++++++++++++++++
 libguile/hashtab.h      |    2 ++
 module/ice-9/boot-9.scm |    8 ++++++++
 3 files changed, 29 insertions(+)

diff --git a/libguile/hashtab.c b/libguile/hashtab.c
index 88cb199..b1e59b8 100644
--- a/libguile/hashtab.c
+++ b/libguile/hashtab.c
@@ -383,6 +383,25 @@ scm_i_rehash (SCM table,
     }
 }
 
+SCM_DEFINE (scm_hash_items, "hash-items", 1, 0, 0,
+            (SCM table),
+            "Get the amount of items in this hash table.")
+#define FUNC_NAME s_scm_hash_items
+{
+  SCM_VALIDATE_HASHTABLE (1, table);
+  return scm_from_uint (SCM_HASHTABLE_N_ITEMS (table));
+}
+#undef FUNC_NAME
+
+SCM_DEFINE (scm_hash_size, "hash-size", 1, 0, 0,
+            (SCM table),
+            "Get the size of this hash table.")
+#define FUNC_NAME s_scm_hash_size
+{
+  SCM_VALIDATE_HASHTABLE (1, table);
+  return scm_from_uint (SCM_SIMPLE_VECTOR_LENGTH (SCM_HASHTABLE_VECTOR (table)));
+}
+#undef FUNC_NAME
 
 void
 scm_i_hashtable_print (SCM exp, SCM port, scm_print_state *pstate)
diff --git a/libguile/hashtab.h b/libguile/hashtab.h
index dcebcb8..227299e 100644
--- a/libguile/hashtab.h
+++ b/libguile/hashtab.h
@@ -96,6 +96,8 @@ typedef struct scm_t_hashtable {
 
 SCM_API SCM scm_vector_to_hash_table (SCM vector);
 SCM_API SCM scm_c_make_hash_table (unsigned long k);
+SCM_API SCM scm_hash_table_size (SCM h);
+SCM_API SCM scm_hash_table_items (SCM h);
 SCM_API SCM scm_make_hash_table (SCM n);
 SCM_API SCM scm_make_weak_key_hash_table (SCM k);
 SCM_API SCM scm_make_weak_value_hash_table (SCM k);
diff --git a/module/ice-9/boot-9.scm b/module/ice-9/boot-9.scm
index ced3a28..ae1c49b 100644
--- a/module/ice-9/boot-9.scm
+++ b/module/ice-9/boot-9.scm
@@ -34,6 +34,14 @@
 
 \f
 
+;; Native hash-table helper functions.
+
+(define (hash-keys table)
+  "Return all the keys from hash table."
+  (hash-map->list (lambda (x y) x) table))
+
+\f
+
 ;; Before compiling, make sure any symbols are resolved in the (guile)
 ;; module, the primary location of those symbols, rather than in
 ;; (guile-user), the default module that we compile in.
-- 
1.7.10.4


^ permalink raw reply related	[flat|nested] 12+ messages in thread

end of thread, other threads:[~2013-03-27 20:21 UTC | newest]

Thread overview: 12+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2013-03-26 10:40 [PATCH] Add-native-hashtable-helper-functions Nala Ginrut
2013-03-26 12:54 ` Noah Lavine
2013-03-26 21:58 ` Ludovic Courtès
2013-03-27  0:47   ` Nala Ginrut
2013-03-27  5:10     ` Daniel Hartwig
2013-03-27  6:32       ` Nala Ginrut
2013-03-27  8:55         ` Daniel Hartwig
2013-03-27 10:25           ` Nala Ginrut
2013-03-27 10:03         ` Ludovic Courtès
2013-03-27 13:33   ` Mark H Weaver
2013-03-27 13:55     ` Nala Ginrut
2013-03-27 20:21     ` Ludovic Courtès

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).