Distinguish into internal and leaf nodes; Implement appending leaf node
No internal node splittingmaster
parent
0674a825b0
commit
9de4680d39
51
cb/cb.factor
51
cb/cb.factor
|
@ -16,7 +16,7 @@
|
|||
|
||||
USING: accessors arrays assocs kernel math namespaces sequences serialize trees
|
||||
trees.private ;
|
||||
IN: trees.cb;
|
||||
IN: trees.cb
|
||||
|
||||
TUPLE: cb < tree ;
|
||||
|
||||
|
@ -24,13 +24,16 @@ TUPLE: cb < tree ;
|
|||
|
||||
<PRIVATE
|
||||
|
||||
TUPLE: cb-node < node { byte# integer } { bits integer } ;
|
||||
TUPLE: cb-node { byte# integer } { bits integer } left right ;
|
||||
|
||||
: <cb-node> ( byte# bits key value -- node )
|
||||
cb-node new-node
|
||||
: new-node ( byte# bits class -- node )
|
||||
new
|
||||
swap >>bits
|
||||
swap >>byte# ; inline
|
||||
|
||||
: <cb-node> ( byte# bits -- node )
|
||||
cb-node new-node ;
|
||||
|
||||
! 0 = left
|
||||
! 1 = right
|
||||
: direction ( n n -- direction )
|
||||
|
@ -49,8 +52,8 @@ SYMBOL: current-key
|
|||
|
||||
: with-key ( key quot -- )
|
||||
[
|
||||
{ current-key key-bytes }
|
||||
[ dup object>bytes 2array ] dip zip
|
||||
{ current-key key-bytes } swap
|
||||
dup object>bytes 2array zip
|
||||
] dip with-variables ; inline
|
||||
|
||||
: byte-at ( byte# -- byte/0 )
|
||||
|
@ -61,29 +64,33 @@ SYMBOL: current-key
|
|||
|
||||
DEFER: cb-set
|
||||
|
||||
: cb-insert ( value key node -- node taller? created? )
|
||||
f
|
||||
swap [
|
||||
dup [ bits>> ] [ byte#>> ] bi
|
||||
byte-at key-side [
|
||||
node-link cb-set
|
||||
] with-side
|
||||
] with-key ;
|
||||
: cb-insert ( value node -- node created? )
|
||||
;
|
||||
|
||||
! Implement this and cb-insert using SYMBOL for current key bytes while
|
||||
! traversing the tree.
|
||||
: (cb-set) ( value key node -- node taller? created? )
|
||||
drop
|
||||
dup key>> current-key get = [
|
||||
current-key get >>key swap >>value f f
|
||||
] [ current-key get swap cb-insert ] if ;
|
||||
: (cb-set) ( value node -- created? )
|
||||
dup cb-node? [
|
||||
dup [ bits>> ] [ byte#>> ] bi
|
||||
byte-at key-side [
|
||||
[ node-link cb-set ] keep
|
||||
swap [ set-node-link ] dip
|
||||
] with-side
|
||||
] [
|
||||
dup key>> current-key get = [
|
||||
dup current-key get
|
||||
[ value<< ] [ >>key drop ] 2bi* f
|
||||
] [
|
||||
2drop f ! cb-insert
|
||||
] if
|
||||
] if ;
|
||||
|
||||
! Insert a node into the tree
|
||||
: cb-set ( value key node -- node taller? created? )
|
||||
[ (cb-set) ] [ [ 0 byte-at 0xfe ] 2dip swap <cb-node> t t ] if* ;
|
||||
: cb-set ( value node -- node created? )
|
||||
[ [ (cb-set) ] keep ] [ current-key get swap <node> t ] if* ;
|
||||
|
||||
PRIVATE>
|
||||
|
||||
M: cb set-at ( value key cb -- )
|
||||
[ cb-set nip swap ] change-root
|
||||
[ swap [ cb-set ] with-key ] change-root
|
||||
swap [ dup inc-count ] when drop ;
|
||||
|
|
Loading…
Reference in New Issue