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
43 : assoc-find ( assoc quot -- key value ? )
44 (assoc-each) find swap [ first2 t ] [ drop f f f ] if ; inline
46 : key? ( key assoc -- ? ) at* nip ; inline
48 : assoc-each ( assoc quot -- )
49 (assoc-each) each ; inline
51 : assoc>map ( assoc quot exemplar -- seq )
52 [ accumulator [ assoc-each ] dip ] dip like ; inline
54 : assoc-map-as ( assoc quot exemplar -- newassoc )
55 [ [ 2array ] compose V{ } assoc>map ] dip assoc-like ; inline
57 : assoc-map ( assoc quot -- newassoc )
58 over assoc-map-as ; inline
60 : assoc-filter-as ( assoc quot exemplar -- subassoc )
61 [ (assoc-each) filter ] dip assoc-like ; inline
63 : assoc-filter ( assoc quot -- subassoc )
64 over assoc-filter-as ; inline
66 : assoc-partition ( assoc quot -- true-assoc false-assoc )
67 [ (assoc-each) partition ] [ drop ] 2bi
68 tuck [ assoc-like ] 2bi@ ; inline
70 : assoc-contains? ( assoc quot -- ? )
71 assoc-find 2nip ; inline
73 : assoc-all? ( assoc quot -- ? )
74 [ not ] compose assoc-contains? not ; inline
76 : at ( key assoc -- value/f )
79 : at-default ( key assoc -- value/key )
80 2dup at* [ 2nip ] [ 2drop ] if ; inline
82 M: assoc assoc-clone-like ( assoc exemplar -- newassoc )
83 [ dup assoc-size ] dip new-assoc
84 [ [ swapd set-at ] curry assoc-each ] keep ;
86 : keys ( assoc -- keys )
87 [ drop ] { } assoc>map ;
89 : values ( assoc -- values )
90 [ nip ] { } assoc>map ;
92 : delete-at* ( key assoc -- old ? )
93 [ at* ] 2keep delete-at ;
95 : rename-at ( newkey key assoc -- )
96 [ delete-at* ] keep [ swapd set-at ] curry [ 2drop ] if ;
98 : assoc-empty? ( assoc -- ? )
101 : assoc-stack ( key seq -- value )
102 [ length 1- ] keep (assoc-stack) ; flushable
104 : assoc-subset? ( assoc1 assoc2 -- ? )
105 [ swapd at* [ = ] [ 2drop f ] if ] curry assoc-all? ;
107 : assoc= ( assoc1 assoc2 -- ? )
108 [ assoc-subset? ] [ swap assoc-subset? ] 2bi and ;
110 : assoc-hashcode ( n assoc -- code )
113 : assoc-intersect ( assoc1 assoc2 -- intersection )
114 swap [ nip key? ] curry assoc-filter ;
116 : update ( assoc1 assoc2 -- )
117 swap [ swapd set-at ] curry assoc-each ;
119 : assoc-union ( assoc1 assoc2 -- union )
120 [ [ [ assoc-size ] bi@ + ] [ drop ] 2bi new-assoc ] 2keep
121 [ dupd update ] bi@ ;
123 : assoc-combine ( seq -- union )
124 H{ } clone [ dupd update ] reduce ;
126 : assoc-diff ( assoc1 assoc2 -- diff )
127 [ nip key? not ] curry assoc-filter ;
129 : remove-all ( assoc seq -- subseq )
130 swap [ key? not ] curry filter ;
132 : substitute-here ( seq assoc -- )
133 substituter change-each ;
135 : substitute ( seq assoc -- newseq )
138 : cache ( key assoc quot -- value )
139 [ [ at* ] 2keep ] dip
140 [ [ nip call dup ] [ drop ] 3bi set-at ] 3curry
144 : 2cache ( key1 key2 assoc quot -- value )
145 [ 2array ] 2dip [ first2 ] prepose cache ; inline
147 : change-at ( key assoc quot -- )
148 [ [ at ] dip call ] [ drop ] 3bi set-at ; inline
150 : at+ ( n key assoc -- ) [ 0 or + ] change-at ; inline
152 : inc-at ( key assoc -- ) [ 1 ] 2dip at+ ; inline
154 : map>assoc ( seq quot exemplar -- assoc )
155 [ [ 2array ] compose { } map-as ] dip assoc-like ; inline
157 : extract-keys ( seq assoc -- subassoc )
158 [ [ dupd at ] curry ] keep map>assoc ;
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 ;
176 search-alist [ second t ] [ f ] if ;
181 [ drop [ swap 2array ] dip push ] if ;
183 M: sequence new-assoc drop <vector> ;
185 M: sequence clear-assoc delete-all ;
187 M: sequence delete-at
188 [ nip ] [ search-alist nip ] 2bi
189 [ swap delete-nth ] [ drop ] if* ;
191 M: sequence assoc-size length ;
193 M: sequence assoc-clone-like
194 [ >alist ] dip clone-like ;
196 M: sequence assoc-like
197 [ >alist ] dip like ;
201 ! Override sequence => assoc instance for f
202 M: f clear-assoc drop ;
204 M: f assoc-like drop dup assoc-empty? [ drop f ] when ;
206 INSTANCE: sequence assoc
213 seq>> 2dup bounds-check?
214 [ nth t ] [ 2drop f f ] if ;
216 M: enum set-at seq>> set-nth ;
218 M: enum delete-at seq>> delete-nth ;
220 M: enum >alist ( enum -- alist )
221 seq>> [ length ] keep zip ;
223 M: enum assoc-size seq>> length ;
225 M: enum clear-assoc seq>> delete-all ;