module Data.DAWG
( DAWG (..)
, empty
, numStates
, insert
, delete
, lookup
, fromList
, fromLang
) where
import Prelude hiding (lookup)
import Control.Applicative ((<$>), (<*>))
import Data.List (foldl')
import Data.Binary (Binary, put, get)
import qualified Control.Monad.State.Strict as S
import Data.DAWG.Graph (Id, Node, Graph)
import qualified Data.DAWG.Graph as G
import qualified Data.DAWG.VMap as V
type GraphM a b = S.State (Graph (Maybe a)) b
mkState :: (Graph a -> Graph a) -> Graph a -> ((), Graph a)
mkState f g = ((), f g)
leaf :: Node (Maybe a)
leaf = G.Node
{ G.value = Nothing
, G.edges = V.empty }
nodeBy :: Id -> GraphM a (Node (Maybe a))
nodeBy i = G.nodeBy i <$> S.get
insertNode :: Ord a => Node (Maybe a) -> GraphM a Id
insertNode = S.state . G.insert
deleteNode :: Ord a => Node (Maybe a) -> GraphM a ()
deleteNode = S.state . mkState . G.delete
insertM :: Ord a => String -> a -> Id -> GraphM a Id
insertM [] y i = do
n <- nodeBy i
deleteNode n
insertNode (n { G.value = Just y })
insertM (x:xs) y i = do
n <- nodeBy i
j <- case G.onChar x n of
Just j -> return j
Nothing -> insertNode leaf
k <- insertM xs y j
deleteNode n
insertNode (G.subst x k n)
deleteM :: Ord a => String -> Id -> GraphM a Id
deleteM [] i = do
n <- nodeBy i
deleteNode n
insertNode (n { G.value = Nothing })
deleteM (x:xs) i = do
n <- nodeBy i
case G.onChar x n of
Nothing -> return i
Just j -> do
k <- deleteM xs j
deleteNode n
insertNode (G.subst x k n)
lookupM :: String -> Id -> GraphM a (Maybe a)
lookupM [] i = G.value <$> nodeBy i
lookupM (x:xs) i = do
n <- nodeBy i
case G.onChar x n of
Just j -> lookupM xs j
Nothing -> return Nothing
data DAWG a = DAWG
{ graph :: !(Graph (Maybe a))
, root :: !Id }
deriving (Show, Eq, Ord)
instance (Ord a, Binary a) => Binary (DAWG a) where
put d = do
put (graph d)
put (root d)
get = DAWG <$> get <*> get
empty :: Ord a => DAWG a
empty =
let (i, g) = G.insert leaf G.empty
in DAWG g i
numStates :: DAWG a -> Int
numStates = G.size . graph
insert :: Ord a => String -> a -> DAWG a -> DAWG a
insert xs y d =
let (i, g) = S.runState (insertM xs y $ root d) (graph d)
in DAWG g i
delete :: Ord a => String -> DAWG a -> DAWG a
delete xs d =
let (i, g) = S.runState (deleteM xs $ root d) (graph d)
in DAWG g i
lookup :: String -> DAWG a -> Maybe a
lookup xs d = S.evalState (lookupM xs $ root d) (graph d)
fromList :: Ord a => [(String, a)] -> DAWG a
fromList xs =
let update t (x, v) = insert x v t
in foldl' update empty xs
fromLang :: [String] -> DAWG ()
fromLang xs = fromList [(x, ()) | x <- xs]