From 950d728dc12302858f0c20d9890dc97975f4e9a9 Mon Sep 17 00:00:00 2001 From: Sam T Date: Sun, 5 May 2013 03:50:07 +0400 Subject: ~ Minor changes. --- src/Network/BitTorrent/PeerWire/Bitfield.hs | 12 ++++++++++-- 1 file changed, 10 insertions(+), 2 deletions(-) (limited to 'src/Network/BitTorrent/PeerWire/Bitfield.hs') diff --git a/src/Network/BitTorrent/PeerWire/Bitfield.hs b/src/Network/BitTorrent/PeerWire/Bitfield.hs index 2baeb516..9d88e784 100644 --- a/src/Network/BitTorrent/PeerWire/Bitfield.hs +++ b/src/Network/BitTorrent/PeerWire/Bitfield.hs @@ -18,6 +18,7 @@ module Network.BitTorrent.PeerWire.Bitfield -- * Construction , empty, full + , toList , fromByteString, toByteString -- * Query @@ -62,6 +63,13 @@ full :: Int -> Bitfield full n = MkBitfield $ B.replicate (sizeInBase n 8) (complement 0) {-# INLINE full #-} +toList :: Bitfield -> [Bool] +toList (MkBitfield bs) = concatMap unpkg (B.unpack bs) + where + unpkg :: Word8 -> [Bool] + unpkg byte = L.map (testBit byte) [0..bitSize (undefined :: Word8) - 1] +{-# INLINE toList #-} + fromByteString :: ByteString -> Bitfield fromByteString = MkBitfield {-# INLINE fromByteString #-} @@ -278,5 +286,5 @@ findMax (MkBitfield b) = do {-# INLINE findMax #-} -frequencies :: [Bitfield] -> UArray PieceIx Int -frequencies = undefined +frequencies :: [Bitfield] -> [Int] +frequencies xs = foldr1 (zipWith (+)) $ map (map fromEnum . toList) xs -- cgit v1.2.3