papa-0.7.0: src/Papa/Containers/Data/IntMap.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 #-}
-- | Strict maps from 'Int', with total versions of the partial functions of
-- "Data.IntMap.Strict". Import qualified:
--
-- > import qualified Papa.Containers.Data.IntMap as IntMap
--
-- The functions of "Data.IntMap.Strict" 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 map from an ordered list, or map its keys
-- with a monotonic function, check the order in the same linear time, and
-- return 'Data.Maybe.Nothing' where it does not hold, where "Data.IntMap.Strict"'s
-- build a map that breaks its invariant.
module Papa.Containers.Data.IntMap (
module X,
(!),
findMin,
findMax,
deleteFindMin,
deleteFindMax,
fromAscList,
fromAscListWith,
fromAscListWithKey,
fromDistinctAscList,
fromDescList,
mapKeysMonotonic,
) where
import Control.Lens (over, _1)
import Control.Monad (guard)
import Data.Functor (fmap, (<$))
import Data.Int (Int)
import Data.IntMap.Strict as X hiding (
deleteFindMax,
deleteFindMin,
findMax,
findMin,
fromAscList,
fromAscListWith,
fromAscListWithKey,
fromDescList,
fromDistinctAscList,
mapKeysMonotonic,
(!),
)
import qualified Data.IntMap.Strict as M
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.IntMap as IntMap
-- >>> import Hedgehog
-- >>> import qualified Hedgehog.Gen as Gen
-- >>> import qualified Hedgehog.Range as Range
-- >>> let genKVs0 = Gen.list (Range.linear 0 10) ((,) <$> Gen.int (Range.linear 0 9) <*> Gen.alpha)
-- >>> let genKVs = Gen.choice [P.sortOn P.fst <$> genKVs0, P.sortOn (Down . P.fst) <$> genKVs0, P.nubBy (\a b -> P.fst a == P.fst b) . P.sortOn P.fst <$> genKVs0, genKVs0]
-- >>> let genIntMap = IntMap.fromList <$> Gen.list (Range.linear 0 20) ((,) <$> Gen.int (Range.linear (-30) 30) <*> Gen.alpha)
-- | Total 'M.!': the value at a key. 'Data.Maybe.Nothing' where the key is not in the
-- map. This is 'M.!?'.
--
-- ==== __Examples__
--
-- >>> IntMap.fromList [(1, 'a')] IntMap.! 1
-- Just 'a'
--
-- >>> IntMap.fromList [(1, 'a')] IntMap.! 2
-- Nothing
--
-- >>> check $ withTests 100 $ property $ do m <- forAll genIntMap; k <- forAll (Gen.int (Range.linear (-30) 30)); (m IntMap.! k) === IntMap.lookup k m
-- ...passed 100 tests.
-- True
--
-- @since 0.6.0
(!) :: IntMap a -> Int -> Maybe a
(!) =
(M.!?)
{-# INLINE (!) #-}
infixl 9 !
-- | Total 'M.findMin': the smallest key and its value. 'Data.Maybe.Nothing' for the
-- empty map. This is 'M.lookupMin'.
--
-- ==== __Examples__
--
-- >>> IntMap.findMin (IntMap.fromList [(2, 'b'), (-1, 'a')])
-- Just (-1,'a')
--
-- >>> IntMap.findMin (IntMap.empty :: IntMap Char)
-- Nothing
--
-- >>> check $ withTests 100 $ property $ do m <- forAll genIntMap; IntMap.findMin m === preview _head (IntMap.toAscList m)
-- ...passed 100 tests.
-- True
--
-- @since 0.6.0
findMin :: IntMap a -> Maybe (Int, a)
findMin =
M.lookupMin
{-# INLINE findMin #-}
-- | Total 'M.findMax': the largest key and its value. 'Data.Maybe.Nothing' for the
-- empty map. This is 'M.lookupMax'.
--
-- ==== __Examples__
--
-- >>> IntMap.findMax (IntMap.fromList [(2, 'b'), (-1, 'a')])
-- Just (2,'b')
--
-- >>> IntMap.findMax (IntMap.empty :: IntMap Char)
-- Nothing
--
-- >>> check $ withTests 100 $ property $ do m <- forAll genIntMap; IntMap.findMax m === preview _last (IntMap.toAscList m)
-- ...passed 100 tests.
-- True
--
-- @since 0.6.0
findMax :: IntMap a -> Maybe (Int, a)
findMax =
M.lookupMax
{-# INLINE findMax #-}
-- | Total 'M.deleteFindMin': the smallest key and its value, and the map
-- without them. 'Data.Maybe.Nothing' for the empty map. This is 'M.minViewWithKey'.
--
-- ==== __Examples__
--
-- >>> IntMap.deleteFindMin (IntMap.fromList [(2, 'b'), (1, 'a')])
-- Just ((1,'a'),fromList [(2,'b')])
--
-- >>> IntMap.deleteFindMin (IntMap.empty :: IntMap Char)
-- Nothing
--
-- >>> check $ withTests 100 $ property $ do m <- forAll genIntMap; fmap (\(kv, m') -> IntMap.insert (P.fst kv) (P.snd kv) m') (IntMap.deleteFindMin m) === (m <$ IntMap.findMin m)
-- ...passed 100 tests.
-- True
--
-- @since 0.6.0
deleteFindMin :: IntMap a -> Maybe ((Int, a), IntMap a)
deleteFindMin =
M.minViewWithKey
{-# INLINE deleteFindMin #-}
-- | Total 'M.deleteFindMax': the largest key and its value, and the map
-- without them. 'Data.Maybe.Nothing' for the empty map. This is 'M.maxViewWithKey'.
--
-- ==== __Examples__
--
-- >>> IntMap.deleteFindMax (IntMap.fromList [(2, 'b'), (1, 'a')])
-- Just ((2,'b'),fromList [(1,'a')])
--
-- >>> IntMap.deleteFindMax (IntMap.empty :: IntMap Char)
-- Nothing
--
-- >>> check $ withTests 100 $ property $ do m <- forAll genIntMap; fmap (\(kv, m') -> IntMap.insert (P.fst kv) (P.snd kv) m') (IntMap.deleteFindMax m) === (m <$ IntMap.findMax m)
-- ...passed 100 tests.
-- True
--
-- @since 0.6.0
deleteFindMax :: IntMap a -> Maybe ((Int, a), IntMap a)
deleteFindMax =
M.maxViewWithKey
{-# INLINE deleteFindMax #-}
-- | Total @fromAscList@: build a map from a list in ascending order of keys, in linear time. 'Data.Maybe.Nothing' where it is not, where "Data.IntMap.Strict"'s builds a map that breaks its invariant.
--
-- ==== __Examples__
--
-- >>> IntMap.fromAscList [(1, 'a'), (2, 'b')]
-- Just (fromList [(1,'a'),(2,'b')])
--
-- >>> IntMap.fromAscList [(2, 'b'), (1, 'a')]
-- Nothing
--
-- >>> check $ withTests 200 $ property $ do kvs <- forAll genKVs; IntMap.fromAscList kvs === bool Nothing (Just (IntMap.fromList kvs)) (P.sortOn P.fst kvs == kvs)
-- ...passed 200 tests.
-- True
--
-- @since 0.6.0
fromAscList :: [(Int, a)] -> Maybe (IntMap a)
fromAscList kvs =
M.fromAscList kvs <$ guard (pairwise (<=) (fmap fst kvs))
{-# INLINE fromAscList #-}
-- | Total @fromAscListWith@: build a map from a list in ascending order of keys, combining the values of equal keys, in linear time. 'Data.Maybe.Nothing' where it is not, where "Data.IntMap.Strict"'s builds a map that breaks its invariant.
--
-- ==== __Examples__
--
-- >>> IntMap.fromAscListWith (<>) [(1, "a"), (1, "b"), (2, "c")]
-- Just (fromList [(1,"ba"),(2,"c")])
--
-- >>> IntMap.fromAscListWith (<>) [(2, "a"), (1, "b")]
-- Nothing
--
-- >>> check $ withTests 200 $ property $ do kvs <- forAll genKVs; let { kss = fmap (fmap (: [])) kvs }; IntMap.fromAscListWith (<>) kss === bool Nothing (Just (IntMap.fromListWith (<>) kss)) (P.sortOn P.fst kvs == kvs)
-- ...passed 200 tests.
-- True
--
-- @since 0.6.0
fromAscListWith :: (a -> a -> a) -> [(Int, a)] -> Maybe (IntMap a)
fromAscListWith f kvs =
M.fromAscListWith f kvs <$ guard (pairwise (<=) (fmap fst kvs))
{-# INLINE fromAscListWith #-}
-- | Total @fromAscListWithKey@: build a map from a list in ascending order of keys, combining the values of equal keys with the key, in linear time. 'Data.Maybe.Nothing' where it is not, where "Data.IntMap.Strict"'s builds a map that breaks its invariant.
--
-- ==== __Examples__
--
-- >>> IntMap.fromAscListWithKey (\k a b -> show k <> a <> b) [(1, "a"), (1, "b")]
-- Just (fromList [(1,"1ba")])
--
-- >>> IntMap.fromAscListWithKey (\_ a _ -> a) [(2, "a"), (1, "b")]
-- Nothing
--
-- >>> check $ withTests 200 $ property $ do kvs <- forAll genKVs; let { kss = fmap (fmap (: [])) kvs; f k a b = show k <> a <> b }; IntMap.fromAscListWithKey f kss === bool Nothing (Just (IntMap.fromListWithKey f kss)) (P.sortOn P.fst kvs == kvs)
-- ...passed 200 tests.
-- True
--
-- @since 0.6.0
fromAscListWithKey :: (Int -> a -> a -> a) -> [(Int, a)] -> Maybe (IntMap a)
fromAscListWithKey f kvs =
M.fromAscListWithKey f kvs <$ guard (pairwise (<=) (fmap fst kvs))
{-# INLINE fromAscListWithKey #-}
-- | Total @fromDistinctAscList@: build a map from a list in strictly ascending order of keys, in linear time. 'Data.Maybe.Nothing' where it is not, where "Data.IntMap.Strict"'s builds a map that breaks its invariant.
--
-- ==== __Examples__
--
-- >>> IntMap.fromDistinctAscList [(1, 'a'), (2, 'b')]
-- Just (fromList [(1,'a'),(2,'b')])
--
-- >>> IntMap.fromDistinctAscList [(1, 'a'), (1, 'b')]
-- Nothing
--
-- >>> check $ withTests 200 $ property $ do kvs <- forAll genKVs; IntMap.fromDistinctAscList kvs === bool Nothing (Just (IntMap.fromList kvs)) (P.sortOn P.fst kvs == kvs && P.nub (fmap P.fst kvs) == fmap P.fst kvs)
-- ...passed 200 tests.
-- True
--
-- @since 0.6.0
fromDistinctAscList :: [(Int, a)] -> Maybe (IntMap a)
fromDistinctAscList kvs =
M.fromDistinctAscList kvs <$ guard (pairwise (<) (fmap fst kvs))
{-# INLINE fromDistinctAscList #-}
-- | Total @fromDescList@: build a map from a list in descending order of keys, in linear time. 'Data.Maybe.Nothing' where it is not, where "Data.IntMap.Strict"'s builds a map that breaks its invariant.
--
-- ==== __Examples__
--
-- >>> IntMap.fromDescList [(2, 'b'), (1, 'a')]
-- Just (fromList [(1,'a'),(2,'b')])
--
-- >>> IntMap.fromDescList [(1, 'a'), (2, 'b')]
-- Nothing
--
-- >>> check $ withTests 200 $ property $ do kvs <- forAll genKVs; IntMap.fromDescList kvs === bool Nothing (Just (IntMap.fromList kvs)) (P.sortOn (Down . P.fst) kvs == kvs)
-- ...passed 200 tests.
-- True
--
-- @since 0.6.0
fromDescList :: [(Int, a)] -> Maybe (IntMap a)
fromDescList kvs =
M.fromAscListWith (\_ old -> old) (reverse kvs) <$ guard (pairwise (>=) (fmap fst kvs))
{-# INLINE fromDescList #-}
-- | Total @mapKeysMonotonic@: map the keys with a strictly increasing function, in linear time. 'Data.Maybe.Nothing' where the function is not strictly increasing on the keys of the map, where "Data.IntMap.Strict"'s builds a map that breaks its invariant.
--
-- ==== __Examples__
--
-- >>> IntMap.mapKeysMonotonic (* 2) (IntMap.fromList [(1, 'a'), (2, 'b')])
-- Just (fromList [(2,'a'),(4,'b')])
--
-- >>> IntMap.mapKeysMonotonic negate (IntMap.fromList [(1, 'a'), (2, 'b')])
-- Nothing
--
-- >>> check $ withTests 200 $ property $ do m <- forAll (fmap IntMap.fromList genKVs); c <- forAll (Gen.int (Range.linear (-2) 2)); IntMap.mapKeysMonotonic (* c) m === bool Nothing (Just (IntMap.mapKeys (* c) m)) (c > 0 || IntMap.size m <= 1)
-- ...passed 200 tests.
-- True
--
-- @since 0.6.0
mapKeysMonotonic :: (Int -> Int) -> IntMap a -> Maybe (IntMap a)
mapKeysMonotonic f m =
let kvs = fmap (over _1 f) (M.toAscList m)
in M.fromDistinctAscList kvs <$ guard (pairwise (<) (fmap fst kvs))
{-# INLINE mapKeysMonotonic #-}