diff options
Diffstat (limited to 'src')
-rw-r--r-- | src/Data/Wrapper/PSQ.hs | 37 | ||||
-rw-r--r-- | src/Network/DHT/Routing.hs | 4 |
2 files changed, 29 insertions, 12 deletions
diff --git a/src/Data/Wrapper/PSQ.hs b/src/Data/Wrapper/PSQ.hs index 2c08011b..7e14008b 100644 --- a/src/Data/Wrapper/PSQ.hs +++ b/src/Data/Wrapper/PSQ.hs | |||
@@ -1,17 +1,34 @@ | |||
1 | {-# LANGUAGE PatternSynonyms #-} | 1 | {-# LANGUAGE PatternSynonyms #-} |
2 | {-# LANGUAGE CPP #-} | 2 | {-# LANGUAGE CPP #-} |
3 | {-# LANGUAGE ConstraintKinds #-} | ||
3 | module Data.Wrapper.PSQ | 4 | module Data.Wrapper.PSQ |
4 | #if 0 | 5 | #if 0 |
5 | ( module Data.PSQueue ) where ; import Data.PSQueue | 6 | ( module Data.PSQueue ) where |
7 | |||
8 | import Data.PSQueue hiding (foldr, foldl) | ||
9 | import qualified Data.PSQueue as PSQueue | ||
10 | |||
11 | -- | Wrapper over PSQueue-style foldr to implement a psqueues-style interface. | ||
12 | fold' :: (Ord k, Ord p) => (k -> p -> () -> a -> a) -> a -> PSQ k p -> a | ||
13 | fold' f a q = PSQueue.foldr f' a q | ||
14 | where | ||
15 | f' (k :-> prio) x = f k prio () x | ||
16 | |||
6 | #else | 17 | #else |
7 | ( module Data.Wrapper.PSQ , module OrdPSQ ) where | 18 | ( module Data.Wrapper.PSQ , module OrdPSQ ) where |
8 | 19 | ||
9 | import Data.OrdPSQ as OrdPSQ hiding (insert, map, singleton, minView) | 20 | import Data.OrdPSQ as OrdPSQ hiding (insert, map, singleton, minView) |
10 | import qualified Data.OrdPSQ as OrdPSQ | 21 | import qualified Data.OrdPSQ as Q |
22 | |||
23 | import Data.HashPSQ as HashPSQ hiding (insert, map, singleton, minView) | ||
24 | import qualified Data.HashPSQ as HashPSQ | ||
25 | import Data.Hashable | ||
11 | 26 | ||
12 | type PSQ k p = OrdPSQ k p () | 27 | type PSQ k p = OrdPSQ k p () |
13 | type Binding k p = (k,p,()) | 28 | type Binding k p = (k,p,()) |
14 | 29 | ||
30 | type PSQKey k = (Ord k) | ||
31 | |||
15 | pattern (:->) :: k -> p -> Binding k p | 32 | pattern (:->) :: k -> p -> Binding k p |
16 | pattern k :-> p <- (k,p,()) where k :-> p = (k,p,()) | 33 | pattern k :-> p <- (k,p,()) where k :-> p = (k,p,()) |
17 | 34 | ||
@@ -23,23 +40,23 @@ prio :: Binding k p -> p | |||
23 | prio (k,p,v) = p | 40 | prio (k,p,v) = p |
24 | {-# INLINE prio #-} | 41 | {-# INLINE prio #-} |
25 | 42 | ||
26 | insert :: (Ord k, Ord p) => k -> p -> PSQ k p -> PSQ k p | 43 | insert :: (PSQKey k, Ord p) => k -> p -> PSQ k p -> PSQ k p |
27 | insert k p q = OrdPSQ.insert k p () q | 44 | insert k p q = Q.insert k p () q |
28 | {-# INLINE insert #-} | 45 | {-# INLINE insert #-} |
29 | 46 | ||
30 | insertWith :: (Ord k, Ord p) => (p -> p -> p) -> k -> p -> PSQ k p -> PSQ k p | 47 | insertWith :: (PSQKey k, Ord p) => (p -> p -> p) -> k -> p -> PSQ k p -> PSQ k p |
31 | insertWith f k p0 q = snd $ OrdPSQ.alter f' k q | 48 | insertWith f k p0 q = snd $ Q.alter f' k q |
32 | where | 49 | where |
33 | f' (Just (p,())) = ((),Just (f p0 p, ())) | 50 | f' (Just (p,())) = ((),Just (f p0 p, ())) |
34 | f' Nothing = ((),Nothing) | 51 | f' Nothing = ((),Nothing) |
35 | {-# INLINE insertWith #-} | 52 | {-# INLINE insertWith #-} |
36 | 53 | ||
37 | singleton :: (Ord k, Ord p) => k -> p -> PSQ k p | 54 | singleton :: (PSQKey k, Ord p) => k -> p -> PSQ k p |
38 | singleton k p = OrdPSQ.singleton k p () | 55 | singleton k p = Q.singleton k p () |
39 | {-# INLINE singleton #-} | 56 | {-# INLINE singleton #-} |
40 | 57 | ||
41 | minView :: (Ord k, Ord p) => PSQ k p -> Maybe (Binding k p, PSQ k p) | 58 | minView :: (PSQKey k, Ord p) => PSQ k p -> Maybe (Binding k p, PSQ k p) |
42 | minView q = fmap (\(k,p,(),q') -> (k :-> p, q')) $ OrdPSQ.minView q | 59 | minView q = fmap (\(k,p,(),q') -> (k :-> p, q')) $ Q.minView q |
43 | {-# INLINE minView #-} | 60 | {-# INLINE minView #-} |
44 | 61 | ||
45 | #endif | 62 | #endif |
diff --git a/src/Network/DHT/Routing.hs b/src/Network/DHT/Routing.hs index 273cf9dc..d380c6f2 100644 --- a/src/Network/DHT/Routing.hs +++ b/src/Network/DHT/Routing.hs | |||
@@ -248,10 +248,10 @@ instance (Eq ip, Ord (NodeId), Serialize (NodeId), Serialize ip, Serialize u) => | |||
248 | 248 | ||
249 | #endif | 249 | #endif |
250 | 250 | ||
251 | psqFromPairList :: (Ord p, Ord k) => [(k, p)] -> OrdPSQ k p () | 251 | psqFromPairList :: (Ord p, Ord k) => [(k, p)] -> PSQ k p |
252 | psqFromPairList xs = PSQ.fromList $ map (\(a,b) -> a :-> b) xs | 252 | psqFromPairList xs = PSQ.fromList $ map (\(a,b) -> a :-> b) xs |
253 | 253 | ||
254 | psqToPairList :: OrdPSQ t t1 () -> [(t, t1)] | 254 | psqToPairList :: PSQ t t1 -> [(t, t1)] |
255 | psqToPairList psq = map (\(a :-> b) -> (a,b)) $ PSQ.toList psq | 255 | psqToPairList psq = map (\(a :-> b) -> (a,b)) $ PSQ.toList psq |
256 | 256 | ||
257 | -- | Update interval, in seconds. | 257 | -- | Update interval, in seconds. |