Skip to content

Commit 5437d1d

Browse files
committed
Avoid list traversal to get indices
1 parent 8d26e68 commit 5437d1d

File tree

1 file changed

+5
-6
lines changed

1 file changed

+5
-6
lines changed

core/src/main/scala/diffson/lcs/Patience.scala

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,6 @@ import cats.Eq
2020
import cats.implicits._
2121

2222
import scala.annotation.tailrec
23-
import scala.collection.SortedMap
2423
import scala.collection.immutable.TreeMap
2524
import scala.collection.compat._
2625

@@ -43,17 +42,17 @@ class Patience[T: Eq](withFallback: Boolean = true) extends Lcs[T] {
4342
/** Returns occurrences that appear only once in the list, associated with their index */
4443
private def uniques(l: List[T]): Map[T, Int] = {
4544
@tailrec
46-
def loop(l: List[Occurrence], acc: Map[T, Int]): Map[T, Int] = l match {
47-
case (value, idx) :: tl =>
45+
def loop(l: List[T], idx: Int, acc: Map[T, Int]): Map[T, Int] = l match {
46+
case value :: tl =>
4847
if (acc.contains(value))
4948
// not unique, remove it from the accumulator and go further
50-
loop(tl, acc - value)
49+
loop(tl, idx + 1, acc - value)
5150
else
52-
loop(tl, acc + (value -> idx))
51+
loop(tl, idx + 1, acc.updated(value, idx))
5352
case Nil =>
5453
acc
5554
}
56-
loop(l.zipWithIndex, Map.empty)
55+
loop(l, 0, Map.empty)
5756
}
5857

5958
/** Takes all occurences from the first sequence and order them as in the second sequence if it is present */

0 commit comments

Comments
 (0)