papa-0.7.0: src/Papa/Containers/Data/IntSet.hs
{-# LANGUAGE NoImplicitPrelude #-}
{-# OPTIONS_GHC -Wall #-}
-- Reason: fromDescList is hidden from the library module, which exports it
-- from containers 0.8, and not before; hiding it where it is not exported
-- is a dodgy import, and the alternative is CPP in the import list.
{-# OPTIONS_GHC -Wno-dodgy-imports #-}
-- | Sets of 'Int', with total versions of the partial functions of
-- "Data.IntSet". Import qualified:
--
-- > import qualified Papa.Containers.Data.IntSet as IntSet
--
-- The functions of "Data.IntSet" that throw (@findMin@, @findMax@,
-- @deleteFindMin@, @deleteFindMax@) 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.IntSet"'s
-- build a set that breaks its invariant.
module Papa.Containers.Data.IntSet (
module X,
findMin,
findMax,
deleteFindMin,
deleteFindMax,
fromAscList,
fromDescList,
fromDistinctAscList,
mapMonotonic,
) where
import Control.Category ((.))
import Control.Monad (guard)
import Data.Functor (fmap, (<$))
import Data.Int (Int)
import Data.IntSet as X hiding (
deleteFindMax,
deleteFindMin,
findMax,
findMin,
fromAscList,
fromDescList,
fromDistinctAscList,
mapMonotonic,
)
import qualified Data.IntSet as S
import Data.List (reverse)
import Data.Maybe (Maybe)
import Data.Ord (Ord ((<), (<=), (>=)))
import Data.Tuple (fst)
import Papa.Internal.Order (pairwise)
-- $setup
-- >>> :set -XNoImplicitPrelude
-- >>> import Papa
-- >>> import qualified Papa as P
-- >>> import qualified Papa.Containers.Data.IntSet as IntSet
-- >>> 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 genIntSet = IntSet.fromList <$> Gen.list (Range.linear 0 20) (Gen.int (Range.linear (-30) 30))
-- | Total 'S.findMin': the smallest element. 'Data.Maybe.Nothing' for the empty set.
--
-- ==== __Examples__
--
-- >>> IntSet.findMin (IntSet.fromList [2, -1])
-- Just (-1)
--
-- >>> IntSet.findMin IntSet.empty
-- Nothing
--
-- >>> check $ withTests 100 $ property $ do s <- forAll genIntSet; IntSet.findMin s === preview _head (IntSet.toAscList s)
-- ...passed 100 tests.
-- True
--
-- @since 0.6.0
findMin :: IntSet -> Maybe Int
findMin =
fmap fst . S.minView
{-# INLINE findMin #-}
-- | Total 'S.findMax': the largest element. 'Data.Maybe.Nothing' for the empty set.
--
-- ==== __Examples__
--
-- >>> IntSet.findMax (IntSet.fromList [2, -1])
-- Just 2
--
-- >>> IntSet.findMax IntSet.empty
-- Nothing
--
-- >>> check $ withTests 100 $ property $ do s <- forAll genIntSet; IntSet.findMax s === preview _head (IntSet.toDescList s)
-- ...passed 100 tests.
-- True
--
-- @since 0.6.0
findMax :: IntSet -> Maybe Int
findMax =
fmap fst . S.maxView
{-# 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__
--
-- >>> IntSet.deleteFindMin (IntSet.fromList [2, 1])
-- Just (1,fromList [2])
--
-- >>> IntSet.deleteFindMin IntSet.empty
-- Nothing
--
-- >>> check $ withTests 100 $ property $ do s <- forAll genIntSet; fmap (\(a, s') -> IntSet.insert a s') (IntSet.deleteFindMin s) === (s <$ IntSet.findMin s)
-- ...passed 100 tests.
-- True
--
-- @since 0.6.0
deleteFindMin :: IntSet -> Maybe (Int, IntSet)
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__
--
-- >>> IntSet.deleteFindMax (IntSet.fromList [2, 1])
-- Just (2,fromList [1])
--
-- >>> IntSet.deleteFindMax IntSet.empty
-- Nothing
--
-- >>> check $ withTests 100 $ property $ do s <- forAll genIntSet; fmap (\(a, s') -> IntSet.insert a s') (IntSet.deleteFindMax s) === (s <$ IntSet.findMax s)
-- ...passed 100 tests.
-- True
--
-- @since 0.6.0
deleteFindMax :: IntSet -> Maybe (Int, IntSet)
deleteFindMax =
S.maxView
{-# INLINE deleteFindMax #-}
-- | Total @fromAscList@: build a set from a list in ascending order, in linear time. 'Data.Maybe.Nothing' where it is not, where "Data.IntSet"'s builds a set that breaks its invariant.
--
-- ==== __Examples__
--
-- >>> IntSet.fromAscList [1, 2, 2, 3]
-- Just (fromList [1,2,3])
--
-- >>> IntSet.fromAscList [2, 1]
-- Nothing
--
-- >>> check $ withTests 200 $ property $ do xs <- forAll genXs; IntSet.fromAscList xs === bool Nothing (Just (IntSet.fromList xs)) (P.sort xs == xs)
-- ...passed 200 tests.
-- True
--
-- @since 0.6.0
fromAscList :: [Int] -> Maybe (IntSet)
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.IntSet"'s builds a set that breaks its invariant.
--
-- ==== __Examples__
--
-- >>> IntSet.fromDescList [3, 2, 2, 1]
-- Just (fromList [1,2,3])
--
-- >>> IntSet.fromDescList [1, 2]
-- Nothing
--
-- >>> check $ withTests 200 $ property $ do xs <- forAll genXs; IntSet.fromDescList xs === bool Nothing (Just (IntSet.fromList xs)) (P.sortOn Down xs == xs)
-- ...passed 200 tests.
-- True
--
-- @since 0.6.0
fromDescList :: [Int] -> Maybe (IntSet)
fromDescList xs =
S.fromAscList (reverse 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.IntSet"'s builds a set that breaks its invariant.
--
-- ==== __Examples__
--
-- >>> IntSet.fromDistinctAscList [1, 2, 3]
-- Just (fromList [1,2,3])
--
-- >>> IntSet.fromDistinctAscList [1, 1]
-- Nothing
--
-- >>> check $ withTests 200 $ property $ do xs <- forAll genXs; IntSet.fromDistinctAscList xs === bool Nothing (Just (IntSet.fromList xs)) (P.sort xs == xs && P.nub xs == xs)
-- ...passed 200 tests.
-- True
--
-- @since 0.6.0
fromDistinctAscList :: [Int] -> Maybe (IntSet)
fromDistinctAscList xs =
S.fromDistinctAscList xs <$ guard (pairwise (<) xs)
{-# INLINE fromDistinctAscList #-}
-- | 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.IntSet"'s builds a set that breaks its invariant.
--
-- ==== __Examples__
--
-- >>> IntSet.mapMonotonic (* 2) (IntSet.fromList [1, 2])
-- Just (fromList [2,4])
--
-- >>> IntSet.mapMonotonic negate (IntSet.fromList [1, 2])
-- Nothing
--
-- >>> check $ withTests 200 $ property $ do s <- forAll (fmap IntSet.fromList genXs); c <- forAll (Gen.int (Range.linear (-2) 2)); IntSet.mapMonotonic (* c) s === bool Nothing (Just (IntSet.map (* c) s)) (c > 0 || IntSet.size s <= 1)
-- ...passed 200 tests.
-- True
--
-- @since 0.6.0
mapMonotonic :: (Int -> Int) -> IntSet -> Maybe IntSet
mapMonotonic f s =
let xs = fmap f (S.toAscList s)
in S.fromDistinctAscList xs <$ guard (pairwise (<) xs)
{-# INLINE mapMonotonic #-}