factor/basis/persistent/hashtables/nodes/full/full.factor

50 lines
1.3 KiB
Factor
Raw Normal View History

2008-08-06 02:06:14 -04:00
! Based on Clojure's PersistentHashMap by Rich Hickey.
USING: math accessors kernel arrays sequences sequences.private
2008-09-05 20:29:14 -04:00
locals
2008-08-06 02:06:14 -04:00
persistent.sequences
persistent.hashtables.config
persistent.hashtables.nodes ;
IN: persistent.hashtables.nodes.full
M:: full-node (new-at) ( shift value key hashcode full-node -- node' added-leaf )
2009-10-27 22:50:31 -04:00
full-node nodes>> :> nodes
hashcode full-node shift>> mask :> idx
idx nodes nth-unsafe :> n
shift radix-bits + value key hashcode n (new-at) :> ( n' new-leaf )
2009-10-27 22:50:31 -04:00
n n' eq? [
full-node
] [
n' idx nodes new-nth shift <full-node>
] if
new-leaf ;
2008-08-06 02:06:14 -04:00
M:: full-node (pluck-at) ( key hashcode full-node -- node' )
2009-10-27 22:50:31 -04:00
hashcode full-node shift>> mask :> idx
idx full-node nodes>> nth :> n
key hashcode n (pluck-at) :> n'
n n' eq? [
full-node
] [
n' [
n' idx full-node nodes>> new-nth
full-node shift>>
<full-node>
2008-08-06 02:06:14 -04:00
] [
2009-10-27 22:50:31 -04:00
hashcode full-node shift>> bitpos bitnot full-bitmap-mask bitand
idx full-node nodes>> remove-nth
full-node shift>>
<bitmap-node>
2008-08-06 02:06:14 -04:00
] if
2009-10-27 22:50:31 -04:00
] if ;
2008-08-06 02:06:14 -04:00
M:: full-node (entry-at) ( key hashcode full-node -- node' )
key hashcode
hashcode full-node shift>> mask
full-node nodes>> nth-unsafe
(entry-at) ;
M: full-node >alist% nodes>> >alist-each% ;