]> gitweb.factorcode.org Git - factor.git/blob - core/hashtables/hashtables.factor
use radix literals
[factor.git] / core / hashtables / hashtables.factor
1 ! Copyright (C) 2005, 2011 John Benediktsson, Slava Pestov.
2 ! See http://factorcode.org/license.txt for BSD license.
3 USING: accessors arrays kernel kernel.private slots.private math
4 assocs math.private sequences sequences.private vectors ;
5 IN: hashtables
6
7 TUPLE: hashtable
8 { count array-capacity }
9 { deleted array-capacity }
10 { array array } ;
11
12 <PRIVATE
13
14 : wrap ( i array -- n )
15     length>> 1 fixnum-fast fixnum-bitand ; inline
16
17 : hash@ ( key array -- i )
18     [ hashcode >fixnum dup fixnum+fast ] dip wrap ; inline
19
20 : probe ( array i probe# -- array i probe# )
21     2 fixnum+fast [ fixnum+fast over wrap ] keep ; inline
22
23 : no-key ( key array -- array n ? ) nip f f ; inline
24
25 : (key@) ( key array i probe# -- array n ? )
26     [ 3dup swap array-nth ] dip over ((empty)) eq?
27     [ drop 3drop no-key ] [
28         [ = ] dip swap
29         [ drop rot drop t ]
30         [ probe (key@) ]
31         if
32     ] if ; inline recursive
33
34 : key@ ( key hash -- array n ? )
35     array>> dup length>> 0 eq?
36     [ no-key ] [ 2dup hash@ 0 (key@) ] if ; inline
37
38 : <hash-array> ( n -- array )
39     1 + next-power-of-2 4 * ((empty)) <array> ; inline
40
41 : init-hash ( hash -- )
42     0 >>count 0 >>deleted drop ; inline
43
44 : reset-hash ( n hash -- )
45     swap <hash-array> >>array init-hash ; inline
46
47 : hash-count+ ( hash -- )
48     [ 1 fixnum+fast ] change-count drop ; inline
49
50 : hash-deleted+ ( hash -- )
51     [ 1 fixnum+fast ] change-deleted drop ; inline
52
53 : hash-deleted- ( hash -- )
54     [ 1 fixnum-fast ] change-deleted drop ; inline
55
56 ! i = first-empty-or-found
57 ! j = first-deleted
58 ! empty? = if true, key was not found
59 !
60 ! if empty? is f:
61 ! - we want to store into i
62
63 ! if empty? is t:
64 ! - we want to store into j if j is not f
65 ! - otherwise we want to store into i
66 ! - ... and increment count
67
68 : (new-key@) ( key array i probe# j -- array i j empty? )
69     [ 2dup swap array-nth ] 2dip pick tombstone?
70     [
71         rot ((empty)) eq?
72         [ nip [ drop ] 3dip t ]
73         [ pick or [ probe ] dip (new-key@) ]
74         if
75     ] [
76         [ [ pick ] dip = ] 2dip rot
77         [ nip [ drop ] 3dip f ]
78         [ [ probe ] dip (new-key@) ]
79         if
80     ] if ; inline recursive
81
82 : new-key@ ( key hash -- array n )
83     [ array>> 2dup hash@ 0 f (new-key@) ] keep swap
84     [ over [ hash-deleted- ] [ hash-count+ ] if swap or ] [ 2drop ] if ; inline
85
86 : set-nth-pair ( value key seq n -- )
87     2 fixnum+fast [ set-slot ] 2keep
88     1 fixnum+fast set-slot ; inline
89
90 : (rehash) ( hash alist -- )
91     swap [ swapd set-at ] curry assoc-each ; inline
92
93 : hash-large? ( hash -- ? )
94     [ count>> 3 fixnum*fast 1 fixnum+fast ]
95     [ array>> length>> ] bi fixnum> ; inline
96
97 : grow-hash ( hash -- )
98     [ [ >alist ] [ assoc-size 1 + ] bi ] keep
99     [ reset-hash ] keep
100     swap (rehash) ;
101
102 : ?grow-hash ( hash -- )
103     dup hash-large? [ grow-hash ] [ drop ] if ; inline
104
105 PRIVATE>
106
107 : <hashtable> ( n -- hash )
108     hashtable new [ reset-hash ] keep ; inline
109
110 M: hashtable at* ( key hash -- value ? )
111     key@ [ 3 fixnum+fast slot t ] [ 2drop f f ] if ;
112
113 M: hashtable clear-assoc ( hash -- )
114     [ init-hash ] [ array>> [ drop ((empty)) ] map! drop ] bi ;
115
116 M: hashtable delete-at ( key hash -- )
117     [ nip ] [ key@ ] 2bi [
118         [ ((tombstone)) dup ] 2dip set-nth-pair
119         hash-deleted+
120     ] [
121         3drop
122     ] if ;
123
124 M: hashtable assoc-size ( hash -- n )
125     [ count>> ] [ deleted>> ] bi - ; inline
126
127 : rehash ( hash -- )
128     dup >alist [ dup clear-assoc ] dip (rehash) ;
129
130 M: hashtable set-at ( value key hash -- )
131     dup ?grow-hash dupd new-key@ set-nth-pair ;
132
133 : associate ( value key -- hash )
134     2 <hashtable> [ set-at ] keep ;
135
136 <PRIVATE
137
138 : push-unsafe ( elt seq -- )
139     [ length ] keep
140     [ underlying>> set-array-nth ]
141     [ [ 1 fixnum+fast { array-capacity } declare ] dip length<< ]
142     2bi ; inline
143
144 PRIVATE>
145
146 M: hashtable >alist
147     [ array>> [ length 2/ ] keep ] [ assoc-size <vector> ] bi [
148         [
149             [
150                 [ 1 fixnum-shift-fast ] dip
151                 [ array-nth ] [ [ 1 fixnum+fast ] dip array-nth ] 2bi
152             ] dip
153             pick tombstone? [ 3drop ] [ [ 2array ] dip push-unsafe ] if
154         ] 2curry each-integer
155     ] keep { } like ;
156
157 M: hashtable clone
158     (clone) [ clone ] change-array ; inline
159
160 M: hashtable equal?
161     over hashtable? [ assoc= ] [ 2drop f ] if ;
162
163 ! Default method
164 M: assoc new-assoc drop <hashtable> ; inline
165
166 M: f new-assoc drop <hashtable> ; inline
167
168 : >hashtable ( assoc -- hashtable )
169     H{ } assoc-clone-like ;
170
171 M: hashtable assoc-like
172     drop dup hashtable? [ >hashtable ] unless ; inline
173
174 : ?set-at ( value key assoc/f -- assoc )
175     [ [ set-at ] keep ] [ associate ] if* ;
176
177 ! borrowed from boost::hash_combine, but the
178 ! magic number is 2^29/phi instead of 2^32/phi
179 ! due to max fixnum value on 32-bit machines
180 : hash-combine ( obj oldhash -- newhash )
181     [ hashcode 0x13c6ef37 + ] dip
182     [ 6 shift ] [ -2 shift ] bi + + ;
183
184 INSTANCE: hashtable assoc