1 ! Based on Clojure's PersistentHashMap by Rich Hickey.
3 USING: accessors assocs combinators kernel make math
4 parser persistent.assocs persistent.hashtables.nodes
7 IN: persistent.hashtables
10 { root read-only initial: empty-node }
11 { count fixnum read-only } ;
13 M: persistent-hash assoc-size count>> ;
15 M: persistent-hash at*
16 [ dup hashcode >fixnum ] [ root>> ] bi* (entry-at)
17 dup [ value>> t ] [ f ] if ;
19 M: persistent-hash new-at
22 [ dup hashcode >fixnum ] [ root>> ] bi* (new-at) 1 0 ?
23 ] [ count>> ] bi + persistent-hash boa ;
25 M: persistent-hash pluck-at
26 [ [ dup hashcode >fixnum ] [ root>> ] bi* (pluck-at) ] keep
28 { [ 2dup root>> eq? ] [ nip ] }
29 { [ over not ] [ 2drop T{ persistent-hash } ] }
30 [ count>> 1 - persistent-hash boa ]
33 M: persistent-hash >alist [ root>> >alist% ] { } make ;
35 M: persistent-hash keys >alist keys ;
37 M: persistent-hash values >alist values ;
39 : >persistent-hash ( assoc -- phash )
40 T{ persistent-hash } swap [ swap rot new-at ] assoc-each ;
42 M: persistent-hash equal?
43 over persistent-hash? [ assoc= ] [ 2drop f ] if ;
45 M: persistent-hash hashcode* nip assoc-size ;
47 M: persistent-hash clone ;
49 SYNTAX: PH{ \ } [ >persistent-hash ] parse-literal ;
51 M: persistent-hash pprint-delims drop \ PH{ \ } ;
52 M: persistent-hash >pprint-sequence >alist ;
53 M: persistent-hash pprint* pprint-object ;
55 : passociate ( value key -- phash )
56 T{ persistent-hash } new-at ; inline