X-Git-Url: http://repo.macrolet.net/gitweb/?a=blobdiff_plain;f=src%2Fhash-table.lisp;h=dc4f4180b194b9b25fc224c01eeef887e0c03b22;hb=d0e2cc2ea3ae036fba1d085b9c88a5ffe24de956;hp=0641ca821280e975682e6b6e40f42d9930ecb5fb;hpb=85f0d5790e87525e7bbde14ad21935ab683a328f;p=jscl.git diff --git a/src/hash-table.lisp b/src/hash-table.lisp index 0641ca8..dc4f418 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)) @@ -106,3 +112,19 @@ (hash (funcall (cadr hash-table) key))) (prog1 (in hash obj) (delete-property hash obj)))) + + +(defun hash-table-count (hash-table) + (let ((count 0)) + (map-for-in (lambda (x) + (declare (ignore x)) + (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)