diff options
author | Sam Truzjan <pxqr.sta@gmail.com> | 2014-02-25 17:27:34 +0400 |
---|---|---|
committer | Sam Truzjan <pxqr.sta@gmail.com> | 2014-02-25 17:27:34 +0400 |
commit | 22d6d17dc8b1d864ad0752f052bf89f622ada2de (patch) | |
tree | 3d64a37bcab016570cba2e1c8c91858c1f85f41d /src/Network/BitTorrent/Exchange/Block.hs | |
parent | 627b44ab54111322eeb273de87c6d8a24d7f37c1 (diff) |
Implement spans function
Diffstat (limited to 'src/Network/BitTorrent/Exchange/Block.hs')
-rw-r--r-- | src/Network/BitTorrent/Exchange/Block.hs | 20 |
1 files changed, 17 insertions, 3 deletions
diff --git a/src/Network/BitTorrent/Exchange/Block.hs b/src/Network/BitTorrent/Exchange/Block.hs index 32e4f99f..4f49c6c9 100644 --- a/src/Network/BitTorrent/Exchange/Block.hs +++ b/src/Network/BitTorrent/Exchange/Block.hs | |||
@@ -284,9 +284,23 @@ size bkt = go bkt | |||
284 | go (Span sz xs) = sz + go xs | 284 | go (Span sz xs) = sz + go xs |
285 | go (Fill sz _ xs) = sz + go xs | 285 | go (Fill sz _ xs) = sz + go xs |
286 | 286 | ||
287 | -- | /O(n)/. List incomplete blocks to download. | 287 | -- | /O(n)/. List incomplete blocks to download. If some block have |
288 | -- size more than the specified 'BlockSize' then block is split into | ||
289 | -- smaller blocks to satisfy given 'BlockSize'. Small (for | ||
290 | -- e.g. trailing) blocks is not ignored, but returned in-order. | ||
288 | spans :: BlockSize -> Bucket -> [(BlockOffset, BlockSize)] | 291 | spans :: BlockSize -> Bucket -> [(BlockOffset, BlockSize)] |
289 | spans = undefined | 292 | spans expectedSize = go 0 |
293 | where | ||
294 | go _ Nil = [] | ||
295 | go off (Span sz xs) = listChunks off sz ++ go (off + sz) xs | ||
296 | go off (Fill sz _ xs) = go (off + sz) xs | ||
297 | |||
298 | listChunks off restSize | ||
299 | | restSize <= 0 = [] | ||
300 | | otherwise = (off, blkSize) | ||
301 | : listChunks (off + blkSize) (restSize - blkSize) | ||
302 | where | ||
303 | blkSize = min expectedSize restSize | ||
290 | 304 | ||
291 | {----------------------------------------------------------------------- | 305 | {----------------------------------------------------------------------- |
292 | -- Bucket contstruction | 306 | -- Bucket contstruction |
@@ -336,7 +350,7 @@ insertLazy pos bl = Network.BitTorrent.Exchange.Block.insert pos (BL.toStrict bl | |||
336 | 350 | ||
337 | -- | /O(n)/. | 351 | -- | /O(n)/. |
338 | merge :: Bucket -> Bucket -> Bucket | 352 | merge :: Bucket -> Bucket -> Bucket |
339 | merge = undefined | 353 | merge = error "Bucket.merge: not implemented" |
340 | 354 | ||
341 | -- | /O(1)/. | 355 | -- | /O(1)/. |
342 | toPiece :: Bucket -> Maybe BL.ByteString | 356 | toPiece :: Bucket -> Maybe BL.ByteString |