PageRenderTime 32ms CodeModel.GetById 18ms app.highlight 9ms RepoModel.GetById 1ms app.codeStats 1ms

/razpub/src/com/razie/pubstage/data/TreeStruc.scala

http://razpub.googlecode.com/
Scala | 77 lines | 33 code | 17 blank | 27 comment | 0 complexity | 84d761356e23cd77c7998ef8300a1e7f MD5 | raw file
 1package com.razie.pubstage.data;
 2
 3/**
 4 * a tree structure, with nodes and leafs etc
 5 * 
 6 * @author razvanc
 7 * @param [T]
 8 */
 9trait TreeStruc[T] extends Structure[T] {
10   def children : List[TreeStruc[T]]
11   def isLeaf : Boolean
12}
13
14class TreeImplNode[T] (c:T) extends StrucImpl[T] (c) with TreeStruc[T] {
15   val kids = razie.Listi[TreeStruc[T]]()
16
17   override def children : List[TreeStruc[T]] = kids.toList
18   override def isLeaf : Boolean = false
19
20   def addLeaf (t:T) = this.kids += new TreeImplLeaf[T](t)
21
22   def addNode (t:T) : TreeStruc[T] = {
23     val newT = new TreeImplNode[T](t);
24     this.kids += newT
25     newT
26     }
27}
28    
29class TreeImplLeaf[T] (c:T) extends StrucImpl[T](c) with TreeStruc[T] {
30   override def children : List[TreeStruc[T]] = scala.collection.immutable.List[TreeStruc[T]]()
31   override def isLeaf : Boolean = true
32}
33
34
35class TreeBuilder[T] (t:T)(f: =>List[TreeStruc[T]]) extends TreeImplNode[T] (t) {
36   override val children : List[TreeStruc[T]] = f
37}
38
39case class INode (t:Int)(f: =>List[TreeStruc[Int]]) extends TreeBuilder[Int] (t)(f) {}
40case class ILeaf (t:Int) extends TreeImplLeaf[Int] (t) {}
41
42
43object TestTree1 {
44
45   val tree =  INode (0) { 
46      ILeaf(1) :: ILeaf(2) :: Nil
47   }
48}
49
50
51/** the f should create also LazyTreeBuiled nodes or leafs */
52class LazyTreeBuilder[T] (t:T)(f: TreeStruc[T] => List[TreeStruc[T]]) extends TreeImplNode[T] (t) {
53   override lazy val children : List[TreeStruc[T]] = f (this)
54}
55
56
57// morph test
58
59//class TreeMorph[T] (t:T)(f: =>List[TreeMorph[T]] = Nil) extends TreeImplNode[T] (t) {
60//}
61//
62//class TreeMorphAny extends TreeImplNode[Any] {
63//   
64//}
65//
66//case class IT (t:Int)(f: =>List[TreeMorph[Int]] = Nil) extends TreeMorph[Int] (i) (f) {
67//}
68//
69//
70//object TestTree2 {
71//
72//   val tree =  IT (0) { 
73////      IT(1) :: IT(2)
74//   }
75//   
76//   
77//}