mirror of
https://github.com/GrammaticalFramework/gf-core.git
synced 2026-04-09 04:59:31 -06:00
Added a collapse function to Trie
This commit is contained in:
@@ -12,6 +12,7 @@ module Map
|
||||
(
|
||||
Map,
|
||||
empty,
|
||||
isEmpty,
|
||||
(!), -- lookup operator.
|
||||
(!+), -- lookupMany operator.
|
||||
(|->), -- insert operator.
|
||||
|
||||
@@ -10,6 +10,7 @@
|
||||
|
||||
module RedBlack (
|
||||
emptyTree,
|
||||
isEmpty,
|
||||
Tree,
|
||||
lookupTree,
|
||||
insertTree,
|
||||
@@ -32,6 +33,10 @@ balance color a x b = T color a x b
|
||||
emptyTree :: Tree key el
|
||||
emptyTree = E
|
||||
|
||||
isEmpty :: Tree key el -> Bool
|
||||
isEmpty (E) = True
|
||||
isEmpty _ = False
|
||||
|
||||
lookupTree :: Ord a => a -> Tree a b -> Maybe b
|
||||
lookupTree _ E = Nothing
|
||||
lookupTree x (T _ a (y,z) b)
|
||||
|
||||
@@ -10,6 +10,7 @@
|
||||
|
||||
module Trie (
|
||||
tcompile,
|
||||
collapse,
|
||||
Trie,
|
||||
trieLookup,
|
||||
decompose,
|
||||
@@ -35,6 +36,14 @@ optimize :: TrieT -> Trie
|
||||
optimize (TrieT (xs,res)) = Trie ([(c,optimize t) | (c,t) <- xs] |->+ empty,
|
||||
res)
|
||||
|
||||
collapse :: Trie -> [(String,[(Attr,String)])]
|
||||
collapse trie = collapse' trie []
|
||||
where collapse' (Trie (map,(x:xs))) s = if (isEmpty map) then [(reverse s,(x:xs))]
|
||||
else (reverse s,(x:xs)):
|
||||
concat [ collapse' trie (c:s) | (c,trie) <- flatten map]
|
||||
collapse' (Trie (map,[])) s
|
||||
= concat [ collapse' trie (c:s) | (c,trie) <- flatten map]
|
||||
|
||||
tcompile :: [(String,[(Attr,String)])] -> Trie
|
||||
tcompile xs = optimize $ build xs emptyTrie
|
||||
|
||||
|
||||
Reference in New Issue
Block a user