From fbd053967c814697ad21fde2dc8e95f7a4683302 Mon Sep 17 00:00:00 2001 From: joe Date: Thu, 20 Jul 2017 00:01:29 -0400 Subject: Update MinMaxPSQ to use HashPSQ. --- src/Data/MinMaxPSQ.hs | 24 ++++++++++++------------ src/Network/BitTorrent/DHT/Search.hs | 13 ++++++++----- 2 files changed, 20 insertions(+), 17 deletions(-) (limited to 'src') diff --git a/src/Data/MinMaxPSQ.hs b/src/Data/MinMaxPSQ.hs index 96937604..f41da4a4 100644 --- a/src/Data/MinMaxPSQ.hs +++ b/src/Data/MinMaxPSQ.hs @@ -17,48 +17,48 @@ null (MinMaxPSQ nq xq) = PSQ.null nq size :: MinMaxPSQ k p -> Int size (MinMaxPSQ nq xq) = PSQ.size nq -toList :: (Ord k, Ord p) => MinMaxPSQ k p -> [Binding k p] +toList :: (PSQKey k, Ord p) => MinMaxPSQ k p -> [Binding k p] toList (MinMaxPSQ nq xq) = PSQ.toList nq -fromList :: (Ord k, Ord p) => [Binding k p] -> MinMaxPSQ k p +fromList :: (PSQKey k, Ord p) => [Binding k p] -> MinMaxPSQ k p fromList kps = MinMaxPSQ (PSQ.fromList kps) (PSQ.fromList $ map (\(k :-> p) -> (k :-> Down p)) kps) -findMin :: (Ord k, Ord p) => MinMaxPSQ k p -> Maybe (Binding k p) +findMin :: (PSQKey k, Ord p) => MinMaxPSQ k p -> Maybe (Binding k p) findMin (MinMaxPSQ nq xq) = PSQ.findMin nq -findMax :: (Ord k, Ord p) => MinMaxPSQ k p -> Maybe (Binding k p) +findMax :: (PSQKey k, Ord p) => MinMaxPSQ k p -> Maybe (Binding k p) findMax (MinMaxPSQ nq xq) = fmap (\(k :-> Down p) -> k :-> p) $ PSQ.findMin xq -insert :: (Ord k, Ord p) => k -> p -> MinMaxPSQ k p -> MinMaxPSQ k p +insert :: (PSQKey k, Ord p) => k -> p -> MinMaxPSQ k p -> MinMaxPSQ k p insert k p (MinMaxPSQ nq xq) = MinMaxPSQ (PSQ.insert k p nq) (PSQ.insert k (Down p) xq) -delete :: (Ord k, Ord p) => k -> MinMaxPSQ k p -> MinMaxPSQ k p +delete :: (PSQKey k, Ord p) => k -> MinMaxPSQ k p -> MinMaxPSQ k p delete k (MinMaxPSQ nq xq) = MinMaxPSQ (PSQ.delete k nq) (PSQ.delete k xq) -deleteMin :: (Ord k, Ord p) => MinMaxPSQ k p -> MinMaxPSQ k p +deleteMin :: (PSQKey k, Ord p) => MinMaxPSQ k p -> MinMaxPSQ k p deleteMin (MinMaxPSQ nq xq) = case PSQ.minView nq of Just (k :-> _, nq') -> MinMaxPSQ nq' (PSQ.delete k xq) Nothing -> MinMaxPSQ nq xq -deleteMax :: (Ord k, Ord p) => MinMaxPSQ k p -> MinMaxPSQ k p +deleteMax :: (PSQKey k, Ord p) => MinMaxPSQ k p -> MinMaxPSQ k p deleteMax (MinMaxPSQ nq xq) = case PSQ.minView xq of Just (k :-> _, xq') -> MinMaxPSQ (PSQ.delete k nq) xq' Nothing -> MinMaxPSQ nq xq -minView :: (Ord k, Ord p) => MinMaxPSQ k p -> Maybe (Binding k p, MinMaxPSQ k p) +minView :: (PSQKey k, Ord p) => MinMaxPSQ k p -> Maybe (Binding k p, MinMaxPSQ k p) minView (MinMaxPSQ nq xq) = fmap (\(k :-> p, nq') -> (k :-> p, MinMaxPSQ nq' (PSQ.delete k xq))) $ PSQ.minView nq -maxView :: (Ord k, Ord p) => MinMaxPSQ k p -> Maybe (Binding k p, MinMaxPSQ k p) +maxView :: (PSQKey k, Ord p) => MinMaxPSQ k p -> Maybe (Binding k p, MinMaxPSQ k p) maxView (MinMaxPSQ nq xq) = fmap (\(k :-> Down p, xq') -> (k :-> p, MinMaxPSQ (PSQ.delete k nq) xq')) $ PSQ.minView xq -insertTake :: (Ord k, Ord p) => Int -> k -> p -> MinMaxPSQ k p -> MinMaxPSQ k p +insertTake :: (PSQKey k, Ord p) => Int -> k -> p -> MinMaxPSQ k p -> MinMaxPSQ k p insertTake n k p q = take n $ insert k p q -take :: (Ord k, Ord p) => Int -> MinMaxPSQ k p -> MinMaxPSQ k p +take :: (PSQKey k, Ord p) => Int -> MinMaxPSQ k p -> MinMaxPSQ k p take !n !q | (size q <= n) = q | null q = q | otherwise = take n $ deleteMax q diff --git a/src/Network/BitTorrent/DHT/Search.hs b/src/Network/BitTorrent/DHT/Search.hs index 356f6fd9..07240755 100644 --- a/src/Network/BitTorrent/DHT/Search.hs +++ b/src/Network/BitTorrent/DHT/Search.hs @@ -23,7 +23,7 @@ import System.IO import qualified Data.MinMaxPSQ as MM ;import Data.MinMaxPSQ (MinMaxPSQ) import qualified Data.Wrapper.PSQ as PSQ - ;import Data.Wrapper.PSQ (pattern (:->), Binding, PSQ) + ;import Data.Wrapper.PSQ (pattern (:->), Binding, PSQ, PSQKey) import Network.Address hiding (NodeId) import Network.DatagramServer.Types import Data.Bits @@ -39,7 +39,8 @@ data IterativeSearch dht u ip r = IterativeSearch } newSearch :: ( Eq ip - , Ord (NodeId dht) + , PSQKey (NodeId dht) + , PSQKey (NodeInfo dht ip u) , FiniteBits (NodeId dht) ) => (NodeInfo dht ip u -> IO ([NodeInfo dht ip u], [r])) -> NodeId dht -> [NodeInfo dht ip u] -> IO (IterativeSearch dht u ip r) @@ -60,7 +61,8 @@ searchK = 8 sendQuery :: forall a ip dht u. ( Ord a , Ord ip - , Ord (NodeId dht) + , PSQKey (NodeId dht) + , PSQKey (NodeInfo dht ip u) , FiniteBits (NodeId dht) ) => IterativeSearch dht u ip a @@ -85,7 +87,8 @@ sendQuery IterativeSearch{..} (ni :-> d) = do searchIsFinished :: ( Ord ip - , Ord (NodeId dht) + , PSQKey (NodeId dht) + , PSQKey (NodeInfo dht ip u) ) => IterativeSearch dht u ip r -> STM Bool searchIsFinished IterativeSearch{..} = do q <- readTVar searchQueued @@ -98,7 +101,7 @@ searchIsFinished IterativeSearch{..} = do <= PSQ.prio (fromJust $ MM.findMin q)))) search :: - (Ord r, Ord ip, Ord (NodeId dht), FiniteBits (NodeId dht)) => + (Ord r, Ord ip, PSQKey (NodeId dht), PSQKey (NodeInfo dht ip u), FiniteBits (NodeId dht)) => IterativeSearch dht u ip r -> IO () search s@IterativeSearch{..} = withTaskGroup searchAlpha $ \g -> do fix $ \again -> do -- cgit v1.2.3