{-# OPTIONS_GHC -Wall -fwarn-tabs #-}
{-# OPTIONS_HADDOCK not-home #-}
{-# LANGUAGE NoImplicitPrelude, CPP, BangPatterns #-}
#if __GLASGOW_HASKELL__ >= 708
{-# LANGUAGE TypeFamilies #-}
#endif
#if __GLASGOW_HASKELL__ >= 701
{-# LANGUAGE Trustworthy #-}
#endif
module Data.Trie.Internal
(
Trie()
, empty, null, singleton, size
, fromList
, toList, toListBy, elems
, lookupBy_, submap
, match_, matches_
, alterBy, alterBy_, adjust
, wip_unionWith
, mergeBy, intersectBy
, minAssoc, maxAssoc
, updateMinViewBy, updateMaxViewBy
, filter
, filterMap
, mapBy
, filterA
, wither
, contextualMap
, contextualMap'
, contextualFilterMap
, contextualMapBy
, foldrWithKey, foldrWithKey', foldlWithKey, foldlWithKey'
, cata_, cata
, traverseWithKey
, showTrie
, breakMaximalPrefix
) where
import Prelude hiding (null, lookup, filter)
import qualified Data.ByteString as S
import qualified Data.ByteString.Unsafe as SU
import Data.Trie.Internal.ByteString
import Data.Trie.Internal.BitTwiddle
import Data.Trie.Internal.Errors (impossible)
import Data.Binary (Binary(..), Get, Word8)
import Data.Bits (xor)
#if MIN_VERSION_base(4,9,0)
import Data.Semigroup (Semigroup(..))
#endif
#if !(MIN_VERSION_base(4,8,0))
import Data.Monoid (Monoid(..))
#endif
import Control.DeepSeq (NFData(rnf))
import Control.Monad (liftM3, liftM4)
import qualified Data.Foldable as F
import Control.Applicative (liftA2)
#if MIN_VERSION_base(4,8,0)
#else
import Control.Applicative (Applicative(..), (<$>))
import Data.Foldable (Foldable())
import Data.Traversable (Traversable(traverse))
#endif
#if MIN_VERSION_base(4,9,0)
import Data.Functor.Classes (Eq1(..), Ord1(..), Show1(..), Read1(..))
import qualified Data.Functor.Classes as FC
#endif
#ifdef __GLASGOW_HASKELL__
import qualified Text.Read as R
import GHC.Exts (build)
#endif
#if __GLASGOW_HASKELL__ >= 708
import qualified GHC.Exts (IsList(..))
#endif
infix 4 $$
($$) :: (a -> b -> c) -> (a, b) -> c
$$ :: (a -> b -> c) -> (a, b) -> c
($$) = (a -> b -> c) -> (a, b) -> c
forall a b c. (a -> b -> c) -> (a, b) -> c
uncurry
{-# INLINE ($$) #-}
data Trie a
= Branch {-# UNPACK #-} !Prefix
{-# UNPACK #-} !Mask
!(Trie a)
!(Trie a)
| Arc {-# UNPACK #-} !ByteString
!(Maybe a)
!(Trie a)
| Empty
deriving Trie a -> Trie a -> Bool
(Trie a -> Trie a -> Bool)
-> (Trie a -> Trie a -> Bool) -> Eq (Trie a)
forall a. Eq a => Trie a -> Trie a -> Bool
forall a. (a -> a -> Bool) -> (a -> a -> Bool) -> Eq a
/= :: Trie a -> Trie a -> Bool
$c/= :: forall a. Eq a => Trie a -> Trie a -> Bool
== :: Trie a -> Trie a -> Bool
$c== :: forall a. Eq a => Trie a -> Trie a -> Bool
Eq
arc :: ByteString -> Maybe a -> Trie a -> Trie a
{-# INLINE arc #-}
arc :: ByteString -> Maybe a -> Trie a -> Trie a
arc !ByteString
k mv :: Maybe a
mv@(Just a
_) = ByteString -> Maybe a -> Trie a -> Trie a
forall a. ByteString -> Maybe a -> Trie a -> Trie a
Arc ByteString
k Maybe a
mv
arc ByteString
k Maybe a
Nothing = ByteString -> Trie a -> Trie a
forall a. ByteString -> Trie a -> Trie a
prepend ByteString
k
arcNN :: ByteString -> Maybe a -> Trie a -> Trie a
{-# INLINE arcNN #-}
arcNN :: ByteString -> Maybe a -> Trie a -> Trie a
arcNN !ByteString
k mv :: Maybe a
mv@(Just a
_) = ByteString -> Maybe a -> Trie a -> Trie a
forall a. ByteString -> Maybe a -> Trie a -> Trie a
Arc ByteString
k Maybe a
mv
arcNN ByteString
k Maybe a
Nothing = ByteString -> Trie a -> Trie a
forall a. ByteString -> Trie a -> Trie a
prependNN ByteString
k
prepend :: ByteString -> Trie a -> Trie a
{-# INLINE prepend #-}
prepend :: ByteString -> Trie a -> Trie a
prepend ByteString
k
| ByteString -> Bool
S.null ByteString
k = Trie a -> Trie a
forall a. a -> a
id
| Bool
otherwise = ByteString -> Trie a -> Trie a
forall a. ByteString -> Trie a -> Trie a
prependNN ByteString
k
prependNN :: ByteString -> Trie a -> Trie a
{-# INLINE prependNN #-}
prependNN :: ByteString -> Trie a -> Trie a
prependNN !ByteString
_ t :: Trie a
t@Trie a
Empty = Trie a
t
prependNN ByteString
q t :: Trie a
t@(Branch{}) = ByteString -> Maybe a -> Trie a -> Trie a
forall a. ByteString -> Maybe a -> Trie a -> Trie a
Arc ByteString
q Maybe a
forall a. Maybe a
Nothing Trie a
t
prependNN ByteString
q (Arc ByteString
k Maybe a
mv Trie a
s) = ByteString -> Maybe a -> Trie a -> Trie a
forall a. ByteString -> Maybe a -> Trie a -> Trie a
Arc (ByteString -> ByteString -> ByteString
S.append ByteString
q ByteString
k) Maybe a
mv Trie a
s
mayEpsilon :: Maybe a -> Trie a -> Trie a
{-# INLINE mayEpsilon #-}
mayEpsilon :: Maybe a -> Trie a -> Trie a
mayEpsilon (Just a
v) = a -> Trie a -> Trie a
forall a. a -> Trie a -> Trie a
epsilon a
v
mayEpsilon Maybe a
Nothing = Trie a -> Trie a
forall a. a -> a
id
epsilon :: a -> Trie a -> Trie a
{-# INLINE epsilon #-}
epsilon :: a -> Trie a -> Trie a
epsilon = ByteString -> Maybe a -> Trie a -> Trie a
forall a. ByteString -> Maybe a -> Trie a -> Trie a
Arc ByteString
S.empty (Maybe a -> Trie a -> Trie a)
-> (a -> Maybe a) -> a -> Trie a -> Trie a
forall b c a. (b -> c) -> (a -> b) -> a -> c
. a -> Maybe a
forall a. a -> Maybe a
Just
branch :: Prefix -> Mask -> Trie a -> Trie a -> Trie a
{-# INLINE branch #-}
branch :: Prefix -> Prefix -> Trie a -> Trie a -> Trie a
branch !Prefix
_ !Prefix
_ Trie a
Empty Trie a
r = Trie a
r
branch Prefix
_ Prefix
_ Trie a
l Trie a
Empty = Trie a
l
branch Prefix
p Prefix
m Trie a
l Trie a
r = Prefix -> Prefix -> Trie a -> Trie a -> Trie a
forall a. Prefix -> Prefix -> Trie a -> Trie a -> Trie a
Branch Prefix
p Prefix
m Trie a
l Trie a
r
branchL :: Prefix -> Mask -> Trie a -> Trie a -> Trie a
{-# INLINE branchL #-}
branchL :: Prefix -> Prefix -> Trie a -> Trie a -> Trie a
branchL !Prefix
_ !Prefix
_ Trie a
Empty Trie a
r = Trie a
r
branchL Prefix
p Prefix
m Trie a
l Trie a
r = Prefix -> Prefix -> Trie a -> Trie a -> Trie a
forall a. Prefix -> Prefix -> Trie a -> Trie a -> Trie a
Branch Prefix
p Prefix
m Trie a
l Trie a
r
branchR :: Prefix -> Mask -> Trie a -> Trie a -> Trie a
{-# INLINE branchR #-}
branchR :: Prefix -> Prefix -> Trie a -> Trie a -> Trie a
branchR !Prefix
_ !Prefix
_ Trie a
l Trie a
Empty = Trie a
l
branchR Prefix
p Prefix
m Trie a
l Trie a
r = Prefix -> Prefix -> Trie a -> Trie a -> Trie a
forall a. Prefix -> Prefix -> Trie a -> Trie a -> Trie a
Branch Prefix
p Prefix
m Trie a
l Trie a
r
graft :: Prefix -> Trie a -> Prefix -> Trie a -> Trie a
{-# INLINE graft #-}
graft :: Prefix -> Trie a -> Prefix -> Trie a -> Trie a
graft Prefix
p0 Trie a
t0 Prefix
p1 Trie a
t1
| Prefix -> Prefix -> Bool
zero Prefix
p0 Prefix
m = Prefix -> Prefix -> Trie a -> Trie a -> Trie a
forall a. Prefix -> Prefix -> Trie a -> Trie a -> Trie a
Branch Prefix
p Prefix
m Trie a
t0 Trie a
t1
| Bool
otherwise = Prefix -> Prefix -> Trie a -> Trie a -> Trie a
forall a. Prefix -> Prefix -> Trie a -> Trie a -> Trie a
Branch Prefix
p Prefix
m Trie a
t1 Trie a
t0
where
m :: Prefix
m = Prefix -> Prefix -> Prefix
getMask Prefix
p0 Prefix
p1
p :: Prefix
p = Prefix -> Prefix -> Prefix
applyMask Prefix
p0 Prefix
m
wye :: ByteString
-> ByteString -> Trie a
-> ByteString -> Trie a
-> Trie a
wye :: ByteString
-> ByteString -> Trie a -> ByteString -> Trie a -> Trie a
wye ByteString
p ByteString
k0 Trie a
t0 ByteString
k1 Trie a
t1 =
ByteString -> Maybe a -> Trie a -> Trie a
forall a. ByteString -> Maybe a -> Trie a -> Trie a
Arc ByteString
p Maybe a
forall a. Maybe a
Nothing (Trie a -> Trie a) -> Trie a -> Trie a
forall a b. (a -> b) -> a -> b
$ Prefix -> Trie a -> Prefix -> Trie a -> Trie a
forall a. Prefix -> Trie a -> Prefix -> Trie a -> Trie a
graft (ByteString -> Prefix
arcPrefix ByteString
k0) Trie a
t0 (ByteString -> Prefix
arcPrefix ByteString
k1) Trie a
t1
arcMerge
:: ByteString -> Trie a
-> ByteString -> Trie a
-> (ByteString -> ByteString -> ByteString -> Trie a)
-> Trie a
{-# INLINE arcMerge #-}
arcMerge :: ByteString
-> Trie a
-> ByteString
-> Trie a
-> (ByteString -> ByteString -> ByteString -> Trie a)
-> Trie a
arcMerge ByteString
k0 Trie a
t0 ByteString
k1 Trie a
t1 ByteString -> ByteString -> ByteString -> Trie a
whenMatch
| Prefix
m Prefix -> Prefix -> Bool
forall a. Eq a => a -> a -> Bool
== Prefix
0 =
case ByteString -> ByteString -> (ByteString, ByteString, ByteString)
breakMaximalPrefix ByteString
k0 ByteString
k1 of
(ByteString
pre, ByteString
k0', ByteString
k1')
| ByteString -> Bool
S.null ByteString
pre -> String -> Trie a
forall a. String -> a
impossible String
"arcMerge"
| Bool
otherwise -> ByteString -> ByteString -> ByteString -> Trie a
whenMatch ByteString
pre ByteString
k0' ByteString
k1'
| Prefix -> Prefix -> Bool
zero Prefix
p0 Prefix
m = Prefix -> Prefix -> Trie a -> Trie a -> Trie a
forall a. Prefix -> Prefix -> Trie a -> Trie a -> Trie a
Branch Prefix
p Prefix
m Trie a
t0 Trie a
t1
| Bool
otherwise = Prefix -> Prefix -> Trie a -> Trie a -> Trie a
forall a. Prefix -> Prefix -> Trie a -> Trie a -> Trie a
Branch Prefix
p Prefix
m Trie a
t1 Trie a
t0
where
p0 :: Prefix
p0 = ByteString -> Prefix
arcPrefix ByteString
k0
p1 :: Prefix
p1 = ByteString -> Prefix
arcPrefix ByteString
k1
m :: Prefix
m = Prefix -> Prefix -> Prefix
getMask Prefix
p0 Prefix
p1
p :: Prefix
p = Prefix -> Prefix -> Prefix
applyMask Prefix
p0 Prefix
m
arcPrefix :: ByteString -> Prefix
{-# INLINE arcPrefix #-}
arcPrefix :: ByteString -> Prefix
arcPrefix ByteString
k
| ByteString -> Bool
S.null ByteString
k = Prefix
0
| Bool
otherwise = ByteString -> Prefix
SU.unsafeHead ByteString
k
errorLogHead :: String -> ByteString -> ByteStringElem
{-# NOINLINE errorLogHead #-}
errorLogHead :: String -> ByteString -> Prefix
errorLogHead String
fn ByteString
q
| ByteString -> Bool
S.null ByteString
q = String -> Prefix
forall a. HasCallStack => String -> a
error (String -> Prefix) -> String -> Prefix
forall a b. (a -> b) -> a -> b
$ String
"Data.Trie.Internal." String -> String -> String
forall a. [a] -> [a] -> [a]
++ String
fn String -> String -> String
forall a. [a] -> [a] -> [a]
++String
": found null subquery"
| Bool
otherwise = ByteString -> Prefix
SU.unsafeHead ByteString
q
#if MIN_VERSION_base(4,9,0)
instance Eq1 Trie where
liftEq :: (a -> b -> Bool) -> Trie a -> Trie b -> Bool
liftEq = (a -> b -> Bool) -> Trie a -> Trie b -> Bool
forall a b. (a -> b -> Bool) -> Trie a -> Trie b -> Bool
equal1
equal1 :: (a -> b -> Bool) -> Trie a -> Trie b -> Bool
equal1 :: (a -> b -> Bool) -> Trie a -> Trie b -> Bool
equal1 a -> b -> Bool
eq (Branch Prefix
p0 Prefix
m0 Trie a
l0 Trie a
r0) (Branch Prefix
p1 Prefix
m1 Trie b
l1 Trie b
r1) =
Prefix
m0 Prefix -> Prefix -> Bool
forall a. Eq a => a -> a -> Bool
== Prefix
m1 Bool -> Bool -> Bool
&& Prefix
p0 Prefix -> Prefix -> Bool
forall a. Eq a => a -> a -> Bool
== Prefix
p1 Bool -> Bool -> Bool
&& (a -> b -> Bool) -> Trie a -> Trie b -> Bool
forall a b. (a -> b -> Bool) -> Trie a -> Trie b -> Bool
equal1 a -> b -> Bool
eq Trie a
l0 Trie b
l1 Bool -> Bool -> Bool
&& (a -> b -> Bool) -> Trie a -> Trie b -> Bool
forall a b. (a -> b -> Bool) -> Trie a -> Trie b -> Bool
equal1 a -> b -> Bool
eq Trie a
r0 Trie b
r1
equal1 a -> b -> Bool
eq (Arc ByteString
k0 Maybe a
mv0 Trie a
t0) (Arc ByteString
k1 Maybe b
mv1 Trie b
t1) =
ByteString
k0 ByteString -> ByteString -> Bool
forall a. Eq a => a -> a -> Bool
== ByteString
k1 Bool -> Bool -> Bool
&& (a -> b -> Bool) -> Maybe a -> Maybe b -> Bool
forall (f :: * -> *) a b.
Eq1 f =>
(a -> b -> Bool) -> f a -> f b -> Bool
liftEq a -> b -> Bool
eq Maybe a
mv0 Maybe b
mv1 Bool -> Bool -> Bool
&& (a -> b -> Bool) -> Trie a -> Trie b -> Bool
forall a b. (a -> b -> Bool) -> Trie a -> Trie b -> Bool
equal1 a -> b -> Bool
eq Trie a
t0 Trie b
t1
equal1 a -> b -> Bool
_ Trie a
Empty Trie b
Empty = Bool
True
equal1 a -> b -> Bool
_ Trie a
_ Trie b
_ = Bool
False
#endif
instance Ord a => Ord (Trie a) where
compare :: Trie a -> Trie a -> Ordering
compare Trie a
t0 Trie a
t1 = [(ByteString, a)] -> [(ByteString, a)] -> Ordering
forall a. Ord a => a -> a -> Ordering
compare (Trie a -> [(ByteString, a)]
forall a. Trie a -> [(ByteString, a)]
toList Trie a
t0) (Trie a -> [(ByteString, a)]
forall a. Trie a -> [(ByteString, a)]
toList Trie a
t1)
#if MIN_VERSION_base(4,9,0)
instance Ord1 Trie where
liftCompare :: (a -> b -> Ordering) -> Trie a -> Trie b -> Ordering
liftCompare a -> b -> Ordering
cmp Trie a
t0 Trie b
t1 =
((ByteString, a) -> (ByteString, b) -> Ordering)
-> [(ByteString, a)] -> [(ByteString, b)] -> Ordering
forall (f :: * -> *) a b.
Ord1 f =>
(a -> b -> Ordering) -> f a -> f b -> Ordering
liftCompare ((a -> b -> Ordering)
-> (ByteString, a) -> (ByteString, b) -> Ordering
forall (f :: * -> *) a b.
Ord1 f =>
(a -> b -> Ordering) -> f a -> f b -> Ordering
liftCompare a -> b -> Ordering
cmp) (Trie a -> [(ByteString, a)]
forall a. Trie a -> [(ByteString, a)]
toList Trie a
t0) (Trie b -> [(ByteString, b)]
forall a. Trie a -> [(ByteString, a)]
toList Trie b
t1)
#endif
instance (Show a) => Show (Trie a) where
showsPrec :: Int -> Trie a -> String -> String
showsPrec Int
p Trie a
t = Bool -> (String -> String) -> String -> String
showParen (Int
p Int -> Int -> Bool
forall a. Ord a => a -> a -> Bool
> Int
10)
((String -> String) -> String -> String)
-> (String -> String) -> String -> String
forall a b. (a -> b) -> a -> b
$ (String
"fromList " String -> String -> String
forall a. [a] -> [a] -> [a]
++) (String -> String) -> (String -> String) -> String -> String
forall b c a. (b -> c) -> (a -> b) -> a -> c
. [(ByteString, a)] -> String -> String
forall a. Show a => a -> String -> String
shows (Trie a -> [(ByteString, a)]
forall a. Trie a -> [(ByteString, a)]
toList Trie a
t)
#if MIN_VERSION_base(4,9,0)
instance Show1 Trie where
liftShowsPrec :: (Int -> a -> String -> String)
-> ([a] -> String -> String) -> Int -> Trie a -> String -> String
liftShowsPrec Int -> a -> String -> String
sp [a] -> String -> String
sl Int
p Trie a
t =
(Int -> [(ByteString, a)] -> String -> String)
-> String -> Int -> [(ByteString, a)] -> String -> String
forall a.
(Int -> a -> String -> String)
-> String -> Int -> a -> String -> String
FC.showsUnaryWith ((Int -> (ByteString, a) -> String -> String)
-> ([(ByteString, a)] -> String -> String)
-> Int
-> [(ByteString, a)]
-> String
-> String
forall (f :: * -> *) a.
Show1 f =>
(Int -> a -> String -> String)
-> ([a] -> String -> String) -> Int -> f a -> String -> String
liftShowsPrec Int -> (ByteString, a) -> String -> String
sp' [(ByteString, a)] -> String -> String
sl') String
"fromList" Int
p (Trie a -> [(ByteString, a)]
forall a. Trie a -> [(ByteString, a)]
toList Trie a
t)
where
sp' :: Int -> (ByteString, a) -> String -> String
sp' = (Int -> a -> String -> String)
-> ([a] -> String -> String)
-> Int
-> (ByteString, a)
-> String
-> String
forall (f :: * -> *) a.
Show1 f =>
(Int -> a -> String -> String)
-> ([a] -> String -> String) -> Int -> f a -> String -> String
liftShowsPrec Int -> a -> String -> String
sp [a] -> String -> String
sl
sl' :: [(ByteString, a)] -> String -> String
sl' = (Int -> a -> String -> String)
-> ([a] -> String -> String)
-> [(ByteString, a)]
-> String
-> String
forall (f :: * -> *) a.
Show1 f =>
(Int -> a -> String -> String)
-> ([a] -> String -> String) -> [f a] -> String -> String
liftShowList Int -> a -> String -> String
sp [a] -> String -> String
sl
#endif
showTrie :: (Show a) => Trie a -> String
showTrie :: Trie a -> String
showTrie Trie a
t = (String -> String) -> Trie a -> String -> String
forall a.
Show a =>
(String -> String) -> Trie a -> String -> String
shows' String -> String
forall a. a -> a
id Trie a
t String
""
where
spaces :: (String -> [b]) -> String
spaces String -> [b]
f = (b -> Char) -> [b] -> String
forall a b. (a -> b) -> [a] -> [b]
map (Char -> b -> Char
forall a b. a -> b -> a
const Char
' ') (String -> [b]
f String
"")
shows' :: (String -> String) -> Trie a -> String -> String
shows' String -> String
_ Trie a
Empty = (String
".\n"String -> String -> String
forall a. [a] -> [a] -> [a]
++)
shows' String -> String
ss (Branch Prefix
p Prefix
m Trie a
l Trie a
r) =
let s' :: String -> String
s' = (String
"--"String -> String -> String
forall a. [a] -> [a] -> [a]
++) (String -> String) -> (String -> String) -> String -> String
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Prefix -> String -> String
forall a. Show a => a -> String -> String
shows Prefix
p (String -> String) -> (String -> String) -> String -> String
forall b c a. (b -> c) -> (a -> b) -> a -> c
. (String
","String -> String -> String
forall a. [a] -> [a] -> [a]
++) (String -> String) -> (String -> String) -> String -> String
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Prefix -> String -> String
forall a. Show a => a -> String -> String
shows Prefix
m (String -> String) -> (String -> String) -> String -> String
forall b c a. (b -> c) -> (a -> b) -> a -> c
. (String
"-+"String -> String -> String
forall a. [a] -> [a] -> [a]
++)
ss' :: String -> String
ss' = String -> String
ss (String -> String) -> (String -> String) -> String -> String
forall b c a. (b -> c) -> (a -> b) -> a -> c
. (String -> String
forall a. [a] -> [a]
tail ((String -> String) -> String
forall b. (String -> [b]) -> String
spaces String -> String
s') String -> String -> String
forall a. [a] -> [a] -> [a]
++)
in String -> String
s' (String -> String) -> (String -> String) -> String -> String
forall b c a. (b -> c) -> (a -> b) -> a -> c
. (String -> String) -> Trie a -> String -> String
shows' (String -> String
ss' (String -> String) -> (String -> String) -> String -> String
forall b c a. (b -> c) -> (a -> b) -> a -> c
. (String
"|"String -> String -> String
forall a. [a] -> [a] -> [a]
++)) Trie a
l
(String -> String) -> (String -> String) -> String -> String
forall b c a. (b -> c) -> (a -> b) -> a -> c
. String -> String
ss' (String -> String) -> (String -> String) -> String -> String
forall b c a. (b -> c) -> (a -> b) -> a -> c
. (String
"|\n"String -> String -> String
forall a. [a] -> [a] -> [a]
++)
(String -> String) -> (String -> String) -> String -> String
forall b c a. (b -> c) -> (a -> b) -> a -> c
. String -> String
ss' (String -> String) -> (String -> String) -> String -> String
forall b c a. (b -> c) -> (a -> b) -> a -> c
. (String
"`"String -> String -> String
forall a. [a] -> [a] -> [a]
++) (String -> String) -> (String -> String) -> String -> String
forall b c a. (b -> c) -> (a -> b) -> a -> c
. (String -> String) -> Trie a -> String -> String
shows' (String -> String
ss' (String -> String) -> (String -> String) -> String -> String
forall b c a. (b -> c) -> (a -> b) -> a -> c
. (String
" "String -> String -> String
forall a. [a] -> [a] -> [a]
++)) Trie a
r
shows' String -> String
ss (Arc ByteString
k Maybe a
mv Trie a
t') =
let s' :: String -> String
s' = (String
"--"String -> String -> String
forall a. [a] -> [a] -> [a]
++) (String -> String) -> (String -> String) -> String -> String
forall b c a. (b -> c) -> (a -> b) -> a -> c
. ByteString -> String -> String
forall a. Show a => a -> String -> String
shows ByteString
k
(String -> String) -> (String -> String) -> String -> String
forall b c a. (b -> c) -> (a -> b) -> a -> c
. (String -> String)
-> (a -> String -> String) -> Maybe a -> String -> String
forall b a. b -> (a -> b) -> Maybe a -> b
maybe String -> String
forall a. a -> a
id (\a
v -> (String
"-("String -> String -> String
forall a. [a] -> [a] -> [a]
++) (String -> String) -> (String -> String) -> String -> String
forall b c a. (b -> c) -> (a -> b) -> a -> c
. a -> String -> String
forall a. Show a => a -> String -> String
shows a
v (String -> String) -> (String -> String) -> String -> String
forall b c a. (b -> c) -> (a -> b) -> a -> c
. (String
")"String -> String -> String
forall a. [a] -> [a] -> [a]
++)) Maybe a
mv
(String -> String) -> (String -> String) -> String -> String
forall b c a. (b -> c) -> (a -> b) -> a -> c
. (String
"--"String -> String -> String
forall a. [a] -> [a] -> [a]
++)
in String -> String
s' (String -> String) -> (String -> String) -> String -> String
forall b c a. (b -> c) -> (a -> b) -> a -> c
. (String -> String) -> Trie a -> String -> String
shows' (String -> String
ss (String -> String) -> (String -> String) -> String -> String
forall b c a. (b -> c) -> (a -> b) -> a -> c
. ((String -> String) -> String
forall b. (String -> [b]) -> String
spaces String -> String
s' String -> String -> String
forall a. [a] -> [a] -> [a]
++)) Trie a
t'
instance (Read a) => Read (Trie a) where
#ifdef __GLASGOW_HASKELL__
readPrec :: ReadPrec (Trie a)
readPrec = ReadPrec (Trie a) -> ReadPrec (Trie a)
forall a. ReadPrec a -> ReadPrec a
R.parens (ReadPrec (Trie a) -> ReadPrec (Trie a))
-> (ReadPrec (Trie a) -> ReadPrec (Trie a))
-> ReadPrec (Trie a)
-> ReadPrec (Trie a)
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Int -> ReadPrec (Trie a) -> ReadPrec (Trie a)
forall a. Int -> ReadPrec a -> ReadPrec a
R.prec Int
10 (ReadPrec (Trie a) -> ReadPrec (Trie a))
-> ReadPrec (Trie a) -> ReadPrec (Trie a)
forall a b. (a -> b) -> a -> b
$ do
R.Ident String
"fromList" <- ReadPrec Lexeme
R.lexP
[(ByteString, a)] -> Trie a
forall a. [(ByteString, a)] -> Trie a
fromList ([(ByteString, a)] -> Trie a)
-> ReadPrec [(ByteString, a)] -> ReadPrec (Trie a)
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> ReadPrec [(ByteString, a)]
forall a. Read a => ReadPrec a
R.readPrec
readListPrec :: ReadPrec [Trie a]
readListPrec = ReadPrec [Trie a]
forall a. Read a => ReadPrec [a]
R.readListPrecDefault
#else
readsPrec p = readParen (p > 10) $ \ r0 -> do
("fromList", r1) <- lex r0
(xs, r2) <- reads r1
return (fromList xs, r2)
#endif
#if MIN_VERSION_base(4,9,0)
instance Read1 Trie where
liftReadsPrec :: (Int -> ReadS a) -> ReadS [a] -> Int -> ReadS (Trie a)
liftReadsPrec Int -> ReadS a
rp ReadS [a]
rl =
(String -> ReadS (Trie a)) -> Int -> ReadS (Trie a)
forall a. (String -> ReadS a) -> Int -> ReadS a
FC.readsData ((String -> ReadS (Trie a)) -> Int -> ReadS (Trie a))
-> (String -> ReadS (Trie a)) -> Int -> ReadS (Trie a)
forall a b. (a -> b) -> a -> b
$ (Int -> ReadS [(ByteString, a)])
-> String
-> ([(ByteString, a)] -> Trie a)
-> String
-> ReadS (Trie a)
forall a t.
(Int -> ReadS a) -> String -> (a -> t) -> String -> ReadS t
FC.readsUnaryWith ((Int -> ReadS (ByteString, a))
-> ReadS [(ByteString, a)] -> Int -> ReadS [(ByteString, a)]
forall (f :: * -> *) a.
Read1 f =>
(Int -> ReadS a) -> ReadS [a] -> Int -> ReadS (f a)
liftReadsPrec Int -> ReadS (ByteString, a)
rp' ReadS [(ByteString, a)]
rl')
String
"fromList" [(ByteString, a)] -> Trie a
forall a. [(ByteString, a)] -> Trie a
fromList
where
rp' :: Int -> ReadS (ByteString, a)
rp' = (Int -> ReadS a) -> ReadS [a] -> Int -> ReadS (ByteString, a)
forall (f :: * -> *) a.
Read1 f =>
(Int -> ReadS a) -> ReadS [a] -> Int -> ReadS (f a)
liftReadsPrec Int -> ReadS a
rp ReadS [a]
rl
rl' :: ReadS [(ByteString, a)]
rl' = (Int -> ReadS a) -> ReadS [a] -> ReadS [(ByteString, a)]
forall (f :: * -> *) a.
Read1 f =>
(Int -> ReadS a) -> ReadS [a] -> ReadS [f a]
liftReadList Int -> ReadS a
rp ReadS [a]
rl
#endif
instance (Binary a) => Binary (Trie a) where
put :: Trie a -> Put
put Trie a
Empty = do Prefix -> Put
forall t. Binary t => t -> Put
put (Prefix
0 :: Word8)
put (Arc ByteString
k Maybe a
mv Trie a
t) = do Prefix -> Put
forall t. Binary t => t -> Put
put (Prefix
1 :: Word8); ByteString -> Put
forall t. Binary t => t -> Put
put ByteString
k; Maybe a -> Put
forall t. Binary t => t -> Put
put Maybe a
mv; Trie a -> Put
forall t. Binary t => t -> Put
put Trie a
t
put (Branch Prefix
p Prefix
m Trie a
l Trie a
r) = do Prefix -> Put
forall t. Binary t => t -> Put
put (Prefix
2 :: Word8); Prefix -> Put
forall t. Binary t => t -> Put
put Prefix
p; Prefix -> Put
forall t. Binary t => t -> Put
put Prefix
m; Trie a -> Put
forall t. Binary t => t -> Put
put Trie a
l; Trie a -> Put
forall t. Binary t => t -> Put
put Trie a
r
get :: Get (Trie a)
get = do Prefix
tag <- Get Prefix
forall t. Binary t => Get t
get :: Get Word8
case Prefix
tag of
Prefix
0 -> Trie a -> Get (Trie a)
forall (m :: * -> *) a. Monad m => a -> m a
return Trie a
forall a. Trie a
Empty
Prefix
1 -> (ByteString -> Maybe a -> Trie a -> Trie a)
-> Get ByteString -> Get (Maybe a) -> Get (Trie a) -> Get (Trie a)
forall (m :: * -> *) a1 a2 a3 r.
Monad m =>
(a1 -> a2 -> a3 -> r) -> m a1 -> m a2 -> m a3 -> m r
liftM3 ByteString -> Maybe a -> Trie a -> Trie a
forall a. ByteString -> Maybe a -> Trie a -> Trie a
Arc Get ByteString
forall t. Binary t => Get t
get Get (Maybe a)
forall t. Binary t => Get t
get Get (Trie a)
forall t. Binary t => Get t
get
Prefix
_ -> (Prefix -> Prefix -> Trie a -> Trie a -> Trie a)
-> Get Prefix
-> Get Prefix
-> Get (Trie a)
-> Get (Trie a)
-> Get (Trie a)
forall (m :: * -> *) a1 a2 a3 a4 r.
Monad m =>
(a1 -> a2 -> a3 -> a4 -> r) -> m a1 -> m a2 -> m a3 -> m a4 -> m r
liftM4 Prefix -> Prefix -> Trie a -> Trie a -> Trie a
forall a. Prefix -> Prefix -> Trie a -> Trie a -> Trie a
Branch Get Prefix
forall t. Binary t => Get t
get Get Prefix
forall t. Binary t => Get t
get Get (Trie a)
forall t. Binary t => Get t
get Get (Trie a)
forall t. Binary t => Get t
get
instance NFData a => NFData (Trie a) where
rnf :: Trie a -> ()
rnf Trie a
Empty = ()
rnf (Arc ByteString
_ Maybe a
mv Trie a
t) = Maybe a -> ()
forall a. NFData a => a -> ()
rnf Maybe a
mv () -> () -> ()
`seq` Trie a -> ()
forall a. NFData a => a -> ()
rnf Trie a
t
rnf (Branch Prefix
_ Prefix
_ Trie a
l Trie a
r) = Trie a -> ()
forall a. NFData a => a -> ()
rnf Trie a
l () -> () -> ()
`seq` Trie a -> ()
forall a. NFData a => a -> ()
rnf Trie a
r
instance Functor Trie where
fmap :: (a -> b) -> Trie a -> Trie b
fmap a -> b
f = Trie a -> Trie b
go
where
go :: Trie a -> Trie b
go Trie a
Empty = Trie b
forall a. Trie a
Empty
go (Arc ByteString
k Maybe a
Nothing Trie a
t) = ByteString -> Maybe b -> Trie b -> Trie b
forall a. ByteString -> Maybe a -> Trie a -> Trie a
Arc ByteString
k Maybe b
forall a. Maybe a
Nothing (Trie a -> Trie b
go Trie a
t)
go (Arc ByteString
k (Just a
v) Trie a
t) = ByteString -> Maybe b -> Trie b -> Trie b
forall a. ByteString -> Maybe a -> Trie a -> Trie a
Arc ByteString
k (b -> Maybe b
forall a. a -> Maybe a
Just (a -> b
f a
v)) (Trie a -> Trie b
go Trie a
t)
go (Branch Prefix
p Prefix
m Trie a
l Trie a
r) = Prefix -> Prefix -> Trie b -> Trie b -> Trie b
forall a. Prefix -> Prefix -> Trie a -> Trie a -> Trie a
Branch Prefix
p Prefix
m (Trie a -> Trie b
go Trie a
l) (Trie a -> Trie b
go Trie a
r)
#if __GLASGOW_HASKELL__
a
_ <$ :: a -> Trie b -> Trie a
<$ Trie b
Empty = Trie a
forall a. Trie a
Empty
a
v <$ (Arc ByteString
k Maybe b
Nothing Trie b
t) = ByteString -> Maybe a -> Trie a -> Trie a
forall a. ByteString -> Maybe a -> Trie a -> Trie a
Arc ByteString
k Maybe a
forall a. Maybe a
Nothing (a
v a -> Trie b -> Trie a
forall (f :: * -> *) a b. Functor f => a -> f b -> f a
<$ Trie b
t)
a
v <$ (Arc ByteString
k (Just b
_) Trie b
t) = ByteString -> Maybe a -> Trie a -> Trie a
forall a. ByteString -> Maybe a -> Trie a -> Trie a
Arc ByteString
k (a -> Maybe a
forall a. a -> Maybe a
Just a
v) (a
v a -> Trie b -> Trie a
forall (f :: * -> *) a b. Functor f => a -> f b -> f a
<$ Trie b
t)
a
v <$ (Branch Prefix
p Prefix
m Trie b
l Trie b
r) = Prefix -> Prefix -> Trie a -> Trie a -> Trie a
forall a. Prefix -> Prefix -> Trie a -> Trie a -> Trie a
Branch Prefix
p Prefix
m (a
v a -> Trie b -> Trie a
forall (f :: * -> *) a b. Functor f => a -> f b -> f a
<$ Trie b
l) (a
v a -> Trie b -> Trie a
forall (f :: * -> *) a b. Functor f => a -> f b -> f a
<$ Trie b
r)
#endif
instance Traversable Trie where
traverse :: (a -> f b) -> Trie a -> f (Trie b)
traverse a -> f b
f = Trie a -> f (Trie b)
go
where
go :: Trie a -> f (Trie b)
go Trie a
Empty = Trie b -> f (Trie b)
forall (f :: * -> *) a. Applicative f => a -> f a
pure Trie b
forall a. Trie a
Empty
go (Arc ByteString
k Maybe a
Nothing Trie a
t) = (Trie b -> Trie b) -> f (Trie b) -> f (Trie b)
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
fmap (ByteString -> Maybe b -> Trie b -> Trie b
forall a. ByteString -> Maybe a -> Trie a -> Trie a
Arc ByteString
k Maybe b
forall a. Maybe a
Nothing) (Trie a -> f (Trie b)
go Trie a
t)
go (Arc ByteString
k (Just a
v) Trie a
t) = (b -> Trie b -> Trie b) -> f b -> f (Trie b) -> f (Trie b)
forall (f :: * -> *) a b c.
Applicative f =>
(a -> b -> c) -> f a -> f b -> f c
liftA2 (ByteString -> Maybe b -> Trie b -> Trie b
forall a. ByteString -> Maybe a -> Trie a -> Trie a
Arc ByteString
k (Maybe b -> Trie b -> Trie b)
-> (b -> Maybe b) -> b -> Trie b -> Trie b
forall b c a. (b -> c) -> (a -> b) -> a -> c
. b -> Maybe b
forall a. a -> Maybe a
Just) (a -> f b
f a
v) (Trie a -> f (Trie b)
go Trie a
t)
go (Branch Prefix
p Prefix
m Trie a
l Trie a
r) = (Trie b -> Trie b -> Trie b)
-> f (Trie b) -> f (Trie b) -> f (Trie b)
forall (f :: * -> *) a b c.
Applicative f =>
(a -> b -> c) -> f a -> f b -> f c
liftA2 (Prefix -> Prefix -> Trie b -> Trie b -> Trie b
forall a. Prefix -> Prefix -> Trie a -> Trie a -> Trie a
Branch Prefix
p Prefix
m) (Trie a -> f (Trie b)
go Trie a
l) (Trie a -> f (Trie b)
go Trie a
r)
traverseWithKey
:: Applicative f => (ByteString -> a -> f b) -> Trie a -> f (Trie b)
{-# INLINE traverseWithKey #-}
traverseWithKey :: (ByteString -> a -> f b) -> Trie a -> f (Trie b)
traverseWithKey ByteString -> a -> f b
f = RevLazyByteString -> Trie a -> f (Trie b)
go RevLazyByteString
Nil
where
go :: RevLazyByteString -> Trie a -> f (Trie b)
go RevLazyByteString
_ Trie a
Empty = Trie b -> f (Trie b)
forall (f :: * -> *) a. Applicative f => a -> f a
pure Trie b
forall a. Trie a
Empty
go RevLazyByteString
q (Branch Prefix
p Prefix
m Trie a
l Trie a
r) = (Trie b -> Trie b -> Trie b)
-> f (Trie b) -> f (Trie b) -> f (Trie b)
forall (f :: * -> *) a b c.
Applicative f =>
(a -> b -> c) -> f a -> f b -> f c
liftA2 (Prefix -> Prefix -> Trie b -> Trie b -> Trie b
forall a. Prefix -> Prefix -> Trie a -> Trie a -> Trie a
Branch Prefix
p Prefix
m) (RevLazyByteString -> Trie a -> f (Trie b)
go RevLazyByteString
q Trie a
l) (RevLazyByteString -> Trie a -> f (Trie b)
go RevLazyByteString
q Trie a
r)
go RevLazyByteString
q (Arc ByteString
k Maybe a
Nothing Trie a
t) = (Trie b -> Trie b) -> f (Trie b) -> f (Trie b)
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
fmap (ByteString -> Maybe b -> Trie b -> Trie b
forall a. ByteString -> Maybe a -> Trie a -> Trie a
Arc ByteString
k Maybe b
forall a. Maybe a
Nothing) (RevLazyByteString -> Trie a -> f (Trie b)
go (RevLazyByteString
q RevLazyByteString -> ByteString -> RevLazyByteString
+>! ByteString
k) Trie a
t)
go RevLazyByteString
q (Arc ByteString
k (Just a
v) Trie a
t) =
let q' :: ByteString
q' = RevLazyByteString -> ByteString
toStrict (RevLazyByteString
q RevLazyByteString -> ByteString -> RevLazyByteString
+>? ByteString
k)
in (b -> Trie b -> Trie b) -> f b -> f (Trie b) -> f (Trie b)
forall (f :: * -> *) a b c.
Applicative f =>
(a -> b -> c) -> f a -> f b -> f c
liftA2 (ByteString -> Maybe b -> Trie b -> Trie b
forall a. ByteString -> Maybe a -> Trie a -> Trie a
Arc ByteString
k (Maybe b -> Trie b -> Trie b)
-> (b -> Maybe b) -> b -> Trie b -> Trie b
forall b c a. (b -> c) -> (a -> b) -> a -> c
. b -> Maybe b
forall a. a -> Maybe a
Just) (ByteString -> a -> f b
f ByteString
q' a
v) (RevLazyByteString -> Trie a -> f (Trie b)
go (ByteString -> RevLazyByteString
fromStrict ByteString
q') Trie a
t)
instance Applicative Trie where
pure :: a -> Trie a
pure = ByteString -> a -> Trie a
forall a. ByteString -> a -> Trie a
singleton ByteString
S.empty
Trie (a -> b)
t0 <*> :: Trie (a -> b) -> Trie a -> Trie b
<*> Trie a
t1 = Trie (a -> b)
t0 Trie (a -> b) -> ((a -> b) -> Trie b) -> Trie b
forall (m :: * -> *) a b. Monad m => m a -> (a -> m b) -> m b
>>= ((a -> b) -> Trie a -> Trie b
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> Trie a
t1)
#if MIN_VERSION_base(4,10,0)
#endif
instance Monad Trie where
#if (!(MIN_VERSION_base(4,8,0)))
return = pure
#endif
>>= :: Trie a -> (a -> Trie b) -> Trie b
(>>=) Trie a
Empty a -> Trie b
_ = Trie b
forall a. Trie a
empty
(>>=) (Branch Prefix
p Prefix
m Trie a
l Trie a
r) a -> Trie b
f = Prefix -> Prefix -> Trie b -> Trie b -> Trie b
forall a. Prefix -> Prefix -> Trie a -> Trie a -> Trie a
branch Prefix
p Prefix
m (Trie a
l Trie a -> (a -> Trie b) -> Trie b
forall (m :: * -> *) a b. Monad m => m a -> (a -> m b) -> m b
>>= a -> Trie b
f) (Trie a
r Trie a -> (a -> Trie b) -> Trie b
forall (m :: * -> *) a b. Monad m => m a -> (a -> m b) -> m b
>>= a -> Trie b
f)
(>>=) (Arc ByteString
k Maybe a
Nothing Trie a
t) a -> Trie b
f = ByteString -> Trie b -> Trie b
forall a. ByteString -> Trie a -> Trie a
prependNN ByteString
k (Trie a
t Trie a -> (a -> Trie b) -> Trie b
forall (m :: * -> *) a b. Monad m => m a -> (a -> m b) -> m b
>>= a -> Trie b
f)
(>>=) (Arc ByteString
k (Just a
v) Trie a
t) a -> Trie b
f = ByteString -> Trie b -> Trie b
forall a. ByteString -> Trie a -> Trie a
prepend ByteString
k (a -> Trie b
f a
v Trie b -> Trie b -> Trie b
forall a. Trie a -> Trie a -> Trie a
`unionL` (Trie a
t Trie a -> (a -> Trie b) -> Trie b
forall (m :: * -> *) a b. Monad m => m a -> (a -> m b) -> m b
>>= a -> Trie b
f))
where unionL :: Trie a -> Trie a -> Trie a
unionL = (a -> a -> Maybe a) -> Trie a -> Trie a -> Trie a
forall a. (a -> a -> Maybe a) -> Trie a -> Trie a -> Trie a
mergeBy (\a
x a
_ -> a -> Maybe a
forall a. a -> Maybe a
Just a
x)
#if MIN_VERSION_base(4,9,0)
instance (Semigroup a) => Semigroup (Trie a) where
<> :: Trie a -> Trie a -> Trie a
(<>) = (a -> a -> Maybe a) -> Trie a -> Trie a -> Trie a
forall a. (a -> a -> Maybe a) -> Trie a -> Trie a -> Trie a
mergeBy ((a -> a -> Maybe a) -> Trie a -> Trie a -> Trie a)
-> (a -> a -> Maybe a) -> Trie a -> Trie a -> Trie a
forall a b. (a -> b) -> a -> b
$ \a
x a
y -> a -> Maybe a
forall a. a -> Maybe a
Just (a
x a -> a -> a
forall a. Semigroup a => a -> a -> a
<> a
y)
stimes :: b -> Trie a -> Trie a
stimes = (a -> a) -> Trie a -> Trie a
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
fmap ((a -> a) -> Trie a -> Trie a)
-> (b -> a -> a) -> b -> Trie a -> Trie a
forall b c a. (b -> c) -> (a -> b) -> a -> c
. b -> a -> a
forall a b. (Semigroup a, Integral b) => b -> a -> a
stimes
#endif
instance (Monoid a) => Monoid (Trie a) where
mempty :: Trie a
mempty = Trie a
forall a. Trie a
empty
#if MIN_VERSION_base(4,11,0)
#else
mappend = mergeBy $ \x y -> Just (x `mappend` y)
#endif
filterMap :: (a -> Maybe b) -> Trie a -> Trie b
filterMap :: (a -> Maybe b) -> Trie a -> Trie b
filterMap a -> Maybe b
f = Trie a -> Trie b
start
where
start :: Trie a -> Trie b
start (Arc ByteString
k (Just a
v) Trie a
t) = ByteString -> Maybe b -> Trie b -> Trie b
forall a. ByteString -> Maybe a -> Trie a -> Trie a
arc ByteString
k (a -> Maybe b
f a
v) (Trie a -> Trie b
go Trie a
t)
start Trie a
t = Trie a -> Trie b
go Trie a
t
go :: Trie a -> Trie b
go Trie a
Empty = Trie b
forall a. Trie a
empty
go (Arc ByteString
k Maybe a
Nothing Trie a
t) = ByteString -> Trie b -> Trie b
forall a. ByteString -> Trie a -> Trie a
prependNN ByteString
k (Trie a -> Trie b
go Trie a
t)
go (Arc ByteString
k (Just a
v) Trie a
t) = ByteString -> Maybe b -> Trie b -> Trie b
forall a. ByteString -> Maybe a -> Trie a -> Trie a
arcNN ByteString
k (a -> Maybe b
f a
v) (Trie a -> Trie b
go Trie a
t)
go (Branch Prefix
p Prefix
m Trie a
l Trie a
r) = Prefix -> Prefix -> Trie b -> Trie b -> Trie b
forall a. Prefix -> Prefix -> Trie a -> Trie a -> Trie a
branch Prefix
p Prefix
m (Trie a -> Trie b
go Trie a
l) (Trie a -> Trie b
go Trie a
r)
filter :: (a -> Bool) -> Trie a -> Trie a
filter :: (a -> Bool) -> Trie a -> Trie a
filter a -> Bool
f = Trie a -> Trie a
start
where
start :: Trie a -> Trie a
start (Arc ByteString
k (Just a
v) Trie a
t) = ByteString -> a -> Bool -> Trie a -> Trie a
forall a. ByteString -> a -> Bool -> Trie a -> Trie a
arcB ByteString
k a
v (a -> Bool
f a
v) (Trie a -> Trie a
go Trie a
t)
start Trie a
t = Trie a -> Trie a
go Trie a
t
go :: Trie a -> Trie a
go Trie a
Empty = Trie a
forall a. Trie a
empty
go (Arc ByteString
k Maybe a
Nothing Trie a
t) = ByteString -> Trie a -> Trie a
forall a. ByteString -> Trie a -> Trie a
prependNN ByteString
k (Trie a -> Trie a
go Trie a
t)
go (Arc ByteString
k (Just a
v) Trie a
t) = ByteString -> a -> Bool -> Trie a -> Trie a
forall a. ByteString -> a -> Bool -> Trie a -> Trie a
arcNNB ByteString
k a
v (a -> Bool
f a
v) (Trie a -> Trie a
go Trie a
t)
go (Branch Prefix
p Prefix
m Trie a
l Trie a
r) = Prefix -> Prefix -> Trie a -> Trie a -> Trie a
forall a. Prefix -> Prefix -> Trie a -> Trie a -> Trie a
branch Prefix
p Prefix
m (Trie a -> Trie a
go Trie a
l) (Trie a -> Trie a
go Trie a
r)
arcB :: ByteString -> a -> Bool -> Trie a -> Trie a
{-# INLINE arcB #-}
arcB :: ByteString -> a -> Bool -> Trie a -> Trie a
arcB ByteString
k a
v Bool
True = ByteString -> Maybe a -> Trie a -> Trie a
forall a. ByteString -> Maybe a -> Trie a -> Trie a
Arc ByteString
k (a -> Maybe a
forall a. a -> Maybe a
Just a
v)
arcB ByteString
k a
_ Bool
False = ByteString -> Trie a -> Trie a
forall a. ByteString -> Trie a -> Trie a
prepend ByteString
k
arcNNB :: ByteString -> a -> Bool -> Trie a -> Trie a
{-# INLINE arcNNB #-}
arcNNB :: ByteString -> a -> Bool -> Trie a -> Trie a
arcNNB ByteString
k a
v Bool
True = ByteString -> Maybe a -> Trie a -> Trie a
forall a. ByteString -> Maybe a -> Trie a -> Trie a
Arc ByteString
k (a -> Maybe a
forall a. a -> Maybe a
Just a
v)
arcNNB ByteString
k a
_ Bool
False = ByteString -> Trie a -> Trie a
forall a. ByteString -> Trie a -> Trie a
prependNN ByteString
k
wither :: Applicative f => (a -> f (Maybe b)) -> Trie a -> f (Trie b)
wither :: (a -> f (Maybe b)) -> Trie a -> f (Trie b)
wither a -> f (Maybe b)
f = Trie a -> f (Trie b)
start
where
start :: Trie a -> f (Trie b)
start (Arc ByteString
k (Just a
v) Trie a
t) = (Maybe b -> Trie b -> Trie b)
-> f (Maybe b) -> f (Trie b) -> f (Trie b)
forall (f :: * -> *) a b c.
Applicative f =>
(a -> b -> c) -> f a -> f b -> f c
liftA2 (ByteString -> Maybe b -> Trie b -> Trie b
forall a. ByteString -> Maybe a -> Trie a -> Trie a
arc ByteString
k) (a -> f (Maybe b)
f a
v) (Trie a -> f (Trie b)
go Trie a
t)
start Trie a
t = Trie a -> f (Trie b)
go Trie a
t
go :: Trie a -> f (Trie b)
go Trie a
Empty = Trie b -> f (Trie b)
forall (f :: * -> *) a. Applicative f => a -> f a
pure Trie b
forall a. Trie a
empty
go (Arc ByteString
k Maybe a
Nothing Trie a
t) = (Trie b -> Trie b) -> f (Trie b) -> f (Trie b)
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
fmap (ByteString -> Trie b -> Trie b
forall a. ByteString -> Trie a -> Trie a
prependNN ByteString
k) (Trie a -> f (Trie b)
go Trie a
t)
go (Arc ByteString
k (Just a
v) Trie a
t) = (Maybe b -> Trie b -> Trie b)
-> f (Maybe b) -> f (Trie b) -> f (Trie b)
forall (f :: * -> *) a b c.
Applicative f =>
(a -> b -> c) -> f a -> f b -> f c
liftA2 (ByteString -> Maybe b -> Trie b -> Trie b
forall a. ByteString -> Maybe a -> Trie a -> Trie a
arcNN ByteString
k) (a -> f (Maybe b)
f a
v) (Trie a -> f (Trie b)
go Trie a
t)
go (Branch Prefix
p Prefix
m Trie a
l Trie a
r) = (Trie b -> Trie b -> Trie b)
-> f (Trie b) -> f (Trie b) -> f (Trie b)
forall (f :: * -> *) a b c.
Applicative f =>
(a -> b -> c) -> f a -> f b -> f c
liftA2 (Prefix -> Prefix -> Trie b -> Trie b -> Trie b
forall a. Prefix -> Prefix -> Trie a -> Trie a -> Trie a
branch Prefix
p Prefix
m) (Trie a -> f (Trie b)
go Trie a
l) (Trie a -> f (Trie b)
go Trie a
r)
filterA :: Applicative f => (a -> f Bool) -> Trie a -> f (Trie a)
filterA :: (a -> f Bool) -> Trie a -> f (Trie a)
filterA a -> f Bool
f = Trie a -> f (Trie a)
start
where
start :: Trie a -> f (Trie a)
start (Arc ByteString
k (Just a
v) Trie a
t) = (Bool -> Trie a -> Trie a) -> f Bool -> f (Trie a) -> f (Trie a)
forall (f :: * -> *) a b c.
Applicative f =>
(a -> b -> c) -> f a -> f b -> f c
liftA2 (ByteString -> a -> Bool -> Trie a -> Trie a
forall a. ByteString -> a -> Bool -> Trie a -> Trie a
arcB ByteString
k a
v) (a -> f Bool
f a
v) (Trie a -> f (Trie a)
go Trie a
t)
start Trie a
t = Trie a -> f (Trie a)
go Trie a
t
go :: Trie a -> f (Trie a)
go Trie a
Empty = Trie a -> f (Trie a)
forall (f :: * -> *) a. Applicative f => a -> f a
pure Trie a
forall a. Trie a
empty
go (Arc ByteString
k Maybe a
Nothing Trie a
t) = (Trie a -> Trie a) -> f (Trie a) -> f (Trie a)
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
fmap (ByteString -> Trie a -> Trie a
forall a. ByteString -> Trie a -> Trie a
prependNN ByteString
k) (Trie a -> f (Trie a)
go Trie a
t)
go (Arc ByteString
k (Just a
v) Trie a
t) = (Bool -> Trie a -> Trie a) -> f Bool -> f (Trie a) -> f (Trie a)
forall (f :: * -> *) a b c.
Applicative f =>
(a -> b -> c) -> f a -> f b -> f c
liftA2 (ByteString -> a -> Bool -> Trie a -> Trie a
forall a. ByteString -> a -> Bool -> Trie a -> Trie a
arcNNB ByteString
k a
v) (a -> f Bool
f a
v) (Trie a -> f (Trie a)
go Trie a
t)
go (Branch Prefix
p Prefix
m Trie a
l Trie a
r) = (Trie a -> Trie a -> Trie a)
-> f (Trie a) -> f (Trie a) -> f (Trie a)
forall (f :: * -> *) a b c.
Applicative f =>
(a -> b -> c) -> f a -> f b -> f c
liftA2 (Prefix -> Prefix -> Trie a -> Trie a -> Trie a
forall a. Prefix -> Prefix -> Trie a -> Trie a -> Trie a
branch Prefix
p Prefix
m) (Trie a -> f (Trie a)
go Trie a
l) (Trie a -> f (Trie a)
go Trie a
r)
mapBy :: (ByteString -> a -> Maybe b) -> Trie a -> Trie b
mapBy :: (ByteString -> a -> Maybe b) -> Trie a -> Trie b
mapBy ByteString -> a -> Maybe b
f = Trie a -> Trie b
start
where
start :: Trie a -> Trie b
start (Arc ByteString
k (Just a
v) Trie a
t) = ByteString -> Maybe b -> Trie b -> Trie b
forall a. ByteString -> Maybe a -> Trie a -> Trie a
arc ByteString
k (ByteString -> a -> Maybe b
f ByteString
k a
v) (RevLazyByteString -> Trie a -> Trie b
go (ByteString -> RevLazyByteString
fromStrict ByteString
k) Trie a
t)
start Trie a
t = RevLazyByteString -> Trie a -> Trie b
go RevLazyByteString
Nil Trie a
t
go :: RevLazyByteString -> Trie a -> Trie b
go RevLazyByteString
_ Trie a
Empty = Trie b
forall a. Trie a
empty
go RevLazyByteString
q (Branch Prefix
p Prefix
m Trie a
l Trie a
r) = Prefix -> Prefix -> Trie b -> Trie b -> Trie b
forall a. Prefix -> Prefix -> Trie a -> Trie a -> Trie a
branch Prefix
p Prefix
m (RevLazyByteString -> Trie a -> Trie b
go RevLazyByteString
q Trie a
l) (RevLazyByteString -> Trie a -> Trie b
go RevLazyByteString
q Trie a
r)
go RevLazyByteString
q (Arc ByteString
k Maybe a
Nothing Trie a
t) = ByteString -> Trie b -> Trie b
forall a. ByteString -> Trie a -> Trie a
prependNN ByteString
k (RevLazyByteString -> Trie a -> Trie b
go (RevLazyByteString
q RevLazyByteString -> ByteString -> RevLazyByteString
+>! ByteString
k) Trie a
t)
go RevLazyByteString
q (Arc ByteString
k (Just a
v) Trie a
t) = ByteString -> Maybe b -> Trie b -> Trie b
forall a. ByteString -> Maybe a -> Trie a -> Trie a
arcNN ByteString
k (ByteString -> a -> Maybe b
f ByteString
q' a
v) (RevLazyByteString -> Trie a -> Trie b
go (ByteString -> RevLazyByteString
fromStrict ByteString
q') Trie a
t)
where q' :: ByteString
q' = RevLazyByteString -> ByteString
toStrict (RevLazyByteString
q RevLazyByteString -> ByteString -> RevLazyByteString
+>? ByteString
k)
contextualMap :: (a -> Trie a -> b) -> Trie a -> Trie b
contextualMap :: (a -> Trie a -> b) -> Trie a -> Trie b
contextualMap a -> Trie a -> b
f = Trie a -> Trie b
go
where
go :: Trie a -> Trie b
go Trie a
Empty = Trie b
forall a. Trie a
Empty
go (Arc ByteString
k Maybe a
Nothing Trie a
t) = ByteString -> Maybe b -> Trie b -> Trie b
forall a. ByteString -> Maybe a -> Trie a -> Trie a
Arc ByteString
k Maybe b
forall a. Maybe a
Nothing (Trie a -> Trie b
go Trie a
t)
go (Arc ByteString
k (Just a
v) Trie a
t) = ByteString -> Maybe b -> Trie b -> Trie b
forall a. ByteString -> Maybe a -> Trie a -> Trie a
Arc ByteString
k (b -> Maybe b
forall a. a -> Maybe a
Just (a -> Trie a -> b
f a
v Trie a
t)) (Trie a -> Trie b
go Trie a
t)
go (Branch Prefix
p Prefix
m Trie a
l Trie a
r) = Prefix -> Prefix -> Trie b -> Trie b -> Trie b
forall a. Prefix -> Prefix -> Trie a -> Trie a -> Trie a
Branch Prefix
p Prefix
m (Trie a -> Trie b
go Trie a
l) (Trie a -> Trie b
go Trie a
r)
contextualMap' :: (a -> Trie a -> b) -> Trie a -> Trie b
contextualMap' :: (a -> Trie a -> b) -> Trie a -> Trie b
contextualMap' a -> Trie a -> b
f = Trie a -> Trie b
go
where
go :: Trie a -> Trie b
go Trie a
Empty = Trie b
forall a. Trie a
Empty
go (Arc ByteString
k Maybe a
Nothing Trie a
t) = ByteString -> Maybe b -> Trie b -> Trie b
forall a. ByteString -> Maybe a -> Trie a -> Trie a
Arc ByteString
k Maybe b
forall a. Maybe a
Nothing (Trie a -> Trie b
go Trie a
t)
go (Arc ByteString
k (Just a
v) Trie a
t) = ByteString -> Maybe b -> Trie b -> Trie b
forall a. ByteString -> Maybe a -> Trie a -> Trie a
Arc ByteString
k (b -> Maybe b
forall a. a -> Maybe a
Just (b -> Maybe b) -> b -> Maybe b
forall a b. (a -> b) -> a -> b
$! a -> Trie a -> b
f a
v Trie a
t) (Trie a -> Trie b
go Trie a
t)
go (Branch Prefix
p Prefix
m Trie a
l Trie a
r) = Prefix -> Prefix -> Trie b -> Trie b -> Trie b
forall a. Prefix -> Prefix -> Trie a -> Trie a -> Trie a
Branch Prefix
p Prefix
m (Trie a -> Trie b
go Trie a
l) (Trie a -> Trie b
go Trie a
r)
contextualFilterMap :: (a -> Trie a -> Maybe b) -> Trie a -> Trie b
contextualFilterMap :: (a -> Trie a -> Maybe b) -> Trie a -> Trie b
contextualFilterMap a -> Trie a -> Maybe b
f = Trie a -> Trie b
start
where
start :: Trie a -> Trie b
start (Arc ByteString
k (Just a
v) Trie a
t) = ByteString -> Maybe b -> Trie b -> Trie b
forall a. ByteString -> Maybe a -> Trie a -> Trie a
arc ByteString
k (a -> Trie a -> Maybe b
f a
v Trie a
t) (Trie a -> Trie b
go Trie a
t)
start Trie a
t = Trie a -> Trie b
go Trie a
t
go :: Trie a -> Trie b
go Trie a
Empty = Trie b
forall a. Trie a
empty
go (Arc ByteString
k Maybe a
Nothing Trie a
t) = ByteString -> Trie b -> Trie b
forall a. ByteString -> Trie a -> Trie a
prependNN ByteString
k (Trie a -> Trie b
go Trie a
t)
go (Arc ByteString
k (Just a
v) Trie a
t) = ByteString -> Maybe b -> Trie b -> Trie b
forall a. ByteString -> Maybe a -> Trie a -> Trie a
arcNN ByteString
k (a -> Trie a -> Maybe b
f a
v Trie a
t) (Trie a -> Trie b
go Trie a
t)
go (Branch Prefix
p Prefix
m Trie a
l Trie a
r) = Prefix -> Prefix -> Trie b -> Trie b -> Trie b
forall a. Prefix -> Prefix -> Trie a -> Trie a -> Trie a
branch Prefix
p Prefix
m (Trie a -> Trie b
go Trie a
l) (Trie a -> Trie b
go Trie a
r)
contextualMapBy :: (ByteString -> a -> Trie a -> Maybe b) -> Trie a -> Trie b
contextualMapBy :: (ByteString -> a -> Trie a -> Maybe b) -> Trie a -> Trie b
contextualMapBy ByteString -> a -> Trie a -> Maybe b
f = Trie a -> Trie b
start
where
start :: Trie a -> Trie b
start (Arc ByteString
k (Just a
v) Trie a
t) = ByteString -> Maybe b -> Trie b -> Trie b
forall a. ByteString -> Maybe a -> Trie a -> Trie a
arc ByteString
k (ByteString -> a -> Trie a -> Maybe b
f ByteString
k a
v Trie a
t) (RevLazyByteString -> Trie a -> Trie b
go (ByteString -> RevLazyByteString
fromStrict ByteString
k) Trie a
t)
start Trie a
t = RevLazyByteString -> Trie a -> Trie b
go RevLazyByteString
Nil Trie a
t
go :: RevLazyByteString -> Trie a -> Trie b
go RevLazyByteString
_ Trie a
Empty = Trie b
forall a. Trie a
empty
go RevLazyByteString
q (Branch Prefix
p Prefix
m Trie a
l Trie a
r) = Prefix -> Prefix -> Trie b -> Trie b -> Trie b
forall a. Prefix -> Prefix -> Trie a -> Trie a -> Trie a
branch Prefix
p Prefix
m (RevLazyByteString -> Trie a -> Trie b
go RevLazyByteString
q Trie a
l) (RevLazyByteString -> Trie a -> Trie b
go RevLazyByteString
q Trie a
r)
go RevLazyByteString
q (Arc ByteString
k Maybe a
Nothing Trie a
t) = ByteString -> Trie b -> Trie b
forall a. ByteString -> Trie a -> Trie a
prependNN ByteString
k (RevLazyByteString -> Trie a -> Trie b
go (RevLazyByteString
q RevLazyByteString -> ByteString -> RevLazyByteString
+>! ByteString
k) Trie a
t)
go RevLazyByteString
q (Arc ByteString
k (Just a
v) Trie a
t) = ByteString -> Maybe b -> Trie b -> Trie b
forall a. ByteString -> Maybe a -> Trie a -> Trie a
arcNN ByteString
k (ByteString -> a -> Trie a -> Maybe b
f ByteString
q' a
v Trie a
t) (RevLazyByteString -> Trie a -> Trie b
go (ByteString -> RevLazyByteString
fromStrict ByteString
q') Trie a
t)
where q' :: ByteString
q' = RevLazyByteString -> ByteString
toStrict (RevLazyByteString
q RevLazyByteString -> ByteString -> RevLazyByteString
+>? ByteString
k)
empty :: Trie a
{-# INLINE empty #-}
empty :: Trie a
empty = Trie a
forall a. Trie a
Empty
null :: Trie a -> Bool
{-# INLINE null #-}
null :: Trie a -> Bool
null Trie a
Empty = Bool
True
null Trie a
_ = Bool
False
singleton :: ByteString -> a -> Trie a
{-# INLINE singleton #-}
singleton :: ByteString -> a -> Trie a
singleton ByteString
k a
v = ByteString -> Maybe a -> Trie a -> Trie a
forall a. ByteString -> Maybe a -> Trie a -> Trie a
Arc ByteString
k (a -> Maybe a
forall a. a -> Maybe a
Just a
v) Trie a
forall a. Trie a
Empty
size :: Trie a -> Int
{-# INLINE size #-}
size :: Trie a -> Int
size = Int -> Trie a -> Int
forall a. Int -> Trie a -> Int
size' Int
0
size' :: Int -> Trie a -> Int
size' :: Int -> Trie a -> Int
size' !Int
n Trie a
Empty = Int
n
size' Int
n (Arc ByteString
_ Maybe a
Nothing Trie a
t) = Int -> Trie a -> Int
forall a. Int -> Trie a -> Int
size' Int
n Trie a
t
size' Int
n (Arc ByteString
_ (Just a
_) Trie a
t) = Int -> Trie a -> Int
forall a. Int -> Trie a -> Int
size' (Int
n Int -> Int -> Int
forall a. Num a => a -> a -> a
+ Int
1) Trie a
t
size' Int
n (Branch Prefix
_ Prefix
_ Trie a
l Trie a
r) = Int -> Trie a -> Int
forall a. Int -> Trie a -> Int
size' (Int -> Trie a -> Int
forall a. Int -> Trie a -> Int
size' Int
n Trie a
l) Trie a
r
instance Foldable Trie where
{-# INLINABLE fold #-}
fold :: Trie m -> m
fold = Trie m -> m
forall m. Monoid m => Trie m -> m
go
where
go :: Trie a -> a
go Trie a
Empty = a
forall a. Monoid a => a
mempty
go (Arc ByteString
_ Maybe a
Nothing Trie a
t) = Trie a -> a
go Trie a
t
go (Arc ByteString
_ (Just a
v) Trie a
t) = a
v a -> a -> a
forall a. Monoid a => a -> a -> a
`mappend` Trie a -> a
go Trie a
t
go (Branch Prefix
_ Prefix
_ Trie a
l Trie a
r) = Trie a -> a
go Trie a
l a -> a -> a
forall a. Monoid a => a -> a -> a
`mappend` Trie a -> a
go Trie a
r
{-# INLINE foldMap #-}
foldMap :: (a -> m) -> Trie a -> m
foldMap a -> m
f = Trie a -> m
go
where
go :: Trie a -> m
go Trie a
Empty = m
forall a. Monoid a => a
mempty
go (Arc ByteString
_ Maybe a
Nothing Trie a
t) = Trie a -> m
go Trie a
t
go (Arc ByteString
_ (Just a
v) Trie a
t) = a -> m
f a
v m -> m -> m
forall a. Monoid a => a -> a -> a
`mappend` Trie a -> m
go Trie a
t
go (Branch Prefix
_ Prefix
_ Trie a
l Trie a
r) = Trie a -> m
go Trie a
l m -> m -> m
forall a. Monoid a => a -> a -> a
`mappend` Trie a -> m
go Trie a
r
#if MIN_VERSION_base(4,13,0)
{-# INLINE foldMap' #-}
foldMap' :: (a -> m) -> Trie a -> m
foldMap' a -> m
f = m -> Trie a -> m
go m
forall a. Monoid a => a
mempty
where
go :: m -> Trie a -> m
go !m
m Trie a
Empty = m
m
go m
m (Arc ByteString
_ Maybe a
Nothing Trie a
t) = m -> Trie a -> m
go m
m Trie a
t
go m
m (Arc ByteString
_ (Just a
v) Trie a
t) = m -> Trie a -> m
go (m
m m -> m -> m
forall a. Monoid a => a -> a -> a
`mappend` a -> m
f a
v) Trie a
t
go m
m (Branch Prefix
_ Prefix
_ Trie a
l Trie a
r) = m -> Trie a -> m
go (m -> Trie a -> m
go m
m Trie a
l) Trie a
r
#endif
{-# INLINE foldr #-}
foldr :: (a -> b -> b) -> b -> Trie a -> b
foldr a -> b -> b
f b
z0 = \Trie a
t -> Trie a -> b -> b
go Trie a
t b
z0
where
go :: Trie a -> b -> b
go Trie a
Empty = b -> b
forall a. a -> a
id
go (Arc ByteString
_ Maybe a
Nothing Trie a
t) = Trie a -> b -> b
go Trie a
t
go (Arc ByteString
_ (Just a
v) Trie a
t) = a -> b -> b
f a
v (b -> b) -> (b -> b) -> b -> b
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Trie a -> b -> b
go Trie a
t
go (Branch Prefix
_ Prefix
_ Trie a
l Trie a
r) = Trie a -> b -> b
go Trie a
l (b -> b) -> (b -> b) -> b -> b
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Trie a -> b -> b
go Trie a
r
#if MIN_VERSION_base(4,6,0)
{-# INLINE foldr' #-}
foldr' :: (a -> b -> b) -> b -> Trie a -> b
foldr' a -> b -> b
f b
z0 = b -> Trie a -> b
go b
z0
where
go :: b -> Trie a -> b
go !b
z Trie a
Empty = b
z
go b
z (Arc ByteString
_ Maybe a
Nothing Trie a
t) = b -> Trie a -> b
go b
z Trie a
t
go b
z (Arc ByteString
_ (Just a
v) Trie a
t) = a -> b -> b
f a
v (b -> b) -> b -> b
forall a b. (a -> b) -> a -> b
$! b -> Trie a -> b
go b
z Trie a
t
go b
z (Branch Prefix
_ Prefix
_ Trie a
l Trie a
r) = b -> Trie a -> b
go (b -> Trie a -> b
go b
z Trie a
r) Trie a
l
#endif
{-# INLINE foldl #-}
foldl :: (b -> a -> b) -> b -> Trie a -> b
foldl b -> a -> b
f b
z0 = \Trie a
t -> Trie a -> b -> b
go Trie a
t b
z0
where
go :: Trie a -> b -> b
go Trie a
Empty b
z = b
z
go (Arc ByteString
_ Maybe a
Nothing Trie a
t) b
z = Trie a -> b -> b
go Trie a
t b
z
go (Arc ByteString
_ (Just a
v) Trie a
t) b
z = Trie a -> b -> b
go Trie a
t (b -> a -> b
f b
z a
v)
go (Branch Prefix
_ Prefix
_ Trie a
l Trie a
r) b
z = Trie a -> b -> b
go Trie a
r (Trie a -> b -> b
go Trie a
l b
z)
#if MIN_VERSION_base(4,6,0)
{-# INLINE foldl' #-}
foldl' :: (b -> a -> b) -> b -> Trie a -> b
foldl' b -> a -> b
f b
z0 = b -> Trie a -> b
go b
z0
where
go :: b -> Trie a -> b
go !b
z Trie a
Empty = b
z
go b
z (Arc ByteString
_ Maybe a
Nothing Trie a
t) = b -> Trie a -> b
go b
z Trie a
t
go b
z (Arc ByteString
_ (Just a
v) Trie a
t) = b -> Trie a -> b
go (b -> a -> b
f b
z a
v) Trie a
t
go b
z (Branch Prefix
_ Prefix
_ Trie a
l Trie a
r) = b -> Trie a -> b
go (b -> Trie a -> b
go b
z Trie a
l) Trie a
r
#endif
#if MIN_VERSION_base(4,8,0)
{-# INLINE length #-}
length :: Trie a -> Int
length = Trie a -> Int
forall a. Trie a -> Int
size
{-# INLINE null #-}
null :: Trie a -> Bool
null = Trie a -> Bool
forall a. Trie a -> Bool
null
{-# INLINE toList #-}
toList :: Trie a -> [a]
toList = Trie a -> [a]
forall a. Trie a -> [a]
elems
{-# INLINABLE maximum #-}
maximum :: Trie a -> a
maximum = Trie a -> a
forall a. Ord a => Trie a -> a
go0
where
go0 :: Trie a -> a
go0 Trie a
Empty = String -> a
forall a. HasCallStack => String -> a
error String
"Data.Foldable.maximum @Trie: empty trie"
go0 (Arc ByteString
_ Maybe a
Nothing Trie a
t) = Trie a -> a
go0 Trie a
t
go0 (Arc ByteString
_ (Just a
v) Trie a
t) = a -> Trie a -> a
forall a. Ord a => a -> Trie a -> a
go a
v Trie a
t
go0 (Branch Prefix
_ Prefix
_ Trie a
l Trie a
r) = a -> Trie a -> a
forall a. Ord a => a -> Trie a -> a
go (Trie a -> a
go0 Trie a
l) Trie a
r
go :: a -> Trie a -> a
go !a
w Trie a
Empty = a
w
go a
w (Arc ByteString
_ Maybe a
Nothing Trie a
t) = a -> Trie a -> a
go a
w Trie a
t
go a
w (Arc ByteString
_ (Just a
v) Trie a
t) = a -> Trie a -> a
go (a -> a -> a
forall a. Ord a => a -> a -> a
max a
w a
v) Trie a
t
go a
w (Branch Prefix
_ Prefix
_ Trie a
l Trie a
r) = a -> Trie a -> a
go (a -> Trie a -> a
go a
w Trie a
l) Trie a
r
{-# INLINABLE minimum #-}
minimum :: Trie a -> a
minimum = Trie a -> a
forall a. Ord a => Trie a -> a
go0
where
go0 :: Trie a -> a
go0 Trie a
Empty = String -> a
forall a. HasCallStack => String -> a
error String
"Data.Foldable.minimum @Trie: empty trie"
go0 (Arc ByteString
_ Maybe a
Nothing Trie a
t) = Trie a -> a
go0 Trie a
t
go0 (Arc ByteString
_ (Just a
v) Trie a
t) = a -> Trie a -> a
forall a. Ord a => a -> Trie a -> a
go a
v Trie a
t
go0 (Branch Prefix
_ Prefix
_ Trie a
l Trie a
r) = a -> Trie a -> a
forall a. Ord a => a -> Trie a -> a
go (Trie a -> a
go0 Trie a
l) Trie a
r
go :: a -> Trie a -> a
go !a
w Trie a
Empty = a
w
go a
w (Arc ByteString
_ Maybe a
Nothing Trie a
t) = a -> Trie a -> a
go a
w Trie a
t
go a
w (Arc ByteString
_ (Just a
v) Trie a
t) = a -> Trie a -> a
go (a -> a -> a
forall a. Ord a => a -> a -> a
min a
w a
v) Trie a
t
go a
w (Branch Prefix
_ Prefix
_ Trie a
l Trie a
r) = a -> Trie a -> a
go (a -> Trie a -> a
go a
w Trie a
l) Trie a
r
{-# INLINABLE sum #-}
sum :: Trie a -> a
sum = (a -> a -> a) -> a -> Trie a -> a
forall (t :: * -> *) b a.
Foldable t =>
(b -> a -> b) -> b -> t a -> b
F.foldl' a -> a -> a
forall a. Num a => a -> a -> a
(+) a
0
{-# INLINABLE product #-}
product :: Trie a -> a
product = (a -> a -> a) -> a -> Trie a -> a
forall (t :: * -> *) b a.
Foldable t =>
(b -> a -> b) -> b -> t a -> b
F.foldl' a -> a -> a
forall a. Num a => a -> a -> a
(*) a
1
#endif
foldrWithKey :: (ByteString -> a -> b -> b) -> b -> Trie a -> b
{-# INLINE foldrWithKey #-}
foldrWithKey :: (ByteString -> a -> b -> b) -> b -> Trie a -> b
foldrWithKey ByteString -> a -> b -> b
f b
z0 = \Trie a
t -> RevLazyByteString -> Trie a -> b -> b
go RevLazyByteString
Nil Trie a
t b
z0
where
go :: RevLazyByteString -> Trie a -> b -> b
go RevLazyByteString
_ Trie a
Empty = b -> b
forall a. a -> a
id
go RevLazyByteString
q (Branch Prefix
_ Prefix
_ Trie a
l Trie a
r) = RevLazyByteString -> Trie a -> b -> b
go RevLazyByteString
q Trie a
l (b -> b) -> (b -> b) -> b -> b
forall b c a. (b -> c) -> (a -> b) -> a -> c
. RevLazyByteString -> Trie a -> b -> b
go RevLazyByteString
q Trie a
r
go RevLazyByteString
q (Arc ByteString
k Maybe a
Nothing Trie a
t) = RevLazyByteString -> Trie a -> b -> b
go (RevLazyByteString
q RevLazyByteString -> ByteString -> RevLazyByteString
+>! ByteString
k) Trie a
t
go RevLazyByteString
q (Arc ByteString
k (Just a
v) Trie a
t) = ByteString -> a -> b -> b
f ByteString
q' a
v (b -> b) -> (b -> b) -> b -> b
forall b c a. (b -> c) -> (a -> b) -> a -> c
. RevLazyByteString -> Trie a -> b -> b
go (ByteString -> RevLazyByteString
fromStrict ByteString
q') Trie a
t
where q' :: ByteString
q' = RevLazyByteString -> ByteString
toStrict (RevLazyByteString
q RevLazyByteString -> ByteString -> RevLazyByteString
+>? ByteString
k)
foldrWithKey' :: (ByteString -> a -> b -> b) -> b -> Trie a -> b
{-# INLINE foldrWithKey' #-}
foldrWithKey' :: (ByteString -> a -> b -> b) -> b -> Trie a -> b
foldrWithKey' ByteString -> a -> b -> b
f b
z0 = RevLazyByteString -> b -> Trie a -> b
go RevLazyByteString
Nil b
z0
where
go :: RevLazyByteString -> b -> Trie a -> b
go RevLazyByteString
_ !b
z Trie a
Empty = b
z
go RevLazyByteString
q b
z (Branch Prefix
_ Prefix
_ Trie a
l Trie a
r) = RevLazyByteString -> b -> Trie a -> b
go RevLazyByteString
q (RevLazyByteString -> b -> Trie a -> b
go RevLazyByteString
q b
z Trie a
r) Trie a
l
go RevLazyByteString
q b
z (Arc ByteString
k Maybe a
Nothing Trie a
t) = RevLazyByteString -> b -> Trie a -> b
go (RevLazyByteString
q RevLazyByteString -> ByteString -> RevLazyByteString
+>! ByteString
k) b
z Trie a
t
go RevLazyByteString
q b
z (Arc ByteString
k (Just a
v) Trie a
t) = ByteString -> a -> b -> b
f ByteString
q' a
v (b -> b) -> b -> b
forall a b. (a -> b) -> a -> b
$! RevLazyByteString -> b -> Trie a -> b
go (ByteString -> RevLazyByteString
fromStrict ByteString
q') b
z Trie a
t
where q' :: ByteString
q' = RevLazyByteString -> ByteString
toStrict (RevLazyByteString
q RevLazyByteString -> ByteString -> RevLazyByteString
+>? ByteString
k)
foldlWithKey :: (b -> ByteString -> a -> b) -> b -> Trie a -> b
{-# INLINE foldlWithKey #-}
foldlWithKey :: (b -> ByteString -> a -> b) -> b -> Trie a -> b
foldlWithKey b -> ByteString -> a -> b
f b
z0 = RevLazyByteString -> b -> Trie a -> b
go RevLazyByteString
Nil b
z0
where
go :: RevLazyByteString -> b -> Trie a -> b
go RevLazyByteString
_ b
z Trie a
Empty = b
z
go RevLazyByteString
q b
z (Branch Prefix
_ Prefix
_ Trie a
l Trie a
r) = RevLazyByteString -> b -> Trie a -> b
go RevLazyByteString
q (RevLazyByteString -> b -> Trie a -> b
go RevLazyByteString
q b
z Trie a
l) Trie a
r
go RevLazyByteString
q b
z (Arc ByteString
k Maybe a
Nothing Trie a
t) = RevLazyByteString -> b -> Trie a -> b
go (RevLazyByteString
q RevLazyByteString -> ByteString -> RevLazyByteString
+>! ByteString
k) b
z Trie a
t
go RevLazyByteString
q b
z (Arc ByteString
k (Just a
v) Trie a
t) = RevLazyByteString -> b -> Trie a -> b
go (ByteString -> RevLazyByteString
fromStrict ByteString
q') (b -> ByteString -> a -> b
f b
z ByteString
q' a
v) Trie a
t
where q' :: ByteString
q' = RevLazyByteString -> ByteString
toStrict (RevLazyByteString
q RevLazyByteString -> ByteString -> RevLazyByteString
+>? ByteString
k)
foldlWithKey' :: (b -> ByteString -> a -> b) -> b -> Trie a -> b
{-# INLINE foldlWithKey' #-}
foldlWithKey' :: (b -> ByteString -> a -> b) -> b -> Trie a -> b
foldlWithKey' b -> ByteString -> a -> b
f b
z0 = RevLazyByteString -> b -> Trie a -> b
go RevLazyByteString
Nil b
z0
where
go :: RevLazyByteString -> b -> Trie a -> b
go RevLazyByteString
_ !b
z Trie a
Empty = b
z
go RevLazyByteString
q b
z (Branch Prefix
_ Prefix
_ Trie a
l Trie a
r) = RevLazyByteString -> b -> Trie a -> b
go RevLazyByteString
q (RevLazyByteString -> b -> Trie a -> b
go RevLazyByteString
q b
z Trie a
l) Trie a
r
go RevLazyByteString
q b
z (Arc ByteString
k Maybe a
Nothing Trie a
t) = RevLazyByteString -> b -> Trie a -> b
go (RevLazyByteString
q RevLazyByteString -> ByteString -> RevLazyByteString
+>! ByteString
k) b
z Trie a
t
go RevLazyByteString
q b
z (Arc ByteString
k (Just a
v) Trie a
t) = RevLazyByteString -> b -> Trie a -> b
go (ByteString -> RevLazyByteString
fromStrict ByteString
q') (b -> ByteString -> a -> b
f b
z ByteString
q' a
v) Trie a
t
where q' :: ByteString
q' = RevLazyByteString -> ByteString
toStrict (RevLazyByteString
q RevLazyByteString -> ByteString -> RevLazyByteString
+>? ByteString
k)
cata_
:: (ByteString -> Maybe a -> b -> b)
-> (b -> b -> b)
-> b
-> Trie a -> b
{-# INLINE cata_ #-}
cata_ :: (ByteString -> Maybe a -> b -> b)
-> (b -> b -> b) -> b -> Trie a -> b
cata_ ByteString -> Maybe a -> b -> b
a b -> b -> b
b b
e = Trie a -> b
go
where
go :: Trie a -> b
go Trie a
Empty = b
e
go (Arc ByteString
k Maybe a
mv Trie a
t) = ByteString -> Maybe a -> b -> b
a ByteString
k Maybe a
mv (Trie a -> b
go Trie a
t)
go (Branch Prefix
_ Prefix
_ Trie a
l Trie a
r) = b -> b -> b
b (Trie a -> b
go Trie a
l) (Trie a -> b
go Trie a
r)
cata
:: (ByteString -> a -> b -> b)
-> (ByteString -> [b] -> b)
-> b
-> Trie a -> b
cata :: (ByteString -> a -> b -> b)
-> (ByteString -> [b] -> b) -> b -> Trie a -> b
cata ByteString -> a -> b -> b
a ByteString -> [b] -> b
b b
e = Trie a -> b
go
where
step :: ByteString -> Maybe a -> Trie a -> b
step ByteString
k (Just a
v) Trie a
t = ByteString -> a -> b -> b
a ByteString
k a
v (Trie a -> b
go Trie a
t)
step ByteString
k Maybe a
Nothing Trie a
t = ByteString -> [b] -> b
b ByteString
k (Trie a -> [b] -> [b]
collect Trie a
t [])
go :: Trie a -> b
go Trie a
Empty = b
e
go (Arc ByteString
k Maybe a
mv Trie a
t) = ByteString -> Maybe a -> Trie a -> b
step ByteString
k Maybe a
mv Trie a
t
go (Branch Prefix
_ Prefix
_ Trie a
l Trie a
r) = ByteString -> [b] -> b
b ByteString
S.empty (Trie a -> [b] -> [b]
collect Trie a
l (Trie a -> [b] -> [b]
collect Trie a
r []))
collect :: Trie a -> [b] -> [b]
collect Trie a
Empty [b]
bs = [b]
bs
collect (Arc ByteString
k Maybe a
mv Trie a
t) [b]
bs = ByteString -> Maybe a -> Trie a -> b
step ByteString
k Maybe a
mv Trie a
t b -> [b] -> [b]
forall a. a -> [a] -> [a]
: [b]
bs
collect (Branch Prefix
_ Prefix
_ Trie a
l Trie a
r) [b]
bs = Trie a -> [b] -> [b]
collect Trie a
l (Trie a -> [b] -> [b]
collect Trie a
r [b]
bs)
#if __GLASGOW_HASKELL__ >= 708
instance GHC.Exts.IsList (Trie a) where
type Item (Trie a) = (ByteString, a)
fromList :: [Item (Trie a)] -> Trie a
fromList = [Item (Trie a)] -> Trie a
forall a. [(ByteString, a)] -> Trie a
fromList
toList :: Trie a -> [Item (Trie a)]
toList = Trie a -> [Item (Trie a)]
forall a. Trie a -> [(ByteString, a)]
toList
#endif
fromList :: [(ByteString,a)] -> Trie a
{-# INLINE fromList #-}
fromList :: [(ByteString, a)] -> Trie a
fromList = ((ByteString, a) -> Trie a -> Trie a)
-> Trie a -> [(ByteString, a)] -> Trie a
forall (t :: * -> *) a b.
Foldable t =>
(a -> b -> b) -> b -> t a -> b
foldr ((ByteString -> a -> Trie a -> Trie a)
-> (ByteString, a) -> Trie a -> Trie a
forall a b c. (a -> b -> c) -> (a, b) -> c
uncurry ByteString -> a -> Trie a -> Trie a
forall a. ByteString -> a -> Trie a -> Trie a
insert) Trie a
forall a. Trie a
empty
where
insert :: ByteString -> a -> Trie a -> Trie a
insert = (ByteString -> a -> Maybe a -> Maybe a)
-> ByteString -> a -> Trie a -> Trie a
forall a.
(ByteString -> a -> Maybe a -> Maybe a)
-> ByteString -> a -> Trie a -> Trie a
alterBy (\ByteString
_ a
x Maybe a
_ -> a -> Maybe a
forall a. a -> Maybe a
Just a
x)
toList :: Trie a -> [(ByteString,a)]
{-# INLINE toList #-}
toList :: Trie a -> [(ByteString, a)]
toList = (ByteString -> a -> (ByteString, a)) -> Trie a -> [(ByteString, a)]
forall a b. (ByteString -> a -> b) -> Trie a -> [b]
toListBy (,)
toListBy :: (ByteString -> a -> b) -> Trie a -> [b]
{-# INLINE toListBy #-}
#if !defined(__GLASGOW_HASKELL__)
toListBy f t = foldrWithKey (((:) .) . f) [] t
#else
toListBy :: (ByteString -> a -> b) -> Trie a -> [b]
toListBy ByteString -> a -> b
f Trie a
t = (forall b. (b -> b -> b) -> b -> b) -> [b]
forall a. (forall b. (a -> b -> b) -> b -> b) -> [a]
build ((ByteString -> a -> b) -> Trie a -> (b -> b -> b) -> b -> b
forall a b c.
(ByteString -> a -> b) -> Trie a -> (b -> c -> c) -> c -> c
toListByFB ByteString -> a -> b
f Trie a
t)
toListByFB :: (ByteString -> a -> b) -> Trie a -> (b -> c -> c) -> c -> c
{-# INLINE [0] toListByFB #-}
toListByFB :: (ByteString -> a -> b) -> Trie a -> (b -> c -> c) -> c -> c
toListByFB ByteString -> a -> b
f Trie a
t b -> c -> c
cons c
nil = (ByteString -> a -> c -> c) -> c -> Trie a -> c
forall a b. (ByteString -> a -> b -> b) -> b -> Trie a -> b
foldrWithKey ((b -> c -> c
cons (b -> c -> c) -> (a -> b) -> a -> c -> c
forall b c a. (b -> c) -> (a -> b) -> a -> c
.) ((a -> b) -> a -> c -> c)
-> (ByteString -> a -> b) -> ByteString -> a -> c -> c
forall b c a. (b -> c) -> (a -> b) -> a -> c
. ByteString -> a -> b
f) c
nil Trie a
t
#endif
elems :: Trie a -> [a]
{-# INLINE elems #-}
#ifdef __GLASGOW_HASKELL__
elems :: Trie a -> [a]
elems Trie a
t = (forall b. (a -> b -> b) -> b -> b) -> [a]
forall a. (forall b. (a -> b -> b) -> b -> b) -> [a]
build (\a -> b -> b
cons b
nil -> (a -> b -> b) -> b -> Trie a -> b
forall (t :: * -> *) a b.
Foldable t =>
(a -> b -> b) -> b -> t a -> b
F.foldr a -> b -> b
cons b
nil Trie a
t)
#else
elems = F.foldr (:) []
#endif
lookupBy_
:: (a -> Trie a -> b)
-> (Trie a -> b)
-> b
-> ByteString -> Trie a -> b
lookupBy_ :: (a -> Trie a -> b)
-> (Trie a -> b) -> b -> ByteString -> Trie a -> b
lookupBy_ a -> Trie a -> b
found Trie a -> b
missing b
clash = ByteString -> Trie a -> b
start
where
start :: ByteString -> Trie a -> b
start ByteString
q t :: Trie a
t@(Branch{}) | ByteString -> Bool
S.null ByteString
q = Trie a -> b
missing Trie a
t
start ByteString
q Trie a
t = ByteString -> Trie a -> b
go ByteString
q Trie a
t
go :: ByteString -> Trie a -> b
go ByteString
_ Trie a
Empty = b
clash
go ByteString
q (Arc ByteString
k Maybe a
mv Trie a
t) =
let (ByteString
_,ByteString
k',ByteString
q') = ByteString -> ByteString -> (ByteString, ByteString, ByteString)
breakMaximalPrefix ByteString
k ByteString
q
in case (ByteString -> Bool
S.null ByteString
k', ByteString -> Bool
S.null ByteString
q') of
(Bool
False, Bool
True) -> Trie a -> b
missing (ByteString -> Maybe a -> Trie a -> Trie a
forall a. ByteString -> Maybe a -> Trie a -> Trie a
Arc ByteString
k' Maybe a
mv Trie a
t)
(Bool
False, Bool
False) -> b
clash
(Bool
True, Bool
True) ->
case Maybe a
mv of
Maybe a
Nothing -> Trie a -> b
missing Trie a
t
Just a
v -> a -> Trie a -> b
found a
v Trie a
t
(Bool
True, Bool
False) -> ByteString -> Trie a -> b
go ByteString
q' Trie a
t
go ByteString
q t_ :: Trie a
t_@(Branch{}) = Trie a -> b
findArc Trie a
t_
where
qh :: Prefix
qh = String -> ByteString -> Prefix
errorLogHead String
"lookupBy_" ByteString
q
findArc :: Trie a -> b
findArc Trie a
Empty = String -> b
forall a. String -> a
impossible String
"lookupBy_"
findArc t :: Trie a
t@(Arc{}) = ByteString -> Trie a -> b
go ByteString
q Trie a
t
findArc (Branch Prefix
p Prefix
m Trie a
l Trie a
r)
| Prefix -> Prefix -> Prefix -> Bool
nomatch Prefix
qh Prefix
p Prefix
m = b
clash
| Prefix -> Prefix -> Bool
zero Prefix
qh Prefix
m = Trie a -> b
findArc Trie a
l
| Bool
otherwise = Trie a -> b
findArc Trie a
r
submap :: ByteString -> Trie a -> Trie a
{-# INLINE submap #-}
submap :: ByteString -> Trie a -> Trie a
submap ByteString
q
| ByteString -> Bool
S.null ByteString
q = Trie a -> Trie a
forall a. a -> a
id
| Bool
otherwise = (a -> Trie a -> Trie a)
-> (Trie a -> Trie a) -> Trie a -> ByteString -> Trie a -> Trie a
forall a b.
(a -> Trie a -> b)
-> (Trie a -> b) -> b -> ByteString -> Trie a -> b
lookupBy_ (ByteString -> Maybe a -> Trie a -> Trie a
forall a. ByteString -> Maybe a -> Trie a -> Trie a
Arc ByteString
q (Maybe a -> Trie a -> Trie a)
-> (a -> Maybe a) -> a -> Trie a -> Trie a
forall b c a. (b -> c) -> (a -> b) -> a -> c
. a -> Maybe a
forall a. a -> Maybe a
Just) (ByteString -> Trie a -> Trie a
forall a. ByteString -> Trie a -> Trie a
prependNN ByteString
q) Trie a
forall a. Trie a
empty ByteString
q
match_ :: Trie a -> ByteString -> Maybe (Int, a)
match_ :: Trie a -> ByteString -> Maybe (Int, a)
match_ = (ByteString -> Trie a -> Maybe (Int, a))
-> Trie a -> ByteString -> Maybe (Int, a)
forall a b c. (a -> b -> c) -> b -> a -> c
flip ByteString -> Trie a -> Maybe (Int, a)
forall b. ByteString -> Trie b -> Maybe (Int, b)
start
where
start :: ByteString -> Trie b -> Maybe (Int, b)
start ByteString
q (Branch{}) | ByteString -> Bool
S.null ByteString
q = Maybe (Int, b)
forall a. Maybe a
Nothing
start ByteString
q Trie b
t = Int -> ByteString -> Trie b -> Maybe (Int, b)
forall b. Int -> ByteString -> Trie b -> Maybe (Int, b)
match1 Int
0 ByteString
q Trie b
t
match1 :: Int -> ByteString -> Trie b -> Maybe (Int, b)
match1 Int
_ ByteString
_ Trie b
Empty = Maybe (Int, b)
forall a. Maybe a
Nothing
match1 Int
n ByteString
q (Arc ByteString
k Maybe b
mv Trie b
t) =
let (ByteString
p,ByteString
k',ByteString
q') = ByteString -> ByteString -> (ByteString, ByteString, ByteString)
breakMaximalPrefix ByteString
k ByteString
q
!n' :: Int
n' = Int
n Int -> Int -> Int
forall a. Num a => a -> a -> a
+ ByteString -> Int
S.length ByteString
p
in case (ByteString -> Bool
S.null ByteString
k', ByteString -> Bool
S.null ByteString
q') of
(Bool
False, Bool
_) -> Maybe (Int, b)
forall a. Maybe a
Nothing
(Bool
True, Bool
True) -> (,) Int
n' (b -> (Int, b)) -> Maybe b -> Maybe (Int, b)
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> Maybe b
mv
(Bool
True, Bool
False) ->
case Maybe b
mv of
Maybe b
Nothing -> Int -> ByteString -> Trie b -> Maybe (Int, b)
match1 Int
n' ByteString
q' Trie b
t
Just b
v -> Int -> b -> Int -> ByteString -> Trie b -> Maybe (Int, b)
forall b. Int -> b -> Int -> ByteString -> Trie b -> Maybe (Int, b)
matchN Int
n' b
v Int
n' ByteString
q' Trie b
t
match1 Int
n ByteString
q t_ :: Trie b
t_@(Branch{}) = Trie b -> Maybe (Int, b)
findArc Trie b
t_
where
qh :: Prefix
qh = String -> ByteString -> Prefix
errorLogHead String
"match_" ByteString
q
findArc :: Trie b -> Maybe (Int, b)
findArc Trie b
Empty = String -> Maybe (Int, b)
forall a. String -> a
impossible String
"match_"
findArc t :: Trie b
t@(Arc{}) = Int -> ByteString -> Trie b -> Maybe (Int, b)
match1 Int
n ByteString
q Trie b
t
findArc (Branch Prefix
p Prefix
m Trie b
l Trie b
r)
| Prefix -> Prefix -> Prefix -> Bool
nomatch Prefix
qh Prefix
p Prefix
m = Maybe (Int, b)
forall a. Maybe a
Nothing
| Prefix -> Prefix -> Bool
zero Prefix
qh Prefix
m = Trie b -> Maybe (Int, b)
findArc Trie b
l
| Bool
otherwise = Trie b -> Maybe (Int, b)
findArc Trie b
r
matchN :: Int -> b -> Int -> ByteString -> Trie b -> Maybe (Int, b)
matchN Int
n0 b
v0 Int
_ ByteString
_ Trie b
Empty = (Int, b) -> Maybe (Int, b)
forall a. a -> Maybe a
Just (Int
n0,b
v0)
matchN Int
n0 b
v0 Int
n ByteString
q (Arc ByteString
k Maybe b
mv Trie b
t) =
let (ByteString
p,ByteString
k',ByteString
q') = ByteString -> ByteString -> (ByteString, ByteString, ByteString)
breakMaximalPrefix ByteString
k ByteString
q
!n' :: Int
n' = Int
n Int -> Int -> Int
forall a. Num a => a -> a -> a
+ ByteString -> Int
S.length ByteString
p
in case (ByteString -> Bool
S.null ByteString
k', ByteString -> Bool
S.null ByteString
q') of
(Bool
False, Bool
_) -> (Int, b) -> Maybe (Int, b)
forall a. a -> Maybe a
Just (Int
n0,b
v0)
(Bool
True, Bool
True) ->
case Maybe b
mv of
Maybe b
Nothing -> (Int, b) -> Maybe (Int, b)
forall a. a -> Maybe a
Just (Int
n0,b
v0)
Just b
v -> (Int, b) -> Maybe (Int, b)
forall a. a -> Maybe a
Just (Int
n',b
v)
(Bool
True, Bool
False) ->
case Maybe b
mv of
Maybe b
Nothing -> Int -> b -> Int -> ByteString -> Trie b -> Maybe (Int, b)
matchN Int
n0 b
v0 Int
n' ByteString
q' Trie b
t
Just b
v -> Int -> b -> Int -> ByteString -> Trie b -> Maybe (Int, b)
matchN Int
n' b
v Int
n' ByteString
q' Trie b
t
matchN Int
n0 b
v0 Int
n ByteString
q t_ :: Trie b
t_@(Branch{}) = Trie b -> Maybe (Int, b)
findArc Trie b
t_
where
qh :: Prefix
qh = String -> ByteString -> Prefix
errorLogHead String
"match_" ByteString
q
findArc :: Trie b -> Maybe (Int, b)
findArc Trie b
Empty = String -> Maybe (Int, b)
forall a. String -> a
impossible String
"match_"
findArc t :: Trie b
t@(Arc{}) = Int -> b -> Int -> ByteString -> Trie b -> Maybe (Int, b)
matchN Int
n0 b
v0 Int
n ByteString
q Trie b
t
findArc (Branch Prefix
p Prefix
m Trie b
l Trie b
r)
| Prefix -> Prefix -> Prefix -> Bool
nomatch Prefix
qh Prefix
p Prefix
m = (Int, b) -> Maybe (Int, b)
forall a. a -> Maybe a
Just (Int
n0,b
v0)
| Prefix -> Prefix -> Bool
zero Prefix
qh Prefix
m = Trie b -> Maybe (Int, b)
findArc Trie b
l
| Bool
otherwise = Trie b -> Maybe (Int, b)
findArc Trie b
r
matches_ :: Trie a -> ByteString -> [(Int,a)]
matches_ :: Trie a -> ByteString -> [(Int, a)]
matches_ Trie a
t ByteString
q =
#if !defined(__GLASGOW_HASKELL__)
matchFB_ t q (((:) .) . (,)) []
#else
(forall b. ((Int, a) -> b -> b) -> b -> b) -> [(Int, a)]
forall a. (forall b. (a -> b -> b) -> b -> b) -> [a]
build (\(Int, a) -> b -> b
cons b
nil -> Trie a -> ByteString -> (Int -> a -> b -> b) -> b -> b
forall a r. Trie a -> ByteString -> (Int -> a -> r -> r) -> r -> r
matchFB_ Trie a
t ByteString
q (((Int, a) -> b -> b
cons ((Int, a) -> b -> b) -> (a -> (Int, a)) -> a -> b -> b
forall b c a. (b -> c) -> (a -> b) -> a -> c
.) ((a -> (Int, a)) -> a -> b -> b)
-> (Int -> a -> (Int, a)) -> Int -> a -> b -> b
forall b c a. (b -> c) -> (a -> b) -> a -> c
. (,)) b
nil)
{-# INLINE matches_ #-}
#endif
matchFB_ :: Trie a -> ByteString -> (Int -> a -> r -> r) -> r -> r
matchFB_ :: Trie a -> ByteString -> (Int -> a -> r -> r) -> r -> r
matchFB_ = \Trie a
t ByteString
q Int -> a -> r -> r
cons r
nil -> (Int -> a -> r -> r) -> ByteString -> Trie a -> r -> r
forall t a. (Int -> t -> a -> a) -> ByteString -> Trie t -> a -> a
matchFB_' Int -> a -> r -> r
cons ByteString
q Trie a
t r
nil
where
matchFB_' :: (Int -> t -> a -> a) -> ByteString -> Trie t -> a -> a
matchFB_' Int -> t -> a -> a
cons = ByteString -> Trie t -> a -> a
start
where
start :: ByteString -> Trie t -> a -> a
start ByteString
q (Branch{}) | ByteString -> Bool
S.null ByteString
q = a -> a
forall a. a -> a
id
start ByteString
q Trie t
t = Int -> ByteString -> Trie t -> a -> a
go Int
0 ByteString
q Trie t
t
go :: Int -> ByteString -> Trie t -> a -> a
go Int
_ ByteString
_ Trie t
Empty = a -> a
forall a. a -> a
id
go Int
n ByteString
q (Arc ByteString
k Maybe t
mv Trie t
t) =
let (ByteString
p,ByteString
k',ByteString
q') = ByteString -> ByteString -> (ByteString, ByteString, ByteString)
breakMaximalPrefix ByteString
k ByteString
q
!n' :: Int
n' = Int
n Int -> Int -> Int
forall a. Num a => a -> a -> a
+ ByteString -> Int
S.length ByteString
p
in if ByteString -> Bool
S.null ByteString
k'
then
case Maybe t
mv of { Maybe t
Nothing -> a -> a
forall a. a -> a
id; Just t
v -> Int -> t -> a -> a
cons Int
n' t
v}
(a -> a) -> (a -> a) -> a -> a
forall b c a. (b -> c) -> (a -> b) -> a -> c
.
if ByteString -> Bool
S.null ByteString
q' then a -> a
forall a. a -> a
id else Int -> ByteString -> Trie t -> a -> a
go Int
n' ByteString
q' Trie t
t
else a -> a
forall a. a -> a
id
go Int
n ByteString
q t_ :: Trie t
t_@(Branch{}) = Trie t -> a -> a
findArc Trie t
t_
where
qh :: Prefix
qh = String -> ByteString -> Prefix
errorLogHead String
"matches_" ByteString
q
findArc :: Trie t -> a -> a
findArc Trie t
Empty = String -> a -> a
forall a. String -> a
impossible String
"matches_"
findArc t :: Trie t
t@(Arc{}) = Int -> ByteString -> Trie t -> a -> a
go Int
n ByteString
q Trie t
t
findArc (Branch Prefix
p Prefix
m Trie t
l Trie t
r)
| Prefix -> Prefix -> Prefix -> Bool
nomatch Prefix
qh Prefix
p Prefix
m = a -> a
forall a. a -> a
id
| Prefix -> Prefix -> Bool
zero Prefix
qh Prefix
m = Trie t -> a -> a
findArc Trie t
l
| Bool
otherwise = Trie t -> a -> a
findArc Trie t
r
alterBy :: (ByteString -> a -> Maybe a -> Maybe a)
-> ByteString -> a -> Trie a -> Trie a
alterBy :: (ByteString -> a -> Maybe a -> Maybe a)
-> ByteString -> a -> Trie a -> Trie a
alterBy ByteString -> a -> Maybe a -> Maybe a
f ByteString
q a
x = (Maybe a -> Trie a -> (Maybe a, Trie a))
-> ByteString -> Trie a -> Trie a
forall a.
(Maybe a -> Trie a -> (Maybe a, Trie a))
-> ByteString -> Trie a -> Trie a
alterBy_ (\Maybe a
mv Trie a
t -> (ByteString -> a -> Maybe a -> Maybe a
f ByteString
q a
x Maybe a
mv, Trie a
t)) ByteString
q
alterBy_
:: (Maybe a -> Trie a -> (Maybe a, Trie a))
-> ByteString -> Trie a -> Trie a
alterBy_ :: (Maybe a -> Trie a -> (Maybe a, Trie a))
-> ByteString -> Trie a -> Trie a
alterBy_ Maybe a -> Trie a -> (Maybe a, Trie a)
f = ByteString -> Trie a -> Trie a
start
where
start :: ByteString -> Trie a -> Trie a
start ByteString
q Trie a
t | Bool -> Bool
not (ByteString -> Bool
S.null ByteString
q) = ByteString -> Trie a -> Trie a
go ByteString
q Trie a
t
start ByteString
_ (Arc ByteString
k Maybe a
mv Trie a
s) | ByteString -> Bool
S.null ByteString
k = Maybe a -> Trie a -> Trie a
forall a. Maybe a -> Trie a -> Trie a
mayEpsilon (Maybe a -> Trie a -> Trie a) -> (Maybe a, Trie a) -> Trie a
forall a b c. (a -> b -> c) -> (a, b) -> c
$$ Maybe a -> Trie a -> (Maybe a, Trie a)
f Maybe a
mv Trie a
s
start ByteString
_ Trie a
t = Maybe a -> Trie a -> Trie a
forall a. Maybe a -> Trie a -> Trie a
mayEpsilon (Maybe a -> Trie a -> Trie a) -> (Maybe a, Trie a) -> Trie a
forall a b c. (a -> b -> c) -> (a, b) -> c
$$ Maybe a -> Trie a -> (Maybe a, Trie a)
f Maybe a
forall a. Maybe a
Nothing Trie a
t
nothing :: ByteString -> Trie a
nothing ByteString
q = ByteString -> Maybe a -> Trie a -> Trie a
forall a. ByteString -> Maybe a -> Trie a -> Trie a
arcNN ByteString
q (Maybe a -> Trie a -> Trie a) -> (Maybe a, Trie a) -> Trie a
forall a b c. (a -> b -> c) -> (a, b) -> c
$$ Maybe a -> Trie a -> (Maybe a, Trie a)
f Maybe a
forall a. Maybe a
Nothing Trie a
forall a. Trie a
Empty
go :: ByteString -> Trie a -> Trie a
go ByteString
q Trie a
Empty = ByteString -> Trie a
nothing ByteString
q
go ByteString
q t :: Trie a
t@(Branch Prefix
p Prefix
m Trie a
l Trie a
r)
| Prefix -> Prefix -> Prefix -> Bool
nomatch Prefix
qh Prefix
p Prefix
m =
case ByteString -> Trie a
nothing ByteString
q of
Trie a
Empty -> Trie a
t
Trie a
s -> Prefix -> Trie a -> Prefix -> Trie a -> Trie a
forall a. Prefix -> Trie a -> Prefix -> Trie a -> Trie a
graft Prefix
p Trie a
t Prefix
qh Trie a
s
| Prefix -> Prefix -> Bool
zero Prefix
qh Prefix
m = Prefix -> Prefix -> Trie a -> Trie a -> Trie a
forall a. Prefix -> Prefix -> Trie a -> Trie a -> Trie a
branchL Prefix
p Prefix
m (ByteString -> Trie a -> Trie a
go ByteString
q Trie a
l) Trie a
r
| Bool
otherwise = Prefix -> Prefix -> Trie a -> Trie a -> Trie a
forall a. Prefix -> Prefix -> Trie a -> Trie a -> Trie a
branchR Prefix
p Prefix
m Trie a
l (ByteString -> Trie a -> Trie a
go ByteString
q Trie a
r)
where qh :: Prefix
qh = String -> ByteString -> Prefix
errorLogHead String
"alterBy_" ByteString
q
go ByteString
q t :: Trie a
t@(Arc ByteString
k Maybe a
mv Trie a
s) =
let (ByteString
p,ByteString
k',ByteString
q') = ByteString -> ByteString -> (ByteString, ByteString, ByteString)
breakMaximalPrefix ByteString
k ByteString
q in
case (ByteString -> Bool
S.null ByteString
k', ByteString -> Bool
S.null ByteString
q') of
(Bool
False, Bool
True) ->
ByteString -> Maybe a -> Trie a -> Trie a
forall a. ByteString -> Maybe a -> Trie a -> Trie a
arcNN ByteString
p (Maybe a -> Trie a -> Trie a) -> (Maybe a, Trie a) -> Trie a
forall a b c. (a -> b -> c) -> (a, b) -> c
$$ Maybe a -> Trie a -> (Maybe a, Trie a)
f Maybe a
forall a. Maybe a
Nothing (ByteString -> Maybe a -> Trie a -> Trie a
forall a. ByteString -> Maybe a -> Trie a -> Trie a
Arc ByteString
k' Maybe a
mv Trie a
s)
(Bool
False, Bool
False) ->
case ByteString -> Trie a
nothing ByteString
q' of
Trie a
Empty -> Trie a
t
Branch{} -> String -> Trie a
forall a. String -> a
impossible String
"alterBy_"
l :: Trie a
l@(Arc{}) ->
(if ByteString -> Bool
S.null ByteString
p then Trie a -> Trie a
forall a. a -> a
id else ByteString -> Maybe a -> Trie a -> Trie a
forall a. ByteString -> Maybe a -> Trie a -> Trie a
Arc ByteString
p Maybe a
forall a. Maybe a
Nothing)
(Trie a -> Trie a) -> Trie a -> Trie a
forall a b. (a -> b) -> a -> b
$ Prefix -> Trie a -> Prefix -> Trie a -> Trie a
forall a. Prefix -> Trie a -> Prefix -> Trie a -> Trie a
graft (ByteString -> Prefix
arcPrefix ByteString
q') Trie a
l (ByteString -> Prefix
arcPrefix ByteString
k') (ByteString -> Maybe a -> Trie a -> Trie a
forall a. ByteString -> Maybe a -> Trie a -> Trie a
Arc ByteString
k' Maybe a
mv Trie a
s)
(Bool
True, Bool
True) -> ByteString -> Maybe a -> Trie a -> Trie a
forall a. ByteString -> Maybe a -> Trie a -> Trie a
arcNN ByteString
k (Maybe a -> Trie a -> Trie a) -> (Maybe a, Trie a) -> Trie a
forall a b c. (a -> b -> c) -> (a, b) -> c
$$ Maybe a -> Trie a -> (Maybe a, Trie a)
f Maybe a
mv Trie a
s
(Bool
True, Bool
False) -> ByteString -> Maybe a -> Trie a -> Trie a
forall a. ByteString -> Maybe a -> Trie a -> Trie a
arcNN ByteString
k Maybe a
mv (ByteString -> Trie a -> Trie a
go ByteString
q' Trie a
s)
adjust :: (a -> a) -> ByteString -> Trie a -> Trie a
adjust :: (a -> a) -> ByteString -> Trie a -> Trie a
adjust a -> a
f = ByteString -> Trie a -> Trie a
start
where
start :: ByteString -> Trie a -> Trie a
start ByteString
q Trie a
t | Bool -> Bool
not (ByteString -> Bool
S.null ByteString
q) = ByteString -> Trie a -> Trie a
go ByteString
q Trie a
t
start ByteString
_ (Arc ByteString
k (Just a
v) Trie a
t) | ByteString -> Bool
S.null ByteString
k = ByteString -> Maybe a -> Trie a -> Trie a
forall a. ByteString -> Maybe a -> Trie a -> Trie a
Arc ByteString
k (a -> Maybe a
forall a. a -> Maybe a
Just (a -> a
f a
v)) Trie a
t
start ByteString
_ Trie a
t = Trie a
t
go :: ByteString -> Trie a -> Trie a
go ByteString
_ Trie a
Empty = Trie a
forall a. Trie a
Empty
go ByteString
q t :: Trie a
t@(Branch Prefix
p Prefix
m Trie a
l Trie a
r)
| Prefix -> Prefix -> Prefix -> Bool
nomatch Prefix
qh Prefix
p Prefix
m = Trie a
t
| Prefix -> Prefix -> Bool
zero Prefix
qh Prefix
m = Prefix -> Prefix -> Trie a -> Trie a -> Trie a
forall a. Prefix -> Prefix -> Trie a -> Trie a -> Trie a
Branch Prefix
p Prefix
m (ByteString -> Trie a -> Trie a
go ByteString
q Trie a
l) Trie a
r
| Bool
otherwise = Prefix -> Prefix -> Trie a -> Trie a -> Trie a
forall a. Prefix -> Prefix -> Trie a -> Trie a -> Trie a
Branch Prefix
p Prefix
m Trie a
l (ByteString -> Trie a -> Trie a
go ByteString
q Trie a
r)
where qh :: Prefix
qh = String -> ByteString -> Prefix
errorLogHead String
"adjust" ByteString
q
go ByteString
q t :: Trie a
t@(Arc ByteString
k Maybe a
mv Trie a
s) =
let (ByteString
_,ByteString
k',ByteString
q') = ByteString -> ByteString -> (ByteString, ByteString, ByteString)
breakMaximalPrefix ByteString
k ByteString
q in
case (ByteString -> Bool
S.null ByteString
k', ByteString -> Bool
S.null ByteString
q') of
(Bool
False, Bool
_) -> Trie a
t
(Bool
True, Bool
True) -> ByteString -> Maybe a -> Trie a -> Trie a
forall a. ByteString -> Maybe a -> Trie a -> Trie a
Arc ByteString
k (a -> a
f (a -> a) -> Maybe a -> Maybe a
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> Maybe a
mv) Trie a
s
(Bool
True, Bool
False) -> ByteString -> Maybe a -> Trie a -> Trie a
forall a. ByteString -> Maybe a -> Trie a -> Trie a
Arc ByteString
k Maybe a
mv (ByteString -> Trie a -> Trie a
go ByteString
q' Trie a
s)
wip_unionWith :: (a -> a -> a) -> Trie a -> Trie a -> Trie a
wip_unionWith :: (a -> a -> a) -> Trie a -> Trie a -> Trie a
wip_unionWith a -> a -> a
f = Trie a -> Trie a -> Trie a
start
where
start :: Trie a -> Trie a -> Trie a
start (Arc ByteString
k0 (Just a
v0) Trie a
s0) (Arc ByteString
k1 (Just a
v1) Trie a
s1) | ByteString -> Bool
S.null ByteString
k0 Bool -> Bool -> Bool
&& ByteString -> Bool
S.null ByteString
k1
= a -> Trie a -> Trie a
forall a. a -> Trie a -> Trie a
epsilon (a -> a -> a
f a
v0 a
v1) (Trie a -> Trie a -> Trie a
go Trie a
s0 Trie a
s1)
start (Arc ByteString
k0 (Just a
v0) Trie a
s0) Trie a
t1 | ByteString -> Bool
S.null ByteString
k0 = a -> Trie a -> Trie a
forall a. a -> Trie a -> Trie a
epsilon a
v0 (Trie a -> Trie a -> Trie a
go Trie a
s0 Trie a
t1)
start Trie a
t0 (Arc ByteString
k1 (Just a
v1) Trie a
s1) | ByteString -> Bool
S.null ByteString
k1 = a -> Trie a -> Trie a
forall a. a -> Trie a -> Trie a
epsilon a
v1 (Trie a -> Trie a -> Trie a
go Trie a
t0 Trie a
s1)
start Trie a
t0 Trie a
t1 = Trie a -> Trie a -> Trie a
go Trie a
t0 Trie a
t1
go :: Trie a -> Trie a -> Trie a
go Trie a
Empty Trie a
t1 = Trie a
t1
go Trie a
t0 Trie a
Empty = Trie a
t0
go t0 :: Trie a
t0@(Branch Prefix
p0 Prefix
m0 Trie a
l0 Trie a
r0)
t1 :: Trie a
t1@(Branch Prefix
p1 Prefix
m1 Trie a
l1 Trie a
r1)
| Prefix -> Prefix -> Bool
shorter Prefix
m0 Prefix
m1 = Trie a
union0
| Prefix -> Prefix -> Bool
shorter Prefix
m1 Prefix
m0 = Trie a
union1
| Prefix
p0 Prefix -> Prefix -> Bool
forall a. Eq a => a -> a -> Bool
== Prefix
p1 = Prefix -> Prefix -> Trie a -> Trie a -> Trie a
forall a. Prefix -> Prefix -> Trie a -> Trie a -> Trie a
Branch Prefix
p0 Prefix
m0 (Trie a -> Trie a -> Trie a
go Trie a
l0 Trie a
l1) (Trie a -> Trie a -> Trie a
go Trie a
r0 Trie a
r1)
| Bool
otherwise = Prefix -> Trie a -> Prefix -> Trie a -> Trie a
forall a. Prefix -> Trie a -> Prefix -> Trie a -> Trie a
graft Prefix
p0 Trie a
t0 Prefix
p1 Trie a
t1
where
union0 :: Trie a
union0 | Prefix -> Prefix -> Prefix -> Bool
nomatch Prefix
p1 Prefix
p0 Prefix
m0 = Prefix -> Trie a -> Prefix -> Trie a -> Trie a
forall a. Prefix -> Trie a -> Prefix -> Trie a -> Trie a
graft Prefix
p0 Trie a
t0 Prefix
p1 Trie a
t1
| Prefix -> Prefix -> Bool
zero Prefix
p1 Prefix
m0 = Prefix -> Prefix -> Trie a -> Trie a -> Trie a
forall a. Prefix -> Prefix -> Trie a -> Trie a -> Trie a
Branch Prefix
p0 Prefix
m0 (Trie a -> Trie a -> Trie a
go Trie a
l0 Trie a
t1) Trie a
r0
| Bool
otherwise = Prefix -> Prefix -> Trie a -> Trie a -> Trie a
forall a. Prefix -> Prefix -> Trie a -> Trie a -> Trie a
Branch Prefix
p0 Prefix
m0 Trie a
l0 (Trie a -> Trie a -> Trie a
go Trie a
r0 Trie a
t1)
union1 :: Trie a
union1 | Prefix -> Prefix -> Prefix -> Bool
nomatch Prefix
p0 Prefix
p1 Prefix
m1 = Prefix -> Trie a -> Prefix -> Trie a -> Trie a
forall a. Prefix -> Trie a -> Prefix -> Trie a -> Trie a
graft Prefix
p0 Trie a
t0 Prefix
p1 Trie a
t1
| Prefix -> Prefix -> Bool
zero Prefix
p0 Prefix
m1 = Prefix -> Prefix -> Trie a -> Trie a -> Trie a
forall a. Prefix -> Prefix -> Trie a -> Trie a -> Trie a
Branch Prefix
p1 Prefix
m1 (Trie a -> Trie a -> Trie a
go Trie a
t0 Trie a
l1) Trie a
r1
| Bool
otherwise = Prefix -> Prefix -> Trie a -> Trie a -> Trie a
forall a. Prefix -> Prefix -> Trie a -> Trie a -> Trie a
Branch Prefix
p1 Prefix
m1 Trie a
l1 (Trie a -> Trie a -> Trie a
go Trie a
t0 Trie a
r1)
go t0 :: Trie a
t0@(Arc ByteString
k0 Maybe a
mv0 Trie a
s0)
t1 :: Trie a
t1@(Arc ByteString
k1 Maybe a
mv1 Trie a
s1)
= ByteString
-> Trie a
-> ByteString
-> Trie a
-> (ByteString -> ByteString -> ByteString -> Trie a)
-> Trie a
forall a.
ByteString
-> Trie a
-> ByteString
-> Trie a
-> (ByteString -> ByteString -> ByteString -> Trie a)
-> Trie a
arcMerge ByteString
k0 Trie a
t0 ByteString
k1 Trie a
t1 ((ByteString -> ByteString -> ByteString -> Trie a) -> Trie a)
-> (ByteString -> ByteString -> ByteString -> Trie a) -> Trie a
forall a b. (a -> b) -> a -> b
$ \ ByteString
pre ByteString
k0' ByteString
k1' ->
let {-# INLINE t0' #-}
t0' :: Trie a
t0' = ByteString -> Maybe a -> Trie a -> Trie a
forall a. ByteString -> Maybe a -> Trie a -> Trie a
Arc ByteString
k0' Maybe a
mv0 Trie a
s0
{-# INLINE t1' #-}
t1' :: Trie a
t1' = ByteString -> Maybe a -> Trie a -> Trie a
forall a. ByteString -> Maybe a -> Trie a -> Trie a
Arc ByteString
k1' Maybe a
mv1 Trie a
s1
in
case (ByteString -> Bool
S.null ByteString
k0', ByteString -> Bool
S.null ByteString
k1') of
(Bool
True, Bool
True) -> ByteString -> Maybe a -> Trie a -> Trie a
forall a. ByteString -> Maybe a -> Trie a -> Trie a
arcNN ByteString
pre ((a -> a -> Maybe a) -> Maybe a -> Maybe a -> Maybe a
forall a. (a -> a -> Maybe a) -> Maybe a -> Maybe a -> Maybe a
mergeMaybe (\a
v0 a
v1 -> a -> Maybe a
forall a. a -> Maybe a
Just (a -> a -> a
f a
v0 a
v1)) Maybe a
mv0 Maybe a
mv1) (Trie a -> Trie a -> Trie a
go Trie a
s0 Trie a
s1)
(Bool
True, Bool
False) -> ByteString -> Maybe a -> Trie a -> Trie a
forall a. ByteString -> Maybe a -> Trie a -> Trie a
Arc ByteString
pre Maybe a
mv0 (Trie a -> Trie a -> Trie a
go Trie a
s0 Trie a
t1')
(Bool
False,Bool
True) -> ByteString -> Maybe a -> Trie a -> Trie a
forall a. ByteString -> Maybe a -> Trie a -> Trie a
Arc ByteString
pre Maybe a
mv1 (Trie a -> Trie a -> Trie a
go Trie a
t0' Trie a
s1)
(Bool
False,Bool
False) -> ByteString
-> ByteString -> Trie a -> ByteString -> Trie a -> Trie a
forall a.
ByteString
-> ByteString -> Trie a -> ByteString -> Trie a -> Trie a
wye ByteString
pre ByteString
k0' Trie a
t0' ByteString
k1' Trie a
t1'
go t0 :: Trie a
t0@(Arc ByteString
k0 Maybe a
_ Trie a
_)
t1 :: Trie a
t1@(Branch Prefix
p1 Prefix
m1 Trie a
l Trie a
r)
| Prefix -> Prefix -> Prefix -> Bool
nomatch Prefix
p0 Prefix
p1 Prefix
m1 = Prefix -> Trie a -> Prefix -> Trie a -> Trie a
forall a. Prefix -> Trie a -> Prefix -> Trie a -> Trie a
graft Prefix
p1 Trie a
t1 Prefix
p0 Trie a
t0
| Prefix -> Prefix -> Bool
zero Prefix
p0 Prefix
m1 = Prefix -> Prefix -> Trie a -> Trie a -> Trie a
forall a. Prefix -> Prefix -> Trie a -> Trie a -> Trie a
Branch Prefix
p1 Prefix
m1 (Trie a -> Trie a -> Trie a
go Trie a
t0 Trie a
l) Trie a
r
| Bool
otherwise = Prefix -> Prefix -> Trie a -> Trie a -> Trie a
forall a. Prefix -> Prefix -> Trie a -> Trie a -> Trie a
Branch Prefix
p1 Prefix
m1 Trie a
l (Trie a -> Trie a -> Trie a
go Trie a
t0 Trie a
r)
where p0 :: Prefix
p0 = ByteString -> Prefix
arcPrefix ByteString
k0
go t0 :: Trie a
t0@(Branch Prefix
p0 Prefix
m0 Trie a
l Trie a
r)
t1 :: Trie a
t1@(Arc ByteString
k1 Maybe a
_ Trie a
_)
| Prefix -> Prefix -> Prefix -> Bool
nomatch Prefix
p1 Prefix
p0 Prefix
m0 = Prefix -> Trie a -> Prefix -> Trie a -> Trie a
forall a. Prefix -> Trie a -> Prefix -> Trie a -> Trie a
graft Prefix
p0 Trie a
t0 Prefix
p1 Trie a
t1
| Prefix -> Prefix -> Bool
zero Prefix
p1 Prefix
m0 = Prefix -> Prefix -> Trie a -> Trie a -> Trie a
forall a. Prefix -> Prefix -> Trie a -> Trie a -> Trie a
Branch Prefix
p0 Prefix
m0 (Trie a -> Trie a -> Trie a
go Trie a
l Trie a
t1) Trie a
r
| Bool
otherwise = Prefix -> Prefix -> Trie a -> Trie a -> Trie a
forall a. Prefix -> Prefix -> Trie a -> Trie a -> Trie a
Branch Prefix
p0 Prefix
m0 Trie a
l (Trie a -> Trie a -> Trie a
go Trie a
r Trie a
t1)
where p1 :: Prefix
p1 = ByteString -> Prefix
arcPrefix ByteString
k1
mergeBy :: (a -> a -> Maybe a) -> Trie a -> Trie a -> Trie a
mergeBy :: (a -> a -> Maybe a) -> Trie a -> Trie a -> Trie a
mergeBy a -> a -> Maybe a
f = Trie a -> Trie a -> Trie a
start
where
start :: Trie a -> Trie a -> Trie a
start (Arc ByteString
k0 (Just a
v0) Trie a
s0) (Arc ByteString
k1 (Just a
v1) Trie a
s1) | ByteString -> Bool
S.null ByteString
k0 Bool -> Bool -> Bool
&& ByteString -> Bool
S.null ByteString
k1
= Maybe a -> Trie a -> Trie a
forall a. Maybe a -> Trie a -> Trie a
mayEpsilon (a -> a -> Maybe a
f a
v0 a
v1) (Trie a -> Trie a -> Trie a
go Trie a
s0 Trie a
s1)
start (Arc ByteString
k0 (Just a
v0) Trie a
s0) Trie a
t1 | ByteString -> Bool
S.null ByteString
k0 = a -> Trie a -> Trie a
forall a. a -> Trie a -> Trie a
epsilon a
v0 (Trie a -> Trie a -> Trie a
go Trie a
s0 Trie a
t1)
start Trie a
t0 (Arc ByteString
k1 (Just a
v1) Trie a
s1) | ByteString -> Bool
S.null ByteString
k1 = a -> Trie a -> Trie a
forall a. a -> Trie a -> Trie a
epsilon a
v1 (Trie a -> Trie a -> Trie a
go Trie a
t0 Trie a
s1)
start Trie a
t0 Trie a
t1 = Trie a -> Trie a -> Trie a
go Trie a
t0 Trie a
t1
go :: Trie a -> Trie a -> Trie a
go Trie a
Empty Trie a
t1 = Trie a
t1
go Trie a
t0 Trie a
Empty = Trie a
t0
go t0 :: Trie a
t0@(Branch Prefix
p0 Prefix
m0 Trie a
l0 Trie a
r0)
t1 :: Trie a
t1@(Branch Prefix
p1 Prefix
m1 Trie a
l1 Trie a
r1)
| Prefix -> Prefix -> Bool
shorter Prefix
m0 Prefix
m1 = Trie a
union0
| Prefix -> Prefix -> Bool
shorter Prefix
m1 Prefix
m0 = Trie a
union1
| Prefix
p0 Prefix -> Prefix -> Bool
forall a. Eq a => a -> a -> Bool
== Prefix
p1 = Prefix -> Prefix -> Trie a -> Trie a -> Trie a
forall a. Prefix -> Prefix -> Trie a -> Trie a -> Trie a
branch Prefix
p0 Prefix
m0 (Trie a -> Trie a -> Trie a
go Trie a
l0 Trie a
l1) (Trie a -> Trie a -> Trie a
go Trie a
r0 Trie a
r1)
| Bool
otherwise = Prefix -> Trie a -> Prefix -> Trie a -> Trie a
forall a. Prefix -> Trie a -> Prefix -> Trie a -> Trie a
graft Prefix
p0 Trie a
t0 Prefix
p1 Trie a
t1
where
union0 :: Trie a
union0 | Prefix -> Prefix -> Prefix -> Bool
nomatch Prefix
p1 Prefix
p0 Prefix
m0 = Prefix -> Trie a -> Prefix -> Trie a -> Trie a
forall a. Prefix -> Trie a -> Prefix -> Trie a -> Trie a
graft Prefix
p0 Trie a
t0 Prefix
p1 Trie a
t1
| Prefix -> Prefix -> Bool
zero Prefix
p1 Prefix
m0 = Prefix -> Prefix -> Trie a -> Trie a -> Trie a
forall a. Prefix -> Prefix -> Trie a -> Trie a -> Trie a
branchL Prefix
p0 Prefix
m0 (Trie a -> Trie a -> Trie a
go Trie a
l0 Trie a
t1) Trie a
r0
| Bool
otherwise = Prefix -> Prefix -> Trie a -> Trie a -> Trie a
forall a. Prefix -> Prefix -> Trie a -> Trie a -> Trie a
branchR Prefix
p0 Prefix
m0 Trie a
l0 (Trie a -> Trie a -> Trie a
go Trie a
r0 Trie a
t1)
union1 :: Trie a
union1 | Prefix -> Prefix -> Prefix -> Bool
nomatch Prefix
p0 Prefix
p1 Prefix
m1 = Prefix -> Trie a -> Prefix -> Trie a -> Trie a
forall a. Prefix -> Trie a -> Prefix -> Trie a -> Trie a
graft Prefix
p0 Trie a
t0 Prefix
p1 Trie a
t1
| Prefix -> Prefix -> Bool
zero Prefix
p0 Prefix
m1 = Prefix -> Prefix -> Trie a -> Trie a -> Trie a
forall a. Prefix -> Prefix -> Trie a -> Trie a -> Trie a
branchL Prefix
p1 Prefix
m1 (Trie a -> Trie a -> Trie a
go Trie a
t0 Trie a
l1) Trie a
r1
| Bool
otherwise = Prefix -> Prefix -> Trie a -> Trie a -> Trie a
forall a. Prefix -> Prefix -> Trie a -> Trie a -> Trie a
branchR Prefix
p1 Prefix
m1 Trie a
l1 (Trie a -> Trie a -> Trie a
go Trie a
t0 Trie a
r1)
go t0 :: Trie a
t0@(Arc ByteString
k0 Maybe a
mv0 Trie a
s0)
t1 :: Trie a
t1@(Arc ByteString
k1 Maybe a
mv1 Trie a
s1)
= ByteString
-> Trie a
-> ByteString
-> Trie a
-> (ByteString -> ByteString -> ByteString -> Trie a)
-> Trie a
forall a.
ByteString
-> Trie a
-> ByteString
-> Trie a
-> (ByteString -> ByteString -> ByteString -> Trie a)
-> Trie a
arcMerge ByteString
k0 Trie a
t0 ByteString
k1 Trie a
t1 ((ByteString -> ByteString -> ByteString -> Trie a) -> Trie a)
-> (ByteString -> ByteString -> ByteString -> Trie a) -> Trie a
forall a b. (a -> b) -> a -> b
$ \ ByteString
pre ByteString
k0' ByteString
k1' ->
let {-# INLINE t0' #-}
t0' :: Trie a
t0' = ByteString -> Maybe a -> Trie a -> Trie a
forall a. ByteString -> Maybe a -> Trie a -> Trie a
Arc ByteString
k0' Maybe a
mv0 Trie a
s0
{-# INLINE t1' #-}
t1' :: Trie a
t1' = ByteString -> Maybe a -> Trie a -> Trie a
forall a. ByteString -> Maybe a -> Trie a -> Trie a
Arc ByteString
k1' Maybe a
mv1 Trie a
s1
in
case (ByteString -> Bool
S.null ByteString
k0', ByteString -> Bool
S.null ByteString
k1') of
(Bool
True, Bool
True) -> ByteString -> Maybe a -> Trie a -> Trie a
forall a. ByteString -> Maybe a -> Trie a -> Trie a
arcNN ByteString
pre ((a -> a -> Maybe a) -> Maybe a -> Maybe a -> Maybe a
forall a. (a -> a -> Maybe a) -> Maybe a -> Maybe a -> Maybe a
mergeMaybe a -> a -> Maybe a
f Maybe a
mv0 Maybe a
mv1) (Trie a -> Trie a -> Trie a
go Trie a
s0 Trie a
s1)
(Bool
True, Bool
False) -> ByteString -> Maybe a -> Trie a -> Trie a
forall a. ByteString -> Maybe a -> Trie a -> Trie a
arcNN ByteString
pre Maybe a
mv0 (Trie a -> Trie a -> Trie a
go Trie a
s0 Trie a
t1')
(Bool
False,Bool
True) -> ByteString -> Maybe a -> Trie a -> Trie a
forall a. ByteString -> Maybe a -> Trie a -> Trie a
arcNN ByteString
pre Maybe a
mv1 (Trie a -> Trie a -> Trie a
go Trie a
t0' Trie a
s1)
(Bool
False,Bool
False) -> ByteString
-> ByteString -> Trie a -> ByteString -> Trie a -> Trie a
forall a.
ByteString
-> ByteString -> Trie a -> ByteString -> Trie a -> Trie a
wye ByteString
pre ByteString
k0' Trie a
t0' ByteString
k1' Trie a
t1'
go t0 :: Trie a
t0@(Arc ByteString
k0 Maybe a
_ Trie a
_)
t1 :: Trie a
t1@(Branch Prefix
p1 Prefix
m1 Trie a
l Trie a
r)
| Prefix -> Prefix -> Prefix -> Bool
nomatch Prefix
p0 Prefix
p1 Prefix
m1 = Prefix -> Trie a -> Prefix -> Trie a -> Trie a
forall a. Prefix -> Trie a -> Prefix -> Trie a -> Trie a
graft Prefix
p1 Trie a
t1 Prefix
p0 Trie a
t0
| Prefix -> Prefix -> Bool
zero Prefix
p0 Prefix
m1 = Prefix -> Prefix -> Trie a -> Trie a -> Trie a
forall a. Prefix -> Prefix -> Trie a -> Trie a -> Trie a
branchL Prefix
p1 Prefix
m1 (Trie a -> Trie a -> Trie a
go Trie a
t0 Trie a
l) Trie a
r
| Bool
otherwise = Prefix -> Prefix -> Trie a -> Trie a -> Trie a
forall a. Prefix -> Prefix -> Trie a -> Trie a -> Trie a
branchR Prefix
p1 Prefix
m1 Trie a
l (Trie a -> Trie a -> Trie a
go Trie a
t0 Trie a
r)
where p0 :: Prefix
p0 = ByteString -> Prefix
arcPrefix ByteString
k0
go t0 :: Trie a
t0@(Branch Prefix
p0 Prefix
m0 Trie a
l Trie a
r)
t1 :: Trie a
t1@(Arc ByteString
k1 Maybe a
_ Trie a
_)
| Prefix -> Prefix -> Prefix -> Bool
nomatch Prefix
p1 Prefix
p0 Prefix
m0 = Prefix -> Trie a -> Prefix -> Trie a -> Trie a
forall a. Prefix -> Trie a -> Prefix -> Trie a -> Trie a
graft Prefix
p0 Trie a
t0 Prefix
p1 Trie a
t1
| Prefix -> Prefix -> Bool
zero Prefix
p1 Prefix
m0 = Prefix -> Prefix -> Trie a -> Trie a -> Trie a
forall a. Prefix -> Prefix -> Trie a -> Trie a -> Trie a
branchL Prefix
p0 Prefix
m0 (Trie a -> Trie a -> Trie a
go Trie a
l Trie a
t1) Trie a
r
| Bool
otherwise = Prefix -> Prefix -> Trie a -> Trie a -> Trie a
forall a. Prefix -> Prefix -> Trie a -> Trie a -> Trie a
branchR Prefix
p0 Prefix
m0 Trie a
l (Trie a -> Trie a -> Trie a
go Trie a
r Trie a
t1)
where p1 :: Prefix
p1 = ByteString -> Prefix
arcPrefix ByteString
k1
mergeMaybe :: (a -> a -> Maybe a) -> Maybe a -> Maybe a -> Maybe a
{-# INLINE mergeMaybe #-}
mergeMaybe :: (a -> a -> Maybe a) -> Maybe a -> Maybe a -> Maybe a
mergeMaybe a -> a -> Maybe a
_ Maybe a
Nothing Maybe a
Nothing = Maybe a
forall a. Maybe a
Nothing
mergeMaybe a -> a -> Maybe a
_ Maybe a
Nothing mv1 :: Maybe a
mv1@(Just a
_) = Maybe a
mv1
mergeMaybe a -> a -> Maybe a
_ mv0 :: Maybe a
mv0@(Just a
_) Maybe a
Nothing = Maybe a
mv0
mergeMaybe a -> a -> Maybe a
f (Just a
v0) (Just a
v1) = a -> a -> Maybe a
f a
v0 a
v1
intersectBy :: (a -> b -> Maybe c) -> Trie a -> Trie b -> Trie c
intersectBy :: (a -> b -> Maybe c) -> Trie a -> Trie b -> Trie c
intersectBy a -> b -> Maybe c
f = Trie a -> Trie b -> Trie c
start
where
start :: Trie a -> Trie b -> Trie c
start (Arc ByteString
k0 Maybe a
mv0 Trie a
s0) (Arc ByteString
k1 Maybe b
mv1 Trie b
s1) | ByteString -> Bool
S.null ByteString
k0 Bool -> Bool -> Bool
&& ByteString -> Bool
S.null ByteString
k1
= Maybe c -> Trie c -> Trie c
forall a. Maybe a -> Trie a -> Trie a
mayEpsilon ((a -> b -> Maybe c) -> Maybe a -> Maybe b -> Maybe c
forall a b c. (a -> b -> Maybe c) -> Maybe a -> Maybe b -> Maybe c
intersectMaybe a -> b -> Maybe c
f Maybe a
mv0 Maybe b
mv1) (Trie a -> Trie b -> Trie c
go Trie a
s0 Trie b
s1)
start (Arc ByteString
k0 (Just a
_) Trie a
s0) Trie b
t1 | ByteString -> Bool
S.null ByteString
k0 = Trie a -> Trie b -> Trie c
go Trie a
s0 Trie b
t1
start Trie a
t0 (Arc ByteString
k1 (Just b
_) Trie b
s1) | ByteString -> Bool
S.null ByteString
k1 = Trie a -> Trie b -> Trie c
go Trie a
t0 Trie b
s1
start Trie a
t0 Trie b
t1 = Trie a -> Trie b -> Trie c
go Trie a
t0 Trie b
t1
go :: Trie a -> Trie b -> Trie c
go Trie a
Empty Trie b
_ = Trie c
forall a. Trie a
Empty
go Trie a
_ Trie b
Empty = Trie c
forall a. Trie a
Empty
go t0 :: Trie a
t0@(Branch Prefix
p0 Prefix
m0 Trie a
l0 Trie a
r0)
t1 :: Trie b
t1@(Branch Prefix
p1 Prefix
m1 Trie b
l1 Trie b
r1)
| Prefix -> Prefix -> Bool
shorter Prefix
m0 Prefix
m1 = Trie c
isect0
| Prefix -> Prefix -> Bool
shorter Prefix
m1 Prefix
m0 = Trie c
isect1
| Prefix
p0 Prefix -> Prefix -> Bool
forall a. Eq a => a -> a -> Bool
== Prefix
p1 = Prefix -> Prefix -> Trie c -> Trie c -> Trie c
forall a. Prefix -> Prefix -> Trie a -> Trie a -> Trie a
branch Prefix
p0 Prefix
m0 (Trie a -> Trie b -> Trie c
go Trie a
l0 Trie b
l1) (Trie a -> Trie b -> Trie c
go Trie a
r0 Trie b
r1)
| Bool
otherwise = Trie c
forall a. Trie a
Empty
where
isect0 :: Trie c
isect0 | Prefix -> Prefix -> Prefix -> Bool
nomatch Prefix
p1 Prefix
p0 Prefix
m0 = Trie c
forall a. Trie a
Empty
| Prefix -> Prefix -> Bool
zero Prefix
p1 Prefix
m0 = Trie a -> Trie b -> Trie c
go Trie a
l0 Trie b
t1
| Bool
otherwise = Trie a -> Trie b -> Trie c
go Trie a
r0 Trie b
t1
isect1 :: Trie c
isect1 | Prefix -> Prefix -> Prefix -> Bool
nomatch Prefix
p0 Prefix
p1 Prefix
m1 = Trie c
forall a. Trie a
Empty
| Prefix -> Prefix -> Bool
zero Prefix
p0 Prefix
m1 = Trie a -> Trie b -> Trie c
go Trie a
t0 Trie b
l1
| Bool
otherwise = Trie a -> Trie b -> Trie c
go Trie a
t0 Trie b
r1
go (Arc ByteString
k0 Maybe a
mv0 Trie a
s0)
(Arc ByteString
k1 Maybe b
mv1 Trie b
s1)
| Prefix -> Prefix -> Prefix
forall a. Bits a => a -> a -> a
xor (ByteString -> Prefix
arcPrefix ByteString
k0) (ByteString -> Prefix
arcPrefix ByteString
k1) Prefix -> Prefix -> Bool
forall a. Eq a => a -> a -> Bool
/= Prefix
0 = Trie c
forall a. Trie a
Empty
| Bool
otherwise =
let (ByteString
pre,ByteString
k0',ByteString
k1') = ByteString -> ByteString -> (ByteString, ByteString, ByteString)
breakMaximalPrefix ByteString
k0 ByteString
k1 in
if ByteString -> Bool
S.null ByteString
pre
then String -> Trie c
forall a. HasCallStack => String -> a
error String
"intersectBy: no mask, but no prefix string"
else
let {-# INLINE t0' #-}
t0' :: Trie a
t0' = ByteString -> Maybe a -> Trie a -> Trie a
forall a. ByteString -> Maybe a -> Trie a -> Trie a
Arc ByteString
k0' Maybe a
mv0 Trie a
s0
{-# INLINE t1' #-}
t1' :: Trie b
t1' = ByteString -> Maybe b -> Trie b -> Trie b
forall a. ByteString -> Maybe a -> Trie a -> Trie a
Arc ByteString
k1' Maybe b
mv1 Trie b
s1
in
case (ByteString -> Bool
S.null ByteString
k0', ByteString -> Bool
S.null ByteString
k1') of
(Bool
True, Bool
True) -> ByteString -> Maybe c -> Trie c -> Trie c
forall a. ByteString -> Maybe a -> Trie a -> Trie a
arcNN ByteString
pre ((a -> b -> Maybe c) -> Maybe a -> Maybe b -> Maybe c
forall a b c. (a -> b -> Maybe c) -> Maybe a -> Maybe b -> Maybe c
intersectMaybe a -> b -> Maybe c
f Maybe a
mv0 Maybe b
mv1) (Trie a -> Trie b -> Trie c
go Trie a
s0 Trie b
s1)
(Bool
True, Bool
False) -> ByteString -> Trie c -> Trie c
forall a. ByteString -> Trie a -> Trie a
prependNN ByteString
pre (Trie a -> Trie b -> Trie c
go Trie a
s0 Trie b
t1')
(Bool
False,Bool
True) -> ByteString -> Trie c -> Trie c
forall a. ByteString -> Trie a -> Trie a
prependNN ByteString
pre (Trie a -> Trie b -> Trie c
go Trie a
t0' Trie b
s1)
(Bool
False,Bool
False) -> ByteString -> Trie c -> Trie c
forall a. ByteString -> Trie a -> Trie a
prependNN ByteString
pre (Trie a -> Trie b -> Trie c
go Trie a
t0' Trie b
t1')
go t0 :: Trie a
t0@(Arc ByteString
k0 Maybe a
_ Trie a
_)
(Branch Prefix
p1 Prefix
m1 Trie b
l Trie b
r)
| Prefix -> Prefix -> Prefix -> Bool
nomatch Prefix
p0 Prefix
p1 Prefix
m1 = Trie c
forall a. Trie a
Empty
| Prefix -> Prefix -> Bool
zero Prefix
p0 Prefix
m1 = Trie a -> Trie b -> Trie c
go Trie a
t0 Trie b
l
| Bool
otherwise = Trie a -> Trie b -> Trie c
go Trie a
t0 Trie b
r
where p0 :: Prefix
p0 = ByteString -> Prefix
arcPrefix ByteString
k0
go (Branch Prefix
p0 Prefix
m0 Trie a
l Trie a
r)
t1 :: Trie b
t1@(Arc ByteString
k1 Maybe b
_ Trie b
_)
| Prefix -> Prefix -> Prefix -> Bool
nomatch Prefix
p1 Prefix
p0 Prefix
m0 = Trie c
forall a. Trie a
Empty
| Prefix -> Prefix -> Bool
zero Prefix
p1 Prefix
m0 = Trie a -> Trie b -> Trie c
go Trie a
l Trie b
t1
| Bool
otherwise = Trie a -> Trie b -> Trie c
go Trie a
r Trie b
t1
where p1 :: Prefix
p1 = ByteString -> Prefix
arcPrefix ByteString
k1
intersectMaybe :: (a -> b -> Maybe c) -> Maybe a -> Maybe b -> Maybe c
{-# INLINE intersectMaybe #-}
intersectMaybe :: (a -> b -> Maybe c) -> Maybe a -> Maybe b -> Maybe c
intersectMaybe a -> b -> Maybe c
f (Just a
v0) (Just b
v1) = a -> b -> Maybe c
f a
v0 b
v1
intersectMaybe a -> b -> Maybe c
_ Maybe a
_ Maybe b
_ = Maybe c
forall a. Maybe a
Nothing
minAssoc :: Trie a -> Maybe (ByteString, a)
minAssoc :: Trie a -> Maybe (ByteString, a)
minAssoc = RevLazyByteString -> Trie a -> Maybe (ByteString, a)
forall b. RevLazyByteString -> Trie b -> Maybe (ByteString, b)
go RevLazyByteString
Nil
where
go :: RevLazyByteString -> Trie b -> Maybe (ByteString, b)
go !RevLazyByteString
_ Trie b
Empty = Maybe (ByteString, b)
forall a. Maybe a
Nothing
go RevLazyByteString
q (Arc ByteString
k (Just b
v) Trie b
_) = (ByteString, b) -> Maybe (ByteString, b)
forall a. a -> Maybe a
Just (RevLazyByteString -> ByteString
toStrict (RevLazyByteString
q RevLazyByteString -> ByteString -> RevLazyByteString
+>? ByteString
k), b
v)
go RevLazyByteString
q (Arc ByteString
k Maybe b
Nothing Trie b
t) = RevLazyByteString -> Trie b -> Maybe (ByteString, b)
go (RevLazyByteString
q RevLazyByteString -> ByteString -> RevLazyByteString
+>! ByteString
k) Trie b
t
go RevLazyByteString
q (Branch Prefix
_ Prefix
_ Trie b
l Trie b
_) = RevLazyByteString -> Trie b -> Maybe (ByteString, b)
go RevLazyByteString
q Trie b
l
maxAssoc :: Trie a -> Maybe (ByteString, a)
maxAssoc :: Trie a -> Maybe (ByteString, a)
maxAssoc = RevLazyByteString -> Trie a -> Maybe (ByteString, a)
forall b. RevLazyByteString -> Trie b -> Maybe (ByteString, b)
go RevLazyByteString
Nil
where
go :: RevLazyByteString -> Trie b -> Maybe (ByteString, b)
go !RevLazyByteString
_ Trie b
Empty = Maybe (ByteString, b)
forall a. Maybe a
Nothing
go RevLazyByteString
q (Arc ByteString
k (Just b
v) Trie b
Empty) = (ByteString, b) -> Maybe (ByteString, b)
forall a. a -> Maybe a
Just (RevLazyByteString -> ByteString
toStrict (RevLazyByteString
q RevLazyByteString -> ByteString -> RevLazyByteString
+>? ByteString
k), b
v)
go RevLazyByteString
q (Arc ByteString
k (Just b
_) Trie b
t) = RevLazyByteString -> Trie b -> Maybe (ByteString, b)
go (RevLazyByteString
q RevLazyByteString -> ByteString -> RevLazyByteString
+>? ByteString
k) Trie b
t
go RevLazyByteString
q (Arc ByteString
k Maybe b
Nothing Trie b
t) = RevLazyByteString -> Trie b -> Maybe (ByteString, b)
go (RevLazyByteString
q RevLazyByteString -> ByteString -> RevLazyByteString
+>! ByteString
k) Trie b
t
go RevLazyByteString
q (Branch Prefix
_ Prefix
_ Trie b
_ Trie b
r) = RevLazyByteString -> Trie b -> Maybe (ByteString, b)
go RevLazyByteString
q Trie b
r
mapView :: (Trie a -> Trie a)
-> Maybe (ByteString, a, Trie a) -> Maybe (ByteString, a, Trie a)
{-# INLINE mapView #-}
mapView :: (Trie a -> Trie a)
-> Maybe (ByteString, a, Trie a) -> Maybe (ByteString, a, Trie a)
mapView Trie a -> Trie a
_ Maybe (ByteString, a, Trie a)
Nothing = Maybe (ByteString, a, Trie a)
forall a. Maybe a
Nothing
mapView Trie a -> Trie a
f (Just (ByteString
k,a
v,Trie a
t)) = (ByteString, a, Trie a) -> Maybe (ByteString, a, Trie a)
forall a. a -> Maybe a
Just (ByteString
k,a
v, Trie a -> Trie a
f Trie a
t)
updateMinViewBy :: (ByteString -> a -> Maybe a)
-> Trie a -> Maybe (ByteString, a, Trie a)
updateMinViewBy :: (ByteString -> a -> Maybe a)
-> Trie a -> Maybe (ByteString, a, Trie a)
updateMinViewBy ByteString -> a -> Maybe a
f = RevLazyByteString -> Trie a -> Maybe (ByteString, a, Trie a)
go RevLazyByteString
Nil
where
go :: RevLazyByteString -> Trie a -> Maybe (ByteString, a, Trie a)
go !RevLazyByteString
_ Trie a
Empty = Maybe (ByteString, a, Trie a)
forall a. Maybe a
Nothing
go RevLazyByteString
q (Arc ByteString
k (Just a
v) Trie a
t) = let q' :: ByteString
q' = RevLazyByteString -> ByteString
toStrict (RevLazyByteString
q RevLazyByteString -> ByteString -> RevLazyByteString
+>? ByteString
k)
in (ByteString, a, Trie a) -> Maybe (ByteString, a, Trie a)
forall a. a -> Maybe a
Just (ByteString
q',a
v, ByteString -> Maybe a -> Trie a -> Trie a
forall a. ByteString -> Maybe a -> Trie a -> Trie a
arc ByteString
k (ByteString -> a -> Maybe a
f ByteString
q' a
v) Trie a
t)
go RevLazyByteString
q (Arc ByteString
k Maybe a
Nothing Trie a
t) = (Trie a -> Trie a)
-> Maybe (ByteString, a, Trie a) -> Maybe (ByteString, a, Trie a)
forall a.
(Trie a -> Trie a)
-> Maybe (ByteString, a, Trie a) -> Maybe (ByteString, a, Trie a)
mapView (ByteString -> Trie a -> Trie a
forall a. ByteString -> Trie a -> Trie a
prependNN ByteString
k) (RevLazyByteString -> Trie a -> Maybe (ByteString, a, Trie a)
go (RevLazyByteString
q RevLazyByteString -> ByteString -> RevLazyByteString
+>! ByteString
k) Trie a
t)
go RevLazyByteString
q (Branch Prefix
p Prefix
m Trie a
l Trie a
r) = (Trie a -> Trie a)
-> Maybe (ByteString, a, Trie a) -> Maybe (ByteString, a, Trie a)
forall a.
(Trie a -> Trie a)
-> Maybe (ByteString, a, Trie a) -> Maybe (ByteString, a, Trie a)
mapView (\Trie a
l' -> Prefix -> Prefix -> Trie a -> Trie a -> Trie a
forall a. Prefix -> Prefix -> Trie a -> Trie a -> Trie a
branchL Prefix
p Prefix
m Trie a
l' Trie a
r) (RevLazyByteString -> Trie a -> Maybe (ByteString, a, Trie a)
go RevLazyByteString
q Trie a
l)
updateMaxViewBy :: (ByteString -> a -> Maybe a)
-> Trie a -> Maybe (ByteString, a, Trie a)
updateMaxViewBy :: (ByteString -> a -> Maybe a)
-> Trie a -> Maybe (ByteString, a, Trie a)
updateMaxViewBy ByteString -> a -> Maybe a
f = RevLazyByteString -> Trie a -> Maybe (ByteString, a, Trie a)
go RevLazyByteString
Nil
where
go :: RevLazyByteString -> Trie a -> Maybe (ByteString, a, Trie a)
go !RevLazyByteString
_ Trie a
Empty = Maybe (ByteString, a, Trie a)
forall a. Maybe a
Nothing
go RevLazyByteString
q (Arc ByteString
k (Just a
v) Trie a
Empty) = let q' :: ByteString
q' = RevLazyByteString -> ByteString
toStrict (RevLazyByteString
q RevLazyByteString -> ByteString -> RevLazyByteString
+>? ByteString
k)
in (ByteString, a, Trie a) -> Maybe (ByteString, a, Trie a)
forall a. a -> Maybe a
Just (ByteString
q',a
v, ByteString -> Maybe a -> Trie a -> Trie a
forall a. ByteString -> Maybe a -> Trie a -> Trie a
arc ByteString
k (ByteString -> a -> Maybe a
f ByteString
q' a
v) Trie a
forall a. Trie a
Empty)
go RevLazyByteString
q (Arc ByteString
k mv :: Maybe a
mv@(Just a
_) Trie a
t) = (Trie a -> Trie a)
-> Maybe (ByteString, a, Trie a) -> Maybe (ByteString, a, Trie a)
forall a.
(Trie a -> Trie a)
-> Maybe (ByteString, a, Trie a) -> Maybe (ByteString, a, Trie a)
mapView (ByteString -> Maybe a -> Trie a -> Trie a
forall a. ByteString -> Maybe a -> Trie a -> Trie a
Arc ByteString
k Maybe a
mv) (RevLazyByteString -> Trie a -> Maybe (ByteString, a, Trie a)
go (RevLazyByteString
q RevLazyByteString -> ByteString -> RevLazyByteString
+>? ByteString
k) Trie a
t)
go RevLazyByteString
q (Arc ByteString
k Maybe a
Nothing Trie a
t) = (Trie a -> Trie a)
-> Maybe (ByteString, a, Trie a) -> Maybe (ByteString, a, Trie a)
forall a.
(Trie a -> Trie a)
-> Maybe (ByteString, a, Trie a) -> Maybe (ByteString, a, Trie a)
mapView (ByteString -> Trie a -> Trie a
forall a. ByteString -> Trie a -> Trie a
prepend ByteString
k) (RevLazyByteString -> Trie a -> Maybe (ByteString, a, Trie a)
go (RevLazyByteString
q RevLazyByteString -> ByteString -> RevLazyByteString
+>! ByteString
k) Trie a
t)
go RevLazyByteString
q (Branch Prefix
p Prefix
m Trie a
l Trie a
r) = (Trie a -> Trie a)
-> Maybe (ByteString, a, Trie a) -> Maybe (ByteString, a, Trie a)
forall a.
(Trie a -> Trie a)
-> Maybe (ByteString, a, Trie a) -> Maybe (ByteString, a, Trie a)
mapView (Prefix -> Prefix -> Trie a -> Trie a -> Trie a
forall a. Prefix -> Prefix -> Trie a -> Trie a -> Trie a
branchR Prefix
p Prefix
m Trie a
l) (RevLazyByteString -> Trie a -> Maybe (ByteString, a, Trie a)
go RevLazyByteString
q Trie a
r)