{-# LANGUAGE BangPatterns #-}
{-# OPTIONS_HADDOCK not-home #-}
module Data.Sequence.Internal.Sorting
(
sort
,sortBy
,sortOn
,unstableSort
,unstableSortBy
,unstableSortOn
,
Queue(..)
,QList(..)
,IndexedQueue(..)
,IQList(..)
,TaggedQueue(..)
,TQList(..)
,IndexedTaggedQueue(..)
,ITQList(..)
,
mergeQ
,mergeIQ
,mergeTQ
,mergeITQ
,
popMinQ
,popMinIQ
,popMinTQ
,popMinITQ
,
buildQ
,buildIQ
,buildTQ
,buildITQ
,
foldToMaybeTree
,foldToMaybeWithIndexTree)
where
import Data.Sequence.Internal
(Elem(..), Seq(..), Node(..), Digit(..), Sized(..), FingerTree(..),
replicateA, foldDigit, foldNode, foldWithIndexDigit,
foldWithIndexNode)
import Utils.Containers.Internal.State (State(..), execState)
sort :: Ord a => Seq a -> Seq a
sort :: forall a. Ord a => Seq a -> Seq a
sort = forall a. (a -> a -> Ordering) -> Seq a -> Seq a
sortBy forall a. Ord a => a -> a -> Ordering
compare
sortBy :: (a -> a -> Ordering) -> Seq a -> Seq a
sortBy :: forall a. (a -> a -> Ordering) -> Seq a -> Seq a
sortBy a -> a -> Ordering
cmp (Seq FingerTree (Elem a)
xs) =
forall b a. b -> (a -> b) -> Maybe a -> b
maybe
(forall a. FingerTree (Elem a) -> Seq a
Seq forall a. FingerTree a
EmptyT)
(forall s a. State s a -> s -> a
execState (forall (f :: * -> *) a. Applicative f => Int -> f a -> f (Seq a)
replicateA (forall a. Sized a => a -> Int
size FingerTree (Elem a)
xs) (forall s a. (s -> (s, a)) -> State s a
State (forall e.
(e -> e -> Ordering) -> IndexedQueue e -> (IndexedQueue e, e)
popMinIQ a -> a -> Ordering
cmp))))
(forall b y.
(b -> b -> Ordering)
-> (Int -> Elem y -> IndexedQueue b)
-> Int
-> FingerTree (Elem y)
-> Maybe (IndexedQueue b)
buildIQ a -> a -> Ordering
cmp (\Int
s (Elem a
x) -> forall e. Int -> e -> IQList e -> IndexedQueue e
IQ Int
s a
x forall e. IQList e
IQNil) Int
0 FingerTree (Elem a)
xs)
sortOn :: Ord b => (a -> b) -> Seq a -> Seq a
sortOn :: forall b a. Ord b => (a -> b) -> Seq a -> Seq a
sortOn a -> b
f (Seq FingerTree (Elem a)
xs) =
forall b a. b -> (a -> b) -> Maybe a -> b
maybe
(forall a. FingerTree (Elem a) -> Seq a
Seq forall a. FingerTree a
EmptyT)
(forall s a. State s a -> s -> a
execState (forall (f :: * -> *) a. Applicative f => Int -> f a -> f (Seq a)
replicateA (forall a. Sized a => a -> Int
size FingerTree (Elem a)
xs) (forall s a. (s -> (s, a)) -> State s a
State (forall e b.
(e -> e -> Ordering)
-> IndexedTaggedQueue e b -> (IndexedTaggedQueue e b, b)
popMinITQ forall a. Ord a => a -> a -> Ordering
compare))))
(forall b y c.
(b -> b -> Ordering)
-> (Int -> Elem y -> IndexedTaggedQueue b c)
-> Int
-> FingerTree (Elem y)
-> Maybe (IndexedTaggedQueue b c)
buildITQ forall a. Ord a => a -> a -> Ordering
compare (\Int
s (Elem a
x) -> forall e a. Int -> e -> a -> ITQList e a -> IndexedTaggedQueue e a
ITQ Int
s (a -> b
f a
x) a
x forall e a. ITQList e a
ITQNil) Int
0 FingerTree (Elem a)
xs)
unstableSort :: Ord a => Seq a -> Seq a
unstableSort :: forall a. Ord a => Seq a -> Seq a
unstableSort = forall a. (a -> a -> Ordering) -> Seq a -> Seq a
unstableSortBy forall a. Ord a => a -> a -> Ordering
compare
unstableSortBy :: (a -> a -> Ordering) -> Seq a -> Seq a
unstableSortBy :: forall a. (a -> a -> Ordering) -> Seq a -> Seq a
unstableSortBy a -> a -> Ordering
cmp (Seq FingerTree (Elem a)
xs) =
forall b a. b -> (a -> b) -> Maybe a -> b
maybe
(forall a. FingerTree (Elem a) -> Seq a
Seq forall a. FingerTree a
EmptyT)
(forall s a. State s a -> s -> a
execState (forall (f :: * -> *) a. Applicative f => Int -> f a -> f (Seq a)
replicateA (forall a. Sized a => a -> Int
size FingerTree (Elem a)
xs) (forall s a. (s -> (s, a)) -> State s a
State (forall e. (e -> e -> Ordering) -> Queue e -> (Queue e, e)
popMinQ a -> a -> Ordering
cmp))))
(forall b a.
(b -> b -> Ordering)
-> (a -> Queue b) -> FingerTree a -> Maybe (Queue b)
buildQ a -> a -> Ordering
cmp (\(Elem a
x) -> forall e. e -> QList e -> Queue e
Q a
x forall e. QList e
Nil) FingerTree (Elem a)
xs)
unstableSortOn :: Ord b => (a -> b) -> Seq a -> Seq a
unstableSortOn :: forall b a. Ord b => (a -> b) -> Seq a -> Seq a
unstableSortOn a -> b
f (Seq FingerTree (Elem a)
xs) =
forall b a. b -> (a -> b) -> Maybe a -> b
maybe
(forall a. FingerTree (Elem a) -> Seq a
Seq forall a. FingerTree a
EmptyT)
(forall s a. State s a -> s -> a
execState (forall (f :: * -> *) a. Applicative f => Int -> f a -> f (Seq a)
replicateA (forall a. Sized a => a -> Int
size FingerTree (Elem a)
xs) (forall s a. (s -> (s, a)) -> State s a
State (forall a b.
(a -> a -> Ordering) -> TaggedQueue a b -> (TaggedQueue a b, b)
popMinTQ forall a. Ord a => a -> a -> Ordering
compare))))
(forall b a c.
(b -> b -> Ordering)
-> (a -> TaggedQueue b c)
-> FingerTree a
-> Maybe (TaggedQueue b c)
buildTQ forall a. Ord a => a -> a -> Ordering
compare (\(Elem a
x) -> forall a b. a -> b -> TQList a b -> TaggedQueue a b
TQ (a -> b
f a
x) a
x forall a b. TQList a b
TQNil) FingerTree (Elem a)
xs)
data Queue e = Q !e (QList e)
data QList e
= Nil
| QCons {-# UNPACK #-} !(Queue e)
(QList e)
data IndexedQueue e =
IQ {-# UNPACK #-} !Int !e (IQList e)
data IQList e
= IQNil
| IQCons {-# UNPACK #-} !(IndexedQueue e)
(IQList e)
data TaggedQueue a b =
TQ !a b (TQList a b)
data TQList a b
= TQNil
| TQCons {-# UNPACK #-} !(TaggedQueue a b)
(TQList a b)
data IndexedTaggedQueue e a =
ITQ {-# UNPACK #-} !Int !e a (ITQList e a)
data ITQList e a
= ITQNil
| ITQCons {-# UNPACK #-} !(IndexedTaggedQueue e a)
(ITQList e a)
infixr 8 `ITQCons`, `TQCons`, `QCons`, `IQCons`
mergeQ :: (a -> a -> Ordering) -> Queue a -> Queue a -> Queue a
mergeQ :: forall a. (a -> a -> Ordering) -> Queue a -> Queue a -> Queue a
mergeQ a -> a -> Ordering
cmp q1 :: Queue a
q1@(Q a
x1 QList a
ts1) q2 :: Queue a
q2@(Q a
x2 QList a
ts2)
| a -> a -> Ordering
cmp a
x1 a
x2 forall a. Eq a => a -> a -> Bool
== Ordering
GT = forall e. e -> QList e -> Queue e
Q a
x2 (Queue a
q1 forall e. Queue e -> QList e -> QList e
`QCons` QList a
ts2)
| Bool
otherwise = forall e. e -> QList e -> Queue e
Q a
x1 (Queue a
q2 forall e. Queue e -> QList e -> QList e
`QCons` QList a
ts1)
mergeTQ :: (a -> a -> Ordering)
-> TaggedQueue a b
-> TaggedQueue a b
-> TaggedQueue a b
mergeTQ :: forall a b.
(a -> a -> Ordering)
-> TaggedQueue a b -> TaggedQueue a b -> TaggedQueue a b
mergeTQ a -> a -> Ordering
cmp q1 :: TaggedQueue a b
q1@(TQ a
x1 b
y1 TQList a b
ts1) q2 :: TaggedQueue a b
q2@(TQ a
x2 b
y2 TQList a b
ts2)
| a -> a -> Ordering
cmp a
x1 a
x2 forall a. Eq a => a -> a -> Bool
== Ordering
GT = forall a b. a -> b -> TQList a b -> TaggedQueue a b
TQ a
x2 b
y2 (TaggedQueue a b
q1 forall a b. TaggedQueue a b -> TQList a b -> TQList a b
`TQCons` TQList a b
ts2)
| Bool
otherwise = forall a b. a -> b -> TQList a b -> TaggedQueue a b
TQ a
x1 b
y1 (TaggedQueue a b
q2 forall a b. TaggedQueue a b -> TQList a b -> TQList a b
`TQCons` TQList a b
ts1)
mergeIQ :: (a -> a -> Ordering)
-> IndexedQueue a
-> IndexedQueue a
-> IndexedQueue a
mergeIQ :: forall a.
(a -> a -> Ordering)
-> IndexedQueue a -> IndexedQueue a -> IndexedQueue a
mergeIQ a -> a -> Ordering
cmp q1 :: IndexedQueue a
q1@(IQ Int
i1 a
x1 IQList a
ts1) q2 :: IndexedQueue a
q2@(IQ Int
i2 a
x2 IQList a
ts2) =
case a -> a -> Ordering
cmp a
x1 a
x2 of
Ordering
LT -> forall e. Int -> e -> IQList e -> IndexedQueue e
IQ Int
i1 a
x1 (IndexedQueue a
q2 forall e. IndexedQueue e -> IQList e -> IQList e
`IQCons` IQList a
ts1)
Ordering
EQ | Int
i1 forall a. Ord a => a -> a -> Bool
<= Int
i2 -> forall e. Int -> e -> IQList e -> IndexedQueue e
IQ Int
i1 a
x1 (IndexedQueue a
q2 forall e. IndexedQueue e -> IQList e -> IQList e
`IQCons` IQList a
ts1)
Ordering
_ -> forall e. Int -> e -> IQList e -> IndexedQueue e
IQ Int
i2 a
x2 (IndexedQueue a
q1 forall e. IndexedQueue e -> IQList e -> IQList e
`IQCons` IQList a
ts2)
mergeITQ
:: (a -> a -> Ordering)
-> IndexedTaggedQueue a b
-> IndexedTaggedQueue a b
-> IndexedTaggedQueue a b
mergeITQ :: forall a b.
(a -> a -> Ordering)
-> IndexedTaggedQueue a b
-> IndexedTaggedQueue a b
-> IndexedTaggedQueue a b
mergeITQ a -> a -> Ordering
cmp q1 :: IndexedTaggedQueue a b
q1@(ITQ Int
i1 a
x1 b
y1 ITQList a b
ts1) q2 :: IndexedTaggedQueue a b
q2@(ITQ Int
i2 a
x2 b
y2 ITQList a b
ts2) =
case a -> a -> Ordering
cmp a
x1 a
x2 of
Ordering
LT -> forall e a. Int -> e -> a -> ITQList e a -> IndexedTaggedQueue e a
ITQ Int
i1 a
x1 b
y1 (IndexedTaggedQueue a b
q2 forall e a. IndexedTaggedQueue e a -> ITQList e a -> ITQList e a
`ITQCons` ITQList a b
ts1)
Ordering
EQ | Int
i1 forall a. Ord a => a -> a -> Bool
<= Int
i2 -> forall e a. Int -> e -> a -> ITQList e a -> IndexedTaggedQueue e a
ITQ Int
i1 a
x1 b
y1 (IndexedTaggedQueue a b
q2 forall e a. IndexedTaggedQueue e a -> ITQList e a -> ITQList e a
`ITQCons` ITQList a b
ts1)
Ordering
_ -> forall e a. Int -> e -> a -> ITQList e a -> IndexedTaggedQueue e a
ITQ Int
i2 a
x2 b
y2 (IndexedTaggedQueue a b
q1 forall e a. IndexedTaggedQueue e a -> ITQList e a -> ITQList e a
`ITQCons` ITQList a b
ts2)
popMinQ :: (e -> e -> Ordering) -> Queue e -> (Queue e, e)
popMinQ :: forall e. (e -> e -> Ordering) -> Queue e -> (Queue e, e)
popMinQ e -> e -> Ordering
cmp (Q e
x QList e
xs) = (QList e -> Queue e
mergeQs QList e
xs, e
x)
where
mergeQs :: QList e -> Queue e
mergeQs (Queue e
t `QCons` QList e
Nil) = Queue e
t
mergeQs (Queue e
t1 `QCons` Queue e
t2 `QCons` QList e
Nil) = Queue e
t1 Queue e -> Queue e -> Queue e
<+> Queue e
t2
mergeQs (Queue e
t1 `QCons` Queue e
t2 `QCons` QList e
ts) = (Queue e
t1 Queue e -> Queue e -> Queue e
<+> Queue e
t2) Queue e -> Queue e -> Queue e
<+> QList e -> Queue e
mergeQs QList e
ts
mergeQs QList e
Nil = forall a. HasCallStack => [Char] -> a
error [Char]
"popMinQ: tried to pop from empty queue"
<+> :: Queue e -> Queue e -> Queue e
(<+>) = forall a. (a -> a -> Ordering) -> Queue a -> Queue a -> Queue a
mergeQ e -> e -> Ordering
cmp
popMinIQ :: (e -> e -> Ordering) -> IndexedQueue e -> (IndexedQueue e, e)
popMinIQ :: forall e.
(e -> e -> Ordering) -> IndexedQueue e -> (IndexedQueue e, e)
popMinIQ e -> e -> Ordering
cmp (IQ Int
_ e
x IQList e
xs) = (IQList e -> IndexedQueue e
mergeQs IQList e
xs, e
x)
where
mergeQs :: IQList e -> IndexedQueue e
mergeQs (IndexedQueue e
t `IQCons` IQList e
IQNil) = IndexedQueue e
t
mergeQs (IndexedQueue e
t1 `IQCons` IndexedQueue e
t2 `IQCons` IQList e
IQNil) = IndexedQueue e
t1 IndexedQueue e -> IndexedQueue e -> IndexedQueue e
<+> IndexedQueue e
t2
mergeQs (IndexedQueue e
t1 `IQCons` IndexedQueue e
t2 `IQCons` IQList e
ts) = (IndexedQueue e
t1 IndexedQueue e -> IndexedQueue e -> IndexedQueue e
<+> IndexedQueue e
t2) IndexedQueue e -> IndexedQueue e -> IndexedQueue e
<+> IQList e -> IndexedQueue e
mergeQs IQList e
ts
mergeQs IQList e
IQNil = forall a. HasCallStack => [Char] -> a
error [Char]
"popMinQ: tried to pop from empty queue"
<+> :: IndexedQueue e -> IndexedQueue e -> IndexedQueue e
(<+>) = forall a.
(a -> a -> Ordering)
-> IndexedQueue a -> IndexedQueue a -> IndexedQueue a
mergeIQ e -> e -> Ordering
cmp
popMinTQ :: (a -> a -> Ordering) -> TaggedQueue a b -> (TaggedQueue a b, b)
popMinTQ :: forall a b.
(a -> a -> Ordering) -> TaggedQueue a b -> (TaggedQueue a b, b)
popMinTQ a -> a -> Ordering
cmp (TQ a
_ b
x TQList a b
xs) = (forall {b}. TQList a b -> TaggedQueue a b
mergeQs TQList a b
xs, b
x)
where
mergeQs :: TQList a b -> TaggedQueue a b
mergeQs (TaggedQueue a b
t `TQCons` TQList a b
TQNil) = TaggedQueue a b
t
mergeQs (TaggedQueue a b
t1 `TQCons` TaggedQueue a b
t2 `TQCons` TQList a b
TQNil) = TaggedQueue a b
t1 forall {b}. TaggedQueue a b -> TaggedQueue a b -> TaggedQueue a b
<+> TaggedQueue a b
t2
mergeQs (TaggedQueue a b
t1 `TQCons` TaggedQueue a b
t2 `TQCons` TQList a b
ts) = (TaggedQueue a b
t1 forall {b}. TaggedQueue a b -> TaggedQueue a b -> TaggedQueue a b
<+> TaggedQueue a b
t2) forall {b}. TaggedQueue a b -> TaggedQueue a b -> TaggedQueue a b
<+> TQList a b -> TaggedQueue a b
mergeQs TQList a b
ts
mergeQs TQList a b
TQNil = forall a. HasCallStack => [Char] -> a
error [Char]
"popMinQ: tried to pop from empty queue"
<+> :: TaggedQueue a b -> TaggedQueue a b -> TaggedQueue a b
(<+>) = forall a b.
(a -> a -> Ordering)
-> TaggedQueue a b -> TaggedQueue a b -> TaggedQueue a b
mergeTQ a -> a -> Ordering
cmp
popMinITQ :: (e -> e -> Ordering)
-> IndexedTaggedQueue e b
-> (IndexedTaggedQueue e b, b)
popMinITQ :: forall e b.
(e -> e -> Ordering)
-> IndexedTaggedQueue e b -> (IndexedTaggedQueue e b, b)
popMinITQ e -> e -> Ordering
cmp (ITQ Int
_ e
_ b
x ITQList e b
xs) = (forall {a}. ITQList e a -> IndexedTaggedQueue e a
mergeQs ITQList e b
xs, b
x)
where
mergeQs :: ITQList e a -> IndexedTaggedQueue e a
mergeQs (IndexedTaggedQueue e a
t `ITQCons` ITQList e a
ITQNil) = IndexedTaggedQueue e a
t
mergeQs (IndexedTaggedQueue e a
t1 `ITQCons` IndexedTaggedQueue e a
t2 `ITQCons` ITQList e a
ITQNil) = IndexedTaggedQueue e a
t1 forall {b}.
IndexedTaggedQueue e b
-> IndexedTaggedQueue e b -> IndexedTaggedQueue e b
<+> IndexedTaggedQueue e a
t2
mergeQs (IndexedTaggedQueue e a
t1 `ITQCons` IndexedTaggedQueue e a
t2 `ITQCons` ITQList e a
ts) = (IndexedTaggedQueue e a
t1 forall {b}.
IndexedTaggedQueue e b
-> IndexedTaggedQueue e b -> IndexedTaggedQueue e b
<+> IndexedTaggedQueue e a
t2) forall {b}.
IndexedTaggedQueue e b
-> IndexedTaggedQueue e b -> IndexedTaggedQueue e b
<+> ITQList e a -> IndexedTaggedQueue e a
mergeQs ITQList e a
ts
mergeQs ITQList e a
ITQNil = forall a. HasCallStack => [Char] -> a
error [Char]
"popMinQ: tried to pop from empty queue"
<+> :: IndexedTaggedQueue e b
-> IndexedTaggedQueue e b -> IndexedTaggedQueue e b
(<+>) = forall a b.
(a -> a -> Ordering)
-> IndexedTaggedQueue a b
-> IndexedTaggedQueue a b
-> IndexedTaggedQueue a b
mergeITQ e -> e -> Ordering
cmp
buildQ :: (b -> b -> Ordering) -> (a -> Queue b) -> FingerTree a -> Maybe (Queue b)
buildQ :: forall b a.
(b -> b -> Ordering)
-> (a -> Queue b) -> FingerTree a -> Maybe (Queue b)
buildQ b -> b -> Ordering
cmp = forall b a. (b -> b -> b) -> (a -> b) -> FingerTree a -> Maybe b
foldToMaybeTree (forall a. (a -> a -> Ordering) -> Queue a -> Queue a -> Queue a
mergeQ b -> b -> Ordering
cmp)
buildIQ
:: (b -> b -> Ordering)
-> (Int -> Elem y -> IndexedQueue b)
-> Int
-> FingerTree (Elem y)
-> Maybe (IndexedQueue b)
buildIQ :: forall b y.
(b -> b -> Ordering)
-> (Int -> Elem y -> IndexedQueue b)
-> Int
-> FingerTree (Elem y)
-> Maybe (IndexedQueue b)
buildIQ b -> b -> Ordering
cmp = forall b y.
(b -> b -> b)
-> (Int -> Elem y -> b) -> Int -> FingerTree (Elem y) -> Maybe b
foldToMaybeWithIndexTree (forall a.
(a -> a -> Ordering)
-> IndexedQueue a -> IndexedQueue a -> IndexedQueue a
mergeIQ b -> b -> Ordering
cmp)
buildTQ
:: (b -> b -> Ordering)
-> (a -> TaggedQueue b c)
-> FingerTree a
-> Maybe (TaggedQueue b c)
buildTQ :: forall b a c.
(b -> b -> Ordering)
-> (a -> TaggedQueue b c)
-> FingerTree a
-> Maybe (TaggedQueue b c)
buildTQ b -> b -> Ordering
cmp = forall b a. (b -> b -> b) -> (a -> b) -> FingerTree a -> Maybe b
foldToMaybeTree (forall a b.
(a -> a -> Ordering)
-> TaggedQueue a b -> TaggedQueue a b -> TaggedQueue a b
mergeTQ b -> b -> Ordering
cmp)
buildITQ
:: (b -> b -> Ordering)
-> (Int -> Elem y -> IndexedTaggedQueue b c)
-> Int
-> FingerTree (Elem y)
-> Maybe (IndexedTaggedQueue b c)
buildITQ :: forall b y c.
(b -> b -> Ordering)
-> (Int -> Elem y -> IndexedTaggedQueue b c)
-> Int
-> FingerTree (Elem y)
-> Maybe (IndexedTaggedQueue b c)
buildITQ b -> b -> Ordering
cmp = forall b y.
(b -> b -> b)
-> (Int -> Elem y -> b) -> Int -> FingerTree (Elem y) -> Maybe b
foldToMaybeWithIndexTree (forall a b.
(a -> a -> Ordering)
-> IndexedTaggedQueue a b
-> IndexedTaggedQueue a b
-> IndexedTaggedQueue a b
mergeITQ b -> b -> Ordering
cmp)
foldToMaybeTree :: (b -> b -> b) -> (a -> b) -> FingerTree a -> Maybe b
foldToMaybeTree :: forall b a. (b -> b -> b) -> (a -> b) -> FingerTree a -> Maybe b
foldToMaybeTree b -> b -> b
_ a -> b
_ FingerTree a
EmptyT = forall a. Maybe a
Nothing
foldToMaybeTree b -> b -> b
_ a -> b
f (Single a
xs) = forall a. a -> Maybe a
Just (a -> b
f a
xs)
foldToMaybeTree b -> b -> b
(<+>) a -> b
f (Deep Int
_ Digit a
pr FingerTree (Node a)
m Digit a
sf) =
forall a. a -> Maybe a
Just (forall b a. b -> (a -> b) -> Maybe a -> b
maybe (b
pr' b -> b -> b
<+> b
sf') ((b
pr' b -> b -> b
<+> b
sf') b -> b -> b
<+>) Maybe b
m')
where
pr' :: b
pr' = forall b a. (b -> b -> b) -> (a -> b) -> Digit a -> b
foldDigit b -> b -> b
(<+>) a -> b
f Digit a
pr
sf' :: b
sf' = forall b a. (b -> b -> b) -> (a -> b) -> Digit a -> b
foldDigit b -> b -> b
(<+>) a -> b
f Digit a
sf
m' :: Maybe b
m' = forall b a. (b -> b -> b) -> (a -> b) -> FingerTree a -> Maybe b
foldToMaybeTree b -> b -> b
(<+>) (forall b a. (b -> b -> b) -> (a -> b) -> Node a -> b
foldNode b -> b -> b
(<+>) a -> b
f) FingerTree (Node a)
m
foldToMaybeWithIndexTree :: (b -> b -> b)
-> (Int -> Elem y -> b)
-> Int
-> FingerTree (Elem y)
-> Maybe b
foldToMaybeWithIndexTree :: forall b y.
(b -> b -> b)
-> (Int -> Elem y -> b) -> Int -> FingerTree (Elem y) -> Maybe b
foldToMaybeWithIndexTree = forall a b.
Sized a =>
(b -> b -> b) -> (Int -> a -> b) -> Int -> FingerTree a -> Maybe b
foldToMaybeWithIndexTree'
where
{-# SPECIALISE foldToMaybeWithIndexTree' :: (b -> b -> b) -> (Int -> Elem y -> b) -> Int -> FingerTree (Elem y) -> Maybe b #-}
{-# SPECIALISE foldToMaybeWithIndexTree' :: (b -> b -> b) -> (Int -> Node y -> b) -> Int -> FingerTree (Node y) -> Maybe b #-}
foldToMaybeWithIndexTree'
:: Sized a
=> (b -> b -> b) -> (Int -> a -> b) -> Int -> FingerTree a -> Maybe b
foldToMaybeWithIndexTree' :: forall a b.
Sized a =>
(b -> b -> b) -> (Int -> a -> b) -> Int -> FingerTree a -> Maybe b
foldToMaybeWithIndexTree' b -> b -> b
_ Int -> a -> b
_ !Int
_s FingerTree a
EmptyT = forall a. Maybe a
Nothing
foldToMaybeWithIndexTree' b -> b -> b
_ Int -> a -> b
f Int
s (Single a
xs) = forall a. a -> Maybe a
Just (Int -> a -> b
f Int
s a
xs)
foldToMaybeWithIndexTree' b -> b -> b
(<+>) Int -> a -> b
f Int
s (Deep Int
_ Digit a
pr FingerTree (Node a)
m Digit a
sf) =
forall a. a -> Maybe a
Just (forall b a. b -> (a -> b) -> Maybe a -> b
maybe (b
pr' b -> b -> b
<+> b
sf') ((b
pr' b -> b -> b
<+> b
sf') b -> b -> b
<+>) Maybe b
m')
where
pr' :: b
pr' = forall a b.
Sized a =>
(b -> b -> b) -> (Int -> a -> b) -> Int -> Digit a -> b
digit b -> b -> b
(<+>) Int -> a -> b
f Int
s Digit a
pr
sf' :: b
sf' = forall a b.
Sized a =>
(b -> b -> b) -> (Int -> a -> b) -> Int -> Digit a -> b
digit b -> b -> b
(<+>) Int -> a -> b
f Int
sPsprm Digit a
sf
m' :: Maybe b
m' = forall a b.
Sized a =>
(b -> b -> b) -> (Int -> a -> b) -> Int -> FingerTree a -> Maybe b
foldToMaybeWithIndexTree' b -> b -> b
(<+>) (forall a b.
Sized a =>
(b -> b -> b) -> (Int -> a -> b) -> Int -> Node a -> b
node b -> b -> b
(<+>) Int -> a -> b
f) Int
sPspr FingerTree (Node a)
m
!sPspr :: Int
sPspr = Int
s forall a. Num a => a -> a -> a
+ forall a. Sized a => a -> Int
size Digit a
pr
!sPsprm :: Int
sPsprm = Int
sPspr forall a. Num a => a -> a -> a
+ forall a. Sized a => a -> Int
size FingerTree (Node a)
m
{-# SPECIALISE digit :: (b -> b -> b) -> (Int -> Elem y -> b) -> Int -> Digit (Elem y) -> b #-}
{-# SPECIALISE digit :: (b -> b -> b) -> (Int -> Node y -> b) -> Int -> Digit (Node y) -> b #-}
digit
:: Sized a
=> (b -> b -> b) -> (Int -> a -> b) -> Int -> Digit a -> b
digit :: forall a b.
Sized a =>
(b -> b -> b) -> (Int -> a -> b) -> Int -> Digit a -> b
digit = forall a b.
Sized a =>
(b -> b -> b) -> (Int -> a -> b) -> Int -> Digit a -> b
foldWithIndexDigit
{-# SPECIALISE node :: (b -> b -> b) -> (Int -> Elem y -> b) -> Int -> Node (Elem y) -> b #-}
{-# SPECIALISE node :: (b -> b -> b) -> (Int -> Node y -> b) -> Int -> Node (Node y) -> b #-}
node
:: Sized a
=> (b -> b -> b) -> (Int -> a -> b) -> Int -> Node a -> b
node :: forall a b.
Sized a =>
(b -> b -> b) -> (Int -> a -> b) -> Int -> Node a -> b
node = forall a b.
Sized a =>
(b -> b -> b) -> (Int -> a -> b) -> Int -> Node a -> b
foldWithIndexNode
{-# INLINE foldToMaybeWithIndexTree #-}