2004-07-22 19:48:50 -04:00
|
|
|
! :folding=indent:collapseFolds=1:
|
2004-07-16 02:26:21 -04:00
|
|
|
|
|
|
|
! $Id$
|
|
|
|
!
|
2005-01-25 19:40:57 -05:00
|
|
|
! Copyright (C) 2004, 2005 Slava Pestov.
|
2004-07-16 02:26:21 -04:00
|
|
|
!
|
|
|
|
! Redistribution and use in source and binary forms, with or without
|
|
|
|
! modification, are permitted provided that the following conditions are met:
|
|
|
|
!
|
|
|
|
! 1. Redistributions of source code must retain the above copyright notice,
|
|
|
|
! this list of conditions and the following disclaimer.
|
|
|
|
!
|
|
|
|
! 2. Redistributions in binary form must reproduce the above copyright notice,
|
|
|
|
! this list of conditions and the following disclaimer in the documentation
|
|
|
|
! and/or other materials provided with the distribution.
|
|
|
|
!
|
|
|
|
! THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES,
|
|
|
|
! INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND
|
|
|
|
! FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
|
|
|
|
! DEVELOPERS AND CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
|
|
|
|
! SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
|
|
|
|
! PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
|
|
|
|
! OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
|
|
|
|
! WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
|
|
|
|
! OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
|
|
|
|
! ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
|
|
|
|
2005-01-27 20:06:10 -05:00
|
|
|
IN: hashtables
|
2004-12-12 23:49:44 -05:00
|
|
|
USE: generic
|
2004-07-16 02:26:21 -04:00
|
|
|
USE: kernel
|
|
|
|
USE: lists
|
2004-08-26 22:21:17 -04:00
|
|
|
USE: math
|
2004-07-16 02:26:21 -04:00
|
|
|
USE: vectors
|
|
|
|
|
2005-01-27 20:06:10 -05:00
|
|
|
BUILTIN: hashtable 10
|
|
|
|
|
|
|
|
! 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.
|
|
|
|
|
|
|
|
IN: kernel-internals
|
|
|
|
|
|
|
|
: hash-array 2 slot ; inline
|
2005-01-25 19:40:57 -05:00
|
|
|
|
|
|
|
: hash-bucket ( n hash -- alist )
|
2005-01-27 20:06:10 -05:00
|
|
|
swap >fixnum swap >hashtable hash-array array-nth ; inline
|
2005-01-25 19:40:57 -05:00
|
|
|
|
|
|
|
: set-hash-bucket ( obj n hash -- )
|
2005-01-27 20:06:10 -05:00
|
|
|
swap >fixnum swap >hashtable hash-array set-array-nth ;
|
|
|
|
inline
|
2005-01-25 19:40:57 -05:00
|
|
|
|
2005-01-27 20:06:10 -05:00
|
|
|
: hash-size+ ( hash -- )
|
|
|
|
>hashtable dup 1 slot 1 + swap 1 set-slot ; inline
|
|
|
|
|
|
|
|
: hash-size- ( hash -- )
|
|
|
|
>hashtable dup 1 slot 1 - swap 1 set-slot ; inline
|
2005-01-25 19:40:57 -05:00
|
|
|
|
2005-01-27 20:06:10 -05:00
|
|
|
IN: hashtables
|
2004-07-16 02:26:21 -04:00
|
|
|
|
2005-01-27 20:06:10 -05:00
|
|
|
: hash-size ( hash -- n )
|
|
|
|
#! Number of elements in the hashtable.
|
|
|
|
>hashtable 1 slot ;
|
2004-07-16 02:26:21 -04:00
|
|
|
|
2005-01-27 20:06:10 -05:00
|
|
|
: bucket-count ( hash -- n )
|
|
|
|
>hashtable hash-array array-capacity ; inline
|
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 ]] )
|
2004-07-16 02:26:21 -04:00
|
|
|
#! Look up a value in the hashtable. First the bucket is
|
|
|
|
#! determined using the hash function, then the association
|
|
|
|
#! list therein is searched linearly.
|
2005-01-25 19:40:57 -05:00
|
|
|
2dup (hashcode) swap hash-bucket assoc* ;
|
2004-07-16 02:26:21 -04:00
|
|
|
|
|
|
|
: hash ( key table -- value )
|
|
|
|
#! Unlike hash*, this word cannot distinglish between an
|
|
|
|
#! undefined value, or a value set to f.
|
|
|
|
hash* dup [ cdr ] when ;
|
|
|
|
|
2004-12-17 21:46:19 -05:00
|
|
|
: set-hash* ( key table quot -- )
|
|
|
|
#! 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.
|
2004-12-17 23:02:19 -05:00
|
|
|
>r
|
|
|
|
2dup (hashcode)
|
|
|
|
r> pick >r
|
|
|
|
over >r
|
2005-01-25 19:40:57 -05:00
|
|
|
>r swap hash-bucket r> call
|
2004-12-17 23:02:19 -05:00
|
|
|
r>
|
2005-01-25 19:40:57 -05:00
|
|
|
r> set-hash-bucket ; inline
|
2004-12-17 21:46:19 -05:00
|
|
|
|
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-01-27 20:06:10 -05:00
|
|
|
dup hash-size+
|
2004-12-17 23:02:19 -05:00
|
|
|
[ set-assoc ] 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.
|
|
|
|
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.
|
|
|
|
dup bucket-count swap hash-array array>list ;
|
|
|
|
|
|
|
|
: (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) ;
|
|
|
|
|
|
|
|
: alist>hash ( alist -- hash )
|
|
|
|
dup length <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
|
|
|
|
2004-08-31 00:27:09 -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
|