Skip to content
This repository has been archived by the owner on Oct 4, 2020. It is now read-only.

Commit

Permalink
Merge pull request #105 from matthewleon/simplify-pop
Browse files Browse the repository at this point in the history
simplify pop code
  • Loading branch information
paf31 authored Jun 3, 2017
2 parents 3a2e9ec + b60c246 commit 808055b
Showing 1 changed file with 11 additions and 11 deletions.
22 changes: 11 additions & 11 deletions src/Data/Map.purs
Original file line number Diff line number Diff line change
Expand Up @@ -313,21 +313,21 @@ delete k m = maybe m snd (pop k m)
-- | Delete a key and its corresponding value from a map, returning the value
-- | as well as the subsequent map.
pop :: forall k v. Ord k => k -> Map k v -> Maybe (Tuple v (Map k v))
pop = down Nil
pop k = down Nil
where
comp :: k -> k -> Ordering
comp = compare

down :: List (TreeContext k v) -> k -> Map k v -> Maybe (Tuple v (Map k v))
down = unsafePartial \ctx k m -> case m of
down :: List (TreeContext k v) -> Map k v -> Maybe (Tuple v (Map k v))
down = unsafePartial \ctx m -> case m of
Leaf -> Nothing
Two left k1 v1 right ->
case right, comp k k1 of
Leaf, EQ -> Just (Tuple v1 (up ctx Leaf))
_ , EQ -> let max = maxNode left
in Just (Tuple v1 (removeMaxNode (Cons (TwoLeft max.key max.value right) ctx) left))
_ , LT -> down (Cons (TwoLeft k1 v1 right) ctx) k left
_ , _ -> down (Cons (TwoRight left k1 v1) ctx) k right
_ , LT -> down (Cons (TwoLeft k1 v1 right) ctx) left
_ , _ -> down (Cons (TwoRight left k1 v1) ctx) right
Three left k1 v1 mid k2 v2 right ->
let leaves =
case left, mid, right of
Expand All @@ -340,9 +340,9 @@ pop = down Nil
in Just (Tuple v1 (removeMaxNode (Cons (ThreeLeft max.key max.value mid k2 v2 right) ctx) left))
_ , _ , EQ -> let max = maxNode mid
in Just (Tuple v2 (removeMaxNode (Cons (ThreeMiddle left k1 v1 max.key max.value right) ctx) mid))
_ , LT, _ -> down (Cons (ThreeLeft k1 v1 mid k2 v2 right) ctx) k left
_ , GT, LT -> down (Cons (ThreeMiddle left k1 v1 k2 v2 right) ctx) k mid
_ , _ , _ -> down (Cons (ThreeRight left k1 v1 mid k2 v2) ctx) k right
_ , LT, _ -> down (Cons (ThreeLeft k1 v1 mid k2 v2 right) ctx) left
_ , GT, LT -> down (Cons (ThreeMiddle left k1 v1 k2 v2 right) ctx) mid
_ , _ , _ -> down (Cons (ThreeRight left k1 v1 mid k2 v2) ctx) right

up :: List (TreeContext k v) -> Map k v -> Map k v
up = unsafePartial \ctxs tree ->
Expand Down Expand Up @@ -370,17 +370,17 @@ pop = down Nil

maxNode :: Map k v -> { key :: k, value :: v }
maxNode = unsafePartial \m -> case m of
Two _ k v Leaf -> { key: k, value: v }
Two _ k' v Leaf -> { key: k', value: v }
Two _ _ _ right -> maxNode right
Three _ _ _ _ k v Leaf -> { key: k, value: v }
Three _ _ _ _ k' v Leaf -> { key: k', value: v }
Three _ _ _ _ _ _ right -> maxNode right


removeMaxNode :: List (TreeContext k v) -> Map k v -> Map k v
removeMaxNode = unsafePartial \ctx m ->
case m of
Two Leaf _ _ Leaf -> up ctx Leaf
Two left k v right -> removeMaxNode (Cons (TwoRight left k v) ctx) right
Two left k' v right -> removeMaxNode (Cons (TwoRight left k' v) ctx) right
Three Leaf k1 v1 Leaf _ _ Leaf -> up (Cons (TwoRight Leaf k1 v1) ctx) Leaf
Three left k1 v1 mid k2 v2 right -> removeMaxNode (Cons (ThreeRight left k1 v1 mid k2 v2) ctx) right

Expand Down

0 comments on commit 808055b

Please sign in to comment.