module Data.DAWG.Wrapper
( DAWG (..)
, empty
, numStates
, insert
, delete
, lookup
, fromList
, fromLang
) where
import Prelude hiding (lookup)
import Data.List (foldl')
import qualified Control.Monad.Codec as C
import qualified Data.DAWG as D
data DAWG a = DAWG
{ dawg :: !(D.DAWG Int)
, codec :: !(C.AtomCodec a) }
empty :: DAWG a
empty = DAWG D.empty C.empty
numStates :: DAWG a -> Int
numStates = D.numStates . dawg
insert :: Ord a => String -> a -> DAWG a -> DAWG a
insert xs y d =
let (y', c') = C.runCodec (codec d) (C.encode C.idLens y)
in DAWG (D.insert xs y' (dawg d)) c'
delete :: Ord a => String -> DAWG a -> DAWG a
delete xs d = DAWG (D.delete xs (dawg d)) (codec d)
lookup :: Ord a => String -> DAWG a -> Maybe a
lookup xs d =
D.lookup xs (dawg d) >>=
C.evalCodec (codec d) . C.maybeDecode C.idLens
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]