module Manatee.Toolkit.General.Set where
import Control.Monad hiding (filterM, mapM)
import Data.Set
import qualified Control.Monad as CM
import qualified Data.Set as Set
maybeFindMin :: Ord a => Set a -> (a -> Bool) -> Maybe a
maybeFindMin s f
| Set.null set
= Nothing
| otherwise
= Just $ findMin set
where
set = Set.filter f s
maybeFindMinM :: (Ord a, Monad m) => Set a -> (a -> m Bool) -> m (Maybe a)
maybeFindMinM s f = do
set <- filterM f s
return $
if Set.null set
then Nothing
else Just $ findMin set
filterM :: (Ord a, Monad m) => (a -> m Bool) -> Set a -> m (Set a)
filterM f xs = fromList `liftM` CM.filterM f (toList xs)
mapM :: (Ord a, Ord b, Monad m) => (a -> m b) -> Set a -> m (Set b)
mapM f sa = fromList `liftM` CM.mapM f (toList sa)
nextElement :: (Ord a) => Set a -> a -> Maybe a
nextElement set a
| hasA
= if Set.null nextSet
then Nothing
else Just $ findMin nextSet
| otherwise
= Nothing
where (_, hasA, nextSet) = splitMember a set
prevElement :: (Ord a) => Set a -> a -> Maybe a
prevElement set a
| hasA
= if Set.null prevSet
then Nothing
else Just $ findMax prevSet
| otherwise
= Nothing
where (prevSet, hasA, _) = splitMember a set
tryFindMin :: (Ord a) => Set a -> Maybe a
tryFindMin set
| Set.null set = Nothing
| otherwise = Just $ findMin set
tryFindMax :: (Ord a) => Set a -> Maybe a
tryFindMax set
| Set.null set = Nothing
| otherwise = Just $ findMax set