X-Git-Url: http://repo.macrolet.net/gitweb/?p=jscl.git;a=blobdiff_plain;f=src%2Fhash-table.lisp;fp=src%2Fhash-table.lisp;h=e47384e6fe2228e8b13b6507ef827a91e8461823;hp=301addde9614fb7bb0d1a453dd9a9460306173e9;hb=5ebb91faa8f2c565bf3307d8641474eee9909f5d;hpb=05b3840a974805eae28a84dd2986e55d5deac91d diff --git a/src/hash-table.lisp b/src/hash-table.lisp index 301addd..e47384e 100644 --- a/src/hash-table.lisp +++ b/src/hash-table.lisp @@ -22,6 +22,10 @@ ;;; `equalp'). ;;; +;;; Additionally, we want to iterate across the hash table +;;; key-values. So we use a cons (key . value) +;;; as value in the Javascript object. It implicitly gives the +;;; inverse mapping of strings to our objects. ;;; If a hash table has `eq' as test, we need to generate unique ;;; strings for each Lisp object. To do this, we tag the objects with @@ -73,15 +77,17 @@ `(hash-table ,hash-fn ,(new)))) (defun gethash (key hash-table &optional default) - (let ((obj (caddr hash-table)) - (hash (funcall (cadr hash-table) key))) - (values (oget obj hash) - (in hash obj)))) + (let* ((obj (caddr hash-table)) + (hash (funcall (cadr hash-table) key)) + (exists (in hash obj))) + (if exists + (values (cdr (oget obj hash)) t) + (values default nil)))) (defun sethash (new-value key hash-table) (let ((obj (caddr hash-table)) (hash (funcall (cadr hash-table) key))) - (oset new-value obj hash) + (oset (cons key new-value) obj hash) new-value)) @@ -115,3 +121,10 @@ (incf count)) (caddr hash-table)) count)) + + +(defun maphash (function hash-table) + (map-for-in (lambda (x) + (funcall function (car x) (cdr x))) + (caddr hash-table)) + nil)