Add minView and maxView to Map and Set
authorjeanphilippe.bernardy@gmail.com <unknown>
Fri, 16 Jun 2006 18:01:21 +0000 (18:01 +0000)
committerjeanphilippe.bernardy@gmail.com <unknown>
Fri, 16 Jun 2006 18:01:21 +0000 (18:01 +0000)
Data/Map.hs
Data/Set.hs

index 54730f8..269fe5e 100644 (file)
@@ -148,6 +148,8 @@ module Data.Map  (
             , updateMax
             , updateMinWithKey
             , updateMaxWithKey
+            , minView
+            , maxView
             
             -- * Debugging
             , showTree
@@ -511,13 +513,13 @@ deleteAt i map
 findMin :: Map k a -> (k,a)
 findMin (Bin _ kx x Tip r)  = (kx,x)
 findMin (Bin _ kx x l r)    = findMin l
-findMin Tip                 = error "Map.findMin: empty tree has no minimal element"
+findMin Tip                 = error "Map.findMin: empty map has no minimal element"
 
 -- | /O(log n)/. The maximal key of the map.
 findMax :: Map k a -> (k,a)
 findMax (Bin _ kx x l Tip)  = (kx,x)
 findMax (Bin _ kx x l r)    = findMax r
-findMax Tip                 = error "Map.findMax: empty tree has no maximal element"
+findMax Tip                 = error "Map.findMax: empty map has no maximal element"
 
 -- | /O(log n)/. Delete the minimal key.
 deleteMin :: Map k a -> Map k a
@@ -562,6 +564,19 @@ updateMaxWithKey f t
       Bin sx kx x l r    -> balance kx x l (updateMaxWithKey f r)
       Tip                -> Tip
 
+-- | /O(log n)/. Retrieves the minimal key of the map, and the map stripped from that element
+-- @fail@s (in the monad) when passed an empty map.
+minView :: Monad m => Map k a -> m (Map k a, (k,a))
+minView Tip = fail "Map.minView: empty map"
+minView x = return (swap $ deleteFindMin x)
+
+-- | /O(log n)/. Retrieves the maximal key of the map, and the map stripped from that element
+-- @fail@s (in the monad) when passed an empty map.
+maxView :: Monad m => Map k a -> m (Map k a, (k,a))
+maxView Tip = fail "Map.maxView: empty map"
+maxView x = return (swap $ deleteFindMax x)
+
+swap (a,b) = (b,a)
 
 {--------------------------------------------------------------------
   Union. 
index 36c7ecd..0e3a5b8 100644 (file)
@@ -78,6 +78,8 @@ module Data.Set  (
             , deleteMax
             , deleteFindMin
             , deleteFindMax
+            , maxView
+            , minView
 
             -- * Conversion
 
@@ -756,6 +758,21 @@ deleteFindMax t
       Bin _ x l r   -> let (xm,r') = deleteFindMax r in (xm,balance x l r')
       Tip           -> (error "Set.deleteFindMax: can not return the maximal element of an empty set", Tip)
 
+-- | /O(log n)/. Retrieves the minimal key of the set, and the set stripped from that element
+-- @fail@s (in the monad) when passed an empty set.
+minView :: Monad m => Set a -> m (Set a, a)
+minView Tip = fail "Set.minView: empty set"
+minView x = return (swap $ deleteFindMin x)
+
+-- | /O(log n)/. Retrieves the maximal key of the set, and the set stripped from that element
+-- @fail@s (in the monad) when passed an empty set.
+maxView :: Monad m => Set a -> m (Set a, a)
+maxView Tip = fail "Set.maxView: empty set"
+maxView x = return (swap $ deleteFindMax x)
+
+swap (a,b) = (b,a)
+
+
 
 {--------------------------------------------------------------------
   [balance x l r] balances two trees with value x.