@@ -18,6 +18,8 @@ module Data.Map
18
18
, findMax
19
19
, deleteMin
20
20
, deleteMax
21
+ , minView
22
+ , maxView
21
23
, foldSubmap
22
24
, submap
23
25
, fromFoldable
@@ -299,27 +301,75 @@ findMin = go Nothing
299
301
-- |
300
302
-- | Return an empty map if the map is empty.
301
303
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
304
323
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 }
306
328
down = unsafePartial \ctx -> case _ of
307
329
Two left k v right ->
308
330
case left, right of
309
- Leaf , Leaf -> deleteUp ctx Leaf
331
+ Leaf , Leaf -> { key: k, value: v, strippedMap: deleteUp ctx Leaf }
310
332
_ , _ -> down (Cons (TwoLeft k v right) ctx) left
311
333
Three left k1 v1 mid k2 v2 right ->
312
334
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
+ }
314
340
_ , _ , _ ->
315
341
down (Cons (ThreeLeft k1 v1 mid k2 v2 right) ctx) left
316
342
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)
318
345
-- |
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
323
373
324
374
-- | Fold over the entries of a given map where the key is between a lower and
325
375
-- | an upper bound. Passing `Nothing` as either the lower or upper bound
@@ -526,13 +576,13 @@ pop k = down Nil
526
576
Three _ _ _ _ k' v Leaf -> { key: k', value: v }
527
577
Three _ _ _ _ _ _ right -> maxNode right
528
578
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
536
586
537
587
deleteUp :: forall k v . Ord k => List (TreeContext k v ) -> Map k v -> Map k v
538
588
deleteUp = unsafePartial \ctxs tree ->
0 commit comments