diff options
author | Martin Odersky <odersky@gmail.com> | 2015-02-10 12:32:22 +0100 |
---|---|---|
committer | Dmitry Petrashko <dmitry.petrashko@gmail.com> | 2015-03-18 11:09:43 +0100 |
commit | df404e51a41020e9385020f6ee123ff07fd4badc (patch) | |
tree | 57a7543ab1646c0b6e52ea0250a9531ce7ce8447 /src/dotty | |
parent | 0c755d211fcfeb7d19a3fe87d5513e97a3f47da1 (diff) | |
download | dotty-df404e51a41020e9385020f6ee123ff07fd4badc.tar.gz dotty-df404e51a41020e9385020f6ee123ff07fd4badc.tar.bz2 dotty-df404e51a41020e9385020f6ee123ff07fd4badc.zip |
Make bestFit work for partially filled arrays
Diffstat (limited to 'src/dotty')
-rw-r--r-- | src/dotty/tools/dotc/core/pickling/TreeBuffer.scala | 2 | ||||
-rw-r--r-- | src/dotty/tools/dotc/util/SourceFile.scala | 2 | ||||
-rw-r--r-- | src/dotty/tools/dotc/util/Util.scala | 12 |
3 files changed, 7 insertions, 9 deletions
diff --git a/src/dotty/tools/dotc/core/pickling/TreeBuffer.scala b/src/dotty/tools/dotc/core/pickling/TreeBuffer.scala index 5a445124d..73b944b92 100644 --- a/src/dotty/tools/dotc/core/pickling/TreeBuffer.scala +++ b/src/dotty/tools/dotc/core/pickling/TreeBuffer.scala @@ -46,7 +46,7 @@ class TreeBuffer extends TastyBuffer(1000000) { } def adjusted(x: Addr): Addr = { - val idx = bestFit(offsets, x.index - 1) + val idx = bestFit(offsets, numOffsets, x.index - 1) if (idx < 0) x else x - delta(idx) } diff --git a/src/dotty/tools/dotc/util/SourceFile.scala b/src/dotty/tools/dotc/util/SourceFile.scala index c5d88d7bf..45119a881 100644 --- a/src/dotty/tools/dotc/util/SourceFile.scala +++ b/src/dotty/tools/dotc/util/SourceFile.scala @@ -99,7 +99,7 @@ case class SourceFile(file: AbstractFile, content: Array[Char]) { * Lines are numbered from 0 */ def offsetToLine(offset: Int): Int = { - lastLine = Util.bestFit(lineIndices, offset, lastLine) + lastLine = Util.bestFit(lineIndices, lineIndices.length, offset, lastLine) lastLine } diff --git a/src/dotty/tools/dotc/util/Util.scala b/src/dotty/tools/dotc/util/Util.scala index ed9a54e38..98f0b62db 100644 --- a/src/dotty/tools/dotc/util/Util.scala +++ b/src/dotty/tools/dotc/util/Util.scala @@ -11,18 +11,16 @@ object Util { * `candidates.length/2`. * @pre candidates is sorted */ - def bestFit(candidates: Array[Int], x: Int, hint: Int = -1): Int = { + def bestFit(candidates: Array[Int], length: Int, x: Int, hint: Int = -1): Int = { def recur(lo: Int, hi: Int, mid: Int): Int = if (x < candidates(mid)) recur(lo, mid - 1, (lo + mid - 1) / 2) - else if (mid + 1 < candidates.length && x >= candidates(mid + 1)) + else if (mid + 1 < length && x >= candidates(mid + 1)) recur(mid + 1, hi, (mid + 1 + hi) / 2) else mid - val initMid = - if (0 <= hint && hint < candidates.length) hint - else candidates.length / 2 - if (candidates.isEmpty || x < candidates(0)) -1 - else recur(0, candidates.length, initMid) + val initMid = if (0 <= hint && hint < length) hint else length / 2 + if (length == 0 || x < candidates(0)) -1 + else recur(0, length, initMid) } /** An array twice the size of given array, with existing elements copied over */ |