From 74c047ad77261ca4867ed17487a3fa31cd3f7c10 Mon Sep 17 00:00:00 2001 From: Alex Horsman Date: Fri, 2 Nov 2012 20:58:49 +0000 Subject: [PATCH 1/5] Fixed use of a literal for a Bits value. Num is no longer a superclass of Bits, so the literal wasn't valid. --- src/Data/Vector/Bit.hs | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/src/Data/Vector/Bit.hs b/src/Data/Vector/Bit.hs index 321443f..120d89f 100644 --- a/src/Data/Vector/Bit.hs +++ b/src/Data/Vector/Bit.hs @@ -107,10 +107,12 @@ unpack w = trimLeading $ V.generate (bitSize w) (testBit w) -- | Converts a 'BitVector' to an instance of 'Bits'. pack :: (Bits a) => BitVector -> a -pack v = V.ifoldl' set 0 v +pack v = V.ifoldl' set zero v where set w i True = w `setBit` i set w _ _ = w + -- Currently the best way to represent an empty Bits instance (Ugh) + zero = clearBit (bit 0) 0 unpackInteger :: Integer -> BitVector unpackInteger = V.unfoldr f From 772d3ed25b33cd1dc1b057b178df27b25fe42053 Mon Sep 17 00:00:00 2001 From: Alex Horsman Date: Fri, 2 Nov 2012 21:02:15 +0000 Subject: [PATCH 2/5] Added zipPadWith to go with zipPad. --- src/Data/Vector/Bit.hs | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/src/Data/Vector/Bit.hs b/src/Data/Vector/Bit.hs index 120d89f..d1f90df 100644 --- a/src/Data/Vector/Bit.hs +++ b/src/Data/Vector/Bit.hs @@ -57,6 +57,11 @@ padMax xs ys = (padlen xs, padlen ys) zipPad :: BitVector -> BitVector -> V.Vector (Bool, Bool) zipPad xs ys = uncurry V.zip (padMax xs ys) +-- | Like 'V.zipWith', except pads the vectors to equal length +-- rather than discarding elements of the longer vector. +zipPadWith :: V.Unbox a => (Bool -> Bool -> a) -> BitVector -> BitVector -> V.Vector a +zipPadWith f xs ys = uncurry (V.zipWith f) (padMax xs ys) + -- | Discards any 'False' values at the most-significant end of the -- given 'BitVector'. trimLeading :: BitVector -> BitVector From ef2e51383657a7304c7e648bdcf8fd782fd309a2 Mon Sep 17 00:00:00 2001 From: Alex Horsman Date: Fri, 2 Nov 2012 21:10:39 +0000 Subject: [PATCH 3/5] Modifed (.|.) and xor to work with mismatched BitVector sizes. --- src/Data/Vector/Bit.hs | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/Data/Vector/Bit.hs b/src/Data/Vector/Bit.hs index d1f90df..b0385c1 100644 --- a/src/Data/Vector/Bit.hs +++ b/src/Data/Vector/Bit.hs @@ -90,8 +90,8 @@ instance Num BitVector where instance Bits BitVector where (.&.) = V.zipWith (&&) - (.|.) = V.zipWith (||) - xor = V.zipWith (/=) + (.|.) = zipPadWith (||) + xor = zipPadWith (/=) complement = V.map not shiftL v i = V.replicate i False V.++ v shiftR = flip V.drop From a282fe1792e92e3fea65e8832b159fe27afe4e7c Mon Sep 17 00:00:00 2001 From: Alex Horsman Date: Fri, 2 Nov 2012 21:11:36 +0000 Subject: [PATCH 4/5] Added missing Bits methods for latest version. --- src/Data/Vector/Bit.hs | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/src/Data/Vector/Bit.hs b/src/Data/Vector/Bit.hs index b0385c1..cff97cc 100644 --- a/src/Data/Vector/Bit.hs +++ b/src/Data/Vector/Bit.hs @@ -101,6 +101,11 @@ instance Bits BitVector where where (low, high) = V.splitAt (V.length v - i) v bitSize = V.length isSigned = const False + bit i = V.replicate i False `V.snoc` True + testBit v i = case (v V.!? i) of + Just x -> x + Nothing -> False + popCount v = V.length $ V.filter id v -- | Converts an instance of 'Bits' to a 'BitVector'. -- From ffb6805206f3d45775285a8f8f32cb00f7642595 Mon Sep 17 00:00:00 2001 From: Alex Horsman Date: Fri, 2 Nov 2012 21:30:40 +0000 Subject: [PATCH 5/5] Exported zipPadWith. --- src/Data/Vector/Bit.hs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/Data/Vector/Bit.hs b/src/Data/Vector/Bit.hs index cff97cc..6400058 100644 --- a/src/Data/Vector/Bit.hs +++ b/src/Data/Vector/Bit.hs @@ -28,7 +28,7 @@ module Data.Vector.Bit ( unpackInteger, packInteger, unpackInt, packInt, -- * Utilities - pad, padMax, zipPad, trimLeading + pad, padMax, zipPad, zipPadWith, trimLeading ) where