{-# LANGUAGE FlexibleContexts #-}
{-# LANGUAGE FlexibleInstances #-}
{-# LANGUAGE NoMonomorphismRestriction #-}
{-# LANGUAGE UndecidableInstances #-}
module Flat.Instances.Mono
( sizeSequence
, encodeSequence
, decodeSequence
, sizeList
, encodeList
, decodeList
, sizeSet
, encodeSet
, decodeSet
, sizeMap
, encodeMap
, decodeMap
, AsArray(..)
, AsList(..)
, AsSet(..)
, AsMap(..)
)
where
import Data.Containers
import qualified Data.Foldable as F
import Data.MonoTraversable (Element, MonoFoldable, ofoldl', otoList)
import Data.Sequences (IsSequence)
import qualified Data.Sequences as S
import Flat.Instances.Util
newtype AsArray a =
AsArray
{ forall a. AsArray a -> a
unArray :: a
} deriving (Int -> AsArray a -> ShowS
forall a. Show a => Int -> AsArray a -> ShowS
forall a. Show a => [AsArray a] -> ShowS
forall a. Show a => AsArray a -> String
forall a.
(Int -> a -> ShowS) -> (a -> String) -> ([a] -> ShowS) -> Show a
showList :: [AsArray a] -> ShowS
$cshowList :: forall a. Show a => [AsArray a] -> ShowS
show :: AsArray a -> String
$cshow :: forall a. Show a => AsArray a -> String
showsPrec :: Int -> AsArray a -> ShowS
$cshowsPrec :: forall a. Show a => Int -> AsArray a -> ShowS
Show,AsArray a -> AsArray a -> Bool
forall a. Eq a => AsArray a -> AsArray a -> Bool
forall a. (a -> a -> Bool) -> (a -> a -> Bool) -> Eq a
/= :: AsArray a -> AsArray a -> Bool
$c/= :: forall a. Eq a => AsArray a -> AsArray a -> Bool
== :: AsArray a -> AsArray a -> Bool
$c== :: forall a. Eq a => AsArray a -> AsArray a -> Bool
Eq,AsArray a -> AsArray a -> Bool
AsArray a -> AsArray a -> Ordering
AsArray a -> AsArray a -> AsArray a
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 (AsArray a)
forall a. Ord a => AsArray a -> AsArray a -> Bool
forall a. Ord a => AsArray a -> AsArray a -> Ordering
forall a. Ord a => AsArray a -> AsArray a -> AsArray a
min :: AsArray a -> AsArray a -> AsArray a
$cmin :: forall a. Ord a => AsArray a -> AsArray a -> AsArray a
max :: AsArray a -> AsArray a -> AsArray a
$cmax :: forall a. Ord a => AsArray a -> AsArray a -> AsArray a
>= :: AsArray a -> AsArray a -> Bool
$c>= :: forall a. Ord a => AsArray a -> AsArray a -> Bool
> :: AsArray a -> AsArray a -> Bool
$c> :: forall a. Ord a => AsArray a -> AsArray a -> Bool
<= :: AsArray a -> AsArray a -> Bool
$c<= :: forall a. Ord a => AsArray a -> AsArray a -> Bool
< :: AsArray a -> AsArray a -> Bool
$c< :: forall a. Ord a => AsArray a -> AsArray a -> Bool
compare :: AsArray a -> AsArray a -> Ordering
$ccompare :: forall a. Ord a => AsArray a -> AsArray a -> Ordering
Ord)
instance (IsSequence r, Flat (Element r)) => Flat (AsArray r) where
size :: AsArray r -> Int -> Int
size (AsArray r
a) = forall mono.
(IsSequence mono, Flat (Element mono)) =>
mono -> Int -> Int
sizeSequence r
a
encode :: AsArray r -> Encoding
encode (AsArray r
a) = forall mono.
(Flat (Element mono), MonoFoldable mono) =>
mono -> Encoding
encodeSequence r
a
decode :: Get (AsArray r)
decode = forall a. a -> AsArray a
AsArray forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> forall b. (Flat (Element b), IsSequence b) => Get b
decodeSequence
sizeSequence
:: (IsSequence mono, Flat (Element mono)) => mono -> NumBits -> NumBits
sizeSequence :: forall mono.
(IsSequence mono, Flat (Element mono)) =>
mono -> Int -> Int
sizeSequence mono
s Int
acc =
let (Int
sz, Int
len) =
forall mono a.
MonoFoldable mono =>
(a -> Element mono -> a) -> a -> mono -> a
ofoldl' (\(Int
acc, Int
l) Element mono
e -> (forall a. Flat a => a -> Int -> Int
size Element mono
e Int
acc, Int
l forall a. Num a => a -> a -> a
+ Int
1)) (Int
acc, Int
0 :: NumBits) mono
s
in Int
sz forall a. Num a => a -> a -> a
+ Int -> Int
arrayBits Int
len
{-# INLINE sizeSequence #-}
encodeSequence :: (Flat (Element mono), MonoFoldable mono) => mono -> Encoding
encodeSequence :: forall mono.
(Flat (Element mono), MonoFoldable mono) =>
mono -> Encoding
encodeSequence = forall a. Flat a => [a] -> Encoding
encodeArray forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall mono. MonoFoldable mono => mono -> [Element mono]
otoList
{-# INLINE encodeSequence #-}
decodeSequence :: (Flat (Element b), IsSequence b) => Get b
decodeSequence :: forall b. (Flat (Element b), IsSequence b) => Get b
decodeSequence = forall seq. IsSequence seq => [Element seq] -> seq
S.fromList forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> forall a. Get a -> Get [a]
decodeArrayWith forall a. Flat a => Get a
decode
{-# INLINE decodeSequence #-}
newtype AsList a =
AsList
{ forall a. AsList a -> a
unList :: a
} deriving (Int -> AsList a -> ShowS
forall a. Show a => Int -> AsList a -> ShowS
forall a. Show a => [AsList a] -> ShowS
forall a. Show a => AsList a -> String
forall a.
(Int -> a -> ShowS) -> (a -> String) -> ([a] -> ShowS) -> Show a
showList :: [AsList a] -> ShowS
$cshowList :: forall a. Show a => [AsList a] -> ShowS
show :: AsList a -> String
$cshow :: forall a. Show a => AsList a -> String
showsPrec :: Int -> AsList a -> ShowS
$cshowsPrec :: forall a. Show a => Int -> AsList a -> ShowS
Show,AsList a -> AsList a -> Bool
forall a. Eq a => AsList a -> AsList a -> Bool
forall a. (a -> a -> Bool) -> (a -> a -> Bool) -> Eq a
/= :: AsList a -> AsList a -> Bool
$c/= :: forall a. Eq a => AsList a -> AsList a -> Bool
== :: AsList a -> AsList a -> Bool
$c== :: forall a. Eq a => AsList a -> AsList a -> Bool
Eq,AsList a -> AsList a -> Bool
AsList a -> AsList a -> Ordering
AsList a -> AsList a -> AsList a
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 (AsList a)
forall a. Ord a => AsList a -> AsList a -> Bool
forall a. Ord a => AsList a -> AsList a -> Ordering
forall a. Ord a => AsList a -> AsList a -> AsList a
min :: AsList a -> AsList a -> AsList a
$cmin :: forall a. Ord a => AsList a -> AsList a -> AsList a
max :: AsList a -> AsList a -> AsList a
$cmax :: forall a. Ord a => AsList a -> AsList a -> AsList a
>= :: AsList a -> AsList a -> Bool
$c>= :: forall a. Ord a => AsList a -> AsList a -> Bool
> :: AsList a -> AsList a -> Bool
$c> :: forall a. Ord a => AsList a -> AsList a -> Bool
<= :: AsList a -> AsList a -> Bool
$c<= :: forall a. Ord a => AsList a -> AsList a -> Bool
< :: AsList a -> AsList a -> Bool
$c< :: forall a. Ord a => AsList a -> AsList a -> Bool
compare :: AsList a -> AsList a -> Ordering
$ccompare :: forall a. Ord a => AsList a -> AsList a -> Ordering
Ord)
instance (IsSequence l, Flat (Element l)) => Flat (AsList l) where
size :: AsList l -> Int -> Int
size = forall mono.
(MonoFoldable mono, Flat (Element mono)) =>
mono -> Int -> Int
sizeList forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall a. AsList a -> a
unList
encode :: AsList l -> Encoding
encode = forall mono.
(Flat (Element mono), MonoFoldable mono) =>
mono -> Encoding
encodeList forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall a. AsList a -> a
unList
decode :: Get (AsList l)
decode = forall a. a -> AsList a
AsList forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> forall b. (IsSequence b, Flat (Element b)) => Get b
decodeList
{-# INLINE sizeList #-}
sizeList
:: (MonoFoldable mono, Flat (Element mono)) => mono -> NumBits -> NumBits
sizeList :: forall mono.
(MonoFoldable mono, Flat (Element mono)) =>
mono -> Int -> Int
sizeList mono
l Int
sz = forall mono a.
MonoFoldable mono =>
(a -> Element mono -> a) -> a -> mono -> a
ofoldl' (\Int
s Element mono
e -> forall a. Flat a => a -> Int -> Int
size Element mono
e (Int
s forall a. Num a => a -> a -> a
+ Int
1)) (Int
sz forall a. Num a => a -> a -> a
+ Int
1) mono
l
{-# INLINE encodeList #-}
encodeList :: (Flat (Element mono), MonoFoldable mono) => mono -> Encoding
encodeList :: forall mono.
(Flat (Element mono), MonoFoldable mono) =>
mono -> Encoding
encodeList = forall t. (t -> Encoding) -> [t] -> Encoding
encodeListWith forall a. Flat a => a -> Encoding
encode forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall mono. MonoFoldable mono => mono -> [Element mono]
otoList
{-# INLINE decodeList #-}
decodeList :: (IsSequence b, Flat (Element b)) => Get b
decodeList :: forall b. (IsSequence b, Flat (Element b)) => Get b
decodeList = forall seq. IsSequence seq => [Element seq] -> seq
S.fromList forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> forall a. Get a -> Get [a]
decodeListWith forall a. Flat a => Get a
decode
newtype AsSet a =
AsSet
{ forall a. AsSet a -> a
unSet :: a
} deriving (Int -> AsSet a -> ShowS
forall a. Show a => Int -> AsSet a -> ShowS
forall a. Show a => [AsSet a] -> ShowS
forall a. Show a => AsSet a -> String
forall a.
(Int -> a -> ShowS) -> (a -> String) -> ([a] -> ShowS) -> Show a
showList :: [AsSet a] -> ShowS
$cshowList :: forall a. Show a => [AsSet a] -> ShowS
show :: AsSet a -> String
$cshow :: forall a. Show a => AsSet a -> String
showsPrec :: Int -> AsSet a -> ShowS
$cshowsPrec :: forall a. Show a => Int -> AsSet a -> ShowS
Show,AsSet a -> AsSet a -> Bool
forall a. Eq a => AsSet a -> AsSet a -> Bool
forall a. (a -> a -> Bool) -> (a -> a -> Bool) -> Eq a
/= :: AsSet a -> AsSet a -> Bool
$c/= :: forall a. Eq a => AsSet a -> AsSet a -> Bool
== :: AsSet a -> AsSet a -> Bool
$c== :: forall a. Eq a => AsSet a -> AsSet a -> Bool
Eq,AsSet a -> AsSet a -> Bool
AsSet a -> AsSet a -> Ordering
AsSet a -> AsSet a -> AsSet a
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 (AsSet a)
forall a. Ord a => AsSet a -> AsSet a -> Bool
forall a. Ord a => AsSet a -> AsSet a -> Ordering
forall a. Ord a => AsSet a -> AsSet a -> AsSet a
min :: AsSet a -> AsSet a -> AsSet a
$cmin :: forall a. Ord a => AsSet a -> AsSet a -> AsSet a
max :: AsSet a -> AsSet a -> AsSet a
$cmax :: forall a. Ord a => AsSet a -> AsSet a -> AsSet a
>= :: AsSet a -> AsSet a -> Bool
$c>= :: forall a. Ord a => AsSet a -> AsSet a -> Bool
> :: AsSet a -> AsSet a -> Bool
$c> :: forall a. Ord a => AsSet a -> AsSet a -> Bool
<= :: AsSet a -> AsSet a -> Bool
$c<= :: forall a. Ord a => AsSet a -> AsSet a -> Bool
< :: AsSet a -> AsSet a -> Bool
$c< :: forall a. Ord a => AsSet a -> AsSet a -> Bool
compare :: AsSet a -> AsSet a -> Ordering
$ccompare :: forall a. Ord a => AsSet a -> AsSet a -> Ordering
Ord)
instance (IsSet set, Flat (Element set)) => Flat (AsSet set) where
size :: AsSet set -> Int -> Int
size = forall set. (IsSet set, Flat (Element set)) => Size set
sizeSet forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall a. AsSet a -> a
unSet
encode :: AsSet set -> Encoding
encode = forall set. (IsSet set, Flat (Element set)) => set -> Encoding
encodeSet forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall a. AsSet a -> a
unSet
decode :: Get (AsSet set)
decode = forall a. a -> AsSet a
AsSet forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> forall set. (IsSet set, Flat (Element set)) => Get set
decodeSet
sizeSet :: (IsSet set, Flat (Element set)) => Size set
sizeSet :: forall set. (IsSet set, Flat (Element set)) => Size set
sizeSet set
l Int
acc = forall mono a.
MonoFoldable mono =>
(a -> Element mono -> a) -> a -> mono -> a
ofoldl' (\Int
acc Element set
e -> forall a. Flat a => a -> Int -> Int
size Element set
e (Int
acc forall a. Num a => a -> a -> a
+ Int
1)) (Int
acc forall a. Num a => a -> a -> a
+ Int
1) set
l
{-# INLINE sizeSet #-}
encodeSet :: (IsSet set, Flat (Element set)) => set -> Encoding
encodeSet :: forall set. (IsSet set, Flat (Element set)) => set -> Encoding
encodeSet = forall mono.
(Flat (Element mono), MonoFoldable mono) =>
mono -> Encoding
encodeList forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall set. IsSet set => set -> [Element set]
setToList
{-# INLINE encodeSet #-}
decodeSet :: (IsSet set, Flat (Element set)) => Get set
decodeSet :: forall set. (IsSet set, Flat (Element set)) => Get set
decodeSet = forall set. IsSet set => [Element set] -> set
setFromList forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> forall b. (IsSequence b, Flat (Element b)) => Get b
decodeList
{-# INLINE decodeSet #-}
newtype AsMap a =
AsMap
{ forall a. AsMap a -> a
unMap :: a
} deriving (Int -> AsMap a -> ShowS
forall a. Show a => Int -> AsMap a -> ShowS
forall a. Show a => [AsMap a] -> ShowS
forall a. Show a => AsMap a -> String
forall a.
(Int -> a -> ShowS) -> (a -> String) -> ([a] -> ShowS) -> Show a
showList :: [AsMap a] -> ShowS
$cshowList :: forall a. Show a => [AsMap a] -> ShowS
show :: AsMap a -> String
$cshow :: forall a. Show a => AsMap a -> String
showsPrec :: Int -> AsMap a -> ShowS
$cshowsPrec :: forall a. Show a => Int -> AsMap a -> ShowS
Show,AsMap a -> AsMap a -> Bool
forall a. Eq a => AsMap a -> AsMap a -> Bool
forall a. (a -> a -> Bool) -> (a -> a -> Bool) -> Eq a
/= :: AsMap a -> AsMap a -> Bool
$c/= :: forall a. Eq a => AsMap a -> AsMap a -> Bool
== :: AsMap a -> AsMap a -> Bool
$c== :: forall a. Eq a => AsMap a -> AsMap a -> Bool
Eq,AsMap a -> AsMap a -> Bool
AsMap a -> AsMap a -> Ordering
AsMap a -> AsMap a -> AsMap a
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 (AsMap a)
forall a. Ord a => AsMap a -> AsMap a -> Bool
forall a. Ord a => AsMap a -> AsMap a -> Ordering
forall a. Ord a => AsMap a -> AsMap a -> AsMap a
min :: AsMap a -> AsMap a -> AsMap a
$cmin :: forall a. Ord a => AsMap a -> AsMap a -> AsMap a
max :: AsMap a -> AsMap a -> AsMap a
$cmax :: forall a. Ord a => AsMap a -> AsMap a -> AsMap a
>= :: AsMap a -> AsMap a -> Bool
$c>= :: forall a. Ord a => AsMap a -> AsMap a -> Bool
> :: AsMap a -> AsMap a -> Bool
$c> :: forall a. Ord a => AsMap a -> AsMap a -> Bool
<= :: AsMap a -> AsMap a -> Bool
$c<= :: forall a. Ord a => AsMap a -> AsMap a -> Bool
< :: AsMap a -> AsMap a -> Bool
$c< :: forall a. Ord a => AsMap a -> AsMap a -> Bool
compare :: AsMap a -> AsMap a -> Ordering
$ccompare :: forall a. Ord a => AsMap a -> AsMap a -> Ordering
Ord)
instance (IsMap map, Flat (ContainerKey map), Flat (MapValue map)) => Flat (AsMap map) where
size :: AsMap map -> Int -> Int
size = forall r.
(Flat (ContainerKey r), Flat (MapValue r), IsMap r) =>
Size r
sizeMap forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall a. AsMap a -> a
unMap
encode :: AsMap map -> Encoding
encode = forall map.
(Flat (ContainerKey map), Flat (MapValue map), IsMap map) =>
map -> Encoding
encodeMap forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall a. AsMap a -> a
unMap
decode :: Get (AsMap map)
decode = forall a. a -> AsMap a
AsMap forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> forall map.
(Flat (ContainerKey map), Flat (MapValue map), IsMap map) =>
Get map
decodeMap
{-# INLINE sizeMap #-}
sizeMap :: (Flat (ContainerKey r), Flat (MapValue r), IsMap r) => Size r
sizeMap :: forall r.
(Flat (ContainerKey r), Flat (MapValue r), IsMap r) =>
Size r
sizeMap r
m Int
acc =
forall (t :: * -> *) b a.
Foldable t =>
(b -> a -> b) -> b -> t a -> b
F.foldl' (\Int
acc' (ContainerKey r
k, MapValue r
v) -> forall a. Flat a => a -> Int -> Int
size ContainerKey r
k (forall a. Flat a => a -> Int -> Int
size MapValue r
v (Int
acc' forall a. Num a => a -> a -> a
+ Int
1))) (Int
acc forall a. Num a => a -> a -> a
+ Int
1)
forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall map. IsMap map => map -> [(ContainerKey map, MapValue map)]
mapToList
forall a b. (a -> b) -> a -> b
$ r
m
{-# INLINE encodeMap #-}
encodeMap
:: (Flat (ContainerKey map), Flat (MapValue map), IsMap map)
=> map
-> Encoding
encodeMap :: forall map.
(Flat (ContainerKey map), Flat (MapValue map), IsMap map) =>
map -> Encoding
encodeMap = forall t. (t -> Encoding) -> [t] -> Encoding
encodeListWith (\(ContainerKey map
k, MapValue map
v) -> forall a. Flat a => a -> Encoding
encode ContainerKey map
k forall a. Semigroup a => a -> a -> a
<> forall a. Flat a => a -> Encoding
encode MapValue map
v) forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall map. IsMap map => map -> [(ContainerKey map, MapValue map)]
mapToList
{-# INLINE decodeMap #-}
decodeMap
:: (Flat (ContainerKey map), Flat (MapValue map), IsMap map) => Get map
decodeMap :: forall map.
(Flat (ContainerKey map), Flat (MapValue map), IsMap map) =>
Get map
decodeMap = forall map. IsMap map => [(ContainerKey map, MapValue map)] -> map
mapFromList forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> forall a. Get a -> Get [a]
decodeListWith ((,) forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> forall a. Flat a => Get a
decode forall (f :: * -> *) a b. Applicative f => f (a -> b) -> f a -> f b
<*> forall a. Flat a => Get a
decode)