author | wenzelm |
Tue, 08 Dec 2009 16:30:20 +0100 | |
changeset 34760 | dc7f5e0d9d27 |
parent 34759 | bfea7839d9e1 |
child 34778 | 8eccd35e975e |
permissions | -rw-r--r-- |
34407 | 1 |
/* |
34485 | 2 |
* Document as list of commands, consisting of lists of tokens |
34407 | 3 |
* |
4 |
* @author Johannes Hölzl, TU Munich |
|
34532 | 5 |
* @author Fabian Immler, TU Munich |
34485 | 6 |
* @author Makarius |
34407 | 7 |
*/ |
8 |
||
34318
c13e168a8ae6
original sources from Johannes Hölzl a48e0c6ab1aea77c52d596f7efc007a543d3d10c with minor modifications of directory layout;
wenzelm
parents:
diff
changeset
|
9 |
package isabelle.proofdocument |
c13e168a8ae6
original sources from Johannes Hölzl a48e0c6ab1aea77c52d596f7efc007a543d3d10c with minor modifications of directory layout;
wenzelm
parents:
diff
changeset
|
10 |
|
34760 | 11 |
|
34703 | 12 |
import scala.actors.Actor, Actor._ |
13 |
||
34318
c13e168a8ae6
original sources from Johannes Hölzl a48e0c6ab1aea77c52d596f7efc007a543d3d10c with minor modifications of directory layout;
wenzelm
parents:
diff
changeset
|
14 |
import java.util.regex.Pattern |
34703 | 15 |
|
34318
c13e168a8ae6
original sources from Johannes Hölzl a48e0c6ab1aea77c52d596f7efc007a543d3d10c with minor modifications of directory layout;
wenzelm
parents:
diff
changeset
|
16 |
|
34760 | 17 |
object Proof_Document |
34483 | 18 |
{ |
34582 | 19 |
// Be careful when changing this regex. Not only must it handle the |
34318
c13e168a8ae6
original sources from Johannes Hölzl a48e0c6ab1aea77c52d596f7efc007a543d3d10c with minor modifications of directory layout;
wenzelm
parents:
diff
changeset
|
20 |
// spurious end of a token but also: |
c13e168a8ae6
original sources from Johannes Hölzl a48e0c6ab1aea77c52d596f7efc007a543d3d10c with minor modifications of directory layout;
wenzelm
parents:
diff
changeset
|
21 |
// Bug ID: 5050507 Pattern.matches throws StackOverflow Error |
c13e168a8ae6
original sources from Johannes Hölzl a48e0c6ab1aea77c52d596f7efc007a543d3d10c with minor modifications of directory layout;
wenzelm
parents:
diff
changeset
|
22 |
// http://bugs.sun.com/bugdatabase/view_bug.do?bug_id=5050507 |
c13e168a8ae6
original sources from Johannes Hölzl a48e0c6ab1aea77c52d596f7efc007a543d3d10c with minor modifications of directory layout;
wenzelm
parents:
diff
changeset
|
23 |
|
34483 | 24 |
val token_pattern = |
34318
c13e168a8ae6
original sources from Johannes Hölzl a48e0c6ab1aea77c52d596f7efc007a543d3d10c with minor modifications of directory layout;
wenzelm
parents:
diff
changeset
|
25 |
Pattern.compile( |
c13e168a8ae6
original sources from Johannes Hölzl a48e0c6ab1aea77c52d596f7efc007a543d3d10c with minor modifications of directory layout;
wenzelm
parents:
diff
changeset
|
26 |
"\\{\\*([^*]|\\*[^}]|\\*\\z)*(\\z|\\*\\})|" + |
c13e168a8ae6
original sources from Johannes Hölzl a48e0c6ab1aea77c52d596f7efc007a543d3d10c with minor modifications of directory layout;
wenzelm
parents:
diff
changeset
|
27 |
"\\(\\*([^*]|\\*[^)]|\\*\\z)*(\\z|\\*\\))|" + |
c13e168a8ae6
original sources from Johannes Hölzl a48e0c6ab1aea77c52d596f7efc007a543d3d10c with minor modifications of directory layout;
wenzelm
parents:
diff
changeset
|
28 |
"(\\?'?|')[A-Za-z_0-9.]*|" + |
c13e168a8ae6
original sources from Johannes Hölzl a48e0c6ab1aea77c52d596f7efc007a543d3d10c with minor modifications of directory layout;
wenzelm
parents:
diff
changeset
|
29 |
"[A-Za-z_0-9.]+|" + |
c13e168a8ae6
original sources from Johannes Hölzl a48e0c6ab1aea77c52d596f7efc007a543d3d10c with minor modifications of directory layout;
wenzelm
parents:
diff
changeset
|
30 |
"[!#$%&*+-/<=>?@^_|~]+|" + |
c13e168a8ae6
original sources from Johannes Hölzl a48e0c6ab1aea77c52d596f7efc007a543d3d10c with minor modifications of directory layout;
wenzelm
parents:
diff
changeset
|
31 |
"\"([^\\\\\"]?(\\\\(.|\\z))?)*+(\"|\\z)|" + |
c13e168a8ae6
original sources from Johannes Hölzl a48e0c6ab1aea77c52d596f7efc007a543d3d10c with minor modifications of directory layout;
wenzelm
parents:
diff
changeset
|
32 |
"`([^\\\\`]?(\\\\(.|\\z))?)*+(`|\\z)|" + |
c13e168a8ae6
original sources from Johannes Hölzl a48e0c6ab1aea77c52d596f7efc007a543d3d10c with minor modifications of directory layout;
wenzelm
parents:
diff
changeset
|
33 |
"[()\\[\\]{}:;]", Pattern.MULTILINE) |
34485 | 34 |
|
34660 | 35 |
val empty = |
34760 | 36 |
new Proof_Document(isabelle.jedit.Isabelle.system.id(), |
34724
b1079c3ba1da
Prover: keep command_change/document_change event buses here, not in ProofDocument, Command, State, Plugin;
wenzelm
parents:
34722
diff
changeset
|
37 |
Linear_Set(), Map(), Linear_Set(), Map(), _ => false) |
34660 | 38 |
|
39 |
type StructureChange = List[(Option[Command], Option[Command])] |
|
34538
20bfcca24658
Prover as actor managing ProofDocument-versions (removed EventBus structural_changes);
immler@in.tum.de
parents:
34532
diff
changeset
|
40 |
|
34318
c13e168a8ae6
original sources from Johannes Hölzl a48e0c6ab1aea77c52d596f7efc007a543d3d10c with minor modifications of directory layout;
wenzelm
parents:
diff
changeset
|
41 |
} |
c13e168a8ae6
original sources from Johannes Hölzl a48e0c6ab1aea77c52d596f7efc007a543d3d10c with minor modifications of directory layout;
wenzelm
parents:
diff
changeset
|
42 |
|
34760 | 43 |
class Proof_Document( |
34582 | 44 |
val id: String, |
34689 | 45 |
val tokens: Linear_Set[Token], |
34582 | 46 |
val token_start: Map[Token, Int], |
34689 | 47 |
val commands: Linear_Set[Command], |
34722 | 48 |
var states: Map[Command, Command_State], // FIXME immutable |
34724
b1079c3ba1da
Prover: keep command_change/document_change event buses here, not in ProofDocument, Command, State, Plugin;
wenzelm
parents:
34722
diff
changeset
|
49 |
is_command_keyword: String => Boolean) |
34483 | 50 |
{ |
34760 | 51 |
import Proof_Document.StructureChange |
34653 | 52 |
|
34760 | 53 |
def set_command_keyword(f: String => Boolean): Proof_Document = |
54 |
new Proof_Document(id, tokens, token_start, commands, states, f) |
|
34485 | 55 |
|
34582 | 56 |
def content = Token.string_from_tokens(Nil ++ tokens, token_start) |
34657 | 57 |
|
58 |
||
34693 | 59 |
|
34485 | 60 |
/** token view **/ |
61 |
||
34760 | 62 |
def text_changed(change: Change): (Proof_Document, StructureChange) = |
34485 | 63 |
{ |
34760 | 64 |
def edit_doc(doc_chgs: (Proof_Document, StructureChange), edit: Edit) = { |
34660 | 65 |
val (doc, chgs) = doc_chgs |
34693 | 66 |
val (new_doc, chg) = doc.text_edit(edit, change.id) |
34660 | 67 |
(new_doc, chgs ++ chg) |
68 |
} |
|
34693 | 69 |
((this, Nil: StructureChange) /: change.edits)(edit_doc) |
34660 | 70 |
} |
71 |
||
34760 | 72 |
def text_edit(e: Edit, id: String): (Proof_Document, StructureChange) = |
34660 | 73 |
{ |
74 |
case class TextChange(start: Int, added: String, removed: String) |
|
75 |
val change = e match { |
|
76 |
case Insert(s, a) => TextChange(s, a, "") |
|
77 |
case Remove(s, r) => TextChange(s, "", r) |
|
78 |
} |
|
34551
bd2b8fde9e25
incomplete changes of immutable tokens and commands
immler@in.tum.de
parents:
34550
diff
changeset
|
79 |
//indices of tokens |
bd2b8fde9e25
incomplete changes of immutable tokens and commands
immler@in.tum.de
parents:
34550
diff
changeset
|
80 |
var start: Map[Token, Int] = token_start |
bd2b8fde9e25
incomplete changes of immutable tokens and commands
immler@in.tum.de
parents:
34550
diff
changeset
|
81 |
def stop(t: Token) = start(t) + t.length |
bd2b8fde9e25
incomplete changes of immutable tokens and commands
immler@in.tum.de
parents:
34550
diff
changeset
|
82 |
// split old token lists |
34582 | 83 |
val tokens = Nil ++ this.tokens |
34551
bd2b8fde9e25
incomplete changes of immutable tokens and commands
immler@in.tum.de
parents:
34550
diff
changeset
|
84 |
val (begin, remaining) = tokens.span(stop(_) < change.start) |
34648 | 85 |
val (removed, end) = remaining.span(token_start(_) <= change.start + change.removed.length) |
34551
bd2b8fde9e25
incomplete changes of immutable tokens and commands
immler@in.tum.de
parents:
34550
diff
changeset
|
86 |
// update indices |
34582 | 87 |
start = end.foldLeft(start)((s, t) => |
34648 | 88 |
s + (t -> (s(t) + change.added.length - change.removed.length))) |
34485 | 89 |
|
34551
bd2b8fde9e25
incomplete changes of immutable tokens and commands
immler@in.tum.de
parents:
34550
diff
changeset
|
90 |
val split_begin = removed.takeWhile(start(_) < change.start). |
34554
7dc6c231da40
abs. stops, markup nodes depend on doc-version;
immler@in.tum.de
parents:
34551
diff
changeset
|
91 |
map (t => { |
7dc6c231da40
abs. stops, markup nodes depend on doc-version;
immler@in.tum.de
parents:
34551
diff
changeset
|
92 |
val split_tok = new Token(t.content.substring(0, change.start - start(t)), t.kind) |
7dc6c231da40
abs. stops, markup nodes depend on doc-version;
immler@in.tum.de
parents:
34551
diff
changeset
|
93 |
start += (split_tok -> start(t)) |
7dc6c231da40
abs. stops, markup nodes depend on doc-version;
immler@in.tum.de
parents:
34551
diff
changeset
|
94 |
split_tok |
7dc6c231da40
abs. stops, markup nodes depend on doc-version;
immler@in.tum.de
parents:
34551
diff
changeset
|
95 |
}) |
7dc6c231da40
abs. stops, markup nodes depend on doc-version;
immler@in.tum.de
parents:
34551
diff
changeset
|
96 |
|
34648 | 97 |
val split_end = removed.dropWhile(stop(_) < change.start + change.removed.length). |
34554
7dc6c231da40
abs. stops, markup nodes depend on doc-version;
immler@in.tum.de
parents:
34551
diff
changeset
|
98 |
map (t => { |
34582 | 99 |
val split_tok = |
34648 | 100 |
new Token(t.content.substring(change.start + change.removed.length - start(t)), t.kind) |
34554
7dc6c231da40
abs. stops, markup nodes depend on doc-version;
immler@in.tum.de
parents:
34551
diff
changeset
|
101 |
start += (split_tok -> start(t)) |
7dc6c231da40
abs. stops, markup nodes depend on doc-version;
immler@in.tum.de
parents:
34551
diff
changeset
|
102 |
split_tok |
7dc6c231da40
abs. stops, markup nodes depend on doc-version;
immler@in.tum.de
parents:
34551
diff
changeset
|
103 |
}) |
34551
bd2b8fde9e25
incomplete changes of immutable tokens and commands
immler@in.tum.de
parents:
34550
diff
changeset
|
104 |
// update indices |
34554
7dc6c231da40
abs. stops, markup nodes depend on doc-version;
immler@in.tum.de
parents:
34551
diff
changeset
|
105 |
start = removed.foldLeft (start) ((s, t) => s - t) |
7dc6c231da40
abs. stops, markup nodes depend on doc-version;
immler@in.tum.de
parents:
34551
diff
changeset
|
106 |
start = split_end.foldLeft (start) ((s, t) => |
7dc6c231da40
abs. stops, markup nodes depend on doc-version;
immler@in.tum.de
parents:
34551
diff
changeset
|
107 |
s + (t -> (change.start + change.added.length))) |
34318
c13e168a8ae6
original sources from Johannes Hölzl a48e0c6ab1aea77c52d596f7efc007a543d3d10c with minor modifications of directory layout;
wenzelm
parents:
diff
changeset
|
108 |
|
34551
bd2b8fde9e25
incomplete changes of immutable tokens and commands
immler@in.tum.de
parents:
34550
diff
changeset
|
109 |
val ins = new Token(change.added, Token.Kind.OTHER) |
bd2b8fde9e25
incomplete changes of immutable tokens and commands
immler@in.tum.de
parents:
34550
diff
changeset
|
110 |
start += (ins -> change.start) |
bd2b8fde9e25
incomplete changes of immutable tokens and commands
immler@in.tum.de
parents:
34550
diff
changeset
|
111 |
|
34582 | 112 |
var invalid_tokens = split_begin ::: ins :: split_end ::: end |
113 |
var new_tokens: List[Token] = Nil |
|
114 |
var old_suffix: List[Token] = Nil |
|
34318
c13e168a8ae6
original sources from Johannes Hölzl a48e0c6ab1aea77c52d596f7efc007a543d3d10c with minor modifications of directory layout;
wenzelm
parents:
diff
changeset
|
115 |
|
34551
bd2b8fde9e25
incomplete changes of immutable tokens and commands
immler@in.tum.de
parents:
34550
diff
changeset
|
116 |
val match_start = invalid_tokens.firstOption.map(start(_)).getOrElse(0) |
34582 | 117 |
val matcher = |
34760 | 118 |
Proof_Document.token_pattern.matcher(Token.string_from_tokens(invalid_tokens, start)) |
34526 | 119 |
|
120 |
while (matcher.find() && invalid_tokens != Nil) { |
|
34485 | 121 |
val kind = |
34505
87f4f70d61bb
ProofDocument: pass is_command_keyword directly, not via full-blown Prover object;
wenzelm
parents:
34496
diff
changeset
|
122 |
if (is_command_keyword(matcher.group)) |
34485 | 123 |
Token.Kind.COMMAND_START |
34494 | 124 |
else if (matcher.end - matcher.start > 2 && matcher.group.substring(0, 2) == "(*") |
34485 | 125 |
Token.Kind.COMMENT |
126 |
else |
|
127 |
Token.Kind.OTHER |
|
34551
bd2b8fde9e25
incomplete changes of immutable tokens and commands
immler@in.tum.de
parents:
34550
diff
changeset
|
128 |
val new_token = new Token(matcher.group, kind) |
bd2b8fde9e25
incomplete changes of immutable tokens and commands
immler@in.tum.de
parents:
34550
diff
changeset
|
129 |
start += (new_token -> (match_start + matcher.start)) |
34526 | 130 |
new_tokens ::= new_token |
34318
c13e168a8ae6
original sources from Johannes Hölzl a48e0c6ab1aea77c52d596f7efc007a543d3d10c with minor modifications of directory layout;
wenzelm
parents:
diff
changeset
|
131 |
|
34660 | 132 |
invalid_tokens = invalid_tokens dropWhile (stop(_) < stop(new_token)) |
34526 | 133 |
invalid_tokens match { |
34582 | 134 |
case t :: ts => |
135 |
if (start(t) == start(new_token) && |
|
136 |
start(t) > change.start + change.added.length) { |
|
34597 | 137 |
old_suffix = t :: ts |
34592 | 138 |
new_tokens = new_tokens.tail |
34526 | 139 |
invalid_tokens = Nil |
140 |
} |
|
141 |
case _ => |
|
34318
c13e168a8ae6
original sources from Johannes Hölzl a48e0c6ab1aea77c52d596f7efc007a543d3d10c with minor modifications of directory layout;
wenzelm
parents:
diff
changeset
|
142 |
} |
c13e168a8ae6
original sources from Johannes Hölzl a48e0c6ab1aea77c52d596f7efc007a543d3d10c with minor modifications of directory layout;
wenzelm
parents:
diff
changeset
|
143 |
} |
34526 | 144 |
val insert = new_tokens.reverse |
34544
56217d219e27
proofdocument-versions get id from changes
immler@in.tum.de
parents:
34541
diff
changeset
|
145 |
val new_token_list = begin ::: insert ::: old_suffix |
34660 | 146 |
token_changed(id, begin.lastOption, insert, |
34597 | 147 |
old_suffix.firstOption, new_token_list, start) |
34318
c13e168a8ae6
original sources from Johannes Hölzl a48e0c6ab1aea77c52d596f7efc007a543d3d10c with minor modifications of directory layout;
wenzelm
parents:
diff
changeset
|
148 |
} |
34582 | 149 |
|
34318
c13e168a8ae6
original sources from Johannes Hölzl a48e0c6ab1aea77c52d596f7efc007a543d3d10c with minor modifications of directory layout;
wenzelm
parents:
diff
changeset
|
150 |
|
34485 | 151 |
/** command view **/ |
152 |
||
34582 | 153 |
private def token_changed( |
154 |
new_id: String, |
|
155 |
before_change: Option[Token], |
|
156 |
inserted_tokens: List[Token], |
|
157 |
after_change: Option[Token], |
|
34597 | 158 |
new_tokens: List[Token], |
34660 | 159 |
new_token_start: Map[Token, Int]): |
34760 | 160 |
(Proof_Document, StructureChange) = |
34485 | 161 |
{ |
34689 | 162 |
val new_tokenset = Linear_Set[Token]() ++ new_tokens |
34593 | 163 |
val cmd_before_change = before_change match { |
164 |
case None => None |
|
165 |
case Some(bc) => |
|
166 |
val cmd_with_bc = commands.find(_.contains(bc)).get |
|
167 |
if (cmd_with_bc.tokens.last == bc) { |
|
168 |
if (new_tokenset.next(bc).map(_.is_start).getOrElse(true)) |
|
169 |
Some(cmd_with_bc) |
|
170 |
else commands.prev(cmd_with_bc) |
|
171 |
} |
|
172 |
else commands.prev(cmd_with_bc) |
|
173 |
} |
|
34544
56217d219e27
proofdocument-versions get id from changes
immler@in.tum.de
parents:
34541
diff
changeset
|
174 |
|
34593 | 175 |
val cmd_after_change = after_change match { |
176 |
case None => None |
|
177 |
case Some(ac) => |
|
178 |
val cmd_with_ac = commands.find(_.contains(ac)).get |
|
179 |
if (ac.is_start) |
|
180 |
Some(cmd_with_ac) |
|
181 |
else |
|
182 |
commands.next(cmd_with_ac) |
|
183 |
} |
|
34485 | 184 |
|
34593 | 185 |
val removed_commands = commands.dropWhile(Some(_) != cmd_before_change).drop(1). |
34554
7dc6c231da40
abs. stops, markup nodes depend on doc-version;
immler@in.tum.de
parents:
34551
diff
changeset
|
186 |
takeWhile(Some(_) != cmd_after_change) |
7dc6c231da40
abs. stops, markup nodes depend on doc-version;
immler@in.tum.de
parents:
34551
diff
changeset
|
187 |
|
7dc6c231da40
abs. stops, markup nodes depend on doc-version;
immler@in.tum.de
parents:
34551
diff
changeset
|
188 |
// calculate inserted commands |
34526 | 189 |
def tokens_to_commands(tokens: List[Token]): List[Command]= { |
190 |
tokens match { |
|
191 |
case Nil => Nil |
|
34582 | 192 |
case t :: ts => |
193 |
val (cmd, rest) = |
|
194 |
ts.span(t => t.kind != Token.Kind.COMMAND_START && t.kind != Token.Kind.COMMENT) |
|
34724
b1079c3ba1da
Prover: keep command_change/document_change event buses here, not in ProofDocument, Command, State, Plugin;
wenzelm
parents:
34722
diff
changeset
|
195 |
new Command(t :: cmd, new_token_start) :: tokens_to_commands(rest) |
34485 | 196 |
} |
197 |
} |
|
198 |
||
34593 | 199 |
val split_begin = |
200 |
if (before_change.isDefined) { |
|
201 |
val changed = |
|
202 |
if (cmd_before_change.isDefined) |
|
34595 | 203 |
new_tokens.dropWhile(_ != cmd_before_change.get.tokens.last).drop(1) |
34593 | 204 |
else new_tokenset |
205 |
if (changed.exists(_ == before_change.get)) |
|
34597 | 206 |
changed.takeWhile(_ != before_change.get).toList ::: |
207 |
List(before_change.get) |
|
34593 | 208 |
else Nil |
209 |
} else Nil |
|
34554
7dc6c231da40
abs. stops, markup nodes depend on doc-version;
immler@in.tum.de
parents:
34551
diff
changeset
|
210 |
|
34593 | 211 |
val split_end = |
34667 | 212 |
if (after_change.isDefined) { |
34595 | 213 |
val unchanged = new_tokens.dropWhile(_ != after_change.get) |
34667 | 214 |
if(cmd_after_change.isDefined) { |
215 |
if (unchanged.exists(_ == cmd_after_change.get.tokens.first)) |
|
216 |
unchanged.takeWhile(_ != cmd_after_change.get.tokens.first).toList |
|
217 |
else Nil |
|
218 |
} else { |
|
219 |
unchanged |
|
220 |
} |
|
34593 | 221 |
} else Nil |
222 |
||
34597 | 223 |
val rescan_begin = |
224 |
split_begin ::: |
|
225 |
before_change.map(bc => new_tokens.dropWhile(_ != bc).drop(1)).getOrElse(new_tokens) |
|
34582 | 226 |
val rescanning_tokens = |
34597 | 227 |
after_change.map(ac => rescan_begin.takeWhile(_ != ac)).getOrElse(rescan_begin) ::: |
228 |
split_end |
|
34593 | 229 |
val inserted_commands = tokens_to_commands(rescanning_tokens.toList) |
34554
7dc6c231da40
abs. stops, markup nodes depend on doc-version;
immler@in.tum.de
parents:
34551
diff
changeset
|
230 |
|
34550
171c8c6e5707
prepared proofdocument for only needed changes
immler@in.tum.de
parents:
34544
diff
changeset
|
231 |
// build new document |
34739 | 232 |
val new_commandset = commands. |
233 |
delete_between(cmd_before_change, cmd_after_change). |
|
234 |
append_after(cmd_before_change, inserted_commands) |
|
235 |
||
34554
7dc6c231da40
abs. stops, markup nodes depend on doc-version;
immler@in.tum.de
parents:
34551
diff
changeset
|
236 |
|
34544
56217d219e27
proofdocument-versions get id from changes
immler@in.tum.de
parents:
34541
diff
changeset
|
237 |
val doc = |
34760 | 238 |
new Proof_Document(new_id, new_tokenset, new_token_start, new_commandset, |
34724
b1079c3ba1da
Prover: keep command_change/document_change event buses here, not in ProofDocument, Command, State, Plugin;
wenzelm
parents:
34722
diff
changeset
|
239 |
states -- removed_commands, is_command_keyword) |
34660 | 240 |
|
241 |
val removes = |
|
242 |
for (cmd <- removed_commands) yield (cmd_before_change -> None) |
|
243 |
val inserts = |
|
244 |
for (cmd <- inserted_commands) yield (doc.commands.prev(cmd) -> Some(cmd)) |
|
245 |
||
246 |
return (doc, removes.toList ++ inserts) |
|
34485 | 247 |
} |
34596 | 248 |
|
249 |
val commands_offsets = { |
|
250 |
var last_stop = 0 |
|
251 |
(for (c <- commands) yield { |
|
252 |
val r = c -> (last_stop,c.stop(this)) |
|
253 |
last_stop = c.stop(this) |
|
254 |
r |
|
255 |
}).toArray |
|
256 |
} |
|
257 |
||
34712
4f0ee5ab0380
replaced find_command_at by command_at -- null-free, proper Option;
wenzelm
parents:
34703
diff
changeset
|
258 |
def command_at(pos: Int): Option[Command] = |
4f0ee5ab0380
replaced find_command_at by command_at -- null-free, proper Option;
wenzelm
parents:
34703
diff
changeset
|
259 |
find_command(pos, 0, commands_offsets.length) |
4f0ee5ab0380
replaced find_command_at by command_at -- null-free, proper Option;
wenzelm
parents:
34703
diff
changeset
|
260 |
|
34596 | 261 |
// use a binary search to find commands for a given offset |
34712
4f0ee5ab0380
replaced find_command_at by command_at -- null-free, proper Option;
wenzelm
parents:
34703
diff
changeset
|
262 |
private def find_command(pos: Int, array_start: Int, array_stop: Int): Option[Command] = |
4f0ee5ab0380
replaced find_command_at by command_at -- null-free, proper Option;
wenzelm
parents:
34703
diff
changeset
|
263 |
{ |
34596 | 264 |
val middle_index = (array_start + array_stop) / 2 |
34712
4f0ee5ab0380
replaced find_command_at by command_at -- null-free, proper Option;
wenzelm
parents:
34703
diff
changeset
|
265 |
if (middle_index >= commands_offsets.length) return None |
34596 | 266 |
val (middle, (start, stop)) = commands_offsets(middle_index) |
267 |
// does middle contain pos? |
|
34712
4f0ee5ab0380
replaced find_command_at by command_at -- null-free, proper Option;
wenzelm
parents:
34703
diff
changeset
|
268 |
if (start <= pos && pos < stop) |
4f0ee5ab0380
replaced find_command_at by command_at -- null-free, proper Option;
wenzelm
parents:
34703
diff
changeset
|
269 |
Some(middle) |
34596 | 270 |
else if (start > pos) |
34712
4f0ee5ab0380
replaced find_command_at by command_at -- null-free, proper Option;
wenzelm
parents:
34703
diff
changeset
|
271 |
find_command(pos, array_start, middle_index) |
34596 | 272 |
else if (stop <= pos) |
34712
4f0ee5ab0380
replaced find_command_at by command_at -- null-free, proper Option;
wenzelm
parents:
34703
diff
changeset
|
273 |
find_command(pos, middle_index + 1, array_stop) |
4f0ee5ab0380
replaced find_command_at by command_at -- null-free, proper Option;
wenzelm
parents:
34703
diff
changeset
|
274 |
else error("impossible") |
34596 | 275 |
} |
34318
c13e168a8ae6
original sources from Johannes Hölzl a48e0c6ab1aea77c52d596f7efc007a543d3d10c with minor modifications of directory layout;
wenzelm
parents:
diff
changeset
|
276 |
} |