packages feed

lhc-0.6.20081127: src/Support/MapBinaryInstance.hs

{-
 We can't use the standard instances for serializing maps containing
 Atoms or Ids. Atoms are compared using their unique  id which depends
 on the order in which they were created. This completely invalidates
 the standard map instances which rely on
 'fromAscDistinctList . toAscDistinctList = id' to be true.
 Once we've gotten rid of StringTable.Atoms, we can get rid of this
 module as well.
-}
module Support.MapBinaryInstance where


import Data.Binary
import Data.Word
import Data.Map as Map
import Data.Set as Set
import Control.Monad

putMap :: (Binary k,Ord k,Binary v) => Map.Map k v -> Put
putMap x = do
        put (fromIntegral $ Map.size x :: Word32)
        mapM_ put (Map.toList x)
getMap :: (Binary k,Ord k,Binary v) => Get (Map.Map k v)
getMap = do
        sz <- get :: Get Word32
        ls <- replicateM (fromIntegral sz) get
        return (Map.fromList ls)


putSet :: (Binary a,Ord a) => Set.Set a -> Put
putSet x = do
        put (fromIntegral $ Set.size x :: Word32)
        mapM_ put (Set.toList x)

getSet :: (Binary a,Ord a) => Get (Set.Set a)
getSet = do
        sz <- get :: Get Word32
        ls <- replicateM (fromIntegral sz) get
        return (Set.fromList ls)