module Data.Trie.Pred.Interface
(
PTBuilderT (..)
, execPTBuilderT
,
insert
, insertHere
, prefix
,
only
, pred
, (./)
, nil
,
lookup
, match
, matches
,
delete
,
RootedPredTrie
, PathChunks
, PathChunk
) where
import Prelude hiding (lookup, pred)
import Data.Trie.Pred.Base
import Data.Trie.Pred.Interface.Types
import Data.Function.Poly
import qualified Data.Trie.Class as TC
import Data.Hashable
import Data.Monoid
import Control.Monad.State
import Control.Monad.Writer
newtype PTBuilderT k v m a = PTBuilderT
{ runPTBuilderT :: StateT (RootedPredTrie k v) m a
} deriving (Functor, Applicative, Monad, MonadTrans, MonadState (RootedPredTrie k v))
instance ( Monad m
, Eq k
, Hashable k
) => MonadWriter (RootedPredTrie k v) (PTBuilderT k v m) where
tell x = modify' (x <>)
listen x = do
x' <- x
w <- get
return (x', w)
pass x = do
(x', f) <- x
modify' f
return x'
execPTBuilderT :: ( Monad m
, Eq k
, Hashable k
) => PTBuilderT k v m a -> m (RootedPredTrie k v)
execPTBuilderT = flip execStateT mempty . runPTBuilderT
insert :: ( Monad m
, Eq k
, Hashable k
, Singleton (PathChunks k xs)
childContent
(RootedPredTrie k resultContent)
, cleanxs ~ CatMaybes xs
, ArityTypeListIso childContent cleanxs resultContent
) => PathChunks k xs
-> childContent
-> PTBuilderT k resultContent m ()
insert !ts !vl =
modify' ((singleton ts vl) <>)
insertHere :: ( Monad m
, Eq k
, Hashable k
) => v
-> PTBuilderT k v m ()
insertHere = insert nil
prefix :: ( Monad m
, Eq k
, Hashable k
, cleanxs ~ CatMaybes xs
, ExtrudeSoundly k cleanxs xs childContent resultContent
) => PathChunks k xs
-> PTBuilderT k childContent m ()
-> PTBuilderT k resultContent m ()
prefix !ts cs = do
trie <- lift (execPTBuilderT cs)
modify' ((extrude ts trie) <>)
lookup :: ( Eq k
, Hashable k
) => [k] -> RootedPredTrie k a -> Maybe a
lookup = TC.lookup
delete :: ( Eq k
, Hashable k
) => [k] -> RootedPredTrie k a -> RootedPredTrie k a
delete = TC.delete
match :: ( Hashable k
, Eq k
) => [k] -> RootedPredTrie k a -> Maybe ([k], a, [k])
match = matchRPT
matches :: ( Hashable k
, Eq k
) => [k] -> RootedPredTrie k a -> [([k], a, [k])]
matches = matchesRPT