author | wenzelm |
Thu, 20 Sep 2012 10:43:04 +0200 | |
changeset 49465 | 76ecbc7f3683 |
parent 49417 | c5a8592fb5d3 |
child 49466 | 99ed1f422635 |
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 |
e628da370072
more efficient Markup_Tree, based on branches sorted by quasi-order;
wenzelm
parents:
38478
diff
changeset
|
15 |
|
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 |
object Markup_Tree |
e628da370072
more efficient Markup_Tree, based on branches sorted by quasi-order;
wenzelm
parents:
38478
diff
changeset
|
18 |
{ |
45456 | 19 |
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
|
20 |
|
45473 | 21 |
object Entry |
22 |
{ |
|
23 |
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
|
24 |
Entry(markup.range, List(markup.info), Set(markup.info.markup.name), subtree) |
45473 | 25 |
} |
26 |
||
45474
f793dd5d84b2
index markup elements for more efficient cumulate/select operations;
wenzelm
parents:
45473
diff
changeset
|
27 |
sealed case class Entry( |
f793dd5d84b2
index markup elements for more efficient cumulate/select operations;
wenzelm
parents:
45473
diff
changeset
|
28 |
range: Text.Range, |
f793dd5d84b2
index markup elements for more efficient cumulate/select operations;
wenzelm
parents:
45473
diff
changeset
|
29 |
rev_markup: List[XML.Elem], |
f793dd5d84b2
index markup elements for more efficient cumulate/select operations;
wenzelm
parents:
45473
diff
changeset
|
30 |
elements: Set[String], |
f793dd5d84b2
index markup elements for more efficient cumulate/select operations;
wenzelm
parents:
45473
diff
changeset
|
31 |
subtree: Markup_Tree) |
45469
25306d92f4ad
refined Markup_Tree implementation: stacked markup within each entry;
wenzelm
parents:
45468
diff
changeset
|
32 |
{ |
45474
f793dd5d84b2
index markup elements for more efficient cumulate/select operations;
wenzelm
parents:
45473
diff
changeset
|
33 |
def + (info: XML.Elem): Entry = |
f793dd5d84b2
index markup elements for more efficient cumulate/select operations;
wenzelm
parents:
45473
diff
changeset
|
34 |
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
|
35 |
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
|
36 |
|
45470 | 37 |
def markup: List[XML.Elem] = rev_markup.reverse |
49417 | 38 |
|
39 |
def reverse_markup: Entry = |
|
40 |
copy(rev_markup = rev_markup.reverse, subtree = subtree.reverse_markup) |
|
45469
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 quasi-order;
wenzelm
parents:
38478
diff
changeset
|
43 |
object Branches |
e628da370072
more efficient Markup_Tree, based on branches sorted by quasi-order;
wenzelm
parents:
38478
diff
changeset
|
44 |
{ |
38578 | 45 |
type T = SortedMap[Text.Range, Entry] |
45456 | 46 |
val empty: T = SortedMap.empty(Text.Range.Ordering) |
49417 | 47 |
|
48 |
def reverse_markup(branches: T): T = |
|
49 |
(empty /: branches.iterator) { case (bs, (r, entry)) => bs + (r -> entry.reverse_markup) } |
|
38479
e628da370072
more efficient Markup_Tree, based on branches sorted by quasi-order;
wenzelm
parents:
38478
diff
changeset
|
50 |
} |
e628da370072
more efficient Markup_Tree, based on branches sorted by quasi-order;
wenzelm
parents:
38478
diff
changeset
|
51 |
} |
34554
7dc6c231da40
abs. stops, markup nodes depend on doc-version;
immler@in.tum.de
parents:
34517
diff
changeset
|
52 |
|
34393 | 53 |
|
46712 | 54 |
final 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
|
55 |
{ |
49417 | 56 |
import Markup_Tree._ |
57 |
||
45469
25306d92f4ad
refined Markup_Tree implementation: stacked markup within each entry;
wenzelm
parents:
45468
diff
changeset
|
58 |
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
|
59 |
this(branches + (entry.range -> entry)) |
25306d92f4ad
refined Markup_Tree implementation: stacked markup within each entry;
wenzelm
parents:
45468
diff
changeset
|
60 |
|
49417 | 61 |
def reverse_markup: Markup_Tree = new Markup_Tree(Branches.reverse_markup(branches)) |
34557 | 62 |
|
38563 | 63 |
override def toString = |
64 |
branches.toList.map(_._2) match { |
|
65 |
case Nil => "Empty" |
|
66 |
case list => list.mkString("Tree(", ",", ")") |
|
67 |
} |
|
68 |
||
45469
25306d92f4ad
refined Markup_Tree implementation: stacked markup within each entry;
wenzelm
parents:
45468
diff
changeset
|
69 |
private def overlapping(range: Text.Range): Branches.T = |
45457 | 70 |
{ |
71 |
val start = Text.Range(range.start) |
|
72 |
val stop = Text.Range(range.stop) |
|
73 |
val bs = branches.range(start, stop) |
|
74 |
// FIXME check after Scala 2.8.x |
|
75 |
branches.get(stop) match { |
|
45469
25306d92f4ad
refined Markup_Tree implementation: stacked markup within each entry;
wenzelm
parents:
45468
diff
changeset
|
76 |
case Some(end) if range overlaps end.range => bs + (end.range -> end) |
45457 | 77 |
case _ => bs |
78 |
} |
|
79 |
} |
|
80 |
||
45469
25306d92f4ad
refined Markup_Tree implementation: stacked markup within each entry;
wenzelm
parents:
45468
diff
changeset
|
81 |
def + (new_markup: Text.Markup): Markup_Tree = |
34703 | 82 |
{ |
45469
25306d92f4ad
refined Markup_Tree implementation: stacked markup within each entry;
wenzelm
parents:
45468
diff
changeset
|
83 |
val new_range = new_markup.range |
25306d92f4ad
refined Markup_Tree implementation: stacked markup within each entry;
wenzelm
parents:
45468
diff
changeset
|
84 |
|
38578 | 85 |
branches.get(new_range) match { |
45473 | 86 |
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
|
87 |
case Some(entry) => |
25306d92f4ad
refined Markup_Tree implementation: stacked markup within each entry;
wenzelm
parents:
45468
diff
changeset
|
88 |
if (entry.range == new_range) |
45473 | 89 |
new Markup_Tree(branches, entry + new_markup.info) |
45469
25306d92f4ad
refined Markup_Tree implementation: stacked markup within each entry;
wenzelm
parents:
45468
diff
changeset
|
90 |
else if (entry.range.contains(new_range)) |
25306d92f4ad
refined Markup_Tree implementation: stacked markup within each entry;
wenzelm
parents:
45468
diff
changeset
|
91 |
new Markup_Tree(branches, entry.copy(subtree = entry.subtree + new_markup)) |
38578 | 92 |
else if (new_range.contains(branches.head._1) && new_range.contains(branches.last._1)) |
45473 | 93 |
new Markup_Tree(Branches.empty, Entry(new_markup, this)) |
38482
7b6ee937b75f
tuned Markup_Tree, using SortedMap more carefully;
wenzelm
parents:
38479
diff
changeset
|
94 |
else { |
45457 | 95 |
val body = overlapping(new_range) |
38578 | 96 |
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
|
97 |
val rest = // branches -- body, modulo workarounds for Redblack in Scala 2.8.0 FIXME |
38657 | 98 |
if (body.size > 1) |
99 |
(Branches.empty /: branches)((rest, entry) => |
|
100 |
if (body.isDefinedAt(entry._1)) rest else rest + entry) |
|
101 |
else branches |
|
45473 | 102 |
new Markup_Tree(rest, Entry(new_markup, new Markup_Tree(body))) |
38482
7b6ee937b75f
tuned Markup_Tree, using SortedMap more carefully;
wenzelm
parents:
38479
diff
changeset
|
103 |
} |
7b6ee937b75f
tuned Markup_Tree, using SortedMap more carefully;
wenzelm
parents:
38479
diff
changeset
|
104 |
else { // FIXME split markup!? |
48762 | 105 |
System.err.println("Ignored overlapping markup information: " + new_markup + |
106 |
body.filter(e => !new_range.contains(e._1)).mkString("\n")) |
|
38482
7b6ee937b75f
tuned Markup_Tree, using SortedMap more carefully;
wenzelm
parents:
38479
diff
changeset
|
107 |
this |
7b6ee937b75f
tuned Markup_Tree, using SortedMap more carefully;
wenzelm
parents:
38479
diff
changeset
|
108 |
} |
7b6ee937b75f
tuned Markup_Tree, using SortedMap more carefully;
wenzelm
parents:
38479
diff
changeset
|
109 |
} |
34703 | 110 |
} |
111 |
} |
|
112 |
||
46178
1c5c88f6feb5
clarified Isabelle_Rendering vs. physical painting;
wenzelm
parents:
46165
diff
changeset
|
113 |
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
|
114 |
result: PartialFunction[(A, Text.Markup), A]): Stream[Text.Info[A]] = |
45459 | 115 |
{ |
46178
1c5c88f6feb5
clarified Isabelle_Rendering vs. physical painting;
wenzelm
parents:
46165
diff
changeset
|
116 |
def results(x: A, entry: Entry): Option[A] = |
1c5c88f6feb5
clarified Isabelle_Rendering vs. physical painting;
wenzelm
parents:
46165
diff
changeset
|
117 |
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
|
118 |
val (y, changed) = |
f793dd5d84b2
index markup elements for more efficient cumulate/select operations;
wenzelm
parents:
45473
diff
changeset
|
119 |
(entry.markup :\ (x, false))((info, res) => |
f793dd5d84b2
index markup elements for more efficient cumulate/select operations;
wenzelm
parents:
45473
diff
changeset
|
120 |
{ |
f793dd5d84b2
index markup elements for more efficient cumulate/select operations;
wenzelm
parents:
45473
diff
changeset
|
121 |
val (y, changed) = res |
46165 | 122 |
val arg = (y, Text.Info(entry.range, info)) |
46178
1c5c88f6feb5
clarified Isabelle_Rendering vs. physical painting;
wenzelm
parents:
46165
diff
changeset
|
123 |
if (result.isDefinedAt(arg)) (result(arg), true) |
45474
f793dd5d84b2
index markup elements for more efficient cumulate/select operations;
wenzelm
parents:
45473
diff
changeset
|
124 |
else res |
f793dd5d84b2
index markup elements for more efficient cumulate/select operations;
wenzelm
parents:
45473
diff
changeset
|
125 |
}) |
f793dd5d84b2
index markup elements for more efficient cumulate/select operations;
wenzelm
parents:
45473
diff
changeset
|
126 |
if (changed) Some(y) else None |
f793dd5d84b2
index markup elements for more efficient cumulate/select operations;
wenzelm
parents:
45473
diff
changeset
|
127 |
} |
f793dd5d84b2
index markup elements for more efficient cumulate/select operations;
wenzelm
parents:
45473
diff
changeset
|
128 |
else None |
45467 | 129 |
|
45459 | 130 |
def stream( |
131 |
last: Text.Offset, |
|
45469
25306d92f4ad
refined Markup_Tree implementation: stacked markup within each entry;
wenzelm
parents:
45468
diff
changeset
|
132 |
stack: List[(Text.Info[A], Stream[(Text.Range, Entry)])]): Stream[Text.Info[A]] = |
45459 | 133 |
{ |
134 |
stack match { |
|
45469
25306d92f4ad
refined Markup_Tree implementation: stacked markup within each entry;
wenzelm
parents:
45468
diff
changeset
|
135 |
case (parent, (range, entry) #:: more) :: rest => |
45467 | 136 |
val subrange = range.restrict(root_range) |
45469
25306d92f4ad
refined Markup_Tree implementation: stacked markup within each entry;
wenzelm
parents:
45468
diff
changeset
|
137 |
val subtree = entry.subtree.overlapping(subrange).toStream |
45459 | 138 |
val start = subrange.start |
139 |
||
46178
1c5c88f6feb5
clarified Isabelle_Rendering vs. physical painting;
wenzelm
parents:
46165
diff
changeset
|
140 |
results(parent.info, entry) match { |
45469
25306d92f4ad
refined Markup_Tree implementation: stacked markup within each entry;
wenzelm
parents:
45468
diff
changeset
|
141 |
case Some(res) => |
25306d92f4ad
refined Markup_Tree implementation: stacked markup within each entry;
wenzelm
parents:
45468
diff
changeset
|
142 |
val next = Text.Info(subrange, res) |
25306d92f4ad
refined Markup_Tree implementation: stacked markup within each entry;
wenzelm
parents:
45468
diff
changeset
|
143 |
val nexts = stream(start, (next, subtree) :: (parent, more) :: rest) |
25306d92f4ad
refined Markup_Tree implementation: stacked markup within each entry;
wenzelm
parents:
45468
diff
changeset
|
144 |
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
|
145 |
else nexts |
25306d92f4ad
refined Markup_Tree implementation: stacked markup within each entry;
wenzelm
parents:
45468
diff
changeset
|
146 |
case None => stream(last, (parent, subtree #::: more) :: rest) |
45459 | 147 |
} |
148 |
||
149 |
case (parent, Stream.Empty) :: rest => |
|
150 |
val stop = parent.range.stop |
|
151 |
val nexts = stream(stop, rest) |
|
152 |
if (last < stop) parent.restrict(Text.Range(last, stop)) #:: nexts |
|
153 |
else nexts |
|
154 |
||
155 |
case Nil => |
|
45467 | 156 |
val stop = root_range.stop |
157 |
if (last < stop) Stream(Text.Info(Text.Range(last, stop), root_info)) |
|
45459 | 158 |
else Stream.empty |
159 |
} |
|
160 |
} |
|
45467 | 161 |
stream(root_range.start, |
162 |
List((Text.Info(root_range, root_info), overlapping(root_range).toStream))) |
|
45459 | 163 |
} |
164 |
||
49465 | 165 |
def swing_tree(parent: DefaultMutableTreeNode, |
166 |
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
|
167 |
{ |
45469
25306d92f4ad
refined Markup_Tree implementation: stacked markup within each entry;
wenzelm
parents:
45468
diff
changeset
|
168 |
for ((_, entry) <- branches) { |
47539 | 169 |
val node = swing_node(Text.Info(entry.range, entry.markup)) |
49465 | 170 |
entry.subtree.swing_tree(node, swing_node) |
171 |
parent.add(node) |
|
34514
2104a836b415
renamed fields of MarkupNode; implemented flatten and leafs
immler@in.tum.de
parents:
34503
diff
changeset
|
172 |
} |
2104a836b415
renamed fields of MarkupNode; implemented flatten and leafs
immler@in.tum.de
parents:
34503
diff
changeset
|
173 |
} |
34717
3f32e08bbb6c
sidekick root data: set buffer length to avoid crash of initial caret move;
wenzelm
parents:
34708
diff
changeset
|
174 |
} |
34514
2104a836b415
renamed fields of MarkupNode; implemented flatten and leafs
immler@in.tum.de
parents:
34503
diff
changeset
|
175 |