{-# LANGUAGE CPP #-}
{-# LANGUAGE BangPatterns #-}
{-# LANGUAGE StandaloneDeriving #-}
module BinomialQueue.Internals (
MinQueue (..),
BinomHeap,
BinomForest(..),
BinomTree(..),
Extract(..),
MExtract(..),
Succ(..),
Zero(..),
empty,
extractHeap,
null,
size,
getMin,
minView,
singleton,
insert,
insertEager,
union,
unionPlusOne,
mapMaybe,
mapEither,
mapMonotonic,
foldrAsc,
foldlAsc,
foldrDesc,
foldrUnfold,
foldlUnfold,
insertMinQ,
insertMinQ',
insertMaxQ',
toAscList,
toDescList,
toListU,
fromList,
mapU,
fromAscList,
foldMapU,
foldrU,
foldlU,
foldlU',
seqSpine,
unions
) where
import Control.DeepSeq (NFData(rnf), deepseq)
import Data.Foldable (foldl')
import Data.Function (on)
#if MIN_VERSION_base(4,9,0)
import Data.Semigroup (Semigroup(..), stimesMonoid)
#endif
import Data.PQueue.Internals.Foldable
#ifdef __GLASGOW_HASKELL__
import Data.Data
import Text.Read (Lexeme(Ident), lexP, parens, prec,
readPrec, readListPrec, readListPrecDefault)
import GHC.Exts (build)
#endif
import Prelude hiding (null)
#ifndef __GLASGOW_HASKELL__
build :: ((a -> [a] -> [a]) -> [a] -> [a]) -> [a]
build f = f (:) []
#endif
newtype MinQueue a = MinQueue (BinomHeap a)
#ifdef __GLASGOW_HASKELL__
instance (Ord a, Data a) => Data (MinQueue a) where
gfoldl :: forall (c :: * -> *).
(forall d b. Data d => c (d -> b) -> d -> c b)
-> (forall g. g -> c g) -> MinQueue a -> c (MinQueue a)
gfoldl forall d b. Data d => c (d -> b) -> d -> c b
f forall g. g -> c g
z MinQueue a
q = case forall a. Ord a => MinQueue a -> Maybe (a, MinQueue a)
minView MinQueue a
q of
Maybe (a, MinQueue a)
Nothing -> forall g. g -> c g
z forall a. MinQueue a
empty
Just (a
x, MinQueue a
q') -> forall g. g -> c g
z forall a. Ord a => a -> MinQueue a -> MinQueue a
insert forall d b. Data d => c (d -> b) -> d -> c b
`f` a
x forall d b. Data d => c (d -> b) -> d -> c b
`f` MinQueue a
q'
gunfold :: forall (c :: * -> *).
(forall b r. Data b => c (b -> r) -> c r)
-> (forall r. r -> c r) -> Constr -> c (MinQueue a)
gunfold forall b r. Data b => c (b -> r) -> c r
k forall r. r -> c r
z Constr
c = case Constr -> Int
constrIndex Constr
c of
Int
1 -> forall r. r -> c r
z forall a. MinQueue a
empty
Int
2 -> forall b r. Data b => c (b -> r) -> c r
k (forall b r. Data b => c (b -> r) -> c r
k (forall r. r -> c r
z forall a. a -> MinQueue a -> MinQueue a
insertMinQ))
Int
_ -> forall a. HasCallStack => [Char] -> a
error [Char]
"gunfold"
dataCast1 :: forall (t :: * -> *) (c :: * -> *).
Typeable t =>
(forall d. Data d => c (t d)) -> Maybe (c (MinQueue a))
dataCast1 forall d. Data d => c (t d)
x = forall {k1} {k2} (c :: k1 -> *) (t :: k2 -> k1) (t' :: k2 -> k1)
(a :: k2).
(Typeable t, Typeable t') =>
c (t a) -> Maybe (c (t' a))
gcast1 forall d. Data d => c (t d)
x
toConstr :: MinQueue a -> Constr
toConstr MinQueue a
q
| forall a. MinQueue a -> Bool
null MinQueue a
q = Constr
emptyConstr
| Bool
otherwise = Constr
consConstr
dataTypeOf :: MinQueue a -> DataType
dataTypeOf MinQueue a
_ = DataType
queueDataType
queueDataType :: DataType
queueDataType :: DataType
queueDataType = [Char] -> [Constr] -> DataType
mkDataType [Char]
"Data.PQueue.Min.MinQueue" [Constr
emptyConstr, Constr
consConstr]
emptyConstr, consConstr :: Constr
emptyConstr :: Constr
emptyConstr = DataType -> [Char] -> [[Char]] -> Fixity -> Constr
mkConstr DataType
queueDataType [Char]
"empty" [] Fixity
Prefix
consConstr :: Constr
consConstr = DataType -> [Char] -> [[Char]] -> Fixity -> Constr
mkConstr DataType
queueDataType [Char]
"<|" [] Fixity
Infix
#endif
type BinomHeap = BinomForest Zero
instance Ord a => Eq (MinQueue a) where
== :: MinQueue a -> MinQueue a -> Bool
(==) = forall a. Eq a => a -> a -> Bool
(==) forall b c a. (b -> b -> c) -> (a -> b) -> a -> a -> c
`on` forall a. Ord a => MinQueue a -> Maybe (a, MinQueue a)
minView
instance Ord a => Ord (MinQueue a) where
compare :: MinQueue a -> MinQueue a -> Ordering
compare = forall a. Ord a => a -> a -> Ordering
compare forall b c a. (b -> b -> c) -> (a -> b) -> a -> a -> c
`on` forall a. Ord a => MinQueue a -> Maybe (a, MinQueue a)
minView
data BinomForest rk a
= Nil
| Skip (BinomForest (Succ rk) a)
| Cons {-# UNPACK #-} !(BinomTree rk a) (BinomForest (Succ rk) a)
data BinomTree rk a = BinomTree !a !(rk a)
data Succ rk a = Succ {-# UNPACK #-} !(BinomTree rk a) !(rk a)
data Zero a = Zero
empty :: MinQueue a
empty :: forall a. MinQueue a
empty = forall a. BinomHeap a -> MinQueue a
MinQueue forall (rk :: * -> *) a. BinomForest rk a
Nil
null :: MinQueue a -> Bool
null :: forall a. MinQueue a -> Bool
null (MinQueue BinomForest Zero a
Nil) = Bool
True
null MinQueue a
_ = Bool
False
size :: MinQueue a -> Int
size :: forall a. MinQueue a -> Int
size (MinQueue BinomHeap a
hp) = forall (rk :: * -> *) a. Int -> Int -> BinomForest rk a -> Int
go Int
0 Int
1 BinomHeap a
hp
where
go :: Int -> Int -> BinomForest rk a -> Int
go :: forall (rk :: * -> *) a. Int -> Int -> BinomForest rk a -> Int
go Int
acc Int
rk BinomForest rk a
Nil = Int
rk seq :: forall a b. a -> b -> b
`seq` Int
acc
go Int
acc Int
rk (Skip BinomForest (Succ rk) a
f) = forall (rk :: * -> *) a. Int -> Int -> BinomForest rk a -> Int
go Int
acc (Int
2 forall a. Num a => a -> a -> a
* Int
rk) BinomForest (Succ rk) a
f
go Int
acc Int
rk (Cons BinomTree rk a
_t BinomForest (Succ rk) a
f) = forall (rk :: * -> *) a. Int -> Int -> BinomForest rk a -> Int
go (Int
acc forall a. Num a => a -> a -> a
+ Int
rk) (Int
2 forall a. Num a => a -> a -> a
* Int
rk) BinomForest (Succ rk) a
f
getMin :: Ord a => MinQueue a -> Maybe a
getMin :: forall a. Ord a => MinQueue a -> Maybe a
getMin MinQueue a
xs = case forall a. Ord a => MinQueue a -> Maybe (a, MinQueue a)
minView MinQueue a
xs of
Just (a
a, MinQueue a
_) -> forall a. a -> Maybe a
Just a
a
Maybe (a, MinQueue a)
Nothing -> forall a. Maybe a
Nothing
minView :: Ord a => MinQueue a -> Maybe (a, MinQueue a)
minView :: forall a. Ord a => MinQueue a -> Maybe (a, MinQueue a)
minView (MinQueue BinomHeap a
ts) = case forall a (rk :: * -> *). Ord a => BinomForest rk a -> MExtract rk a
extractBin BinomHeap a
ts of
MExtract Zero a
No -> forall a. Maybe a
Nothing
Yes (Extract a
x ~Zero a
Zero BinomHeap a
ts') -> forall a. a -> Maybe a
Just (a
x, forall a. BinomHeap a -> MinQueue a
MinQueue BinomHeap a
ts')
singleton :: a -> MinQueue a
singleton :: forall a. a -> MinQueue a
singleton a
x = forall a. BinomHeap a -> MinQueue a
MinQueue (forall (rk :: * -> *) a.
BinomTree rk a -> BinomForest (Succ rk) a -> BinomForest rk a
Cons (forall a. a -> BinomTree Zero a
tip a
x) forall (rk :: * -> *) a. BinomForest rk a
Nil)
insert :: Ord a => a -> MinQueue a -> MinQueue a
insert :: forall a. Ord a => a -> MinQueue a -> MinQueue a
insert a
x (MinQueue BinomHeap a
ts) = forall a. BinomHeap a -> MinQueue a
MinQueue (forall a (rk :: * -> *).
Ord a =>
BinomTree rk a -> BinomForest rk a -> BinomForest rk a
incr (forall a. a -> BinomTree Zero a
tip a
x) BinomHeap a
ts)
insertEager :: Ord a => a -> MinQueue a -> MinQueue a
insertEager :: forall a. Ord a => a -> MinQueue a -> MinQueue a
insertEager a
x (MinQueue BinomHeap a
ts) = forall a. BinomHeap a -> MinQueue a
MinQueue (forall a (rk :: * -> *).
Ord a =>
BinomTree rk a -> BinomForest rk a -> BinomForest rk a
incr' (forall a. a -> BinomTree Zero a
tip a
x) BinomHeap a
ts)
{-# INLINE insertEager #-}
union :: Ord a => MinQueue a -> MinQueue a -> MinQueue a
union :: forall a. Ord a => MinQueue a -> MinQueue a -> MinQueue a
union (MinQueue BinomHeap a
f1) (MinQueue BinomHeap a
f2) = forall a. BinomHeap a -> MinQueue a
MinQueue (forall a (rk :: * -> *).
Ord a =>
BinomForest rk a -> BinomForest rk a -> BinomForest rk a
merge BinomHeap a
f1 BinomHeap a
f2)
unions :: Ord a => [MinQueue a] -> MinQueue a
unions :: forall a. Ord a => [MinQueue a] -> MinQueue a
unions = forall (t :: * -> *) b a.
Foldable t =>
(b -> a -> b) -> b -> t a -> b
foldl' forall a. Ord a => MinQueue a -> MinQueue a -> MinQueue a
union forall a. MinQueue a
empty
mapMaybe :: Ord b => (a -> Maybe b) -> MinQueue a -> MinQueue b
mapMaybe :: forall b a. Ord b => (a -> Maybe b) -> MinQueue a -> MinQueue b
mapMaybe a -> Maybe b
f = forall a b c. (a -> b -> c) -> b -> a -> c
flip forall b a. (b -> a -> b) -> b -> MinQueue a -> b
foldlU' forall a. MinQueue a
empty forall a b. (a -> b) -> a -> b
$ \MinQueue b
q a
a ->
case a -> Maybe b
f a
a of
Maybe b
Nothing -> MinQueue b
q
Just b
b -> forall a. Ord a => a -> MinQueue a -> MinQueue a
insertEager b
b MinQueue b
q
{-# INLINABLE mapMaybe #-}
mapEither :: (Ord b, Ord c) => (a -> Either b c) -> MinQueue a -> (MinQueue b, MinQueue c)
mapEither :: forall b c a.
(Ord b, Ord c) =>
(a -> Either b c) -> MinQueue a -> (MinQueue b, MinQueue c)
mapEither a -> Either b c
f = forall a b. Partition a b -> (MinQueue a, MinQueue b)
fromPartition forall b c a. (b -> c) -> (a -> b) -> a -> c
.
forall b a. (b -> a -> b) -> b -> MinQueue a -> b
foldlU'
(\(Partition MinQueue b
ls MinQueue c
rs) a
a ->
case a -> Either b c
f a
a of
Left b
b -> forall a b. MinQueue a -> MinQueue b -> Partition a b
Partition (forall a. Ord a => a -> MinQueue a -> MinQueue a
insertEager b
b MinQueue b
ls) MinQueue c
rs
Right c
b -> forall a b. MinQueue a -> MinQueue b -> Partition a b
Partition MinQueue b
ls (forall a. Ord a => a -> MinQueue a -> MinQueue a
insertEager c
b MinQueue c
rs))
(forall a b. MinQueue a -> MinQueue b -> Partition a b
Partition forall a. MinQueue a
empty forall a. MinQueue a
empty)
{-# INLINABLE mapEither #-}
mapMonotonic :: (a -> b) -> MinQueue a -> MinQueue b
mapMonotonic :: forall a b. (a -> b) -> MinQueue a -> MinQueue b
mapMonotonic = forall a b. (a -> b) -> MinQueue a -> MinQueue b
mapU
{-# INLINABLE [0] foldrAsc #-}
foldrAsc :: Ord a => (a -> b -> b) -> b -> MinQueue a -> b
foldrAsc :: forall a b. Ord a => (a -> b -> b) -> b -> MinQueue a -> b
foldrAsc a -> b -> b
f b
z (MinQueue BinomHeap a
ts) = forall a c b. (a -> c -> c) -> c -> (b -> Maybe (a, b)) -> b -> c
foldrUnfold a -> b -> b
f b
z forall a. Ord a => BinomHeap a -> Maybe (a, BinomHeap a)
extractHeap BinomHeap a
ts
foldrDesc :: Ord a => (a -> b -> b) -> b -> MinQueue a -> b
foldrDesc :: forall a b. Ord a => (a -> b -> b) -> b -> MinQueue a -> b
foldrDesc = forall a b. Ord a => (b -> a -> b) -> b -> MinQueue a -> b
foldlAsc forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall a b c. (a -> b -> c) -> b -> a -> c
flip
{-# INLINE [0] foldrDesc #-}
{-# INLINE foldrUnfold #-}
foldrUnfold :: (a -> c -> c) -> c -> (b -> Maybe (a, b)) -> b -> c
foldrUnfold :: forall a c b. (a -> c -> c) -> c -> (b -> Maybe (a, b)) -> b -> c
foldrUnfold a -> c -> c
f c
z b -> Maybe (a, b)
suc b
s0 = b -> c
unf b
s0 where
unf :: b -> c
unf b
s = case b -> Maybe (a, b)
suc b
s of
Maybe (a, b)
Nothing -> c
z
Just (a
x, b
s') -> a
x a -> c -> c
`f` b -> c
unf b
s'
foldlAsc :: Ord a => (b -> a -> b) -> b -> MinQueue a -> b
foldlAsc :: forall a b. Ord a => (b -> a -> b) -> b -> MinQueue a -> b
foldlAsc b -> a -> b
f b
z (MinQueue BinomHeap a
ts) = forall c a b. (c -> a -> c) -> c -> (b -> Maybe (a, b)) -> b -> c
foldlUnfold b -> a -> b
f b
z forall a. Ord a => BinomHeap a -> Maybe (a, BinomHeap a)
extractHeap BinomHeap a
ts
{-# INLINE foldlUnfold #-}
foldlUnfold :: (c -> a -> c) -> c -> (b -> Maybe (a, b)) -> b -> c
foldlUnfold :: forall c a b. (c -> a -> c) -> c -> (b -> Maybe (a, b)) -> b -> c
foldlUnfold c -> a -> c
f c
z0 b -> Maybe (a, b)
suc b
s0 = c -> b -> c
unf c
z0 b
s0 where
unf :: c -> b -> c
unf c
z b
s = case b -> Maybe (a, b)
suc b
s of
Maybe (a, b)
Nothing -> c
z
Just (a
x, b
s') -> c -> b -> c
unf (c
z c -> a -> c
`f` a
x) b
s'
{-# INLINABLE [1] toAscList #-}
toAscList :: Ord a => MinQueue a -> [a]
toAscList :: forall a. Ord a => MinQueue a -> [a]
toAscList MinQueue a
queue = forall a b. Ord a => (a -> b -> b) -> b -> MinQueue a -> b
foldrAsc (:) [] MinQueue a
queue
{-# INLINABLE toAscListApp #-}
toAscListApp :: Ord a => MinQueue a -> [a] -> [a]
toAscListApp :: forall a. Ord a => MinQueue a -> [a] -> [a]
toAscListApp (MinQueue BinomHeap a
ts) [a]
app = forall a c b. (a -> c -> c) -> c -> (b -> Maybe (a, b)) -> b -> c
foldrUnfold (:) [a]
app forall a. Ord a => BinomHeap a -> Maybe (a, BinomHeap a)
extractHeap BinomHeap a
ts
{-# INLINABLE [1] toDescList #-}
toDescList :: Ord a => MinQueue a -> [a]
toDescList :: forall a. Ord a => MinQueue a -> [a]
toDescList MinQueue a
queue = forall a b. Ord a => (a -> b -> b) -> b -> MinQueue a -> b
foldrDesc (:) [] MinQueue a
queue
{-# INLINABLE toDescListApp #-}
toDescListApp :: Ord a => MinQueue a -> [a] -> [a]
toDescListApp :: forall a. Ord a => MinQueue a -> [a] -> [a]
toDescListApp (MinQueue BinomHeap a
ts) [a]
app = forall c a b. (c -> a -> c) -> c -> (b -> Maybe (a, b)) -> b -> c
foldlUnfold (forall a b c. (a -> b -> c) -> b -> a -> c
flip (:)) [a]
app forall a. Ord a => BinomHeap a -> Maybe (a, BinomHeap a)
extractHeap BinomHeap a
ts
{-# RULES
"toAscList" [~1] forall q. toAscList q = build (\c nil -> foldrAsc c nil q)
"toDescList" [~1] forall q. toDescList q = build (\c nil -> foldrDesc c nil q)
"ascList" [1] forall q add. foldrAsc (:) add q = toAscListApp q add
"descList" [1] forall q add. foldrDesc (:) add q = toDescListApp q add
#-}
{-# INLINE fromAscList #-}
fromAscList :: [a] -> MinQueue a
fromAscList :: forall a. [a] -> MinQueue a
fromAscList [a]
xs = forall (t :: * -> *) b a.
Foldable t =>
(b -> a -> b) -> b -> t a -> b
foldl' (forall a b c. (a -> b -> c) -> b -> a -> c
flip forall a. a -> MinQueue a -> MinQueue a
insertMaxQ') forall a. MinQueue a
empty [a]
xs
extractHeap :: Ord a => BinomHeap a -> Maybe (a, BinomHeap a)
BinomHeap a
ts = case forall a (rk :: * -> *). Ord a => BinomForest rk a -> MExtract rk a
extractBin BinomHeap a
ts of
MExtract Zero a
No -> forall a. Maybe a
Nothing
Yes (Extract a
x ~Zero a
Zero BinomHeap a
ts') -> forall a. a -> Maybe a
Just (a
x, BinomHeap a
ts')
data rk a = !a !(rk a) !(BinomForest rk a)
data rk a = No | Yes {-# UNPACK #-} !(Extract rk a)
incrExtract :: Extract (Succ rk) a -> Extract rk a
(Extract a
minKey (Succ BinomTree rk a
kChild rk a
kChildren) BinomForest (Succ rk) a
ts)
= forall (rk :: * -> *) a.
a -> rk a -> BinomForest rk a -> Extract rk a
Extract a
minKey rk a
kChildren (forall (rk :: * -> *) a.
BinomTree rk a -> BinomForest (Succ rk) a -> BinomForest rk a
Cons BinomTree rk a
kChild BinomForest (Succ rk) a
ts)
incrExtract' :: Ord a => BinomTree rk a -> Extract (Succ rk) a -> Extract rk a
BinomTree rk a
t (Extract a
minKey (Succ BinomTree rk a
kChild rk a
kChildren) BinomForest (Succ rk) a
ts)
= forall (rk :: * -> *) a.
a -> rk a -> BinomForest rk a -> Extract rk a
Extract a
minKey rk a
kChildren (forall (rk :: * -> *) a.
BinomForest (Succ rk) a -> BinomForest rk a
Skip forall a b. (a -> b) -> a -> b
$! forall a (rk :: * -> *).
Ord a =>
BinomTree rk a -> BinomForest rk a -> BinomForest rk a
incr' (BinomTree rk a
t forall a (rk :: * -> *).
Ord a =>
BinomTree rk a -> BinomTree rk a -> BinomTree (Succ rk) a
`joinBin` BinomTree rk a
kChild) BinomForest (Succ rk) a
ts)
extractBin :: Ord a => BinomForest rk a -> MExtract rk a
= forall a (rk :: * -> *). Ord a => BinomForest rk a -> MExtract rk a
start
where
start :: Ord a => BinomForest rk a -> MExtract rk a
start :: forall a (rk :: * -> *). Ord a => BinomForest rk a -> MExtract rk a
start BinomForest rk a
Nil = forall (rk :: * -> *) a. MExtract rk a
No
start (Skip BinomForest (Succ rk) a
f) = case forall a (rk :: * -> *). Ord a => BinomForest rk a -> MExtract rk a
start BinomForest (Succ rk) a
f of
MExtract (Succ rk) a
No -> forall (rk :: * -> *) a. MExtract rk a
No
Yes Extract (Succ rk) a
ex -> forall (rk :: * -> *) a. Extract rk a -> MExtract rk a
Yes (forall (rk :: * -> *) a. Extract (Succ rk) a -> Extract rk a
incrExtract Extract (Succ rk) a
ex)
start (Cons t :: BinomTree rk a
t@(BinomTree a
x rk a
ts) BinomForest (Succ rk) a
f) = forall (rk :: * -> *) a. Extract rk a -> MExtract rk a
Yes forall a b. (a -> b) -> a -> b
$ case forall a (rk :: * -> *).
Ord a =>
a -> BinomForest rk a -> MExtract rk a
go a
x BinomForest (Succ rk) a
f of
MExtract (Succ rk) a
No -> forall (rk :: * -> *) a.
a -> rk a -> BinomForest rk a -> Extract rk a
Extract a
x rk a
ts (forall (rk :: * -> *) a.
BinomForest (Succ rk) a -> BinomForest rk a
skip BinomForest (Succ rk) a
f)
Yes Extract (Succ rk) a
ex -> forall a (rk :: * -> *).
Ord a =>
BinomTree rk a -> Extract (Succ rk) a -> Extract rk a
incrExtract' BinomTree rk a
t Extract (Succ rk) a
ex
go :: Ord a => a -> BinomForest rk a -> MExtract rk a
go :: forall a (rk :: * -> *).
Ord a =>
a -> BinomForest rk a -> MExtract rk a
go a
_min_above BinomForest rk a
Nil = a
_min_above seq :: forall a b. a -> b -> b
`seq` forall (rk :: * -> *) a. MExtract rk a
No
go a
min_above (Skip BinomForest (Succ rk) a
f) = case forall a (rk :: * -> *).
Ord a =>
a -> BinomForest rk a -> MExtract rk a
go a
min_above BinomForest (Succ rk) a
f of
MExtract (Succ rk) a
No -> forall (rk :: * -> *) a. MExtract rk a
No
Yes Extract (Succ rk) a
ex -> forall (rk :: * -> *) a. Extract rk a -> MExtract rk a
Yes (forall (rk :: * -> *) a. Extract (Succ rk) a -> Extract rk a
incrExtract Extract (Succ rk) a
ex)
go a
min_above (Cons t :: BinomTree rk a
t@(BinomTree a
x rk a
ts) BinomForest (Succ rk) a
f)
| a
min_above forall a. Ord a => a -> a -> Bool
<= a
x = case forall a (rk :: * -> *).
Ord a =>
a -> BinomForest rk a -> MExtract rk a
go a
min_above BinomForest (Succ rk) a
f of
MExtract (Succ rk) a
No -> forall (rk :: * -> *) a. MExtract rk a
No
Yes Extract (Succ rk) a
ex -> forall (rk :: * -> *) a. Extract rk a -> MExtract rk a
Yes (forall a (rk :: * -> *).
Ord a =>
BinomTree rk a -> Extract (Succ rk) a -> Extract rk a
incrExtract' BinomTree rk a
t Extract (Succ rk) a
ex)
| Bool
otherwise = case forall a (rk :: * -> *).
Ord a =>
a -> BinomForest rk a -> MExtract rk a
go a
x BinomForest (Succ rk) a
f of
MExtract (Succ rk) a
No -> forall (rk :: * -> *) a. Extract rk a -> MExtract rk a
Yes (forall (rk :: * -> *) a.
a -> rk a -> BinomForest rk a -> Extract rk a
Extract a
x rk a
ts (forall (rk :: * -> *) a.
BinomForest (Succ rk) a -> BinomForest rk a
skip BinomForest (Succ rk) a
f))
Yes Extract (Succ rk) a
ex -> forall (rk :: * -> *) a. Extract rk a -> MExtract rk a
Yes (forall a (rk :: * -> *).
Ord a =>
BinomTree rk a -> Extract (Succ rk) a -> Extract rk a
incrExtract' BinomTree rk a
t Extract (Succ rk) a
ex)
skip :: BinomForest (Succ rk) a -> BinomForest rk a
skip :: forall (rk :: * -> *) a.
BinomForest (Succ rk) a -> BinomForest rk a
skip BinomForest (Succ rk) a
Nil = forall (rk :: * -> *) a. BinomForest rk a
Nil
skip BinomForest (Succ rk) a
f = forall (rk :: * -> *) a.
BinomForest (Succ rk) a -> BinomForest rk a
Skip BinomForest (Succ rk) a
f
{-# INLINE skip #-}
data Partition a b = Partition !(MinQueue a) !(MinQueue b)
fromPartition :: Partition a b -> (MinQueue a, MinQueue b)
fromPartition :: forall a b. Partition a b -> (MinQueue a, MinQueue b)
fromPartition (Partition MinQueue a
p MinQueue b
q) = (MinQueue a
p, MinQueue b
q)
{-# INLINE tip #-}
tip :: a -> BinomTree Zero a
tip :: forall a. a -> BinomTree Zero a
tip a
x = forall (rk :: * -> *) a. a -> rk a -> BinomTree rk a
BinomTree a
x forall a. Zero a
Zero
insertMinQ :: a -> MinQueue a -> MinQueue a
insertMinQ :: forall a. a -> MinQueue a -> MinQueue a
insertMinQ a
x (MinQueue BinomHeap a
f) = forall a. BinomHeap a -> MinQueue a
MinQueue (forall (rk :: * -> *) a.
BinomTree rk a -> BinomForest rk a -> BinomForest rk a
insertMin (forall a. a -> BinomTree Zero a
tip a
x) BinomHeap a
f)
insertMin :: BinomTree rk a -> BinomForest rk a -> BinomForest rk a
insertMin :: forall (rk :: * -> *) a.
BinomTree rk a -> BinomForest rk a -> BinomForest rk a
insertMin BinomTree rk a
t BinomForest rk a
Nil = forall (rk :: * -> *) a.
BinomTree rk a -> BinomForest (Succ rk) a -> BinomForest rk a
Cons BinomTree rk a
t forall (rk :: * -> *) a. BinomForest rk a
Nil
insertMin BinomTree rk a
t (Skip BinomForest (Succ rk) a
f) = forall (rk :: * -> *) a.
BinomTree rk a -> BinomForest (Succ rk) a -> BinomForest rk a
Cons BinomTree rk a
t BinomForest (Succ rk) a
f
insertMin (BinomTree a
x rk a
ts) (Cons BinomTree rk a
t' BinomForest (Succ rk) a
f) = BinomForest (Succ rk) a
f seq :: forall a b. a -> b -> b
`seq` forall (rk :: * -> *) a.
BinomForest (Succ rk) a -> BinomForest rk a
Skip (forall (rk :: * -> *) a.
BinomTree rk a -> BinomForest rk a -> BinomForest rk a
insertMin (forall (rk :: * -> *) a. a -> rk a -> BinomTree rk a
BinomTree a
x (forall (rk :: * -> *) a. BinomTree rk a -> rk a -> Succ rk a
Succ BinomTree rk a
t' rk a
ts)) BinomForest (Succ rk) a
f)
insertMinQ' :: a -> MinQueue a -> MinQueue a
insertMinQ' :: forall a. a -> MinQueue a -> MinQueue a
insertMinQ' a
x (MinQueue BinomHeap a
f) = forall a. BinomHeap a -> MinQueue a
MinQueue (forall (rk :: * -> *) a.
BinomTree rk a -> BinomForest rk a -> BinomForest rk a
insertMin' (forall a. a -> BinomTree Zero a
tip a
x) BinomHeap a
f)
insertMin' :: BinomTree rk a -> BinomForest rk a -> BinomForest rk a
insertMin' :: forall (rk :: * -> *) a.
BinomTree rk a -> BinomForest rk a -> BinomForest rk a
insertMin' BinomTree rk a
t BinomForest rk a
Nil = forall (rk :: * -> *) a.
BinomTree rk a -> BinomForest (Succ rk) a -> BinomForest rk a
Cons BinomTree rk a
t forall (rk :: * -> *) a. BinomForest rk a
Nil
insertMin' BinomTree rk a
t (Skip BinomForest (Succ rk) a
f) = forall (rk :: * -> *) a.
BinomTree rk a -> BinomForest (Succ rk) a -> BinomForest rk a
Cons BinomTree rk a
t BinomForest (Succ rk) a
f
insertMin' (BinomTree a
x rk a
ts) (Cons BinomTree rk a
t' BinomForest (Succ rk) a
f) = forall (rk :: * -> *) a.
BinomForest (Succ rk) a -> BinomForest rk a
Skip forall a b. (a -> b) -> a -> b
$! forall (rk :: * -> *) a.
BinomTree rk a -> BinomForest rk a -> BinomForest rk a
insertMin' (forall (rk :: * -> *) a. a -> rk a -> BinomTree rk a
BinomTree a
x (forall (rk :: * -> *) a. BinomTree rk a -> rk a -> Succ rk a
Succ BinomTree rk a
t' rk a
ts)) BinomForest (Succ rk) a
f
insertMaxQ' :: a -> MinQueue a -> MinQueue a
insertMaxQ' :: forall a. a -> MinQueue a -> MinQueue a
insertMaxQ' a
x (MinQueue BinomHeap a
f) = forall a. BinomHeap a -> MinQueue a
MinQueue (forall (rk :: * -> *) a.
BinomTree rk a -> BinomForest rk a -> BinomForest rk a
insertMax' (forall a. a -> BinomTree Zero a
tip a
x) BinomHeap a
f)
insertMax' :: BinomTree rk a -> BinomForest rk a -> BinomForest rk a
insertMax' :: forall (rk :: * -> *) a.
BinomTree rk a -> BinomForest rk a -> BinomForest rk a
insertMax' BinomTree rk a
t BinomForest rk a
Nil = forall (rk :: * -> *) a.
BinomTree rk a -> BinomForest (Succ rk) a -> BinomForest rk a
Cons BinomTree rk a
t forall (rk :: * -> *) a. BinomForest rk a
Nil
insertMax' BinomTree rk a
t (Skip BinomForest (Succ rk) a
f) = forall (rk :: * -> *) a.
BinomTree rk a -> BinomForest (Succ rk) a -> BinomForest rk a
Cons BinomTree rk a
t BinomForest (Succ rk) a
f
insertMax' BinomTree rk a
t (Cons (BinomTree a
x rk a
ts) BinomForest (Succ rk) a
f) = forall (rk :: * -> *) a.
BinomForest (Succ rk) a -> BinomForest rk a
Skip forall a b. (a -> b) -> a -> b
$! forall (rk :: * -> *) a.
BinomTree rk a -> BinomForest rk a -> BinomForest rk a
insertMax' (forall (rk :: * -> *) a. a -> rk a -> BinomTree rk a
BinomTree a
x (forall (rk :: * -> *) a. BinomTree rk a -> rk a -> Succ rk a
Succ BinomTree rk a
t rk a
ts)) BinomForest (Succ rk) a
f
{-# INLINABLE fromList #-}
fromList :: Ord a => [a] -> MinQueue a
fromList :: forall a. Ord a => [a] -> MinQueue a
fromList [a]
xs = forall (t :: * -> *) b a.
Foldable t =>
(b -> a -> b) -> b -> t a -> b
foldl' (forall a b c. (a -> b -> c) -> b -> a -> c
flip forall a. Ord a => a -> MinQueue a -> MinQueue a
insertEager) forall a. MinQueue a
empty [a]
xs
merge :: Ord a => BinomForest rk a -> BinomForest rk a -> BinomForest rk a
merge :: forall a (rk :: * -> *).
Ord a =>
BinomForest rk a -> BinomForest rk a -> BinomForest rk a
merge BinomForest rk a
f1 BinomForest rk a
f2 = case (BinomForest rk a
f1, BinomForest rk a
f2) of
(Skip BinomForest (Succ rk) a
f1', Skip BinomForest (Succ rk) a
f2') -> forall (rk :: * -> *) a.
BinomForest (Succ rk) a -> BinomForest rk a
Skip forall a b. (a -> b) -> a -> b
$! forall a (rk :: * -> *).
Ord a =>
BinomForest rk a -> BinomForest rk a -> BinomForest rk a
merge BinomForest (Succ rk) a
f1' BinomForest (Succ rk) a
f2'
(Skip BinomForest (Succ rk) a
f1', Cons BinomTree rk a
t2 BinomForest (Succ rk) a
f2') -> forall (rk :: * -> *) a.
BinomTree rk a -> BinomForest (Succ rk) a -> BinomForest rk a
Cons BinomTree rk a
t2 forall a b. (a -> b) -> a -> b
$! forall a (rk :: * -> *).
Ord a =>
BinomForest rk a -> BinomForest rk a -> BinomForest rk a
merge BinomForest (Succ rk) a
f1' BinomForest (Succ rk) a
f2'
(Cons BinomTree rk a
t1 BinomForest (Succ rk) a
f1', Skip BinomForest (Succ rk) a
f2') -> forall (rk :: * -> *) a.
BinomTree rk a -> BinomForest (Succ rk) a -> BinomForest rk a
Cons BinomTree rk a
t1 forall a b. (a -> b) -> a -> b
$! forall a (rk :: * -> *).
Ord a =>
BinomForest rk a -> BinomForest rk a -> BinomForest rk a
merge BinomForest (Succ rk) a
f1' BinomForest (Succ rk) a
f2'
(Cons BinomTree rk a
t1 BinomForest (Succ rk) a
f1', Cons BinomTree rk a
t2 BinomForest (Succ rk) a
f2')
-> forall (rk :: * -> *) a.
BinomForest (Succ rk) a -> BinomForest rk a
Skip forall a b. (a -> b) -> a -> b
$! forall a (rk :: * -> *).
Ord a =>
BinomTree rk a
-> BinomForest rk a -> BinomForest rk a -> BinomForest rk a
carry (BinomTree rk a
t1 forall a (rk :: * -> *).
Ord a =>
BinomTree rk a -> BinomTree rk a -> BinomTree (Succ rk) a
`joinBin` BinomTree rk a
t2) BinomForest (Succ rk) a
f1' BinomForest (Succ rk) a
f2'
(BinomForest rk a
Nil, BinomForest rk a
_) -> BinomForest rk a
f2
(BinomForest rk a
_, BinomForest rk a
Nil) -> BinomForest rk a
f1
unionPlusOne :: Ord a => a -> MinQueue a -> MinQueue a -> MinQueue a
unionPlusOne :: forall a. Ord a => a -> MinQueue a -> MinQueue a -> MinQueue a
unionPlusOne a
a (MinQueue BinomHeap a
xs) (MinQueue BinomHeap a
ys) = forall a. BinomHeap a -> MinQueue a
MinQueue (forall a (rk :: * -> *).
Ord a =>
BinomTree rk a
-> BinomForest rk a -> BinomForest rk a -> BinomForest rk a
carry (forall a. a -> BinomTree Zero a
tip a
a) BinomHeap a
xs BinomHeap a
ys)
carry :: Ord a => BinomTree rk a -> BinomForest rk a -> BinomForest rk a -> BinomForest rk a
carry :: forall a (rk :: * -> *).
Ord a =>
BinomTree rk a
-> BinomForest rk a -> BinomForest rk a -> BinomForest rk a
carry BinomTree rk a
t0 BinomForest rk a
f1 BinomForest rk a
f2 = BinomTree rk a
t0 seq :: forall a b. a -> b -> b
`seq` case (BinomForest rk a
f1, BinomForest rk a
f2) of
(Skip BinomForest (Succ rk) a
f1', Skip BinomForest (Succ rk) a
f2') -> forall (rk :: * -> *) a.
BinomTree rk a -> BinomForest (Succ rk) a -> BinomForest rk a
Cons BinomTree rk a
t0 forall a b. (a -> b) -> a -> b
$! forall a (rk :: * -> *).
Ord a =>
BinomForest rk a -> BinomForest rk a -> BinomForest rk a
merge BinomForest (Succ rk) a
f1' BinomForest (Succ rk) a
f2'
(Skip BinomForest (Succ rk) a
f1', Cons BinomTree rk a
t2 BinomForest (Succ rk) a
f2') -> forall (rk :: * -> *) a.
BinomForest (Succ rk) a -> BinomForest rk a
Skip forall a b. (a -> b) -> a -> b
$! forall {a} {rk :: * -> *}.
Ord a =>
BinomTree rk a
-> BinomTree rk a
-> BinomForest (Succ rk) a
-> BinomForest (Succ rk) a
-> BinomForest (Succ rk) a
mergeCarry BinomTree rk a
t0 BinomTree rk a
t2 BinomForest (Succ rk) a
f1' BinomForest (Succ rk) a
f2'
(Cons BinomTree rk a
t1 BinomForest (Succ rk) a
f1', Skip BinomForest (Succ rk) a
f2') -> forall (rk :: * -> *) a.
BinomForest (Succ rk) a -> BinomForest rk a
Skip forall a b. (a -> b) -> a -> b
$! forall {a} {rk :: * -> *}.
Ord a =>
BinomTree rk a
-> BinomTree rk a
-> BinomForest (Succ rk) a
-> BinomForest (Succ rk) a
-> BinomForest (Succ rk) a
mergeCarry BinomTree rk a
t0 BinomTree rk a
t1 BinomForest (Succ rk) a
f1' BinomForest (Succ rk) a
f2'
(Cons BinomTree rk a
t1 BinomForest (Succ rk) a
f1', Cons BinomTree rk a
t2 BinomForest (Succ rk) a
f2')
-> forall (rk :: * -> *) a.
BinomTree rk a -> BinomForest (Succ rk) a -> BinomForest rk a
Cons BinomTree rk a
t0 forall a b. (a -> b) -> a -> b
$! forall {a} {rk :: * -> *}.
Ord a =>
BinomTree rk a
-> BinomTree rk a
-> BinomForest (Succ rk) a
-> BinomForest (Succ rk) a
-> BinomForest (Succ rk) a
mergeCarry BinomTree rk a
t1 BinomTree rk a
t2 BinomForest (Succ rk) a
f1' BinomForest (Succ rk) a
f2'
(BinomForest rk a
Nil, BinomForest rk a
_f2) -> forall a (rk :: * -> *).
Ord a =>
BinomTree rk a -> BinomForest rk a -> BinomForest rk a
incr BinomTree rk a
t0 BinomForest rk a
f2
(BinomForest rk a
_f1, BinomForest rk a
Nil) -> forall a (rk :: * -> *).
Ord a =>
BinomTree rk a -> BinomForest rk a -> BinomForest rk a
incr BinomTree rk a
t0 BinomForest rk a
f1
where
mergeCarry :: BinomTree rk a
-> BinomTree rk a
-> BinomForest (Succ rk) a
-> BinomForest (Succ rk) a
-> BinomForest (Succ rk) a
mergeCarry BinomTree rk a
tA BinomTree rk a
tB = forall a (rk :: * -> *).
Ord a =>
BinomTree rk a
-> BinomForest rk a -> BinomForest rk a -> BinomForest rk a
carry (BinomTree rk a
tA forall a (rk :: * -> *).
Ord a =>
BinomTree rk a -> BinomTree rk a -> BinomTree (Succ rk) a
`joinBin` BinomTree rk a
tB)
incr :: Ord a => BinomTree rk a -> BinomForest rk a -> BinomForest rk a
incr :: forall a (rk :: * -> *).
Ord a =>
BinomTree rk a -> BinomForest rk a -> BinomForest rk a
incr BinomTree rk a
t BinomForest rk a
f0 = BinomTree rk a
t seq :: forall a b. a -> b -> b
`seq` case BinomForest rk a
f0 of
BinomForest rk a
Nil -> forall (rk :: * -> *) a.
BinomTree rk a -> BinomForest (Succ rk) a -> BinomForest rk a
Cons BinomTree rk a
t forall (rk :: * -> *) a. BinomForest rk a
Nil
Skip BinomForest (Succ rk) a
f -> forall (rk :: * -> *) a.
BinomTree rk a -> BinomForest (Succ rk) a -> BinomForest rk a
Cons BinomTree rk a
t BinomForest (Succ rk) a
f
Cons BinomTree rk a
t' BinomForest (Succ rk) a
f' -> BinomForest (Succ rk) a
f' seq :: forall a b. a -> b -> b
`seq` forall (rk :: * -> *) a.
BinomForest (Succ rk) a -> BinomForest rk a
Skip (forall a (rk :: * -> *).
Ord a =>
BinomTree rk a -> BinomForest rk a -> BinomForest rk a
incr (BinomTree rk a
t forall a (rk :: * -> *).
Ord a =>
BinomTree rk a -> BinomTree rk a -> BinomTree (Succ rk) a
`joinBin` BinomTree rk a
t') BinomForest (Succ rk) a
f')
incr' :: Ord a => BinomTree rk a -> BinomForest rk a -> BinomForest rk a
incr' :: forall a (rk :: * -> *).
Ord a =>
BinomTree rk a -> BinomForest rk a -> BinomForest rk a
incr' BinomTree rk a
t BinomForest rk a
f0 = BinomTree rk a
t seq :: forall a b. a -> b -> b
`seq` case BinomForest rk a
f0 of
BinomForest rk a
Nil -> forall (rk :: * -> *) a.
BinomTree rk a -> BinomForest (Succ rk) a -> BinomForest rk a
Cons BinomTree rk a
t forall (rk :: * -> *) a. BinomForest rk a
Nil
Skip BinomForest (Succ rk) a
f -> forall (rk :: * -> *) a.
BinomTree rk a -> BinomForest (Succ rk) a -> BinomForest rk a
Cons BinomTree rk a
t BinomForest (Succ rk) a
f
Cons BinomTree rk a
t' BinomForest (Succ rk) a
f' -> forall (rk :: * -> *) a.
BinomForest (Succ rk) a -> BinomForest rk a
Skip forall a b. (a -> b) -> a -> b
$! forall a (rk :: * -> *).
Ord a =>
BinomTree rk a -> BinomForest rk a -> BinomForest rk a
incr' (BinomTree rk a
t forall a (rk :: * -> *).
Ord a =>
BinomTree rk a -> BinomTree rk a -> BinomTree (Succ rk) a
`joinBin` BinomTree rk a
t') BinomForest (Succ rk) a
f'
joinBin :: Ord a => BinomTree rk a -> BinomTree rk a -> BinomTree (Succ rk) a
joinBin :: forall a (rk :: * -> *).
Ord a =>
BinomTree rk a -> BinomTree rk a -> BinomTree (Succ rk) a
joinBin t1 :: BinomTree rk a
t1@(BinomTree a
x1 rk a
ts1) t2 :: BinomTree rk a
t2@(BinomTree a
x2 rk a
ts2)
| a
x1 forall a. Ord a => a -> a -> Bool
<= a
x2 = forall (rk :: * -> *) a. a -> rk a -> BinomTree rk a
BinomTree a
x1 (forall (rk :: * -> *) a. BinomTree rk a -> rk a -> Succ rk a
Succ BinomTree rk a
t2 rk a
ts1)
| Bool
otherwise = forall (rk :: * -> *) a. a -> rk a -> BinomTree rk a
BinomTree a
x2 (forall (rk :: * -> *) a. BinomTree rk a -> rk a -> Succ rk a
Succ BinomTree rk a
t1 rk a
ts2)
instance Functor Zero where
fmap :: forall a b. (a -> b) -> Zero a -> Zero b
fmap a -> b
_ Zero a
_ = forall a. Zero a
Zero
instance Functor rk => Functor (Succ rk) where
fmap :: forall a b. (a -> b) -> Succ rk a -> Succ rk b
fmap a -> b
f (Succ BinomTree rk a
t rk a
ts) = forall (rk :: * -> *) a. BinomTree rk a -> rk a -> Succ rk a
Succ (forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
fmap a -> b
f BinomTree rk a
t) (forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
fmap a -> b
f rk a
ts)
instance Functor rk => Functor (BinomTree rk) where
fmap :: forall a b. (a -> b) -> BinomTree rk a -> BinomTree rk b
fmap a -> b
f (BinomTree a
x rk a
ts) = forall (rk :: * -> *) a. a -> rk a -> BinomTree rk a
BinomTree (a -> b
f a
x) (forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
fmap a -> b
f rk a
ts)
instance Functor rk => Functor (BinomForest rk) where
fmap :: forall a b. (a -> b) -> BinomForest rk a -> BinomForest rk b
fmap a -> b
_ BinomForest rk a
Nil = forall (rk :: * -> *) a. BinomForest rk a
Nil
fmap a -> b
f (Skip BinomForest (Succ rk) a
ts) = forall (rk :: * -> *) a.
BinomForest (Succ rk) a -> BinomForest rk a
Skip forall a b. (a -> b) -> a -> b
$! forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
fmap a -> b
f BinomForest (Succ rk) a
ts
fmap a -> b
f (Cons BinomTree rk a
t BinomForest (Succ rk) a
ts) = forall (rk :: * -> *) a.
BinomTree rk a -> BinomForest (Succ rk) a -> BinomForest rk a
Cons (forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
fmap a -> b
f BinomTree rk a
t) forall a b. (a -> b) -> a -> b
$! forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
fmap a -> b
f BinomForest (Succ rk) a
ts
instance Foldr Zero where
foldr_ :: forall a b. (a -> b -> b) -> b -> Zero a -> b
foldr_ a -> b -> b
_ b
z ~Zero a
Zero = b
z
instance Foldl Zero where
foldl_ :: forall b a. (b -> a -> b) -> b -> Zero a -> b
foldl_ b -> a -> b
_ b
z ~Zero a
Zero = b
z
instance Foldl' Zero where
foldl'_ :: forall b a. (b -> a -> b) -> b -> Zero a -> b
foldl'_ b -> a -> b
_ b
z ~Zero a
Zero = b
z
instance FoldMap Zero where
foldMap_ :: forall m a. Monoid m => (a -> m) -> Zero a -> m
foldMap_ a -> m
_ ~Zero a
Zero = forall a. Monoid a => a
mempty
instance Foldr rk => Foldr (Succ rk) where
foldr_ :: forall a b. (a -> b -> b) -> b -> Succ rk a -> b
foldr_ a -> b -> b
f b
z (Succ BinomTree rk a
t rk a
ts) = forall (t :: * -> *) a b. Foldr t => (a -> b -> b) -> b -> t a -> b
foldr_ a -> b -> b
f (forall (t :: * -> *) a b. Foldr t => (a -> b -> b) -> b -> t a -> b
foldr_ a -> b -> b
f b
z rk a
ts) BinomTree rk a
t
instance Foldl rk => Foldl (Succ rk) where
foldl_ :: forall b a. (b -> a -> b) -> b -> Succ rk a -> b
foldl_ b -> a -> b
f b
z (Succ BinomTree rk a
t rk a
ts) = forall (t :: * -> *) b a. Foldl t => (b -> a -> b) -> b -> t a -> b
foldl_ b -> a -> b
f (forall (t :: * -> *) b a. Foldl t => (b -> a -> b) -> b -> t a -> b
foldl_ b -> a -> b
f b
z BinomTree rk a
t) rk a
ts
instance Foldl' rk => Foldl' (Succ rk) where
foldl'_ :: forall b a. (b -> a -> b) -> b -> Succ rk a -> b
foldl'_ b -> a -> b
f !b
z (Succ BinomTree rk a
t rk a
ts) = forall (t :: * -> *) b a.
Foldl' t =>
(b -> a -> b) -> b -> t a -> b
foldl'_ b -> a -> b
f (forall (t :: * -> *) b a.
Foldl' t =>
(b -> a -> b) -> b -> t a -> b
foldl'_ b -> a -> b
f b
z BinomTree rk a
t) rk a
ts
instance FoldMap rk => FoldMap (Succ rk) where
foldMap_ :: forall m a. Monoid m => (a -> m) -> Succ rk a -> m
foldMap_ a -> m
f (Succ BinomTree rk a
t rk a
ts) = forall (t :: * -> *) m a.
(FoldMap t, Monoid m) =>
(a -> m) -> t a -> m
foldMap_ a -> m
f BinomTree rk a
t forall a. Monoid a => a -> a -> a
`mappend` forall (t :: * -> *) m a.
(FoldMap t, Monoid m) =>
(a -> m) -> t a -> m
foldMap_ a -> m
f rk a
ts
instance Foldr rk => Foldr (BinomTree rk) where
foldr_ :: forall a b. (a -> b -> b) -> b -> BinomTree rk a -> b
foldr_ a -> b -> b
f b
z (BinomTree a
x rk a
ts) = a
x a -> b -> b
`f` forall (t :: * -> *) a b. Foldr t => (a -> b -> b) -> b -> t a -> b
foldr_ a -> b -> b
f b
z rk a
ts
instance Foldl rk => Foldl (BinomTree rk) where
foldl_ :: forall b a. (b -> a -> b) -> b -> BinomTree rk a -> b
foldl_ b -> a -> b
f b
z (BinomTree a
x rk a
ts) = forall (t :: * -> *) b a. Foldl t => (b -> a -> b) -> b -> t a -> b
foldl_ b -> a -> b
f (b
z b -> a -> b
`f` a
x) rk a
ts
instance Foldl' rk => Foldl' (BinomTree rk) where
foldl'_ :: forall b a. (b -> a -> b) -> b -> BinomTree rk a -> b
foldl'_ b -> a -> b
f !b
z (BinomTree a
x rk a
ts) = forall (t :: * -> *) b a.
Foldl' t =>
(b -> a -> b) -> b -> t a -> b
foldl'_ b -> a -> b
f (b
z b -> a -> b
`f` a
x) rk a
ts
instance FoldMap rk => FoldMap (BinomTree rk) where
foldMap_ :: forall m a. Monoid m => (a -> m) -> BinomTree rk a -> m
foldMap_ a -> m
f (BinomTree a
x rk a
ts) = a -> m
f a
x forall a. Monoid a => a -> a -> a
`mappend` forall (t :: * -> *) m a.
(FoldMap t, Monoid m) =>
(a -> m) -> t a -> m
foldMap_ a -> m
f rk a
ts
instance Foldr rk => Foldr (BinomForest rk) where
foldr_ :: forall a b. (a -> b -> b) -> b -> BinomForest rk a -> b
foldr_ a -> b -> b
_ b
z BinomForest rk a
Nil = b
z
foldr_ a -> b -> b
f b
z (Skip BinomForest (Succ rk) a
tss) = forall (t :: * -> *) a b. Foldr t => (a -> b -> b) -> b -> t a -> b
foldr_ a -> b -> b
f b
z BinomForest (Succ rk) a
tss
foldr_ a -> b -> b
f b
z (Cons BinomTree rk a
t BinomForest (Succ rk) a
tss) = forall (t :: * -> *) a b. Foldr t => (a -> b -> b) -> b -> t a -> b
foldr_ a -> b -> b
f (forall (t :: * -> *) a b. Foldr t => (a -> b -> b) -> b -> t a -> b
foldr_ a -> b -> b
f b
z BinomForest (Succ rk) a
tss) BinomTree rk a
t
instance Foldl rk => Foldl (BinomForest rk) where
foldl_ :: forall b a. (b -> a -> b) -> b -> BinomForest rk a -> b
foldl_ b -> a -> b
_ b
z BinomForest rk a
Nil = b
z
foldl_ b -> a -> b
f b
z (Skip BinomForest (Succ rk) a
tss) = forall (t :: * -> *) b a. Foldl t => (b -> a -> b) -> b -> t a -> b
foldl_ b -> a -> b
f b
z BinomForest (Succ rk) a
tss
foldl_ b -> a -> b
f b
z (Cons BinomTree rk a
t BinomForest (Succ rk) a
tss) = forall (t :: * -> *) b a. Foldl t => (b -> a -> b) -> b -> t a -> b
foldl_ b -> a -> b
f (forall (t :: * -> *) b a. Foldl t => (b -> a -> b) -> b -> t a -> b
foldl_ b -> a -> b
f b
z BinomTree rk a
t) BinomForest (Succ rk) a
tss
instance Foldl' rk => Foldl' (BinomForest rk) where
foldl'_ :: forall b a. (b -> a -> b) -> b -> BinomForest rk a -> b
foldl'_ b -> a -> b
_ !b
z BinomForest rk a
Nil = b
z
foldl'_ b -> a -> b
f !b
z (Skip BinomForest (Succ rk) a
tss) = forall (t :: * -> *) b a.
Foldl' t =>
(b -> a -> b) -> b -> t a -> b
foldl'_ b -> a -> b
f b
z BinomForest (Succ rk) a
tss
foldl'_ b -> a -> b
f !b
z (Cons BinomTree rk a
t BinomForest (Succ rk) a
tss) = forall (t :: * -> *) b a.
Foldl' t =>
(b -> a -> b) -> b -> t a -> b
foldl'_ b -> a -> b
f (forall (t :: * -> *) b a.
Foldl' t =>
(b -> a -> b) -> b -> t a -> b
foldl'_ b -> a -> b
f b
z BinomTree rk a
t) BinomForest (Succ rk) a
tss
instance FoldMap rk => FoldMap (BinomForest rk) where
foldMap_ :: forall m a. Monoid m => (a -> m) -> BinomForest rk a -> m
foldMap_ a -> m
_ BinomForest rk a
Nil = forall a. Monoid a => a
mempty
foldMap_ a -> m
f (Skip BinomForest (Succ rk) a
tss) = forall (t :: * -> *) m a.
(FoldMap t, Monoid m) =>
(a -> m) -> t a -> m
foldMap_ a -> m
f BinomForest (Succ rk) a
tss
foldMap_ a -> m
f (Cons BinomTree rk a
t BinomForest (Succ rk) a
tss) = forall (t :: * -> *) m a.
(FoldMap t, Monoid m) =>
(a -> m) -> t a -> m
foldMap_ a -> m
f BinomTree rk a
t forall a. Monoid a => a -> a -> a
`mappend` forall (t :: * -> *) m a.
(FoldMap t, Monoid m) =>
(a -> m) -> t a -> m
foldMap_ a -> m
f BinomForest (Succ rk) a
tss
mapU :: (a -> b) -> MinQueue a -> MinQueue b
mapU :: forall a b. (a -> b) -> MinQueue a -> MinQueue b
mapU a -> b
f (MinQueue BinomHeap a
ts) = forall a. BinomHeap a -> MinQueue a
MinQueue (a -> b
f forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> BinomHeap a
ts)
{-# NOINLINE [0] foldrU #-}
foldrU :: (a -> b -> b) -> b -> MinQueue a -> b
foldrU :: forall a b. (a -> b -> b) -> b -> MinQueue a -> b
foldrU a -> b -> b
f b
z (MinQueue BinomHeap a
ts) = forall (t :: * -> *) a b. Foldr t => (a -> b -> b) -> b -> t a -> b
foldr_ a -> b -> b
f b
z BinomHeap a
ts
foldlU :: (b -> a -> b) -> b -> MinQueue a -> b
foldlU :: forall b a. (b -> a -> b) -> b -> MinQueue a -> b
foldlU b -> a -> b
f b
z (MinQueue BinomHeap a
ts) = forall (t :: * -> *) b a. Foldl t => (b -> a -> b) -> b -> t a -> b
foldl_ b -> a -> b
f b
z BinomHeap a
ts
foldlU' :: (b -> a -> b) -> b -> MinQueue a -> b
foldlU' :: forall b a. (b -> a -> b) -> b -> MinQueue a -> b
foldlU' b -> a -> b
f b
z (MinQueue BinomHeap a
ts) = forall (t :: * -> *) b a.
Foldl' t =>
(b -> a -> b) -> b -> t a -> b
foldl'_ b -> a -> b
f b
z BinomHeap a
ts
foldMapU :: Monoid m => (a -> m) -> MinQueue a -> m
foldMapU :: forall m a. Monoid m => (a -> m) -> MinQueue a -> m
foldMapU a -> m
f (MinQueue BinomHeap a
ts) = forall (t :: * -> *) m a.
(FoldMap t, Monoid m) =>
(a -> m) -> t a -> m
foldMap_ a -> m
f BinomHeap a
ts
{-# NOINLINE toListU #-}
toListU :: MinQueue a -> [a]
toListU :: forall a. MinQueue a -> [a]
toListU MinQueue a
q = forall a b. (a -> b -> b) -> b -> MinQueue a -> b
foldrU (:) [] MinQueue a
q
{-# NOINLINE toListUApp #-}
toListUApp :: MinQueue a -> [a] -> [a]
toListUApp :: forall a. MinQueue a -> [a] -> [a]
toListUApp (MinQueue BinomHeap a
ts) [a]
app = forall (t :: * -> *) a b. Foldr t => (a -> b -> b) -> b -> t a -> b
foldr_ (:) [a]
app BinomHeap a
ts
{-# RULES
"toListU/build" [~1] forall q. toListU q = build (\c n -> foldrU c n q)
"toListU" [1] forall q app. foldrU (:) app q = toListUApp q app
#-}
seqSpine :: MinQueue a -> b -> b
seqSpine :: forall a b. MinQueue a -> b -> b
seqSpine (MinQueue BinomHeap a
ts) b
z = forall (rk :: * -> *) a b. BinomForest rk a -> b -> b
seqSpineF BinomHeap a
ts b
z
seqSpineF :: BinomForest rk a -> b -> b
seqSpineF :: forall (rk :: * -> *) a b. BinomForest rk a -> b -> b
seqSpineF BinomForest rk a
Nil b
z = b
z
seqSpineF (Skip BinomForest (Succ rk) a
ts') b
z = forall (rk :: * -> *) a b. BinomForest rk a -> b -> b
seqSpineF BinomForest (Succ rk) a
ts' b
z
seqSpineF (Cons BinomTree rk a
_ BinomForest (Succ rk) a
ts') b
z = forall (rk :: * -> *) a b. BinomForest rk a -> b -> b
seqSpineF BinomForest (Succ rk) a
ts' b
z
class NFRank rk where
rnfRk :: NFData a => rk a -> ()
instance NFRank Zero where
rnfRk :: forall a. NFData a => Zero a -> ()
rnfRk Zero a
_ = ()
instance NFRank rk => NFRank (Succ rk) where
rnfRk :: forall a. NFData a => Succ rk a -> ()
rnfRk (Succ BinomTree rk a
t rk a
ts) = BinomTree rk a
t forall a b. NFData a => a -> b -> b
`deepseq` forall (rk :: * -> *) a. (NFRank rk, NFData a) => rk a -> ()
rnfRk rk a
ts
instance (NFData a, NFRank rk) => NFData (BinomTree rk a) where
rnf :: BinomTree rk a -> ()
rnf (BinomTree a
x rk a
ts) = a
x forall a b. NFData a => a -> b -> b
`deepseq` forall (rk :: * -> *) a. (NFRank rk, NFData a) => rk a -> ()
rnfRk rk a
ts
instance (NFData a, NFRank rk) => NFData (BinomForest rk a) where
rnf :: BinomForest rk a -> ()
rnf BinomForest rk a
Nil = ()
rnf (Skip BinomForest (Succ rk) a
ts) = forall a. NFData a => a -> ()
rnf BinomForest (Succ rk) a
ts
rnf (Cons BinomTree rk a
t BinomForest (Succ rk) a
ts) = BinomTree rk a
t forall a b. NFData a => a -> b -> b
`deepseq` forall a. NFData a => a -> ()
rnf BinomForest (Succ rk) a
ts
instance NFData a => NFData (MinQueue a) where
rnf :: MinQueue a -> ()
rnf (MinQueue BinomHeap a
ts) = forall a. NFData a => a -> ()
rnf BinomHeap a
ts
instance (Ord a, Show a) => Show (MinQueue a) where
showsPrec :: Int -> MinQueue a -> ShowS
showsPrec Int
p MinQueue a
xs = Bool -> ShowS -> ShowS
showParen (Int
p forall a. Ord a => a -> a -> Bool
> Int
10) forall a b. (a -> b) -> a -> b
$
[Char] -> ShowS
showString [Char]
"fromAscList " forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall a. Show a => a -> ShowS
shows (forall a. Ord a => MinQueue a -> [a]
toAscList MinQueue a
xs)
instance Read a => Read (MinQueue a) where
#ifdef __GLASGOW_HASKELL__
readPrec :: ReadPrec (MinQueue a)
readPrec = forall a. ReadPrec a -> ReadPrec a
parens forall a b. (a -> b) -> a -> b
$ forall a. Int -> ReadPrec a -> ReadPrec a
prec Int
10 forall a b. (a -> b) -> a -> b
$ do
Ident [Char]
"fromAscList" <- ReadPrec Lexeme
lexP
[a]
xs <- forall a. Read a => ReadPrec a
readPrec
forall (m :: * -> *) a. Monad m => a -> m a
return (forall a. [a] -> MinQueue a
fromAscList [a]
xs)
readListPrec :: ReadPrec [MinQueue a]
readListPrec = forall a. Read a => ReadPrec [a]
readListPrecDefault
#else
readsPrec p = readParen (p > 10) $ \r -> do
("fromAscList",s) <- lex r
(xs,t) <- reads s
return (fromAscList xs,t)
#endif
#if MIN_VERSION_base(4,9,0)
instance Ord a => Semigroup (MinQueue a) where
<> :: MinQueue a -> MinQueue a -> MinQueue a
(<>) = forall a. Ord a => MinQueue a -> MinQueue a -> MinQueue a
union
stimes :: forall b. Integral b => b -> MinQueue a -> MinQueue a
stimes = forall b a. (Integral b, Monoid a) => b -> a -> a
stimesMonoid
{-# INLINABLE stimes #-}
#endif
instance Ord a => Monoid (MinQueue a) where
mempty :: MinQueue a
mempty = forall a. MinQueue a
empty
#if !MIN_VERSION_base(4,11,0)
mappend = union
#endif
mconcat :: [MinQueue a] -> MinQueue a
mconcat = forall a. Ord a => [MinQueue a] -> MinQueue a
unions