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

Generic Instances for HashMap and HashSet #54

Closed
wants to merge 3 commits into from
Closed
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
9 changes: 9 additions & 0 deletions Data/HashMap/Base.hs
Original file line number Original file line Diff line number Diff line change
@@ -1,6 +1,10 @@
{-# LANGUAGE BangPatterns, CPP, DeriveDataTypeable, MagicHash #-} {-# LANGUAGE BangPatterns, CPP, DeriveDataTypeable, MagicHash #-}
{-# OPTIONS_GHC -fno-full-laziness -funbox-strict-fields #-} {-# OPTIONS_GHC -fno-full-laziness -funbox-strict-fields #-}


#if defined(__GLASGOW_HASKELL__)
{-# LANGUAGE DeriveGeneric #-}
#endif

module Data.HashMap.Base module Data.HashMap.Base
( (
HashMap(..) HashMap(..)
Expand Down Expand Up @@ -97,6 +101,7 @@ import Data.Typeable (Typeable)
#if defined(__GLASGOW_HASKELL__) #if defined(__GLASGOW_HASKELL__)
import Data.Data hiding (Typeable) import Data.Data hiding (Typeable)
import GHC.Exts ((==#), build, reallyUnsafePtrEquality#) import GHC.Exts ((==#), build, reallyUnsafePtrEquality#)
import GHC.Generics (Generic)
#endif #endif


------------------------------------------------------------------------ ------------------------------------------------------------------------
Expand All @@ -122,7 +127,11 @@ data HashMap k v
| Leaf !Hash !(Leaf k v) | Leaf !Hash !(Leaf k v)
| Full !(A.Array (HashMap k v)) | Full !(A.Array (HashMap k v))
| Collision !Hash !(A.Array (Leaf k v)) | Collision !Hash !(A.Array (Leaf k v))
#if defined(__GLASGOW_HASKELL__)
deriving (Generic, Typeable)
#else
deriving (Typeable) deriving (Typeable)
#endif


instance (NFData k, NFData v) => NFData (HashMap k v) where instance (NFData k, NFData v) => NFData (HashMap k v) where
rnf Empty = () rnf Empty = ()
Expand Down
9 changes: 9 additions & 0 deletions Data/HashSet.hs
Original file line number Original file line Diff line number Diff line change
@@ -1,5 +1,9 @@
{-# LANGUAGE CPP, DeriveDataTypeable #-} {-# LANGUAGE CPP, DeriveDataTypeable #-}


#if defined(__GLASGOW_HASKELL__)
{-# LANGUAGE DeriveGeneric #-}
#endif

------------------------------------------------------------------------ ------------------------------------------------------------------------
-- | -- |
-- Module : Data.HashSet -- Module : Data.HashSet
Expand Down Expand Up @@ -72,12 +76,17 @@ import Data.Typeable (Typeable)
#if defined(__GLASGOW_HASKELL__) #if defined(__GLASGOW_HASKELL__)
import Data.Data hiding (Typeable) import Data.Data hiding (Typeable)
import GHC.Exts (build) import GHC.Exts (build)
import GHC.Generics (Generic)
#endif #endif


-- | A set of values. A set cannot contain duplicate values. -- | A set of values. A set cannot contain duplicate values.
newtype HashSet a = HashSet { newtype HashSet a = HashSet {
asMap :: HashMap a () asMap :: HashMap a ()
#if defined(__GLASGOW_HASKELL__)
} deriving (Generic, Typeable)
#else
} deriving (Typeable) } deriving (Typeable)
#endif


instance (NFData a) => NFData (HashSet a) where instance (NFData a) => NFData (HashSet a) where
rnf = rnf . asMap rnf = rnf . asMap
Expand Down
1 change: 1 addition & 0 deletions unordered-containers.cabal
Original file line number Original file line Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@ library
build-depends: build-depends:
base >= 4 && < 5, base >= 4 && < 5,
deepseq >= 1.1, deepseq >= 1.1,
ghc-prim >= 0.2,

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@2piix, why is this dependency necessary?

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

GHC.Generics is exported by base so it shouldn't be necessary.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Not for all base, GHC.Generics is in only since base-4.6. Also Generics are added in GHC-7.2, yet unordered-containers seems to compile down to 7.0.

I'm not sure whether @tibbe wants to keep the compatibility.

hashable >= 1.0.1.1 hashable >= 1.0.1.1


if impl(ghc < 7.4) if impl(ghc < 7.4)
Expand Down