factor/library/hashtables.factor

164 lines
4.4 KiB
Factor
Raw Normal View History

! Copyright (C) 2004, 2005 Slava Pestov.
! See http://factor.sf.net/license.txt for BSD license.
IN: kernel-internals
DEFER: hash-array
DEFER: set-hash-array
DEFER: set-hash-size
IN: hashtables
2005-04-02 02:39:33 -05:00
USING: generic kernel lists math sequences vectors ;
2004-07-16 02:26:21 -04:00
! We put hash-size in the hashtables vocabulary, and
! the other words in kernel-internals.
2005-01-27 20:06:10 -05:00
BUILTIN: hashtable 10
[ 1 "hash-size" set-hash-size ]
[ 2 hash-array set-hash-array ] ;
2005-01-27 20:06:10 -05:00
! A hashtable is implemented as an array of buckets. The
! array index is determined using a hash function, and the
! buckets are associative lists which are searched
! linearly.
2005-01-28 23:55:22 -05:00
! The unsafe words go in kernel internals. Everything else, even
! if it is somewhat 'implementation detail', is in the
! public 'hashtables' vocabulary.
2005-01-27 20:06:10 -05:00
IN: kernel-internals
: hash-bucket ( n hash -- alist )
>r >fixnum r> hash-array array-nth ;
: set-hash-bucket ( obj n hash -- )
>r >fixnum r> hash-array set-array-nth ;
2005-01-28 23:55:22 -05:00
: change-bucket ( n hash quot -- )
-rot hash-array
[ array-nth swap call ] 2keep
set-array-nth ; inline
: hash-size+ ( hash -- ) dup hash-size 1 + swap set-hash-size ;
: hash-size- ( hash -- ) dup hash-size 1 - swap set-hash-size ;
2005-01-27 20:06:10 -05:00
IN: hashtables
2004-07-16 02:26:21 -04:00
: bucket-count ( hash -- n ) hash-array array-capacity ;
2004-07-16 02:26:21 -04:00
: (hashcode) ( key table -- index )
#! Compute the index of the bucket for a key.
>r hashcode r> bucket-count rem ; inline
2004-07-16 02:26:21 -04:00
: hash* ( key table -- [[ key value ]] )
#! Look up a value in the hashtable.
2dup (hashcode) swap hash-bucket assoc* ;
2004-07-16 02:26:21 -04:00
2005-03-19 00:30:49 -05:00
: hash ( key table -- value ) hash* cdr ;
2004-07-16 02:26:21 -04:00
2005-01-28 23:55:22 -05:00
: set-hash* ( key hash quot -- )
2004-12-17 21:46:19 -05:00
#! Apply the quotation to yield a new association list.
2005-01-27 20:06:10 -05:00
#! If the association list already contains the key,
#! decrement the hash size, since it will get removed.
2005-01-28 23:55:22 -05:00
-rot 2dup (hashcode) over [
( quot key hash assoc -- )
swapd 2dup
assoc* [ rot hash-size- ] [ rot drop ] ifte
2005-01-28 23:55:22 -05:00
rot call
] change-bucket ; inline
: rehash? ( hash -- ? )
dup bucket-count 3 * 2 /i swap hash-size < ;
: grow-hash ( hash -- )
#! A good way to earn a living.
dup hash-size 2 * <array> swap set-hash-array ;
2005-01-28 23:55:22 -05:00
: (hash>alist) ( alist n hash -- alist )
2dup bucket-count >= [
2drop
] [
[ hash-bucket [ swons ] each ] 2keep
>r 1 + r> (hash>alist)
] ifte ;
: hash>alist ( hash -- alist )
#! Push a list of key/value pairs in a hashtable.
[ ] 0 rot (hash>alist) ;
: (set-hash) ( value key hash -- )
dup hash-size+ [ set-assoc ] set-hash* ;
: rehash ( hash -- )
#! Increase the hashtable size if its too small.
dup rehash? [
dup hash>alist
over grow-hash
0 pick set-hash-size
2005-01-28 23:55:22 -05:00
[ unswons rot (set-hash) ] each-with
] [
drop
] ifte ;
: set-hash ( value key table -- )
2004-07-16 02:26:21 -04:00
#! Store the value in the hashtable. Either replaces an
#! existing value in the appropriate bucket, or adds a new
2004-12-17 21:46:19 -05:00
#! key/value pair.
2005-01-28 23:55:22 -05:00
dup rehash (set-hash) ;
2004-12-17 21:46:19 -05:00
: remove-hash ( key table -- )
#! Remove a value from a hashtable.
[ remove-assoc ] set-hash* ;
2004-07-16 02:26:21 -04:00
2005-01-27 20:06:10 -05:00
: hash-clear ( hash -- )
#! Remove all entries from a hashtable.
0 over set-hash-size
2005-01-27 20:06:10 -05:00
dup bucket-count [
[ f swap pick set-hash-bucket ] keep
] repeat drop ;
: buckets>list ( hash -- list )
#! Push a list of key/value pairs in a hashtable.
2005-04-02 02:39:33 -05:00
hash-array >list ;
2005-01-27 20:06:10 -05:00
: alist>hash ( alist -- hash )
2005-01-28 23:55:22 -05:00
dup length 1 max <hashtable> swap
[ unswons pick set-hash ] each ;
2004-07-16 02:26:21 -04:00
: hash-keys ( hash -- list )
#! Push a list of keys in a hashtable.
2005-01-27 20:06:10 -05:00
hash>alist [ car ] map ;
2004-07-16 02:26:21 -04:00
: hash-values ( hash -- alist )
2004-07-16 02:26:21 -04:00
#! Push a list of values in a hashtable.
2005-01-27 20:06:10 -05:00
hash>alist [ cdr ] map ;
2004-07-16 02:26:21 -04:00
2005-01-27 20:06:10 -05:00
: hash-each ( hash code -- )
#! Apply the code to each key/value pair of the hashtable.
>r hash>alist r> each ; inline
2005-01-28 23:55:22 -05:00
M: hashtable clone ( hash -- hash )
dup bucket-count <hashtable>
over hash-size over set-hash-size [
hash-array swap hash-array dup array-capacity copy-array
2005-01-28 23:55:22 -05:00
] keep ;
: hash-subset? ( subset of -- ? )
hash>alist [ uncons >r swap hash r> = ] all-with? ;
M: hashtable = ( obj hash -- ? )
2dup eq? [
2drop t
] [
over hashtable? [
2dup hash-subset? >r swap hash-subset? r> and
] [
2drop f
] ifte
] ifte ;
M: hashtable hashcode ( hash -- n )
dup bucket-count 0 number= [
drop 0
] [
0 swap hash-bucket hashcode
] ifte ;