Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

#305 with versions CPP #314

Merged
merged 2 commits into from
May 13, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
24 changes: 24 additions & 0 deletions Data/HashMap/Internal.hs
Original file line number Diff line number Diff line change
Expand Up @@ -178,6 +178,10 @@ import GHC.Stack
import qualified Data.Hashable.Lifted as H
#endif

#if MIN_VERSION_deepseq(1,4,3)
import qualified Control.DeepSeq as NF
#endif

#if __GLASGOW_HASKELL__ >= 802
import GHC.Exts (TYPE, Int (..), Int#)
#endif
Expand All @@ -201,6 +205,14 @@ data Leaf k v = L !k v
instance (NFData k, NFData v) => NFData (Leaf k v) where
rnf (L k v) = rnf k `seq` rnf v

#if MIN_VERSION_deepseq(1,4,3)
instance NFData k => NF.NFData1 (Leaf k) where
liftRnf rnf2 = NF.liftRnf2 rnf rnf2

instance NF.NFData2 Leaf where
liftRnf2 rnf1 rnf2 (L k v) = rnf1 k `seq` rnf2 v
#endif

-- Invariant: The length of the 1st argument to 'Full' is
-- 2^bitsPerSubkey

Expand All @@ -223,6 +235,18 @@ instance (NFData k, NFData v) => NFData (HashMap k v) where
rnf (Full ary) = rnf ary
rnf (Collision _ ary) = rnf ary

#if MIN_VERSION_deepseq(1,4,3)
instance NFData k => NF.NFData1 (HashMap k) where
liftRnf rnf2 = NF.liftRnf2 rnf rnf2

instance NF.NFData2 HashMap where
liftRnf2 _ _ Empty = ()
liftRnf2 rnf1 rnf2 (BitmapIndexed _ ary) = NF.liftRnf (NF.liftRnf2 rnf1 rnf2) ary
liftRnf2 rnf1 rnf2 (Leaf _ l) = NF.liftRnf2 rnf1 rnf2 l
liftRnf2 rnf1 rnf2 (Full ary) = NF.liftRnf (NF.liftRnf2 rnf1 rnf2) ary
liftRnf2 rnf1 rnf2 (Collision _ ary) = NF.liftRnf (NF.liftRnf2 rnf1 rnf2) ary
#endif

instance Functor (HashMap k) where
fmap = map

Expand Down
21 changes: 20 additions & 1 deletion Data/HashMap/Internal/Array.hs
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,7 @@ module Data.HashMap.Internal.Array
import Control.Applicative (Applicative (..), (<$>))
#endif
import Control.Applicative (liftA2)
import Control.DeepSeq
import Control.DeepSeq (NFData (..))
import GHC.Exts(Int(..), Int#, reallyUnsafePtrEquality#, tagToEnum#, unsafeCoerce#, State#)
import GHC.ST (ST(..))
import Control.Monad.ST (stToIO)
Expand Down Expand Up @@ -104,6 +104,10 @@ import Data.Monoid (Monoid (..))
import qualified Prelude
#endif

#if MIN_VERSION_deepseq(1,4,3)
import qualified Control.DeepSeq as NF
#endif

import Data.HashMap.Internal.Unsafe (runST)
import Control.Monad ((>=>))

Expand Down Expand Up @@ -250,6 +254,21 @@ rnfArray ary0 = go ary0 n0 0
-- relevant rnf is strict, or in case it actually isn't.
{-# INLINE rnfArray #-}

#if MIN_VERSION_deepseq(1,4,3)
instance NF.NFData1 Array where
liftRnf = liftRnfArray

liftRnfArray :: (a -> ()) -> Array a -> ()
liftRnfArray rnf0 ary0 = go ary0 n0 0
where
n0 = length ary0
go !ary !n !i
| i >= n = ()
| (# x #) <- index# ary i
= rnf0 x `seq` go ary n (i+1)
{-# INLINE liftRnfArray #-}
#endif

-- | Create a new mutable array of specified size, in the specified
-- state thread, with each element containing the specified initial
-- value.
Expand Down
9 changes: 9 additions & 0 deletions Data/HashSet/Internal.hs
Original file line number Diff line number Diff line change
Expand Up @@ -123,6 +123,10 @@ import Data.Functor.Classes
import qualified Data.Hashable.Lifted as H
#endif

#if MIN_VERSION_deepseq(1,4,3)
import qualified Control.DeepSeq as NF
#endif

import Data.Functor ((<$))

-- | A set of values. A set cannot contain duplicate values.
Expand All @@ -138,6 +142,11 @@ instance (NFData a) => NFData (HashSet a) where
rnf = rnf . asMap
{-# INLINE rnf #-}

#if MIN_VERSION_deepseq(1,4,3)
instance NF.NFData1 HashSet where
liftRnf rnf1 = NF.liftRnf2 rnf1 rnf . asMap
#endif

-- | Note that, in the presence of hash collisions, equal @HashSet@s may
-- behave differently, i.e. substitutivity may be violated:
--
Expand Down