module Data.Trie.Class where
import Prelude hiding (lookup)
import qualified Data.Trie as BT
import qualified Data.ByteString as BS
import Data.Maybe (isJust)
import Data.Foldable as F
import Data.Functor.Identity (Identity (..))
class Trie p s t | t -> p where
lookup :: p s -> t s a -> Maybe a
insert :: p s -> a -> t s a -> t s a
delete :: p s -> t s a -> t s a
member :: Trie p s t => p s -> t s a -> Bool
member t = isJust . lookup t
notMember :: Trie p s t => p s -> t s a -> Bool
notMember t = not . member t
fromFoldable :: (Foldable f, Monoid (t s a), Trie p s t) => f (p s, a) -> t s a
fromFoldable = F.foldr (uncurry insert) mempty
newtype BSTrie q a = BSTrie {unBSTrie :: (q, BT.Trie a)}
makeBSTrie :: BT.Trie a -> BSTrie BS.ByteString a
makeBSTrie x = BSTrie (mempty,x)
getBSTrie :: BSTrie BS.ByteString a -> BT.Trie a
getBSTrie (BSTrie (_,x)) = x
instance Trie Identity BS.ByteString BSTrie where
lookup (Identity ps) (BSTrie (_,xs)) = BT.lookup ps xs
insert (Identity ps) x (BSTrie (q,xs)) = BSTrie (q, BT.insert ps x xs)
delete (Identity ps) (BSTrie (q,xs)) = BSTrie (q, BT.delete ps xs)