summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--src/Data/Torrent/Bitfield.hs29
1 files changed, 13 insertions, 16 deletions
diff --git a/src/Data/Torrent/Bitfield.hs b/src/Data/Torrent/Bitfield.hs
index b14094fb..02a4c14f 100644
--- a/src/Data/Torrent/Bitfield.hs
+++ b/src/Data/Torrent/Bitfield.hs
@@ -66,12 +66,13 @@ module Data.Torrent.Bitfield
66 , intersection 66 , intersection
67 , difference 67 , difference
68 68
69 -- * Serialization 69 -- * Conversion
70 , fromBitmap, toBitmap
71 , toList 70 , toList
71 , fromList
72 72
73 -- * Debug 73 -- * Serialization
74 , mkBitfield 74 , fromBitmap
75 , toBitmap
75 ) where 76 ) where
76 77
77import Control.Monad 78import Control.Monad
@@ -268,10 +269,17 @@ unions = {-# SCC unions #-} foldl' union (haveNone 0)
268 Serialization 269 Serialization
269-----------------------------------------------------------------------} 270-----------------------------------------------------------------------}
270 271
271-- | List all have indexes. 272-- | List all /have/ indexes.
272toList :: Bitfield -> [PieceIx] 273toList :: Bitfield -> [PieceIx]
273toList Bitfield {..} = S.toList bfSet 274toList Bitfield {..} = S.toList bfSet
274 275
276-- | Make bitfield from list of /have/ indexes.
277fromList :: PieceCount -> [PieceIx] -> Bitfield
278fromList s ixs = Bitfield {
279 bfSize = s
280 , bfSet = S.splitGT (-1) $ S.splitLT s $ S.fromList ixs
281 }
282
275-- | Unpack 'Bitfield' from tightly packed bit array. Note resulting 283-- | Unpack 'Bitfield' from tightly packed bit array. Note resulting
276-- size might be more than real bitfield size, use 'adjustSize'. 284-- size might be more than real bitfield size, use 'adjustSize'.
277fromBitmap :: ByteString -> Bitfield 285fromBitmap :: ByteString -> Bitfield
@@ -288,14 +296,3 @@ toBitmap Bitfield {..} = {-# SCC toBitmap #-} Lazy.fromChunks [intsetBM, alignme
288 byteSize = bfSize `div` 8 + if bfSize `mod` 8 == 0 then 0 else 1 296 byteSize = bfSize `div` 8 + if bfSize `mod` 8 == 0 then 0 else 1
289 alignment = B.replicate (byteSize - B.length intsetBM) 0 297 alignment = B.replicate (byteSize - B.length intsetBM) 0
290 intsetBM = S.toByteString bfSet 298 intsetBM = S.toByteString bfSet
291
292{-----------------------------------------------------------------------
293 Debug
294-----------------------------------------------------------------------}
295
296-- | For internal use only.
297mkBitfield :: PieceCount -> [PieceIx] -> Bitfield
298mkBitfield s ixs = Bitfield {
299 bfSize = s
300 , bfSet = S.splitGT (-1) $ S.splitLT s $ S.fromList ixs
301 }