-- | Little-endian-per-byte bitmap helpers compatible with go-libp2p-pubsub.
module LibP2P.Protocol.GossipSub.PartialMessages.Bitmap
  ( Bitmap
  , emptyBitmap
  , fullBitmap
  , bitmapFromBytes
  , bitmapToBytes
  , bitmapSet
  , bitmapClear
  , bitmapMember
  , bitmapUnion
  , bitmapIntersection
  , bitmapDifference
  , bitmapCount
  , bitmapIsZero
  ) where

import Data.Bits
import Data.ByteString (ByteString)
import qualified Data.ByteString as BS
import Data.Word (Word8)

newtype Bitmap = Bitmap ByteString
  deriving (Int -> Bitmap -> ShowS
[Bitmap] -> ShowS
Bitmap -> String
(Int -> Bitmap -> ShowS)
-> (Bitmap -> String) -> ([Bitmap] -> ShowS) -> Show Bitmap
forall a.
(Int -> a -> ShowS) -> (a -> String) -> ([a] -> ShowS) -> Show a
$cshowsPrec :: Int -> Bitmap -> ShowS
showsPrec :: Int -> Bitmap -> ShowS
$cshow :: Bitmap -> String
show :: Bitmap -> String
$cshowList :: [Bitmap] -> ShowS
showList :: [Bitmap] -> ShowS
Show, Bitmap -> Bitmap -> Bool
(Bitmap -> Bitmap -> Bool)
-> (Bitmap -> Bitmap -> Bool) -> Eq Bitmap
forall a. (a -> a -> Bool) -> (a -> a -> Bool) -> Eq a
$c== :: Bitmap -> Bitmap -> Bool
== :: Bitmap -> Bitmap -> Bool
$c/= :: Bitmap -> Bitmap -> Bool
/= :: Bitmap -> Bitmap -> Bool
Eq)

-- | A zeroed bitmap large enough for the requested bit count.
emptyBitmap :: Int -> Bitmap
emptyBitmap :: Int -> Bitmap
emptyBitmap Int
bitCount = ByteString -> Bitmap
Bitmap (Int -> Word8 -> ByteString
BS.replicate (Int -> Int
byteCount Int
bitCount) Word8
0)

-- | A bitmap whose first @bitCount@ bits are set.
fullBitmap :: Int -> Bitmap
fullBitmap :: Int -> Bitmap
fullBitmap Int
bitCount
  | Int
bitCount Int -> Int -> Bool
forall a. Ord a => a -> a -> Bool
<= Int
0 = Int -> Bitmap
emptyBitmap Int
0
  | Bool
otherwise =
      ByteString -> Bitmap
Bitmap (ByteString -> Bitmap) -> ByteString -> Bitmap
forall a b. (a -> b) -> a -> b
$
        [Word8] -> ByteString
BS.pack
          [Int -> Word8
forall {a}. (Bounded a, Num a) => Int -> a
byteAt Int
index | Int
index <- [Int
0 .. Int -> Int
byteCount Int
bitCount Int -> Int -> Int
forall a. Num a => a -> a -> a
- Int
1]]
 where
  completeBytes :: Int
completeBytes = Int
bitCount Int -> Int -> Int
forall a. Integral a => a -> a -> a
`div` Int
8
  remainder :: Int
remainder = Int
bitCount Int -> Int -> Int
forall a. Integral a => a -> a -> a
`mod` Int
8
  byteAt :: Int -> a
byteAt Int
index
    | Int
index Int -> Int -> Bool
forall a. Ord a => a -> a -> Bool
< Int
completeBytes = a
forall a. Bounded a => a
maxBound
    | Int
remainder Int -> Int -> Bool
forall a. Eq a => a -> a -> Bool
== Int
0 = a
forall a. Bounded a => a
maxBound
    | Bool
otherwise = Int -> a
forall a b. (Integral a, Num b) => a -> b
fromIntegral ((Int
1 Int -> Int -> Int
forall a. Bits a => a -> Int -> a
`shiftL` Int
remainder) Int -> Int -> Int
forall a. Num a => a -> a -> a
- Int
1 :: Int)

bitmapFromBytes :: ByteString -> Bitmap
bitmapFromBytes :: ByteString -> Bitmap
bitmapFromBytes = ByteString -> Bitmap
Bitmap

bitmapToBytes :: Bitmap -> ByteString
bitmapToBytes :: Bitmap -> ByteString
bitmapToBytes (Bitmap ByteString
bytes) = ByteString
bytes

-- | Set one bit, growing the bitmap when necessary.
bitmapSet :: Int -> Bitmap -> Bitmap
bitmapSet :: Int -> Bitmap -> Bitmap
bitmapSet Int
index Bitmap
bitmap
  | Int
index Int -> Int -> Bool
forall a. Ord a => a -> a -> Bool
< Int
0 = Bitmap
bitmap
  | Bool
otherwise = Int -> (Word8 -> Word8) -> Bitmap -> Bitmap
modifyBit Int
index (Word8 -> Int -> Word8
forall a. Bits a => a -> Int -> a
`setBit` Int -> Int
bitOffset Int
index) Bitmap
bitmap

bitmapClear :: Int -> Bitmap -> Bitmap
bitmapClear :: Int -> Bitmap -> Bitmap
bitmapClear Int
index Bitmap
bitmap
  | Int
index Int -> Int -> Bool
forall a. Ord a => a -> a -> Bool
< Int
0 = Bitmap
bitmap
  | Bool
otherwise = Int -> (Word8 -> Word8) -> Bitmap -> Bitmap
modifyExistingBit Int
index (Word8 -> Int -> Word8
forall a. Bits a => a -> Int -> a
`clearBit` Int -> Int
bitOffset Int
index) Bitmap
bitmap

bitmapMember :: Int -> Bitmap -> Bool
bitmapMember :: Int -> Bitmap -> Bool
bitmapMember Int
index (Bitmap ByteString
bytes)
  | Int
index Int -> Int -> Bool
forall a. Ord a => a -> a -> Bool
< Int
0 Bool -> Bool -> Bool
|| Int -> Int
byteOffset Int
index Int -> Int -> Bool
forall a. Ord a => a -> a -> Bool
>= ByteString -> Int
BS.length ByteString
bytes = Bool
False
  | Bool
otherwise = Word8 -> Int -> Bool
forall a. Bits a => a -> Int -> Bool
testBit (HasCallStack => ByteString -> Int -> Word8
ByteString -> Int -> Word8
BS.index ByteString
bytes (Int -> Int
byteOffset Int
index)) (Int -> Int
bitOffset Int
index)

bitmapUnion :: Bitmap -> Bitmap -> Bitmap
bitmapUnion :: Bitmap -> Bitmap -> Bitmap
bitmapUnion = (Word8 -> Word8 -> Word8) -> Bitmap -> Bitmap -> Bitmap
zipBitmaps Word8 -> Word8 -> Word8
forall a. Bits a => a -> a -> a
(.|.)

bitmapIntersection :: Bitmap -> Bitmap -> Bitmap
bitmapIntersection :: Bitmap -> Bitmap -> Bitmap
bitmapIntersection = (Word8 -> Word8 -> Word8) -> Bitmap -> Bitmap -> Bitmap
zipBitmaps Word8 -> Word8 -> Word8
forall a. Bits a => a -> a -> a
(.&.)

bitmapDifference :: Bitmap -> Bitmap -> Bitmap
bitmapDifference :: Bitmap -> Bitmap -> Bitmap
bitmapDifference = (Word8 -> Word8 -> Word8) -> Bitmap -> Bitmap -> Bitmap
zipBitmaps (\Word8
left Word8
right -> Word8
left Word8 -> Word8 -> Word8
forall a. Bits a => a -> a -> a
.&. Word8 -> Word8
forall a. Bits a => a -> a
complement Word8
right)

bitmapCount :: Bitmap -> Int
bitmapCount :: Bitmap -> Int
bitmapCount (Bitmap ByteString
bytes) = (Int -> Word8 -> Int) -> Int -> ByteString -> Int
forall a. (a -> Word8 -> a) -> a -> ByteString -> a
BS.foldl' (\Int
count Word8
byte -> Int
count Int -> Int -> Int
forall a. Num a => a -> a -> a
+ Word8 -> Int
forall a. Bits a => a -> Int
popCount Word8
byte) Int
0 ByteString
bytes

bitmapIsZero :: Bitmap -> Bool
bitmapIsZero :: Bitmap -> Bool
bitmapIsZero (Bitmap ByteString
bytes) = (Word8 -> Bool) -> ByteString -> Bool
BS.all (Word8 -> Word8 -> Bool
forall a. Eq a => a -> a -> Bool
== Word8
0) ByteString
bytes

modifyBit :: Int -> (Word8 -> Word8) -> Bitmap -> Bitmap
modifyBit :: Int -> (Word8 -> Word8) -> Bitmap -> Bitmap
modifyBit Int
index Word8 -> Word8
update (Bitmap ByteString
bytes) =
  let needed :: Int
needed = Int -> Int
byteOffset Int
index Int -> Int -> Int
forall a. Num a => a -> a -> a
+ Int
1
      extended :: ByteString
extended = ByteString
bytes ByteString -> ByteString -> ByteString
forall a. Semigroup a => a -> a -> a
<> Int -> Word8 -> ByteString
BS.replicate (Int -> Int -> Int
forall a. Ord a => a -> a -> a
max Int
0 (Int
needed Int -> Int -> Int
forall a. Num a => a -> a -> a
- ByteString -> Int
BS.length ByteString
bytes)) Word8
0
   in ByteString -> Bitmap
Bitmap (Int -> (Word8 -> Word8) -> ByteString -> ByteString
replaceByte (Int -> Int
byteOffset Int
index) Word8 -> Word8
update ByteString
extended)

modifyExistingBit :: Int -> (Word8 -> Word8) -> Bitmap -> Bitmap
modifyExistingBit :: Int -> (Word8 -> Word8) -> Bitmap -> Bitmap
modifyExistingBit Int
index Word8 -> Word8
update bitmap :: Bitmap
bitmap@(Bitmap ByteString
bytes)
  | Int -> Int
byteOffset Int
index Int -> Int -> Bool
forall a. Ord a => a -> a -> Bool
>= ByteString -> Int
BS.length ByteString
bytes = Bitmap
bitmap
  | Bool
otherwise = ByteString -> Bitmap
Bitmap (Int -> (Word8 -> Word8) -> ByteString -> ByteString
replaceByte (Int -> Int
byteOffset Int
index) Word8 -> Word8
update ByteString
bytes)

replaceByte :: Int -> (Word8 -> Word8) -> ByteString -> ByteString
replaceByte :: Int -> (Word8 -> Word8) -> ByteString -> ByteString
replaceByte Int
index Word8 -> Word8
update ByteString
bytes =
  Int -> ByteString -> ByteString
BS.take Int
index ByteString
bytes ByteString -> ByteString -> ByteString
forall a. Semigroup a => a -> a -> a
<> Word8 -> ByteString
BS.singleton (Word8 -> Word8
update (HasCallStack => ByteString -> Int -> Word8
ByteString -> Int -> Word8
BS.index ByteString
bytes Int
index)) ByteString -> ByteString -> ByteString
forall a. Semigroup a => a -> a -> a
<> Int -> ByteString -> ByteString
BS.drop (Int
index Int -> Int -> Int
forall a. Num a => a -> a -> a
+ Int
1) ByteString
bytes

zipBitmaps :: (Word8 -> Word8 -> Word8) -> Bitmap -> Bitmap -> Bitmap
zipBitmaps :: (Word8 -> Word8 -> Word8) -> Bitmap -> Bitmap -> Bitmap
zipBitmaps Word8 -> Word8 -> Word8
combine (Bitmap ByteString
left) (Bitmap ByteString
right) =
  ByteString -> Bitmap
Bitmap (ByteString -> Bitmap) -> ByteString -> Bitmap
forall a b. (a -> b) -> a -> b
$
    [Word8] -> ByteString
BS.pack
      [ Word8 -> Word8 -> Word8
combine (ByteString -> Int -> Word8
at ByteString
left Int
index) (ByteString -> Int -> Word8
at ByteString
right Int
index)
      | Int
index <- [Int
0 .. Int -> Int -> Int
forall a. Ord a => a -> a -> a
max (ByteString -> Int
BS.length ByteString
left) (ByteString -> Int
BS.length ByteString
right) Int -> Int -> Int
forall a. Num a => a -> a -> a
- Int
1]
      ]

at :: ByteString -> Int -> Word8
at :: ByteString -> Int -> Word8
at ByteString
bytes Int
index
  | Int
index Int -> Int -> Bool
forall a. Ord a => a -> a -> Bool
< ByteString -> Int
BS.length ByteString
bytes = HasCallStack => ByteString -> Int -> Word8
ByteString -> Int -> Word8
BS.index ByteString
bytes Int
index
  | Bool
otherwise = Word8
0

byteCount :: Int -> Int
byteCount :: Int -> Int
byteCount Int
bitCount = Int -> Int -> Int
forall a. Ord a => a -> a -> a
max Int
0 ((Int
bitCount Int -> Int -> Int
forall a. Num a => a -> a -> a
+ Int
7) Int -> Int -> Int
forall a. Integral a => a -> a -> a
`div` Int
8)

byteOffset :: Int -> Int
byteOffset :: Int -> Int
byteOffset Int
index = Int
index Int -> Int -> Int
forall a. Integral a => a -> a -> a
`div` Int
8

bitOffset :: Int -> Int
bitOffset :: Int -> Int
bitOffset Int
index = Int
index Int -> Int -> Int
forall a. Integral a => a -> a -> a
`mod` Int
8