{-# LANGUAGE Rank2Types #-}
{-# LANGUAGE FlexibleContexts #-}
module Data.Bits.Lens
( (.|.~), (.&.~), (<.|.~), (<.&.~), (<<.|.~), (<<.&.~)
, (.|.=), (.&.=), (<.|.=), (<.&.=), (<<.|.=), (<<.&.=)
, bitAt
, bits
, byteAt
, bytewise
) where
import Prelude ()
import Control.Lens
import Control.Lens.Internal.Prelude
import Control.Monad.State
import Data.Bits
import Data.Word
infixr 4 .|.~, .&.~, <.|.~, <.&.~, <<.|.~, <<.&.~
infix 4 .|.=, .&.=, <.|.=, <.&.=, <<.|.=, <<.&.=
(.|.~):: Bits a => ASetter s t a a -> a -> s -> t
ASetter s t a a
l .|.~ :: forall a s t. Bits a => ASetter s t a a -> a -> s -> t
.|.~ a
n = forall s t a b. ASetter s t a b -> (a -> b) -> s -> t
over ASetter s t a a
l (forall a. Bits a => a -> a -> a
.|. a
n)
{-# INLINE (.|.~) #-}
(.&.~) :: Bits a => ASetter s t a a -> a -> s -> t
ASetter s t a a
l .&.~ :: forall a s t. Bits a => ASetter s t a a -> a -> s -> t
.&.~ a
n = forall s t a b. ASetter s t a b -> (a -> b) -> s -> t
over ASetter s t a a
l (forall a. Bits a => a -> a -> a
.&. a
n)
{-# INLINE (.&.~) #-}
(.&.=):: (MonadState s m, Bits a) => ASetter' s a -> a -> m ()
ASetter' s a
l .&.= :: forall s (m :: * -> *) a.
(MonadState s m, Bits a) =>
ASetter' s a -> a -> m ()
.&.= a
a = forall s (m :: * -> *). MonadState s m => (s -> s) -> m ()
modify (ASetter' s a
l forall a s t. Bits a => ASetter s t a a -> a -> s -> t
.&.~ a
a)
{-# INLINE (.&.=) #-}
(.|.=) :: (MonadState s m, Bits a) => ASetter' s a -> a -> m ()
ASetter' s a
l .|.= :: forall s (m :: * -> *) a.
(MonadState s m, Bits a) =>
ASetter' s a -> a -> m ()
.|.= a
a = forall s (m :: * -> *). MonadState s m => (s -> s) -> m ()
modify (ASetter' s a
l forall a s t. Bits a => ASetter s t a a -> a -> s -> t
.|.~ a
a)
{-# INLINE (.|.=) #-}
(<.|.~):: Bits a => LensLike ((,) a) s t a a -> a -> s -> (a, t)
LensLike ((,) a) s t a a
l <.|.~ :: forall a s t.
Bits a =>
LensLike ((,) a) s t a a -> a -> s -> (a, t)
<.|.~ a
n = LensLike ((,) a) s t a a
l forall b s t a. LensLike ((,) b) s t a b -> (a -> b) -> s -> (b, t)
<%~ (forall a. Bits a => a -> a -> a
.|. a
n)
{-# INLINE (<.|.~) #-}
(<.&.~) :: Bits a => LensLike ((,) a) s t a a -> a -> s -> (a, t)
LensLike ((,) a) s t a a
l <.&.~ :: forall a s t.
Bits a =>
LensLike ((,) a) s t a a -> a -> s -> (a, t)
<.&.~ a
n = LensLike ((,) a) s t a a
l forall b s t a. LensLike ((,) b) s t a b -> (a -> b) -> s -> (b, t)
<%~ (forall a. Bits a => a -> a -> a
.&. a
n)
{-# INLINE (<.&.~) #-}
(<.&.=):: (MonadState s m, Bits a) => LensLike' ((,)a) s a -> a -> m a
LensLike' ((,) a) s a
l <.&.= :: forall s (m :: * -> *) a.
(MonadState s m, Bits a) =>
LensLike' ((,) a) s a -> a -> m a
<.&.= a
b = LensLike' ((,) a) s a
l forall s (m :: * -> *) b a.
MonadState s m =>
LensLike ((,) b) s s a b -> (a -> b) -> m b
<%= (forall a. Bits a => a -> a -> a
.&. a
b)
{-# INLINE (<.&.=) #-}
(<.|.=) :: (MonadState s m, Bits a) => LensLike' ((,)a) s a -> a -> m a
LensLike' ((,) a) s a
l <.|.= :: forall s (m :: * -> *) a.
(MonadState s m, Bits a) =>
LensLike' ((,) a) s a -> a -> m a
<.|.= a
b = LensLike' ((,) a) s a
l forall s (m :: * -> *) b a.
MonadState s m =>
LensLike ((,) b) s s a b -> (a -> b) -> m b
<%= (forall a. Bits a => a -> a -> a
.|. a
b)
{-# INLINE (<.|.=) #-}
(<<.&.~) :: Bits a => Optical' (->) q ((,)a) s a -> a -> q s (a, s)
Optical' (->) q ((,) a) s a
l <<.&.~ :: forall a (q :: * -> * -> *) s.
Bits a =>
Optical' (->) q ((,) a) s a -> a -> q s (a, s)
<<.&.~ a
b = Optical' (->) q ((,) a) s a
l forall a b. (a -> b) -> a -> b
$ \a
a -> (a
a, a
a forall a. Bits a => a -> a -> a
.&. a
b)
{-# INLINE (<<.&.~) #-}
(<<.|.~) :: Bits a => Optical' (->) q ((,)a) s a -> a -> q s (a, s)
Optical' (->) q ((,) a) s a
l <<.|.~ :: forall a (q :: * -> * -> *) s.
Bits a =>
Optical' (->) q ((,) a) s a -> a -> q s (a, s)
<<.|.~ a
b = Optical' (->) q ((,) a) s a
l forall a b. (a -> b) -> a -> b
$ \a
a -> (a
a, a
a forall a. Bits a => a -> a -> a
.|. a
b)
{-# INLINE (<<.|.~) #-}
(<<.&.=) :: (MonadState s m, Bits a) => LensLike' ((,) a) s a -> a -> m a
LensLike' ((,) a) s a
l <<.&.= :: forall s (m :: * -> *) a.
(MonadState s m, Bits a) =>
LensLike' ((,) a) s a -> a -> m a
<<.&.= a
b = LensLike' ((,) a) s a
l forall {k} s (m :: * -> *) (p :: k -> * -> *) r (a :: k) b.
MonadState s m =>
Over p ((,) r) s s a b -> p a (r, b) -> m r
%%= \a
a -> (a
a, a
a forall a. Bits a => a -> a -> a
.&. a
b)
{-# INLINE (<<.&.=) #-}
(<<.|.=) :: (MonadState s m, Bits a) => LensLike' ((,) a) s a -> a -> m a
LensLike' ((,) a) s a
l <<.|.= :: forall s (m :: * -> *) a.
(MonadState s m, Bits a) =>
LensLike' ((,) a) s a -> a -> m a
<<.|.= a
b = LensLike' ((,) a) s a
l forall {k} s (m :: * -> *) (p :: k -> * -> *) r (a :: k) b.
MonadState s m =>
Over p ((,) r) s s a b -> p a (r, b) -> m r
%%= \a
a -> (a
a, a
a forall a. Bits a => a -> a -> a
.|. a
b)
{-# INLINE (<<.|.=) #-}
bitAt :: Bits b => Int -> IndexedLens' Int b Bool
bitAt :: forall b. Bits b => Int -> IndexedLens' Int b Bool
bitAt Int
n p Bool (f Bool)
f b
b = forall i (p :: * -> * -> *) a b.
Indexable i p =>
p a b -> i -> a -> b
indexed p Bool (f Bool)
f Int
n (forall a. Bits a => a -> Int -> Bool
testBit b
b Int
n) forall (f :: * -> *) a b. Functor f => f a -> (a -> b) -> f b
<&> \Bool
x -> if Bool
x then forall a. Bits a => a -> Int -> a
setBit b
b Int
n else forall a. Bits a => a -> Int -> a
clearBit b
b Int
n
{-# INLINE bitAt #-}
byteAt :: (Integral b, Bits b) => Int -> IndexedLens' Int b Word8
byteAt :: forall b. (Integral b, Bits b) => Int -> IndexedLens' Int b Word8
byteAt Int
i p Word8 (f Word8)
f b
b = forall {a}. Integral a => a -> b
back forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> forall i (p :: * -> * -> *) a b.
Indexable i p =>
p a b -> i -> a -> b
indexed p Word8 (f Word8)
f Int
i (b -> Word8
forward b
b) where
back :: a -> b
back a
w8 = (forall a b. (Integral a, Num b) => a -> b
fromIntegral a
w8 forall a. Bits a => a -> Int -> a
`shiftL` (Int
i forall a. Num a => a -> a -> a
* Int
8))
forall a. Bits a => a -> a -> a
.|. (forall a. Bits a => a -> a
complement (b
255 forall a. Bits a => a -> Int -> a
`shiftL` (Int
i forall a. Num a => a -> a -> a
* Int
8)) forall a. Bits a => a -> a -> a
.&. b
b)
forward :: b -> Word8
forward = forall a b. (Integral a, Num b) => a -> b
fromIntegral forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall a. Bits a => a -> a -> a
(.&.) b
0xff forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall a b c. (a -> b -> c) -> b -> a -> c
flip forall a. Bits a => a -> Int -> a
shiftR (Int
i forall a. Num a => a -> a -> a
* Int
8)
bits :: (Num b, Bits b) => IndexedTraversal' Int b Bool
bits :: forall b. (Num b, Bits b) => IndexedTraversal' Int b Bool
bits p Bool (f Bool)
f b
b = forall (t :: * -> *) a b.
Foldable t =>
(a -> b -> b) -> b -> t a -> b
foldr forall {a}. Bits a => (Int, Bool) -> a -> a
step b
0 forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> forall (t :: * -> *) (f :: * -> *) a b.
(Traversable t, Applicative f) =>
(a -> f b) -> t a -> f (t b)
traverse Int -> f (Int, Bool)
g [Int]
bs where
g :: Int -> f (Int, Bool)
g Int
n = (,) Int
n forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> forall i (p :: * -> * -> *) a b.
Indexable i p =>
p a b -> i -> a -> b
indexed p Bool (f Bool)
f Int
n (forall a. Bits a => a -> Int -> Bool
testBit b
b Int
n)
bs :: [Int]
bs = forall a. (a -> Bool) -> [a] -> [a]
takeWhile Int -> Bool
hasBit [Int
0..]
hasBit :: Int -> Bool
hasBit Int
n = forall a. Bits a => a -> Int -> a
complementBit b
b Int
n forall a. Eq a => a -> a -> Bool
/= b
b
step :: (Int, Bool) -> a -> a
step (Int
n,Bool
True) a
r = forall a. Bits a => a -> Int -> a
setBit a
r Int
n
step (Int, Bool)
_ a
r = a
r
{-# INLINE bits #-}
bytewise :: (Integral b, Bits b) => IndexedTraversal' Int b Word8
bytewise :: forall b. (Integral b, Bits b) => IndexedTraversal' Int b Word8
bytewise p Word8 (f Word8)
f b
b = forall (t :: * -> *) a b.
Foldable t =>
(a -> b -> b) -> b -> t a -> b
foldr forall {a} {a}. (Bits a, Integral a, Num a) => (Int, a) -> a -> a
step b
0 forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> forall (t :: * -> *) (f :: * -> *) a b.
(Traversable t, Applicative f) =>
(a -> f b) -> t a -> f (t b)
traverse Int -> f (Int, Word8)
g [Int]
bs where
g :: Int -> f (Int, Word8)
g Int
n = (,) Int
n forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> forall i (p :: * -> * -> *) a b.
Indexable i p =>
p a b -> i -> a -> b
indexed p Word8 (f Word8)
f Int
n (forall a b. (Integral a, Num b) => a -> b
fromIntegral forall a b. (a -> b) -> a -> b
$ b
b forall a. Bits a => a -> Int -> a
`shiftR` (Int
nforall a. Num a => a -> a -> a
*Int
8))
bs :: [Int]
bs = forall a. (a -> Bool) -> [a] -> [a]
takeWhile Int -> Bool
hasByte [Int
0..]
hasByte :: Int -> Bool
hasByte Int
n = forall a. Bits a => a -> Int -> a
complementBit b
b (Int
nforall a. Num a => a -> a -> a
*Int
8) forall a. Eq a => a -> a -> Bool
/= b
b
step :: (Int, a) -> a -> a
step (Int
n,a
x) a
r = a
r forall a. Bits a => a -> a -> a
.|. (forall a b. (Integral a, Num b) => a -> b
fromIntegral a
x forall a. Bits a => a -> Int -> a
`shiftL` (Int
nforall a. Num a => a -> a -> a
*Int
8))
{-# INLINE bytewise #-}