annotate Jungle.hs @ 2:392c3f30c076

change to String from ByteString
author Daichi TOMA <toma@cr.ie.u-ryukyu.ac.jp>
date Tue, 26 Mar 2013 17:30:20 +0900
parents 98e1a35e4ab0
children 090bdde20e9f
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
1
98e1a35e4ab0 Rewrite almost and Modularization
Daichi TOMA <toma@cr.ie.u-ryukyu.ac.jp>
parents: 0
diff changeset
1 module Jungle
98e1a35e4ab0 Rewrite almost and Modularization
Daichi TOMA <toma@cr.ie.u-ryukyu.ac.jp>
parents: 0
diff changeset
2 ( Jungle
98e1a35e4ab0 Rewrite almost and Modularization
Daichi TOMA <toma@cr.ie.u-ryukyu.ac.jp>
parents: 0
diff changeset
3 , Tree
98e1a35e4ab0 Rewrite almost and Modularization
Daichi TOMA <toma@cr.ie.u-ryukyu.ac.jp>
parents: 0
diff changeset
4 , Node
98e1a35e4ab0 Rewrite almost and Modularization
Daichi TOMA <toma@cr.ie.u-ryukyu.ac.jp>
parents: 0
diff changeset
5 , Children
98e1a35e4ab0 Rewrite almost and Modularization
Daichi TOMA <toma@cr.ie.u-ryukyu.ac.jp>
parents: 0
diff changeset
6 , Attributes
98e1a35e4ab0 Rewrite almost and Modularization
Daichi TOMA <toma@cr.ie.u-ryukyu.ac.jp>
parents: 0
diff changeset
7 , createJungle
98e1a35e4ab0 Rewrite almost and Modularization
Daichi TOMA <toma@cr.ie.u-ryukyu.ac.jp>
parents: 0
diff changeset
8 , createTree
98e1a35e4ab0 Rewrite almost and Modularization
Daichi TOMA <toma@cr.ie.u-ryukyu.ac.jp>
parents: 0
diff changeset
9 , getTreeByName
98e1a35e4ab0 Rewrite almost and Modularization
Daichi TOMA <toma@cr.ie.u-ryukyu.ac.jp>
parents: 0
diff changeset
10 , getRootNode
98e1a35e4ab0 Rewrite almost and Modularization
Daichi TOMA <toma@cr.ie.u-ryukyu.ac.jp>
parents: 0
diff changeset
11 , getChildren
98e1a35e4ab0 Rewrite almost and Modularization
Daichi TOMA <toma@cr.ie.u-ryukyu.ac.jp>
parents: 0
diff changeset
12 , getAttributes
98e1a35e4ab0 Rewrite almost and Modularization
Daichi TOMA <toma@cr.ie.u-ryukyu.ac.jp>
parents: 0
diff changeset
13 , at
98e1a35e4ab0 Rewrite almost and Modularization
Daichi TOMA <toma@cr.ie.u-ryukyu.ac.jp>
parents: 0
diff changeset
14 , get
98e1a35e4ab0 Rewrite almost and Modularization
Daichi TOMA <toma@cr.ie.u-ryukyu.ac.jp>
parents: 0
diff changeset
15 , addNewChildAt
98e1a35e4ab0 Rewrite almost and Modularization
Daichi TOMA <toma@cr.ie.u-ryukyu.ac.jp>
parents: 0
diff changeset
16 , deleteChildAt
98e1a35e4ab0 Rewrite almost and Modularization
Daichi TOMA <toma@cr.ie.u-ryukyu.ac.jp>
parents: 0
diff changeset
17 , putAttribute
98e1a35e4ab0 Rewrite almost and Modularization
Daichi TOMA <toma@cr.ie.u-ryukyu.ac.jp>
parents: 0
diff changeset
18 , deleteAttribute
98e1a35e4ab0 Rewrite almost and Modularization
Daichi TOMA <toma@cr.ie.u-ryukyu.ac.jp>
parents: 0
diff changeset
19 ) where
0
329f462d5dad add nondestructive tree structure.
Daichi TOMA <toma@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
20
1
98e1a35e4ab0 Rewrite almost and Modularization
Daichi TOMA <toma@cr.ie.u-ryukyu.ac.jp>
parents: 0
diff changeset
21 import qualified Data.Map as Map
98e1a35e4ab0 Rewrite almost and Modularization
Daichi TOMA <toma@cr.ie.u-ryukyu.ac.jp>
parents: 0
diff changeset
22
98e1a35e4ab0 Rewrite almost and Modularization
Daichi TOMA <toma@cr.ie.u-ryukyu.ac.jp>
parents: 0
diff changeset
23 data Children = Children (Map.Map Int Node) deriving (Show)
2
392c3f30c076 change to String from ByteString
Daichi TOMA <toma@cr.ie.u-ryukyu.ac.jp>
parents: 1
diff changeset
24 data Attributes = Attributes (Map.Map String String) deriving (Show)
0
329f462d5dad add nondestructive tree structure.
Daichi TOMA <toma@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
25
329f462d5dad add nondestructive tree structure.
Daichi TOMA <toma@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
26 data Node = Empty
1
98e1a35e4ab0 Rewrite almost and Modularization
Daichi TOMA <toma@cr.ie.u-ryukyu.ac.jp>
parents: 0
diff changeset
27 | Node
98e1a35e4ab0 Rewrite almost and Modularization
Daichi TOMA <toma@cr.ie.u-ryukyu.ac.jp>
parents: 0
diff changeset
28 { children :: Children
98e1a35e4ab0 Rewrite almost and Modularization
Daichi TOMA <toma@cr.ie.u-ryukyu.ac.jp>
parents: 0
diff changeset
29 , attributes :: Attributes
98e1a35e4ab0 Rewrite almost and Modularization
Daichi TOMA <toma@cr.ie.u-ryukyu.ac.jp>
parents: 0
diff changeset
30 } deriving (Show)
0
329f462d5dad add nondestructive tree structure.
Daichi TOMA <toma@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
31
1
98e1a35e4ab0 Rewrite almost and Modularization
Daichi TOMA <toma@cr.ie.u-ryukyu.ac.jp>
parents: 0
diff changeset
32 data Tree = Tree
98e1a35e4ab0 Rewrite almost and Modularization
Daichi TOMA <toma@cr.ie.u-ryukyu.ac.jp>
parents: 0
diff changeset
33 { rootNode :: Node
98e1a35e4ab0 Rewrite almost and Modularization
Daichi TOMA <toma@cr.ie.u-ryukyu.ac.jp>
parents: 0
diff changeset
34 } deriving (Show)
0
329f462d5dad add nondestructive tree structure.
Daichi TOMA <toma@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
35
1
98e1a35e4ab0 Rewrite almost and Modularization
Daichi TOMA <toma@cr.ie.u-ryukyu.ac.jp>
parents: 0
diff changeset
36 data Jungle = Jungle (Map.Map String Tree) deriving (Show)
98e1a35e4ab0 Rewrite almost and Modularization
Daichi TOMA <toma@cr.ie.u-ryukyu.ac.jp>
parents: 0
diff changeset
37
98e1a35e4ab0 Rewrite almost and Modularization
Daichi TOMA <toma@cr.ie.u-ryukyu.ac.jp>
parents: 0
diff changeset
38 type Path = [Int]
0
329f462d5dad add nondestructive tree structure.
Daichi TOMA <toma@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
39
1
98e1a35e4ab0 Rewrite almost and Modularization
Daichi TOMA <toma@cr.ie.u-ryukyu.ac.jp>
parents: 0
diff changeset
40 createJungle :: Jungle
98e1a35e4ab0 Rewrite almost and Modularization
Daichi TOMA <toma@cr.ie.u-ryukyu.ac.jp>
parents: 0
diff changeset
41 createJungle = Jungle Map.empty
0
329f462d5dad add nondestructive tree structure.
Daichi TOMA <toma@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
42
1
98e1a35e4ab0 Rewrite almost and Modularization
Daichi TOMA <toma@cr.ie.u-ryukyu.ac.jp>
parents: 0
diff changeset
43 createTree :: Jungle -> String -> Jungle
98e1a35e4ab0 Rewrite almost and Modularization
Daichi TOMA <toma@cr.ie.u-ryukyu.ac.jp>
parents: 0
diff changeset
44 createTree (Jungle map) tree_name = Jungle (Map.insert tree_name emptyTree map)
98e1a35e4ab0 Rewrite almost and Modularization
Daichi TOMA <toma@cr.ie.u-ryukyu.ac.jp>
parents: 0
diff changeset
45 where
98e1a35e4ab0 Rewrite almost and Modularization
Daichi TOMA <toma@cr.ie.u-ryukyu.ac.jp>
parents: 0
diff changeset
46 emptyTree = Tree Empty
0
329f462d5dad add nondestructive tree structure.
Daichi TOMA <toma@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
47
1
98e1a35e4ab0 Rewrite almost and Modularization
Daichi TOMA <toma@cr.ie.u-ryukyu.ac.jp>
parents: 0
diff changeset
48 getTreeByName :: Jungle -> String -> Tree
98e1a35e4ab0 Rewrite almost and Modularization
Daichi TOMA <toma@cr.ie.u-ryukyu.ac.jp>
parents: 0
diff changeset
49 getTreeByName (Jungle map) tree_name = Map.findWithDefault emptyTree tree_name map
98e1a35e4ab0 Rewrite almost and Modularization
Daichi TOMA <toma@cr.ie.u-ryukyu.ac.jp>
parents: 0
diff changeset
50 where
98e1a35e4ab0 Rewrite almost and Modularization
Daichi TOMA <toma@cr.ie.u-ryukyu.ac.jp>
parents: 0
diff changeset
51 emptyTree = Tree Empty
98e1a35e4ab0 Rewrite almost and Modularization
Daichi TOMA <toma@cr.ie.u-ryukyu.ac.jp>
parents: 0
diff changeset
52
98e1a35e4ab0 Rewrite almost and Modularization
Daichi TOMA <toma@cr.ie.u-ryukyu.ac.jp>
parents: 0
diff changeset
53 getRootNode :: Tree -> Node
98e1a35e4ab0 Rewrite almost and Modularization
Daichi TOMA <toma@cr.ie.u-ryukyu.ac.jp>
parents: 0
diff changeset
54 getRootNode tree = rootNode tree
98e1a35e4ab0 Rewrite almost and Modularization
Daichi TOMA <toma@cr.ie.u-ryukyu.ac.jp>
parents: 0
diff changeset
55
98e1a35e4ab0 Rewrite almost and Modularization
Daichi TOMA <toma@cr.ie.u-ryukyu.ac.jp>
parents: 0
diff changeset
56 getChildren :: Node -> Children
98e1a35e4ab0 Rewrite almost and Modularization
Daichi TOMA <toma@cr.ie.u-ryukyu.ac.jp>
parents: 0
diff changeset
57 getChildren node = children node
0
329f462d5dad add nondestructive tree structure.
Daichi TOMA <toma@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
58
1
98e1a35e4ab0 Rewrite almost and Modularization
Daichi TOMA <toma@cr.ie.u-ryukyu.ac.jp>
parents: 0
diff changeset
59 getAttributes :: Node -> Attributes
98e1a35e4ab0 Rewrite almost and Modularization
Daichi TOMA <toma@cr.ie.u-ryukyu.ac.jp>
parents: 0
diff changeset
60 getAttributes node = attributes node
98e1a35e4ab0 Rewrite almost and Modularization
Daichi TOMA <toma@cr.ie.u-ryukyu.ac.jp>
parents: 0
diff changeset
61
98e1a35e4ab0 Rewrite almost and Modularization
Daichi TOMA <toma@cr.ie.u-ryukyu.ac.jp>
parents: 0
diff changeset
62 at :: Children -> Int -> Node
98e1a35e4ab0 Rewrite almost and Modularization
Daichi TOMA <toma@cr.ie.u-ryukyu.ac.jp>
parents: 0
diff changeset
63 at (Children map) pos = Map.findWithDefault Empty pos map
0
329f462d5dad add nondestructive tree structure.
Daichi TOMA <toma@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
64
2
392c3f30c076 change to String from ByteString
Daichi TOMA <toma@cr.ie.u-ryukyu.ac.jp>
parents: 1
diff changeset
65 get :: Attributes -> String -> String
392c3f30c076 change to String from ByteString
Daichi TOMA <toma@cr.ie.u-ryukyu.ac.jp>
parents: 1
diff changeset
66 get (Attributes map) key = Map.findWithDefault "" key map
1
98e1a35e4ab0 Rewrite almost and Modularization
Daichi TOMA <toma@cr.ie.u-ryukyu.ac.jp>
parents: 0
diff changeset
67
98e1a35e4ab0 Rewrite almost and Modularization
Daichi TOMA <toma@cr.ie.u-ryukyu.ac.jp>
parents: 0
diff changeset
68 addNewChildAt :: Tree -> Path -> Int -> Node -> Tree
98e1a35e4ab0 Rewrite almost and Modularization
Daichi TOMA <toma@cr.ie.u-ryukyu.ac.jp>
parents: 0
diff changeset
69 addNewChildAt tree path pos node = Tree $ addNewChildAt' (getRootNode tree) path pos node
0
329f462d5dad add nondestructive tree structure.
Daichi TOMA <toma@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
70
1
98e1a35e4ab0 Rewrite almost and Modularization
Daichi TOMA <toma@cr.ie.u-ryukyu.ac.jp>
parents: 0
diff changeset
71 addNewChildAt' :: Node -> Path -> Int -> Node -> Node
98e1a35e4ab0 Rewrite almost and Modularization
Daichi TOMA <toma@cr.ie.u-ryukyu.ac.jp>
parents: 0
diff changeset
72 addNewChildAt' parent [] pos new_child = addChild parent pos new_child
98e1a35e4ab0 Rewrite almost and Modularization
Daichi TOMA <toma@cr.ie.u-ryukyu.ac.jp>
parents: 0
diff changeset
73 addNewChildAt' parent (x:xs) pos new_child = addChild parent x (addNewChildAt' (child x) xs pos new_child)
98e1a35e4ab0 Rewrite almost and Modularization
Daichi TOMA <toma@cr.ie.u-ryukyu.ac.jp>
parents: 0
diff changeset
74 where
98e1a35e4ab0 Rewrite almost and Modularization
Daichi TOMA <toma@cr.ie.u-ryukyu.ac.jp>
parents: 0
diff changeset
75 child = at (getChildren parent)
0
329f462d5dad add nondestructive tree structure.
Daichi TOMA <toma@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
76
1
98e1a35e4ab0 Rewrite almost and Modularization
Daichi TOMA <toma@cr.ie.u-ryukyu.ac.jp>
parents: 0
diff changeset
77 deleteChildAt :: Tree -> Path -> Int -> Tree
98e1a35e4ab0 Rewrite almost and Modularization
Daichi TOMA <toma@cr.ie.u-ryukyu.ac.jp>
parents: 0
diff changeset
78 deleteChildAt tree path pos = editTree tree path (deleteChild target pos)
98e1a35e4ab0 Rewrite almost and Modularization
Daichi TOMA <toma@cr.ie.u-ryukyu.ac.jp>
parents: 0
diff changeset
79 where
98e1a35e4ab0 Rewrite almost and Modularization
Daichi TOMA <toma@cr.ie.u-ryukyu.ac.jp>
parents: 0
diff changeset
80 root = getRootNode tree
98e1a35e4ab0 Rewrite almost and Modularization
Daichi TOMA <toma@cr.ie.u-ryukyu.ac.jp>
parents: 0
diff changeset
81 target = getNode root path
98e1a35e4ab0 Rewrite almost and Modularization
Daichi TOMA <toma@cr.ie.u-ryukyu.ac.jp>
parents: 0
diff changeset
82
98e1a35e4ab0 Rewrite almost and Modularization
Daichi TOMA <toma@cr.ie.u-ryukyu.ac.jp>
parents: 0
diff changeset
83 addChild :: Node -> Int -> Node -> Node
98e1a35e4ab0 Rewrite almost and Modularization
Daichi TOMA <toma@cr.ie.u-ryukyu.ac.jp>
parents: 0
diff changeset
84 addChild Empty pos child = addChild (Node (Children Map.empty) (Attributes Map.empty)) pos child
98e1a35e4ab0 Rewrite almost and Modularization
Daichi TOMA <toma@cr.ie.u-ryukyu.ac.jp>
parents: 0
diff changeset
85 addChild (Node (Children map) attributes) pos child = Node (Children (Map.insert pos child map)) attributes
98e1a35e4ab0 Rewrite almost and Modularization
Daichi TOMA <toma@cr.ie.u-ryukyu.ac.jp>
parents: 0
diff changeset
86
98e1a35e4ab0 Rewrite almost and Modularization
Daichi TOMA <toma@cr.ie.u-ryukyu.ac.jp>
parents: 0
diff changeset
87 getNode :: Node -> Path -> Node
98e1a35e4ab0 Rewrite almost and Modularization
Daichi TOMA <toma@cr.ie.u-ryukyu.ac.jp>
parents: 0
diff changeset
88 getNode node [] = node
98e1a35e4ab0 Rewrite almost and Modularization
Daichi TOMA <toma@cr.ie.u-ryukyu.ac.jp>
parents: 0
diff changeset
89 getNode node (x:xs) = getNode (child x) xs
98e1a35e4ab0 Rewrite almost and Modularization
Daichi TOMA <toma@cr.ie.u-ryukyu.ac.jp>
parents: 0
diff changeset
90 where
98e1a35e4ab0 Rewrite almost and Modularization
Daichi TOMA <toma@cr.ie.u-ryukyu.ac.jp>
parents: 0
diff changeset
91 child = at (getChildren node)
0
329f462d5dad add nondestructive tree structure.
Daichi TOMA <toma@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
92
1
98e1a35e4ab0 Rewrite almost and Modularization
Daichi TOMA <toma@cr.ie.u-ryukyu.ac.jp>
parents: 0
diff changeset
93 deleteChild :: Node -> Int -> Node
98e1a35e4ab0 Rewrite almost and Modularization
Daichi TOMA <toma@cr.ie.u-ryukyu.ac.jp>
parents: 0
diff changeset
94 deleteChild Empty _ = Empty
98e1a35e4ab0 Rewrite almost and Modularization
Daichi TOMA <toma@cr.ie.u-ryukyu.ac.jp>
parents: 0
diff changeset
95 deleteChild (Node (Children map) attributes) pos = Node (Children (Map.delete pos map)) attributes
98e1a35e4ab0 Rewrite almost and Modularization
Daichi TOMA <toma@cr.ie.u-ryukyu.ac.jp>
parents: 0
diff changeset
96
2
392c3f30c076 change to String from ByteString
Daichi TOMA <toma@cr.ie.u-ryukyu.ac.jp>
parents: 1
diff changeset
97 putAttribute :: Tree -> Path -> String -> String -> Tree
1
98e1a35e4ab0 Rewrite almost and Modularization
Daichi TOMA <toma@cr.ie.u-ryukyu.ac.jp>
parents: 0
diff changeset
98 putAttribute tree path key value = editTree tree path (putAttribute' target key value)
98e1a35e4ab0 Rewrite almost and Modularization
Daichi TOMA <toma@cr.ie.u-ryukyu.ac.jp>
parents: 0
diff changeset
99 where
98e1a35e4ab0 Rewrite almost and Modularization
Daichi TOMA <toma@cr.ie.u-ryukyu.ac.jp>
parents: 0
diff changeset
100 root = getRootNode tree
98e1a35e4ab0 Rewrite almost and Modularization
Daichi TOMA <toma@cr.ie.u-ryukyu.ac.jp>
parents: 0
diff changeset
101 target = getNode root path
0
329f462d5dad add nondestructive tree structure.
Daichi TOMA <toma@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
102
1
98e1a35e4ab0 Rewrite almost and Modularization
Daichi TOMA <toma@cr.ie.u-ryukyu.ac.jp>
parents: 0
diff changeset
103 deleteAttribute :: Tree -> Path -> String -> Tree
98e1a35e4ab0 Rewrite almost and Modularization
Daichi TOMA <toma@cr.ie.u-ryukyu.ac.jp>
parents: 0
diff changeset
104 deleteAttribute tree path key = editTree tree path (deleteAttribute' target key)
98e1a35e4ab0 Rewrite almost and Modularization
Daichi TOMA <toma@cr.ie.u-ryukyu.ac.jp>
parents: 0
diff changeset
105 where
98e1a35e4ab0 Rewrite almost and Modularization
Daichi TOMA <toma@cr.ie.u-ryukyu.ac.jp>
parents: 0
diff changeset
106 root = getRootNode tree
98e1a35e4ab0 Rewrite almost and Modularization
Daichi TOMA <toma@cr.ie.u-ryukyu.ac.jp>
parents: 0
diff changeset
107 target = getNode root path
0
329f462d5dad add nondestructive tree structure.
Daichi TOMA <toma@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
108
2
392c3f30c076 change to String from ByteString
Daichi TOMA <toma@cr.ie.u-ryukyu.ac.jp>
parents: 1
diff changeset
109 putAttribute' :: Node -> String -> String -> Node
1
98e1a35e4ab0 Rewrite almost and Modularization
Daichi TOMA <toma@cr.ie.u-ryukyu.ac.jp>
parents: 0
diff changeset
110 putAttribute' Empty key value = putAttribute' (Node (Children Map.empty) (Attributes Map.empty)) key value
98e1a35e4ab0 Rewrite almost and Modularization
Daichi TOMA <toma@cr.ie.u-ryukyu.ac.jp>
parents: 0
diff changeset
111 putAttribute' (Node children (Attributes map)) key value = Node children (Attributes (Map.insert key value map))
0
329f462d5dad add nondestructive tree structure.
Daichi TOMA <toma@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
112
1
98e1a35e4ab0 Rewrite almost and Modularization
Daichi TOMA <toma@cr.ie.u-ryukyu.ac.jp>
parents: 0
diff changeset
113 deleteAttribute' :: Node -> String -> Node
98e1a35e4ab0 Rewrite almost and Modularization
Daichi TOMA <toma@cr.ie.u-ryukyu.ac.jp>
parents: 0
diff changeset
114 deleteAttribute' Empty _ = Empty
98e1a35e4ab0 Rewrite almost and Modularization
Daichi TOMA <toma@cr.ie.u-ryukyu.ac.jp>
parents: 0
diff changeset
115 deleteAttribute' (Node children (Attributes map)) key = Node children (Attributes (Map.delete key map))
98e1a35e4ab0 Rewrite almost and Modularization
Daichi TOMA <toma@cr.ie.u-ryukyu.ac.jp>
parents: 0
diff changeset
116
98e1a35e4ab0 Rewrite almost and Modularization
Daichi TOMA <toma@cr.ie.u-ryukyu.ac.jp>
parents: 0
diff changeset
117 editTree :: Tree -> Path -> Node -> Tree
98e1a35e4ab0 Rewrite almost and Modularization
Daichi TOMA <toma@cr.ie.u-ryukyu.ac.jp>
parents: 0
diff changeset
118 editTree _ [] node = Tree node
98e1a35e4ab0 Rewrite almost and Modularization
Daichi TOMA <toma@cr.ie.u-ryukyu.ac.jp>
parents: 0
diff changeset
119 editTree tree path node = addNewChildAt tree (init path) (last path) node
98e1a35e4ab0 Rewrite almost and Modularization
Daichi TOMA <toma@cr.ie.u-ryukyu.ac.jp>
parents: 0
diff changeset
120