factor/basis/persistent/hashtables/nodes/bitmap/bitmap.factor

80 lines
2.2 KiB
Factor
Raw Normal View History

2008-08-06 02:06:14 -04:00
! Based on Clojure's PersistentHashMap by Rich Hickey.
2008-09-05 20:29:14 -04:00
USING: math math.bitwise arrays kernel accessors locals sequences
sequences.private
2008-08-06 02:06:14 -04:00
persistent.sequences
persistent.hashtables.config
persistent.hashtables.nodes ;
IN: persistent.hashtables.nodes.bitmap
: index ( bit bitmap -- n ) [ 1 - ] dip bitand bit-count ; inline
2008-08-06 02:06:14 -04:00
M:: bitmap-node (entry-at) ( key hashcode bitmap-node -- entry )
2009-10-27 22:50:31 -04:00
bitmap-node shift>> :> shift
hashcode shift bitpos :> bit
bitmap-node bitmap>> :> bitmap
bitmap-node nodes>> :> nodes
bitmap bit bitand 0 eq? [ f ] [
key hashcode
bit bitmap index nodes nth-unsafe
(entry-at)
] if ;
2008-08-06 02:06:14 -04:00
M:: bitmap-node (new-at) ( shift value key hashcode bitmap-node -- node' added-leaf )
2009-10-27 22:50:31 -04:00
bitmap-node shift>> :> shift
hashcode shift bitpos :> bit
bitmap-node bitmap>> :> bitmap
bit bitmap index :> idx
bitmap-node nodes>> :> nodes
bitmap bit bitand 0 eq? [
value key hashcode <leaf-node> :> new-leaf
bitmap bit bitor
new-leaf idx nodes insert-nth
shift
<bitmap-node>
new-leaf
] [
idx nodes nth :> n
shift radix-bits + value key hashcode n (new-at) :> ( n' new-leaf )
2009-10-28 14:56:15 -04:00
n n' eq? [
bitmap-node
] [
bitmap
n' idx nodes new-nth
shift
<bitmap-node>
] if
new-leaf
2009-10-27 22:50:31 -04:00
] if ;
2008-08-06 02:06:14 -04:00
M:: bitmap-node (pluck-at) ( key hashcode bitmap-node -- node' )
2009-10-27 22:50:31 -04:00
hashcode bitmap-node shift>> bitpos :> bit
bitmap-node bitmap>> :> bitmap
bitmap-node nodes>> :> nodes
bitmap-node shift>> :> shift
bit bitmap bitand 0 eq? [ bitmap-node ] [
bit bitmap index :> idx
idx nodes nth-unsafe :> n
key hashcode n (pluck-at) :> n'
n n' eq? [
bitmap-node
] [
n' [
bitmap
n' idx nodes new-nth
shift
<bitmap-node>
] [
bitmap bit eq? [ f ] [
bitmap bit bitnot bitand
idx nodes remove-nth
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
] if
2009-10-27 22:50:31 -04:00
] if ;
2008-08-06 02:06:14 -04:00
M: bitmap-node >alist% ( node -- ) nodes>> >alist-each% ;