h$`>]]      !"#$%&'()*+,-./0123456789:;<=>?@ABCDEFGHIJKLM None d NOPQRSTUVW(c) 2020 Andrew LelechenkoBSD3NoneXYNoneZbitvecThe number of bits in a [1. A handy constant to have around when defining [&-based bulk operations on bit vectors.\bitvecInsert 0 between each consecutive bits of an input. xyzw --> (x0y0, z0w0)Z]^_`abcdefghij\klmnNone&38 Nbitvec-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 o (which stores one value per byte). but random writes are up to 20% slower.pbitvecread 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.qbitvecread 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.rbitvecwrite 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 s  t#, but up to 33% faster and atomic.!In general there is no reason to ) bit vectors: either you modify it with u (which is u altogether) or with t (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 s   t#, but up to 33% faster and atomic.!In general there is no reason to  ) bit vectors: either you modify it with u (which is u altogether) or with t (which is ).?Data.Vector.Unboxed.modify (\v -> flipBit v 1) (read "[1,1,1]")[1,0,1]vbitvecThere is only one lawful w instance possible with x = y and z =  . {. pq|r 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.  .}bitvec2Clone a vector of bits to a new unboxed vector of ~9. If the bits don't completely fill the words, the last ~ 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. } 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 o (which stores one value per byte). but random writes are up to 10% 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.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 bounds checks are performed. Equivalent to s  t, but up to 2x faster.!In general there is no reason to ) bit vectors: either you modify it with u (which is u altogether) or with t (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 s   t, but up to 2x faster.!In general there is no reason to  ) bit vectors: either you modify it with u (which is u altogether) or with t (which is ).:set -XOverloadedLists2Data.Vector.Unboxed.modify (`flipBit` 2) [1,1,1,1] [1,1,0,1]bitvecThere is only one lawful w instance possible with x = y and z =  . {. None?'q 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.  .bitvec2Clone a vector of bits to a new unboxed vector of ~9. If the bits don't completely fill the words, the last ~ 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.  !None >?<5"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 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 a unboxed vector of ~ to an unboxed vector of bits.On big-endian architectures %; resorts to copying instead of aliasing underlying arrays.: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 ~4. 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.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 flag libgmp! 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!None >?Q4bitvecCast 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 a 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 a unboxed vector of ~ to an unboxed vector of bits.On big-endian architectures 7; resorts to copying instead of aliasing underlying arrays.: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 ~4. It succeeds if a 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 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))AbitvecReverse 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.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 flag libgmp! 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"None38VWFbitvec: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 :: F2Poly0b1001GbitvecConvert F to a vector of coefficients (first element corresponds to a constant term).:set -XBinaryLiteralsunF2Poly 0b1101 [1,0,1,1]HbitvecMake F from a list of coefficients (first element corresponds to a constant term).:set -XOverloadedListstoF2Poly [1,0,1,1,0,0]0b1101Ibitvec;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 F, to  encoding.bitvec3Addition and multiplication are evaluated modulo 2. = u and  =  1.z* converts a binary polynomial, encoded as , to F encoding.FGHI5(c) 2019-2020 Andrew Lelechenko, 2012-2016 James CookBSD3/Andrew Lelechenko NoneV( "#$%&'()*+,-./0123FGHI("#$%&'()*+,/0123-. FGHI(None38\zJbitvec: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 :: F2Poly0b1001KbitvecConvert J to a vector of coefficients (first element corresponds to a constant term).:set -XBinaryLiteralsunF2Poly 0b1101 [1,0,1,1]LbitvecMake J from a list of coefficients (first element corresponds to a constant term).:set -XOverloadedListstoF2Poly [1,0,1,1,0,0]0b1101Mbitvec;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 J, to  encoding.bitvec3Addition and multiplication are evaluated modulo 2. = u and  =  1.z* converts a binary polynomial, encoded as , to J encoding.JKLM5(c) 2019-2020 Andrew Lelechenko, 2012-2016 James CookBSD3/Andrew Lelechenko None]( !456789:;<=>?@ABCDEJKLM(456789:;<=>ABCDE?@! JKLM)*+)*#,,-./01    2 3 4 5 6 7,,-./01234567   89:;<=>?@A! ! ! !8!9!!:!;!!!<!!!=!>!?!@!A"B"C"D"E(B(C(D(EFGHIJKLMNOPQRSTUVWXYZ[\]^_`abcdefghSTijklmnompqmnrsmtumtvmpwmtxmyz{ |m}~jkls{|S"my"mtmtmn((%bitvec-1.1.2.0-9FzyExlCmYDGjTa9mTWGj3Data.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.F2Poly&vector-0.12.3.1-BS9vrRx3ry325IASWLF2UHData.Vector.Unboxed.BaseMVectorBitunBitBitVecBitMVec unsafeFlipBitflipBit zipInPlace mapInPlace invertInPlaceselectBitsInPlaceexcludeBitsInPlacereverseInPlacecastFromWords8 castToWords8cloneFromByteStringcloneToByteString invertBits reverseBitsbitIndex nthBitIndex countBitslistBitsF2PolyunF2PolytoF2PolygcdExtmpnCom mpnPopcountmpnAndNmpnIorNmpnXorNmpnAndnNmpnIornNmpnNandNmpnNiorNmpnXnorNpdeppextwordSizeghc-prim GHC.TypesWord sparseBits lgWordSize divWordSize modWordSize mulWordSizenWords wordsToBytesalignedalignUpmaskedmeld reverseWordreversePartialWordffs selectWordloMaskhiMaskfromPrimVector toPrimVectorBool indexWordreadWord writeWordbaseGHC.Baseflip Data.Bits complementid$fNumBitGHC.NumNum+xor fromIntegerGHC.RealoddmodifyByteArraycloneToWords8MGHC.WordWord8bytestring-0.10.10.0Data.ByteString.Internal ByteString GHC.ClassesOrd$fIntegralF2Poly toIntegerinteger-wired-inGHC.Integer.TypeInteger $fNumF2Polyabssignumconst