Skip to content

Instantly share code, notes, and snippets.

@yamasushi
Created May 16, 2013 03:35
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save yamasushi/5589198 to your computer and use it in GitHub Desktop.
Save yamasushi/5589198 to your computer and use it in GitHub Desktop.
diff --git a/ext/sparse/sparse.scm b/ext/sparse/sparse.scm
index 76199b8..ed869cc 100644
--- a/ext/sparse/sparse.scm
+++ b/ext/sparse/sparse.scm
@@ -33,6 +33,7 @@
(define-module util.sparse
+ (use gauche.collection)
(use gauche.dictionary)
(export <sparse-table> make-sparse-table sparse-table-num-entries
sparse-table-ref sparse-table-set! sparse-table-exists?
@@ -302,3 +303,23 @@
:pop! sparse-vector-pop!
:push! sparse-vector-push!
:update! sparse-vector-update!)
+
+;;===================================================================
+;; collection protocol
+
+; collection.scmのhash-tableのものを流用した
+(define-method call-with-iterator ((coll <sparse-table>) proc :allow-other-keys)
+ (let ([eof-marker (cons #f #f)]
+ [iter (%sparse-table-iter coll)])
+ (receive (k v) (iter eof-marker)
+ (proc (cut eq? k eof-marker)
+ (^[] (begin0 (cons k v)
+ (set!-values (k v) (iter eof-marker))))))))
+
+(define-method call-with-iterator ((coll <sparse-vector-base>) proc :allow-other-keys)
+ (let ([eof-marker (cons #f #f)]
+ [iter (%sparse-vector-iter coll)])
+ (receive (k v) (iter eof-marker)
+ (proc (cut eq? k eof-marker)
+ (^[] (begin0 (cons k v)
+ (set!-values (k v) (iter eof-marker))))))))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment