factor/library/collections/hashtables.factor

163 lines
4.5 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.
DEFER: hashtable?
BUILTIN: hashtable 10 hashtable?
[ 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-06-09 19:49:31 -04:00
: bucket-count ( hash -- n ) hash-array length ;
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
2005-06-08 18:11:53 -04:00
: each-bucket ( hash quot -- | quot: n hash -- )
over bucket-count [ [ -rot call ] 3keep ] repeat 2drop ;
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
: grow-hash ( hash -- )
#! A good way to earn a living.
dup hash-size 2 * <array> swap set-hash-array ;
: (set-bucket-count) ( n hash -- )
>r <array> r> set-hash-array ;
IN: hashtables
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
: grow-hash? ( hash -- ? )
2005-01-28 23:55:22 -05:00
dup bucket-count 3 * 2 /i swap hash-size < ;
: hash>alist ( hash -- alist )
#! Push a list of key/value pairs in a hashtable.
2005-06-08 18:11:53 -04:00
[ ] swap [ hash-bucket [ swons ] each ] each-bucket ;
2005-01-28 23:55:22 -05:00
: (set-hash) ( value key hash -- )
dup hash-size+ [ set-assoc ] set-hash* ;
: set-bucket-count ( new hash -- )
dup hash>alist >r [ (set-bucket-count) ] keep r>
0 pick set-hash-size
[ unswons rot (set-hash) ] each-with ;
: grow-hash ( hash -- )
2005-01-28 23:55:22 -05:00
#! Increase the hashtable size if its too small.
dup grow-hash? [
dup hash-size 2 * swap set-bucket-count
2005-01-28 23:55:22 -05:00
] [
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.
dup grow-hash (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.
2005-06-08 18:11:53 -04:00
0 over set-hash-size [ f -rot set-hash-bucket ] each-bucket ;
2005-01-27 20:06:10 -05:00
: 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
: hash-subset ( hash quot -- hash | quot: [[ k v ]] -- ? )
>r hash>alist r> subset alist>hash ;
2005-01-28 23:55:22 -05:00
M: hashtable clone ( hash -- hash )
dup bucket-count <hashtable>
2005-06-08 18:11:53 -04:00
over hash-size over set-hash-size
2005-06-10 17:41:41 -04:00
[ hash-array swap hash-array copy-array ] keep ;
2005-01-28 23:55:22 -05:00
M: hashtable = ( obj hash -- ? )
2dup eq? [
2drop t
] [
over hashtable? [
swap hash>alist swap hash>alist 2dup
contained? >r swap contained? r> and
2005-01-28 23:55:22 -05:00
] [
2drop f
] ifte
] ifte ;
M: hashtable hashcode ( hash -- n )
dup bucket-count 0 number= [
drop 0
] [
0 swap hash-bucket hashcode
] ifte ;