TernaryTrees-0.1.2.0: Data/Set/StringSet.hs
module Data.Set.StringSet (
StringSet,
insert,
singleton,
member,
size,
fromList
) where
import Data.Bits
import Data.Binary
import Control.Monad
-- | StringSet is ternary tree. It is commonly used for storing word lists
-- like dictionaries for spell checking etc.
data StringSet = Node {-# UNPACK #-} !Char !StringSet !StringSet !StringSet -- | Tree node
| Null !StringSet -- | null nodes can only have a greater than branch by definition
| End -- | a branch that doesn’t contain anything
deriving (Show, Eq)
-- | Inserts a new list of elements into a tree.
insert :: String -> StringSet -> StringSet
insert xss@(_:_) End = singleton xss
insert xss@(_:_) (Null rest) = Null $ insert xss rest
insert [] End = Null End
insert [] (Node ele l e h) = Node ele (insert [] l) e h
insert [] (Null rest) = Null rest
insert xss@(x:xs) (Node ele l e h) =
case compare x ele of
LT -> Node ele (insert xss l) e h
EQ -> Node ele l (insert xs e) h
GT -> Node ele l e (insert xss h)
-- | Quickly build an initial tree.
singleton :: String -> StringSet
singleton (x:xs) = Node x End (singleton xs) End
singleton [] = Null End
-- | Returns true if the string is in the StringSet
member :: String -> StringSet -> Bool
member _ End = False
member [] (Null _) = True
member [] (Node _ l _ _) = member [] l
member xss@(_:_) (Null rest) = member xss rest
member xss@(x:xs) (Node ele l e h) =
case compare x ele of
LT -> member xss l
EQ -> member xs e
GT -> member xss h
-- | Returns the number of non-Null Elems
treeSize :: StringSet -> Int
treeSize End = 0
treeSize (Null rest) = treeSize rest
treeSize (Node _ l e h) = 1 + treeSize l + treeSize e + treeSize h
-- | Counts how many entries there are in the tree.
size :: StringSet -> Int
size End = 0
size (Null rest) = 1 + size rest
size (Node _ l e h) = size l + size e + size h
-- | Creates a new tree from a list of 'strings'
fromList :: [String] -> StringSet
fromList = foldl (flip insert) empty
-- | An empty set.
empty :: StringSet
empty = End
-- | Returns true if the set is empty.
null :: StringSet -> Bool
null End = True
null _ = False
-- | A rather long Binary instance, that uses binary numbers to indicate
-- where Ends are efficiently.
instance Binary StringSet where
put (Node ch End End End) = do
putWord8 0
put ch
put (Node ch End End h) = do
putWord8 1
put ch
put h
put (Node ch End e End) = do
putWord8 2
put ch
put e
put (Node ch End e h) = do
putWord8 3
put ch
put e
put h
put (Node ch l End End) = do
putWord8 4
put ch
put l
put (Node ch l End h) = do
putWord8 5
put ch
put l
put h
put (Node ch l e End) = do
putWord8 6
put ch
put l
put e
-- General case
put (Node ch l e h) = do
putWord8 7
put ch
put l
put e
put h
put (Null End) = putWord8 8
put (Null rest) = do
putWord8 9
put rest
put End = putWord8 10
get = do
tag <- getWord8
case tag of
_ | tag < 8 ->
do
ch <- get
l <- if (tag `testBit` 2) then get else return End
e <- if (tag `testBit` 1) then get else return End
h <- if (tag `testBit` 0) then get else return End
return (Node ch l e h)
8 -> return (Null End)
9 -> liftM Null get
_ -> return End