Commits

Alessandro Vermeulen committed afc2f6f

* Implemented SNMap with HashMap. It `works', but it doesn't give you memoisation,
it is even slower than the original fib.

  • Participants
  • Parent commits 0237983

Comments (0)

Files changed (2)

File code/Main.hs

 
 import System.Vacuum.Ubigraph ( view )
 
-main = do putStrLn "--"
+import System.IO (BufferMode (NoBuffering), hSetBuffering , stdout)
+
+main = do hSetBuffering stdout NoBuffering
+          putStrLn "--"
           print (mfib' 30000)
           putStrLn "--"
           print (mfib' 30003)
 mfib' 1 = 1
 mfib' n = mfib (n-2) + mfib (n-1)
 
+
+snfib :: Integer -> Integer
+snfib = S.memo sfib
+
+sfib 0 = 0
+sfib 1 = 1
+sfib n = snfib (n-2) + snfib (n-1)
+
 memostring :: String -> String
 memostring = C.memo reverse

File code/Memo/SNMap/SNMap.hs

 import System.Mem.Weak
 import System.Mem.StableName
 
+import qualified Data.HashTable as HT
+
 import System.IO.Unsafe ( unsafePerformIO )
 
 
 
 -------------------------------------------------------------------------------- 
 -- SNMap Implementation
-type SNMap k v = [(StableName k, v)]
+type SNMap k v = HT.HashTable (StableName k) v
 
 newSNMap :: IO (SNMap k v)
-newSNMap = return []
+newSNMap = HT.new (==) (HT.hashInt . hashStableName)
 
 lookupSNMap :: SNMap k v -> StableName k -> IO (Maybe v)
-lookupSNMap map sn = return (lookup sn map)
+lookupSNMap = HT.lookup
 
 insertSNMap :: SNMap k v -> StableName k -> v -> IO ()
-insertSNMap m k v = -- (k,v) : filter (\(k',_) -> k /= k') m
-                    undefined
+insertSNMap = HT.insert
 
 removeSNMap :: SNMap k v -> StableName k -> IO ()
-removeSNMap = undefined
+removeSNMap = HT.delete
 
 snMapElems :: SNMap k v -> IO [(k,v)]
-snMapElems = undefined
+snMapElems = undefined --HT.toList
 
 -------------------------------------------------------------------------------- 
 -- Memoisation with finalisers