ä¸ããããæ¨ããï¼åâ親ã¸ã®å¯¾å¿ãä½ãï¼ã C# ã§
æµè¡ã£ã¦ããã£ã½ãã®ã§ãã£ã¦ã¿ã¾ããï¼
ä¸ããããæ¨ãããåâ親ã¸ã®å¯¾å¿ãä½ã
Shiro(2008/05/24 11:55:47 PDT): ãã¾ãã¾æ¨æ¥ãä»äºã§æ±ã£ãå°ãã¿ãåç´ç·¨ã¯ã¤ãºã«ãªããããªã®ã§æ¸ãçãã¦ããã
æ¨æ§é ãä¸ããããããã¨ãã°ãããªã®ï¼
(define *tree* '(Root (Spine (Neck (Head)) (RClavicle (RUpperArm (RLowerArm (RHand)))) (LClavicle (LUpperArm (LLowerArm (LHand))))) (RHip (RUpperLeg (RLowerLeg (RFoot)))) (LHip (LUpperLeg (LLowerLeg (LFoot))))))ã¤ã¾ãã <tree> := (<name> <tree> ...) ã¨ããæ§é ã
ãããããåâ親ã®å¯¾å¿ã表ãalistãä½ãæç¶ããæ¸ããã¨ãããã®ãçµæã®ä¾ã¯ãããªæããåè¦ç´ ã®é åºã¯åããªãã
((LHip . Root) (LUpperLeg . LHip) (LLowerLeg . LUpperLeg) (LFoot . LLowerLeg) (RHip . Root) (RUpperLeg . RHip) (RLowerLeg . RUpperLeg) (RFoot . RLowerLeg) (Spine . Root) (LClavicle . Spine) (LUpperArm . LClavicle) (LLowerArm . LUpperArm) (LHand . LLowerArm) (RClavicle . Spine) (RUpperArm . RClavicle) (RLowerArm . RUpperArm) (RHand . RLowerArm) (Neck . Spine) (Head . Neck))30åã§åç´ã10åã§ä¸ç´ã
ããã¯ç¢ºãã«é »åºåé¡ã£ã½ãæ°ããã¾ãï¼
é層æ§é ã®è¡¨ç¾ã¨ãã¦ï¼æ±ç¨ãªããªã¼ã®ä»ã«ï¼ãã¼ãã«æåãªã¹ãã¬ã¼ã¸åãã®ããã¤ãã®ããªã¼è¡¨ç¾ããã£ã¦ï¼ä»åã®åé¡ã¯æ±ç¨ããªã¼ãããé£æ¥ãªã¹ãã¢ãã«ãã¸ã®æ¸ãæãã¨ããæãã§ããããï¼
ãé£æ¥ãªã¹ãã¢ãã«ãã®ä»ã«ãï¼ãå
¥ãåéåã¢ãã«ãããçµè·¯åæã¢ãã«ããèãããã¨ãããã¾ãï¼ãã¾ãæ¬æ ¼çã«ä½¿ã£ããã¨ã¯ããã¾ãããï¼
- é£æ¥ãªã¹ãã¢ãã« (Adjacency List Model)
- 入れ子集合モデル (Nested Set Model)
- 経路列挙モデル (Path Emuneration Model)
ãã¦ï¼ãé£æ¥ãªã¹ãã¢ãã«ãã§å®ç¾©ãããããªã¼ã«å¯¾ãã¦å帰å¦çãè¡ã C# é¢æ°ç¾¤ã¯ãToLookup
é »åºå帰æ§é ã®ãã¿ã¼ã³å
C# ã®å ´åï¼é
延è©ä¾¡ã®æ©æµãæ«å°¾å帰ã®æé©åã®æ©æµãåºæ¬çã«åãããã¨ãã§ããªãããï¼é¢æ°ã®å帰ã¯ä½ãã¨ä¸èªç±ã§ãï¼å¿
ç¶çã«ï¼C# ã§ã®ã¢ããã¼ãã¯ï¼é »åºå帰æ§é ã¯ã¨ã«ããäºåã«é¢æ°åãã¦ããã¦ã®ã«ã¦ã³ã¿ã¼æ¦ç¥ã«åããã¨ã«ãªãã¾ãï¼(æ¬å½ã¯å帰形ãé¢æ°å½¢ã両æ¹èªç±ã«ä½¿ããæ¹ãããã®ã§ããããã©)
ãã¦ï¼f:a->[a] ã¨ããé¢æ° f (C# 風ã«æ¸ãã° Func<T, IEnumerable<T>> f;) ã«æ³¨ç®ãï¼ãããæ·±ãåªå
ã« unfold ãã¦ããæãã® CascadeDepthFirst ã¨ï¼å¹
åªå
ã« unfold ãã¦ããæãã® CascadeBreadthFirst ã¨ãããµãã¤ã®é¢æ°ãæä½ Achiral ã«ç¨æããã¦ãã¾ãï¼ã¤ã¾ãï¼f:a->[a] ã¨ããé¢æ°å½¢ã«å½ã¦ã¯ã¾ãé¢æ°ã§ããã°ï¼ä½ã§ãä¸çºã§å帰ããããã¨ãã§ããããã§ãï¼unfold èªèº«ï¼å¹
åªå
æ¢ç´¢ CascadeBreadthFirst ã®ç¹æ®ç³»ã¨ã¿ãªãã¾ãï¼
CascadeBreadthFirstï¼CascadeBreadthFirstï¼ãããã®é¢æ°ãæ«å°¾å帰ã®æé©åã«ç¸å½ãããã¨ãæåã§è¡ã£ã¦ããï¼ä¸è¦ã«ãªã£ããã¼ã¿ã¯ããã«æ¨ã¦ãããã«å®è£
ãã¦ããã¾ãï¼ã©ããã¦ãä¿æãã¦ããå¿
è¦ãããæ¢ç´¢éä¸ã®ãã³ãã©ãªãã¼ã¿ã«ã¤ãã¦ãï¼å
¨ã¦ãã¼ãã«ç¢ºä¿ãããããã«ãªã£ã¦ããã®ã§ï¼ãã¹ãã¬ãã«ãå¢å ãã¦ãï¼é¢æ°ã®å¼ã³åºãã¹ã¿ãã¯ãããµãããã¨ã¯ããã¾ããï¼ãã¡ãã yield ã«ããé
延è©ä¾¡ãè¡ã£ã¦ããã®ã§ï¼æ¢ç´¢ä¸æ¢æ¡ä»¶ãå¾æ®µã«åé¢ãããã¨ãå¯è½ã§ãï¼
ä»åã®åé¡ã§ããã°ï¼(tree, tree) ã¨ãã tuple ãã [(tree, tree)] ã¨ãã tuple ã®ãªã¹ããè¿ãé¢æ°ãèããã¨è¯ãããã§ãï¼ãã㧠tuple ã®ç¬¬ä¸è¦ç´ ã注ç®ãã¦ãããã¼ããã®ãã®ï¼ç¬¬äºè¦ç´ ã注ç®ãã¦ãããã¼ãã®è¦ªãã¼ãã¨ãã¾ãããï¼ã¤ã¾ã㯠(theNode, parent) ãåãåã£ã¦ï¼theNode ã®å
¨ã¦ã®åè¦ç´ ã«ã¤ãã¦å種㮠tuple ãè¿ãé¢æ°ãèããããã§ãï¼C# 3.0 ã®ã©ã ãå¼ã§æ¸ãã°ãããªæãã«ãªãã§ãããï¼
(Tuple<Tree<string>, Tree<string>> pair) => pair.Item1.Select(child => Make.Tuple(child, pair.Item1))
Achiral ã§ã¯ Func<T, IEnumerable<T>> ã¨ããããªã²ã¼ãã«ããããã Extension Methods ãå®ç¾©ãã¦ããã¾ãï¼
以ä¸ã«å
¨ã½ã¼ã¹ã示ãã¾ãï¼ãã«ãã«ã¯ Achiral 1.1.0.0 ãå¿
è¦ã§ãï¼
using System; using System.Linq; using System.Collections.Generic; using Achiral; using Achiral.Extension; using ST = Tree<string>; public class Tree<T> : List<Tree<T>> { public readonly T Value; public Tree(T value, params Tree<T>[] children) { this.Value = value; this.AddRange(children); } } static class Program { static void Main(string[] args) { var root = new ST("Root", new ST("Spine", new ST("Neck", new ST("Head")), new ST("RClavicle", new ST("RUpperArm", new ST("RLowerArm", new ST("RHand")))), new ST("LClavicle", new ST("LUpperArm", new ST("LLowerArm", new ST("LHand"))))), new ST("RHip", new ST("RUpperLeg", new ST("RLowerLeg", new ST("RFoot")))), new ST("LHip", new ST("LUpperLeg", new ST("LLowerLeg", new ST("LFoot")))) ); // Item1 := the node, Item2 := parent Make.Func((Tuple<ST, ST> pair) => pair.Item1.Select(child => Make.Tuple(child, pair.Item1))) .CascadeDepthFirst(Make.Tuple(root, default(ST))) .Where(pair => pair.Item2 != null) .ConsoleWriteLine(pair => string.Format("{0} . {1}", pair.Item1.Value, pair.Item2.Value)); } }
Spine . Root Neck . Spine Head . Neck RClavicle . Spine RUpperArm . RClavicle RLowerArm . RUpperArm RHand . RLowerArm LClavicle . Spine LUpperArm . LClavicle LLowerArm . LUpperArm LHand . LLowerArm RHip . Root RUpperLeg . RHip RLowerLeg . RUpperLeg RFoot . RLowerLeg LHip . Root LUpperLeg . LHip LLowerLeg . LUpperLeg LFoot . LLowerLeg