author  wenzelm 
Sat, 12 Nov 2011 17:01:58 +0100  
changeset 45469  25306d92f4ad 
parent 45468  33e946d3f449 
child 45470  81b85d4ed269 
permissions  rwrr 
38479
e628da370072
more efficient Markup_Tree, based on branches sorted by quasiorder;
wenzelm
parents:
38478
diff
changeset

1 
/* Title: Pure/PIDE/markup_tree.scala 
36676  2 
Author: Fabian Immler, TU Munich 
3 
Author: Makarius 

4 

38479
e628da370072
more efficient Markup_Tree, based on branches sorted by quasiorder;
wenzelm
parents:
38478
diff
changeset

5 
Markup trees over nested / nonoverlapping text ranges. 
36676  6 
*/ 
34393  7 

34871
e596a0b71f3c
incorporate "proofdocument" part into main Isabelle/Pure.jar  except for html_panel.scala, which depends on external library (Lobo/Cobra browser);
wenzelm
parents:
34760
diff
changeset

8 
package isabelle 
34393  9 

43520
cec9b95fa35d
explicit import java.lang.System to prevent odd scope problems;
wenzelm
parents:
39178
diff
changeset

10 
import java.lang.System 
34701
80b0add08eef
IsabelleSideKickParser: incorporate former MarkupNode.markup2default_node, observe stopped flag;
wenzelm
parents:
34676
diff
changeset

11 
import javax.swing.tree.DefaultMutableTreeNode 
80b0add08eef
IsabelleSideKickParser: incorporate former MarkupNode.markup2default_node, observe stopped flag;
wenzelm
parents:
34676
diff
changeset

12 

38479
e628da370072
more efficient Markup_Tree, based on branches sorted by quasiorder;
wenzelm
parents:
38478
diff
changeset

13 
import scala.collection.immutable.SortedMap 
e628da370072
more efficient Markup_Tree, based on branches sorted by quasiorder;
wenzelm
parents:
38478
diff
changeset

14 

e628da370072
more efficient Markup_Tree, based on branches sorted by quasiorder;
wenzelm
parents:
38478
diff
changeset

15 

e628da370072
more efficient Markup_Tree, based on branches sorted by quasiorder;
wenzelm
parents:
38478
diff
changeset

16 
object Markup_Tree 
e628da370072
more efficient Markup_Tree, based on branches sorted by quasiorder;
wenzelm
parents:
38478
diff
changeset

17 
{ 
45467  18 
sealed case class Cumulate[A](info: A, result: PartialFunction[(A, Text.Markup), A]) 
45468  19 
sealed case class Select[A](result: PartialFunction[Text.Markup, A]) 
45456  20 

21 
val empty: Markup_Tree = new Markup_Tree(Branches.empty) 

38479
e628da370072
more efficient Markup_Tree, based on branches sorted by quasiorder;
wenzelm
parents:
38478
diff
changeset

22 

45469
25306d92f4ad
refined Markup_Tree implementation: stacked markup within each entry;
wenzelm
parents:
45468
diff
changeset

23 
sealed case class Entry(range: Text.Range, rev_markup: List[XML.Tree], subtree: Markup_Tree) 
25306d92f4ad
refined Markup_Tree implementation: stacked markup within each entry;
wenzelm
parents:
45468
diff
changeset

24 
{ 
25306d92f4ad
refined Markup_Tree implementation: stacked markup within each entry;
wenzelm
parents:
45468
diff
changeset

25 
def + (m: XML.Tree): Entry = copy(rev_markup = m :: rev_markup) 
25306d92f4ad
refined Markup_Tree implementation: stacked markup within each entry;
wenzelm
parents:
45468
diff
changeset

26 
def markup: List[XML.Tree] = rev_markup.reverse 
25306d92f4ad
refined Markup_Tree implementation: stacked markup within each entry;
wenzelm
parents:
45468
diff
changeset

27 
} 
25306d92f4ad
refined Markup_Tree implementation: stacked markup within each entry;
wenzelm
parents:
45468
diff
changeset

28 

38479
e628da370072
more efficient Markup_Tree, based on branches sorted by quasiorder;
wenzelm
parents:
38478
diff
changeset

29 
object Branches 
e628da370072
more efficient Markup_Tree, based on branches sorted by quasiorder;
wenzelm
parents:
38478
diff
changeset

30 
{ 
38578  31 
type T = SortedMap[Text.Range, Entry] 
45456  32 
val empty: T = SortedMap.empty(Text.Range.Ordering) 
38479
e628da370072
more efficient Markup_Tree, based on branches sorted by quasiorder;
wenzelm
parents:
38478
diff
changeset

33 
} 
e628da370072
more efficient Markup_Tree, based on branches sorted by quasiorder;
wenzelm
parents:
38478
diff
changeset

34 
} 
34554
7dc6c231da40
abs. stops, markup nodes depend on docversion;
immler@in.tum.de
parents:
34517
diff
changeset

35 

34393  36 

45456  37 
class Markup_Tree private(branches: Markup_Tree.Branches.T) 
34717
3f32e08bbb6c
sidekick root data: set buffer length to avoid crash of initial caret move;
wenzelm
parents:
34708
diff
changeset

38 
{ 
45469
25306d92f4ad
refined Markup_Tree implementation: stacked markup within each entry;
wenzelm
parents:
45468
diff
changeset

39 
private def this(branches: Markup_Tree.Branches.T, entry: Markup_Tree.Entry) = 
25306d92f4ad
refined Markup_Tree implementation: stacked markup within each entry;
wenzelm
parents:
45468
diff
changeset

40 
this(branches + (entry.range > entry)) 
25306d92f4ad
refined Markup_Tree implementation: stacked markup within each entry;
wenzelm
parents:
45468
diff
changeset

41 

25306d92f4ad
refined Markup_Tree implementation: stacked markup within each entry;
wenzelm
parents:
45468
diff
changeset

42 

38479
e628da370072
more efficient Markup_Tree, based on branches sorted by quasiorder;
wenzelm
parents:
38478
diff
changeset

43 
import Markup_Tree._ 
34557  44 

38563  45 
override def toString = 
46 
branches.toList.map(_._2) match { 

47 
case Nil => "Empty" 

48 
case list => list.mkString("Tree(", ",", ")") 

49 
} 

50 

45469
25306d92f4ad
refined Markup_Tree implementation: stacked markup within each entry;
wenzelm
parents:
45468
diff
changeset

51 
private def overlapping(range: Text.Range): Branches.T = 
45457  52 
{ 
53 
val start = Text.Range(range.start) 

54 
val stop = Text.Range(range.stop) 

55 
val bs = branches.range(start, stop) 

56 
// FIXME check after Scala 2.8.x 

57 
branches.get(stop) match { 

45469
25306d92f4ad
refined Markup_Tree implementation: stacked markup within each entry;
wenzelm
parents:
45468
diff
changeset

58 
case Some(end) if range overlaps end.range => bs + (end.range > end) 
45457  59 
case _ => bs 
60 
} 

61 
} 

62 

45469
25306d92f4ad
refined Markup_Tree implementation: stacked markup within each entry;
wenzelm
parents:
45468
diff
changeset

63 
def + (new_markup: Text.Markup): Markup_Tree = 
34703  64 
{ 
45469
25306d92f4ad
refined Markup_Tree implementation: stacked markup within each entry;
wenzelm
parents:
45468
diff
changeset

65 
val new_range = new_markup.range 
25306d92f4ad
refined Markup_Tree implementation: stacked markup within each entry;
wenzelm
parents:
45468
diff
changeset

66 
val new_info = new_markup.info 
25306d92f4ad
refined Markup_Tree implementation: stacked markup within each entry;
wenzelm
parents:
45468
diff
changeset

67 

38578  68 
branches.get(new_range) match { 
38482
7b6ee937b75f
tuned Markup_Tree, using SortedMap more carefully;
wenzelm
parents:
38479
diff
changeset

69 
case None => 
45469
25306d92f4ad
refined Markup_Tree implementation: stacked markup within each entry;
wenzelm
parents:
45468
diff
changeset

70 
new Markup_Tree(branches, Entry(new_range, List(new_info), empty)) 
25306d92f4ad
refined Markup_Tree implementation: stacked markup within each entry;
wenzelm
parents:
45468
diff
changeset

71 
case Some(entry) => 
25306d92f4ad
refined Markup_Tree implementation: stacked markup within each entry;
wenzelm
parents:
45468
diff
changeset

72 
if (entry.range == new_range) 
25306d92f4ad
refined Markup_Tree implementation: stacked markup within each entry;
wenzelm
parents:
45468
diff
changeset

73 
new Markup_Tree(branches, entry + new_info) 
25306d92f4ad
refined Markup_Tree implementation: stacked markup within each entry;
wenzelm
parents:
45468
diff
changeset

74 
else if (entry.range.contains(new_range)) 
25306d92f4ad
refined Markup_Tree implementation: stacked markup within each entry;
wenzelm
parents:
45468
diff
changeset

75 
new Markup_Tree(branches, entry.copy(subtree = entry.subtree + new_markup)) 
38578  76 
else if (new_range.contains(branches.head._1) && new_range.contains(branches.last._1)) 
45469
25306d92f4ad
refined Markup_Tree implementation: stacked markup within each entry;
wenzelm
parents:
45468
diff
changeset

77 
new Markup_Tree(Branches.empty, Entry(new_range, List(new_info), this)) 
38482
7b6ee937b75f
tuned Markup_Tree, using SortedMap more carefully;
wenzelm
parents:
38479
diff
changeset

78 
else { 
45457  79 
val body = overlapping(new_range) 
38578  80 
if (body.forall(e => new_range.contains(e._1))) { 
45469
25306d92f4ad
refined Markup_Tree implementation: stacked markup within each entry;
wenzelm
parents:
45468
diff
changeset

81 
val rest = // branches  body, modulo workarounds for Redblack in Scala 2.8.0 FIXME 
38657  82 
if (body.size > 1) 
83 
(Branches.empty /: branches)((rest, entry) => 

84 
if (body.isDefinedAt(entry._1)) rest else rest + entry) 

85 
else branches 

45469
25306d92f4ad
refined Markup_Tree implementation: stacked markup within each entry;
wenzelm
parents:
45468
diff
changeset

86 
new Markup_Tree(rest, Entry(new_range, List(new_info), new Markup_Tree(body))) 
38482
7b6ee937b75f
tuned Markup_Tree, using SortedMap more carefully;
wenzelm
parents:
38479
diff
changeset

87 
} 
7b6ee937b75f
tuned Markup_Tree, using SortedMap more carefully;
wenzelm
parents:
38479
diff
changeset

88 
else { // FIXME split markup!? 
45469
25306d92f4ad
refined Markup_Tree implementation: stacked markup within each entry;
wenzelm
parents:
45468
diff
changeset

89 
System.err.println("Ignored overlapping markup information: " + new_markup) 
38482
7b6ee937b75f
tuned Markup_Tree, using SortedMap more carefully;
wenzelm
parents:
38479
diff
changeset

90 
this 
7b6ee937b75f
tuned Markup_Tree, using SortedMap more carefully;
wenzelm
parents:
38479
diff
changeset

91 
} 
7b6ee937b75f
tuned Markup_Tree, using SortedMap more carefully;
wenzelm
parents:
38479
diff
changeset

92 
} 
34703  93 
} 
94 
} 

95 

45467  96 
def cumulate[A](root_range: Text.Range)(body: Cumulate[A]): Stream[Text.Info[A]] = 
45459  97 
{ 
45467  98 
val root_info = body.info 
45469
25306d92f4ad
refined Markup_Tree implementation: stacked markup within each entry;
wenzelm
parents:
45468
diff
changeset

99 

25306d92f4ad
refined Markup_Tree implementation: stacked markup within each entry;
wenzelm
parents:
45468
diff
changeset

100 
def result(x: A, entry: Entry): Option[A] = 
25306d92f4ad
refined Markup_Tree implementation: stacked markup within each entry;
wenzelm
parents:
45468
diff
changeset

101 
{ 
25306d92f4ad
refined Markup_Tree implementation: stacked markup within each entry;
wenzelm
parents:
45468
diff
changeset

102 
val (y, changed) = 
25306d92f4ad
refined Markup_Tree implementation: stacked markup within each entry;
wenzelm
parents:
45468
diff
changeset

103 
(entry.markup :\ (x, false))((info, res) => 
25306d92f4ad
refined Markup_Tree implementation: stacked markup within each entry;
wenzelm
parents:
45468
diff
changeset

104 
{ 
25306d92f4ad
refined Markup_Tree implementation: stacked markup within each entry;
wenzelm
parents:
45468
diff
changeset

105 
val (y, changed) = res 
25306d92f4ad
refined Markup_Tree implementation: stacked markup within each entry;
wenzelm
parents:
45468
diff
changeset

106 
val arg = (x, Text.Info(entry.range, info)) 
25306d92f4ad
refined Markup_Tree implementation: stacked markup within each entry;
wenzelm
parents:
45468
diff
changeset

107 
if (body.result.isDefinedAt(arg)) (body.result(arg), true) 
25306d92f4ad
refined Markup_Tree implementation: stacked markup within each entry;
wenzelm
parents:
45468
diff
changeset

108 
else res 
25306d92f4ad
refined Markup_Tree implementation: stacked markup within each entry;
wenzelm
parents:
45468
diff
changeset

109 
}) 
25306d92f4ad
refined Markup_Tree implementation: stacked markup within each entry;
wenzelm
parents:
45468
diff
changeset

110 
if (changed) Some(y) else None 
25306d92f4ad
refined Markup_Tree implementation: stacked markup within each entry;
wenzelm
parents:
45468
diff
changeset

111 
} 
45467  112 

45459  113 
def stream( 
114 
last: Text.Offset, 

45469
25306d92f4ad
refined Markup_Tree implementation: stacked markup within each entry;
wenzelm
parents:
45468
diff
changeset

115 
stack: List[(Text.Info[A], Stream[(Text.Range, Entry)])]): Stream[Text.Info[A]] = 
45459  116 
{ 
117 
stack match { 

45469
25306d92f4ad
refined Markup_Tree implementation: stacked markup within each entry;
wenzelm
parents:
45468
diff
changeset

118 
case (parent, (range, entry) #:: more) :: rest => 
45467  119 
val subrange = range.restrict(root_range) 
45469
25306d92f4ad
refined Markup_Tree implementation: stacked markup within each entry;
wenzelm
parents:
45468
diff
changeset

120 
val subtree = entry.subtree.overlapping(subrange).toStream 
45459  121 
val start = subrange.start 
122 

45469
25306d92f4ad
refined Markup_Tree implementation: stacked markup within each entry;
wenzelm
parents:
45468
diff
changeset

123 
result(parent.info, entry) match { 
25306d92f4ad
refined Markup_Tree implementation: stacked markup within each entry;
wenzelm
parents:
45468
diff
changeset

124 
case Some(res) => 
25306d92f4ad
refined Markup_Tree implementation: stacked markup within each entry;
wenzelm
parents:
45468
diff
changeset

125 
val next = Text.Info(subrange, res) 
25306d92f4ad
refined Markup_Tree implementation: stacked markup within each entry;
wenzelm
parents:
45468
diff
changeset

126 
val nexts = stream(start, (next, subtree) :: (parent, more) :: rest) 
25306d92f4ad
refined Markup_Tree implementation: stacked markup within each entry;
wenzelm
parents:
45468
diff
changeset

127 
if (last < start) parent.restrict(Text.Range(last, start)) #:: nexts 
25306d92f4ad
refined Markup_Tree implementation: stacked markup within each entry;
wenzelm
parents:
45468
diff
changeset

128 
else nexts 
25306d92f4ad
refined Markup_Tree implementation: stacked markup within each entry;
wenzelm
parents:
45468
diff
changeset

129 
case None => stream(last, (parent, subtree #::: more) :: rest) 
45459  130 
} 
131 

132 
case (parent, Stream.Empty) :: rest => 

133 
val stop = parent.range.stop 

134 
val nexts = stream(stop, rest) 

135 
if (last < stop) parent.restrict(Text.Range(last, stop)) #:: nexts 

136 
else nexts 

137 

138 
case Nil => 

45467  139 
val stop = root_range.stop 
140 
if (last < stop) Stream(Text.Info(Text.Range(last, stop), root_info)) 

45459  141 
else Stream.empty 
142 
} 

143 
} 

45467  144 
stream(root_range.start, 
145 
List((Text.Info(root_range, root_info), overlapping(root_range).toStream))) 

45459  146 
} 
147 

38577  148 
def swing_tree(parent: DefaultMutableTreeNode) 
45455  149 
(swing_node: Text.Markup => DefaultMutableTreeNode) 
38479
e628da370072
more efficient Markup_Tree, based on branches sorted by quasiorder;
wenzelm
parents:
38478
diff
changeset

150 
{ 
45469
25306d92f4ad
refined Markup_Tree implementation: stacked markup within each entry;
wenzelm
parents:
45468
diff
changeset

151 
for ((_, entry) < branches) { 
25306d92f4ad
refined Markup_Tree implementation: stacked markup within each entry;
wenzelm
parents:
45468
diff
changeset

152 
var current = parent 
25306d92f4ad
refined Markup_Tree implementation: stacked markup within each entry;
wenzelm
parents:
45468
diff
changeset

153 
for (info < entry.markup) { 
25306d92f4ad
refined Markup_Tree implementation: stacked markup within each entry;
wenzelm
parents:
45468
diff
changeset

154 
val node = swing_node(Text.Info(entry.range, info)) 
25306d92f4ad
refined Markup_Tree implementation: stacked markup within each entry;
wenzelm
parents:
45468
diff
changeset

155 
current.add(node) 
25306d92f4ad
refined Markup_Tree implementation: stacked markup within each entry;
wenzelm
parents:
45468
diff
changeset

156 
current = node 
25306d92f4ad
refined Markup_Tree implementation: stacked markup within each entry;
wenzelm
parents:
45468
diff
changeset

157 
} 
25306d92f4ad
refined Markup_Tree implementation: stacked markup within each entry;
wenzelm
parents:
45468
diff
changeset

158 
entry.subtree.swing_tree(current)(swing_node) 
34514
2104a836b415
renamed fields of MarkupNode; implemented flatten and leafs
immler@in.tum.de
parents:
34503
diff
changeset

159 
} 
2104a836b415
renamed fields of MarkupNode; implemented flatten and leafs
immler@in.tum.de
parents:
34503
diff
changeset

160 
} 
34717
3f32e08bbb6c
sidekick root data: set buffer length to avoid crash of initial caret move;
wenzelm
parents:
34708
diff
changeset

161 
} 
34514
2104a836b415
renamed fields of MarkupNode; implemented flatten and leafs
immler@in.tum.de
parents:
34503
diff
changeset

162 