module Data.Graph.Planar.Serialisation.PlanarCode(PlanarCode(..)) where
import Data.Graph.Planar(SerialisedGraph)
import Data.Graph.Planar.Serialisation.Internal
import Blaze.ByteString.Builder
import Data.Attoparsec.ByteString.Lazy
import qualified Data.ByteString as SBS
import Data.Foldable(foldMap)
import Control.Applicative((<*))
import Data.Monoid(Monoid(..))
import Control.Monad(replicateM)
data PlanarCode = PlanarCode
deriving (Eq, Ord, Show, Read)
instance PlanarEncoding PlanarCode where
type NLabel PlanarCode = ()
type ELabel PlanarCode = ()
putSG = const putPlanarCode
getSG = const getPlanarCode
putName = const $ fromByteString ">>planar_code<<"
getName = string ">>planar_code<<" >> return PlanarCode
sepByNewline = const False
putPlanarCode :: ((Int,Int),SerialisedGraph n e) -> Builder
putPlanarCode ((ord,_),sg) = fromWord8 (fromIntegral ord)
`mappend` foldMap (fromWrite . putNode . nodeEdgesSer)
sg
where
putNode es = foldMap (writeWord8 . succ . fromIntegral . toNodeSer) es
`mappend` writeWord8 0
getPlanarCode :: Parser (SerialisedGraph () ())
getPlanarCode = do num <- fromIntegral `fmap` anyWord8
ess <- replicateM num getNode
let ess' = map (map $ fromIntegral . pred) ess
return $ processPC ess'
where
getNode = SBS.unpack `fmap` takeWhile1 (/= 0) <* anyWord8