diff Jungle.hs @ 8:f03876c8236a

add ParRead
author Daichi TOMA <toma@cr.ie.u-ryukyu.ac.jp>
date Tue, 14 Jan 2014 18:09:51 +0900
parents 644e1345ee83
children 947c5cfa4149
line wrap: on
line diff
--- a/Jungle.hs	Mon Jan 13 11:43:41 2014 +0900
+++ b/Jungle.hs	Tue Jan 14 18:09:51 2014 +0900
@@ -13,8 +13,9 @@
 , putAttribute
 , deleteAttribute
 , getAttributes
-, drawNode -- デバッグ用
+, drawNode
 , printAttributes
+, size
 ) where
 
 import qualified Data.Map as M
@@ -164,6 +165,7 @@
     map = getAttributesMap $ attributes target
 
 -- デバッグ用表示関数
+
 -- 現在の木構造を整形して表示
 drawNode :: Node -> String
 drawNode node = unlines $ draw "root" node
@@ -186,9 +188,9 @@
 
 printAttr :: String -> Node -> [String]
 printAttr string node =
-    if M.null attr_map
-      then printSubTrees keys
-      else ("Node: " ++ string) : ("  " ++ attr) : printSubTrees keys
+    if not $ M.null attr_map
+      then ("Node: " ++ string) : ("  " ++ attr) : printSubTrees keys
+      else printSubTrees keys
   where
     attr_map  = getAttributesMap $ attributes node
     show_attr [] = []
@@ -200,3 +202,13 @@
     printSubTrees []     = []
     printSubTrees (x:xs) = printAttr (string ++ "-" ++ (show x)) (fromJust $ M.lookup x map) ++ printSubTrees xs
 
+-- ルートノードの下にいくつの子があるか数える
+size :: Node -> Int
+size node = M.size map + subTreesSize keys
+  where
+    map = getChildrenMap $ children node
+    keys = M.keys map
+    subTreesSize [] = 0
+    subTreesSize (x:xs) = size (getNode x) + subTreesSize xs
+    getNode x = fromJust $ M.lookup x map
+