1 ! Copyright (C) 2006, 2010 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 ;
8 ! Most of these combinators have compile-time expansions in
9 ! the optimizing compiler. See stack-checker.transforms and
10 ! compiler.tree.propagation.call-effect
14 : call-effect-unsafe ( quot effect -- ) drop call ;
16 : execute-effect-unsafe ( word effect -- ) drop execute ;
19 ERROR-HANDLER-QUOT special-object [ die ] or
20 ( error -- * ) call-effect-unsafe ;
24 ERROR: wrong-values quot call-site ;
26 ! We can't USE: effects here so we forward reference slots instead
31 : call-effect ( quot effect -- )
32 ! Don't use fancy combinators here, since this word always
35 [ [ datastack ] dip dip ] dip
36 dup terminated?>> [ 2drop f ] [
37 dup in>> length swap out>> length
41 [ 2drop ] [ wrong-values ] if ;
43 : execute-effect ( word effect -- )
44 [ [ execute ] curry ] dip call-effect ;
50 : cleave>quot ( seq -- quot )
51 [ [ keep ] curry ] map concat [ drop ] append [ ] like ;
54 : 2cleave ( x y seq -- )
55 [ 2keep ] each 2drop ;
57 : 2cleave>quot ( seq -- quot )
58 [ [ 2keep ] curry ] map concat [ 2drop ] append [ ] like ;
61 : 3cleave ( x y z seq -- )
62 [ 3keep ] each 3drop ;
64 : 3cleave>quot ( seq -- quot )
65 [ [ 3keep ] curry ] map concat [ 3drop ] append [ ] like ;
68 : shallow-spread>quot ( seq -- quot )
69 [ ] [ [ dup empty? [ [ dip ] curry ] unless ] dip append ] reduce ;
71 : deep-spread>quot ( seq -- quot )
72 [ ] [ [ [ dip ] curry ] dip append ] reduce ;
74 : spread ( objs... seq -- )
75 deep-spread>quot call ;
81 [ dup callable? [ drop t ] [ first call ] if ] find nip
82 [ dup callable? [ call ] [ second call ] if ]
85 : alist>quot ( default assoc -- quot )
86 [ rot \ if 3array append [ ] like ] assoc-each ;
88 : cond>quot ( assoc -- quot )
89 [ dup pair? [ [ t ] swap 2array ] unless ] map
90 reverse [ no-cond ] swap alist>quot ;
93 ERROR: no-case object ;
95 : case-find ( obj assoc -- obj' )
98 dupd first dup word? [
101 dup wrapper? [ wrapped>> ] when
106 \ case-find t "no-compile" set-word-prop
108 : case ( obj assoc -- )
110 { [ dup array? ] [ nip second call ] }
111 { [ dup callable? ] [ call ] }
112 { [ dup not ] [ drop no-case ] }
115 : linear-case-quot ( default assoc -- quot )
117 [ 1quotation \ dup prefix \ = suffix ]
118 [ \ drop prefix ] bi*
119 ] assoc-map alist>quot ;
123 : (distribute-buckets) ( buckets pair keys -- )
125 drop [ swap adjoin ] curry each
128 [ 2dup ] dip hashcode pick length rem rot nth adjoin
132 : <buckets> ( initial length -- array )
133 next-power-of-2 iota swap [ nip clone ] curry map ;
135 : distribute-buckets ( alist initial quot -- buckets )
136 swapd [ [ dup first ] dip call 2array ] curry map
137 [ length <buckets> dup ] keep
138 [ first2 (distribute-buckets) ] with each ; inline
140 : hash-case-table ( default assoc -- array )
141 V{ } [ 1array ] distribute-buckets
142 [ [ [ literalize ] dip ] assoc-map linear-case-quot ] with map ;
144 : hash-dispatch-quot ( table -- quot )
145 [ length 1 - [ fixnum-bitand ] curry ] keep
146 [ dispatch ] curry append ;
148 : hash-case-quot ( default assoc -- quot )
149 hash-case-table hash-dispatch-quot
150 [ dup hashcode >fixnum ] prepend ;
152 : contiguous-range? ( keys -- ? )
153 dup [ fixnum? ] all? [
156 [ [ supremum ] [ infimum ] bi - ]
161 : dispatch-case-quot ( default assoc -- quot )
164 dup keys [ infimum , ] [ supremum , ] bi \ between? ,
166 dup keys infimum , [ - >fixnum ] %
167 sort-keys values [ >quotation ] map ,
169 ] [ ] make , , \ if ,
174 : case>quot ( default assoc -- quot )
175 <reversed> dup keys {
176 { [ dup empty? ] [ 2drop ] }
177 { [ dup [ length 4 <= ] [ [ word? ] any? ] bi or ] [ drop linear-case-quot ] }
178 { [ dup contiguous-range? ] [ drop dispatch-case-quot ] }
179 { [ dup [ wrapper? ] any? not ] [ drop hash-case-quot ] }
180 { [ dup [ wrapper? ] all? ] [ drop [ [ wrapped>> ] dip ] assoc-map hash-case-quot ] }
181 [ drop linear-case-quot ]
184 : recursive-hashcode ( n obj quot -- code )
185 pick 0 <= [ 3drop 0 ] [ [ 1 - ] 2dip call ] if ; inline
187 ! These go here, not in sequences and hashtables, since those
188 ! two cannot depend on us
189 M: sequence hashcode* [ sequence-hashcode ] recursive-hashcode ;
191 M: reversed hashcode* [ sequence-hashcode ] recursive-hashcode ;
193 M: slice hashcode* [ sequence-hashcode ] recursive-hashcode ;
195 M: hashtable hashcode*
198 [ assoc-hashcode ] [ nip assoc-size ] if
199 ] recursive-hashcode ;
201 : to-fixed-point ( ... object quot: ( ... object(n) -- ... object(n+1) ) -- ... object(n) )
202 [ keep over = ] keep [ to-fixed-point ] curry unless ; inline recursive