diff options
author | Sam Truzjan <pxqr.sta@gmail.com> | 2014-01-12 13:55:14 +0400 |
---|---|---|
committer | Sam Truzjan <pxqr.sta@gmail.com> | 2014-01-12 13:55:14 +0400 |
commit | a43a810ff24bc1ab946e5459c0f914aca286c62f (patch) | |
tree | b716159332dd71661da49fe361dd7daf2052a778 /src/Network/BitTorrent/Core | |
parent | e3d3fb375ca01aa844e86b8a4c5ca507919518d3 (diff) |
Unify all iterative queries
Diffstat (limited to 'src/Network/BitTorrent/Core')
-rw-r--r-- | src/Network/BitTorrent/Core/NodeInfo.hs | 7 |
1 files changed, 7 insertions, 0 deletions
diff --git a/src/Network/BitTorrent/Core/NodeInfo.hs b/src/Network/BitTorrent/Core/NodeInfo.hs index fe2357a4..2fd7e575 100644 --- a/src/Network/BitTorrent/Core/NodeInfo.hs +++ b/src/Network/BitTorrent/Core/NodeInfo.hs | |||
@@ -47,6 +47,7 @@ import Data.ByteString.Base16 as Base16 | |||
47 | import Data.BEncode as BE | 47 | import Data.BEncode as BE |
48 | import Data.Default | 48 | import Data.Default |
49 | import Data.Hashable | 49 | import Data.Hashable |
50 | import Data.Foldable | ||
50 | import Data.IP | 51 | import Data.IP |
51 | import Data.List as L | 52 | import Data.List as L |
52 | import Data.Monoid | 53 | import Data.Monoid |
@@ -125,6 +126,12 @@ genNodeId = NodeId <$> getEntropy nodeIdSize | |||
125 | newtype NodeDistance = NodeDistance BS.ByteString | 126 | newtype NodeDistance = NodeDistance BS.ByteString |
126 | deriving (Eq, Ord) | 127 | deriving (Eq, Ord) |
127 | 128 | ||
129 | instance Pretty NodeDistance where | ||
130 | pretty (NodeDistance bs) = foldMap bitseq $ BS.unpack bs | ||
131 | where | ||
132 | listBits w = L.map (testBit w) (L.reverse [0..bitSize w - 1]) | ||
133 | bitseq = foldMap (int . fromEnum) . listBits | ||
134 | |||
128 | -- | distance(A,B) = |A xor B| Smaller values are closer. | 135 | -- | distance(A,B) = |A xor B| Smaller values are closer. |
129 | distance :: NodeId -> NodeId -> NodeDistance | 136 | distance :: NodeId -> NodeId -> NodeDistance |
130 | distance (NodeId a) (NodeId b) = NodeDistance (BS.pack (BS.zipWith xor a b)) | 137 | distance (NodeId a) (NodeId b) = NodeDistance (BS.pack (BS.zipWith xor a b)) |