summaryrefslogtreecommitdiff
path: root/psq-wrap/src/Data/Wrapper/PSQInt.hs
blob: 5badb8b2c0363c766a378f89753a5d3da691082b (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
{-# LANGUAGE PatternSynonyms #-}
{-# LANGUAGE CPP #-}
{-# LANGUAGE ConstraintKinds #-}
module Data.Wrapper.PSQInt
#if 0
     ( module Data.Wrapper.PSQInt , module Data.PSQueue ) where

import Data.PSQueue hiding (foldr, foldl, PSQ)
import qualified Data.PSQueue as PSQueue

type PSQ p = PSQueue.PSQ Int p

-- | Wrapper over PSQueue-style foldr to implement a psqueues-style interface.
fold' :: (Ord p) => (Int -> p -> () -> a -> a) -> a -> PSQ p -> a
fold' f a q = PSQueue.foldr f' a q
 where
    f' (k :-> prio) x = f k prio () x

#else
    ( module Data.Wrapper.PSQInt
    , module IntPSQ
    , module Data.Wrapper.PSQ
    ) where

import Data.Wrapper.PSQ (Binding, pattern (:->), key, prio, toMicroseconds)

import Data.IntPSQ as IntPSQ hiding (insert, map, singleton, minView)
import qualified Data.IntPSQ as Q

type PSQ p = IntPSQ p ()

type PSQKey = ()

insert :: (Ord p) => Int -> p -> PSQ p -> PSQ p
insert k p q = Q.insert k p () q
{-# INLINE insert #-}

insertWith :: (Ord p) => (p -> p -> p) -> Int -> p -> PSQ p -> PSQ p
insertWith f k p0 q = snd $ Q.alter f' k q
 where
    f' (Just (p,())) = ((),Just (f p0 p, ()))
    f' Nothing      = ((),Nothing)
{-# INLINE insertWith #-}

singleton :: (Ord p) => Int -> p -> PSQ p
singleton k p = Q.singleton k p ()
{-# INLINE singleton #-}

minView :: (Ord p) => PSQ p -> Maybe (Binding Int p, PSQ p)
minView q = fmap (\(k,p,(),q') -> (k :-> p, q')) $ Q.minView q
{-# INLINE minView #-}

#endif