{-# LANGUAGE CPP #-}
{-# LANGUAGE DeriveGeneric #-}
module Data.BEncode.BDict
( BKey
, BDictMap (..)
, Data.BEncode.BDict.empty
, Data.BEncode.BDict.singleton
, Data.BEncode.BDict.null
, Data.BEncode.BDict.member
, Data.BEncode.BDict.lookup
, Data.BEncode.BDict.union
, Data.BEncode.BDict.map
, Data.BEncode.BDict.mapWithKey
, Data.BEncode.BDict.foldMapWithKey
, Data.BEncode.BDict.bifoldMap
, Data.BEncode.BDict.fromAscList
, Data.BEncode.BDict.toAscList
) where
import Control.DeepSeq
import Data.ByteString as BS
#if __GLASGOW_HASKELL__ < 710
import Data.Foldable
import Data.Monoid (Monoid (mappend, mempty))
#endif
#if __GLASGOW_HASKELL__ < 808
import Data.Semigroup (Semigroup ((<>)))
#endif
import GHC.Generics (Generic)
type BKey = ByteString
data BDictMap a
= Cons !BKey a !(BDictMap a)
| Nil
deriving (Int -> BDictMap a -> ShowS
forall a. Show a => Int -> BDictMap a -> ShowS
forall a. Show a => [BDictMap a] -> ShowS
forall a. Show a => BDictMap a -> String
forall a.
(Int -> a -> ShowS) -> (a -> String) -> ([a] -> ShowS) -> Show a
showList :: [BDictMap a] -> ShowS
$cshowList :: forall a. Show a => [BDictMap a] -> ShowS
show :: BDictMap a -> String
$cshow :: forall a. Show a => BDictMap a -> String
showsPrec :: Int -> BDictMap a -> ShowS
$cshowsPrec :: forall a. Show a => Int -> BDictMap a -> ShowS
Show, ReadPrec [BDictMap a]
ReadPrec (BDictMap a)
ReadS [BDictMap a]
forall a. Read a => ReadPrec [BDictMap a]
forall a. Read a => ReadPrec (BDictMap a)
forall a. Read a => Int -> ReadS (BDictMap a)
forall a. Read a => ReadS [BDictMap a]
forall a.
(Int -> ReadS a)
-> ReadS [a] -> ReadPrec a -> ReadPrec [a] -> Read a
readListPrec :: ReadPrec [BDictMap a]
$creadListPrec :: forall a. Read a => ReadPrec [BDictMap a]
readPrec :: ReadPrec (BDictMap a)
$creadPrec :: forall a. Read a => ReadPrec (BDictMap a)
readList :: ReadS [BDictMap a]
$creadList :: forall a. Read a => ReadS [BDictMap a]
readsPrec :: Int -> ReadS (BDictMap a)
$creadsPrec :: forall a. Read a => Int -> ReadS (BDictMap a)
Read, BDictMap a -> BDictMap a -> Bool
forall a. Eq a => BDictMap a -> BDictMap a -> Bool
forall a. (a -> a -> Bool) -> (a -> a -> Bool) -> Eq a
/= :: BDictMap a -> BDictMap a -> Bool
$c/= :: forall a. Eq a => BDictMap a -> BDictMap a -> Bool
== :: BDictMap a -> BDictMap a -> Bool
$c== :: forall a. Eq a => BDictMap a -> BDictMap a -> Bool
Eq, BDictMap a -> BDictMap a -> Bool
BDictMap a -> BDictMap a -> Ordering
forall a.
Eq a
-> (a -> a -> Ordering)
-> (a -> a -> Bool)
-> (a -> a -> Bool)
-> (a -> a -> Bool)
-> (a -> a -> Bool)
-> (a -> a -> a)
-> (a -> a -> a)
-> Ord a
forall {a}. Ord a => Eq (BDictMap a)
forall a. Ord a => BDictMap a -> BDictMap a -> Bool
forall a. Ord a => BDictMap a -> BDictMap a -> Ordering
forall a. Ord a => BDictMap a -> BDictMap a -> BDictMap a
min :: BDictMap a -> BDictMap a -> BDictMap a
$cmin :: forall a. Ord a => BDictMap a -> BDictMap a -> BDictMap a
max :: BDictMap a -> BDictMap a -> BDictMap a
$cmax :: forall a. Ord a => BDictMap a -> BDictMap a -> BDictMap a
>= :: BDictMap a -> BDictMap a -> Bool
$c>= :: forall a. Ord a => BDictMap a -> BDictMap a -> Bool
> :: BDictMap a -> BDictMap a -> Bool
$c> :: forall a. Ord a => BDictMap a -> BDictMap a -> Bool
<= :: BDictMap a -> BDictMap a -> Bool
$c<= :: forall a. Ord a => BDictMap a -> BDictMap a -> Bool
< :: BDictMap a -> BDictMap a -> Bool
$c< :: forall a. Ord a => BDictMap a -> BDictMap a -> Bool
compare :: BDictMap a -> BDictMap a -> Ordering
$ccompare :: forall a. Ord a => BDictMap a -> BDictMap a -> Ordering
Ord, forall a.
(forall x. a -> Rep a x) -> (forall x. Rep a x -> a) -> Generic a
forall a x. Rep (BDictMap a) x -> BDictMap a
forall a x. BDictMap a -> Rep (BDictMap a) x
$cto :: forall a x. Rep (BDictMap a) x -> BDictMap a
$cfrom :: forall a x. BDictMap a -> Rep (BDictMap a) x
Generic)
instance NFData a => NFData (BDictMap a) where
rnf :: BDictMap a -> ()
rnf BDictMap a
Nil = ()
rnf (Cons BKey
_ a
v BDictMap a
xs)= forall a. NFData a => a -> ()
rnf a
v seq :: forall a b. a -> b -> b
`seq` forall a. NFData a => a -> ()
rnf BDictMap a
xs
instance Functor BDictMap where
fmap :: forall a b. (a -> b) -> BDictMap a -> BDictMap b
fmap = forall a b. (a -> b) -> BDictMap a -> BDictMap b
Data.BEncode.BDict.map
{-# INLINE fmap #-}
instance Foldable BDictMap where
foldMap :: forall m a. Monoid m => (a -> m) -> BDictMap a -> m
foldMap a -> m
f = BDictMap a -> m
go
where
go :: BDictMap a -> m
go BDictMap a
Nil = forall a. Monoid a => a
mempty
go (Cons BKey
_ a
v BDictMap a
xs) = a -> m
f a
v forall a. Monoid a => a -> a -> a
`mappend` BDictMap a -> m
go BDictMap a
xs
{-# INLINE foldMap #-}
instance Semigroup (BDictMap a) where
<> :: BDictMap a -> BDictMap a -> BDictMap a
(<>) = forall a. BDictMap a -> BDictMap a -> BDictMap a
Data.BEncode.BDict.union
instance Monoid (BDictMap a) where
mempty :: BDictMap a
mempty = forall a. BDictMap a
Data.BEncode.BDict.empty
mappend :: BDictMap a -> BDictMap a -> BDictMap a
mappend = forall a. Semigroup a => a -> a -> a
(<>)
empty :: BDictMap a
empty :: forall a. BDictMap a
empty = forall a. BDictMap a
Nil
{-# INLINE empty #-}
singleton :: BKey -> a -> BDictMap a
singleton :: forall a. BKey -> a -> BDictMap a
singleton BKey
k a
v = forall a. BKey -> a -> BDictMap a -> BDictMap a
Cons BKey
k a
v forall a. BDictMap a
Nil
{-# INLINE singleton #-}
null :: BDictMap a -> Bool
null :: forall a. BDictMap a -> Bool
null BDictMap a
Nil = Bool
True
null BDictMap a
_ = Bool
False
{-# INLINE null #-}
member :: BKey -> BDictMap a -> Bool
member :: forall a. BKey -> BDictMap a -> Bool
member BKey
key = forall a. BDictMap a -> Bool
go
where
go :: BDictMap a -> Bool
go BDictMap a
Nil = Bool
False
go (Cons BKey
k a
_ BDictMap a
xs)
| BKey
k forall a. Eq a => a -> a -> Bool
== BKey
key = Bool
True
| Bool
otherwise = BDictMap a -> Bool
go BDictMap a
xs
lookup :: BKey -> BDictMap a -> Maybe a
lookup :: forall a. BKey -> BDictMap a -> Maybe a
lookup BKey
x = forall {a}. BDictMap a -> Maybe a
go
where
go :: BDictMap a -> Maybe a
go BDictMap a
Nil = forall a. Maybe a
Nothing
go (Cons BKey
k a
v BDictMap a
xs)
| BKey
k forall a. Eq a => a -> a -> Bool
== BKey
x = forall a. a -> Maybe a
Just a
v
| Bool
otherwise = BDictMap a -> Maybe a
go BDictMap a
xs
{-# INLINE lookup #-}
union :: BDictMap a -> BDictMap a -> BDictMap a
union :: forall a. BDictMap a -> BDictMap a -> BDictMap a
union BDictMap a
Nil BDictMap a
xs = BDictMap a
xs
union BDictMap a
xs BDictMap a
Nil = BDictMap a
xs
union bd :: BDictMap a
bd@(Cons BKey
k a
v BDictMap a
xs) bd' :: BDictMap a
bd'@(Cons BKey
k' a
v' BDictMap a
xs')
| BKey
k forall a. Ord a => a -> a -> Bool
< BKey
k' = forall a. BKey -> a -> BDictMap a -> BDictMap a
Cons BKey
k a
v (forall a. BDictMap a -> BDictMap a -> BDictMap a
union BDictMap a
xs BDictMap a
bd')
| Bool
otherwise = forall a. BKey -> a -> BDictMap a -> BDictMap a
Cons BKey
k' a
v' (forall a. BDictMap a -> BDictMap a -> BDictMap a
union BDictMap a
bd BDictMap a
xs')
map :: (a -> b) -> BDictMap a -> BDictMap b
map :: forall a b. (a -> b) -> BDictMap a -> BDictMap b
map a -> b
f = BDictMap a -> BDictMap b
go
where
go :: BDictMap a -> BDictMap b
go BDictMap a
Nil = forall a. BDictMap a
Nil
go (Cons BKey
k a
v BDictMap a
xs) = forall a. BKey -> a -> BDictMap a -> BDictMap a
Cons BKey
k (a -> b
f a
v) (BDictMap a -> BDictMap b
go BDictMap a
xs)
{-# INLINE map #-}
mapWithKey :: (BKey -> a -> b) -> BDictMap a -> BDictMap b
mapWithKey :: forall a b. (BKey -> a -> b) -> BDictMap a -> BDictMap b
mapWithKey BKey -> a -> b
f = BDictMap a -> BDictMap b
go
where
go :: BDictMap a -> BDictMap b
go BDictMap a
Nil = forall a. BDictMap a
Nil
go (Cons BKey
k a
v BDictMap a
xs) = forall a. BKey -> a -> BDictMap a -> BDictMap a
Cons BKey
k (BKey -> a -> b
f BKey
k a
v) (BDictMap a -> BDictMap b
go BDictMap a
xs)
{-# INLINE mapWithKey #-}
foldMapWithKey :: Monoid m => (BKey -> a -> m) -> BDictMap a -> m
foldMapWithKey :: forall m a. Monoid m => (BKey -> a -> m) -> BDictMap a -> m
foldMapWithKey BKey -> a -> m
f = BDictMap a -> m
go
where
go :: BDictMap a -> m
go BDictMap a
Nil = forall a. Monoid a => a
mempty
go (Cons BKey
k a
v BDictMap a
xs) = BKey -> a -> m
f BKey
k a
v forall a. Monoid a => a -> a -> a
`mappend` BDictMap a -> m
go BDictMap a
xs
{-# INLINE foldMapWithKey #-}
{-# DEPRECATED bifoldMap "Use foldMapWithKey instead" #-}
bifoldMap :: Monoid m => (BKey -> a -> m) -> BDictMap a -> m
bifoldMap :: forall m a. Monoid m => (BKey -> a -> m) -> BDictMap a -> m
bifoldMap = forall m a. Monoid m => (BKey -> a -> m) -> BDictMap a -> m
foldMapWithKey
{-# INLINE bifoldMap #-}
fromAscList :: [(BKey, a)] -> BDictMap a
fromAscList :: forall a. [(BKey, a)] -> BDictMap a
fromAscList [] = forall a. BDictMap a
Nil
fromAscList ((BKey
k, a
v) : [(BKey, a)]
xs) = forall a. BKey -> a -> BDictMap a -> BDictMap a
Cons BKey
k a
v (forall a. [(BKey, a)] -> BDictMap a
fromAscList [(BKey, a)]
xs)
toAscList :: BDictMap a -> [(BKey, a)]
toAscList :: forall a. BDictMap a -> [(BKey, a)]
toAscList BDictMap a
Nil = []
toAscList (Cons BKey
k a
v BDictMap a
xs) = (BKey
k, a
v) forall a. a -> [a] -> [a]
: forall a. BDictMap a -> [(BKey, a)]
toAscList BDictMap a
xs