papa-0.7.0: src/Papa/Containers/Data/Set.hs
{-# LANGUAGE NoImplicitPrelude #-}
{-# OPTIONS_GHC -Wall #-}
-- | Sets, with total versions of the partial functions of "Data.Set".
-- Import qualified:
--
-- > import qualified Papa.Containers.Data.Set as Set
--
-- The functions of "Data.Set" that throw (@findMin@, @findMax@,
-- @deleteFindMin@, @deleteFindMax@, @elemAt@, @findIndex@, @deleteAt@) are
-- replaced by functions of the same names that return 'Data.Maybe.Nothing' there
-- instead.
--
-- The functions that build a set from an ordered list, or map it with a
-- monotonic function, check the order in the same linear time, and return
-- 'Data.Maybe.Nothing' where it does not hold, where "Data.Set"'s build a set that
-- breaks its invariant.
module Papa.Containers.Data.Set (
module X,
findMin,
findMax,
deleteFindMin,
deleteFindMax,
elemAt,
findIndex,
deleteAt,
fromAscList,
fromDescList,
fromDistinctAscList,
fromDistinctDescList,
mapMonotonic,
) where
import Control.Monad (guard)
import Data.Bool (Bool, (&&))
import Data.Functor ((<$))
import Data.Int (Int)
import Data.Maybe (Maybe)
import Data.Ord (Ord ((<), (<=), (>), (>=)))
import Data.Set as X hiding (
deleteAt,
deleteFindMax,
deleteFindMin,
elemAt,
findIndex,
findMax,
findMin,
fromAscList,
fromDescList,
fromDistinctAscList,
fromDistinctDescList,
mapMonotonic,
)
import qualified Data.Set as S
import Papa.Internal.Order (pairwise)
-- $setup
-- >>> :set -XNoImplicitPrelude
-- >>> import Papa
-- >>> import qualified Papa as P
-- >>> import qualified Papa.Containers.Data.Set as Set
-- >>> import Hedgehog
-- >>> import qualified Hedgehog.Gen as Gen
-- >>> import qualified Hedgehog.Range as Range
-- >>> let genXs0 = Gen.list (Range.linear 0 10) (Gen.int (Range.linear 0 9))
-- >>> let genXs = Gen.choice [P.sort <$> genXs0, P.sortOn Down <$> genXs0, P.nub . P.sort <$> genXs0, genXs0]
-- >>> let genSet = Set.fromList <$> Gen.list (Range.linear 0 20) (Gen.int (Range.linear 0 30))
-- >>> let genIndex = Gen.int (Range.linear (-5) 25)
-- | Whether an index is a position in a set.
inRange :: Int -> Set a -> Bool
inRange i s =
0 <= i && i < S.size s
{-# INLINE inRange #-}
-- | Total 'S.findMin': the smallest element. 'Data.Maybe.Nothing' for the empty set.
-- This is 'S.lookupMin'.
--
-- ==== __Examples__
--
-- >>> Set.findMin (Set.fromList [2, 1 :: Int])
-- Just 1
--
-- >>> Set.findMin (Set.empty :: Set Int)
-- Nothing
--
-- >>> check $ withTests 100 $ property $ do s <- forAll genSet; Set.findMin s === preview _head (Set.toAscList s)
-- ...passed 100 tests.
-- True
--
-- @since 0.6.0
findMin :: Set a -> Maybe a
findMin =
S.lookupMin
{-# INLINE findMin #-}
-- | Total 'S.findMax': the largest element. 'Data.Maybe.Nothing' for the empty set.
-- This is 'S.lookupMax'.
--
-- ==== __Examples__
--
-- >>> Set.findMax (Set.fromList [2, 1 :: Int])
-- Just 2
--
-- >>> Set.findMax (Set.empty :: Set Int)
-- Nothing
--
-- >>> check $ withTests 100 $ property $ do s <- forAll genSet; Set.findMax s === preview _last (Set.toAscList s)
-- ...passed 100 tests.
-- True
--
-- @since 0.6.0
findMax :: Set a -> Maybe a
findMax =
S.lookupMax
{-# INLINE findMax #-}
-- | Total 'S.deleteFindMin': the smallest element, and the set without it.
-- 'Data.Maybe.Nothing' for the empty set. This is 'S.minView'.
--
-- ==== __Examples__
--
-- >>> Set.deleteFindMin (Set.fromList [2, 1 :: Int])
-- Just (1,fromList [2])
--
-- >>> Set.deleteFindMin (Set.empty :: Set Int)
-- Nothing
--
-- >>> check $ withTests 100 $ property $ do s <- forAll genSet; fmap (\(a, s') -> Set.insert a s') (Set.deleteFindMin s) === (s <$ Set.findMin s)
-- ...passed 100 tests.
-- True
--
-- @since 0.6.0
deleteFindMin :: Set a -> Maybe (a, Set a)
deleteFindMin =
S.minView
{-# INLINE deleteFindMin #-}
-- | Total 'S.deleteFindMax': the largest element, and the set without it.
-- 'Data.Maybe.Nothing' for the empty set. This is 'S.maxView'.
--
-- ==== __Examples__
--
-- >>> Set.deleteFindMax (Set.fromList [2, 1 :: Int])
-- Just (2,fromList [1])
--
-- >>> Set.deleteFindMax (Set.empty :: Set Int)
-- Nothing
--
-- >>> check $ withTests 100 $ property $ do s <- forAll genSet; fmap (\(a, s') -> Set.insert a s') (Set.deleteFindMax s) === (s <$ Set.findMax s)
-- ...passed 100 tests.
-- True
--
-- @since 0.6.0
deleteFindMax :: Set a -> Maybe (a, Set a)
deleteFindMax =
S.maxView
{-# INLINE deleteFindMax #-}
-- | Total 'S.elemAt': the element at an index in ascending order.
-- 'Data.Maybe.Nothing' for an index out of range.
--
-- ==== __Examples__
--
-- >>> Set.elemAt 1 (Set.fromList [2, 1 :: Int])
-- Just 2
--
-- >>> Set.elemAt 2 (Set.fromList [2, 1 :: Int])
-- Nothing
--
-- >>> check $ withTests 100 $ property $ do s <- forAll genSet; i <- forAll genIndex; Set.elemAt i s === (Set.toAscList s !? i)
-- ...passed 100 tests.
-- True
--
-- @since 0.6.0
elemAt :: Int -> Set a -> Maybe a
elemAt i s =
S.elemAt i s <$ guard (inRange i s)
{-# INLINE elemAt #-}
-- | Total 'S.findIndex': the index of an element in ascending order.
-- 'Data.Maybe.Nothing' where it is not in the set. This is 'S.lookupIndex'.
--
-- ==== __Examples__
--
-- >>> Set.findIndex 2 (Set.fromList [2, 1 :: Int])
-- Just 1
--
-- >>> Set.findIndex 3 (Set.fromList [2, 1 :: Int])
-- Nothing
--
-- >>> check $ withTests 100 $ property $ do s <- forAll genSet; a <- forAll (Gen.int (Range.linear 0 30)); (Set.findIndex a s >>= \i -> Set.elemAt i s) === bool Nothing (Just a) (Set.member a s)
-- ...passed 100 tests.
-- True
--
-- @since 0.6.0
findIndex :: (Ord a) => a -> Set a -> Maybe Int
findIndex =
S.lookupIndex
{-# INLINE findIndex #-}
-- | Total 'S.deleteAt': the set without the element at an index in
-- ascending order. 'Data.Maybe.Nothing' for an index out of range.
--
-- ==== __Examples__
--
-- >>> Set.deleteAt 0 (Set.fromList [2, 1 :: Int])
-- Just (fromList [2])
--
-- >>> Set.deleteAt 2 (Set.fromList [2, 1 :: Int])
-- Nothing
--
-- >>> check $ withTests 100 $ property $ do s <- forAll genSet; i <- forAll genIndex; Set.deleteAt i s === fmap (\a -> Set.delete a s) (Set.elemAt i s)
-- ...passed 100 tests.
-- True
--
-- @since 0.6.0
deleteAt :: Int -> Set a -> Maybe (Set a)
deleteAt i s =
S.deleteAt i s <$ guard (inRange i s)
{-# INLINE deleteAt #-}
-- | Total @fromAscList@: build a set from a list in ascending order, in linear time. 'Data.Maybe.Nothing' where it is not, where "Data.Set"'s builds a set that breaks its invariant.
--
-- ==== __Examples__
--
-- >>> Set.fromAscList [1, 2, 2, 3]
-- Just (fromList [1,2,3])
--
-- >>> Set.fromAscList [2, 1]
-- Nothing
--
-- >>> check $ withTests 200 $ property $ do xs <- forAll genXs; Set.fromAscList xs === bool Nothing (Just (Set.fromList xs)) (P.sort xs == xs)
-- ...passed 200 tests.
-- True
--
-- @since 0.6.0
fromAscList :: (Ord a) => [a] -> Maybe (Set a)
fromAscList xs =
S.fromAscList xs <$ guard (pairwise (<=) xs)
{-# INLINE fromAscList #-}
-- | Total @fromDescList@: build a set from a list in descending order, in linear time. 'Data.Maybe.Nothing' where it is not, where "Data.Set"'s builds a set that breaks its invariant.
--
-- ==== __Examples__
--
-- >>> Set.fromDescList [3, 2, 2, 1]
-- Just (fromList [1,2,3])
--
-- >>> Set.fromDescList [1, 2]
-- Nothing
--
-- >>> check $ withTests 200 $ property $ do xs <- forAll genXs; Set.fromDescList xs === bool Nothing (Just (Set.fromList xs)) (P.sortOn Down xs == xs)
-- ...passed 200 tests.
-- True
--
-- @since 0.6.0
fromDescList :: (Ord a) => [a] -> Maybe (Set a)
fromDescList xs =
S.fromDescList xs <$ guard (pairwise (>=) xs)
{-# INLINE fromDescList #-}
-- | Total @fromDistinctAscList@: build a set from a list in strictly ascending order, in linear time. 'Data.Maybe.Nothing' where it is not, where "Data.Set"'s builds a set that breaks its invariant.
--
-- ==== __Examples__
--
-- >>> Set.fromDistinctAscList [1, 2, 3]
-- Just (fromList [1,2,3])
--
-- >>> Set.fromDistinctAscList [1, 1]
-- Nothing
--
-- >>> check $ withTests 200 $ property $ do xs <- forAll genXs; Set.fromDistinctAscList xs === bool Nothing (Just (Set.fromList xs)) (P.sort xs == xs && P.nub xs == xs)
-- ...passed 200 tests.
-- True
--
-- @since 0.6.0
fromDistinctAscList :: (Ord a) => [a] -> Maybe (Set a)
fromDistinctAscList xs =
S.fromDistinctAscList xs <$ guard (pairwise (<) xs)
{-# INLINE fromDistinctAscList #-}
-- | Total @fromDistinctDescList@: build a set from a list in strictly descending order, in linear time. 'Data.Maybe.Nothing' where it is not, where "Data.Set"'s builds a set that breaks its invariant.
--
-- ==== __Examples__
--
-- >>> Set.fromDistinctDescList [3, 2, 1]
-- Just (fromList [1,2,3])
--
-- >>> Set.fromDistinctDescList [1, 1]
-- Nothing
--
-- >>> check $ withTests 200 $ property $ do xs <- forAll genXs; Set.fromDistinctDescList xs === bool Nothing (Just (Set.fromList xs)) (P.sortOn Down xs == xs && P.nub xs == xs)
-- ...passed 200 tests.
-- True
--
-- @since 0.6.0
fromDistinctDescList :: (Ord a) => [a] -> Maybe (Set a)
fromDistinctDescList xs =
S.fromDistinctDescList xs <$ guard (pairwise (>) xs)
{-# INLINE fromDistinctDescList #-}
-- | Total @mapMonotonic@: map the elements with a strictly increasing function, in linear time. 'Data.Maybe.Nothing' where the function is not strictly increasing on the elements of the set, where "Data.Set"'s builds a set that breaks its invariant.
--
-- ==== __Examples__
--
-- >>> Set.mapMonotonic (* 2) (Set.fromList [1, 2])
-- Just (fromList [2,4])
--
-- >>> Set.mapMonotonic negate (Set.fromList [1, 2])
-- Nothing
--
-- >>> check $ withTests 200 $ property $ do s <- forAll (fmap Set.fromList genXs); c <- forAll (Gen.int (Range.linear (-2) 2)); Set.mapMonotonic (* c) s === bool Nothing (Just (Set.map (* c) s)) (c > 0 || Set.size s <= 1)
-- ...passed 200 tests.
-- True
--
-- @since 0.6.0
mapMonotonic :: (Ord b) => (a -> b) -> Set a -> Maybe (Set b)
mapMonotonic f s =
let r = S.mapMonotonic f s
in r <$ guard (pairwise (<) (S.toAscList r))
{-# INLINE mapMonotonic #-}