{-# LANGUAGE UndecidableInstances #-}

-- |
-- Module: M.Pack.Internal.Bit
-- Description: Bit-level serialization support
-- Copyright: (c) axionbuster, 2025
-- License: BSD-3-Clause
--
-- Implements bit-level serialization for boolean flags and bitsets,
-- supporting both variable-length and fixed-length bit patterns.
module M.Pack.Internal.Bit
  ( Bitwise (..),
    Bitreppable (..),
    Bitset (..),
    FixedBitset (..),
  )
where

import Control.DeepSeq
import Data.Bits
import Data.ByteString.Builder (Builder)
import Data.Data
import Data.Hashable
import Data.Int
import Data.Vector.Unboxed qualified as VU
import GHC.Generics
import GHC.TypeLits
import Language.Haskell.TH.Syntax (Lift)
import M.Pack.Internal.Etc ()
import M.Pack.Internal.Types

-- | a wrapper type that enables bit-level packing of boolean product types.
-- the type parameter @i@ specifies the underlying integral type used to store
-- the bits (e.g. Word8, Word16, etc). the type parameter @a@ is the product
-- type containing the boolean fields to be encoded.
newtype Bitwise i a = Bitwise {forall {k} (i :: k) a. Bitwise i a -> a
unbitwise :: a}
  deriving stock ((forall x. Bitwise i a -> Rep (Bitwise i a) x)
-> (forall x. Rep (Bitwise i a) x -> Bitwise i a)
-> Generic (Bitwise i a)
forall x. Rep (Bitwise i a) x -> Bitwise i a
forall x. Bitwise i a -> Rep (Bitwise i a) x
forall a.
(forall x. a -> Rep a x) -> (forall x. Rep a x -> a) -> Generic a
forall k (i :: k) a x. Rep (Bitwise i a) x -> Bitwise i a
forall k (i :: k) a x. Bitwise i a -> Rep (Bitwise i a) x
$cfrom :: forall k (i :: k) a x. Bitwise i a -> Rep (Bitwise i a) x
from :: forall x. Bitwise i a -> Rep (Bitwise i a) x
$cto :: forall k (i :: k) a x. Rep (Bitwise i a) x -> Bitwise i a
to :: forall x. Rep (Bitwise i a) x -> Bitwise i a
Generic, Typeable, Typeable (Bitwise i a)
Typeable (Bitwise i a) =>
(forall (c :: * -> *).
 (forall d b. Data d => c (d -> b) -> d -> c b)
 -> (forall g. g -> c g) -> Bitwise i a -> c (Bitwise i a))
-> (forall (c :: * -> *).
    (forall b r. Data b => c (b -> r) -> c r)
    -> (forall r. r -> c r) -> Constr -> c (Bitwise i a))
-> (Bitwise i a -> Constr)
-> (Bitwise i a -> DataType)
-> (forall (t :: * -> *) (c :: * -> *).
    Typeable t =>
    (forall d. Data d => c (t d)) -> Maybe (c (Bitwise i a)))
-> (forall (t :: * -> * -> *) (c :: * -> *).
    Typeable t =>
    (forall d e. (Data d, Data e) => c (t d e))
    -> Maybe (c (Bitwise i a)))
-> ((forall b. Data b => b -> b) -> Bitwise i a -> Bitwise i a)
-> (forall r r'.
    (r -> r' -> r)
    -> r -> (forall d. Data d => d -> r') -> Bitwise i a -> r)
-> (forall r r'.
    (r' -> r -> r)
    -> r -> (forall d. Data d => d -> r') -> Bitwise i a -> r)
-> (forall u. (forall d. Data d => d -> u) -> Bitwise i a -> [u])
-> (forall u.
    Int -> (forall d. Data d => d -> u) -> Bitwise i a -> u)
-> (forall (m :: * -> *).
    Monad m =>
    (forall d. Data d => d -> m d) -> Bitwise i a -> m (Bitwise i a))
-> (forall (m :: * -> *).
    MonadPlus m =>
    (forall d. Data d => d -> m d) -> Bitwise i a -> m (Bitwise i a))
-> (forall (m :: * -> *).
    MonadPlus m =>
    (forall d. Data d => d -> m d) -> Bitwise i a -> m (Bitwise i a))
-> Data (Bitwise i a)
Bitwise i a -> Constr
Bitwise i a -> DataType
(forall b. Data b => b -> b) -> Bitwise i a -> Bitwise i a
forall a.
Typeable a =>
(forall (c :: * -> *).
 (forall d b. Data d => c (d -> b) -> d -> c b)
 -> (forall g. g -> c g) -> a -> c a)
-> (forall (c :: * -> *).
    (forall b r. Data b => c (b -> r) -> c r)
    -> (forall r. r -> c r) -> Constr -> c a)
-> (a -> Constr)
-> (a -> DataType)
-> (forall (t :: * -> *) (c :: * -> *).
    Typeable t =>
    (forall d. Data d => c (t d)) -> Maybe (c a))
-> (forall (t :: * -> * -> *) (c :: * -> *).
    Typeable t =>
    (forall d e. (Data d, Data e) => c (t d e)) -> Maybe (c a))
-> ((forall b. Data b => b -> b) -> a -> a)
-> (forall r r'.
    (r -> r' -> r) -> r -> (forall d. Data d => d -> r') -> a -> r)
-> (forall r r'.
    (r' -> r -> r) -> r -> (forall d. Data d => d -> r') -> a -> r)
-> (forall u. (forall d. Data d => d -> u) -> a -> [u])
-> (forall u. Int -> (forall d. Data d => d -> u) -> a -> u)
-> (forall (m :: * -> *).
    Monad m =>
    (forall d. Data d => d -> m d) -> a -> m a)
-> (forall (m :: * -> *).
    MonadPlus m =>
    (forall d. Data d => d -> m d) -> a -> m a)
-> (forall (m :: * -> *).
    MonadPlus m =>
    (forall d. Data d => d -> m d) -> a -> m a)
-> Data a
forall u. Int -> (forall d. Data d => d -> u) -> Bitwise i a -> u
forall u. (forall d. Data d => d -> u) -> Bitwise i a -> [u]
forall k (i :: k) a.
(Typeable i, Typeable k, Data a) =>
Typeable (Bitwise i a)
forall k (i :: k) a.
(Typeable i, Typeable k, Data a) =>
Bitwise i a -> Constr
forall k (i :: k) a.
(Typeable i, Typeable k, Data a) =>
Bitwise i a -> DataType
forall k (i :: k) a.
(Typeable i, Typeable k, Data a) =>
(forall b. Data b => b -> b) -> Bitwise i a -> Bitwise i a
forall k (i :: k) a u.
(Typeable i, Typeable k, Data a) =>
Int -> (forall d. Data d => d -> u) -> Bitwise i a -> u
forall k (i :: k) a u.
(Typeable i, Typeable k, Data a) =>
(forall d. Data d => d -> u) -> Bitwise i a -> [u]
forall k (i :: k) a r r'.
(Typeable i, Typeable k, Data a) =>
(r -> r' -> r)
-> r -> (forall d. Data d => d -> r') -> Bitwise i a -> r
forall k (i :: k) a r r'.
(Typeable i, Typeable k, Data a) =>
(r' -> r -> r)
-> r -> (forall d. Data d => d -> r') -> Bitwise i a -> r
forall k (i :: k) a (m :: * -> *).
(Typeable i, Typeable k, Data a, Monad m) =>
(forall d. Data d => d -> m d) -> Bitwise i a -> m (Bitwise i a)
forall k (i :: k) a (m :: * -> *).
(Typeable i, Typeable k, Data a, MonadPlus m) =>
(forall d. Data d => d -> m d) -> Bitwise i a -> m (Bitwise i a)
forall k (i :: k) a (c :: * -> *).
(Typeable i, Typeable k, Data a) =>
(forall b r. Data b => c (b -> r) -> c r)
-> (forall r. r -> c r) -> Constr -> c (Bitwise i a)
forall k (i :: k) a (c :: * -> *).
(Typeable i, Typeable k, Data a) =>
(forall d b. Data d => c (d -> b) -> d -> c b)
-> (forall g. g -> c g) -> Bitwise i a -> c (Bitwise i a)
forall k (i :: k) a (t :: * -> *) (c :: * -> *).
(Typeable i, Typeable k, Data a, Typeable t) =>
(forall d. Data d => c (t d)) -> Maybe (c (Bitwise i a))
forall k (i :: k) a (t :: * -> * -> *) (c :: * -> *).
(Typeable i, Typeable k, Data a, Typeable t) =>
(forall d e. (Data d, Data e) => c (t d e))
-> Maybe (c (Bitwise i a))
forall r r'.
(r -> r' -> r)
-> r -> (forall d. Data d => d -> r') -> Bitwise i a -> r
forall r r'.
(r' -> r -> r)
-> r -> (forall d. Data d => d -> r') -> Bitwise i a -> r
forall (m :: * -> *).
Monad m =>
(forall d. Data d => d -> m d) -> Bitwise i a -> m (Bitwise i a)
forall (m :: * -> *).
MonadPlus m =>
(forall d. Data d => d -> m d) -> Bitwise i a -> m (Bitwise i a)
forall (c :: * -> *).
(forall b r. Data b => c (b -> r) -> c r)
-> (forall r. r -> c r) -> Constr -> c (Bitwise i a)
forall (c :: * -> *).
(forall d b. Data d => c (d -> b) -> d -> c b)
-> (forall g. g -> c g) -> Bitwise i a -> c (Bitwise i a)
forall (t :: * -> *) (c :: * -> *).
Typeable t =>
(forall d. Data d => c (t d)) -> Maybe (c (Bitwise i a))
forall (t :: * -> * -> *) (c :: * -> *).
Typeable t =>
(forall d e. (Data d, Data e) => c (t d e))
-> Maybe (c (Bitwise i a))
$cgfoldl :: forall k (i :: k) a (c :: * -> *).
(Typeable i, Typeable k, Data a) =>
(forall d b. Data d => c (d -> b) -> d -> c b)
-> (forall g. g -> c g) -> Bitwise i a -> c (Bitwise i a)
gfoldl :: forall (c :: * -> *).
(forall d b. Data d => c (d -> b) -> d -> c b)
-> (forall g. g -> c g) -> Bitwise i a -> c (Bitwise i a)
$cgunfold :: forall k (i :: k) a (c :: * -> *).
(Typeable i, Typeable k, Data a) =>
(forall b r. Data b => c (b -> r) -> c r)
-> (forall r. r -> c r) -> Constr -> c (Bitwise i a)
gunfold :: forall (c :: * -> *).
(forall b r. Data b => c (b -> r) -> c r)
-> (forall r. r -> c r) -> Constr -> c (Bitwise i a)
$ctoConstr :: forall k (i :: k) a.
(Typeable i, Typeable k, Data a) =>
Bitwise i a -> Constr
toConstr :: Bitwise i a -> Constr
$cdataTypeOf :: forall k (i :: k) a.
(Typeable i, Typeable k, Data a) =>
Bitwise i a -> DataType
dataTypeOf :: Bitwise i a -> DataType
$cdataCast1 :: forall k (i :: k) a (t :: * -> *) (c :: * -> *).
(Typeable i, Typeable k, Data a, Typeable t) =>
(forall d. Data d => c (t d)) -> Maybe (c (Bitwise i a))
dataCast1 :: forall (t :: * -> *) (c :: * -> *).
Typeable t =>
(forall d. Data d => c (t d)) -> Maybe (c (Bitwise i a))
$cdataCast2 :: forall k (i :: k) a (t :: * -> * -> *) (c :: * -> *).
(Typeable i, Typeable k, Data a, Typeable t) =>
(forall d e. (Data d, Data e) => c (t d e))
-> Maybe (c (Bitwise i a))
dataCast2 :: forall (t :: * -> * -> *) (c :: * -> *).
Typeable t =>
(forall d e. (Data d, Data e) => c (t d e))
-> Maybe (c (Bitwise i a))
$cgmapT :: forall k (i :: k) a.
(Typeable i, Typeable k, Data a) =>
(forall b. Data b => b -> b) -> Bitwise i a -> Bitwise i a
gmapT :: (forall b. Data b => b -> b) -> Bitwise i a -> Bitwise i a
$cgmapQl :: forall k (i :: k) a r r'.
(Typeable i, Typeable k, Data a) =>
(r -> r' -> r)
-> r -> (forall d. Data d => d -> r') -> Bitwise i a -> r
gmapQl :: forall r r'.
(r -> r' -> r)
-> r -> (forall d. Data d => d -> r') -> Bitwise i a -> r
$cgmapQr :: forall k (i :: k) a r r'.
(Typeable i, Typeable k, Data a) =>
(r' -> r -> r)
-> r -> (forall d. Data d => d -> r') -> Bitwise i a -> r
gmapQr :: forall r r'.
(r' -> r -> r)
-> r -> (forall d. Data d => d -> r') -> Bitwise i a -> r
$cgmapQ :: forall k (i :: k) a u.
(Typeable i, Typeable k, Data a) =>
(forall d. Data d => d -> u) -> Bitwise i a -> [u]
gmapQ :: forall u. (forall d. Data d => d -> u) -> Bitwise i a -> [u]
$cgmapQi :: forall k (i :: k) a u.
(Typeable i, Typeable k, Data a) =>
Int -> (forall d. Data d => d -> u) -> Bitwise i a -> u
gmapQi :: forall u. Int -> (forall d. Data d => d -> u) -> Bitwise i a -> u
$cgmapM :: forall k (i :: k) a (m :: * -> *).
(Typeable i, Typeable k, Data a, Monad m) =>
(forall d. Data d => d -> m d) -> Bitwise i a -> m (Bitwise i a)
gmapM :: forall (m :: * -> *).
Monad m =>
(forall d. Data d => d -> m d) -> Bitwise i a -> m (Bitwise i a)
$cgmapMp :: forall k (i :: k) a (m :: * -> *).
(Typeable i, Typeable k, Data a, MonadPlus m) =>
(forall d. Data d => d -> m d) -> Bitwise i a -> m (Bitwise i a)
gmapMp :: forall (m :: * -> *).
MonadPlus m =>
(forall d. Data d => d -> m d) -> Bitwise i a -> m (Bitwise i a)
$cgmapMo :: forall k (i :: k) a (m :: * -> *).
(Typeable i, Typeable k, Data a, MonadPlus m) =>
(forall d. Data d => d -> m d) -> Bitwise i a -> m (Bitwise i a)
gmapMo :: forall (m :: * -> *).
MonadPlus m =>
(forall d. Data d => d -> m d) -> Bitwise i a -> m (Bitwise i a)
Data, (forall a b. (a -> b) -> Bitwise i a -> Bitwise i b)
-> (forall a b. a -> Bitwise i b -> Bitwise i a)
-> Functor (Bitwise i)
forall k (i :: k) a b. a -> Bitwise i b -> Bitwise i a
forall k (i :: k) a b. (a -> b) -> Bitwise i a -> Bitwise i b
forall a b. a -> Bitwise i b -> Bitwise i a
forall a b. (a -> b) -> Bitwise i a -> Bitwise i b
forall (f :: * -> *).
(forall a b. (a -> b) -> f a -> f b)
-> (forall a b. a -> f b -> f a) -> Functor f
$cfmap :: forall k (i :: k) a b. (a -> b) -> Bitwise i a -> Bitwise i b
fmap :: forall a b. (a -> b) -> Bitwise i a -> Bitwise i b
$c<$ :: forall k (i :: k) a b. a -> Bitwise i b -> Bitwise i a
<$ :: forall a b. a -> Bitwise i b -> Bitwise i a
Functor, (forall (m :: * -> *). Quote m => Bitwise i a -> m Exp)
-> (forall (m :: * -> *).
    Quote m =>
    Bitwise i a -> Code m (Bitwise i a))
-> Lift (Bitwise i a)
forall k (i :: k) a (m :: * -> *).
(Lift a, Quote m) =>
Bitwise i a -> m Exp
forall k (i :: k) a (m :: * -> *).
(Lift a, Quote m) =>
Bitwise i a -> Code m (Bitwise i a)
forall t.
(forall (m :: * -> *). Quote m => t -> m Exp)
-> (forall (m :: * -> *). Quote m => t -> Code m t) -> Lift t
forall (m :: * -> *). Quote m => Bitwise i a -> m Exp
forall (m :: * -> *).
Quote m =>
Bitwise i a -> Code m (Bitwise i a)
$clift :: forall k (i :: k) a (m :: * -> *).
(Lift a, Quote m) =>
Bitwise i a -> m Exp
lift :: forall (m :: * -> *). Quote m => Bitwise i a -> m Exp
$cliftTyped :: forall k (i :: k) a (m :: * -> *).
(Lift a, Quote m) =>
Bitwise i a -> Code m (Bitwise i a)
liftTyped :: forall (m :: * -> *).
Quote m =>
Bitwise i a -> Code m (Bitwise i a)
Lift)
  deriving newtype (Bitwise i a -> Bitwise i a -> Bool
(Bitwise i a -> Bitwise i a -> Bool)
-> (Bitwise i a -> Bitwise i a -> Bool) -> Eq (Bitwise i a)
forall a. (a -> a -> Bool) -> (a -> a -> Bool) -> Eq a
forall k (i :: k) a. Eq a => Bitwise i a -> Bitwise i a -> Bool
$c== :: forall k (i :: k) a. Eq a => Bitwise i a -> Bitwise i a -> Bool
== :: Bitwise i a -> Bitwise i a -> Bool
$c/= :: forall k (i :: k) a. Eq a => Bitwise i a -> Bitwise i a -> Bool
/= :: Bitwise i a -> Bitwise i a -> Bool
Eq, Eq (Bitwise i a)
Eq (Bitwise i a) =>
(Bitwise i a -> Bitwise i a -> Ordering)
-> (Bitwise i a -> Bitwise i a -> Bool)
-> (Bitwise i a -> Bitwise i a -> Bool)
-> (Bitwise i a -> Bitwise i a -> Bool)
-> (Bitwise i a -> Bitwise i a -> Bool)
-> (Bitwise i a -> Bitwise i a -> Bitwise i a)
-> (Bitwise i a -> Bitwise i a -> Bitwise i a)
-> Ord (Bitwise i a)
Bitwise i a -> Bitwise i a -> Bool
Bitwise i a -> Bitwise i a -> Ordering
Bitwise i a -> Bitwise i a -> Bitwise i 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 k (i :: k) a. Ord a => Eq (Bitwise i a)
forall k (i :: k) a. Ord a => Bitwise i a -> Bitwise i a -> Bool
forall k (i :: k) a.
Ord a =>
Bitwise i a -> Bitwise i a -> Ordering
forall k (i :: k) a.
Ord a =>
Bitwise i a -> Bitwise i a -> Bitwise i a
$ccompare :: forall k (i :: k) a.
Ord a =>
Bitwise i a -> Bitwise i a -> Ordering
compare :: Bitwise i a -> Bitwise i a -> Ordering
$c< :: forall k (i :: k) a. Ord a => Bitwise i a -> Bitwise i a -> Bool
< :: Bitwise i a -> Bitwise i a -> Bool
$c<= :: forall k (i :: k) a. Ord a => Bitwise i a -> Bitwise i a -> Bool
<= :: Bitwise i a -> Bitwise i a -> Bool
$c> :: forall k (i :: k) a. Ord a => Bitwise i a -> Bitwise i a -> Bool
> :: Bitwise i a -> Bitwise i a -> Bool
$c>= :: forall k (i :: k) a. Ord a => Bitwise i a -> Bitwise i a -> Bool
>= :: Bitwise i a -> Bitwise i a -> Bool
$cmax :: forall k (i :: k) a.
Ord a =>
Bitwise i a -> Bitwise i a -> Bitwise i a
max :: Bitwise i a -> Bitwise i a -> Bitwise i a
$cmin :: forall k (i :: k) a.
Ord a =>
Bitwise i a -> Bitwise i a -> Bitwise i a
min :: Bitwise i a -> Bitwise i a -> Bitwise i a
Ord, Int -> Bitwise i a -> ShowS
[Bitwise i a] -> ShowS
Bitwise i a -> String
(Int -> Bitwise i a -> ShowS)
-> (Bitwise i a -> String)
-> ([Bitwise i a] -> ShowS)
-> Show (Bitwise i a)
forall a.
(Int -> a -> ShowS) -> (a -> String) -> ([a] -> ShowS) -> Show a
forall k (i :: k) a. Show a => Int -> Bitwise i a -> ShowS
forall k (i :: k) a. Show a => [Bitwise i a] -> ShowS
forall k (i :: k) a. Show a => Bitwise i a -> String
$cshowsPrec :: forall k (i :: k) a. Show a => Int -> Bitwise i a -> ShowS
showsPrec :: Int -> Bitwise i a -> ShowS
$cshow :: forall k (i :: k) a. Show a => Bitwise i a -> String
show :: Bitwise i a -> String
$cshowList :: forall k (i :: k) a. Show a => [Bitwise i a] -> ShowS
showList :: [Bitwise i a] -> ShowS
Show, ReadPrec [Bitwise i a]
ReadPrec (Bitwise i a)
Int -> ReadS (Bitwise i a)
ReadS [Bitwise i a]
(Int -> ReadS (Bitwise i a))
-> ReadS [Bitwise i a]
-> ReadPrec (Bitwise i a)
-> ReadPrec [Bitwise i a]
-> Read (Bitwise i a)
forall a.
(Int -> ReadS a)
-> ReadS [a] -> ReadPrec a -> ReadPrec [a] -> Read a
forall k (i :: k) a. Read a => ReadPrec [Bitwise i a]
forall k (i :: k) a. Read a => ReadPrec (Bitwise i a)
forall k (i :: k) a. Read a => Int -> ReadS (Bitwise i a)
forall k (i :: k) a. Read a => ReadS [Bitwise i a]
$creadsPrec :: forall k (i :: k) a. Read a => Int -> ReadS (Bitwise i a)
readsPrec :: Int -> ReadS (Bitwise i a)
$creadList :: forall k (i :: k) a. Read a => ReadS [Bitwise i a]
readList :: ReadS [Bitwise i a]
$creadPrec :: forall k (i :: k) a. Read a => ReadPrec (Bitwise i a)
readPrec :: ReadPrec (Bitwise i a)
$creadListPrec :: forall k (i :: k) a. Read a => ReadPrec [Bitwise i a]
readListPrec :: ReadPrec [Bitwise i a]
Read, Eq (Bitwise i a)
Eq (Bitwise i a) =>
(Int -> Bitwise i a -> Int)
-> (Bitwise i a -> Int) -> Hashable (Bitwise i a)
Int -> Bitwise i a -> Int
Bitwise i a -> Int
forall a. Eq a => (Int -> a -> Int) -> (a -> Int) -> Hashable a
forall k (i :: k) a. Hashable a => Eq (Bitwise i a)
forall k (i :: k) a. Hashable a => Int -> Bitwise i a -> Int
forall k (i :: k) a. Hashable a => Bitwise i a -> Int
$chashWithSalt :: forall k (i :: k) a. Hashable a => Int -> Bitwise i a -> Int
hashWithSalt :: Int -> Bitwise i a -> Int
$chash :: forall k (i :: k) a. Hashable a => Bitwise i a -> Int
hash :: Bitwise i a -> Int
Hashable, Bitwise i a -> ()
(Bitwise i a -> ()) -> NFData (Bitwise i a)
forall a. (a -> ()) -> NFData a
forall k (i :: k) a. NFData a => Bitwise i a -> ()
$crnf :: forall k (i :: k) a. NFData a => Bitwise i a -> ()
rnf :: Bitwise i a -> ()
NFData)

instance (Bitreppable i a) => Pack (Bitwise i a) where
  pack :: Bitwise i a -> Builder
pack = forall i a. Bitreppable i a => a -> Builder
tobits0 @i (a -> Builder) -> (Bitwise i a -> a) -> Bitwise i a -> Builder
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Bitwise i a -> a
forall {k} (i :: k) a. Bitwise i a -> a
unbitwise
  {-# INLINEABLE pack #-}

instance (Bitreppable i a) => Unpack (Bitwise i a) where
  unpack :: forall (st :: ZeroBitType) r. Parser st r (Bitwise i a)
unpack = a -> Bitwise i a
forall {k} (i :: k) a. a -> Bitwise i a
Bitwise (a -> Bitwise i a)
-> ParserT st r ParseError a
-> ParserT st r ParseError (Bitwise i a)
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> forall i a (st :: ZeroBitType) r. Bitreppable i a => Parser st r a
frombits0 @i
  {-# INLINEABLE unpack #-}

-- | type class for types that can be represented as bit flags.
-- provides methods for converting to and from bit representations.
--
-- most users should not need to implement this directly - just derive 'Generic'
-- for your type and declare an instance without implementations:
--
-- @
-- instance Bitreppable Word8 MyFlags
-- @
class Bitreppable i a where
  tobits0 :: a -> Builder
  default tobits0 ::
    (Generic a, GBitRep (Rep a), Integral i, Bits i, Pack i) =>
    a -> Builder
  tobits0 = i -> Builder
forall a. Pack a => a -> Builder
pack (i -> Builder) -> (a -> i) -> a -> Builder
forall b c a. (b -> c) -> (a -> b) -> a -> c
. (i, Int) -> i
forall a b. (a, b) -> a
fst ((i, Int) -> i) -> (a -> (i, Int)) -> a -> i
forall b c a. (b -> c) -> (a -> b) -> a -> c
. i -> Int -> Rep a Any -> (i, Int)
forall i p. (Bits i, Integral i) => i -> Int -> Rep a p -> (i, Int)
forall {k} (f :: k -> *) i (p :: k).
(GBitRep f, Bits i, Integral i) =>
i -> Int -> f p -> (i, Int)
tobits (i
0 :: i) Int
0 (Rep a Any -> (i, Int)) -> (a -> Rep a Any) -> a -> (i, Int)
forall b c a. (b -> c) -> (a -> b) -> a -> c
. a -> Rep a Any
forall x. a -> Rep a x
forall a x. Generic a => a -> Rep a x
from
  {-# INLINE tobits0 #-}
  frombits0 :: Parser st r a
  default frombits0 ::
    forall r st.
    (Generic a, GBitRep (Rep a), Integral i, Bits i, Unpack i) =>
    Parser st r a
  frombits0 = Rep a Any -> a
forall a x. Generic a => Rep a x -> a
forall x. Rep a x -> a
to (Rep a Any -> a) -> (i -> Rep a Any) -> i -> a
forall b c a. (b -> c) -> (a -> b) -> a -> c
. (Rep a Any, Int) -> Rep a Any
forall a b. (a, b) -> a
fst ((Rep a Any, Int) -> Rep a Any)
-> (i -> (Rep a Any, Int)) -> i -> Rep a Any
forall b c a. (b -> c) -> (a -> b) -> a -> c
. (i -> Int -> (Rep a Any, Int)) -> Int -> i -> (Rep a Any, Int)
forall a b c. (a -> b -> c) -> b -> a -> c
flip i -> Int -> (Rep a Any, Int)
forall i p. (Bits i, Integral i) => i -> Int -> (Rep a p, Int)
forall {k} (f :: k -> *) i (p :: k).
(GBitRep f, Bits i, Integral i) =>
i -> Int -> (f p, Int)
frombits Int
0 (i -> a) -> ParserT st r ParseError i -> ParserT st r ParseError a
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> forall a (st :: ZeroBitType) r. Unpack a => Parser st r a
unpack @i
  {-# INLINE frombits0 #-}

-- internal class for encoding/decoding bit flags
class GBitRep f where
  tobits ::
    (Bits i, Integral i) =>
    i -> -- accumulator
    Int -> -- current bit position
    f p ->
    (i, Int) -- new accumulator, new position
  frombits ::
    (Bits i, Integral i) =>
    -- accumulator
    i ->
    -- current bit position
    Int ->
    -- representation, new position
    (f p, Int)

instance GBitRep V1 where
  tobits :: forall i (p :: k).
(Bits i, Integral i) =>
i -> Int -> V1 p -> (i, Int)
tobits i
_ Int
_ V1 p
_ = (i
0, Int
0)
  {-# INLINE tobits #-}
  frombits :: forall i (p :: k). (Bits i, Integral i) => i -> Int -> (V1 p, Int)
frombits i
_ Int
p = (V1 p
forall a. HasCallStack => a
undefined, Int
p)
  {-# INLINE frombits #-}

instance GBitRep U1 where
  tobits :: forall i (p :: k).
(Bits i, Integral i) =>
i -> Int -> U1 p -> (i, Int)
tobits i
i Int
p U1 p
_ = (i
i, Int
p)
  {-# INLINE tobits #-}
  frombits :: forall i (p :: k). (Bits i, Integral i) => i -> Int -> (U1 p, Int)
frombits i
_ Int
p = (U1 p
forall k (p :: k). U1 p
U1, Int
p)
  {-# INLINE frombits #-}

instance GBitRep (K1 R Bool) where
  tobits :: forall i (p :: k).
(Bits i, Integral i) =>
i -> Int -> K1 R Bool p -> (i, Int)
tobits i
i Int
p (K1 Bool
True) = (i -> Int -> i
forall a. Bits a => a -> Int -> a
setBit i
i Int
p, Int
p Int -> Int -> Int
forall a. Num a => a -> a -> a
+ Int
1)
  tobits i
i Int
p (K1 Bool
False) = (i -> Int -> i
forall a. Bits a => a -> Int -> a
clearBit i
i Int
p, Int
p Int -> Int -> Int
forall a. Num a => a -> a -> a
+ Int
1)
  {-# INLINE tobits #-}
  frombits :: forall i (p :: k).
(Bits i, Integral i) =>
i -> Int -> (K1 R Bool p, Int)
frombits i
i Int
p = (Bool -> K1 R Bool p
forall k i c (p :: k). c -> K1 i c p
K1 (i -> Int -> Bool
forall a. Bits a => a -> Int -> Bool
testBit i
i Int
p), Int
p Int -> Int -> Int
forall a. Num a => a -> a -> a
+ Int
1)
  {-# INLINE frombits #-}

instance (GBitRep f) => GBitRep (M1 i c f) where
  tobits :: forall i (p :: k).
(Bits i, Integral i) =>
i -> Int -> M1 i c f p -> (i, Int)
tobits i
i Int
p (M1 f p
x) = i -> Int -> f p -> (i, Int)
forall i (p :: k).
(Bits i, Integral i) =>
i -> Int -> f p -> (i, Int)
forall {k} (f :: k -> *) i (p :: k).
(GBitRep f, Bits i, Integral i) =>
i -> Int -> f p -> (i, Int)
tobits i
i Int
p f p
x
  frombits :: forall i (p :: k).
(Bits i, Integral i) =>
i -> Int -> (M1 i c f p, Int)
frombits i
i Int
p = case i -> Int -> (f p, Int)
forall i (p :: k). (Bits i, Integral i) => i -> Int -> (f p, Int)
forall {k} (f :: k -> *) i (p :: k).
(GBitRep f, Bits i, Integral i) =>
i -> Int -> (f p, Int)
frombits i
i Int
p of
    (f p
x, Int
p') -> (f p -> M1 i c f p
forall k i (c :: Meta) (f :: k -> *) (p :: k). f p -> M1 i c f p
M1 f p
x, Int
p')

instance (GBitRep f, GBitRep g) => GBitRep (f :*: g) where
  tobits :: forall i (p :: k).
(Bits i, Integral i) =>
i -> Int -> (:*:) f g p -> (i, Int)
tobits i
i Int
p (f p
x :*: g p
y) =
    let (i
i', Int
p') = i -> Int -> f p -> (i, Int)
forall i (p :: k).
(Bits i, Integral i) =>
i -> Int -> f p -> (i, Int)
forall {k} (f :: k -> *) i (p :: k).
(GBitRep f, Bits i, Integral i) =>
i -> Int -> f p -> (i, Int)
tobits i
i Int
p f p
x
     in i -> Int -> g p -> (i, Int)
forall i (p :: k).
(Bits i, Integral i) =>
i -> Int -> g p -> (i, Int)
forall {k} (f :: k -> *) i (p :: k).
(GBitRep f, Bits i, Integral i) =>
i -> Int -> f p -> (i, Int)
tobits i
i' Int
p' g p
y
  frombits :: forall i (p :: k).
(Bits i, Integral i) =>
i -> Int -> ((:*:) f g p, Int)
frombits i
i Int
p = case i -> Int -> (f p, Int)
forall i (p :: k). (Bits i, Integral i) => i -> Int -> (f p, Int)
forall {k} (f :: k -> *) i (p :: k).
(GBitRep f, Bits i, Integral i) =>
i -> Int -> (f p, Int)
frombits i
i Int
p of
    (f p
x, Int
p') -> case i -> Int -> (g p, Int)
forall i (p :: k). (Bits i, Integral i) => i -> Int -> (g p, Int)
forall {k} (f :: k -> *) i (p :: k).
(GBitRep f, Bits i, Integral i) =>
i -> Int -> (f p, Int)
frombits i
i Int
p' of
      (g p
y, Int
p'') -> (f p
x f p -> g p -> (:*:) f g p
forall k (f :: k -> *) (g :: k -> *) (p :: k).
f p -> g p -> (:*:) f g p
:*: g p
y, Int
p'')

-- | variable-length bitset
--
-- (network representation: little-endian vector of 'Int64's)
newtype Bitset = Bitset {Bitset -> Integer
getbitset :: Integer}
  deriving stock ((forall x. Bitset -> Rep Bitset x)
-> (forall x. Rep Bitset x -> Bitset) -> Generic Bitset
forall x. Rep Bitset x -> Bitset
forall x. Bitset -> Rep Bitset x
forall a.
(forall x. a -> Rep a x) -> (forall x. Rep a x -> a) -> Generic a
$cfrom :: forall x. Bitset -> Rep Bitset x
from :: forall x. Bitset -> Rep Bitset x
$cto :: forall x. Rep Bitset x -> Bitset
to :: forall x. Rep Bitset x -> Bitset
Generic, Typeable, Typeable Bitset
Typeable Bitset =>
(forall (c :: * -> *).
 (forall d b. Data d => c (d -> b) -> d -> c b)
 -> (forall g. g -> c g) -> Bitset -> c Bitset)
-> (forall (c :: * -> *).
    (forall b r. Data b => c (b -> r) -> c r)
    -> (forall r. r -> c r) -> Constr -> c Bitset)
-> (Bitset -> Constr)
-> (Bitset -> DataType)
-> (forall (t :: * -> *) (c :: * -> *).
    Typeable t =>
    (forall d. Data d => c (t d)) -> Maybe (c Bitset))
-> (forall (t :: * -> * -> *) (c :: * -> *).
    Typeable t =>
    (forall d e. (Data d, Data e) => c (t d e)) -> Maybe (c Bitset))
-> ((forall b. Data b => b -> b) -> Bitset -> Bitset)
-> (forall r r'.
    (r -> r' -> r)
    -> r -> (forall d. Data d => d -> r') -> Bitset -> r)
-> (forall r r'.
    (r' -> r -> r)
    -> r -> (forall d. Data d => d -> r') -> Bitset -> r)
-> (forall u. (forall d. Data d => d -> u) -> Bitset -> [u])
-> (forall u. Int -> (forall d. Data d => d -> u) -> Bitset -> u)
-> (forall (m :: * -> *).
    Monad m =>
    (forall d. Data d => d -> m d) -> Bitset -> m Bitset)
-> (forall (m :: * -> *).
    MonadPlus m =>
    (forall d. Data d => d -> m d) -> Bitset -> m Bitset)
-> (forall (m :: * -> *).
    MonadPlus m =>
    (forall d. Data d => d -> m d) -> Bitset -> m Bitset)
-> Data Bitset
Bitset -> Constr
Bitset -> DataType
(forall b. Data b => b -> b) -> Bitset -> Bitset
forall a.
Typeable a =>
(forall (c :: * -> *).
 (forall d b. Data d => c (d -> b) -> d -> c b)
 -> (forall g. g -> c g) -> a -> c a)
-> (forall (c :: * -> *).
    (forall b r. Data b => c (b -> r) -> c r)
    -> (forall r. r -> c r) -> Constr -> c a)
-> (a -> Constr)
-> (a -> DataType)
-> (forall (t :: * -> *) (c :: * -> *).
    Typeable t =>
    (forall d. Data d => c (t d)) -> Maybe (c a))
-> (forall (t :: * -> * -> *) (c :: * -> *).
    Typeable t =>
    (forall d e. (Data d, Data e) => c (t d e)) -> Maybe (c a))
-> ((forall b. Data b => b -> b) -> a -> a)
-> (forall r r'.
    (r -> r' -> r) -> r -> (forall d. Data d => d -> r') -> a -> r)
-> (forall r r'.
    (r' -> r -> r) -> r -> (forall d. Data d => d -> r') -> a -> r)
-> (forall u. (forall d. Data d => d -> u) -> a -> [u])
-> (forall u. Int -> (forall d. Data d => d -> u) -> a -> u)
-> (forall (m :: * -> *).
    Monad m =>
    (forall d. Data d => d -> m d) -> a -> m a)
-> (forall (m :: * -> *).
    MonadPlus m =>
    (forall d. Data d => d -> m d) -> a -> m a)
-> (forall (m :: * -> *).
    MonadPlus m =>
    (forall d. Data d => d -> m d) -> a -> m a)
-> Data a
forall u. Int -> (forall d. Data d => d -> u) -> Bitset -> u
forall u. (forall d. Data d => d -> u) -> Bitset -> [u]
forall r r'.
(r -> r' -> r) -> r -> (forall d. Data d => d -> r') -> Bitset -> r
forall r r'.
(r' -> r -> r) -> r -> (forall d. Data d => d -> r') -> Bitset -> r
forall (m :: * -> *).
Monad m =>
(forall d. Data d => d -> m d) -> Bitset -> m Bitset
forall (m :: * -> *).
MonadPlus m =>
(forall d. Data d => d -> m d) -> Bitset -> m Bitset
forall (c :: * -> *).
(forall b r. Data b => c (b -> r) -> c r)
-> (forall r. r -> c r) -> Constr -> c Bitset
forall (c :: * -> *).
(forall d b. Data d => c (d -> b) -> d -> c b)
-> (forall g. g -> c g) -> Bitset -> c Bitset
forall (t :: * -> *) (c :: * -> *).
Typeable t =>
(forall d. Data d => c (t d)) -> Maybe (c Bitset)
forall (t :: * -> * -> *) (c :: * -> *).
Typeable t =>
(forall d e. (Data d, Data e) => c (t d e)) -> Maybe (c Bitset)
$cgfoldl :: forall (c :: * -> *).
(forall d b. Data d => c (d -> b) -> d -> c b)
-> (forall g. g -> c g) -> Bitset -> c Bitset
gfoldl :: forall (c :: * -> *).
(forall d b. Data d => c (d -> b) -> d -> c b)
-> (forall g. g -> c g) -> Bitset -> c Bitset
$cgunfold :: forall (c :: * -> *).
(forall b r. Data b => c (b -> r) -> c r)
-> (forall r. r -> c r) -> Constr -> c Bitset
gunfold :: forall (c :: * -> *).
(forall b r. Data b => c (b -> r) -> c r)
-> (forall r. r -> c r) -> Constr -> c Bitset
$ctoConstr :: Bitset -> Constr
toConstr :: Bitset -> Constr
$cdataTypeOf :: Bitset -> DataType
dataTypeOf :: Bitset -> DataType
$cdataCast1 :: forall (t :: * -> *) (c :: * -> *).
Typeable t =>
(forall d. Data d => c (t d)) -> Maybe (c Bitset)
dataCast1 :: forall (t :: * -> *) (c :: * -> *).
Typeable t =>
(forall d. Data d => c (t d)) -> Maybe (c Bitset)
$cdataCast2 :: forall (t :: * -> * -> *) (c :: * -> *).
Typeable t =>
(forall d e. (Data d, Data e) => c (t d e)) -> Maybe (c Bitset)
dataCast2 :: forall (t :: * -> * -> *) (c :: * -> *).
Typeable t =>
(forall d e. (Data d, Data e) => c (t d e)) -> Maybe (c Bitset)
$cgmapT :: (forall b. Data b => b -> b) -> Bitset -> Bitset
gmapT :: (forall b. Data b => b -> b) -> Bitset -> Bitset
$cgmapQl :: forall r r'.
(r -> r' -> r) -> r -> (forall d. Data d => d -> r') -> Bitset -> r
gmapQl :: forall r r'.
(r -> r' -> r) -> r -> (forall d. Data d => d -> r') -> Bitset -> r
$cgmapQr :: forall r r'.
(r' -> r -> r) -> r -> (forall d. Data d => d -> r') -> Bitset -> r
gmapQr :: forall r r'.
(r' -> r -> r) -> r -> (forall d. Data d => d -> r') -> Bitset -> r
$cgmapQ :: forall u. (forall d. Data d => d -> u) -> Bitset -> [u]
gmapQ :: forall u. (forall d. Data d => d -> u) -> Bitset -> [u]
$cgmapQi :: forall u. Int -> (forall d. Data d => d -> u) -> Bitset -> u
gmapQi :: forall u. Int -> (forall d. Data d => d -> u) -> Bitset -> u
$cgmapM :: forall (m :: * -> *).
Monad m =>
(forall d. Data d => d -> m d) -> Bitset -> m Bitset
gmapM :: forall (m :: * -> *).
Monad m =>
(forall d. Data d => d -> m d) -> Bitset -> m Bitset
$cgmapMp :: forall (m :: * -> *).
MonadPlus m =>
(forall d. Data d => d -> m d) -> Bitset -> m Bitset
gmapMp :: forall (m :: * -> *).
MonadPlus m =>
(forall d. Data d => d -> m d) -> Bitset -> m Bitset
$cgmapMo :: forall (m :: * -> *).
MonadPlus m =>
(forall d. Data d => d -> m d) -> Bitset -> m Bitset
gmapMo :: forall (m :: * -> *).
MonadPlus m =>
(forall d. Data d => d -> m d) -> Bitset -> m Bitset
Data, (forall (m :: * -> *). Quote m => Bitset -> m Exp)
-> (forall (m :: * -> *). Quote m => Bitset -> Code m Bitset)
-> Lift Bitset
forall t.
(forall (m :: * -> *). Quote m => t -> m Exp)
-> (forall (m :: * -> *). Quote m => t -> Code m t) -> Lift t
forall (m :: * -> *). Quote m => Bitset -> m Exp
forall (m :: * -> *). Quote m => Bitset -> Code m Bitset
$clift :: forall (m :: * -> *). Quote m => Bitset -> m Exp
lift :: forall (m :: * -> *). Quote m => Bitset -> m Exp
$cliftTyped :: forall (m :: * -> *). Quote m => Bitset -> Code m Bitset
liftTyped :: forall (m :: * -> *). Quote m => Bitset -> Code m Bitset
Lift)
  deriving newtype (Bitset -> Bitset -> Bool
(Bitset -> Bitset -> Bool)
-> (Bitset -> Bitset -> Bool) -> Eq Bitset
forall a. (a -> a -> Bool) -> (a -> a -> Bool) -> Eq a
$c== :: Bitset -> Bitset -> Bool
== :: Bitset -> Bitset -> Bool
$c/= :: Bitset -> Bitset -> Bool
/= :: Bitset -> Bitset -> Bool
Eq, Eq Bitset
Eq Bitset =>
(Bitset -> Bitset -> Ordering)
-> (Bitset -> Bitset -> Bool)
-> (Bitset -> Bitset -> Bool)
-> (Bitset -> Bitset -> Bool)
-> (Bitset -> Bitset -> Bool)
-> (Bitset -> Bitset -> Bitset)
-> (Bitset -> Bitset -> Bitset)
-> Ord Bitset
Bitset -> Bitset -> Bool
Bitset -> Bitset -> Ordering
Bitset -> Bitset -> Bitset
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
$ccompare :: Bitset -> Bitset -> Ordering
compare :: Bitset -> Bitset -> Ordering
$c< :: Bitset -> Bitset -> Bool
< :: Bitset -> Bitset -> Bool
$c<= :: Bitset -> Bitset -> Bool
<= :: Bitset -> Bitset -> Bool
$c> :: Bitset -> Bitset -> Bool
> :: Bitset -> Bitset -> Bool
$c>= :: Bitset -> Bitset -> Bool
>= :: Bitset -> Bitset -> Bool
$cmax :: Bitset -> Bitset -> Bitset
max :: Bitset -> Bitset -> Bitset
$cmin :: Bitset -> Bitset -> Bitset
min :: Bitset -> Bitset -> Bitset
Ord, Int -> Bitset -> ShowS
[Bitset] -> ShowS
Bitset -> String
(Int -> Bitset -> ShowS)
-> (Bitset -> String) -> ([Bitset] -> ShowS) -> Show Bitset
forall a.
(Int -> a -> ShowS) -> (a -> String) -> ([a] -> ShowS) -> Show a
$cshowsPrec :: Int -> Bitset -> ShowS
showsPrec :: Int -> Bitset -> ShowS
$cshow :: Bitset -> String
show :: Bitset -> String
$cshowList :: [Bitset] -> ShowS
showList :: [Bitset] -> ShowS
Show, ReadPrec [Bitset]
ReadPrec Bitset
Int -> ReadS Bitset
ReadS [Bitset]
(Int -> ReadS Bitset)
-> ReadS [Bitset]
-> ReadPrec Bitset
-> ReadPrec [Bitset]
-> Read Bitset
forall a.
(Int -> ReadS a)
-> ReadS [a] -> ReadPrec a -> ReadPrec [a] -> Read a
$creadsPrec :: Int -> ReadS Bitset
readsPrec :: Int -> ReadS Bitset
$creadList :: ReadS [Bitset]
readList :: ReadS [Bitset]
$creadPrec :: ReadPrec Bitset
readPrec :: ReadPrec Bitset
$creadListPrec :: ReadPrec [Bitset]
readListPrec :: ReadPrec [Bitset]
Read, Eq Bitset
Eq Bitset =>
(Int -> Bitset -> Int) -> (Bitset -> Int) -> Hashable Bitset
Int -> Bitset -> Int
Bitset -> Int
forall a. Eq a => (Int -> a -> Int) -> (a -> Int) -> Hashable a
$chashWithSalt :: Int -> Bitset -> Int
hashWithSalt :: Int -> Bitset -> Int
$chash :: Bitset -> Int
hash :: Bitset -> Int
Hashable, Bitset -> ()
(Bitset -> ()) -> NFData Bitset
forall a. (a -> ()) -> NFData a
$crnf :: Bitset -> ()
rnf :: Bitset -> ()
NFData)
  deriving newtype (Int -> Bitset
Bitset -> Int
Bitset -> [Bitset]
Bitset -> Bitset
Bitset -> Bitset -> [Bitset]
Bitset -> Bitset -> Bitset -> [Bitset]
(Bitset -> Bitset)
-> (Bitset -> Bitset)
-> (Int -> Bitset)
-> (Bitset -> Int)
-> (Bitset -> [Bitset])
-> (Bitset -> Bitset -> [Bitset])
-> (Bitset -> Bitset -> [Bitset])
-> (Bitset -> Bitset -> Bitset -> [Bitset])
-> Enum Bitset
forall a.
(a -> a)
-> (a -> a)
-> (Int -> a)
-> (a -> Int)
-> (a -> [a])
-> (a -> a -> [a])
-> (a -> a -> [a])
-> (a -> a -> a -> [a])
-> Enum a
$csucc :: Bitset -> Bitset
succ :: Bitset -> Bitset
$cpred :: Bitset -> Bitset
pred :: Bitset -> Bitset
$ctoEnum :: Int -> Bitset
toEnum :: Int -> Bitset
$cfromEnum :: Bitset -> Int
fromEnum :: Bitset -> Int
$cenumFrom :: Bitset -> [Bitset]
enumFrom :: Bitset -> [Bitset]
$cenumFromThen :: Bitset -> Bitset -> [Bitset]
enumFromThen :: Bitset -> Bitset -> [Bitset]
$cenumFromTo :: Bitset -> Bitset -> [Bitset]
enumFromTo :: Bitset -> Bitset -> [Bitset]
$cenumFromThenTo :: Bitset -> Bitset -> Bitset -> [Bitset]
enumFromThenTo :: Bitset -> Bitset -> Bitset -> [Bitset]
Enum, Integer -> Bitset
Bitset -> Bitset
Bitset -> Bitset -> Bitset
(Bitset -> Bitset -> Bitset)
-> (Bitset -> Bitset -> Bitset)
-> (Bitset -> Bitset -> Bitset)
-> (Bitset -> Bitset)
-> (Bitset -> Bitset)
-> (Bitset -> Bitset)
-> (Integer -> Bitset)
-> Num Bitset
forall a.
(a -> a -> a)
-> (a -> a -> a)
-> (a -> a -> a)
-> (a -> a)
-> (a -> a)
-> (a -> a)
-> (Integer -> a)
-> Num a
$c+ :: Bitset -> Bitset -> Bitset
+ :: Bitset -> Bitset -> Bitset
$c- :: Bitset -> Bitset -> Bitset
- :: Bitset -> Bitset -> Bitset
$c* :: Bitset -> Bitset -> Bitset
* :: Bitset -> Bitset -> Bitset
$cnegate :: Bitset -> Bitset
negate :: Bitset -> Bitset
$cabs :: Bitset -> Bitset
abs :: Bitset -> Bitset
$csignum :: Bitset -> Bitset
signum :: Bitset -> Bitset
$cfromInteger :: Integer -> Bitset
fromInteger :: Integer -> Bitset
Num, Num Bitset
Ord Bitset
(Num Bitset, Ord Bitset) => (Bitset -> Rational) -> Real Bitset
Bitset -> Rational
forall a. (Num a, Ord a) => (a -> Rational) -> Real a
$ctoRational :: Bitset -> Rational
toRational :: Bitset -> Rational
Real, Eq Bitset
Bitset
Eq Bitset =>
(Bitset -> Bitset -> Bitset)
-> (Bitset -> Bitset -> Bitset)
-> (Bitset -> Bitset -> Bitset)
-> (Bitset -> Bitset)
-> (Bitset -> Int -> Bitset)
-> (Bitset -> Int -> Bitset)
-> Bitset
-> (Int -> Bitset)
-> (Bitset -> Int -> Bitset)
-> (Bitset -> Int -> Bitset)
-> (Bitset -> Int -> Bitset)
-> (Bitset -> Int -> Bool)
-> (Bitset -> Maybe Int)
-> (Bitset -> Int)
-> (Bitset -> Bool)
-> (Bitset -> Int -> Bitset)
-> (Bitset -> Int -> Bitset)
-> (Bitset -> Int -> Bitset)
-> (Bitset -> Int -> Bitset)
-> (Bitset -> Int -> Bitset)
-> (Bitset -> Int -> Bitset)
-> (Bitset -> Int)
-> Bits Bitset
Int -> Bitset
Bitset -> Bool
Bitset -> Int
Bitset -> Maybe Int
Bitset -> Bitset
Bitset -> Int -> Bool
Bitset -> Int -> Bitset
Bitset -> Bitset -> Bitset
forall a.
Eq a =>
(a -> a -> a)
-> (a -> a -> a)
-> (a -> a -> a)
-> (a -> a)
-> (a -> Int -> a)
-> (a -> Int -> a)
-> a
-> (Int -> a)
-> (a -> Int -> a)
-> (a -> Int -> a)
-> (a -> Int -> a)
-> (a -> Int -> Bool)
-> (a -> Maybe Int)
-> (a -> Int)
-> (a -> Bool)
-> (a -> Int -> a)
-> (a -> Int -> a)
-> (a -> Int -> a)
-> (a -> Int -> a)
-> (a -> Int -> a)
-> (a -> Int -> a)
-> (a -> Int)
-> Bits a
$c.&. :: Bitset -> Bitset -> Bitset
.&. :: Bitset -> Bitset -> Bitset
$c.|. :: Bitset -> Bitset -> Bitset
.|. :: Bitset -> Bitset -> Bitset
$cxor :: Bitset -> Bitset -> Bitset
xor :: Bitset -> Bitset -> Bitset
$ccomplement :: Bitset -> Bitset
complement :: Bitset -> Bitset
$cshift :: Bitset -> Int -> Bitset
shift :: Bitset -> Int -> Bitset
$crotate :: Bitset -> Int -> Bitset
rotate :: Bitset -> Int -> Bitset
$czeroBits :: Bitset
zeroBits :: Bitset
$cbit :: Int -> Bitset
bit :: Int -> Bitset
$csetBit :: Bitset -> Int -> Bitset
setBit :: Bitset -> Int -> Bitset
$cclearBit :: Bitset -> Int -> Bitset
clearBit :: Bitset -> Int -> Bitset
$ccomplementBit :: Bitset -> Int -> Bitset
complementBit :: Bitset -> Int -> Bitset
$ctestBit :: Bitset -> Int -> Bool
testBit :: Bitset -> Int -> Bool
$cbitSizeMaybe :: Bitset -> Maybe Int
bitSizeMaybe :: Bitset -> Maybe Int
$cbitSize :: Bitset -> Int
bitSize :: Bitset -> Int
$cisSigned :: Bitset -> Bool
isSigned :: Bitset -> Bool
$cshiftL :: Bitset -> Int -> Bitset
shiftL :: Bitset -> Int -> Bitset
$cunsafeShiftL :: Bitset -> Int -> Bitset
unsafeShiftL :: Bitset -> Int -> Bitset
$cshiftR :: Bitset -> Int -> Bitset
shiftR :: Bitset -> Int -> Bitset
$cunsafeShiftR :: Bitset -> Int -> Bitset
unsafeShiftR :: Bitset -> Int -> Bitset
$crotateL :: Bitset -> Int -> Bitset
rotateL :: Bitset -> Int -> Bitset
$crotateR :: Bitset -> Int -> Bitset
rotateR :: Bitset -> Int -> Bitset
$cpopCount :: Bitset -> Int
popCount :: Bitset -> Int
Bits)

instance Pack Bitset where
  pack :: Bitset -> Builder
pack = Vector Int8 -> Builder
forall a. Pack a => a -> Builder
pack (Vector Int8 -> Builder)
-> (Bitset -> Vector Int8) -> Bitset -> Builder
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Bitset -> Vector Int8
bitsettovui64
  {-# INLINE pack #-}

instance Unpack Bitset where
  unpack :: forall (st :: ZeroBitType) r. Parser st r Bitset
unpack = Vector Int8 -> Bitset
bitsetfromvui64 (Vector Int8 -> Bitset)
-> ParserT st r ParseError (Vector Int8)
-> ParserT st r ParseError Bitset
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> ParserT st r ParseError (Vector Int8)
forall (st :: ZeroBitType) r. Parser st r (Vector Int8)
forall a (st :: ZeroBitType) r. Unpack a => Parser st r a
unpack
  {-# INLINE unpack #-}

-- sort of a naive implementation, but it's fine for our purposes
--
-- actually returns the highest bit location + 1
highestbitloc :: Integer -> Int
highestbitloc :: Integer -> Int
highestbitloc Integer
x | Integer
x Integer -> Integer -> Bool
forall a. Ord a => a -> a -> Bool
< Integer
0 = String -> Int
forall a. HasCallStack => String -> a
error String
"highestbitloc: negative argument"
highestbitloc Integer
0 = Int
0
highestbitloc Integer
x = Int
1 Int -> Int -> Int
forall a. Num a => a -> a -> a
+ Integer -> Int
highestbitloc (Integer
x Integer -> Int -> Integer
forall a. Bits a => a -> Int -> a
.>>. Int
1)

bitsettovuin :: Int -> Bitset -> VU.Vector Int8
bitsettovuin :: Int -> Bitset -> Vector Int8
bitsettovuin Int
n (Bitset Integer
x) = Int -> [Int8] -> Vector Int8
forall a. Unbox a => Int -> [a] -> Vector a
VU.fromListN (Int
m Int -> Int -> Int
forall a. Num a => a -> a -> a
+ Int
1) (Integer -> [Int8]
go Integer
x)
  where
    m :: Int
m = (Integer -> Int
highestbitloc Integer
x Int -> Int -> Int
forall a. Num a => a -> a -> a
+ Int
n Int -> Int -> Int
forall a. Num a => a -> a -> a
- Int
1) Int -> Int -> Int
forall a. Integral a => a -> a -> a
`div` Int
n
    go :: Integer -> [Int8]
go Integer
0 = []
    go Integer
y = Integer -> Int8
forall a b. (Integral a, Num b) => a -> b
fromIntegral Integer
y Int8 -> [Int8] -> [Int8]
forall a. a -> [a] -> [a]
: Integer -> [Int8]
go (Integer
y Integer -> Int -> Integer
forall a. Bits a => a -> Int -> a
.>>. Int
n)
{-# INLINE bitsettovuin #-}

bitsetfromvuin :: Int -> VU.Vector Int8 -> Bitset
bitsetfromvuin :: Int -> Vector Int8 -> Bitset
bitsetfromvuin Int
n = Integer -> Bitset
Bitset (Integer -> Bitset)
-> (Vector Int8 -> Integer) -> Vector Int8 -> Bitset
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Integer -> Int -> Vector Int8 -> Integer
go Integer
0 Int
0
  where
    go :: Integer -> Int -> Vector Int8 -> Integer
go Integer
a Int
s Vector Int8
v =
      case Vector Int8 -> Maybe (Int8, Vector Int8)
forall a. Unbox a => Vector a -> Maybe (a, Vector a)
VU.uncons Vector Int8
v of
        Maybe (Int8, Vector Int8)
Nothing -> Integer
a
        Just (Int8
x, Vector Int8
xs) -> Integer -> Int -> Vector Int8 -> Integer
go (Integer
a Integer -> Integer -> Integer
forall a. Bits a => a -> a -> a
.|. (Int8 -> Integer
forall a b. (Integral a, Num b) => a -> b
fromIntegral Int8
x Integer -> Int -> Integer
forall a. Bits a => a -> Int -> a
.<<. Int
s)) (Int
s Int -> Int -> Int
forall a. Num a => a -> a -> a
+ Int
n) Vector Int8
xs

bitsettovui64 :: Bitset -> VU.Vector Int8
bitsettovui64 :: Bitset -> Vector Int8
bitsettovui64 = Int -> Bitset -> Vector Int8
bitsettovuin Int
64

bitsetfromvui64 :: VU.Vector Int8 -> Bitset
bitsetfromvui64 :: Vector Int8 -> Bitset
bitsetfromvui64 = Int -> Vector Int8 -> Bitset
bitsetfromvuin Int
64

bitsettovui8 :: Bitset -> VU.Vector Int8
bitsettovui8 :: Bitset -> Vector Int8
bitsettovui8 = Int -> Bitset -> Vector Int8
bitsettovuin Int
8

bitsetfromvui8 :: VU.Vector Int8 -> Bitset
bitsetfromvui8 :: Vector Int8 -> Bitset
bitsetfromvui8 = Int -> Vector Int8 -> Bitset
bitsetfromvuin Int
8

-- | a fixed-size bitset with @i@ bits
--
-- (implemented identically to 'Bitset'; only difference is that
-- when ser/de occurs, it pads missing bits with zeroes. hence it is
-- also possible to access out-of-bounds bits, and these bits will
-- get silently truncated when ser/de occurs)
newtype FixedBitset i = FixedBitset {forall {k} (i :: k). FixedBitset i -> Integer
getfixedbitset :: Integer}
  deriving stock ((forall x. FixedBitset i -> Rep (FixedBitset i) x)
-> (forall x. Rep (FixedBitset i) x -> FixedBitset i)
-> Generic (FixedBitset i)
forall x. Rep (FixedBitset i) x -> FixedBitset i
forall x. FixedBitset i -> Rep (FixedBitset i) x
forall a.
(forall x. a -> Rep a x) -> (forall x. Rep a x -> a) -> Generic a
forall k (i :: k) x. Rep (FixedBitset i) x -> FixedBitset i
forall k (i :: k) x. FixedBitset i -> Rep (FixedBitset i) x
$cfrom :: forall k (i :: k) x. FixedBitset i -> Rep (FixedBitset i) x
from :: forall x. FixedBitset i -> Rep (FixedBitset i) x
$cto :: forall k (i :: k) x. Rep (FixedBitset i) x -> FixedBitset i
to :: forall x. Rep (FixedBitset i) x -> FixedBitset i
Generic, Typeable, Typeable (FixedBitset i)
Typeable (FixedBitset i) =>
(forall (c :: * -> *).
 (forall d b. Data d => c (d -> b) -> d -> c b)
 -> (forall g. g -> c g) -> FixedBitset i -> c (FixedBitset i))
-> (forall (c :: * -> *).
    (forall b r. Data b => c (b -> r) -> c r)
    -> (forall r. r -> c r) -> Constr -> c (FixedBitset i))
-> (FixedBitset i -> Constr)
-> (FixedBitset i -> DataType)
-> (forall (t :: * -> *) (c :: * -> *).
    Typeable t =>
    (forall d. Data d => c (t d)) -> Maybe (c (FixedBitset i)))
-> (forall (t :: * -> * -> *) (c :: * -> *).
    Typeable t =>
    (forall d e. (Data d, Data e) => c (t d e))
    -> Maybe (c (FixedBitset i)))
-> ((forall b. Data b => b -> b) -> FixedBitset i -> FixedBitset i)
-> (forall r r'.
    (r -> r' -> r)
    -> r -> (forall d. Data d => d -> r') -> FixedBitset i -> r)
-> (forall r r'.
    (r' -> r -> r)
    -> r -> (forall d. Data d => d -> r') -> FixedBitset i -> r)
-> (forall u. (forall d. Data d => d -> u) -> FixedBitset i -> [u])
-> (forall u.
    Int -> (forall d. Data d => d -> u) -> FixedBitset i -> u)
-> (forall (m :: * -> *).
    Monad m =>
    (forall d. Data d => d -> m d)
    -> FixedBitset i -> m (FixedBitset i))
-> (forall (m :: * -> *).
    MonadPlus m =>
    (forall d. Data d => d -> m d)
    -> FixedBitset i -> m (FixedBitset i))
-> (forall (m :: * -> *).
    MonadPlus m =>
    (forall d. Data d => d -> m d)
    -> FixedBitset i -> m (FixedBitset i))
-> Data (FixedBitset i)
FixedBitset i -> Constr
FixedBitset i -> DataType
(forall b. Data b => b -> b) -> FixedBitset i -> FixedBitset i
forall a.
Typeable a =>
(forall (c :: * -> *).
 (forall d b. Data d => c (d -> b) -> d -> c b)
 -> (forall g. g -> c g) -> a -> c a)
-> (forall (c :: * -> *).
    (forall b r. Data b => c (b -> r) -> c r)
    -> (forall r. r -> c r) -> Constr -> c a)
-> (a -> Constr)
-> (a -> DataType)
-> (forall (t :: * -> *) (c :: * -> *).
    Typeable t =>
    (forall d. Data d => c (t d)) -> Maybe (c a))
-> (forall (t :: * -> * -> *) (c :: * -> *).
    Typeable t =>
    (forall d e. (Data d, Data e) => c (t d e)) -> Maybe (c a))
-> ((forall b. Data b => b -> b) -> a -> a)
-> (forall r r'.
    (r -> r' -> r) -> r -> (forall d. Data d => d -> r') -> a -> r)
-> (forall r r'.
    (r' -> r -> r) -> r -> (forall d. Data d => d -> r') -> a -> r)
-> (forall u. (forall d. Data d => d -> u) -> a -> [u])
-> (forall u. Int -> (forall d. Data d => d -> u) -> a -> u)
-> (forall (m :: * -> *).
    Monad m =>
    (forall d. Data d => d -> m d) -> a -> m a)
-> (forall (m :: * -> *).
    MonadPlus m =>
    (forall d. Data d => d -> m d) -> a -> m a)
-> (forall (m :: * -> *).
    MonadPlus m =>
    (forall d. Data d => d -> m d) -> a -> m a)
-> Data a
forall u. Int -> (forall d. Data d => d -> u) -> FixedBitset i -> u
forall u. (forall d. Data d => d -> u) -> FixedBitset i -> [u]
forall k (i :: k).
(Typeable i, Typeable k) =>
Typeable (FixedBitset i)
forall k (i :: k).
(Typeable i, Typeable k) =>
FixedBitset i -> Constr
forall k (i :: k).
(Typeable i, Typeable k) =>
FixedBitset i -> DataType
forall k (i :: k).
(Typeable i, Typeable k) =>
(forall b. Data b => b -> b) -> FixedBitset i -> FixedBitset i
forall k (i :: k) u.
(Typeable i, Typeable k) =>
Int -> (forall d. Data d => d -> u) -> FixedBitset i -> u
forall k (i :: k) u.
(Typeable i, Typeable k) =>
(forall d. Data d => d -> u) -> FixedBitset i -> [u]
forall k (i :: k) r r'.
(Typeable i, Typeable k) =>
(r -> r' -> r)
-> r -> (forall d. Data d => d -> r') -> FixedBitset i -> r
forall k (i :: k) r r'.
(Typeable i, Typeable k) =>
(r' -> r -> r)
-> r -> (forall d. Data d => d -> r') -> FixedBitset i -> r
forall k (i :: k) (m :: * -> *).
(Typeable i, Typeable k, Monad m) =>
(forall d. Data d => d -> m d)
-> FixedBitset i -> m (FixedBitset i)
forall k (i :: k) (m :: * -> *).
(Typeable i, Typeable k, MonadPlus m) =>
(forall d. Data d => d -> m d)
-> FixedBitset i -> m (FixedBitset i)
forall k (i :: k) (c :: * -> *).
(Typeable i, Typeable k) =>
(forall b r. Data b => c (b -> r) -> c r)
-> (forall r. r -> c r) -> Constr -> c (FixedBitset i)
forall k (i :: k) (c :: * -> *).
(Typeable i, Typeable k) =>
(forall d b. Data d => c (d -> b) -> d -> c b)
-> (forall g. g -> c g) -> FixedBitset i -> c (FixedBitset i)
forall k (i :: k) (t :: * -> *) (c :: * -> *).
(Typeable i, Typeable k, Typeable t) =>
(forall d. Data d => c (t d)) -> Maybe (c (FixedBitset i))
forall k (i :: k) (t :: * -> * -> *) (c :: * -> *).
(Typeable i, Typeable k, Typeable t) =>
(forall d e. (Data d, Data e) => c (t d e))
-> Maybe (c (FixedBitset i))
forall r r'.
(r -> r' -> r)
-> r -> (forall d. Data d => d -> r') -> FixedBitset i -> r
forall r r'.
(r' -> r -> r)
-> r -> (forall d. Data d => d -> r') -> FixedBitset i -> r
forall (m :: * -> *).
Monad m =>
(forall d. Data d => d -> m d)
-> FixedBitset i -> m (FixedBitset i)
forall (m :: * -> *).
MonadPlus m =>
(forall d. Data d => d -> m d)
-> FixedBitset i -> m (FixedBitset i)
forall (c :: * -> *).
(forall b r. Data b => c (b -> r) -> c r)
-> (forall r. r -> c r) -> Constr -> c (FixedBitset i)
forall (c :: * -> *).
(forall d b. Data d => c (d -> b) -> d -> c b)
-> (forall g. g -> c g) -> FixedBitset i -> c (FixedBitset i)
forall (t :: * -> *) (c :: * -> *).
Typeable t =>
(forall d. Data d => c (t d)) -> Maybe (c (FixedBitset i))
forall (t :: * -> * -> *) (c :: * -> *).
Typeable t =>
(forall d e. (Data d, Data e) => c (t d e))
-> Maybe (c (FixedBitset i))
$cgfoldl :: forall k (i :: k) (c :: * -> *).
(Typeable i, Typeable k) =>
(forall d b. Data d => c (d -> b) -> d -> c b)
-> (forall g. g -> c g) -> FixedBitset i -> c (FixedBitset i)
gfoldl :: forall (c :: * -> *).
(forall d b. Data d => c (d -> b) -> d -> c b)
-> (forall g. g -> c g) -> FixedBitset i -> c (FixedBitset i)
$cgunfold :: forall k (i :: k) (c :: * -> *).
(Typeable i, Typeable k) =>
(forall b r. Data b => c (b -> r) -> c r)
-> (forall r. r -> c r) -> Constr -> c (FixedBitset i)
gunfold :: forall (c :: * -> *).
(forall b r. Data b => c (b -> r) -> c r)
-> (forall r. r -> c r) -> Constr -> c (FixedBitset i)
$ctoConstr :: forall k (i :: k).
(Typeable i, Typeable k) =>
FixedBitset i -> Constr
toConstr :: FixedBitset i -> Constr
$cdataTypeOf :: forall k (i :: k).
(Typeable i, Typeable k) =>
FixedBitset i -> DataType
dataTypeOf :: FixedBitset i -> DataType
$cdataCast1 :: forall k (i :: k) (t :: * -> *) (c :: * -> *).
(Typeable i, Typeable k, Typeable t) =>
(forall d. Data d => c (t d)) -> Maybe (c (FixedBitset i))
dataCast1 :: forall (t :: * -> *) (c :: * -> *).
Typeable t =>
(forall d. Data d => c (t d)) -> Maybe (c (FixedBitset i))
$cdataCast2 :: forall k (i :: k) (t :: * -> * -> *) (c :: * -> *).
(Typeable i, Typeable k, Typeable t) =>
(forall d e. (Data d, Data e) => c (t d e))
-> Maybe (c (FixedBitset i))
dataCast2 :: forall (t :: * -> * -> *) (c :: * -> *).
Typeable t =>
(forall d e. (Data d, Data e) => c (t d e))
-> Maybe (c (FixedBitset i))
$cgmapT :: forall k (i :: k).
(Typeable i, Typeable k) =>
(forall b. Data b => b -> b) -> FixedBitset i -> FixedBitset i
gmapT :: (forall b. Data b => b -> b) -> FixedBitset i -> FixedBitset i
$cgmapQl :: forall k (i :: k) r r'.
(Typeable i, Typeable k) =>
(r -> r' -> r)
-> r -> (forall d. Data d => d -> r') -> FixedBitset i -> r
gmapQl :: forall r r'.
(r -> r' -> r)
-> r -> (forall d. Data d => d -> r') -> FixedBitset i -> r
$cgmapQr :: forall k (i :: k) r r'.
(Typeable i, Typeable k) =>
(r' -> r -> r)
-> r -> (forall d. Data d => d -> r') -> FixedBitset i -> r
gmapQr :: forall r r'.
(r' -> r -> r)
-> r -> (forall d. Data d => d -> r') -> FixedBitset i -> r
$cgmapQ :: forall k (i :: k) u.
(Typeable i, Typeable k) =>
(forall d. Data d => d -> u) -> FixedBitset i -> [u]
gmapQ :: forall u. (forall d. Data d => d -> u) -> FixedBitset i -> [u]
$cgmapQi :: forall k (i :: k) u.
(Typeable i, Typeable k) =>
Int -> (forall d. Data d => d -> u) -> FixedBitset i -> u
gmapQi :: forall u. Int -> (forall d. Data d => d -> u) -> FixedBitset i -> u
$cgmapM :: forall k (i :: k) (m :: * -> *).
(Typeable i, Typeable k, Monad m) =>
(forall d. Data d => d -> m d)
-> FixedBitset i -> m (FixedBitset i)
gmapM :: forall (m :: * -> *).
Monad m =>
(forall d. Data d => d -> m d)
-> FixedBitset i -> m (FixedBitset i)
$cgmapMp :: forall k (i :: k) (m :: * -> *).
(Typeable i, Typeable k, MonadPlus m) =>
(forall d. Data d => d -> m d)
-> FixedBitset i -> m (FixedBitset i)
gmapMp :: forall (m :: * -> *).
MonadPlus m =>
(forall d. Data d => d -> m d)
-> FixedBitset i -> m (FixedBitset i)
$cgmapMo :: forall k (i :: k) (m :: * -> *).
(Typeable i, Typeable k, MonadPlus m) =>
(forall d. Data d => d -> m d)
-> FixedBitset i -> m (FixedBitset i)
gmapMo :: forall (m :: * -> *).
MonadPlus m =>
(forall d. Data d => d -> m d)
-> FixedBitset i -> m (FixedBitset i)
Data, (forall (m :: * -> *). Quote m => FixedBitset i -> m Exp)
-> (forall (m :: * -> *).
    Quote m =>
    FixedBitset i -> Code m (FixedBitset i))
-> Lift (FixedBitset i)
forall k (i :: k) (m :: * -> *). Quote m => FixedBitset i -> m Exp
forall k (i :: k) (m :: * -> *).
Quote m =>
FixedBitset i -> Code m (FixedBitset i)
forall t.
(forall (m :: * -> *). Quote m => t -> m Exp)
-> (forall (m :: * -> *). Quote m => t -> Code m t) -> Lift t
forall (m :: * -> *). Quote m => FixedBitset i -> m Exp
forall (m :: * -> *).
Quote m =>
FixedBitset i -> Code m (FixedBitset i)
$clift :: forall k (i :: k) (m :: * -> *). Quote m => FixedBitset i -> m Exp
lift :: forall (m :: * -> *). Quote m => FixedBitset i -> m Exp
$cliftTyped :: forall k (i :: k) (m :: * -> *).
Quote m =>
FixedBitset i -> Code m (FixedBitset i)
liftTyped :: forall (m :: * -> *).
Quote m =>
FixedBitset i -> Code m (FixedBitset i)
Lift)
  deriving newtype (FixedBitset i -> FixedBitset i -> Bool
(FixedBitset i -> FixedBitset i -> Bool)
-> (FixedBitset i -> FixedBitset i -> Bool) -> Eq (FixedBitset i)
forall a. (a -> a -> Bool) -> (a -> a -> Bool) -> Eq a
forall k (i :: k). FixedBitset i -> FixedBitset i -> Bool
$c== :: forall k (i :: k). FixedBitset i -> FixedBitset i -> Bool
== :: FixedBitset i -> FixedBitset i -> Bool
$c/= :: forall k (i :: k). FixedBitset i -> FixedBitset i -> Bool
/= :: FixedBitset i -> FixedBitset i -> Bool
Eq, Eq (FixedBitset i)
Eq (FixedBitset i) =>
(FixedBitset i -> FixedBitset i -> Ordering)
-> (FixedBitset i -> FixedBitset i -> Bool)
-> (FixedBitset i -> FixedBitset i -> Bool)
-> (FixedBitset i -> FixedBitset i -> Bool)
-> (FixedBitset i -> FixedBitset i -> Bool)
-> (FixedBitset i -> FixedBitset i -> FixedBitset i)
-> (FixedBitset i -> FixedBitset i -> FixedBitset i)
-> Ord (FixedBitset i)
FixedBitset i -> FixedBitset i -> Bool
FixedBitset i -> FixedBitset i -> Ordering
FixedBitset i -> FixedBitset i -> FixedBitset i
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 k (i :: k). Eq (FixedBitset i)
forall k (i :: k). FixedBitset i -> FixedBitset i -> Bool
forall k (i :: k). FixedBitset i -> FixedBitset i -> Ordering
forall k (i :: k). FixedBitset i -> FixedBitset i -> FixedBitset i
$ccompare :: forall k (i :: k). FixedBitset i -> FixedBitset i -> Ordering
compare :: FixedBitset i -> FixedBitset i -> Ordering
$c< :: forall k (i :: k). FixedBitset i -> FixedBitset i -> Bool
< :: FixedBitset i -> FixedBitset i -> Bool
$c<= :: forall k (i :: k). FixedBitset i -> FixedBitset i -> Bool
<= :: FixedBitset i -> FixedBitset i -> Bool
$c> :: forall k (i :: k). FixedBitset i -> FixedBitset i -> Bool
> :: FixedBitset i -> FixedBitset i -> Bool
$c>= :: forall k (i :: k). FixedBitset i -> FixedBitset i -> Bool
>= :: FixedBitset i -> FixedBitset i -> Bool
$cmax :: forall k (i :: k). FixedBitset i -> FixedBitset i -> FixedBitset i
max :: FixedBitset i -> FixedBitset i -> FixedBitset i
$cmin :: forall k (i :: k). FixedBitset i -> FixedBitset i -> FixedBitset i
min :: FixedBitset i -> FixedBitset i -> FixedBitset i
Ord, Int -> FixedBitset i -> ShowS
[FixedBitset i] -> ShowS
FixedBitset i -> String
(Int -> FixedBitset i -> ShowS)
-> (FixedBitset i -> String)
-> ([FixedBitset i] -> ShowS)
-> Show (FixedBitset i)
forall a.
(Int -> a -> ShowS) -> (a -> String) -> ([a] -> ShowS) -> Show a
forall k (i :: k). Int -> FixedBitset i -> ShowS
forall k (i :: k). [FixedBitset i] -> ShowS
forall k (i :: k). FixedBitset i -> String
$cshowsPrec :: forall k (i :: k). Int -> FixedBitset i -> ShowS
showsPrec :: Int -> FixedBitset i -> ShowS
$cshow :: forall k (i :: k). FixedBitset i -> String
show :: FixedBitset i -> String
$cshowList :: forall k (i :: k). [FixedBitset i] -> ShowS
showList :: [FixedBitset i] -> ShowS
Show, ReadPrec [FixedBitset i]
ReadPrec (FixedBitset i)
Int -> ReadS (FixedBitset i)
ReadS [FixedBitset i]
(Int -> ReadS (FixedBitset i))
-> ReadS [FixedBitset i]
-> ReadPrec (FixedBitset i)
-> ReadPrec [FixedBitset i]
-> Read (FixedBitset i)
forall a.
(Int -> ReadS a)
-> ReadS [a] -> ReadPrec a -> ReadPrec [a] -> Read a
forall k (i :: k). ReadPrec [FixedBitset i]
forall k (i :: k). ReadPrec (FixedBitset i)
forall k (i :: k). Int -> ReadS (FixedBitset i)
forall k (i :: k). ReadS [FixedBitset i]
$creadsPrec :: forall k (i :: k). Int -> ReadS (FixedBitset i)
readsPrec :: Int -> ReadS (FixedBitset i)
$creadList :: forall k (i :: k). ReadS [FixedBitset i]
readList :: ReadS [FixedBitset i]
$creadPrec :: forall k (i :: k). ReadPrec (FixedBitset i)
readPrec :: ReadPrec (FixedBitset i)
$creadListPrec :: forall k (i :: k). ReadPrec [FixedBitset i]
readListPrec :: ReadPrec [FixedBitset i]
Read, Eq (FixedBitset i)
Eq (FixedBitset i) =>
(Int -> FixedBitset i -> Int)
-> (FixedBitset i -> Int) -> Hashable (FixedBitset i)
Int -> FixedBitset i -> Int
FixedBitset i -> Int
forall a. Eq a => (Int -> a -> Int) -> (a -> Int) -> Hashable a
forall k (i :: k). Eq (FixedBitset i)
forall k (i :: k). Int -> FixedBitset i -> Int
forall k (i :: k). FixedBitset i -> Int
$chashWithSalt :: forall k (i :: k). Int -> FixedBitset i -> Int
hashWithSalt :: Int -> FixedBitset i -> Int
$chash :: forall k (i :: k). FixedBitset i -> Int
hash :: FixedBitset i -> Int
Hashable, FixedBitset i -> ()
(FixedBitset i -> ()) -> NFData (FixedBitset i)
forall a. (a -> ()) -> NFData a
forall k (i :: k). FixedBitset i -> ()
$crnf :: forall k (i :: k). FixedBitset i -> ()
rnf :: FixedBitset i -> ()
NFData)
  deriving newtype (Int -> FixedBitset i
FixedBitset i -> Int
FixedBitset i -> [FixedBitset i]
FixedBitset i -> FixedBitset i
FixedBitset i -> FixedBitset i -> [FixedBitset i]
FixedBitset i -> FixedBitset i -> FixedBitset i -> [FixedBitset i]
(FixedBitset i -> FixedBitset i)
-> (FixedBitset i -> FixedBitset i)
-> (Int -> FixedBitset i)
-> (FixedBitset i -> Int)
-> (FixedBitset i -> [FixedBitset i])
-> (FixedBitset i -> FixedBitset i -> [FixedBitset i])
-> (FixedBitset i -> FixedBitset i -> [FixedBitset i])
-> (FixedBitset i
    -> FixedBitset i -> FixedBitset i -> [FixedBitset i])
-> Enum (FixedBitset i)
forall a.
(a -> a)
-> (a -> a)
-> (Int -> a)
-> (a -> Int)
-> (a -> [a])
-> (a -> a -> [a])
-> (a -> a -> [a])
-> (a -> a -> a -> [a])
-> Enum a
forall k (i :: k). Int -> FixedBitset i
forall k (i :: k). FixedBitset i -> Int
forall k (i :: k). FixedBitset i -> [FixedBitset i]
forall k (i :: k). FixedBitset i -> FixedBitset i
forall k (i :: k).
FixedBitset i -> FixedBitset i -> [FixedBitset i]
forall k (i :: k).
FixedBitset i -> FixedBitset i -> FixedBitset i -> [FixedBitset i]
$csucc :: forall k (i :: k). FixedBitset i -> FixedBitset i
succ :: FixedBitset i -> FixedBitset i
$cpred :: forall k (i :: k). FixedBitset i -> FixedBitset i
pred :: FixedBitset i -> FixedBitset i
$ctoEnum :: forall k (i :: k). Int -> FixedBitset i
toEnum :: Int -> FixedBitset i
$cfromEnum :: forall k (i :: k). FixedBitset i -> Int
fromEnum :: FixedBitset i -> Int
$cenumFrom :: forall k (i :: k). FixedBitset i -> [FixedBitset i]
enumFrom :: FixedBitset i -> [FixedBitset i]
$cenumFromThen :: forall k (i :: k).
FixedBitset i -> FixedBitset i -> [FixedBitset i]
enumFromThen :: FixedBitset i -> FixedBitset i -> [FixedBitset i]
$cenumFromTo :: forall k (i :: k).
FixedBitset i -> FixedBitset i -> [FixedBitset i]
enumFromTo :: FixedBitset i -> FixedBitset i -> [FixedBitset i]
$cenumFromThenTo :: forall k (i :: k).
FixedBitset i -> FixedBitset i -> FixedBitset i -> [FixedBitset i]
enumFromThenTo :: FixedBitset i -> FixedBitset i -> FixedBitset i -> [FixedBitset i]
Enum, Integer -> FixedBitset i
FixedBitset i -> FixedBitset i
FixedBitset i -> FixedBitset i -> FixedBitset i
(FixedBitset i -> FixedBitset i -> FixedBitset i)
-> (FixedBitset i -> FixedBitset i -> FixedBitset i)
-> (FixedBitset i -> FixedBitset i -> FixedBitset i)
-> (FixedBitset i -> FixedBitset i)
-> (FixedBitset i -> FixedBitset i)
-> (FixedBitset i -> FixedBitset i)
-> (Integer -> FixedBitset i)
-> Num (FixedBitset i)
forall a.
(a -> a -> a)
-> (a -> a -> a)
-> (a -> a -> a)
-> (a -> a)
-> (a -> a)
-> (a -> a)
-> (Integer -> a)
-> Num a
forall k (i :: k). Integer -> FixedBitset i
forall k (i :: k). FixedBitset i -> FixedBitset i
forall k (i :: k). FixedBitset i -> FixedBitset i -> FixedBitset i
$c+ :: forall k (i :: k). FixedBitset i -> FixedBitset i -> FixedBitset i
+ :: FixedBitset i -> FixedBitset i -> FixedBitset i
$c- :: forall k (i :: k). FixedBitset i -> FixedBitset i -> FixedBitset i
- :: FixedBitset i -> FixedBitset i -> FixedBitset i
$c* :: forall k (i :: k). FixedBitset i -> FixedBitset i -> FixedBitset i
* :: FixedBitset i -> FixedBitset i -> FixedBitset i
$cnegate :: forall k (i :: k). FixedBitset i -> FixedBitset i
negate :: FixedBitset i -> FixedBitset i
$cabs :: forall k (i :: k). FixedBitset i -> FixedBitset i
abs :: FixedBitset i -> FixedBitset i
$csignum :: forall k (i :: k). FixedBitset i -> FixedBitset i
signum :: FixedBitset i -> FixedBitset i
$cfromInteger :: forall k (i :: k). Integer -> FixedBitset i
fromInteger :: Integer -> FixedBitset i
Num, Num (FixedBitset i)
Ord (FixedBitset i)
(Num (FixedBitset i), Ord (FixedBitset i)) =>
(FixedBitset i -> Rational) -> Real (FixedBitset i)
FixedBitset i -> Rational
forall a. (Num a, Ord a) => (a -> Rational) -> Real a
forall k (i :: k). Num (FixedBitset i)
forall k (i :: k). Ord (FixedBitset i)
forall k (i :: k). FixedBitset i -> Rational
$ctoRational :: forall k (i :: k). FixedBitset i -> Rational
toRational :: FixedBitset i -> Rational
Real, Eq (FixedBitset i)
FixedBitset i
Eq (FixedBitset i) =>
(FixedBitset i -> FixedBitset i -> FixedBitset i)
-> (FixedBitset i -> FixedBitset i -> FixedBitset i)
-> (FixedBitset i -> FixedBitset i -> FixedBitset i)
-> (FixedBitset i -> FixedBitset i)
-> (FixedBitset i -> Int -> FixedBitset i)
-> (FixedBitset i -> Int -> FixedBitset i)
-> FixedBitset i
-> (Int -> FixedBitset i)
-> (FixedBitset i -> Int -> FixedBitset i)
-> (FixedBitset i -> Int -> FixedBitset i)
-> (FixedBitset i -> Int -> FixedBitset i)
-> (FixedBitset i -> Int -> Bool)
-> (FixedBitset i -> Maybe Int)
-> (FixedBitset i -> Int)
-> (FixedBitset i -> Bool)
-> (FixedBitset i -> Int -> FixedBitset i)
-> (FixedBitset i -> Int -> FixedBitset i)
-> (FixedBitset i -> Int -> FixedBitset i)
-> (FixedBitset i -> Int -> FixedBitset i)
-> (FixedBitset i -> Int -> FixedBitset i)
-> (FixedBitset i -> Int -> FixedBitset i)
-> (FixedBitset i -> Int)
-> Bits (FixedBitset i)
Int -> FixedBitset i
FixedBitset i -> Bool
FixedBitset i -> Int
FixedBitset i -> Maybe Int
FixedBitset i -> FixedBitset i
FixedBitset i -> Int -> Bool
FixedBitset i -> Int -> FixedBitset i
FixedBitset i -> FixedBitset i -> FixedBitset i
forall a.
Eq a =>
(a -> a -> a)
-> (a -> a -> a)
-> (a -> a -> a)
-> (a -> a)
-> (a -> Int -> a)
-> (a -> Int -> a)
-> a
-> (Int -> a)
-> (a -> Int -> a)
-> (a -> Int -> a)
-> (a -> Int -> a)
-> (a -> Int -> Bool)
-> (a -> Maybe Int)
-> (a -> Int)
-> (a -> Bool)
-> (a -> Int -> a)
-> (a -> Int -> a)
-> (a -> Int -> a)
-> (a -> Int -> a)
-> (a -> Int -> a)
-> (a -> Int -> a)
-> (a -> Int)
-> Bits a
forall k (i :: k). Eq (FixedBitset i)
forall k (i :: k). FixedBitset i
forall k (i :: k). Int -> FixedBitset i
forall k (i :: k). FixedBitset i -> Bool
forall k (i :: k). FixedBitset i -> Int
forall k (i :: k). FixedBitset i -> Maybe Int
forall k (i :: k). FixedBitset i -> FixedBitset i
forall k (i :: k). FixedBitset i -> Int -> Bool
forall k (i :: k). FixedBitset i -> Int -> FixedBitset i
forall k (i :: k). FixedBitset i -> FixedBitset i -> FixedBitset i
$c.&. :: forall k (i :: k). FixedBitset i -> FixedBitset i -> FixedBitset i
.&. :: FixedBitset i -> FixedBitset i -> FixedBitset i
$c.|. :: forall k (i :: k). FixedBitset i -> FixedBitset i -> FixedBitset i
.|. :: FixedBitset i -> FixedBitset i -> FixedBitset i
$cxor :: forall k (i :: k). FixedBitset i -> FixedBitset i -> FixedBitset i
xor :: FixedBitset i -> FixedBitset i -> FixedBitset i
$ccomplement :: forall k (i :: k). FixedBitset i -> FixedBitset i
complement :: FixedBitset i -> FixedBitset i
$cshift :: forall k (i :: k). FixedBitset i -> Int -> FixedBitset i
shift :: FixedBitset i -> Int -> FixedBitset i
$crotate :: forall k (i :: k). FixedBitset i -> Int -> FixedBitset i
rotate :: FixedBitset i -> Int -> FixedBitset i
$czeroBits :: forall k (i :: k). FixedBitset i
zeroBits :: FixedBitset i
$cbit :: forall k (i :: k). Int -> FixedBitset i
bit :: Int -> FixedBitset i
$csetBit :: forall k (i :: k). FixedBitset i -> Int -> FixedBitset i
setBit :: FixedBitset i -> Int -> FixedBitset i
$cclearBit :: forall k (i :: k). FixedBitset i -> Int -> FixedBitset i
clearBit :: FixedBitset i -> Int -> FixedBitset i
$ccomplementBit :: forall k (i :: k). FixedBitset i -> Int -> FixedBitset i
complementBit :: FixedBitset i -> Int -> FixedBitset i
$ctestBit :: forall k (i :: k). FixedBitset i -> Int -> Bool
testBit :: FixedBitset i -> Int -> Bool
$cbitSizeMaybe :: forall k (i :: k). FixedBitset i -> Maybe Int
bitSizeMaybe :: FixedBitset i -> Maybe Int
$cbitSize :: forall k (i :: k). FixedBitset i -> Int
bitSize :: FixedBitset i -> Int
$cisSigned :: forall k (i :: k). FixedBitset i -> Bool
isSigned :: FixedBitset i -> Bool
$cshiftL :: forall k (i :: k). FixedBitset i -> Int -> FixedBitset i
shiftL :: FixedBitset i -> Int -> FixedBitset i
$cunsafeShiftL :: forall k (i :: k). FixedBitset i -> Int -> FixedBitset i
unsafeShiftL :: FixedBitset i -> Int -> FixedBitset i
$cshiftR :: forall k (i :: k). FixedBitset i -> Int -> FixedBitset i
shiftR :: FixedBitset i -> Int -> FixedBitset i
$cunsafeShiftR :: forall k (i :: k). FixedBitset i -> Int -> FixedBitset i
unsafeShiftR :: FixedBitset i -> Int -> FixedBitset i
$crotateL :: forall k (i :: k). FixedBitset i -> Int -> FixedBitset i
rotateL :: FixedBitset i -> Int -> FixedBitset i
$crotateR :: forall k (i :: k). FixedBitset i -> Int -> FixedBitset i
rotateR :: FixedBitset i -> Int -> FixedBitset i
$cpopCount :: forall k (i :: k). FixedBitset i -> Int
popCount :: FixedBitset i -> Int
Bits)

-- if too few bytes, pad with zeroes; if too many, truncate
trim :: Int -> VU.Vector Int8 -> VU.Vector Int8
trim :: Int -> Vector Int8 -> Vector Int8
trim Int
n Vector Int8
v
  | Vector Int8 -> Int
forall a. Unbox a => Vector a -> Int
VU.length Vector Int8
v Int -> Int -> Bool
forall a. Ord a => a -> a -> Bool
< Int
n = Vector Int8
v Vector Int8 -> Vector Int8 -> Vector Int8
forall a. Semigroup a => a -> a -> a
<> Int -> Int8 -> Vector Int8
forall a. Unbox a => Int -> a -> Vector a
VU.replicate (Int
n Int -> Int -> Int
forall a. Num a => a -> a -> a
- Vector Int8 -> Int
forall a. Unbox a => Vector a -> Int
VU.length Vector Int8
v) Int8
0
  | Bool
otherwise = Int -> Vector Int8 -> Vector Int8
forall a. Unbox a => Int -> Vector a -> Vector a
VU.take Int
n Vector Int8
v

instance (KnownNat i) => Pack (FixedBitset i) where
  pack :: FixedBitset i -> Builder
pack =
    Vector Int8 -> Builder
forall a. Pack a => a -> Builder
pack
      (Vector Int8 -> Builder)
-> (FixedBitset i -> Vector Int8) -> FixedBitset i -> Builder
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Int -> Vector Int8 -> Vector Int8
trim (Integer -> Int
forall a b. (Integral a, Num b) => a -> b
fromIntegral (Proxy i -> Integer
forall (n :: Nat) (proxy :: Nat -> *).
KnownNat n =>
proxy n -> Integer
natVal (forall (t :: Nat). Proxy t
forall {k} (t :: k). Proxy t
Proxy @i) Integer -> Integer -> Integer
forall a. Num a => a -> a -> a
+ Integer
7 Integer -> Integer -> Integer
forall a. Integral a => a -> a -> a
`div` Integer
8) Int -> Int -> Int
forall a. Num a => a -> a -> a
+ Int
1)
      (Vector Int8 -> Vector Int8)
-> (FixedBitset i -> Vector Int8) -> FixedBitset i -> Vector Int8
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Bitset -> Vector Int8
bitsettovui8
      (Bitset -> Vector Int8)
-> (FixedBitset i -> Bitset) -> FixedBitset i -> Vector Int8
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Integer -> Bitset
Bitset
      (Integer -> Bitset)
-> (FixedBitset i -> Integer) -> FixedBitset i -> Bitset
forall b c a. (b -> c) -> (a -> b) -> a -> c
. FixedBitset i -> Integer
forall {k} (i :: k). FixedBitset i -> Integer
getfixedbitset
  {-# INLINE pack #-}

instance (KnownNat i) => Unpack (FixedBitset i) where
  unpack :: forall (st :: ZeroBitType) r. Parser st r (FixedBitset i)
unpack =
    Integer -> FixedBitset i
forall k (i :: k). Integer -> FixedBitset i
FixedBitset
      (Integer -> FixedBitset i)
-> (Vector Int8 -> Integer) -> Vector Int8 -> FixedBitset i
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Bitset -> Integer
getbitset
      (Bitset -> Integer)
-> (Vector Int8 -> Bitset) -> Vector Int8 -> Integer
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Vector Int8 -> Bitset
bitsetfromvui8
      (Vector Int8 -> Bitset)
-> (Vector Int8 -> Vector Int8) -> Vector Int8 -> Bitset
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Int -> Vector Int8 -> Vector Int8
trim (Integer -> Int
forall a b. (Integral a, Num b) => a -> b
fromIntegral (Proxy i -> Integer
forall (n :: Nat) (proxy :: Nat -> *).
KnownNat n =>
proxy n -> Integer
natVal (forall (t :: Nat). Proxy t
forall {k} (t :: k). Proxy t
Proxy @i) Integer -> Integer -> Integer
forall a. Num a => a -> a -> a
+ Integer
7 Integer -> Integer -> Integer
forall a. Integral a => a -> a -> a
`div` Integer
8) Int -> Int -> Int
forall a. Num a => a -> a -> a
+ Int
1)
      (Vector Int8 -> FixedBitset i)
-> ParserT st r ParseError (Vector Int8)
-> ParserT st r ParseError (FixedBitset i)
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> ParserT st r ParseError (Vector Int8)
forall (st :: ZeroBitType) r. Parser st r (Vector Int8)
forall a (st :: ZeroBitType) r. Unpack a => Parser st r a
unpack
  {-# INLINE unpack #-}