{-# OPTIONS_GHC -Wall -fwarn-tabs #-}
{-# LANGUAGE CPP #-}
#if __GLASGOW_HASKELL__ >= 701
{-# LANGUAGE Safe #-}
#endif
module Data.Trie.Convenience
(
fromListL, fromListR, fromListS
, fromListWith, fromListWith'
, fromListWithL, fromListWithL'
, lookupWithDefault
, insertIfAbsent
, insertWith, insertWith'
, insertWithKey, insertWithKey'
, adjustWithKey
, update, updateWithKey
, disunion
, unionWith, unionWith'
, intersectWith, intersectWith'
) where
import Data.Trie
import Data.Trie.Internal (lookupBy_, alterBy_)
import Data.ByteString (ByteString)
import Data.List (foldl', sortBy)
import Data.Ord (comparing)
fromListL :: [(ByteString,a)] -> Trie a
{-# INLINE fromListL #-}
fromListL :: [(ByteString, a)] -> Trie a
fromListL = (Trie a -> (ByteString, a) -> Trie a)
-> Trie a -> [(ByteString, a)] -> Trie a
forall (t :: * -> *) b a.
Foldable t =>
(b -> a -> b) -> b -> t a -> b
foldl' (((ByteString, a) -> Trie a -> Trie a)
-> Trie a -> (ByteString, a) -> Trie a
forall a b c. (a -> b -> c) -> b -> a -> c
flip (((ByteString, a) -> Trie a -> Trie a)
-> Trie a -> (ByteString, a) -> Trie a)
-> ((ByteString -> a -> Trie a -> Trie a)
-> (ByteString, a) -> Trie a -> Trie a)
-> (ByteString -> a -> Trie a -> Trie a)
-> Trie a
-> (ByteString, a)
-> Trie a
forall b c a. (b -> c) -> (a -> b) -> a -> c
. (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)
-> Trie a -> (ByteString, a) -> Trie a)
-> (ByteString -> a -> Trie a -> Trie a)
-> Trie a
-> (ByteString, a)
-> Trie a
forall a b. (a -> b) -> a -> b
$ ByteString -> a -> Trie a -> Trie a
forall a. ByteString -> a -> Trie a -> Trie a
insertIfAbsent) Trie a
forall a. Trie a
empty
fromListR :: [(ByteString,a)] -> Trie a
{-# INLINE fromListR #-}
fromListR :: [(ByteString, a)] -> Trie a
fromListR = [(ByteString, a)] -> Trie a
forall a. [(ByteString, a)] -> Trie a
fromList
fromListS :: [(ByteString,a)] -> Trie a
{-# INLINE fromListS #-}
fromListS :: [(ByteString, a)] -> Trie a
fromListS = [(ByteString, a)] -> Trie a
forall a. [(ByteString, a)] -> Trie a
fromListR ([(ByteString, a)] -> Trie a)
-> ([(ByteString, a)] -> [(ByteString, a)])
-> [(ByteString, a)]
-> Trie a
forall b c a. (b -> c) -> (a -> b) -> a -> c
. ((ByteString, a) -> (ByteString, a) -> Ordering)
-> [(ByteString, a)] -> [(ByteString, a)]
forall a. (a -> a -> Ordering) -> [a] -> [a]
sortBy (((ByteString, a) -> ByteString)
-> (ByteString, a) -> (ByteString, a) -> Ordering
forall a b. Ord a => (b -> a) -> b -> b -> Ordering
comparing (ByteString, a) -> ByteString
forall a b. (a, b) -> a
fst)
fromListWith :: (a -> a -> a) -> [(ByteString,a)] -> Trie a
{-# INLINE fromListWith #-}
fromListWith :: (a -> a -> a) -> [(ByteString, a)] -> Trie a
fromListWith a -> a -> a
f = ((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)
-> (ByteString, a) -> Trie a -> Trie a)
-> (ByteString -> a -> Trie a -> Trie a)
-> (ByteString, a)
-> Trie a
-> Trie a
forall a b. (a -> b) -> a -> b
$ (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 -> Maybe a -> Maybe a
forall p. p -> a -> Maybe a -> Maybe a
g) Trie a
forall a. Trie a
empty
where
g :: p -> a -> Maybe a -> Maybe a
g p
_ a
v Maybe a
Nothing = a -> Maybe a
forall a. a -> Maybe a
Just a
v
g p
_ a
v (Just a
w) = a -> Maybe a
forall a. a -> Maybe a
Just (a -> a -> a
f a
v a
w)
fromListWith' :: (a -> a -> a) -> [(ByteString,a)] -> Trie a
{-# INLINE fromListWith' #-}
fromListWith' :: (a -> a -> a) -> [(ByteString, a)] -> Trie a
fromListWith' a -> a -> a
f = ((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)
-> (ByteString, a) -> Trie a -> Trie a)
-> (ByteString -> a -> Trie a -> Trie a)
-> (ByteString, a)
-> Trie a
-> Trie a
forall a b. (a -> b) -> a -> b
$ (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 -> Maybe a -> Maybe a
forall p. p -> a -> Maybe a -> Maybe a
g') Trie a
forall a. Trie a
empty
where
g' :: p -> a -> Maybe a -> Maybe a
g' p
_ a
v Maybe a
Nothing = a -> Maybe a
forall a. a -> Maybe a
Just a
v
g' p
_ a
v (Just a
w) = a -> Maybe a
forall a. a -> Maybe a
Just (a -> Maybe a) -> a -> Maybe a
forall a b. (a -> b) -> a -> b
$! a -> a -> a
f a
v a
w
fromListWithL :: (a -> a -> a) -> [(ByteString,a)] -> Trie a
{-# INLINE fromListWithL #-}
fromListWithL :: (a -> a -> a) -> [(ByteString, a)] -> Trie a
fromListWithL a -> a -> a
f = (Trie a -> (ByteString, a) -> Trie a)
-> Trie a -> [(ByteString, a)] -> Trie a
forall (t :: * -> *) b a.
Foldable t =>
(b -> a -> b) -> b -> t a -> b
foldl' (((ByteString, a) -> Trie a -> Trie a)
-> Trie a -> (ByteString, a) -> Trie a
forall a b c. (a -> b -> c) -> b -> a -> c
flip (((ByteString, a) -> Trie a -> Trie a)
-> Trie a -> (ByteString, a) -> Trie a)
-> ((ByteString -> a -> Trie a -> Trie a)
-> (ByteString, a) -> Trie a -> Trie a)
-> (ByteString -> a -> Trie a -> Trie a)
-> Trie a
-> (ByteString, a)
-> Trie a
forall b c a. (b -> c) -> (a -> b) -> a -> c
. (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)
-> Trie a -> (ByteString, a) -> Trie a)
-> (ByteString -> a -> Trie a -> Trie a)
-> Trie a
-> (ByteString, a)
-> Trie a
forall a b. (a -> b) -> a -> b
$ (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 -> Maybe a -> Maybe a
forall p. p -> a -> Maybe a -> Maybe a
flipG) Trie a
forall a. Trie a
empty
where
flipG :: p -> a -> Maybe a -> Maybe a
flipG p
_ a
v Maybe a
Nothing = a -> Maybe a
forall a. a -> Maybe a
Just a
v
flipG p
_ a
v (Just a
w) = a -> Maybe a
forall a. a -> Maybe a
Just (a -> a -> a
f a
w a
v)
fromListWithL' :: (a -> a -> a) -> [(ByteString,a)] -> Trie a
{-# INLINE fromListWithL' #-}
fromListWithL' :: (a -> a -> a) -> [(ByteString, a)] -> Trie a
fromListWithL' a -> a -> a
f = (Trie a -> (ByteString, a) -> Trie a)
-> Trie a -> [(ByteString, a)] -> Trie a
forall (t :: * -> *) b a.
Foldable t =>
(b -> a -> b) -> b -> t a -> b
foldl' (((ByteString, a) -> Trie a -> Trie a)
-> Trie a -> (ByteString, a) -> Trie a
forall a b c. (a -> b -> c) -> b -> a -> c
flip (((ByteString, a) -> Trie a -> Trie a)
-> Trie a -> (ByteString, a) -> Trie a)
-> ((ByteString -> a -> Trie a -> Trie a)
-> (ByteString, a) -> Trie a -> Trie a)
-> (ByteString -> a -> Trie a -> Trie a)
-> Trie a
-> (ByteString, a)
-> Trie a
forall b c a. (b -> c) -> (a -> b) -> a -> c
. (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)
-> Trie a -> (ByteString, a) -> Trie a)
-> (ByteString -> a -> Trie a -> Trie a)
-> Trie a
-> (ByteString, a)
-> Trie a
forall a b. (a -> b) -> a -> b
$ (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 -> Maybe a -> Maybe a
forall p. p -> a -> Maybe a -> Maybe a
flipG') Trie a
forall a. Trie a
empty
where
flipG' :: p -> a -> Maybe a -> Maybe a
flipG' p
_ a
v Maybe a
Nothing = a -> Maybe a
forall a. a -> Maybe a
Just a
v
flipG' p
_ a
v (Just a
w) = a -> Maybe a
forall a. a -> Maybe a
Just (a -> Maybe a) -> a -> Maybe a
forall a b. (a -> b) -> a -> b
$! a -> a -> a
f a
w a
v
lookupWithDefault :: a -> ByteString -> Trie a -> a
lookupWithDefault :: a -> ByteString -> Trie a -> a
lookupWithDefault a
def = (a -> Trie a -> a)
-> (Trie a -> a) -> a -> ByteString -> Trie a -> a
forall a b.
(a -> Trie a -> b)
-> (Trie a -> b) -> b -> ByteString -> Trie a -> b
lookupBy_ a -> Trie a -> a
forall a b. a -> b -> a
const (a -> Trie a -> a
forall a b. a -> b -> a
const a
def) a
def
insertIfAbsent :: ByteString -> a -> Trie a -> Trie a
insertIfAbsent :: ByteString -> a -> Trie a -> Trie a
insertIfAbsent =
(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 -> Maybe a -> Maybe a)
-> ByteString -> a -> Trie a -> Trie a)
-> (ByteString -> a -> Maybe a -> Maybe a)
-> ByteString
-> a
-> Trie a
-> Trie a
forall a b. (a -> b) -> a -> b
$ \ByteString
_ a
x Maybe a
mv ->
case Maybe a
mv of
Maybe a
Nothing -> a -> Maybe a
forall a. a -> Maybe a
Just a
x
Just a
_ -> Maybe a
mv
insertWith :: (a -> a -> a) -> ByteString -> a -> Trie a -> Trie a
insertWith :: (a -> a -> a) -> ByteString -> a -> Trie a -> Trie a
insertWith a -> a -> a
f =
(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 -> Maybe a -> Maybe a)
-> ByteString -> a -> Trie a -> Trie a)
-> (ByteString -> a -> Maybe a -> Maybe a)
-> ByteString
-> a
-> Trie a
-> Trie a
forall a b. (a -> b) -> a -> b
$ \ByteString
_ a
x Maybe a
mv ->
case Maybe a
mv of
Maybe a
Nothing -> a -> Maybe a
forall a. a -> Maybe a
Just a
x
Just a
v -> a -> Maybe a
forall a. a -> Maybe a
Just (a -> a -> a
f a
x a
v)
insertWith' :: (a -> a -> a) -> ByteString -> a -> Trie a -> Trie a
insertWith' :: (a -> a -> a) -> ByteString -> a -> Trie a -> Trie a
insertWith' a -> a -> a
f =
(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 -> Maybe a -> Maybe a)
-> ByteString -> a -> Trie a -> Trie a)
-> (ByteString -> a -> Maybe a -> Maybe a)
-> ByteString
-> a
-> Trie a
-> Trie a
forall a b. (a -> b) -> a -> b
$ \ByteString
_ a
x Maybe a
mv ->
case Maybe a
mv of
Maybe a
Nothing -> a -> Maybe a
forall a. a -> Maybe a
Just a
x
Just a
v -> a -> Maybe a
forall a. a -> Maybe a
Just (a -> Maybe a) -> a -> Maybe a
forall a b. (a -> b) -> a -> b
$! a -> a -> a
f a
x a
v
insertWithKey :: (ByteString -> a -> a -> a) -> ByteString -> a -> Trie a -> Trie a
insertWithKey :: (ByteString -> a -> a -> a) -> ByteString -> a -> Trie a -> Trie a
insertWithKey ByteString -> a -> a -> a
f =
(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 -> Maybe a -> Maybe a)
-> ByteString -> a -> Trie a -> Trie a)
-> (ByteString -> a -> Maybe a -> Maybe a)
-> ByteString
-> a
-> Trie a
-> Trie a
forall a b. (a -> b) -> a -> b
$ \ByteString
k a
x Maybe a
mv ->
case Maybe a
mv of
Maybe a
Nothing -> a -> Maybe a
forall a. a -> Maybe a
Just a
x
Just a
v -> a -> Maybe a
forall a. a -> Maybe a
Just (ByteString -> a -> a -> a
f ByteString
k a
x a
v)
insertWithKey' :: (ByteString -> a -> a -> a) -> ByteString -> a -> Trie a -> Trie a
insertWithKey' :: (ByteString -> a -> a -> a) -> ByteString -> a -> Trie a -> Trie a
insertWithKey' ByteString -> a -> a -> a
f =
(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 -> Maybe a -> Maybe a)
-> ByteString -> a -> Trie a -> Trie a)
-> (ByteString -> a -> Maybe a -> Maybe a)
-> ByteString
-> a
-> Trie a
-> Trie a
forall a b. (a -> b) -> a -> b
$ \ByteString
k a
x Maybe a
mv ->
case Maybe a
mv of
Maybe a
Nothing -> a -> Maybe a
forall a. a -> Maybe a
Just a
x
Just a
v -> a -> Maybe a
forall a. a -> Maybe a
Just (a -> Maybe a) -> a -> Maybe a
forall a b. (a -> b) -> a -> b
$! ByteString -> a -> a -> a
f ByteString
k a
x a
v
adjustWithKey :: (ByteString -> a -> a) -> ByteString -> Trie a -> Trie a
adjustWithKey :: (ByteString -> a -> a) -> ByteString -> Trie a -> Trie a
adjustWithKey ByteString -> a -> a
f ByteString
q = (a -> a) -> ByteString -> Trie a -> Trie a
forall a. (a -> a) -> ByteString -> Trie a -> Trie a
adjust (ByteString -> a -> a
f ByteString
q) ByteString
q
update :: (a -> Maybe a) -> ByteString -> Trie a -> Trie a
update :: (a -> Maybe a) -> ByteString -> Trie a -> Trie a
update a -> Maybe a
f ByteString
q = (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
mx Trie a
t -> (Maybe a
mx Maybe a -> (a -> Maybe a) -> Maybe a
forall (m :: * -> *) a b. Monad m => m a -> (a -> m b) -> m b
>>= a -> Maybe a
f, Trie a
t)) ByteString
q
updateWithKey :: (ByteString -> a -> Maybe a) -> ByteString -> Trie a -> Trie a
updateWithKey :: (ByteString -> a -> Maybe a) -> ByteString -> Trie a -> Trie a
updateWithKey ByteString -> a -> Maybe a
f ByteString
q = (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
mx Trie a
t -> (Maybe a
mx Maybe a -> (a -> Maybe a) -> Maybe a
forall (m :: * -> *) a b. Monad m => m a -> (a -> m b) -> m b
>>= ByteString -> a -> Maybe a
f ByteString
q, Trie a
t)) ByteString
q
disunion :: Trie a -> Trie a -> Trie a
disunion :: Trie a -> Trie a -> Trie a
disunion = (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
forall a. Maybe a
Nothing)
unionWith :: (a -> a -> a) -> Trie a -> Trie a -> Trie a
unionWith :: (a -> a -> a) -> Trie a -> Trie a -> Trie a
unionWith a -> a -> a
f = (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
y -> a -> Maybe a
forall a. a -> Maybe a
Just (a -> a -> a
f a
x a
y))
unionWith' :: (a -> a -> a) -> Trie a -> Trie a -> Trie a
unionWith' :: (a -> a -> a) -> Trie a -> Trie a -> Trie a
unionWith' a -> a -> a
f = (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
y -> a -> Maybe a
forall a. a -> Maybe a
Just (a -> Maybe a) -> a -> Maybe a
forall a b. (a -> b) -> a -> b
$! a -> a -> a
f a
x a
y)
intersectWith :: (a -> b -> c) -> Trie a -> Trie b -> Trie c
intersectWith :: (a -> b -> c) -> Trie a -> Trie b -> Trie c
intersectWith a -> b -> c
f = (a -> b -> Maybe c) -> Trie a -> Trie b -> Trie c
forall a b c. (a -> b -> Maybe c) -> Trie a -> Trie b -> Trie c
intersectBy (\a
x b
y -> c -> Maybe c
forall a. a -> Maybe a
Just (a -> b -> c
f a
x b
y))
intersectWith' :: (a -> b -> c) -> Trie a -> Trie b -> Trie c
intersectWith' :: (a -> b -> c) -> Trie a -> Trie b -> Trie c
intersectWith' a -> b -> c
f = (a -> b -> Maybe c) -> Trie a -> Trie b -> Trie c
forall a b c. (a -> b -> Maybe c) -> Trie a -> Trie b -> Trie c
intersectBy (\a
x b
y -> c -> Maybe c
forall a. a -> Maybe a
Just (c -> Maybe c) -> c -> Maybe c
forall a b. (a -> b) -> a -> b
$! a -> b -> c
f a
x b
y)