interleave clock states

pull/2701/head
Thibault Duplessis 2017-03-25 21:18:31 +01:00
parent c2090b8df0
commit 214d07b462
2 changed files with 26 additions and 15 deletions

View File

@ -0,0 +1,18 @@
package lila.common
import scala.annotation.tailrec
object Sequence {
@tailrec
final def interleave[A](base: Vector[A], a: List[A], b: List[A]): Vector[A] = a match {
case elt :: aTail => interleave(base :+ elt, b, aTail)
case _ => base ++ b
}
@tailrec
final def interleave[A](base: Vector[A], a: Vector[A], b: Vector[A]): Vector[A] = a match {
case elt +: aTail => interleave(base :+ elt, b, aTail)
case _ => base ++ b
}
}

View File

@ -1,7 +1,5 @@
package lila.game
import scala.annotation.tailrec
import scala.collection.breakOut
import scala.concurrent.duration._
import chess.Color.{ White, Black }
@ -11,7 +9,7 @@ import chess.variant.{ Variant, Crazyhouse }
import chess.{ History => ChessHistory, CheckCount, Castles, Board, MoveOrDrop, Pos, Game => ChessGame, Clock, Status, Color, Mode, PositionHash, UnmovedRooks }
import org.joda.time.DateTime
import lila.common.Centis
import lila.common.{ Centis, Sequence }
import lila.db.ByteArray
import lila.rating.PerfType
import lila.user.User
@ -126,17 +124,11 @@ case class Game(
mts.grouped(2).flatMap(_.headOption).toList
}
@tailrec
final private def interleave[A](base: Vector[A], a: List[A], b: List[A]): Vector[A] = a match {
case elt :: aTail => interleave(base :+ elt, b, aTail)
case _ => base ++ b
}
def moveTimes: Option[Vector[Centis]] = {
for {
a <- moveTimes(startColor)
b <- moveTimes(!startColor)
} yield interleave(Vector.empty, a, b)
} yield Sequence.interleave(Vector.empty, a, b)
}
def bothClockStates = clockHistory.map(_ bothClockStates startColor)
@ -734,9 +726,10 @@ case class ClockHistory(
if (color.white) white else black
// first state is of the color that moved first.
// #TODO naive implementation; optimize me maybe?
def bothClockStates(firstMoveBy: Color): List[Centis] =
firstMoveBy.fold(white zip black, black zip white).flatMap {
case (a, b) => List(a, b)
}(breakOut)
def bothClockStates(firstMoveBy: Color): Vector[Centis] =
Sequence.interleave(
Vector.empty,
firstMoveBy.fold(white, black),
firstMoveBy.fold(black, white)
)
}