From: jeanphilippe.bernardy@gmail.com Date: Fri, 16 Jun 2006 18:01:21 +0000 (+0000) Subject: Add minView and maxView to Map and Set X-Git-Url: http://git.megacz.com/?a=commitdiff_plain;h=e61e457666dd094b177edcb8c929264359ca2982;hp=a058649879dc906e4e9c810ab5a0be4b2b878b14;p=haskell-directory.git Add minView and maxView to Map and Set --- diff --git a/Data/Map.hs b/Data/Map.hs index 54730f8..269fe5e 100644 --- a/Data/Map.hs +++ b/Data/Map.hs @@ -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. diff --git a/Data/Set.hs b/Data/Set.hs index 36c7ecd..0e3a5b8 100644 --- a/Data/Set.hs +++ b/Data/Set.hs @@ -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.