Mercurial > hg > Members > toma > Jungle-haskell
changeset 4:48ed16468aaa
add Treename
author | Daichi TOMA <toma@cr.ie.u-ryukyu.ac.jp> |
---|---|
date | Tue, 09 Apr 2013 17:40:00 +0900 |
parents | 090bdde20e9f |
children | afdd87f73f17 |
files | Jungle.hs |
diffstat | 1 files changed, 11 insertions(+), 5 deletions(-) [+] |
line wrap: on
line diff
--- a/Jungle.hs Tue Mar 26 18:28:54 2013 +0900 +++ b/Jungle.hs Tue Apr 09 17:40:00 2013 +0900 @@ -1,11 +1,13 @@ module Jungle ( Jungle -, Tree +, Tree (Tree) , Node (Empty) , Children , Attributes +, Path , createJungle , createTree +, updateTree , getTreeByName , getRootNode , getChildren @@ -32,6 +34,7 @@ data Tree = Tree { rootNode :: Node + , treeName :: String } deriving (Show) data Jungle = Jungle (Map.Map String Tree) deriving (Show) @@ -44,12 +47,15 @@ createTree :: Jungle -> String -> Jungle createTree (Jungle map) tree_name = Jungle (Map.insert tree_name emptyTree map) where - emptyTree = Tree Empty + emptyTree = Tree Empty tree_name + +updateTree :: Jungle -> Tree -> Jungle +updateTree (Jungle map) tree@(Tree node name) = Jungle (Map.insert name tree map) getTreeByName :: Jungle -> String -> Tree getTreeByName (Jungle map) tree_name = Map.findWithDefault emptyTree tree_name map where - emptyTree = Tree Empty + emptyTree = Tree Empty tree_name getRootNode :: Tree -> Node getRootNode tree = rootNode tree @@ -67,7 +73,7 @@ get (Attributes map) key = Map.findWithDefault B.empty key map addNewChildAt :: Tree -> Path -> Int -> Node -> Tree -addNewChildAt tree path pos node = Tree $ addNewChildAt' (getRootNode tree) path pos node +addNewChildAt tree@(Tree root name) path pos node = Tree (addNewChildAt' (getRootNode tree) path pos node) name addNewChildAt' :: Node -> Path -> Int -> Node -> Node addNewChildAt' parent [] pos new_child = addChild parent pos new_child @@ -116,6 +122,6 @@ deleteAttribute' (Node children (Attributes map)) key = Node children (Attributes (Map.delete key map)) editTree :: Tree -> Path -> Node -> Tree -editTree _ [] node = Tree node +editTree (Tree root name) [] node = Tree node name editTree tree path node = addNewChildAt tree (init path) (last path) node