1 ! Based on Clojure's PersistentHashMap by Rich Hickey.
3 USING: math arrays kernel sequences
4 accessors locals persistent.hashtables.config ;
5 IN: persistent.hashtables.nodes
12 { hashcode fixnum read-only } ;
14 C: <leaf-node> leaf-node
17 { hashcode fixnum read-only }
18 { leaves array read-only } ;
20 C: <collision-node> collision-node
23 { nodes array read-only }
24 { shift fixnum read-only }
25 { hashcode fixnum read-only } ;
27 : <full-node> ( nodes shift -- node )
28 over first hashcode>> full-node boa ;
31 { bitmap fixnum read-only }
32 { nodes array read-only }
33 { shift fixnum read-only }
34 { hashcode fixnum read-only } ;
36 : <bitmap-node> ( bitmap nodes shift -- node )
37 pick full-bitmap-mask =
39 [ over first hashcode>> bitmap-node boa ] if ;
41 GENERIC: (entry-at) ( key hashcode node -- entry )
43 GENERIC: (new-at) ( shift value key hashcode node -- node' added-leaf )
45 GENERIC: (pluck-at) ( key hashcode node -- node' )
47 GENERIC: >alist% ( node -- )
49 : >alist-each% ( nodes -- ) [ >alist% ] each ;
51 : mask ( hash shift -- n ) neg shift radix-mask bitand ; inline
53 : bitpos ( hash shift -- n ) mask 2^ ; inline
55 : smash ( idx seq -- seq/elt ? )
56 dup length 2 = [ [ 1 = ] dip first2 ? f ] [ remove-nth t ] if ; inline
58 :: make-bitmap-node ( shift branch value key hashcode -- node' added-leaf )
59 shift value key hashcode
60 branch hashcode>> shift bitpos