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