module Data.DAWG.Static.Node
( Node(..)
, onSym
, onSym'
, edges
, children
, insert
, fromDyn
) where
import Control.Arrow (second)
import Control.Applicative ((<$>), (<*>))
import Data.Binary (Binary, Get, put, get)
import Data.Vector.Binary ()
import qualified Data.Vector.Unboxed as U
import Data.DAWG.Types
import Data.DAWG.Trans.Vector (Trans)
import qualified Data.DAWG.Trans as T
import qualified Data.DAWG.Dynamic.Node as D
data Node a b
= Branch {
eps :: !ID
, transMap :: !Trans
, labelVect :: !(U.Vector a) }
| Leaf { value :: !(Maybe b) }
deriving (Show, Eq, Ord)
instance (U.Unbox a, Binary a, Binary b) => Binary (Node a b) where
put Branch{..} = put (1 :: Int) >> put eps >> put transMap >> put labelVect
put Leaf{..} = put (2 :: Int) >> put value
get = do
x <- get :: Get Int
case x of
1 -> Branch <$> get <*> get <*> get
_ -> Leaf <$> get
onSym :: Sym -> Node a b -> Maybe ID
onSym x (Branch _ t _) = T.lookup x t
onSym _ (Leaf _) = Nothing
onSym' :: U.Unbox a => Sym -> Node a b -> Maybe (ID, a)
onSym' x (Branch _ t ls) = do
k <- T.index x t
(,) <$> (snd <$> T.byIndex k t)
<*> ls U.!? k
onSym' _ (Leaf _) = Nothing
edges :: Node a b -> [(Sym, ID)]
edges (Branch _ t _) = T.toList t
edges (Leaf _) = []
children :: Node a b -> [ID]
children = map snd . edges
insert :: Sym -> ID -> Node a b -> Node a b
insert x i (Branch w t ls) = Branch w (T.insert x i t) ls
insert _ _ l = l
fromDyn
:: (ID -> ID)
-> D.Node b
-> Node () b
fromDyn _ (D.Leaf x) = Leaf x
fromDyn f (D.Branch e t) =
let reTrans = T.fromList . map (second f) . T.toList
in Branch (f e) (reTrans t) U.empty