X-Git-Url: http://git.megacz.com/?a=blobdiff_plain;f=Data%2FSet.hs;h=5f45ce9079a9359a9279e7e09e13aa3429d52500;hb=afe7ed8026edd943550b05f4895c99601207fea5;hp=36c7ecd0791bc3b8e273058a8b5889ea54168106;hpb=6ff8c23c29a6936f7e2f85663ccf5f1709dee181;p=haskell-directory.git diff --git a/Data/Set.hs b/Data/Set.hs index 36c7ecd..5f45ce9 100644 --- a/Data/Set.hs +++ b/Data/Set.hs @@ -9,10 +9,11 @@ -- -- An efficient implementation of sets. -- --- This module is intended to be imported @qualified@, to avoid name --- clashes with "Prelude" functions. eg. +-- Since many function names (but not the type name) clash with +-- "Prelude" names, this module is usually imported @qualified@, e.g. -- --- > import Data.Set as Set +-- > import Data.Set (Set) +-- > import qualified Data.Set as Set -- -- The implementation of 'Set' is based on /size balanced/ binary trees (or -- trees of /bounded balance/) as described by: @@ -78,6 +79,8 @@ module Data.Set ( , deleteMax , deleteFindMin , deleteFindMax + , maxView + , minView -- * Conversion @@ -95,21 +98,6 @@ module Data.Set ( , showTree , showTreeWith , valid - - -- * Old interface, DEPRECATED - ,emptySet, -- :: Set a - mkSet, -- :: Ord a => [a] -> Set a - setToList, -- :: Set a -> [a] - unitSet, -- :: a -> Set a - elementOf, -- :: Ord a => a -> Set a -> Bool - isEmptySet, -- :: Set a -> Bool - cardinality, -- :: Set a -> Int - unionManySets, -- :: Ord a => [Set a] -> Set a - minusSet, -- :: Ord a => Set a -> Set a -> Set a - mapSet, -- :: Ord a => (b -> a) -> Set b -> Set a - intersect, -- :: Ord a => Set a -> Set a -> Set a - addToSet, -- :: Ord a => Set a -> a -> Set a - delFromSet, -- :: Ord a => Set a -> a -> Set a ) where import Prelude hiding (filter,foldr,null,map) @@ -756,6 +744,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. @@ -1138,72 +1141,3 @@ prop_List :: [Int] -> Bool prop_List xs = (sort (nub xs) == toList (fromList xs)) -} - -{-------------------------------------------------------------------- - Old Data.Set compatibility interface ---------------------------------------------------------------------} - -{-# DEPRECATED emptySet "Use empty instead" #-} --- | Obsolete equivalent of 'empty'. -emptySet :: Set a -emptySet = empty - -{-# DEPRECATED mkSet "Use fromList instead" #-} --- | Obsolete equivalent of 'fromList'. -mkSet :: Ord a => [a] -> Set a -mkSet = fromList - -{-# DEPRECATED setToList "Use elems instead." #-} --- | Obsolete equivalent of 'elems'. -setToList :: Set a -> [a] -setToList = elems - -{-# DEPRECATED unitSet "Use singleton instead." #-} --- | Obsolete equivalent of 'singleton'. -unitSet :: a -> Set a -unitSet = singleton - -{-# DEPRECATED elementOf "Use member instead." #-} --- | Obsolete equivalent of 'member'. -elementOf :: Ord a => a -> Set a -> Bool -elementOf = member - -{-# DEPRECATED isEmptySet "Use null instead." #-} --- | Obsolete equivalent of 'null'. -isEmptySet :: Set a -> Bool -isEmptySet = null - -{-# DEPRECATED cardinality "Use size instead." #-} --- | Obsolete equivalent of 'size'. -cardinality :: Set a -> Int -cardinality = size - -{-# DEPRECATED unionManySets "Use unions instead." #-} --- | Obsolete equivalent of 'unions'. -unionManySets :: Ord a => [Set a] -> Set a -unionManySets = unions - -{-# DEPRECATED minusSet "Use difference instead." #-} --- | Obsolete equivalent of 'difference'. -minusSet :: Ord a => Set a -> Set a -> Set a -minusSet = difference - -{-# DEPRECATED mapSet "Use map instead." #-} --- | Obsolete equivalent of 'map'. -mapSet :: (Ord a, Ord b) => (b -> a) -> Set b -> Set a -mapSet = map - -{-# DEPRECATED intersect "Use intersection instead." #-} --- | Obsolete equivalent of 'intersection'. -intersect :: Ord a => Set a -> Set a -> Set a -intersect = intersection - -{-# DEPRECATED addToSet "Use 'flip insert' instead." #-} --- | Obsolete equivalent of @'flip' 'insert'@. -addToSet :: Ord a => Set a -> a -> Set a -addToSet = flip insert - -{-# DEPRECATED delFromSet "Use `flip delete' instead." #-} --- | Obsolete equivalent of @'flip' 'delete'@. -delFromSet :: Ord a => Set a -> a -> Set a -delFromSet = flip delete