uncomment more of rbtree

This commit is contained in:
Folkert 2020-11-12 00:34:35 +01:00
parent 55b26277ca
commit eaf94f2cfc
2 changed files with 35 additions and 24 deletions

View file

@ -362,7 +362,7 @@ pub fn constrain_expr(
vars.push(closure_ext_var); vars.push(closure_ext_var);
vars.push(*fn_var); vars.push(*fn_var);
let body_type = NoExpectation(return_type.clone()); let body_type = NoExpectation(return_type);
let ret_constraint = let ret_constraint =
constrain_expr(env, loc_body_expr.region, &loc_body_expr.value, body_type); constrain_expr(env, loc_body_expr.region, &loc_body_expr.value, body_type);

View file

@ -1,4 +1,4 @@
interface RBTree exposes [ Dict, empty, size, singleton ] imports [] interface RBTree exposes [ Dict, empty, size, singleton, isEmpty, insert, remove, update ] imports []
# The color of a node. Leaves are considered Black. # The color of a node. Leaves are considered Black.
NodeColor : [ Red, Black ] NodeColor : [ Red, Black ]
@ -147,6 +147,8 @@ removeHelp = \targetKey, dict ->
removeHelpPrepEQGT : Key k, Dict (Key k) v, NodeColor, (Key k), v, Dict (Key k) v, Dict (Key k) v -> Dict (Key k) v removeHelpPrepEQGT : Key k, Dict (Key k) v, NodeColor, (Key k), v, Dict (Key k) v, Dict (Key k) v -> Dict (Key k) v
removeHelpPrepEQGT = \_, dict, color, key, value, left, right -> removeHelpPrepEQGT = \_, dict, color, key, value, left, right ->
when left is when left is
@ -171,6 +173,7 @@ removeHelpPrepEQGT = \_, dict, color, key, value, left, right ->
# When we find the node we are looking for, we can remove by replacing the key-value # When we find the node we are looking for, we can remove by replacing the key-value
# pair with the key-value pair of the left-most node on the right side (the closest pair). # pair with the key-value pair of the left-most node on the right side (the closest pair).
removeHelpEQGT : Key k, Dict (Key k) v -> Dict (Key k) v removeHelpEQGT : Key k, Dict (Key k) v -> Dict (Key k) v
@ -192,6 +195,7 @@ removeHelpEQGT = \targetKey, dict ->
getMin : Dict k v -> Dict k v getMin : Dict k v -> Dict k v
getMin = \dict -> getMin = \dict ->
when dict is when dict is
@ -204,12 +208,14 @@ getMin = \dict ->
_ -> _ ->
dict dict
moveRedLeft : Dict k v -> Dict k v moveRedLeft : Dict k v -> Dict k v
moveRedLeft = \dict -> moveRedLeft = \dict ->
when dict is when dict is
# Node clr k v (Node lClr lK lV lLeft lRight) (Node rClr rK rV ((Node Red rlK rlV rlL rlR) as rLeft) rRight) -> # Node clr k v (Node lClr lK lV lLeft lRight) (Node rClr rK rV ((Node Red rlK rlV rlL rlR) as rLeft) rRight) ->
Node clr k v (Node lClr lK lV lLeft lRight) (Node rClr rK rV rLeft rRight) -> # Node clr k v (Node lClr lK lV lLeft lRight) (Node rClr rK rV rLeft rRight) ->
when rList is Node clr k v (Node _ lK lV lLeft lRight) (Node _ rK rV rLeft rRight) ->
when rLeft is
Node Red rlK rlV rlL rlR -> Node Red rlK rlV rlL rlR ->
Node Node
Red Red
@ -274,30 +280,34 @@ moveRedRight = \dict ->
removeMin : Dict k v -> Dict k v removeMin : Dict k v -> Dict k v
removeMin = \dict -> removeMin = \dict ->
when dict is when dict is
Node color key value ((Node lColor _ _ lLeft _) as left) right -> Node color key value left right ->
when lColor is when left is
Black -> Node lColor _ _ lLeft _ ->
when lLeft is when lColor is
Node Red _ _ _ _ -> Black ->
Node color key value (removeMin left) right when lLeft is
Node Red _ _ _ _ ->
Node color key value (removeMin left) right
_ ->
when moveRedLeft dict is
Node nColor nKey nValue nLeft nRight ->
balance nColor nKey nValue (removeMin nLeft) nRight
Empty ->
Empty
_ ->
Node color key value (removeMin left) right
_ -> _ ->
when moveRedLeft dict is Empty
Node nColor nKey nValue nLeft nRight ->
balance nColor nKey nValue (removeMin nLeft) nRight
Empty ->
Empty
_ ->
Node color key value (removeMin left) right
_ -> _ ->
Empty Empty
# Update the value of a dictionary for a specific key with a given function. # Update the value of a dictionary for a specific key with a given function.
update : Key k, (Maybe v, Maybe v), Dict (Key k) v -> Dict (Key k) v update : Key k, (Maybe v -> Maybe v), Dict (Key k) v -> Dict (Key k) v
update = \targetKey, alter, dictionary -> update = \targetKey, alter, dictionary ->
when alter (get targetKey dictionary) is when alter (get targetKey dictionary) is
Just value -> Just value ->
@ -305,3 +315,4 @@ update = \targetKey, alter, dictionary ->
Nothing -> Nothing ->
remove targetKey dictionary remove targetKey dictionary