Browse files

added a recover method to go hunting through the cache

  • Loading branch information...
1 parent 7b27952 commit 87a39dfdf108ec159aebf8e95b778c2f7e4aabed @ekmett committed Aug 3, 2011
Showing with 11 additions and 1 deletion.
  1. +10 −0 Data/Interned/Internal.hs
  2. +1 −1 intern.cabal
View
10 Data/Interned/Internal.hs
@@ -12,6 +12,7 @@ module Data.Interned.Internal
, cacheSize
, Id
, intern
+ , recover
) where
import Data.Hashable
@@ -36,6 +37,7 @@ mkCache :: Interned t => Cache t
mkCache = result where
result = Cache $ unsafePerformIO $ newMVar $ CacheState (seedIdentity result) HashMap.empty
+
type Id = Int
class ( Eq (Description t)
@@ -69,3 +71,11 @@ intern bt = unsafeDupablePerformIO $ modifyMVar (getCache cache) go
return (CacheState (i + 1) (HashMap.insert dt wt m), t)
remove = modifyMVar_ (getCache cache) $
\ (CacheState i m) -> return $ CacheState i (HashMap.delete dt m)
+
+-- given a description, go hunting for an entry in the cache
+recover :: Interned t => Description t -> IO (Maybe t)
+recover dt = do
+ CacheState _ m <- readMVar $ getCache cache
+ case HashMap.lookup dt m of
+ Nothing -> return Nothing
+ Just wt -> deRefWeak wt
View
2 intern.cabal
@@ -1,6 +1,6 @@
name: intern
category: Data, Data Structures
-version: 0.4.0
+version: 0.4.1
license: BSD3
cabal-version: >= 1.6
license-file: LICENSE

0 comments on commit 87a39df

Please sign in to comment.