flat-0.4.4: Principled and efficient bit-oriented binary serialization.

Safe HaskellSafe
LanguageHaskell2010

Data.ZigZag

Description

ZigZag encoding of signed integrals.

Synopsis
  • class (Integral signed, Integral unsigned) => ZigZag signed unsigned | unsigned -> signed, signed -> unsigned where

Documentation

class (Integral signed, Integral unsigned) => ZigZag signed unsigned | unsigned -> signed, signed -> unsigned where Source #

Convert between a signed integral and the corresponding ZigZag encoded unsigned integral (e.g. between Int8 and Word8 or Integral and Natural).

Invalid conversions produce a type error:

zigZag (-1::Int64) :: Word32 ... ... Couldn't match type ... ...

>>> zigZag (0::Int8)
0
>>> zigZag (-1::Int16)
1
>>> zigZag (1::Int32)
2
>>> zigZag (-2::Int16)
3
>>> zigZag (-50::Integer)
99
>>> zigZag (50::Integer)
100
>>> zigZag (64::Integer)
128
>>> zigZag (-256::Integer)
511
>>> zigZag (256::Integer)
512
>>> map zigZag [-3..3::Integer]
[5,3,1,0,2,4,6]
>>> map zagZig [0..6::Word8]
[0,-1,1,-2,2,-3,3]
\(f::Integer) -> zagZig (zigZag f) == f
\(f::Natural) -> zigZag (zagZig f) == f
\(f::Int8) -> zagZig (zigZag f) == f
\(f::Word8) -> zigZag (zagZig f) == f
\(s::Int8) -> zigZag s == fromIntegral (zigZag (fromIntegral s :: Integer))
\(u::Word8) -> zagZig u == fromIntegral (zagZig (fromIntegral u :: Natural))
\(f::Int64) -> zagZig (zigZag f) == f
\(f::Word64) -> zigZag (zagZig f) == f
\(s::Int64) -> zigZag s == fromIntegral (zigZag (fromIntegral s :: Integer))
\(u::Word64) -> zagZig u == fromIntegral (zagZig (fromIntegral u :: Natural))

Minimal complete definition

Nothing

Methods

zigZag :: signed -> unsigned Source #

zigZag :: FiniteBits signed => signed -> unsigned Source #

zagZig :: unsigned -> signed Source #

zagZig :: Bits unsigned => unsigned -> signed Source #

Instances
ZigZag Int8 Word8 Source # 
Instance details

Defined in Data.ZigZag

ZigZag Int16 Word16 Source # 
Instance details

Defined in Data.ZigZag

ZigZag Int32 Word32 Source # 
Instance details

Defined in Data.ZigZag

ZigZag Int64 Word64 Source # 
Instance details

Defined in Data.ZigZag

ZigZag Integer Natural Source # 
Instance details

Defined in Data.ZigZag