{-# LANGUAGE BangPatterns #-}
{-# LANGUAGE DeriveGeneric #-}
{-# LANGUAGE DerivingStrategies #-}
{-# LANGUAGE FlexibleInstances #-}
{-# LANGUAGE GeneralizedNewtypeDeriving #-}
{-# LANGUAGE MultiParamTypeClasses #-}
{-# LANGUAGE StandaloneDeriving #-}
module Data.FingerTree.Strict
( StrictFingerTree,
fromStrict,
forceToStrict,
empty,
singleton,
(<|),
(|>),
(><),
fromList,
null,
viewl,
viewr,
SearchResult (..),
search,
split,
takeUntil,
dropUntil,
reverse,
fmap',
unsafeFmap,
Measured (..),
ViewL (..),
ViewR (..),
)
where
import Data.FingerTree (Measured (..), ViewL (..), ViewR (..))
import qualified Data.FingerTree as FT
import Data.Foldable (foldl', toList)
import Data.Unit.Strict (forceElemsToWHNF)
import GHC.Generics (Generic)
import NoThunks.Class (NoThunks (..), noThunksInValues)
import Prelude hiding (null, reverse)
infixr 5 ><
infixr 5 <|
infixl 5 |>
newtype StrictFingerTree v a = SFT {StrictFingerTree v a -> FingerTree v a
fromStrict :: FT.FingerTree v a}
deriving (StrictFingerTree v a -> StrictFingerTree v a -> Bool
(StrictFingerTree v a -> StrictFingerTree v a -> Bool)
-> (StrictFingerTree v a -> StrictFingerTree v a -> Bool)
-> Eq (StrictFingerTree v a)
forall a. (a -> a -> Bool) -> (a -> a -> Bool) -> Eq a
forall v a.
Eq a =>
StrictFingerTree v a -> StrictFingerTree v a -> Bool
/= :: StrictFingerTree v a -> StrictFingerTree v a -> Bool
$c/= :: forall v a.
Eq a =>
StrictFingerTree v a -> StrictFingerTree v a -> Bool
== :: StrictFingerTree v a -> StrictFingerTree v a -> Bool
$c== :: forall v a.
Eq a =>
StrictFingerTree v a -> StrictFingerTree v a -> Bool
Eq, Eq (StrictFingerTree v a)
Eq (StrictFingerTree v a)
-> (StrictFingerTree v a -> StrictFingerTree v a -> Ordering)
-> (StrictFingerTree v a -> StrictFingerTree v a -> Bool)
-> (StrictFingerTree v a -> StrictFingerTree v a -> Bool)
-> (StrictFingerTree v a -> StrictFingerTree v a -> Bool)
-> (StrictFingerTree v a -> StrictFingerTree v a -> Bool)
-> (StrictFingerTree v a
-> StrictFingerTree v a -> StrictFingerTree v a)
-> (StrictFingerTree v a
-> StrictFingerTree v a -> StrictFingerTree v a)
-> Ord (StrictFingerTree v a)
StrictFingerTree v a -> StrictFingerTree v a -> Bool
StrictFingerTree v a -> StrictFingerTree v a -> Ordering
StrictFingerTree v a
-> StrictFingerTree v a -> StrictFingerTree v a
forall a.
Eq a
-> (a -> a -> Ordering)
-> (a -> a -> Bool)
-> (a -> a -> Bool)
-> (a -> a -> Bool)
-> (a -> a -> Bool)
-> (a -> a -> a)
-> (a -> a -> a)
-> Ord a
forall v a. Ord a => Eq (StrictFingerTree v a)
forall v a.
Ord a =>
StrictFingerTree v a -> StrictFingerTree v a -> Bool
forall v a.
Ord a =>
StrictFingerTree v a -> StrictFingerTree v a -> Ordering
forall v a.
Ord a =>
StrictFingerTree v a
-> StrictFingerTree v a -> StrictFingerTree v a
min :: StrictFingerTree v a
-> StrictFingerTree v a -> StrictFingerTree v a
$cmin :: forall v a.
Ord a =>
StrictFingerTree v a
-> StrictFingerTree v a -> StrictFingerTree v a
max :: StrictFingerTree v a
-> StrictFingerTree v a -> StrictFingerTree v a
$cmax :: forall v a.
Ord a =>
StrictFingerTree v a
-> StrictFingerTree v a -> StrictFingerTree v a
>= :: StrictFingerTree v a -> StrictFingerTree v a -> Bool
$c>= :: forall v a.
Ord a =>
StrictFingerTree v a -> StrictFingerTree v a -> Bool
> :: StrictFingerTree v a -> StrictFingerTree v a -> Bool
$c> :: forall v a.
Ord a =>
StrictFingerTree v a -> StrictFingerTree v a -> Bool
<= :: StrictFingerTree v a -> StrictFingerTree v a -> Bool
$c<= :: forall v a.
Ord a =>
StrictFingerTree v a -> StrictFingerTree v a -> Bool
< :: StrictFingerTree v a -> StrictFingerTree v a -> Bool
$c< :: forall v a.
Ord a =>
StrictFingerTree v a -> StrictFingerTree v a -> Bool
compare :: StrictFingerTree v a -> StrictFingerTree v a -> Ordering
$ccompare :: forall v a.
Ord a =>
StrictFingerTree v a -> StrictFingerTree v a -> Ordering
$cp1Ord :: forall v a. Ord a => Eq (StrictFingerTree v a)
Ord, Int -> StrictFingerTree v a -> ShowS
[StrictFingerTree v a] -> ShowS
StrictFingerTree v a -> String
(Int -> StrictFingerTree v a -> ShowS)
-> (StrictFingerTree v a -> String)
-> ([StrictFingerTree v a] -> ShowS)
-> Show (StrictFingerTree v a)
forall a.
(Int -> a -> ShowS) -> (a -> String) -> ([a] -> ShowS) -> Show a
forall v a. Show a => Int -> StrictFingerTree v a -> ShowS
forall v a. Show a => [StrictFingerTree v a] -> ShowS
forall v a. Show a => StrictFingerTree v a -> String
showList :: [StrictFingerTree v a] -> ShowS
$cshowList :: forall v a. Show a => [StrictFingerTree v a] -> ShowS
show :: StrictFingerTree v a -> String
$cshow :: forall v a. Show a => StrictFingerTree v a -> String
showsPrec :: Int -> StrictFingerTree v a -> ShowS
$cshowsPrec :: forall v a. Show a => Int -> StrictFingerTree v a -> ShowS
Show)
deriving newtype instance Foldable (StrictFingerTree v)
deriving newtype instance (Measured v a) => Measured v (StrictFingerTree v a)
instance NoThunks a => NoThunks (StrictFingerTree v a) where
showTypeOf :: Proxy (StrictFingerTree v a) -> String
showTypeOf Proxy (StrictFingerTree v a)
_ = String
"StrictFingerTree"
wNoThunks :: Context -> StrictFingerTree v a -> IO (Maybe ThunkInfo)
wNoThunks Context
ctxt = Context -> [a] -> IO (Maybe ThunkInfo)
forall a. NoThunks a => Context -> [a] -> IO (Maybe ThunkInfo)
noThunksInValues Context
ctxt ([a] -> IO (Maybe ThunkInfo))
-> (StrictFingerTree v a -> [a])
-> StrictFingerTree v a
-> IO (Maybe ThunkInfo)
forall b c a. (b -> c) -> (a -> b) -> a -> c
. StrictFingerTree v a -> [a]
forall (t :: * -> *) a. Foldable t => t a -> [a]
toList
empty :: Measured v a => StrictFingerTree v a
empty :: StrictFingerTree v a
empty = FingerTree v a -> StrictFingerTree v a
forall v a. FingerTree v a -> StrictFingerTree v a
SFT FingerTree v a
forall v a. Measured v a => FingerTree v a
FT.empty
singleton :: Measured v a => a -> StrictFingerTree v a
singleton :: a -> StrictFingerTree v a
singleton !a
a = FingerTree v a -> StrictFingerTree v a
forall v a. FingerTree v a -> StrictFingerTree v a
SFT (a -> FingerTree v a
forall v a. Measured v a => a -> FingerTree v a
FT.singleton a
a)
fromList :: (Measured v a) => [a] -> StrictFingerTree v a
fromList :: [a] -> StrictFingerTree v a
fromList ![a]
xs = (StrictFingerTree v a -> a -> StrictFingerTree v a)
-> StrictFingerTree v a -> [a] -> StrictFingerTree v a
forall (t :: * -> *) b a.
Foldable t =>
(b -> a -> b) -> b -> t a -> b
foldl' StrictFingerTree v a -> a -> StrictFingerTree v a
forall v a.
Measured v a =>
StrictFingerTree v a -> a -> StrictFingerTree v a
(|>) (FingerTree v a -> StrictFingerTree v a
forall v a. FingerTree v a -> StrictFingerTree v a
SFT FingerTree v a
forall v a. Measured v a => FingerTree v a
FT.empty) [a]
xs
(<|) :: (Measured v a) => a -> StrictFingerTree v a -> StrictFingerTree v a
(!a
a) <| :: a -> StrictFingerTree v a -> StrictFingerTree v a
<| SFT FingerTree v a
ft = FingerTree v a -> StrictFingerTree v a
forall v a. FingerTree v a -> StrictFingerTree v a
SFT (a
a a -> FingerTree v a -> FingerTree v a
forall v a. Measured v a => a -> FingerTree v a -> FingerTree v a
FT.<| FingerTree v a
ft)
(|>) :: (Measured v a) => StrictFingerTree v a -> a -> StrictFingerTree v a
SFT FingerTree v a
ft |> :: StrictFingerTree v a -> a -> StrictFingerTree v a
|> (!a
a) = FingerTree v a -> StrictFingerTree v a
forall v a. FingerTree v a -> StrictFingerTree v a
SFT (FingerTree v a
ft FingerTree v a -> a -> FingerTree v a
forall v a. Measured v a => FingerTree v a -> a -> FingerTree v a
FT.|> a
a)
(><) ::
(Measured v a) =>
StrictFingerTree v a ->
StrictFingerTree v a ->
StrictFingerTree v a
SFT FingerTree v a
ft >< :: StrictFingerTree v a
-> StrictFingerTree v a -> StrictFingerTree v a
>< SFT FingerTree v a
ft' = FingerTree v a -> StrictFingerTree v a
forall v a. FingerTree v a -> StrictFingerTree v a
SFT (FingerTree v a
ft FingerTree v a -> FingerTree v a -> FingerTree v a
forall v a.
Measured v a =>
FingerTree v a -> FingerTree v a -> FingerTree v a
FT.>< FingerTree v a
ft')
forceToStrict :: FT.FingerTree v a -> StrictFingerTree v a
forceToStrict :: FingerTree v a -> StrictFingerTree v a
forceToStrict FingerTree v a
xs = FingerTree v a -> StrictFingerTree v a
forall v a. FingerTree v a -> StrictFingerTree v a
SFT (FingerTree v a -> FingerTree v a
forall (t :: * -> *) a. Foldable t => t a -> t a
forceElemsToWHNF FingerTree v a
xs)
null :: StrictFingerTree v a -> Bool
null :: StrictFingerTree v a -> Bool
null (SFT FingerTree v a
ft) = FingerTree v a -> Bool
forall v a. FingerTree v a -> Bool
FT.null FingerTree v a
ft
viewl ::
(Measured v a) =>
StrictFingerTree v a ->
ViewL (StrictFingerTree v) a
viewl :: StrictFingerTree v a -> ViewL (StrictFingerTree v) a
viewl (SFT FingerTree v a
ft) = case FingerTree v a -> ViewL (FingerTree v) a
forall v a.
Measured v a =>
FingerTree v a -> ViewL (FingerTree v) a
FT.viewl FingerTree v a
ft of
ViewL (FingerTree v) a
EmptyL -> ViewL (StrictFingerTree v) a
forall (s :: * -> *) a. ViewL s a
EmptyL
a
a :< FingerTree v a
ft' -> a
a a -> StrictFingerTree v a -> ViewL (StrictFingerTree v) a
forall (s :: * -> *) a. a -> s a -> ViewL s a
:< FingerTree v a -> StrictFingerTree v a
forall v a. FingerTree v a -> StrictFingerTree v a
SFT FingerTree v a
ft'
viewr ::
(Measured v a) =>
StrictFingerTree v a ->
ViewR (StrictFingerTree v) a
viewr :: StrictFingerTree v a -> ViewR (StrictFingerTree v) a
viewr (SFT FingerTree v a
ft) = case FingerTree v a -> ViewR (FingerTree v) a
forall v a.
Measured v a =>
FingerTree v a -> ViewR (FingerTree v) a
FT.viewr FingerTree v a
ft of
ViewR (FingerTree v) a
EmptyR -> ViewR (StrictFingerTree v) a
forall (s :: * -> *) a. ViewR s a
EmptyR
FingerTree v a
ft' :> a
a -> FingerTree v a -> StrictFingerTree v a
forall v a. FingerTree v a -> StrictFingerTree v a
SFT FingerTree v a
ft' StrictFingerTree v a -> a -> ViewR (StrictFingerTree v) a
forall (s :: * -> *) a. s a -> a -> ViewR s a
:> a
a
data SearchResult v a
=
Position !(StrictFingerTree v a) !a !(StrictFingerTree v a)
|
OnLeft
|
OnRight
|
Nowhere
deriving (SearchResult v a -> SearchResult v a -> Bool
(SearchResult v a -> SearchResult v a -> Bool)
-> (SearchResult v a -> SearchResult v a -> Bool)
-> Eq (SearchResult v a)
forall a. (a -> a -> Bool) -> (a -> a -> Bool) -> Eq a
forall v a. Eq a => SearchResult v a -> SearchResult v a -> Bool
/= :: SearchResult v a -> SearchResult v a -> Bool
$c/= :: forall v a. Eq a => SearchResult v a -> SearchResult v a -> Bool
== :: SearchResult v a -> SearchResult v a -> Bool
$c== :: forall v a. Eq a => SearchResult v a -> SearchResult v a -> Bool
Eq, Eq (SearchResult v a)
Eq (SearchResult v a)
-> (SearchResult v a -> SearchResult v a -> Ordering)
-> (SearchResult v a -> SearchResult v a -> Bool)
-> (SearchResult v a -> SearchResult v a -> Bool)
-> (SearchResult v a -> SearchResult v a -> Bool)
-> (SearchResult v a -> SearchResult v a -> Bool)
-> (SearchResult v a -> SearchResult v a -> SearchResult v a)
-> (SearchResult v a -> SearchResult v a -> SearchResult v a)
-> Ord (SearchResult v a)
SearchResult v a -> SearchResult v a -> Bool
SearchResult v a -> SearchResult v a -> Ordering
SearchResult v a -> SearchResult v a -> SearchResult v a
forall a.
Eq a
-> (a -> a -> Ordering)
-> (a -> a -> Bool)
-> (a -> a -> Bool)
-> (a -> a -> Bool)
-> (a -> a -> Bool)
-> (a -> a -> a)
-> (a -> a -> a)
-> Ord a
forall v a. Ord a => Eq (SearchResult v a)
forall v a. Ord a => SearchResult v a -> SearchResult v a -> Bool
forall v a.
Ord a =>
SearchResult v a -> SearchResult v a -> Ordering
forall v a.
Ord a =>
SearchResult v a -> SearchResult v a -> SearchResult v a
min :: SearchResult v a -> SearchResult v a -> SearchResult v a
$cmin :: forall v a.
Ord a =>
SearchResult v a -> SearchResult v a -> SearchResult v a
max :: SearchResult v a -> SearchResult v a -> SearchResult v a
$cmax :: forall v a.
Ord a =>
SearchResult v a -> SearchResult v a -> SearchResult v a
>= :: SearchResult v a -> SearchResult v a -> Bool
$c>= :: forall v a. Ord a => SearchResult v a -> SearchResult v a -> Bool
> :: SearchResult v a -> SearchResult v a -> Bool
$c> :: forall v a. Ord a => SearchResult v a -> SearchResult v a -> Bool
<= :: SearchResult v a -> SearchResult v a -> Bool
$c<= :: forall v a. Ord a => SearchResult v a -> SearchResult v a -> Bool
< :: SearchResult v a -> SearchResult v a -> Bool
$c< :: forall v a. Ord a => SearchResult v a -> SearchResult v a -> Bool
compare :: SearchResult v a -> SearchResult v a -> Ordering
$ccompare :: forall v a.
Ord a =>
SearchResult v a -> SearchResult v a -> Ordering
$cp1Ord :: forall v a. Ord a => Eq (SearchResult v a)
Ord, Int -> SearchResult v a -> ShowS
[SearchResult v a] -> ShowS
SearchResult v a -> String
(Int -> SearchResult v a -> ShowS)
-> (SearchResult v a -> String)
-> ([SearchResult v a] -> ShowS)
-> Show (SearchResult v a)
forall a.
(Int -> a -> ShowS) -> (a -> String) -> ([a] -> ShowS) -> Show a
forall v a. Show a => Int -> SearchResult v a -> ShowS
forall v a. Show a => [SearchResult v a] -> ShowS
forall v a. Show a => SearchResult v a -> String
showList :: [SearchResult v a] -> ShowS
$cshowList :: forall v a. Show a => [SearchResult v a] -> ShowS
show :: SearchResult v a -> String
$cshow :: forall v a. Show a => SearchResult v a -> String
showsPrec :: Int -> SearchResult v a -> ShowS
$cshowsPrec :: forall v a. Show a => Int -> SearchResult v a -> ShowS
Show, (forall x. SearchResult v a -> Rep (SearchResult v a) x)
-> (forall x. Rep (SearchResult v a) x -> SearchResult v a)
-> Generic (SearchResult v a)
forall x. Rep (SearchResult v a) x -> SearchResult v a
forall x. SearchResult v a -> Rep (SearchResult v a) x
forall a.
(forall x. a -> Rep a x) -> (forall x. Rep a x -> a) -> Generic a
forall v a x. Rep (SearchResult v a) x -> SearchResult v a
forall v a x. SearchResult v a -> Rep (SearchResult v a) x
$cto :: forall v a x. Rep (SearchResult v a) x -> SearchResult v a
$cfrom :: forall v a x. SearchResult v a -> Rep (SearchResult v a) x
Generic)
fromOriginalSearchResult :: FT.SearchResult v a -> SearchResult v a
fromOriginalSearchResult :: SearchResult v a -> SearchResult v a
fromOriginalSearchResult (FT.Position FingerTree v a
left a
a FingerTree v a
right) =
StrictFingerTree v a
-> a -> StrictFingerTree v a -> SearchResult v a
forall v a.
StrictFingerTree v a
-> a -> StrictFingerTree v a -> SearchResult v a
Position (FingerTree v a -> StrictFingerTree v a
forall v a. FingerTree v a -> StrictFingerTree v a
SFT FingerTree v a
left) a
a (FingerTree v a -> StrictFingerTree v a
forall v a. FingerTree v a -> StrictFingerTree v a
SFT FingerTree v a
right)
fromOriginalSearchResult SearchResult v a
FT.OnLeft = SearchResult v a
forall v a. SearchResult v a
OnLeft
fromOriginalSearchResult SearchResult v a
FT.OnRight = SearchResult v a
forall v a. SearchResult v a
OnRight
fromOriginalSearchResult SearchResult v a
FT.Nowhere = SearchResult v a
forall v a. SearchResult v a
Nowhere
search ::
(Measured v a) =>
(v -> v -> Bool) ->
StrictFingerTree v a ->
SearchResult v a
search :: (v -> v -> Bool) -> StrictFingerTree v a -> SearchResult v a
search v -> v -> Bool
p (SFT FingerTree v a
ft) = SearchResult v a -> SearchResult v a
forall v a. SearchResult v a -> SearchResult v a
fromOriginalSearchResult ((v -> v -> Bool) -> FingerTree v a -> SearchResult v a
forall v a.
Measured v a =>
(v -> v -> Bool) -> FingerTree v a -> SearchResult v a
FT.search v -> v -> Bool
p FingerTree v a
ft)
split ::
(Measured v a) =>
(v -> Bool) ->
StrictFingerTree v a ->
(StrictFingerTree v a, StrictFingerTree v a)
split :: (v -> Bool)
-> StrictFingerTree v a
-> (StrictFingerTree v a, StrictFingerTree v a)
split v -> Bool
p (SFT FingerTree v a
ft) = (FingerTree v a -> StrictFingerTree v a
forall v a. FingerTree v a -> StrictFingerTree v a
SFT FingerTree v a
left, FingerTree v a -> StrictFingerTree v a
forall v a. FingerTree v a -> StrictFingerTree v a
SFT FingerTree v a
right)
where
(FingerTree v a
left, FingerTree v a
right) = (v -> Bool) -> FingerTree v a -> (FingerTree v a, FingerTree v a)
forall v a.
Measured v a =>
(v -> Bool) -> FingerTree v a -> (FingerTree v a, FingerTree v a)
FT.split v -> Bool
p FingerTree v a
ft
takeUntil ::
(Measured v a) =>
(v -> Bool) ->
StrictFingerTree v a ->
StrictFingerTree v a
takeUntil :: (v -> Bool) -> StrictFingerTree v a -> StrictFingerTree v a
takeUntil v -> Bool
p (SFT FingerTree v a
ft) = FingerTree v a -> StrictFingerTree v a
forall v a. FingerTree v a -> StrictFingerTree v a
SFT ((v -> Bool) -> FingerTree v a -> FingerTree v a
forall v a.
Measured v a =>
(v -> Bool) -> FingerTree v a -> FingerTree v a
FT.takeUntil v -> Bool
p FingerTree v a
ft)
dropUntil ::
(Measured v a) =>
(v -> Bool) ->
StrictFingerTree v a ->
StrictFingerTree v a
dropUntil :: (v -> Bool) -> StrictFingerTree v a -> StrictFingerTree v a
dropUntil v -> Bool
p (SFT FingerTree v a
ft) = FingerTree v a -> StrictFingerTree v a
forall v a. FingerTree v a -> StrictFingerTree v a
SFT ((v -> Bool) -> FingerTree v a -> FingerTree v a
forall v a.
Measured v a =>
(v -> Bool) -> FingerTree v a -> FingerTree v a
FT.dropUntil v -> Bool
p FingerTree v a
ft)
reverse :: (Measured v a) => StrictFingerTree v a -> StrictFingerTree v a
reverse :: StrictFingerTree v a -> StrictFingerTree v a
reverse (SFT FingerTree v a
ft) = FingerTree v a -> StrictFingerTree v a
forall v a. FingerTree v a -> StrictFingerTree v a
SFT (FingerTree v a -> FingerTree v a
forall v a. Measured v a => FingerTree v a -> FingerTree v a
FT.reverse FingerTree v a
ft)
fmap' ::
(Measured v1 a1, Measured v2 a2) =>
(a1 -> a2) ->
StrictFingerTree v1 a1 ->
StrictFingerTree v2 a2
fmap' :: (a1 -> a2) -> StrictFingerTree v1 a1 -> StrictFingerTree v2 a2
fmap' a1 -> a2
f (SFT FingerTree v1 a1
ft) = FingerTree v2 a2 -> StrictFingerTree v2 a2
forall v a. FingerTree v a -> StrictFingerTree v a
forceToStrict ((a1 -> a2) -> FingerTree v1 a1 -> FingerTree v2 a2
forall v1 a1 v2 a2.
(Measured v1 a1, Measured v2 a2) =>
(a1 -> a2) -> FingerTree v1 a1 -> FingerTree v2 a2
FT.fmap' a1 -> a2
f FingerTree v1 a1
ft)
unsafeFmap :: (a -> b) -> StrictFingerTree v a -> StrictFingerTree v b
unsafeFmap :: (a -> b) -> StrictFingerTree v a -> StrictFingerTree v b
unsafeFmap a -> b
f (SFT FingerTree v a
ft) = FingerTree v b -> StrictFingerTree v b
forall v a. FingerTree v a -> StrictFingerTree v a
forceToStrict ((a -> b) -> FingerTree v a -> FingerTree v b
forall a b v. (a -> b) -> FingerTree v a -> FingerTree v b
FT.unsafeFmap a -> b
f FingerTree v a
ft)