author | wenzelm |
Thu, 27 Sep 2012 14:46:34 +0200 | |
changeset 49607 | b610c0d52bfa |
parent 49469 | 00c301c8d569 |
child 49608 | ce1c34c98eeb |
permissions | -rw-r--r-- |
38479
e628da370072
more efficient Markup_Tree, based on branches sorted by quasi-order;
wenzelm
parents:
38478
diff
changeset
|
1 |
/* Title: Pure/PIDE/markup_tree.scala |
45673
cd41e3903fbf
separate compilation of PIDE vs. Pure sources, which enables independent Scala library;
wenzelm
parents:
45667
diff
changeset
|
2 |
Module: PIDE |
36676 | 3 |
Author: Fabian Immler, TU Munich |
4 |
Author: Makarius |
|
5 |
||
38479
e628da370072
more efficient Markup_Tree, based on branches sorted by quasi-order;
wenzelm
parents:
38478
diff
changeset
|
6 |
Markup trees over nested / non-overlapping text ranges. |
36676 | 7 |
*/ |
34393 | 8 |
|
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
|
9 |
package isabelle |
34393 | 10 |
|
43520
cec9b95fa35d
explicit import java.lang.System to prevent odd scope problems;
wenzelm
parents:
39178
diff
changeset
|
11 |
import java.lang.System |
34701
80b0add08eef
IsabelleSideKickParser: incorporate former MarkupNode.markup2default_node, observe stopped flag;
wenzelm
parents:
34676
diff
changeset
|
12 |
import javax.swing.tree.DefaultMutableTreeNode |
80b0add08eef
IsabelleSideKickParser: incorporate former MarkupNode.markup2default_node, observe stopped flag;
wenzelm
parents:
34676
diff
changeset
|
13 |
|
38479
e628da370072
more efficient Markup_Tree, based on branches sorted by quasi-order;
wenzelm
parents:
38478
diff
changeset
|
14 |
import scala.collection.immutable.SortedMap |
49467 | 15 |
import scala.annotation.tailrec |
38479
e628da370072
more efficient Markup_Tree, based on branches sorted by quasi-order;
wenzelm
parents:
38478
diff
changeset
|
16 |
|
e628da370072
more efficient Markup_Tree, based on branches sorted by quasi-order;
wenzelm
parents:
38478
diff
changeset
|
17 |
|
e628da370072
more efficient Markup_Tree, based on branches sorted by quasi-order;
wenzelm
parents:
38478
diff
changeset
|
18 |
object Markup_Tree |
e628da370072
more efficient Markup_Tree, based on branches sorted by quasi-order;
wenzelm
parents:
38478
diff
changeset
|
19 |
{ |
45456 | 20 |
val empty: Markup_Tree = new Markup_Tree(Branches.empty) |
38479
e628da370072
more efficient Markup_Tree, based on branches sorted by quasi-order;
wenzelm
parents:
38478
diff
changeset
|
21 |
|
49467 | 22 |
def merge_disjoint(trees: List[Markup_Tree]): Markup_Tree = |
23 |
trees match { |
|
24 |
case Nil => empty |
|
25 |
case head :: tail => |
|
26 |
new Markup_Tree( |
|
27 |
(head.branches /: tail) { |
|
28 |
case (branches, tree) => |
|
29 |
(branches /: tree.branches) { |
|
30 |
case (bs, (r, entry)) => |
|
31 |
require(!bs.isDefinedAt(r)) |
|
32 |
bs + (r -> entry) |
|
33 |
} |
|
34 |
}) |
|
35 |
} |
|
36 |
||
45473 | 37 |
object Entry |
38 |
{ |
|
39 |
def apply(markup: Text.Markup, subtree: Markup_Tree): Entry = |
|
45474
f793dd5d84b2
index markup elements for more efficient cumulate/select operations;
wenzelm
parents:
45473
diff
changeset
|
40 |
Entry(markup.range, List(markup.info), Set(markup.info.markup.name), subtree) |
49469 | 41 |
|
42 |
def apply(range: Text.Range, rev_markups: List[XML.Elem], subtree: Markup_Tree): Entry = |
|
43 |
Entry(range, rev_markups, Set.empty ++ rev_markups.iterator.map(_.markup.name), subtree) |
|
45473 | 44 |
} |
45 |
||
45474
f793dd5d84b2
index markup elements for more efficient cumulate/select operations;
wenzelm
parents:
45473
diff
changeset
|
46 |
sealed case class Entry( |
f793dd5d84b2
index markup elements for more efficient cumulate/select operations;
wenzelm
parents:
45473
diff
changeset
|
47 |
range: Text.Range, |
f793dd5d84b2
index markup elements for more efficient cumulate/select operations;
wenzelm
parents:
45473
diff
changeset
|
48 |
rev_markup: List[XML.Elem], |
f793dd5d84b2
index markup elements for more efficient cumulate/select operations;
wenzelm
parents:
45473
diff
changeset
|
49 |
elements: Set[String], |
f793dd5d84b2
index markup elements for more efficient cumulate/select operations;
wenzelm
parents:
45473
diff
changeset
|
50 |
subtree: Markup_Tree) |
45469
25306d92f4ad
refined Markup_Tree implementation: stacked markup within each entry;
wenzelm
parents:
45468
diff
changeset
|
51 |
{ |
45474
f793dd5d84b2
index markup elements for more efficient cumulate/select operations;
wenzelm
parents:
45473
diff
changeset
|
52 |
def + (info: XML.Elem): Entry = |
f793dd5d84b2
index markup elements for more efficient cumulate/select operations;
wenzelm
parents:
45473
diff
changeset
|
53 |
if (elements(info.markup.name)) copy(rev_markup = info :: rev_markup) |
f793dd5d84b2
index markup elements for more efficient cumulate/select operations;
wenzelm
parents:
45473
diff
changeset
|
54 |
else copy(rev_markup = info :: rev_markup, elements = elements + info.markup.name) |
f793dd5d84b2
index markup elements for more efficient cumulate/select operations;
wenzelm
parents:
45473
diff
changeset
|
55 |
|
45470 | 56 |
def markup: List[XML.Elem] = rev_markup.reverse |
45469
25306d92f4ad
refined Markup_Tree implementation: stacked markup within each entry;
wenzelm
parents:
45468
diff
changeset
|
57 |
} |
25306d92f4ad
refined Markup_Tree implementation: stacked markup within each entry;
wenzelm
parents:
45468
diff
changeset
|
58 |
|
38479
e628da370072
more efficient Markup_Tree, based on branches sorted by quasi-order;
wenzelm
parents:
38478
diff
changeset
|
59 |
object Branches |
e628da370072
more efficient Markup_Tree, based on branches sorted by quasi-order;
wenzelm
parents:
38478
diff
changeset
|
60 |
{ |
38578 | 61 |
type T = SortedMap[Text.Range, Entry] |
45456 | 62 |
val empty: T = SortedMap.empty(Text.Range.Ordering) |
38479
e628da370072
more efficient Markup_Tree, based on branches sorted by quasi-order;
wenzelm
parents:
38478
diff
changeset
|
63 |
} |
49466 | 64 |
|
65 |
||
66 |
/* XML representation */ |
|
67 |
||
49467 | 68 |
@tailrec private def strip_elems(markups: List[Markup], body: XML.Body): (List[Markup], XML.Body) = |
69 |
body match { |
|
70 |
case List(XML.Elem(markup1, body1)) => strip_elems(markup1 :: markups, body1) |
|
71 |
case _ => (markups, body) |
|
72 |
} |
|
73 |
||
74 |
private def make_trees(acc: (Int, List[Markup_Tree]), tree: XML.Tree): (Int, List[Markup_Tree]) = |
|
75 |
{ |
|
76 |
val (offset, markup_trees) = acc |
|
77 |
||
78 |
strip_elems(Nil, List(tree)) match { |
|
79 |
case (Nil, body) => |
|
80 |
(offset + XML.text_length(body), markup_trees) |
|
49466 | 81 |
|
49469 | 82 |
case (elems, body) => |
83 |
val (end_offset, subtrees) = ((offset, Nil: List[Markup_Tree]) /: body)(make_trees) |
|
49467 | 84 |
val range = Text.Range(offset, end_offset) |
49469 | 85 |
val entry = Entry(range, elems.map(XML.Elem(_, Nil)), merge_disjoint(subtrees)) |
86 |
(end_offset, new Markup_Tree(Branches.empty, entry) :: markup_trees) |
|
49466 | 87 |
} |
49467 | 88 |
} |
49466 | 89 |
|
49467 | 90 |
def from_XML(body: XML.Body): Markup_Tree = |
91 |
merge_disjoint(((0, Nil: List[Markup_Tree]) /: body)(make_trees)._2) |
|
38479
e628da370072
more efficient Markup_Tree, based on branches sorted by quasi-order;
wenzelm
parents:
38478
diff
changeset
|
92 |
} |
34554
7dc6c231da40
abs. stops, markup nodes depend on doc-version;
immler@in.tum.de
parents:
34517
diff
changeset
|
93 |
|
34393 | 94 |
|
49467 | 95 |
final class Markup_Tree private(private val 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
|
96 |
{ |
49417 | 97 |
import Markup_Tree._ |
98 |
||
45469
25306d92f4ad
refined Markup_Tree implementation: stacked markup within each entry;
wenzelm
parents:
45468
diff
changeset
|
99 |
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
|
100 |
this(branches + (entry.range -> entry)) |
25306d92f4ad
refined Markup_Tree implementation: stacked markup within each entry;
wenzelm
parents:
45468
diff
changeset
|
101 |
|
38563 | 102 |
override def toString = |
103 |
branches.toList.map(_._2) match { |
|
104 |
case Nil => "Empty" |
|
105 |
case list => list.mkString("Tree(", ",", ")") |
|
106 |
} |
|
107 |
||
45469
25306d92f4ad
refined Markup_Tree implementation: stacked markup within each entry;
wenzelm
parents:
45468
diff
changeset
|
108 |
private def overlapping(range: Text.Range): Branches.T = |
45457 | 109 |
{ |
110 |
val start = Text.Range(range.start) |
|
111 |
val stop = Text.Range(range.stop) |
|
112 |
val bs = branches.range(start, stop) |
|
113 |
branches.get(stop) match { |
|
45469
25306d92f4ad
refined Markup_Tree implementation: stacked markup within each entry;
wenzelm
parents:
45468
diff
changeset
|
114 |
case Some(end) if range overlaps end.range => bs + (end.range -> end) |
45457 | 115 |
case _ => bs |
116 |
} |
|
117 |
} |
|
118 |
||
45469
25306d92f4ad
refined Markup_Tree implementation: stacked markup within each entry;
wenzelm
parents:
45468
diff
changeset
|
119 |
def + (new_markup: Text.Markup): Markup_Tree = |
34703 | 120 |
{ |
45469
25306d92f4ad
refined Markup_Tree implementation: stacked markup within each entry;
wenzelm
parents:
45468
diff
changeset
|
121 |
val new_range = new_markup.range |
25306d92f4ad
refined Markup_Tree implementation: stacked markup within each entry;
wenzelm
parents:
45468
diff
changeset
|
122 |
|
38578 | 123 |
branches.get(new_range) match { |
45473 | 124 |
case None => new Markup_Tree(branches, Entry(new_markup, empty)) |
45469
25306d92f4ad
refined Markup_Tree implementation: stacked markup within each entry;
wenzelm
parents:
45468
diff
changeset
|
125 |
case Some(entry) => |
25306d92f4ad
refined Markup_Tree implementation: stacked markup within each entry;
wenzelm
parents:
45468
diff
changeset
|
126 |
if (entry.range == new_range) |
45473 | 127 |
new Markup_Tree(branches, entry + new_markup.info) |
45469
25306d92f4ad
refined Markup_Tree implementation: stacked markup within each entry;
wenzelm
parents:
45468
diff
changeset
|
128 |
else if (entry.range.contains(new_range)) |
25306d92f4ad
refined Markup_Tree implementation: stacked markup within each entry;
wenzelm
parents:
45468
diff
changeset
|
129 |
new Markup_Tree(branches, entry.copy(subtree = entry.subtree + new_markup)) |
38578 | 130 |
else if (new_range.contains(branches.head._1) && new_range.contains(branches.last._1)) |
45473 | 131 |
new Markup_Tree(Branches.empty, Entry(new_markup, this)) |
38482
7b6ee937b75f
tuned Markup_Tree, using SortedMap more carefully;
wenzelm
parents:
38479
diff
changeset
|
132 |
else { |
45457 | 133 |
val body = overlapping(new_range) |
49607
b610c0d52bfa
updated to consolidated SortedMap in scala-2.9.x;
wenzelm
parents:
49469
diff
changeset
|
134 |
if (body.forall(e => new_range.contains(e._1))) |
b610c0d52bfa
updated to consolidated SortedMap in scala-2.9.x;
wenzelm
parents:
49469
diff
changeset
|
135 |
new Markup_Tree(branches -- body.keys, Entry(new_markup, new Markup_Tree(body))) |
38482
7b6ee937b75f
tuned Markup_Tree, using SortedMap more carefully;
wenzelm
parents:
38479
diff
changeset
|
136 |
else { // FIXME split markup!? |
48762 | 137 |
System.err.println("Ignored overlapping markup information: " + new_markup + |
138 |
body.filter(e => !new_range.contains(e._1)).mkString("\n")) |
|
38482
7b6ee937b75f
tuned Markup_Tree, using SortedMap more carefully;
wenzelm
parents:
38479
diff
changeset
|
139 |
this |
7b6ee937b75f
tuned Markup_Tree, using SortedMap more carefully;
wenzelm
parents:
38479
diff
changeset
|
140 |
} |
7b6ee937b75f
tuned Markup_Tree, using SortedMap more carefully;
wenzelm
parents:
38479
diff
changeset
|
141 |
} |
34703 | 142 |
} |
143 |
} |
|
144 |
||
46178
1c5c88f6feb5
clarified Isabelle_Rendering vs. physical painting;
wenzelm
parents:
46165
diff
changeset
|
145 |
def cumulate[A](root_range: Text.Range, root_info: A, result_elements: Option[Set[String]], |
1c5c88f6feb5
clarified Isabelle_Rendering vs. physical painting;
wenzelm
parents:
46165
diff
changeset
|
146 |
result: PartialFunction[(A, Text.Markup), A]): Stream[Text.Info[A]] = |
45459 | 147 |
{ |
46178
1c5c88f6feb5
clarified Isabelle_Rendering vs. physical painting;
wenzelm
parents:
46165
diff
changeset
|
148 |
def results(x: A, entry: Entry): Option[A] = |
1c5c88f6feb5
clarified Isabelle_Rendering vs. physical painting;
wenzelm
parents:
46165
diff
changeset
|
149 |
if (result_elements match { case Some(es) => es.exists(entry.elements) case None => true }) { |
45474
f793dd5d84b2
index markup elements for more efficient cumulate/select operations;
wenzelm
parents:
45473
diff
changeset
|
150 |
val (y, changed) = |
f793dd5d84b2
index markup elements for more efficient cumulate/select operations;
wenzelm
parents:
45473
diff
changeset
|
151 |
(entry.markup :\ (x, false))((info, res) => |
f793dd5d84b2
index markup elements for more efficient cumulate/select operations;
wenzelm
parents:
45473
diff
changeset
|
152 |
{ |
f793dd5d84b2
index markup elements for more efficient cumulate/select operations;
wenzelm
parents:
45473
diff
changeset
|
153 |
val (y, changed) = res |
46165 | 154 |
val arg = (y, Text.Info(entry.range, info)) |
46178
1c5c88f6feb5
clarified Isabelle_Rendering vs. physical painting;
wenzelm
parents:
46165
diff
changeset
|
155 |
if (result.isDefinedAt(arg)) (result(arg), true) |
45474
f793dd5d84b2
index markup elements for more efficient cumulate/select operations;
wenzelm
parents:
45473
diff
changeset
|
156 |
else res |
f793dd5d84b2
index markup elements for more efficient cumulate/select operations;
wenzelm
parents:
45473
diff
changeset
|
157 |
}) |
f793dd5d84b2
index markup elements for more efficient cumulate/select operations;
wenzelm
parents:
45473
diff
changeset
|
158 |
if (changed) Some(y) else None |
f793dd5d84b2
index markup elements for more efficient cumulate/select operations;
wenzelm
parents:
45473
diff
changeset
|
159 |
} |
f793dd5d84b2
index markup elements for more efficient cumulate/select operations;
wenzelm
parents:
45473
diff
changeset
|
160 |
else None |
45467 | 161 |
|
45459 | 162 |
def stream( |
163 |
last: Text.Offset, |
|
45469
25306d92f4ad
refined Markup_Tree implementation: stacked markup within each entry;
wenzelm
parents:
45468
diff
changeset
|
164 |
stack: List[(Text.Info[A], Stream[(Text.Range, Entry)])]): Stream[Text.Info[A]] = |
45459 | 165 |
{ |
166 |
stack match { |
|
45469
25306d92f4ad
refined Markup_Tree implementation: stacked markup within each entry;
wenzelm
parents:
45468
diff
changeset
|
167 |
case (parent, (range, entry) #:: more) :: rest => |
45467 | 168 |
val subrange = range.restrict(root_range) |
45469
25306d92f4ad
refined Markup_Tree implementation: stacked markup within each entry;
wenzelm
parents:
45468
diff
changeset
|
169 |
val subtree = entry.subtree.overlapping(subrange).toStream |
45459 | 170 |
val start = subrange.start |
171 |
||
46178
1c5c88f6feb5
clarified Isabelle_Rendering vs. physical painting;
wenzelm
parents:
46165
diff
changeset
|
172 |
results(parent.info, entry) match { |
45469
25306d92f4ad
refined Markup_Tree implementation: stacked markup within each entry;
wenzelm
parents:
45468
diff
changeset
|
173 |
case Some(res) => |
25306d92f4ad
refined Markup_Tree implementation: stacked markup within each entry;
wenzelm
parents:
45468
diff
changeset
|
174 |
val next = Text.Info(subrange, res) |
25306d92f4ad
refined Markup_Tree implementation: stacked markup within each entry;
wenzelm
parents:
45468
diff
changeset
|
175 |
val nexts = stream(start, (next, subtree) :: (parent, more) :: rest) |
25306d92f4ad
refined Markup_Tree implementation: stacked markup within each entry;
wenzelm
parents:
45468
diff
changeset
|
176 |
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
|
177 |
else nexts |
25306d92f4ad
refined Markup_Tree implementation: stacked markup within each entry;
wenzelm
parents:
45468
diff
changeset
|
178 |
case None => stream(last, (parent, subtree #::: more) :: rest) |
45459 | 179 |
} |
180 |
||
181 |
case (parent, Stream.Empty) :: rest => |
|
182 |
val stop = parent.range.stop |
|
183 |
val nexts = stream(stop, rest) |
|
184 |
if (last < stop) parent.restrict(Text.Range(last, stop)) #:: nexts |
|
185 |
else nexts |
|
186 |
||
187 |
case Nil => |
|
45467 | 188 |
val stop = root_range.stop |
189 |
if (last < stop) Stream(Text.Info(Text.Range(last, stop), root_info)) |
|
45459 | 190 |
else Stream.empty |
191 |
} |
|
192 |
} |
|
45467 | 193 |
stream(root_range.start, |
194 |
List((Text.Info(root_range, root_info), overlapping(root_range).toStream))) |
|
45459 | 195 |
} |
196 |
||
49465 | 197 |
def swing_tree(parent: DefaultMutableTreeNode, |
198 |
swing_node: Text.Info[List[XML.Elem]] => DefaultMutableTreeNode) |
|
38479
e628da370072
more efficient Markup_Tree, based on branches sorted by quasi-order;
wenzelm
parents:
38478
diff
changeset
|
199 |
{ |
45469
25306d92f4ad
refined Markup_Tree implementation: stacked markup within each entry;
wenzelm
parents:
45468
diff
changeset
|
200 |
for ((_, entry) <- branches) { |
47539 | 201 |
val node = swing_node(Text.Info(entry.range, entry.markup)) |
49465 | 202 |
entry.subtree.swing_tree(node, swing_node) |
203 |
parent.add(node) |
|
34514
2104a836b415
renamed fields of MarkupNode; implemented flatten and leafs
immler@in.tum.de
parents:
34503
diff
changeset
|
204 |
} |
2104a836b415
renamed fields of MarkupNode; implemented flatten and leafs
immler@in.tum.de
parents:
34503
diff
changeset
|
205 |
} |
34717
3f32e08bbb6c
sidekick root data: set buffer length to avoid crash of initial caret move;
wenzelm
parents:
34708
diff
changeset
|
206 |
} |
34514
2104a836b415
renamed fields of MarkupNode; implemented flatten and leafs
immler@in.tum.de
parents:
34503
diff
changeset
|
207 |