diff options
author | Sam Truzjan <pxqr.sta@gmail.com> | 2013-10-31 19:17:08 +0400 |
---|---|---|
committer | Sam Truzjan <pxqr.sta@gmail.com> | 2013-10-31 19:17:08 +0400 |
commit | 0853bc857b370ed5f7a453a18a7b03b28cd3b410 (patch) | |
tree | 0437b785e9f91d53dfda170bdb48a9cd395d97da | |
parent | e3b584173f9a58c4c662ec5e933e97c09334910e (diff) |
Introduce PieceCount newtype
-rw-r--r-- | src/Data/Torrent/Block.hs | 1 | ||||
-rw-r--r-- | src/Data/Torrent/Piece.hs | 29 |
2 files changed, 15 insertions, 15 deletions
diff --git a/src/Data/Torrent/Block.hs b/src/Data/Torrent/Block.hs index e0507aed..ca3bef45 100644 --- a/src/Data/Torrent/Block.hs +++ b/src/Data/Torrent/Block.hs | |||
@@ -56,6 +56,7 @@ newtype BlockSize = BlockSize { unBlockSize :: Int } | |||
56 | -- | Widely used semi-official block size. | 56 | -- | Widely used semi-official block size. |
57 | instance Default BlockSize where | 57 | instance Default BlockSize where |
58 | def = 16 * 1024 | 58 | def = 16 * 1024 |
59 | {-# INLINE def #-} | ||
59 | 60 | ||
60 | type BlockLIx = Int | 61 | type BlockLIx = Int |
61 | type PieceLIx = Int | 62 | type PieceLIx = Int |
diff --git a/src/Data/Torrent/Piece.hs b/src/Data/Torrent/Piece.hs index 58bc9ebc..96624729 100644 --- a/src/Data/Torrent/Piece.hs +++ b/src/Data/Torrent/Piece.hs | |||
@@ -12,14 +12,11 @@ | |||
12 | {-# LANGUAGE GeneralizedNewtypeDeriving #-} | 12 | {-# LANGUAGE GeneralizedNewtypeDeriving #-} |
13 | module Data.Torrent.Piece | 13 | module Data.Torrent.Piece |
14 | ( -- * Piece attributes | 14 | ( -- * Piece attributes |
15 | -- ** Piece size | 15 | PieceIx |
16 | PieceSize (..) | 16 | , PieceCount |
17 | , optimalPieceCount | 17 | , PieceSize (..) |
18 | , defaultPieceSize | 18 | , defaultPieceSize |
19 | 19 | ||
20 | -- ** Piece index | ||
21 | , PieceIx | ||
22 | |||
23 | -- * Piece data | 20 | -- * Piece data |
24 | , Piece (..) | 21 | , Piece (..) |
25 | , ppPiece | 22 | , ppPiece |
@@ -33,7 +30,6 @@ module Data.Torrent.Piece | |||
33 | , pieceCount | 30 | , pieceCount |
34 | , checkPieceLazy | 31 | , checkPieceLazy |
35 | 32 | ||
36 | |||
37 | -- * Internal | 33 | -- * Internal |
38 | , getPieceInfo | 34 | , getPieceInfo |
39 | , putPieceInfo | 35 | , putPieceInfo |
@@ -65,9 +61,15 @@ import Data.Torrent.Block | |||
65 | class Lint a where | 61 | class Lint a where |
66 | lint :: a -> Either String a | 62 | lint :: a -> Either String a |
67 | 63 | ||
68 | type PieceCount = Int -- TODO newtype | ||
69 | type PieceIx = Int -- TODO remove | 64 | type PieceIx = Int -- TODO remove |
70 | 65 | ||
66 | newtype PieceCount = PieceCount { unPieceCount :: Int } | ||
67 | |||
68 | -- | TODO | ||
69 | instance Default PieceCount where | ||
70 | def = PieceCount 1000 | ||
71 | {-# INLINE def #-} | ||
72 | |||
71 | newtype PieceIndex = PieceIndex Int | 73 | newtype PieceIndex = PieceIndex Int |
72 | 74 | ||
73 | -- | An int used to denote piece size. | 75 | -- | An int used to denote piece size. |
@@ -97,10 +99,6 @@ instance Bounded PieceSize where | |||
97 | minBound = PieceSize minPieceSize | 99 | minBound = PieceSize minPieceSize |
98 | {-# INLINE minBound #-} | 100 | {-# INLINE minBound #-} |
99 | 101 | ||
100 | -- | TODO | ||
101 | optimalPieceCount :: Int | ||
102 | optimalPieceCount = 1000 | ||
103 | {-# INLINE optimalPieceCount #-} | ||
104 | 102 | ||
105 | toPow2 :: Int -> Int | 103 | toPow2 :: Int -> Int |
106 | toPow2 x = bit $ fromIntegral (leadingZeros (0 :: Int) - leadingZeros x) | 104 | toPow2 x = bit $ fromIntegral (leadingZeros (0 :: Int) - leadingZeros x) |
@@ -109,7 +107,7 @@ toPow2 x = bit $ fromIntegral (leadingZeros (0 :: Int) - leadingZeros x) | |||
109 | defaultPieceSize :: Int64 -> Int | 107 | defaultPieceSize :: Int64 -> Int |
110 | defaultPieceSize x = max minPieceSize $ min maxPieceSize $ toPow2 pc | 108 | defaultPieceSize x = max minPieceSize $ min maxPieceSize $ toPow2 pc |
111 | where | 109 | where |
112 | pc = fromIntegral (x `div` fromIntegral optimalPieceCount) | 110 | pc = fromIntegral (x `div` fromIntegral (unPieceCount def)) |
113 | 111 | ||
114 | -- TODO check if pieceLength is power of 2 | 112 | -- TODO check if pieceLength is power of 2 |
115 | -- | Piece payload should be strict or lazy bytestring. | 113 | -- | Piece payload should be strict or lazy bytestring. |
@@ -201,10 +199,11 @@ pieceHash PieceInfo {..} i = slice (hashsize * i) hashsize (unHashArray piPieceH | |||
201 | -- | Find count of pieces in the torrent. If torrent size is not a | 199 | -- | Find count of pieces in the torrent. If torrent size is not a |
202 | -- multiple of piece size then the count is rounded up. | 200 | -- multiple of piece size then the count is rounded up. |
203 | pieceCount :: PieceInfo -> PieceCount | 201 | pieceCount :: PieceInfo -> PieceCount |
204 | pieceCount PieceInfo {..} = BS.length (unHashArray piPieceHashes) `quot` hashsize | 202 | pieceCount PieceInfo {..} |
203 | = PieceCount (BS.length (unHashArray piPieceHashes) `quot` hashsize) | ||
205 | 204 | ||
206 | isLastPiece :: PieceInfo -> PieceIx -> Bool | 205 | isLastPiece :: PieceInfo -> PieceIx -> Bool |
207 | isLastPiece ci i = pieceCount ci == succ i | 206 | isLastPiece ci i = unPieceCount (pieceCount ci) == succ i |
208 | 207 | ||
209 | class Validation a where | 208 | class Validation a where |
210 | validate :: PieceInfo -> Piece a -> Bool | 209 | validate :: PieceInfo -> Piece a -> Bool |