1 ! Copyright (C) 2006, 2009 Slava Pestov, Daniel Ehrenberg.
2 ! See http://factorcode.org/license.txt for BSD license.
3 USING: accessors arrays sequences sequences.private math.private
4 kernel kernel.private math assocs quotations vectors
5 hashtables sorting words sets math.order make ;
10 : call-effect-unsafe ( quot effect -- ) drop call ;
12 : execute-effect-unsafe ( word effect -- ) drop execute ;
14 M: object throw 5 getenv [ die ] or (( error -- * )) call-effect-unsafe ;
18 ERROR: wrong-values effect ;
20 ! We can't USE: effects here so we forward reference slots instead
24 : call-effect ( quot effect -- )
25 [ [ datastack ] dip dip ] dip
26 [ in>> length ] [ out>> length ] [ ] tri [ check-datastack ] dip
27 [ wrong-values ] curry unless ;
29 : execute-effect ( word effect -- )
30 [ [ execute ] curry ] dip call-effect ;
36 : cleave>quot ( seq -- quot )
37 [ [ keep ] curry ] map concat [ drop ] append [ ] like ;
40 : 2cleave ( x y seq -- )
41 [ 2keep ] each 2drop ;
43 : 2cleave>quot ( seq -- quot )
44 [ [ 2keep ] curry ] map concat [ 2drop ] append [ ] like ;
47 : 3cleave ( x y z seq -- )
48 [ 3keep ] each 3drop ;
50 : 3cleave>quot ( seq -- quot )
51 [ [ 3keep ] curry ] map concat [ 3drop ] append [ ] like ;
54 : spread>quot ( seq -- quot )
55 [ ] [ [ dup empty? [ [ dip ] curry ] unless ] dip append ] reduce ;
57 : spread ( objs... seq -- )
64 [ dup callable? [ drop t ] [ first call ] if ] find nip
65 [ dup callable? [ call ] [ second call ] if ]
68 : alist>quot ( default assoc -- quot )
69 [ rot \ if 3array append [ ] like ] assoc-each ;
71 : cond>quot ( assoc -- quot )
72 [ dup pair? [ [ t ] swap 2array ] unless ] map
73 reverse [ no-cond ] swap alist>quot ;
76 ERROR: no-case object ;
78 : case-find ( obj assoc -- obj' )
81 dupd first dup word? [
84 dup wrapper? [ wrapped>> ] when
89 \ case-find t "no-compile" set-word-prop
91 : case ( obj assoc -- )
93 { [ dup array? ] [ nip second call ] }
94 { [ dup callable? ] [ call ] }
95 { [ dup not ] [ drop no-case ] }
98 : linear-case-quot ( default assoc -- quot )
100 [ 1quotation \ dup prefix \ = suffix ]
101 [ \ drop prefix ] bi*
102 ] assoc-map alist>quot ;
104 : (distribute-buckets) ( buckets pair keys -- )
106 drop [ swap adjoin ] curry each
109 [ 2dup ] dip hashcode pick length rem rot nth adjoin
113 : <buckets> ( initial length -- array )
114 next-power-of-2 swap [ nip clone ] curry map ;
116 : distribute-buckets ( alist initial quot -- buckets )
117 swapd [ [ dup first ] dip call 2array ] curry map
118 [ length <buckets> dup ] keep
119 [ first2 (distribute-buckets) ] with each ; inline
121 : hash-case-table ( default assoc -- array )
122 V{ } [ 1array ] distribute-buckets
123 [ [ [ literalize ] dip ] assoc-map linear-case-quot ] with map ;
125 : hash-dispatch-quot ( table -- quot )
126 [ length 1 - [ fixnum-bitand ] curry ] keep
127 [ dispatch ] curry append ;
129 : hash-case-quot ( default assoc -- quot )
130 hash-case-table hash-dispatch-quot
131 [ dup hashcode >fixnum ] prepend ;
133 : contiguous-range? ( keys -- ? )
134 dup [ fixnum? ] all? [
137 [ [ supremum ] [ infimum ] bi - ]
142 : dispatch-case-quot ( default assoc -- quot )
145 dup keys [ infimum , ] [ supremum , ] bi \ between? ,
147 dup keys infimum , [ - >fixnum ] %
148 sort-keys values [ >quotation ] map ,
150 ] [ ] make , , \ if ,
153 : case>quot ( default assoc -- quot )
155 { [ dup empty? ] [ 2drop ] }
156 { [ dup [ length 4 <= ] [ [ word? ] any? ] bi or ] [ drop linear-case-quot ] }
157 { [ dup contiguous-range? ] [ drop dispatch-case-quot ] }
158 { [ dup [ wrapper? ] any? not ] [ drop hash-case-quot ] }
159 { [ dup [ wrapper? ] all? ] [ drop [ [ wrapped>> ] dip ] assoc-map hash-case-quot ] }
160 [ drop linear-case-quot ]
164 : recursive-hashcode ( n obj quot -- code )
165 pick 0 <= [ 3drop 0 ] [ [ 1 - ] 2dip call ] if ; inline
167 ! These go here, not in sequences and hashtables, since those
168 ! two cannot depend on us
169 M: sequence hashcode* [ sequence-hashcode ] recursive-hashcode ;
171 M: reversed hashcode* [ sequence-hashcode ] recursive-hashcode ;
173 M: slice hashcode* [ sequence-hashcode ] recursive-hashcode ;
175 M: hashtable hashcode*
178 [ assoc-hashcode ] [ nip assoc-size ] if
179 ] recursive-hashcode ;