2005-01-25 19:40:57 -05:00
|
|
|
! Copyright (C) 2004, 2005 Slava Pestov.
|
2005-01-29 14:18:28 -05:00
|
|
|
! See http://factor.sf.net/license.txt for BSD license.
|
2005-02-20 19:03:37 -05:00
|
|
|
IN: hashtables
|
2005-09-11 20:46:55 -04:00
|
|
|
USING: arrays generic kernel lists math sequences vectors
|
2005-09-10 18:27:31 -04:00
|
|
|
kernel-internals sequences-internals ;
|
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-09-10 18:27:31 -04:00
|
|
|
: bucket-count ( hash -- n ) underlying array-capacity ;
|
2005-06-09 19:49:31 -04:00
|
|
|
|
2005-01-27 20:06:10 -05:00
|
|
|
IN: kernel-internals
|
|
|
|
|
|
2005-01-25 19:40:57 -05:00
|
|
|
: hash-bucket ( n hash -- alist )
|
2005-09-10 18:27:31 -04:00
|
|
|
>r >fixnum r> underlying array-nth ;
|
2005-01-25 19:40:57 -05:00
|
|
|
|
|
|
|
|
: set-hash-bucket ( obj n hash -- )
|
2005-09-10 18:27:31 -04:00
|
|
|
>r >fixnum r> underlying set-array-nth ;
|
2005-01-25 19:40:57 -05:00
|
|
|
|
2005-01-28 23:55:22 -05:00
|
|
|
: change-bucket ( n hash quot -- )
|
2005-09-10 18:27:31 -04:00
|
|
|
-rot underlying
|
2005-01-28 23:55:22 -05:00
|
|
|
[ 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
|
|
|
|
|
|
2005-09-16 22:47:28 -04:00
|
|
|
: 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
|
|
|
|
2005-04-29 02:37:12 -04:00
|
|
|
: grow-hash ( hash -- )
|
|
|
|
|
#! A good way to earn a living.
|
2005-09-10 18:27:31 -04:00
|
|
|
dup hash-size 2 * <array> swap set-underlying ;
|
2005-04-29 02:37:12 -04:00
|
|
|
|
2005-05-03 23:50:04 -04:00
|
|
|
: (set-bucket-count) ( n hash -- )
|
2005-09-10 18:27:31 -04:00
|
|
|
>r <array> r> set-underlying ;
|
2005-05-03 23:50:04 -04:00
|
|
|
|
2005-03-05 16:33:40 -05:00
|
|
|
IN: hashtables
|
2004-07-16 02:26:21 -04:00
|
|
|
|
|
|
|
|
: (hashcode) ( key table -- index )
|
|
|
|
|
#! Compute the index of the bucket for a key.
|
2005-01-25 19:40:57 -05:00
|
|
|
>r hashcode r> bucket-count rem ; inline
|
2004-07-16 02:26:21 -04:00
|
|
|
|
2005-01-13 19:49:47 -05:00
|
|
|
: hash* ( key table -- [[ key value ]] )
|
2005-03-05 16:33:40 -05:00
|
|
|
#! Look up a value in the hashtable.
|
2005-08-19 22:22:15 -04:00
|
|
|
2dup (hashcode) swap hash-bucket assoc* ; flushable
|
2004-07-16 02:26:21 -04:00
|
|
|
|
2005-08-19 22:22:15 -04:00
|
|
|
: hash ( key table -- value ) hash* cdr ; flushable
|
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
|
2005-03-05 16:33:40 -05:00
|
|
|
assoc* [ rot hash-size- ] [ rot drop ] ifte
|
2005-01-28 23:55:22 -05:00
|
|
|
rot call
|
|
|
|
|
] change-bucket ; inline
|
|
|
|
|
|
2005-05-03 23:50:04 -04:00
|
|
|
: 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-08-19 22:22:15 -04:00
|
|
|
flushable
|
2005-01-28 23:55:22 -05:00
|
|
|
|
|
|
|
|
: (set-hash) ( value key hash -- )
|
|
|
|
|
dup hash-size+ [ set-assoc ] set-hash* ;
|
|
|
|
|
|
2005-05-03 23:50:04 -04:00
|
|
|
: 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.
|
2005-05-03 23:50:04 -04:00
|
|
|
dup grow-hash? [
|
2005-09-16 20:49:24 -04:00
|
|
|
dup hash-size new-size swap set-bucket-count
|
2005-01-28 23:55:22 -05:00
|
|
|
] [
|
|
|
|
|
drop
|
|
|
|
|
] ifte ;
|
|
|
|
|
|
2004-12-17 23:02:19 -05:00
|
|
|
: 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-05-03 23:50:04 -04:00
|
|
|
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 -- )
|
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
|
|
|
|
|
|
|
|
: alist>hash ( alist -- hash )
|
2005-01-28 23:55:22 -05:00
|
|
|
dup length 1 max <hashtable> swap
|
2005-08-19 22:22:15 -04:00
|
|
|
[ unswons pick set-hash ] each ; foldable
|
2004-07-16 02:26:21 -04:00
|
|
|
|
|
|
|
|
: hash-keys ( hash -- list )
|
2005-08-19 22:22:15 -04:00
|
|
|
hash>alist [ car ] map ; flushable
|
2004-07-16 02:26:21 -04:00
|
|
|
|
2004-08-31 00:27:09 -04:00
|
|
|
: hash-values ( hash -- alist )
|
2005-08-19 22:22:15 -04:00
|
|
|
hash>alist [ cdr ] map ; flushable
|
2004-07-16 02:26:21 -04:00
|
|
|
|
2005-08-11 19:08:22 -04:00
|
|
|
: hash-each ( hash quot -- | quot: [[ k v ]] -- )
|
2005-09-10 18:27:31 -04:00
|
|
|
swap underlying [ swap each ] each-with ; inline
|
2005-01-28 23:55:22 -05:00
|
|
|
|
2005-08-11 19:08:22 -04:00
|
|
|
: hash-each-with ( obj hash quot -- | quot: obj [[ k v ]] -- )
|
2005-06-13 01:42:16 -04:00
|
|
|
swap [ with ] hash-each 2drop ; inline
|
|
|
|
|
|
2005-08-11 19:08:22 -04:00
|
|
|
: hash-all? ( hash quot -- | quot: [[ k v ]] -- ? )
|
2005-09-10 18:27:31 -04:00
|
|
|
swap underlying [ swap all? ] all-with? ; inline
|
2005-08-11 19:08:22 -04:00
|
|
|
|
|
|
|
|
: hash-all-with? ( obj hash quot -- ? | quot: [[ k v ]] -- ? )
|
|
|
|
|
swap [ with rot ] hash-all? 2nip ; inline
|
|
|
|
|
|
|
|
|
|
: hash-contained? ( h1 h2 -- ? )
|
|
|
|
|
#! Test if h2 contains all the key/value pairs of h1.
|
|
|
|
|
swap [
|
|
|
|
|
uncons >r swap hash* dup [
|
|
|
|
|
cdr r> =
|
|
|
|
|
] [
|
|
|
|
|
r> 2drop f
|
|
|
|
|
] ifte
|
2005-08-19 22:22:15 -04:00
|
|
|
] hash-all-with? ; flushable
|
2005-08-11 19:08:22 -04:00
|
|
|
|
2005-09-16 20:49:24 -04:00
|
|
|
: hash-filter-step ( quot assoc -- assoc n )
|
|
|
|
|
[ swap subset dup length ] keep length - ; inline
|
|
|
|
|
|
|
|
|
|
: (hash-filter) ( quot hash -- n )
|
|
|
|
|
#! Output the number of key/value pairs that were removed.
|
|
|
|
|
0 swap underlying [
|
|
|
|
|
pick >r swap >r hash-filter-step r> + swap r> -rot
|
|
|
|
|
] inject nip ; inline
|
|
|
|
|
|
|
|
|
|
: hash-filter ( hash quot -- | quot: [[ k v ]] -- ? )
|
|
|
|
|
#! Remove all key/value pairs that do not satisfy the
|
|
|
|
|
#! predicate.
|
|
|
|
|
swap [ (hash-filter) ] keep
|
|
|
|
|
[ hash-size + ] keep
|
|
|
|
|
set-hash-size ; inline
|
|
|
|
|
|
2005-05-03 23:50:04 -04:00
|
|
|
: hash-subset ( hash quot -- hash | quot: [[ k v ]] -- ? )
|
2005-09-16 20:49:24 -04:00
|
|
|
#! Make a new hash that only includes the key/value pairs
|
|
|
|
|
#! which satisfy the predicate.
|
|
|
|
|
>r clone r> over >r hash-filter r> ; inline
|
2005-05-03 23:50:04 -04:00
|
|
|
|
2005-09-09 16:45:18 -04:00
|
|
|
: hash-subset-with ( obj hash quot -- hash )
|
|
|
|
|
swap [ with rot ] hash-subset 2nip ; inline
|
|
|
|
|
|
2005-09-10 18:27:31 -04:00
|
|
|
M: hashtable clone ( hash -- hash ) clone-growable ;
|
2005-01-28 23:55:22 -05:00
|
|
|
|
2005-09-16 20:49:24 -04:00
|
|
|
: hashtable= ( hash hash -- ? )
|
|
|
|
|
2dup hash-contained? >r swap hash-contained? r> and ;
|
|
|
|
|
|
2005-01-28 23:55:22 -05:00
|
|
|
M: hashtable = ( obj hash -- ? )
|
2005-09-16 20:49:24 -04:00
|
|
|
@{
|
|
|
|
|
@{ [ 2dup eq? ] [ 2drop t ] }@
|
|
|
|
|
@{ [ over hashtable? not ] [ 2drop f ] }@
|
|
|
|
|
@{ [ 2dup [ hash-size ] 2apply number= not ] [ 2drop f ] }@
|
|
|
|
|
@{ [ t ] [ hashtable= ] }@
|
|
|
|
|
}@ cond ;
|
2005-01-29 14:18:28 -05:00
|
|
|
|
|
|
|
|
M: hashtable hashcode ( hash -- n )
|
2005-09-16 02:39:33 -04:00
|
|
|
#! Poor.
|
|
|
|
|
hash-size ;
|
2005-06-27 03:47:22 -04:00
|
|
|
|
|
|
|
|
: cache ( key hash quot -- value | quot: key -- value )
|
|
|
|
|
pick pick hash [
|
|
|
|
|
>r 3drop r>
|
2005-09-16 02:39:33 -04:00
|
|
|
] [
|
2005-06-27 03:47:22 -04:00
|
|
|
pick rot >r >r call dup r> r> set-hash
|
|
|
|
|
] ifte* ; inline
|
2005-07-13 21:28:23 -04:00
|
|
|
|
2005-07-28 15:17:31 -04:00
|
|
|
: map>hash ( seq quot -- hash | quot: elt -- value )
|
|
|
|
|
over >r map r> dup length <hashtable> -rot
|
|
|
|
|
[ pick set-hash ] 2each ; inline
|
|
|
|
|
|
2005-07-13 21:28:23 -04:00
|
|
|
: ?hash ( key hash/f -- value/f )
|
2005-08-19 22:22:15 -04:00
|
|
|
dup [ hash ] [ 2drop f ] ifte ; flushable
|
2005-07-13 21:28:23 -04:00
|
|
|
|
|
|
|
|
: ?set-hash ( value key hash/f -- hash )
|
|
|
|
|
[ 1 <hashtable> ] unless* [ set-hash ] keep ;
|
2005-09-16 02:39:33 -04:00
|
|
|
|
|
|
|
|
: hash-intersect ( hash1 hash2 -- hash1/\hash2 )
|
|
|
|
|
#! Remove all keys from hash2 not in hash1.
|
|
|
|
|
[ car swap hash ] hash-subset-with ;
|
|
|
|
|
|
|
|
|
|
: hash-diff ( hash1 hash2 -- hash2-hash1 )
|
|
|
|
|
#! Remove all keys from hash2 in hash1.
|
|
|
|
|
[ car swap hash not ] hash-subset-with ;
|
|
|
|
|
|
|
|
|
|
: hash-update ( hash1 hash2 -- )
|
|
|
|
|
#! Add all key/value pairs from hash2 to hash1.
|
|
|
|
|
[ unswons rot set-hash ] hash-each-with ;
|
|
|
|
|
|
|
|
|
|
: hash-union ( hash1 hash2 -- hash1\/hash2 )
|
|
|
|
|
#! Make a new hashtable with all key/value pairs from
|
|
|
|
|
#! hash1 and hash2. Values in hash2 take precedence.
|
|
|
|
|
>r clone dup r> hash-update ;
|
2005-09-16 23:33:20 -04:00
|
|
|
|
|
|
|
|
: remove-all ( hash seq -- seq )
|
|
|
|
|
#! Remove all elements from the sequence that are keys
|
|
|
|
|
#! in the hashtable.
|
|
|
|
|
[ swap hash* not ] subset-with ; flushable
|