hash-count

This commit is contained in:
Danny Yoo 2011-11-07 19:07:34 -05:00
parent c02c40f93e
commit 4c4d4437b8
6 changed files with 28 additions and 1 deletions

View File

@ -109,7 +109,8 @@
'hash-remove! 'hash-remove!
'hash-remove 'hash-remove
'equal-hash-code 'equal-hash-code
'hash-count
'string-copy 'string-copy
)) ))
(define-predicate KernelPrimitiveName? KernelPrimitiveName) (define-predicate KernelPrimitiveName? KernelPrimitiveName)

View File

@ -137,6 +137,10 @@
return new WhalesongHashtable(this.type, this.hash_function, this.equality_function, this.hash.clone()); return new WhalesongHashtable(this.type, this.hash_function, this.equality_function, this.hash.clone());
}; };
WhalesongHashtable.prototype.size = function() {
return this.hash.size();
};
WhalesongHashtable.prototype.toWrittenString = function (cache) { WhalesongHashtable.prototype.toWrittenString = function (cache) {
var keys = this.hash.keys(); var keys = this.hash.keys();
var ret = [], i; var ret = [], i;
@ -251,6 +255,11 @@
this.map = map; this.map = map;
}; };
WhalesongImmutableHashtable.prototype.size = function() {
return this.map.items().length;
};
WhalesongImmutableHashtable.prototype.toWrittenString = function (cache) { WhalesongImmutableHashtable.prototype.toWrittenString = function (cache) {
var items = this.map.items(); var items = this.map.items();
var ret = [], i; var ret = [], i;

View File

@ -2578,6 +2578,13 @@
var hash = checkMutableHash(M, 'hash-copy', 0); var hash = checkMutableHash(M, 'hash-copy', 0);
return hash.clone(); return hash.clone();
}); });
installPrimitiveProcedure(
'hash-count',
1,
function(M) {
return checkHash(M, 'hash-count', 0).size();
});
installPrimitiveProcedure( installPrimitiveProcedure(
'hash', 'hash',

View File

@ -168,6 +168,7 @@
hash-remove! hash-remove!
hash-remove hash-remove
equal-hash-code equal-hash-code
hash-count

View File

@ -55,3 +55,6 @@ true
A A
alphabet alphabet
1
2

View File

@ -132,3 +132,9 @@
(hash-set! h2 'a "alphabet") (hash-set! h2 'a "alphabet")
(displayln (hash-ref h1 'a)) (displayln (hash-ref h1 'a))
(displayln (hash-ref h2 'a))) (displayln (hash-ref h2 'a)))
(newline)
(hash-count (make-hash '((a . a))))
(hash-count (make-immutable-hash '((b . b)
(c . d))))