h$^\R      !"#$%&'()*+,-./0123456789:;<=>?@ABCDEFG  Safe-Inferred V(c) 2020 Andrew LelechenkoBSD3NoneHINoneJbitvecThe number of bits in a K1. A handy constant to have around when defining K&-based bulk operations on bit vectors.LbitvecInsert 0 between each consecutive bits of an input. xyzw --> (x0y0, z0w0)JMNOPQRSTUVWXYZL[\]^None&38 6bitvec-A newtype wrapper with a custom instance of Data.Vector.Unboxed, which packs booleans as efficient as possible (8 values per byte). Vectors of % use 8x less memory than vectors of _ (which stores one value per byte). but random writes are up to 20% slower.`bitvecread a word at the given bit offset in little-endian order (i.e., the LSB will correspond to the bit at the given address, the 2's bit will correspond to the address + 1, etc.). If the offset is such that the word extends past the end of the vector, the result is padded with memory garbage.abitvecread a word at the given bit offset in little-endian order (i.e., the LSB will correspond to the bit at the given address, the 2's bit will correspond to the address + 1, etc.). If the offset is such that the word extends past the end of the vector, the result is padded with memory garbage.bbitvecwrite a word at the given bit offset in little-endian order (i.e., the LSB will correspond to the bit at the given address, the 2's bit will correspond to the address + 1, etc.). If the offset is such that the word extends past the end of the vector, the word is truncated and as many low-order bits as possible are written.bitvecFlip the bit at the given position. No bounds checks are performed. Equivalent to c  d#, but up to 33% faster and atomic.!In general there is no reason to ) bit vectors: either you modify it with e (which is e altogether) or with d (which is ).Data.Vector.Unboxed.modify (\v -> unsafeFlipBit v 1) (read "[1,1,1]")[1,0,1]bitvec3Flip the bit at the given position. Equivalent to c   d#, but up to 33% faster and atomic.!In general there is no reason to  ) bit vectors: either you modify it with e (which is e altogether) or with d (which is ).?Data.Vector.Unboxed.modify (\v -> flipBit v 1) (read "[1,1,1]")[1,0,1]fbitvecThere is only one lawful g instance possible with h = i and j =  . k. lmno`apb None? bitvec1Cast a vector of words to a vector of bits. Cf.  .bitvecTry to cast a vector of bits to a vector of words. It succeeds if a vector of bits is aligned. Use  otherwise. Cf.  .bitvecClone a vector of bits to a new unboxed vector of words. If the bits don't completely fill the words, the last word will be zero-padded. Cf.  .qbitvec2Clone a vector of bits to a new unboxed vector of r9. If the bits don't completely fill the words, the last r will be zero-padded. Cf. .bitvecZip two vectors with the given function. rewriting contents of the second argument. Cf. .:set -XOverloadedListsimport Data.Bits=Data.Vector.Unboxed.modify (zipInPlace (.&.) [1,1,0]) [0,1,1][0,1,0]Warning: if the immutable vector is shorter than the mutable one, it is a caller's responsibility to trim the result::set -XOverloadedListsimport Data.BitsData.Vector.Unboxed.modify (zipInPlace (.&.) [1,1,0]) [0,1,1,1,1,1]&[0,1,0,1,1,1] -- note trailing garbage bitvecApply a function to a mutable vector bitwise, rewriting its contents. Cf. .:set -XOverloadedListsimport Data.Bits:Data.Vector.Unboxed.modify (mapInPlace complement) [0,1,1][1,0,0] bitvec Invert (flip) all bits in-place.:set -XOverloadedLists4Data.Vector.Unboxed.modify invertInPlace [0,1,0,1,0] [1,0,1,0,1] bitvecSame as , but deposit selected bits in-place. Returns a number of selected bits. It is caller's responsibility to trim the result to this number.:set -XOverloadedListsimport Control.Monad.ST (runST))import qualified Data.Vector.Unboxed as UrunST $ do { vec <- U.unsafeThaw [1,1,0,0,1]; n <- selectBitsInPlace [0,1,0,1,1] vec; U.take n <$> U.unsafeFreeze vec }[1,0,1] bitvecSame as , but deposit excluded bits in-place. Returns a number of excluded bits. It is caller's responsibility to trim the result to this number.:set -XOverloadedListsimport Control.Monad.ST (runST))import qualified Data.Vector.Unboxed as UrunST $ do { vec <- U.unsafeThaw [1,1,0,0,1]; n <- excludeBitsInPlace [0,1,0,1,1] vec; U.take n <$> U.unsafeFreeze vec }[1,0] bitvec#Reverse the order of bits in-place.:set -XOverloadedLists5Data.Vector.Unboxed.modify reverseInPlace [1,1,0,1,0] [0,1,0,1,1]Consider using  vector-rotcev* package to reverse vectors in O(1) time. q None&38bitvec-A newtype wrapper with a custom instance of Data.Vector.Unboxed, which packs booleans as efficient as possible (8 values per byte). Vectors of % use 8x less memory than vectors of _ (which stores one value per byte). but random writes are up to 10% slower.sbitvecread a word at the given bit offset in little-endian order (i.e., the LSB will correspond to the bit at the given address, the 2's bit will correspond to the address + 1, etc.). If the offset is such that the word extends past the end of the vector, the result is padded with memory garbage.tbitvecread a word at the given bit offset in little-endian order (i.e., the LSB will correspond to the bit at the given address, the 2's bit will correspond to the address + 1, etc.). If the offset is such that the word extends past the end of the vector, the result is padded with memory garbage.ubitvecwrite a word at the given bit offset in little-endian order (i.e., the LSB will correspond to the bit at the given address, the 2's bit will correspond to the address + 1, etc.). If the offset is such that the word extends past the end of the vector, the word is truncated and as many low-order bits as possible are written.bitvecFlip the bit at the given position. No bounds checks are performed. Equivalent to c  d, but up to 2x faster.!In general there is no reason to ) bit vectors: either you modify it with e (which is e altogether) or with d (which is ).:set -XOverloadedLists8Data.Vector.Unboxed.modify (`unsafeFlipBit` 2) [1,1,1,1] [1,1,0,1]bitvec3Flip the bit at the given position. Equivalent to c   d, but up to 2x faster.!In general there is no reason to  ) bit vectors: either you modify it with e (which is e altogether) or with d (which is ).:set -XOverloadedLists2Data.Vector.Unboxed.modify (`flipBit` 2) [1,1,1,1] [1,1,0,1]vbitvecThere is only one lawful g instance possible with h = i and j =  . k. lwnxstyuNone?'N bitvec1Cast a vector of words to a vector of bits. Cf.  .bitvecTry to cast a vector of bits to a vector of words. It succeeds if a vector of bits is aligned. Use  otherwise. Cf.  .bitvecClone a vector of bits to a new unboxed vector of words. If the bits don't completely fill the words, the last word will be zero-padded. Cf.  .zbitvec2Clone a vector of bits to a new unboxed vector of r9. If the bits don't completely fill the words, the last r will be zero-padded. Cf. .bitvecZip two vectors with the given function. rewriting contents of the second argument. Cf. .:set -XOverloadedListsimport Data.Bits=Data.Vector.Unboxed.modify (zipInPlace (.&.) [1,1,0]) [0,1,1][0,1,0]Warning: if the immutable vector is shorter than the mutable one, it is a caller's responsibility to trim the result::set -XOverloadedListsimport Data.BitsData.Vector.Unboxed.modify (zipInPlace (.&.) [1,1,0]) [0,1,1,1,1,1]&[0,1,0,1,1,1] -- note trailing garbagebitvecApply a function to a mutable vector bitwise, rewriting its contents. Cf. .:set -XOverloadedListsimport Data.Bits:Data.Vector.Unboxed.modify (mapInPlace complement) [0,1,1][1,0,0]bitvec Invert (flip) all bits in-place.:set -XOverloadedLists4Data.Vector.Unboxed.modify invertInPlace [0,1,0,1,0] [1,0,1,0,1]bitvecSame as , but deposit selected bits in-place. Returns a number of selected bits. It is caller's responsibility to trim the result to this number.:set -XOverloadedListsimport Control.Monad.ST (runST))import qualified Data.Vector.Unboxed as UrunST $ do { vec <- U.unsafeThaw [1,1,0,0,1]; n <- selectBitsInPlace [0,1,0,1,1] vec; U.take n <$> U.unsafeFreeze vec }[1,0,1]bitvecSame as , but deposit excluded bits in-place. Returns a number of excluded bits. It is caller's responsibility to trim the result to this number.:set -XOverloadedListsimport Control.Monad.ST (runST))import qualified Data.Vector.Unboxed as UrunST $ do { vec <- U.unsafeThaw [1,1,0,0,1]; n <- excludeBitsInPlace [0,1,0,1,1] vec; U.take n <$> U.unsafeFreeze vec }[1,0]bitvec#Reverse the order of bits in-place.:set -XOverloadedLists5Data.Vector.Unboxed.modify reverseInPlace [1,1,0,1,0] [0,1,0,1,1]Consider using  vector-rotcev* package to reverse vectors in O(1) time. zNone>?;bitvecCast an unboxed vector of words to an unboxed vector of bits. Cf. .:set -XOverloadedListscastFromWords [123][1,1,0,1,1,1,1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0]bitvecTry to cast an unboxed vector of bits to an unboxed vector of words. It succeeds if a vector of bits is aligned. Use  otherwise. Cf. . 'castToWords (castFromWords v) == Just vbitvecClone an unboxed vector of bits to a new unboxed vector of words. If the bits don't completely fill the words, the last word will be zero-padded. Cf. .:set -XOverloadedListscloneToWords [1,1,0,1,1,1,1][123]bitvecCast a unboxed vector of r to an unboxed vector of bits.:set -XOverloadedListscastFromWords8 [123][1,1,0,1,1,1,1,0] bitvec?Try to cast an unboxed vector of bits to an unboxed vector of r4. It succeeds if a vector of bits is aligned. Use  otherwise. )castToWords8 (castFromWords8 v) == Just v!bitvec x .&. complement y) [1,1,0] [0,1,1] -- difference[1,0,0]3zipBits xor [1,1,0] [0,1,1] -- symmetric difference[1,0,1]%bitvec3Map a vectors with the given function. Similar to , but faster.:set -XOverloadedListsimport Data.BitsmapBits complement [0,1,1][1,0,0]&bitvecInvert (flip) all bits..Users are strongly encouraged to enable flag libgmp! for the ultimate performance of &.:set -XOverloadedListsinvertBits [0,1,0,1,0] [1,0,1,0,1]'bitvecFor each set bit of the first argument, deposit the corresponding bit of the second argument to the result. Similar to the parallel deposit instruction (PDEP).:set -XOverloadedLists"selectBits [0,1,0,1,1] [1,1,0,0,1][1,0,1].Here is a reference (but slow) implementation: import qualified Data.Vector.Unboxed as U selectBits mask ws == U.map snd (U.filter (unBit . fst) (U.zip mask ws))(bitvecFor each unset bit of the first argument, deposit the corresponding bit of the second argument to the result.:set -XOverloadedLists#excludeBits [0,1,0,1,1] [1,1,0,0,1][1,0].Here is a reference (but slow) implementation: import qualified Data.Vector.Unboxed as U excludeBits mask ws == U.map snd (U.filter (not . unBit . fst) (U.zip mask ws)))bitvecReverse the order of bits.:set -XOverloadedListsreverseBits [1,1,0,1,0] [0,1,0,1,1]Consider using  vector-rotcev* package to reverse vectors in O(1) time.*bitvecReturn the index of the first bit in the vector with the specified value, if any. Similar to , but up to 64x faster.:set -XOverloadedListsbitIndex 1 [0,0,1,0,1]Just 2bitIndex 1 [0,0,0,0,0]Nothing !bitIndex bit == nthBitIndex bit 1One can also use it to reduce a vector with disjunction or conjunction: import Data.Maybe isAnyBitSet = isJust . bitIndex 1 areAllBitsSet = isNothing . bitIndex 0+bitvecReturn the index of the n?-th bit in the vector with the specified value, if any. Here n4 is 1-based and the index is 0-based. Non-positive n results in an error.:set -XOverloadedLists5nthBitIndex 1 2 [0,1,0,1,1,1,0] -- 2nd occurence of 1Just 35nthBitIndex 1 5 [0,1,0,1,1,1,0] -- 5th occurence of 1Nothing One can use + to implement to implement  select{0,1} queries for  5https://en.wikipedia.org/wiki/Succinct_data_structuresuccinct dictionaries.,bitvecReturn the number of set bits in a vector (population count, popcount)..Users are strongly encouraged to enable flag libgmp! for the ultimate performance of ,.:set -XOverloadedListscountBits [1,1,0,1,0,1]4One can combine , with   to implement  rank{0,1} queries for  5https://en.wikipedia.org/wiki/Succinct_data_structuresuccinct dictionaries.-bitvec/Return 0-based indices of set bits in a vector.:set -XOverloadedListslistBits [1,1,0,1,0,1] [0,1,3,5] !"#$%&'()*+,-!None>?P .bitvecCast an unboxed vector of words to an unboxed vector of bits. Cf. .:set -XOverloadedListscastFromWords [123][1,1,0,1,1,1,1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0]/bitvecTry to cast an unboxed vector of bits to an unboxed vector of words. It succeeds if a vector of bits is aligned. Use 0 otherwise. Cf. . 'castToWords (castFromWords v) == Just v0bitvecClone an unboxed vector of bits to a new unboxed vector of words. If the bits don't completely fill the words, the last word will be zero-padded. Cf. .:set -XOverloadedListscloneToWords [1,1,0,1,1,1,1][123]1bitvecCast a unboxed vector of r to an unboxed vector of bits.:set -XOverloadedListscastFromWords8 [123][1,1,0,1,1,1,1,0]2bitvec?Try to cast an unboxed vector of bits to an unboxed vector of r4. It succeeds if a vector of bits is aligned. Use  otherwise. )castToWords8 (castFromWords8 v) == Just v3bitvec x .&. complement y) [1,1,0] [0,1,1] -- difference[1,0,0]3zipBits xor [1,1,0] [0,1,1] -- symmetric difference[1,0,1]7bitvec3Map a vectors with the given function. Similar to , but faster.:set -XOverloadedListsimport Data.BitsmapBits complement [0,1,1][1,0,0]8bitvecInvert (flip) all bits..Users are strongly encouraged to enable flag libgmp! for the ultimate performance of 8.:set -XOverloadedListsinvertBits [0,1,0,1,0] [1,0,1,0,1]9bitvecFor each set bit of the first argument, deposit the corresponding bit of the second argument to the result. Similar to the parallel deposit instruction (PDEP).:set -XOverloadedLists"selectBits [0,1,0,1,1] [1,1,0,0,1][1,0,1].Here is a reference (but slow) implementation: import qualified Data.Vector.Unboxed as U selectBits mask ws == U.map snd (U.filter (unBit . fst) (U.zip mask ws)):bitvecFor each unset bit of the first argument, deposit the corresponding bit of the second argument to the result.:set -XOverloadedLists#excludeBits [0,1,0,1,1] [1,1,0,0,1][1,0].Here is a reference (but slow) implementation: import qualified Data.Vector.Unboxed as U excludeBits mask ws == U.map snd (U.filter (not . unBit . fst) (U.zip mask ws));bitvecReverse the order of bits.:set -XOverloadedListsreverseBits [1,1,0,1,0] [0,1,0,1,1]Consider using  vector-rotcev* package to reverse vectors in O(1) time.<bitvecReturn the index of the first bit in the vector with the specified value, if any. Similar to , but up to 64x faster.:set -XOverloadedListsbitIndex 1 [0,0,1,0,1]Just 2bitIndex 1 [0,0,0,0,0]Nothing !bitIndex bit == nthBitIndex bit 1One can also use it to reduce a vector with disjunction or conjunction: import Data.Maybe isAnyBitSet = isJust . bitIndex 1 areAllBitsSet = isNothing . bitIndex 0=bitvecReturn the index of the n?-th bit in the vector with the specified value, if any. Here n4 is 1-based and the index is 0-based. Non-positive n results in an error.:set -XOverloadedLists5nthBitIndex 1 2 [0,1,0,1,1,1,0] -- 2nd occurence of 1Just 35nthBitIndex 1 5 [0,1,0,1,1,1,0] -- 5th occurence of 1Nothing One can use = to implement to implement  select{0,1} queries for  5https://en.wikipedia.org/wiki/Succinct_data_structuresuccinct dictionaries.>bitvecReturn the number of set bits in a vector (population count, popcount)..Users are strongly encouraged to enable flag libgmp! for the ultimate performance of >.:set -XOverloadedListscountBits [1,1,0,1,0,1]4One can combine > with   to implement  rank{0,1} queries for  5https://en.wikipedia.org/wiki/Succinct_data_structuresuccinct dictionaries.?bitvec/Return 0-based indices of set bits in a vector.:set -XOverloadedListslistBits [1,1,0,1,0,1] [0,1,3,5]./0123456789:;<=>?"None 38U`@bitvec:Binary polynomials of one variable, backed by an unboxed # .Polynomials are stored normalized, without leading zero coefficients.| instance does not make much sense mathematically, it is defined only for the sake of $%, &', etc.:set -XBinaryLiterals*-- (1 + x) (1 + x + x^2) = 1 + x^3 (mod 2)0b11 * 0b111 :: F2Poly0b1001AbitvecConvert @ to a vector of coefficients (first element corresponds to a constant term).:set -XBinaryLiteralsunF2Poly 0b1101 [1,0,1,1]BbitvecMake @ from a list of coefficients (first element corresponds to a constant term).:set -XOverloadedListstoF2Poly [1,0,1,1,0,0]0b1101Cbitvec;Execute the extended Euclidean algorithm. For polynomials a and b/, compute their unique greatest common divisor g( and the unique coefficient polynomial s satisfying  as + bt = g.:set -XBinaryLiteralsgcdExt 0b101 0b0101 (0b101,0b0)gcdExt 0b11 0b111 (0b1,0b10)}bitvec~* converts a binary polynomial, encoded as @, to  encoding.bitvec3Addition and multiplication are evaluated modulo 2. = e and  =  1.j* converts a binary polynomial, encoded as , to @ encoding.@ABC5(c) 2019-2020 Andrew Lelechenko, 2012-2016 James CookBSD3/Andrew Lelechenko NoneU$  !"#$%&'()*+,-@ABC$ !"#$%&)*+,-'( @ABC(None 38[xDbitvec:Binary polynomials of one variable, backed by an unboxed # .Polynomials are stored normalized, without leading zero coefficients.| instance does not make much sense mathematically, it is defined only for the sake of $%, &', etc.:set -XBinaryLiterals*-- (1 + x) (1 + x + x^2) = 1 + x^3 (mod 2)0b11 * 0b111 :: F2Poly0b1001EbitvecConvert D to a vector of coefficients (first element corresponds to a constant term).:set -XBinaryLiteralsunF2Poly 0b1101 [1,0,1,1]FbitvecMake D from a list of coefficients (first element corresponds to a constant term).:set -XOverloadedListstoF2Poly [1,0,1,1,0,0]0b1101Gbitvec;Execute the extended Euclidean algorithm. For polynomials a and b/, compute their unique greatest common divisor g( and the unique coefficient polynomial s satisfying  as + bt = g.:set -XBinaryLiteralsgcdExt 0b101 0b0101 (0b101,0b0)gcdExt 0b11 0b111 (0b1,0b10)bitvec~* converts a binary polynomial, encoded as D, to  encoding.bitvec3Addition and multiplication are evaluated modulo 2. = e and  =  1.j* converts a binary polynomial, encoded as , to D encoding.DEFG5(c) 2019-2020 Andrew Lelechenko, 2012-2016 James CookBSD3/Andrew Lelechenko None\$./0123456789:;<=>?DEFG$./012345678;<=>?9:DEFG))*+,    - . / 0 1 2))*+,-./012   3456789:;<! ! ! !3!4!!5!6!!!7!!!8!9!:!;!<"=">"?"@(=(>(?(@ABCDEFGHIJKLMNOPQRSTUVWXYDEZ[\]^_`^ab^_cd^ef^eg^ah^ei^jklmnolm#pq r^st[\]dopqruvwDxy"z^j{|}~"^e^e^_(z(%bitvec-1.1.0.0-3Fb5JcXEicFDbAZbPuPsznData.Bit.ThreadSafeData.Bit Data.Bit.GmpData.Bit.PdepPextData.Bit.UtilsData.Bit.InternalTSData.Vector.Unboxed.Mutable unsafeModifymodifyData.Bit.MutableTS castFromWords castToWords cloneToWords cloneToWords8zipBitsmapBits selectBits excludeBitsData.Bit.InternalData.Bit.MutableData.Bit.ImmutableTScastFromWordsM castToWordsM cloneToWordsMData.Vector.UnboxedzipWith Data.IntSetunion intersectionmap elemIndextakeData.Bit.ImmutableData.Bit.F2PolyTSVectorData.SetSetData.MapMapData.Bit.F2PolyBitunBit unsafeFlipBitflipBit zipInPlace mapInPlace invertInPlaceselectBitsInPlaceexcludeBitsInPlacereverseInPlacecastFromWords8 castToWords8cloneFromByteStringcloneToByteString invertBits reverseBitsbitIndex nthBitIndex countBitslistBitsF2PolyunF2PolytoF2PolygcdExtpdeppextwordSizeghc-prim GHC.TypesWord sparseBits lgWordSize divWordSize modWordSize mulWordSizenWords wordsToBytesalignedalignUpmaskedmeld reverseWordreversePartialWordffs selectWordloMaskhiMaskfromPrimVector toPrimVectorBool indexWordreadWord writeWordbaseGHC.Baseflip Data.Bits complementid$fNumBitGHC.NumNum+xor fromIntegerGHC.Realodd&vector-0.12.1.2-6jlbObSa8iuJfxUVGBQC5rData.Vector.Unboxed.BaseMVectorBitMVecBitVecmodifyByteArraycloneToWords8MGHC.WordWord8bytestring-0.10.10.0Data.ByteString.Internal ByteString GHC.ClassesOrd$fIntegralF2Poly toIntegerinteger-wired-inGHC.Integer.TypeInteger $fNumF2Polyabssignumconst