1 ! Copyright (C) 2007, 2009 Daniel Ehrenberg, Slava Pestov
2 ! See http://factorcode.org/license.txt for BSD license.
3 USING: kernel sequences arrays math sequences.private vectors
9 GENERIC: at* ( key assoc -- value/f ? )
10 GENERIC: set-at ( value key assoc -- )
11 GENERIC: new-assoc ( capacity exemplar -- newassoc )
12 GENERIC: delete-at ( key assoc -- )
13 GENERIC: clear-assoc ( assoc -- )
14 GENERIC: assoc-size ( assoc -- n )
15 GENERIC: assoc-like ( assoc exemplar -- newassoc )
17 M: assoc assoc-like drop ;
19 GENERIC: assoc-clone-like ( assoc exemplar -- newassoc )
21 GENERIC: >alist ( assoc -- newassoc )
23 : (assoc-each) ( assoc quot -- seq quot' )
24 [ >alist ] dip [ first2 ] prepose ; inline
26 : assoc-find ( assoc quot -- key value ? )
27 (assoc-each) find swap [ first2 t ] [ drop f f f ] if ; inline
29 : key? ( key assoc -- ? ) at* nip ; inline
31 : assoc-each ( assoc quot -- )
32 (assoc-each) each ; inline
34 : assoc>map ( assoc quot exemplar -- seq )
35 [ accumulator [ assoc-each ] dip ] dip like ; inline
37 : assoc-map-as ( assoc quot exemplar -- newassoc )
38 [ [ 2array ] compose V{ } assoc>map ] dip assoc-like ; inline
40 : assoc-map ( assoc quot -- newassoc )
41 over assoc-map-as ; inline
43 : assoc-push-if ( key value quot accum -- )
44 [ 2keep ] dip [ [ 2array ] dip push ] 3curry when ; inline
46 : assoc-pusher ( quot -- quot' accum )
47 V{ } clone [ [ assoc-push-if ] 2curry ] keep ; inline
49 : assoc-filter-as ( assoc quot exemplar -- subassoc )
50 [ assoc-pusher [ assoc-each ] dip ] dip assoc-like ; inline
52 : assoc-filter ( assoc quot -- subassoc )
53 over assoc-filter-as ; inline
55 : assoc-contains? ( assoc quot -- ? )
56 assoc-find 2nip ; inline
58 : assoc-all? ( assoc quot -- ? )
59 [ not ] compose assoc-contains? not ; inline
61 : at ( key assoc -- value/f )
64 : at-default ( key assoc -- value/key )
65 2dup at* [ 2nip ] [ 2drop ] if ; inline
67 M: assoc assoc-clone-like ( assoc exemplar -- newassoc )
68 over assoc-size swap new-assoc
69 [ [ swapd set-at ] curry assoc-each ] keep ;
71 : keys ( assoc -- keys )
72 [ drop ] { } assoc>map ;
74 : values ( assoc -- values )
75 [ nip ] { } assoc>map ;
77 : delete-at* ( key assoc -- old ? )
78 [ at* ] 2keep delete-at ;
80 : rename-at ( newkey key assoc -- )
81 [ delete-at* ] keep [ swapd set-at ] curry [ 2drop ] if ;
83 : assoc-empty? ( assoc -- ? )
86 : (assoc-stack) ( key i seq -- value )
91 [ [ 3drop ] dip ] [ drop [ 1- ] dip (assoc-stack) ] if
92 ] if ; inline recursive
94 : assoc-stack ( key seq -- value )
95 [ length 1- ] keep (assoc-stack) ; flushable
97 : assoc-subset? ( assoc1 assoc2 -- ? )
98 [ swapd at* [ = ] [ 2drop f ] if ] curry assoc-all? ;
100 : assoc= ( assoc1 assoc2 -- ? )
101 [ assoc-subset? ] [ swap assoc-subset? ] 2bi and ;
103 : assoc-hashcode ( n assoc -- code )
105 [ over ] dip hashcode* 2/ [ dupd hashcode* ] dip bitxor
106 ] { } assoc>map hashcode* ;
108 : assoc-intersect ( assoc1 assoc2 -- intersection )
109 swap [ nip key? ] curry assoc-filter ;
111 : update ( assoc1 assoc2 -- )
112 swap [ swapd set-at ] curry assoc-each ;
114 : assoc-union ( assoc1 assoc2 -- union )
115 [ [ [ assoc-size ] bi@ + ] [ drop ] 2bi new-assoc ] 2keep
116 [ dupd update ] bi@ ;
118 : assoc-combine ( seq -- union )
119 H{ } clone [ dupd update ] reduce ;
121 : assoc-diff ( assoc1 assoc2 -- diff )
122 [ nip key? not ] curry assoc-filter ;
124 : remove-all ( assoc seq -- subseq )
125 swap [ key? not ] curry filter ;
127 : substituter ( assoc -- quot )
128 [ dupd at* [ nip ] [ drop ] if ] curry ; inline
130 : substitute-here ( seq assoc -- )
131 substituter change-each ;
133 : substitute ( seq assoc -- newseq )
136 : cache ( key assoc quot -- value )
137 [ [ at* ] 2keep ] dip
138 [ [ nip call dup ] [ drop ] 3bi set-at ] 3curry
142 : 2cache ( key1 key2 assoc quot -- value )
143 [ 2array ] 2dip [ first2 ] prepose cache ; inline
145 : change-at ( key assoc quot -- )
146 [ [ at ] dip call ] [ drop ] 3bi set-at ; inline
148 : at+ ( n key assoc -- ) [ 0 or + ] change-at ; inline
150 : inc-at ( key assoc -- ) [ 1 ] 2dip at+ ; inline
152 : map>assoc ( seq quot exemplar -- assoc )
153 [ [ 2array ] compose { } map-as ] dip assoc-like ; inline
155 : extract-keys ( seq assoc -- subassoc )
156 [ [ dupd at ] curry ] keep map>assoc ;
158 GENERIC: value-at* ( value assoc -- key/f ? )
160 M: assoc value-at* swap [ = nip ] curry assoc-find nip ;
162 : value-at ( value assoc -- key/f ) value-at* drop ;
164 : value? ( value assoc -- ? ) value-at* nip ;
166 : push-at ( value key assoc -- )
167 [ ?push ] change-at ;
169 : zip ( keys values -- alist )
172 : unzip ( assoc -- keys values )
173 dup assoc-empty? [ drop { } { } ] [ >alist flip first2 ] if ;
175 : search-alist ( key alist -- pair/f i/f )
176 [ first = ] with find swap ; inline
179 search-alist [ second t ] [ f ] if ;
184 [ drop [ swap 2array ] dip push ] if ;
186 M: sequence new-assoc drop <vector> ;
188 M: sequence clear-assoc delete-all ;
190 M: sequence delete-at
191 [ nip ] [ search-alist nip ] 2bi
192 [ swap delete-nth ] [ drop ] if* ;
194 M: sequence assoc-size length ;
196 M: sequence assoc-clone-like
197 [ >alist ] dip clone-like ;
199 M: sequence assoc-like
200 [ >alist ] dip like ;
204 ! Override sequence => assoc instance for f
205 M: f clear-assoc drop ;
207 M: f assoc-like drop dup assoc-empty? [ drop f ] when ;
209 INSTANCE: sequence assoc
216 seq>> 2dup bounds-check?
217 [ nth t ] [ 2drop f f ] if ;
219 M: enum set-at seq>> set-nth ;
221 M: enum delete-at seq>> delete-nth ;
223 M: enum >alist ( enum -- alist )
224 seq>> [ length ] keep zip ;
226 M: enum assoc-size seq>> length ;
228 M: enum clear-assoc seq>> delete-all ;