Skip to content

Instantly share code, notes, and snippets.

-- the following is guaranteed not to have something at the child node as we
-- checked it above
insertNode contextNode@TrieNode{children=childNodes}
TargetIsSmallerDatum {pre=target,
suffixS= suffixStringOfSource}
= contextNode{ children=(insertNewChildWordNode suffixStringOfSource childNodes) }
insertNode contextNode ExactMatchDatum{} = switchToWord contextNode
data PrefixSuffixTree a = ExactMatchDatum { shared :: a } |
SharedPrefixDatum { shared :: a, suffixT :: a, suffixS :: a } |
TargetIsSmallerDatum { pre :: a, suffixS :: a } |
SourceIsSmallerDatum { pre :: a, suffixT :: a } |
NoMatchDatum deriving (Show,Eq)
data MatchType = TargetIsPrefix | SourceIsPrefix |
SharedPrefix | ExactMatch | NotEqual deriving (Show)
-- utility functions
switchToWord node = node{nodeType = Word}
data NodeType = Top|Word|NonWord
deriving (Show,Eq)
data TrieNode a = TrieNode { wordFragment :: [a], children :: (Map.Map a (TrieNode a)),
nodeType:: NodeType } deriving (Show,Eq)
(defstruct trie-node :word-fragment :type :children)
(defn type-switch [node type-val]
(assoc node :type type-val))
(defn switch-to-word [node]
(type-switch node :word))
(defn switch-to-nonword [node]
(type-switch node :nonword))
; constructors