h&fQcT      !"#$%&'()*+,-./0123456789:;<=>?@ABCDEFGHIJKLM (c) 2022 Andrew LelechenkoBSD3 Safe-InferredNO Safe-InferredPbitvecThe number of bits in a Q1. A handy constant to have around when defining Q&-based bulk operations on bit vectors.RbitvecThe base 2 logarithm of P.SbitvecInsert 0 between each consecutive bits of an input. xyzw --> (x0y0, z0w0)PRTUVWXYZ[\]^_`Sabcd Safe-Inferred(6; bitvec.A newtype wrapper with a custom instance for Data.Vector.Unboxed, which packs booleans as efficient as possible (8 values per byte). Unboxed vectors of - use 8x less memory than unboxed vectors of e (which store one value per byte), but random writes are slightly slower.bitvecfbitvecRead 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.gbitvecRead 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.hbitvecWrite 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 bound checks are performed. Equivalent to i  j#, but up to 33% faster and atomic.!In general there is no reason to ) bit vectors: either you modify it with k (which is k altogether) or with j (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 i  j#, but up to 33% faster and atomic.!In general there is no reason to ) bit vectors: either you modify it with k (which is k altogether) or with j (which is ).?Data.Vector.Unboxed.modify (\v -> flipBit v 1) (read "[1,1,1]")[1,0,1]lbitvecmbitvecnbitvecobitvecThere is only one lawful p instance possible with q = r and s =  . t.ubitvecvbitvecwbitvec fgxh  Safe-Inferred3 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 the 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.  .ybitvec2Clone a vector of bits to a new unboxed vector of z9. If the bits don't completely fill the words, the last z will be zero-padded. Cf.  . bitvecZip two vectors with the given function, rewriting the 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 the 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 the number of selected bits. It is the 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 the number of excluded bits. It is the 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 the  1https://hackage.haskell.org/package/vector-rotcev vector-rotcev* package to reverse vectors in O(1) time. y  Safe-Inferred(6;bbitvec.A newtype wrapper with a custom instance for Data.Vector.Unboxed, which packs booleans as efficient as possible (8 values per byte). Unboxed vectors of - use 8x less memory than unboxed vectors of e (which store one value per byte), but random writes are slightly slower.bitvec{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.|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.}bitvecWrite 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 bound checks are performed. Equivalent to i  j, but up to 2x faster.!In general there is no reason to ) bit vectors: either you modify it with k (which is k altogether) or with j (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 i  j, but up to 2x faster.!In general there is no reason to ) bit vectors: either you modify it with k (which is k altogether) or with j (which is ).:set -XOverloadedLists2Data.Vector.Unboxed.modify (`flipBit` 2) [1,1,1,1] [1,1,0,1]~bitvecbitvecbitvecbitvecThere is only one lawful p instance possible with q = r and s =  . t.bitvecbitvecbitvec {|} Safe-Inferred) 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 the 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.  .bitvec2Clone a vector of bits to a new unboxed vector of z9. If the bits don't completely fill the words, the last z will be zero-padded. Cf.  .bitvecZip two vectors with the given function, rewriting the 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 the 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 the number of selected bits. It is the 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 the number of excluded bits. It is the 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 the  1https://hackage.haskell.org/package/vector-rotcev vector-rotcev* package to reverse vectors in O(1) time.  ! Safe-Inferred ?"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 the vector of bits is aligned. Use $ otherwise. Cf. . 'castToWords (castFromWords v) == Just v$bitvecClone 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 an unboxed vector of z to an unboxed vector of bits.On big-endian architectures %> resorts to copying instead of aliasing the underlying array.: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 z6. It succeeds if the 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 the libgmp& flag 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  https://en.wikipedia.org/wiki/X86_Bit_manipulation_instruction_set#Parallel_bit_deposit_and_extract'parallel bit 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 the  1https://hackage.haskell.org/package/vector-rotcev vector-rotcev* package to reverse vectors in O(1) time.0bitvecReturn 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 01bitvecReturn 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 1 to implement to implement  select{0,1} queries for  5https://en.wikipedia.org/wiki/Succinct_data_structuresuccinct dictionaries.2bitvecReturn the number of set bits in a vector (population count, popcount).-Users are strongly encouraged to enable the libgmp& flag for the ultimate performance of 2.:set -XOverloadedListscountBits [1,1,0,1,0,1]4One can combine 2 with  to implement  rank{0,1} queries for  5https://en.wikipedia.org/wiki/Succinct_data_structuresuccinct dictionaries.3bitvec/Return 0-based indices of set bits in a vector.:set -XOverloadedListslistBits [1,1,0,1,0,1] [0,1,3,5]"#$%&'()*+,-./0123  Safe-Inferred Uc4bitvecCast 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]5bitvecTry to cast an unboxed vector of bits to an unboxed vector of words. It succeeds if the vector of bits is aligned. Use 6 otherwise. Cf. . 'castToWords (castFromWords v) == Just v6bitvecClone 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]7bitvecCast an unboxed vector of z to an unboxed vector of bits.On big-endian architectures 7> resorts to copying instead of aliasing the underlying array.:set -XOverloadedListscastFromWords8 [123][1,1,0,1,1,1,1,0]8bitvec?Try to cast an unboxed vector of bits to an unboxed vector of z6. It succeeds if the vector of bits is aligned. Use   otherwise. )castToWords8 (castFromWords8 v) == Just v9bitvec 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 the libgmp& flag 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  https://en.wikipedia.org/wiki/X86_Bit_manipulation_instruction_set#Parallel_bit_deposit_and_extract'parallel bit 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))AbitvecReverse the order of bits.:set -XOverloadedListsreverseBits [1,1,0,1,0] [0,1,0,1,1]Consider using the  1https://hackage.haskell.org/package/vector-rotcev vector-rotcev* package to reverse vectors in O(1) time.BbitvecReturn 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 0CbitvecReturn 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 C to implement to implement  select{0,1} queries for  5https://en.wikipedia.org/wiki/Succinct_data_structuresuccinct dictionaries.DbitvecReturn the number of set bits in a vector (population count, popcount).-Users are strongly encouraged to enable the libgmp& flag for the ultimate performance of D.:set -XOverloadedListscountBits [1,1,0,1,0,1]4One can combine D with  to implement  rank{0,1} queries for  5https://en.wikipedia.org/wiki/Succinct_data_structuresuccinct dictionaries.Ebitvec/Return 0-based indices of set bits in a vector.:set -XOverloadedListslistBits [1,1,0,1,0,1] [0,1,3,5]456789:;<=>?@ABCDE! Safe-Inferred6;[{Fbitvec:Binary polynomials of one variable, backed by an unboxed " .Polynomials are stored normalized, without leading zero coefficients.The  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 :: F2Poly0b1001Gbitvec Convert an F to a vector of coefficients (first element corresponds to a constant term).:set -XBinaryLiteralsunF2Poly 0b1101 [1,0,1,1]HbitvecMake an F from a list of coefficients (first element corresponds to a constant term).:set -XOverloadedListstoF2Poly [1,0,1,1,0,0]0b1101bitvecInputs must be valid for wrapping into F2Poly: no trailing garbage is allowed.bitvecMust be >= 2 * wordSize.Ibitvec;Execute the extended Euclidean algorithm. For polynomials a and b/, compute their unique greatest common divisor g( and the unique coefficient polynomial s satisfying  a \cdot s + b \cdot t = g .:set -XBinaryLiteralsgcdExt 0b101 0b0101 (0b101,0b0)gcdExt 0b11 0b111 (0b1,0b10)bitvec* converts a binary polynomial, encoded as F , to an  encoding.bitvec3Addition and multiplication are evaluated modulo 2. = k and  =  1.s* converts a binary polynomial, encoded as , to F encoding.FGHI5(c) 2019-2022 Andrew Lelechenko, 2012-2016 James CookBSD3/Andrew Lelechenko  Safe-Inferred\( "#$%&'()*+,-./0123FGHI("#$%&'()*+,/0123-. FGHI' Safe-Inferred6;bhJbitvec:Binary polynomials of one variable, backed by an unboxed " .Polynomials are stored normalized, without leading zero coefficients.The  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 :: F2Poly0b1001Kbitvec Convert an J to a vector of coefficients (first element corresponds to a constant term).:set -XBinaryLiteralsunF2Poly 0b1101 [1,0,1,1]LbitvecMake an J from a list of coefficients (first element corresponds to a constant term).:set -XOverloadedListstoF2Poly [1,0,1,1,0,0]0b1101bitvecInputs must be valid for wrapping into F2Poly: no trailing garbage is allowed.bitvecMust be >= 2 * wordSize.Mbitvec;Execute the extended Euclidean algorithm. For polynomials a and b/, compute their unique greatest common divisor g( and the unique coefficient polynomial s satisfying  a \cdot s + b \cdot t = g .:set -XBinaryLiteralsgcdExt 0b101 0b0101 (0b101,0b0)gcdExt 0b11 0b111 (0b1,0b10)bitvec* converts a binary polynomial, encoded as J , to an  encoding.bitvec3Addition and multiplication are evaluated modulo 2. = k and  =  1.s* converts a binary polynomial, encoded as , to J encoding.JKLM5(c) 2019-2022 Andrew Lelechenko, 2012-2016 James CookBSD3/Andrew Lelechenko  Safe-Inferredb( !456789:;<=>?@ABCDEJKLM(456789:;<=>ABCDE?@! JKLM()*()"++,-./0    1 2 3 4 5 6++,-./0123456   78 9:;<=>?@ 7 8 9 :   ;   < = > ? @!A!B!C!D'A'B'C'DEFGHIJKLMNOPQRSTUVWXYZ[\]HI^_`abcdbefbcghijkblmblnbeoblpbqrstuv wbxy_`ahijkstuvwz{|H}~!!!bq!blblbc''''%bitvec-1.1.4.0-5ewzE2DXhMS2I6vHnZHkyxData.Bit.ThreadSafeData.BitData.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.F2Poly&vector-0.13.0.0-JKrBPPZBIK2JBM2KZEUb7ZData.Vector.Unboxed.BaseMVectorBitunBitBitVecBitMVec unsafeFlipBitflipBit zipInPlace mapInPlace invertInPlaceselectBitsInPlaceexcludeBitsInPlacereverseInPlacecastFromWords8 castToWords8cloneFromByteStringcloneToByteString invertBits reverseBitsbitIndex nthBitIndex countBitslistBitsF2PolyunF2PolytoF2PolygcdExtpdeppextwordSizeghc-prim GHC.TypesWord lgWordSize sparseBits divWordSize modWordSize mulWordSizenWords wordsToBytesalignedalignUpmaskedmeld reverseWordreversePartialWordffs selectWordloMaskhiMaskfromPrimVector toPrimVectorBool indexWordreadWord writeWordbaseGHC.BaseflipGHC.Bits complementid$fFractionalBit $fIntegralBit $fRealBit$fNumBitGHC.NumNum+xor fromIntegerGHC.Realodd$fFiniteBitsBit $fNFDataBitRep_BitmodifyByteArraycloneToWords8MGHC.WordWord8bytestring-0.11.3.1Data.ByteString.Internal ByteString GHC.ClassesOrdxorBitskaratsubaThreshold$fIntegralF2Poly toInteger ghc-bignumGHC.Num.IntegerInteger $fNumF2Polyabssignumconst