diff options
author | joe <joe@jerkface.net> | 2017-07-19 18:54:43 -0400 |
---|---|---|
committer | joe <joe@jerkface.net> | 2017-07-19 18:54:43 -0400 |
commit | 28ca61179a618a294702912505c70bac49cb98a0 (patch) | |
tree | 7340678ba139c54264f8d887f0a8773463e8c1b0 /src/Data | |
parent | f626282407525533ee4f46196f8fbffcd41079db (diff) |
Made Wrapper.PSQ a little more flexible.
Diffstat (limited to 'src/Data')
-rw-r--r-- | src/Data/Wrapper/PSQ.hs | 37 |
1 files changed, 27 insertions, 10 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 |