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: value-at* ( value assoc -- key/f ? )
11 GENERIC: set-at ( value key assoc -- )
12 GENERIC: new-assoc ( capacity exemplar -- newassoc )
13 GENERIC: delete-at ( key assoc -- )
14 GENERIC: clear-assoc ( assoc -- )
15 GENERIC: assoc-size ( assoc -- n )
16 GENERIC: assoc-like ( assoc exemplar -- newassoc )
17 GENERIC: assoc-clone-like ( assoc exemplar -- newassoc )
18 GENERIC: >alist ( assoc -- newassoc )
20 M: assoc assoc-like drop ;
24 : (assoc-each) ( assoc quot -- seq quot' )
25 [ >alist ] dip [ first2 ] prepose ; inline
27 : (assoc-stack) ( key i seq -- value )
32 [ [ 3drop ] dip ] [ drop [ 1- ] dip (assoc-stack) ] if
33 ] if ; inline recursive
35 : search-alist ( key alist -- pair/f i/f )
36 [ first = ] with find swap ; inline
38 : substituter ( assoc -- quot )
39 [ dupd at* [ nip ] [ drop ] if ] curry ; inline
41 : with-assoc ( assoc quot: ( value key -- assoc ) -- quot: ( key value -- ) )
42 curry [ swap ] prepose ; inline
46 : assoc-find ( assoc quot -- key value ? )
47 (assoc-each) find swap [ first2 t ] [ drop f f f ] if ; inline
49 : key? ( key assoc -- ? ) at* nip ; inline
51 : assoc-each ( assoc quot -- )
52 (assoc-each) each ; inline
54 : assoc>map ( assoc quot exemplar -- seq )
55 [ accumulator [ assoc-each ] dip ] dip like ; inline
57 : assoc-map-as ( assoc quot exemplar -- newassoc )
58 [ [ 2array ] compose V{ } assoc>map ] dip assoc-like ; inline
60 : assoc-map ( assoc quot -- newassoc )
61 over assoc-map-as ; inline
63 : assoc-filter-as ( assoc quot exemplar -- subassoc )
64 [ (assoc-each) filter ] dip assoc-like ; inline
66 : assoc-filter ( assoc quot -- subassoc )
67 over assoc-filter-as ; inline
69 : assoc-partition ( assoc quot -- true-assoc false-assoc )
70 [ (assoc-each) partition ] [ drop ] 2bi
71 tuck [ assoc-like ] 2bi@ ; inline
73 : assoc-contains? ( assoc quot -- ? )
74 assoc-find 2nip ; inline
76 : assoc-all? ( assoc quot -- ? )
77 [ not ] compose assoc-contains? not ; inline
79 : at ( key assoc -- value/f )
82 : at-default ( key assoc -- value/key )
83 2dup at* [ 2nip ] [ 2drop ] if ; inline
85 M: assoc assoc-clone-like ( assoc exemplar -- newassoc )
86 [ dup assoc-size ] dip new-assoc
87 [ [ set-at ] with-assoc assoc-each ] keep ;
89 : keys ( assoc -- keys )
90 [ drop ] { } assoc>map ;
92 : values ( assoc -- values )
93 [ nip ] { } assoc>map ;
95 : delete-at* ( key assoc -- old ? )
96 [ at* ] 2keep delete-at ;
98 : rename-at ( newkey key assoc -- )
99 [ delete-at* ] keep [ set-at ] with-assoc [ 2drop ] if ;
101 : assoc-empty? ( assoc -- ? )
104 : assoc-stack ( key seq -- value )
105 [ length 1- ] keep (assoc-stack) ; flushable
107 : assoc-subset? ( assoc1 assoc2 -- ? )
108 [ at* [ = ] [ 2drop f ] if ] with-assoc assoc-all? ;
110 : assoc= ( assoc1 assoc2 -- ? )
111 [ assoc-subset? ] [ swap assoc-subset? ] 2bi and ;
113 : assoc-hashcode ( n assoc -- code )
116 : assoc-intersect ( assoc1 assoc2 -- intersection )
117 swap [ nip key? ] curry assoc-filter ;
119 : update ( assoc1 assoc2 -- )
120 swap [ set-at ] with-assoc assoc-each ;
122 : assoc-union ( assoc1 assoc2 -- union )
123 [ [ [ assoc-size ] bi@ + ] [ drop ] 2bi new-assoc ] 2keep
124 [ dupd update ] bi@ ;
126 : assoc-combine ( seq -- union )
127 H{ } clone [ dupd update ] reduce ;
129 : assoc-diff ( assoc1 assoc2 -- diff )
130 [ nip key? not ] curry assoc-filter ;
132 : remove-all ( assoc seq -- subseq )
133 swap [ key? not ] curry filter ;
135 : substitute-here ( seq assoc -- )
136 substituter change-each ;
138 : substitute ( seq assoc -- newseq )
141 : cache ( key assoc quot -- value )
142 [ [ at* ] 2keep ] dip
143 [ [ nip call dup ] [ drop ] 3bi set-at ] 3curry
147 : 2cache ( key1 key2 assoc quot -- value )
148 [ 2array ] 2dip [ first2 ] prepose cache ; inline
150 : change-at ( key assoc quot -- )
151 [ [ at ] dip call ] [ drop ] 3bi set-at ; inline
153 : at+ ( n key assoc -- ) [ 0 or + ] change-at ; inline
155 : inc-at ( key assoc -- ) [ 1 ] 2dip at+ ; inline
157 : map>assoc ( seq quot exemplar -- assoc )
158 [ [ 2array ] compose { } map-as ] dip assoc-like ; inline
160 : extract-keys ( seq assoc -- subassoc )
161 [ [ dupd at ] curry ] keep map>assoc ;
163 M: assoc value-at* swap [ = nip ] curry assoc-find nip ;
165 : value-at ( value assoc -- key/f ) value-at* drop ;
167 : value? ( value assoc -- ? ) value-at* nip ;
169 : push-at ( value key assoc -- )
170 [ ?push ] change-at ;
172 : zip ( keys values -- alist )
175 : unzip ( assoc -- keys values )
176 dup assoc-empty? [ drop { } { } ] [ >alist flip first2 ] if ;
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 ;