module Data.DAWG
(
DAWG (..)
, numStates
, lookup
, empty
, fromList
, fromListWith
, fromLang
, insert
, insertWith
, delete
, assocs
, keys
, elems
) where
import Prelude hiding (lookup)
import Control.Applicative ((<$>), (<*>))
import Control.Arrow (first)
import Data.List (foldl')
import Data.Binary (Binary, put, get)
import qualified Control.Monad.State.Strict as S
import Data.DAWG.Internal (Id, Node, Graph)
import qualified Data.DAWG.Internal as I
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)
insertLeaf :: Ord a => GraphM a Id
insertLeaf = do
i <- insertNode (I.Value Nothing)
insertNode (I.Branch i V.empty)
nodeBy :: Id -> GraphM a (Node (Maybe a))
nodeBy i = I.nodeBy i <$> S.get
insertNode :: Ord a => Node (Maybe a) -> GraphM a Id
insertNode = S.state . I.insert
deleteNode :: Ord a => Node (Maybe a) -> GraphM a ()
deleteNode = S.state . mkState . I.delete
insertM :: Ord a => [Int] -> a -> Id -> GraphM a Id
insertM (x:xs) y i = do
n <- nodeBy i
j <- case I.onSym x n of
Just j -> return j
Nothing -> insertLeaf
k <- insertM xs y j
deleteNode n
insertNode (I.subst x k n)
insertM [] y i = do
n <- nodeBy i
w <- nodeBy (I.eps n)
deleteNode w
deleteNode n
j <- insertNode (I.Value $ Just y)
insertNode (n { I.eps = j })
insertWithM :: Ord a => (a -> a -> a) -> [Int] -> a -> Id -> GraphM a Id
insertWithM f (x:xs) y i = do
n <- nodeBy i
j <- case I.onSym x n of
Just j -> return j
Nothing -> insertLeaf
k <- insertWithM f xs y j
deleteNode n
insertNode (I.subst x k n)
insertWithM f [] y i = do
n <- nodeBy i
w <- nodeBy (I.eps n)
deleteNode w
deleteNode n
let y'new = case I.unValue w of
Just y' -> f y y'
Nothing -> y
j <- insertNode (I.Value $ Just y'new)
insertNode (n { I.eps = j })
deleteM :: Ord a => [Int] -> Id -> GraphM a Id
deleteM (x:xs) i = do
n <- nodeBy i
case I.onSym x n of
Nothing -> return i
Just j -> do
k <- deleteM xs j
deleteNode n
insertNode (I.subst x k n)
deleteM [] i = do
n <- nodeBy i
w <- nodeBy (I.eps n)
deleteNode w
deleteNode n
j <- insertLeaf
insertNode (n { I.eps = j })
lookupM :: [Int] -> Id -> GraphM a (Maybe a)
lookupM [] i = do
j <- I.eps <$> nodeBy i
I.unValue <$> nodeBy j
lookupM (x:xs) i = do
n <- nodeBy i
case I.onSym x n of
Just j -> lookupM xs j
Nothing -> return Nothing
assocsAcc :: Graph (Maybe a) -> Id -> [([Int], a)]
assocsAcc g i =
here w ++ concatMap there (I.edges n)
where
n = I.nodeBy i g
w = I.nodeBy (I.eps n) g
here v = case I.unValue v of
Just x -> [([], x)]
Nothing -> []
there (sym, j) = map (first (sym:)) (assocsAcc g j)
data DAWG a b = DAWG
{ graph :: !(Graph (Maybe b))
, root :: !Id }
deriving (Show, Eq, Ord)
instance (Ord b, Binary b) => Binary (DAWG a b) where
put d = do
put (graph d)
put (root d)
get = DAWG <$> get <*> get
empty :: Ord b => DAWG a b
empty =
let (i, g) = S.runState insertLeaf I.empty
in DAWG g i
numStates :: DAWG a b -> Int
numStates = I.size . graph
insert :: (Enum a, Ord b) => [a] -> b -> DAWG a b -> DAWG a b
insert xs' y d =
let xs = map fromEnum xs'
(i, g) = S.runState (insertM xs y $ root d) (graph d)
in DAWG g i
insertWith
:: (Enum a, Ord b) => (b -> b -> b)
-> [a] -> b -> DAWG a b -> DAWG a b
insertWith f xs' y d =
let xs = map fromEnum xs'
(i, g) = S.runState (insertWithM f xs y $ root d) (graph d)
in DAWG g i
delete :: (Enum a, Ord b) => [a] -> DAWG a b -> DAWG a b
delete xs' d =
let xs = map fromEnum xs'
(i, g) = S.runState (deleteM xs $ root d) (graph d)
in DAWG g i
lookup :: Enum a => [a] -> DAWG a b -> Maybe b
lookup xs' d =
let xs = map fromEnum xs'
in S.evalState (lookupM xs $ root d) (graph d)
assocs :: Enum a => DAWG a b -> [([a], b)]
assocs
= map (first (map toEnum))
. (assocsAcc <$> graph <*> root)
keys :: Enum a => DAWG a b -> [[a]]
keys = map fst . assocs
elems :: DAWG a b -> [b]
elems = map snd . (assocsAcc <$> graph <*> root)
fromList :: (Enum a, Ord b) => [([a], b)] -> DAWG a b
fromList xs =
let update t (x, v) = insert x v t
in foldl' update empty xs
fromListWith :: (Enum a, Ord b) => (b -> b -> b) -> [([a], b)] -> DAWG a b
fromListWith f xs =
let update t (x, v) = insertWith f x v t
in foldl' update empty xs
fromLang :: Enum a => [[a]] -> DAWG a ()
fromLang xs = fromList [(x, ()) | x <- xs]