From df404e51a41020e9385020f6ee123ff07fd4badc Mon Sep 17 00:00:00 2001 From: Martin Odersky Date: Tue, 10 Feb 2015 12:32:22 +0100 Subject: Make bestFit work for partially filled arrays --- src/dotty/tools/dotc/core/pickling/TreeBuffer.scala | 2 +- src/dotty/tools/dotc/util/SourceFile.scala | 2 +- src/dotty/tools/dotc/util/Util.scala | 12 +++++------- 3 files changed, 7 insertions(+), 9 deletions(-) (limited to 'src/dotty') 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 */ -- cgit v1.2.3