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

Commit a991925

Browse files
committed
minView and maxView
1 parent 0ce5038 commit a991925

File tree

2 files changed

+85
-19
lines changed

2 files changed

+85
-19
lines changed

src/Data/Map.purs

+67-17
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,8 @@ module Data.Map
1818
, findMax
1919
, deleteMin
2020
, deleteMax
21+
, minView
22+
, maxView
2123
, foldSubmap
2224
, submap
2325
, fromFoldable
@@ -299,27 +301,75 @@ findMin = go Nothing
299301
-- |
300302
-- | Return an empty map if the map is empty.
301303
deleteMin :: forall k v. Ord k => Map k v -> Map k v
302-
deleteMin Leaf = Leaf
303-
deleteMin n = down Nil n
304+
deleteMin = maybe Leaf _.strippedMap <<< minView
305+
306+
-- | Delete the pair with the greatest key. O(logn).
307+
-- |
308+
-- | Return an empty map if the map is empty.
309+
deleteMax :: forall k v. Ord k => Map k v -> Map k v
310+
deleteMax = maybe Leaf _.strippedMap <<< maxView
311+
312+
-- | Retrieves the least key and the value corresponding to that key,
313+
-- | and the map stripped of that element. O(logn)
314+
-- |
315+
-- | Returns Nothing if the map is empty.
316+
minView
317+
:: forall k v
318+
. Ord k
319+
=> Map k v
320+
-> Maybe { key :: k, value :: v, strippedMap :: Map k v}
321+
minView Leaf = Nothing
322+
minView n = Just $ down Nil n
304323
where
305-
down :: List (TreeContext k v) -> Map k v -> Map k v
324+
down
325+
:: List (TreeContext k v)
326+
-> Map k v
327+
-> { key :: k, value :: v, strippedMap :: Map k v}
306328
down = unsafePartial \ctx -> case _ of
307329
Two left k v right ->
308330
case left, right of
309-
Leaf, Leaf -> deleteUp ctx Leaf
331+
Leaf, Leaf -> { key: k, value: v, strippedMap: deleteUp ctx Leaf }
310332
_ , _ -> down (Cons (TwoLeft k v right) ctx) left
311333
Three left k1 v1 mid k2 v2 right ->
312334
case left, mid, right of
313-
Leaf, Leaf, Leaf -> fromZipper ctx (Two Leaf k2 v2 Leaf)
335+
Leaf, Leaf, Leaf ->
336+
{ key: k1
337+
, value: v1
338+
, strippedMap: fromZipper ctx (Two Leaf k2 v2 Leaf)
339+
}
314340
_ , _ , _ ->
315341
down (Cons (ThreeLeft k1 v1 mid k2 v2 right) ctx) left
316342

317-
-- | Delete the pair with the greatest key. O(logn).
343+
-- | Retrieves the greatest key and the value corresponding to that key,
344+
-- | and the map stripped of that element. O(logn)
318345
-- |
319-
-- | Return an empty map if the map is empty.
320-
deleteMax :: forall k v. Ord k => Map k v -> Map k v
321-
deleteMax Leaf = Leaf
322-
deleteMax n = removeMaxNode Nil n
346+
-- | Returns Nothing if the map is empty.
347+
maxView
348+
:: forall k v
349+
. Ord k
350+
=> Map k v
351+
-> Maybe { key :: k, value :: v, strippedMap :: Map k v}
352+
maxView Leaf = Nothing
353+
maxView n = Just $ down Nil n
354+
where
355+
down
356+
:: List (TreeContext k v)
357+
-> Map k v
358+
-> { key :: k, value :: v, strippedMap :: Map k v}
359+
down = unsafePartial \ctx -> case _ of
360+
Two left k v right ->
361+
case left, right of
362+
Leaf, Leaf -> { key: k, value: v, strippedMap: deleteUp ctx Leaf }
363+
_ , _ -> down (Cons (TwoRight left k v) ctx) right
364+
Three left k1 v1 mid k2 v2 right ->
365+
case left, mid, right of
366+
Leaf, Leaf, Leaf ->
367+
{ key: k2
368+
, value: v2
369+
, strippedMap: fromZipper ctx (Two Leaf k1 v1 Leaf)
370+
}
371+
_ , _ , _ ->
372+
down (Cons (ThreeRight left k1 v1 mid k2 v2) ctx) right
323373

324374
-- | Fold over the entries of a given map where the key is between a lower and
325375
-- | an upper bound. Passing `Nothing` as either the lower or upper bound
@@ -526,13 +576,13 @@ pop k = down Nil
526576
Three _ _ _ _ k' v Leaf -> { key: k', value: v }
527577
Three _ _ _ _ _ _ right -> maxNode right
528578

529-
530-
removeMaxNode :: forall k v. Ord k => List (TreeContext k v) -> Map k v -> Map k v
531-
removeMaxNode = unsafePartial \ctx -> case _ of
532-
Two Leaf _ _ Leaf -> deleteUp ctx Leaf
533-
Two left k' v right -> removeMaxNode (Cons (TwoRight left k' v) ctx) right
534-
Three Leaf k1 v1 Leaf _ _ Leaf -> deleteUp (Cons (TwoRight Leaf k1 v1) ctx) Leaf
535-
Three left k1 v1 mid k2 v2 right -> removeMaxNode (Cons (ThreeRight left k1 v1 mid k2 v2) ctx) right
579+
removeMaxNode :: List (TreeContext k v) -> Map k v -> Map k v
580+
removeMaxNode = unsafePartial \ctx m ->
581+
case m of
582+
Two Leaf _ _ Leaf -> deleteUp ctx Leaf
583+
Two left k' v right -> removeMaxNode (Cons (TwoRight left k' v) ctx) right
584+
Three Leaf k1 v1 Leaf _ _ Leaf -> deleteUp (Cons (TwoRight Leaf k1 v1) ctx) Leaf
585+
Three left k1 v1 mid k2 v2 right -> removeMaxNode (Cons (ThreeRight left k1 v1 mid k2 v2) ctx) right
536586

537587
deleteUp :: forall k v. Ord k => List (TreeContext k v) -> Map k v -> Map k v
538588
deleteUp = unsafePartial \ctxs tree ->

test/Test/Data/Map.purs

+18-2
Original file line numberDiff line numberDiff line change
@@ -9,11 +9,11 @@ import Control.Monad.Eff.Random (RANDOM)
99
import Data.Array as A
1010
import Data.Foldable (foldl, for_, all)
1111
import Data.Function (on)
12-
import Data.List (List(Cons), groupBy, length, nubBy, singleton, sort, sortBy, tail, init)
12+
import Data.List (List(Cons), groupBy, length, nubBy, singleton, sort, sortBy, tail, init, uncons, unsnoc)
1313
import Data.List.NonEmpty as NEL
1414
import Data.Map as M
1515
import Data.Map.Gen (genMap)
16-
import Data.Maybe (Maybe(..), fromMaybe, maybe)
16+
import Data.Maybe (Maybe(..), fromMaybe, maybe, isNothing)
1717
import Data.NonEmpty ((:|))
1818
import Data.Tuple (Tuple(..), fst, uncurry)
1919
import Partial.Unsafe (unsafePartial)
@@ -277,6 +277,22 @@ mapTests = do
277277
quickCheck $ \(TestMap m :: TestMap String Int) ->
278278
M.deleteMax m == maybe m M.fromFoldable (init $ M.toAscUnfoldable m)
279279

280+
log "minView result is correct"
281+
quickCheck $ \(TestMap m :: TestMap String Int) ->
282+
case uncons (M.toAscUnfoldable m) of
283+
Nothing -> isNothing $ M.minView m
284+
Just {head: (Tuple k v), tail} -> unsafePartial
285+
let Just {key: minK, value: minV, strippedMap: sM} = M.minView m
286+
in minK == k && minV == v && sM == (M.fromFoldable tail)
287+
288+
log "maxView result is correct"
289+
quickCheck $ \(TestMap m :: TestMap String Int) ->
290+
case unsnoc (M.toAscUnfoldable m) of
291+
Nothing -> isNothing $ M.minView m
292+
Just {last: (Tuple k v), init} -> unsafePartial
293+
let Just {key: maxK, value: maxV, strippedMap: sM} = M.maxView m
294+
in maxK == k && maxV == v && sM == (M.fromFoldable init)
295+
280296
log "mapWithKey is correct"
281297
quickCheck $ \(TestMap m :: TestMap String Int) -> let
282298
f k v = k <> show v

0 commit comments

Comments
 (0)