Permalink
Browse files

anonymized Id to avoid fighting with ghc's UNPACK paranoia. added int…

…ernals modules and Text support
  • Loading branch information...
1 parent 0f31dc9 commit 3a0a372c7a46dbfd6a389866c2c1f6e17af05af1 @ekmett committed Aug 3, 2011
Showing with 17 additions and 98 deletions.
  1. +1 −41 Data/ByteString/Interned.hs
  2. +1 −1 Data/Interned.hs
  3. +5 −9 Data/Interned/Internal.hs
  4. +2 −44 Data/String/Interned.hs
  5. +8 −3 intern.cabal
@@ -1,46 +1,6 @@
-{-# LANGUAGE TypeFamilies, FlexibleInstances #-}
module Data.ByteString.Interned
( InternedByteString(..)
) where
-import Data.String
-import Data.Interned
-import Data.ByteString
-import Data.ByteString.Char8 as Char8
-import Data.Hashable
-import Data.Function (on)
+import Data.ByteString.Interned.Internal
-data InternedByteString = InternedByteString
- {-# UNPACK #-} !(Id InternedByteString)
- {-# UNPACK #-} !ByteString
-
-instance IsString InternedByteString where
- fromString = intern . Char8.pack
-
-instance Eq InternedByteString where
- (==) = (==) `on` identity
-
-instance Ord InternedByteString where
- compare = compare `on` identity
-
-instance Show InternedByteString where
- showsPrec d (InternedByteString _ b) = showsPrec d b
-
-instance Interned InternedByteString where
- type Uninterned InternedByteString = ByteString
- data Description InternedByteString = DBS {-# UNPACK #-} !ByteString
- deriving (Eq)
- describe = DBS
- identify = InternedByteString
- identity (InternedByteString i _) = i
- cache = ibsCache
-
-instance Uninternable InternedByteString where
- unintern (InternedByteString _ b) = b
-
-instance Hashable (Description InternedByteString) where
- hash (DBS h) = hash h
-
-ibsCache :: Cache InternedByteString
-ibsCache = mkCache
-{-# NOINLINE ibsCache #-}
View
@@ -4,7 +4,7 @@ module Data.Interned
, mkCache
, Cache
, cacheSize
- , Id(..)
+ , Id
, intern
) where
View
@@ -10,7 +10,7 @@ module Data.Interned.Internal
, Cache(..)
, CacheState(..)
, cacheSize
- , Id(..)
+ , Id
, intern
) where
@@ -22,7 +22,7 @@ import GHC.IO (unsafeDupablePerformIO, unsafePerformIO)
import System.Mem.Weak
data CacheState t = CacheState
- {-# UNPACK #-} !(Id t)
+ {-# UNPACK #-} !Id
!(HashMap (Description t) (Weak t))
newtype Cache t = Cache { getCache :: MVar (CacheState t) }
@@ -35,20 +35,16 @@ cacheSize (Cache t) = do
mkCache :: Cache t
mkCache = Cache $ unsafePerformIO $ newMVar $ CacheState 0 HashMap.empty
-newtype Id t = Id Int deriving (Eq,Ord,Show,Num,Real,Integral,Enum)
-
-instance Hashable (Id t) where
- hash (Id t) = hash t
- hashWithSalt s (Id t) = hashWithSalt s t
+type Id = Int
class ( Eq (Description t)
, Hashable (Description t)
) => Interned t where
data Description t
type Uninterned t
describe :: Uninterned t -> Description t
- identify :: Id t -> Uninterned t -> t
- identity :: t -> Id t
+ identify :: Id -> Uninterned t -> t
+ identity :: t -> Id
cache :: Cache t
class Interned t => Uninternable t where
View
@@ -1,47 +1,5 @@
-{-# LANGUAGE TypeFamilies, FlexibleInstances #-}
module Data.String.Interned
- ( InternedString(..)
+ ( InternedString
) where
-import Data.String
-import Data.Interned
-import Data.Hashable
-import Data.Foldable
-import Data.Function (on)
-
-data InternedString = IS
- {-# UNPACK #-} !(Id InternedString)
- String
-
-instance IsString InternedString where
- fromString = intern
-
-instance Eq InternedString where
- (==) = (==) `on` identity
-
-instance Ord InternedString where
- compare = compare `on` identity
-
-instance Show InternedString where
- showsPrec d (IS _ b) = showsPrec d b
-
-instance Interned InternedString where
- type Uninterned InternedString = String
- data Description InternedString = Cons {-# UNPACK #-} !Char String | Nil
- deriving (Eq)
- describe (c:cs) = Cons c cs
- describe [] = Nil
- identify = IS
- identity (IS i _) = i
- cache = stringCache
-
-instance Uninternable InternedString where
- unintern (IS _ b) = b
-
-instance Hashable (Description InternedString) where
- hash (Cons c s) = foldl' hashWithSalt (hashWithSalt 0 c) s
- hash Nil = 0
-
-stringCache :: Cache InternedString
-stringCache = mkCache
-{-# NOINLINE stringCache #-}
+import Data.String.Interned.Internal
View
@@ -1,6 +1,6 @@
name: intern
category: Data, Data Structures
-version: 0.2.2.1
+version: 0.3.0
license: BSD3
cabal-version: >= 1.6
license-file: LICENSE
@@ -23,13 +23,18 @@ library
build-depends:
base >= 4 && < 5,
bytestring >= 0.9.1 && < 0.10,
- unordered-containers >= 0.1.4 && < 0.2,
- hashable >= 1.1.2 && < 1.2
+ text >= 0.11.1.5 && < 0.12,
+ hashable >= 1.1.2 && < 1.2,
+ unordered-containers >= 0.1.4 && < 0.2
exposed-modules:
Data.Interned
Data.Interned.Internal
Data.ByteString.Interned
+ Data.ByteString.Interned.Internal
Data.String.Interned
+ Data.String.Interned.Internal
+ Data.Text.Interned
+ Data.Text.Interned.Internal
ghc-options: -Wall

0 comments on commit 3a0a372

Please sign in to comment.