{-# OPTIONS_GHC -Wall -fwarn-tabs #-}
{-# LANGUAGE NoImplicitPrelude, CPP #-}
#if __GLASGOW_HASKELL__ >= 701
{-# LANGUAGE Safe #-}
#endif
module Data.Trie
(
Trie()
, empty, null, singleton, size
, fromList, toListBy, toList, keys, elems
, lookupBy, lookup, member, submap, match, minMatch, matches
, insert, adjust, adjustBy, alterBy, delete, deleteSubmap
, mergeBy, unionL, unionR
, intersectBy, intersectL, intersectR
, mapBy, filterMap
) where
import Prelude hiding (null, lookup)
import Data.Trie.Internal
import Data.ByteString (ByteString)
import qualified Data.ByteString as S
import Data.Maybe (isJust)
keys :: Trie a -> [ByteString]
{-# INLINE keys #-}
keys :: Trie a -> [ByteString]
keys = (ByteString -> a -> ByteString) -> Trie a -> [ByteString]
forall a b. (ByteString -> a -> b) -> Trie a -> [b]
toListBy ByteString -> a -> ByteString
forall a b. a -> b -> a
const
lookupBy :: (Maybe a -> Trie a -> b) -> ByteString -> Trie a -> b
{-# INLINE lookupBy #-}
lookupBy :: (Maybe a -> Trie a -> b) -> ByteString -> Trie a -> b
lookupBy Maybe a -> Trie a -> b
f = (a -> Trie a -> b)
-> (Trie a -> b) -> b -> ByteString -> Trie a -> b
forall a b.
(a -> Trie a -> b)
-> (Trie a -> b) -> b -> ByteString -> Trie a -> b
lookupBy_ (Maybe a -> Trie a -> b
f (Maybe a -> Trie a -> b) -> (a -> Maybe a) -> a -> Trie a -> b
forall b c a. (b -> c) -> (a -> b) -> a -> c
. a -> Maybe a
forall a. a -> Maybe a
Just) (Maybe a -> Trie a -> b
f Maybe a
forall a. Maybe a
Nothing) (Maybe a -> Trie a -> b
f Maybe a
forall a. Maybe a
Nothing Trie a
forall a. Trie a
empty)
lookup :: ByteString -> Trie a -> Maybe a
{-# INLINE lookup #-}
lookup :: ByteString -> Trie a -> Maybe a
lookup = (Maybe a -> Trie a -> Maybe a) -> ByteString -> Trie a -> Maybe a
forall a b. (Maybe a -> Trie a -> b) -> ByteString -> Trie a -> b
lookupBy Maybe a -> Trie a -> Maybe a
forall a b. a -> b -> a
const
member :: ByteString -> Trie a -> Bool
{-# INLINE member #-}
member :: ByteString -> Trie a -> Bool
member ByteString
q = Maybe a -> Bool
forall a. Maybe a -> Bool
isJust (Maybe a -> Bool) -> (Trie a -> Maybe a) -> Trie a -> Bool
forall b c a. (b -> c) -> (a -> b) -> a -> c
. ByteString -> Trie a -> Maybe a
forall a. ByteString -> Trie a -> Maybe a
lookup ByteString
q
getMatch :: ByteString -> (Int, a) -> (ByteString, a, ByteString)
{-# INLINE getMatch #-}
getMatch :: ByteString -> (Int, a) -> (ByteString, a, ByteString)
getMatch ByteString
q (Int
n,a
x) =
case Int -> ByteString -> (ByteString, ByteString)
S.splitAt Int
n ByteString
q of
(ByteString
p,ByteString
q') -> (ByteString
p, a
x, ByteString
q')
match :: Trie a -> ByteString -> Maybe (ByteString, a, ByteString)
{-# INLINE match #-}
match :: Trie a -> ByteString -> Maybe (ByteString, a, ByteString)
match Trie a
t ByteString
q = ByteString -> (Int, a) -> (ByteString, a, ByteString)
forall a. ByteString -> (Int, a) -> (ByteString, a, ByteString)
getMatch ByteString
q ((Int, a) -> (ByteString, a, ByteString))
-> Maybe (Int, a) -> Maybe (ByteString, a, ByteString)
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> Trie a -> ByteString -> Maybe (Int, a)
forall a. Trie a -> ByteString -> Maybe (Int, a)
match_ Trie a
t ByteString
q
minMatch :: Trie a -> ByteString -> Maybe (ByteString, a, ByteString)
{-# INLINE minMatch #-}
minMatch :: Trie a -> ByteString -> Maybe (ByteString, a, ByteString)
minMatch Trie a
t ByteString
q =
case Trie a -> ByteString -> [(ByteString, a, ByteString)]
forall a. Trie a -> ByteString -> [(ByteString, a, ByteString)]
matches Trie a
t ByteString
q of
[] -> Maybe (ByteString, a, ByteString)
forall a. Maybe a
Nothing
(ByteString, a, ByteString)
x:[(ByteString, a, ByteString)]
_ -> (ByteString, a, ByteString) -> Maybe (ByteString, a, ByteString)
forall a. a -> Maybe a
Just (ByteString, a, ByteString)
x
matches :: Trie a -> ByteString -> [(ByteString, a, ByteString)]
{-# INLINE matches #-}
matches :: Trie a -> ByteString -> [(ByteString, a, ByteString)]
matches Trie a
t ByteString
q = ByteString -> (Int, a) -> (ByteString, a, ByteString)
forall a. ByteString -> (Int, a) -> (ByteString, a, ByteString)
getMatch ByteString
q ((Int, a) -> (ByteString, a, ByteString))
-> [(Int, a)] -> [(ByteString, a, ByteString)]
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> Trie a -> ByteString -> [(Int, a)]
forall a. Trie a -> ByteString -> [(Int, a)]
matches_ Trie a
t ByteString
q
insert :: ByteString -> a -> Trie a -> Trie a
{-# INLINE insert #-}
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)
adjustBy :: (ByteString -> a -> a -> a)
-> ByteString -> a -> Trie a -> Trie a
{-# INLINE adjustBy #-}
adjustBy :: (ByteString -> a -> a -> a) -> ByteString -> a -> Trie a -> Trie a
adjustBy ByteString -> a -> a -> a
f ByteString
q a
x = (a -> a) -> ByteString -> Trie a -> Trie a
forall a. (a -> a) -> ByteString -> Trie a -> Trie a
adjust (ByteString -> a -> a -> a
f ByteString
q a
x) ByteString
q
delete :: ByteString -> Trie a -> Trie a
{-# INLINE delete #-}
delete :: ByteString -> Trie a -> Trie a
delete = (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
_ Trie a
t -> (Maybe a
forall a. Maybe a
Nothing, Trie a
t))
deleteSubmap :: ByteString -> Trie a -> Trie a
{-# INLINE deleteSubmap #-}
deleteSubmap :: ByteString -> Trie a -> Trie a
deleteSubmap = (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
_ Trie a
_ -> (Maybe a
forall a. Maybe a
Nothing, Trie a
forall a. Trie a
empty))
unionL :: Trie a -> Trie a -> Trie a
{-# INLINE unionL #-}
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)
unionR :: Trie a -> Trie a -> Trie a
{-# INLINE unionR #-}
unionR :: Trie a -> Trie a -> Trie a
unionR = (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
y -> a -> Maybe a
forall a. a -> Maybe a
Just a
y)
intersectL :: Trie a -> Trie b -> Trie a
{-# INLINE intersectL #-}
intersectL :: Trie a -> Trie b -> Trie a
intersectL = (a -> b -> Maybe a) -> Trie a -> Trie b -> Trie a
forall a b c. (a -> b -> Maybe c) -> Trie a -> Trie b -> Trie c
intersectBy (\a
x b
_ -> a -> Maybe a
forall a. a -> Maybe a
Just a
x)
intersectR :: Trie a -> Trie b -> Trie b
{-# INLINE intersectR #-}
intersectR :: Trie a -> Trie b -> Trie b
intersectR = (a -> b -> Maybe b) -> Trie a -> Trie b -> Trie b
forall a b c. (a -> b -> Maybe c) -> Trie a -> Trie b -> Trie c
intersectBy (\a
_ b
y -> b -> Maybe b
forall a. a -> Maybe a
Just b
y)