{-# LANGUAGE FlexibleInstances #-}
{-# LANGUAGE FlexibleContexts #-}
{-# LANGUAGE DeriveFunctor #-}

module Data.BList (BList(..)) where

import Data.Binary.Get
import Data.Binary

import Data.Endian

newtype BList a = BList { forall a. BList a -> [a]
fromBList :: [a] } deriving (forall a b. (a -> b) -> BList a -> BList b)
-> (forall a b. a -> BList b -> BList a) -> Functor BList
forall a b. a -> BList b -> BList a
forall a b. (a -> b) -> BList a -> BList b
forall (f :: * -> *).
(forall a b. (a -> b) -> f a -> f b)
-> (forall a b. a -> f b -> f a) -> Functor f
$cfmap :: forall a b. (a -> b) -> BList a -> BList b
fmap :: forall a b. (a -> b) -> BList a -> BList b
$c<$ :: forall a b. a -> BList b -> BList a
<$ :: forall a b. a -> BList b -> BList a
Functor

instance Binary a => Binary (BList a) where
    put :: BList a -> Put
put (BList (a
a:[a]
as)) = a -> Put
forall t. Binary t => t -> Put
put a
a Put -> Put -> Put
forall a b. PutM a -> PutM b -> PutM b
forall (m :: * -> *) a b. Monad m => m a -> m b -> m b
>> BList a -> Put
forall t. Binary t => t -> Put
put ([a] -> BList a
forall a. [a] -> BList a
BList [a]
as)
    put (BList []) = () -> Put
forall a. a -> PutM a
forall (m :: * -> *) a. Monad m => a -> m a
return ()
    get :: Get (BList a)
get = do
        Bool
e <- Get Bool
isEmpty
        if Bool
e then BList a -> Get (BList a)
forall a. a -> Get a
forall (m :: * -> *) a. Monad m => a -> m a
return (BList a -> Get (BList a)) -> BList a -> Get (BList a)
forall a b. (a -> b) -> a -> b
$ [a] -> BList a
forall a. [a] -> BList a
BList [] else do
            a
a <- Get a
forall t. Binary t => Get t
get
            (BList [a]
as) <- Get (BList a)
forall t. Binary t => Get t
get
            BList a -> Get (BList a)
forall a. a -> Get a
forall (m :: * -> *) a. Monad m => a -> m a
return (BList a -> Get (BList a)) -> BList a -> Get (BList a)
forall a b. (a -> b) -> a -> b
$ [a] -> BList a
forall a. [a] -> BList a
BList ([a] -> BList a) -> [a] -> BList a
forall a b. (a -> b) -> a -> b
$ a
a a -> [a] -> [a]
forall a. a -> [a] -> [a]
: [a]
as

instance Binary (Be a) => Binary (Be (BList a)) where
    put :: Be (BList a) -> Put
put (Be (BList [a]
l)) = BList (Be a) -> Put
forall t. Binary t => t -> Put
put (BList (Be a) -> Put) -> BList (Be a) -> Put
forall a b. (a -> b) -> a -> b
$ [Be a] -> BList (Be a)
forall a. [a] -> BList a
BList ([Be a] -> BList (Be a)) -> [Be a] -> BList (Be a)
forall a b. (a -> b) -> a -> b
$ (a -> Be a) -> [a] -> [Be a]
forall a b. (a -> b) -> [a] -> [b]
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
fmap a -> Be a
forall a. a -> Be a
Be [a]
l
    get :: Get (Be (BList a))
get = BList a -> Be (BList a)
forall a. a -> Be a
Be (BList a -> Be (BList a))
-> (BList (Be a) -> BList a) -> BList (Be a) -> Be (BList a)
forall b c a. (b -> c) -> (a -> b) -> a -> c
. (Be a -> a) -> BList (Be a) -> BList a
forall a b. (a -> b) -> BList a -> BList b
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
fmap Be a -> a
forall a. Be a -> a
fromBe (BList (Be a) -> Be (BList a))
-> Get (BList (Be a)) -> Get (Be (BList a))
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> Get (BList (Be a))
forall t. Binary t => Get t
get

instance Binary (Le a) => Binary (Le (BList a)) where
    put :: Le (BList a) -> Put
put (Le (BList [a]
l)) = BList (Le a) -> Put
forall t. Binary t => t -> Put
put (BList (Le a) -> Put) -> BList (Le a) -> Put
forall a b. (a -> b) -> a -> b
$ [Le a] -> BList (Le a)
forall a. [a] -> BList a
BList ([Le a] -> BList (Le a)) -> [Le a] -> BList (Le a)
forall a b. (a -> b) -> a -> b
$ (a -> Le a) -> [a] -> [Le a]
forall a b. (a -> b) -> [a] -> [b]
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
fmap a -> Le a
forall a. a -> Le a
Le [a]
l
    get :: Get (Le (BList a))
get = BList a -> Le (BList a)
forall a. a -> Le a
Le (BList a -> Le (BList a))
-> (BList (Le a) -> BList a) -> BList (Le a) -> Le (BList a)
forall b c a. (b -> c) -> (a -> b) -> a -> c
. (Le a -> a) -> BList (Le a) -> BList a
forall a b. (a -> b) -> BList a -> BList b
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
fmap Le a -> a
forall a. Le a -> a
fromLe (BList (Le a) -> Le (BList a))
-> Get (BList (Le a)) -> Get (Le (BList a))
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> Get (BList (Le a))
forall t. Binary t => Get t
get