module Octane.Type.CompressedWord
( CompressedWord(..)
, fromCompressedWord
) where
import Data.Aeson ((.=))
import Data.Function ((&))
import qualified Control.DeepSeq as DeepSeq
import qualified Data.Aeson as Aeson
import qualified Data.Binary.Bits as BinaryBit
import qualified Data.Binary.Bits.Get as BinaryBit
import qualified Data.Bits as Bits
import qualified Data.Default.Class as Default
import qualified Data.OverloadedRecords.TH as OverloadedRecords
import qualified GHC.Generics as Generics
import qualified Octane.Data as Data
import qualified Octane.Type.Boolean as Boolean
data CompressedWord = CompressedWord
{ compressedWordLimit :: Word
, compressedWordValue :: Word
} deriving (Eq, Generics.Generic, Show)
$(OverloadedRecords.overloadedRecord Default.def ''CompressedWord)
instance BinaryBit.BinaryBit CompressedWord where
getBits n = do
let limit = fromIntegral n
value <- getStep limit (bitSize limit) 0 0
pure (CompressedWord limit value)
putBits _ compressedWord = do
let limit = compressedWord & #limit
let value = compressedWord & #value
if value > limit
then fail ("value " ++ show value ++ " > limit " ++ show limit)
else pure ()
let maxBits = bitSize limit
let go position soFar = do
if position < maxBits
then do
let x = Bits.shiftL 1 position
if maxBits > 1 && position == maxBits 1 && soFar + x > limit
then do
pure ()
else do
let bit = position & Bits.testBit value
bit & Boolean.Boolean & BinaryBit.putBits 0
let delta =
if bit
then x
else 0
go (position + 1) (soFar + delta)
else do
pure ()
go 0 0
instance DeepSeq.NFData CompressedWord
instance Aeson.ToJSON CompressedWord where
toJSON compressedWord =
Aeson.object
["Limit" .= #limit compressedWord, "Value" .= #value compressedWord]
fromCompressedWord
:: (Integral a)
=> CompressedWord -> a
fromCompressedWord compressedWord = compressedWord & #value & fromIntegral
bitSize
:: (Integral a, Integral b)
=> a -> b
bitSize x = do
let n = x & max 1 & fromIntegral & logBase (2 :: Double) & ceiling & max 1
if x < Data.maxActorId && x == 2 ^ n
then n + 1
else n
getStep :: Word -> Word -> Word -> Word -> BinaryBit.BitGet Word
getStep limit maxBits position value = do
let x = Bits.shiftL 1 (fromIntegral position)
if position < maxBits && value + x <= limit
then do
(bit :: Boolean.Boolean) <- BinaryBit.getBits 0
let newValue =
if #unpack bit
then value + x
else value
getStep limit maxBits (position + 1) newValue
else pure value