summaryrefslogtreecommitdiff
path: root/src/Data
diff options
context:
space:
mode:
authorjoe <joe@jerkface.net>2017-07-19 18:54:43 -0400
committerjoe <joe@jerkface.net>2017-07-19 18:54:43 -0400
commit28ca61179a618a294702912505c70bac49cb98a0 (patch)
tree7340678ba139c54264f8d887f0a8773463e8c1b0 /src/Data
parentf626282407525533ee4f46196f8fbffcd41079db (diff)
Made Wrapper.PSQ a little more flexible.
Diffstat (limited to 'src/Data')
-rw-r--r--src/Data/Wrapper/PSQ.hs37
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 #-}
3module Data.Wrapper.PSQ 4module Data.Wrapper.PSQ
4#if 0 5#if 0
5 ( module Data.PSQueue ) where ; import Data.PSQueue 6 ( module Data.PSQueue ) where
7
8import Data.PSQueue hiding (foldr, foldl)
9import qualified Data.PSQueue as PSQueue
10
11-- | Wrapper over PSQueue-style foldr to implement a psqueues-style interface.
12fold' :: (Ord k, Ord p) => (k -> p -> () -> a -> a) -> a -> PSQ k p -> a
13fold' 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
9import Data.OrdPSQ as OrdPSQ hiding (insert, map, singleton, minView) 20import Data.OrdPSQ as OrdPSQ hiding (insert, map, singleton, minView)
10import qualified Data.OrdPSQ as OrdPSQ 21import qualified Data.OrdPSQ as Q
22
23import Data.HashPSQ as HashPSQ hiding (insert, map, singleton, minView)
24import qualified Data.HashPSQ as HashPSQ
25import Data.Hashable
11 26
12type PSQ k p = OrdPSQ k p () 27type PSQ k p = OrdPSQ k p ()
13type Binding k p = (k,p,()) 28type Binding k p = (k,p,())
14 29
30type PSQKey k = (Ord k)
31
15pattern (:->) :: k -> p -> Binding k p 32pattern (:->) :: k -> p -> Binding k p
16pattern k :-> p <- (k,p,()) where k :-> p = (k,p,()) 33pattern k :-> p <- (k,p,()) where k :-> p = (k,p,())
17 34
@@ -23,23 +40,23 @@ prio :: Binding k p -> p
23prio (k,p,v) = p 40prio (k,p,v) = p
24{-# INLINE prio #-} 41{-# INLINE prio #-}
25 42
26insert :: (Ord k, Ord p) => k -> p -> PSQ k p -> PSQ k p 43insert :: (PSQKey k, Ord p) => k -> p -> PSQ k p -> PSQ k p
27insert k p q = OrdPSQ.insert k p () q 44insert k p q = Q.insert k p () q
28{-# INLINE insert #-} 45{-# INLINE insert #-}
29 46
30insertWith :: (Ord k, Ord p) => (p -> p -> p) -> k -> p -> PSQ k p -> PSQ k p 47insertWith :: (PSQKey k, Ord p) => (p -> p -> p) -> k -> p -> PSQ k p -> PSQ k p
31insertWith f k p0 q = snd $ OrdPSQ.alter f' k q 48insertWith 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
37singleton :: (Ord k, Ord p) => k -> p -> PSQ k p 54singleton :: (PSQKey k, Ord p) => k -> p -> PSQ k p
38singleton k p = OrdPSQ.singleton k p () 55singleton k p = Q.singleton k p ()
39{-# INLINE singleton #-} 56{-# INLINE singleton #-}
40 57
41minView :: (Ord k, Ord p) => PSQ k p -> Maybe (Binding k p, PSQ k p) 58minView :: (PSQKey k, Ord p) => PSQ k p -> Maybe (Binding k p, PSQ k p)
42minView q = fmap (\(k,p,(),q') -> (k :-> p, q')) $ OrdPSQ.minView q 59minView q = fmap (\(k,p,(),q') -> (k :-> p, q')) $ Q.minView q
43{-# INLINE minView #-} 60{-# INLINE minView #-}
44 61
45#endif 62#endif