module Data.DAWG.VMap
( VMap (unVMap)
, mkVMap
, empty
, lookup
, insert
) where
import Prelude hiding (lookup)
import Control.Applicative ((<$>))
import Data.Binary (Binary, put, get)
import Data.Vector.Binary ()
import qualified Data.Map as M
import qualified Data.Vector.Unboxed as U
newtype VMap a = VMap { unVMap :: U.Vector (Char, a) }
deriving (Show, Eq, Ord)
instance (Binary a, U.Unbox a) => Binary (VMap a) where
put v = put (unVMap v)
get = VMap <$> get
mkVMap :: U.Unbox a => [(Char, a)] -> VMap a
mkVMap = VMap . U.fromList . M.toAscList . M.fromList
empty :: U.Unbox a => VMap a
empty = VMap U.empty
lookup :: U.Unbox a => Char -> VMap a -> Maybe a
lookup x = fmap snd . U.find ((==x) . fst) . unVMap
insert :: U.Unbox a => Char -> a -> VMap a -> VMap a
insert x y
= VMap . U.fromList . M.toAscList
. M.insert x y
. M.fromList . U.toList . unVMap