• 设为首页
  • 点击收藏
  • 手机版
    手机扫一扫访问
    迪恩网络手机版
  • 关注官方公众号
    微信扫一扫关注
    公众号

Scala tailrec类代码示例

原作者: [db:作者] 来自: [db:来源] 收藏 邀请

本文整理汇总了Scala中scala.annotation.tailrec的典型用法代码示例。如果您正苦于以下问题:Scala tailrec类的具体用法?Scala tailrec怎么用?Scala tailrec使用的例子?那么恭喜您, 这里精选的类代码示例或许可以为您提供帮助。



在下文中一共展示了tailrec类的20个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于我们的系统推荐出更棒的Scala代码示例。

示例1: trainDecay

//设置package包名称以及导入依赖的类
package com.esri

import breeze.linalg.Vector
import org.apache.commons.math3.util.FastMath

import scala.annotation.tailrec
import scala.math._


  def trainDecay(trainVec: Seq[Vector[Double]],
                 epochMax: Int,
                 alphaDecay: Decay,
                 radiusDecay: Decay
                )(implicit pb: ProgressBar = NoopProgressBar()): Unit = {

    val trainLen = trainVec.length
    val rnd = new java.security.SecureRandom()

    @tailrec
    def _train(epoch: Int): Unit = {
      if (epoch < epochMax) {
        val alpha = alphaDecay(epoch)
        val rad = radiusDecay(epoch)
        val vec = trainVec(rnd.nextInt(trainLen))
        train(vec, alpha, rad)
        pb.progress()
        _train(epoch + 1)
      }
    }

    _train(0)

    pb.finish()
  }
} 
开发者ID:mraad,项目名称:spark-som-path,代码行数:36,代码来源:SOM.scala


示例2: Server

//设置package包名称以及导入依赖的类
package sh.webserver

import java.net.ServerSocket
import java.util.concurrent.{ExecutorService, Executors}

import sh.webserver.request.Request

import scala.annotation.tailrec

class Server(port: Int) {
  def start() {
    val server = new ServerSocket(port)
    val pool = Executors.newFixedThreadPool(8)
    listen(server, pool)
  }

  @tailrec
  private def listen(server : ServerSocket,pool : ExecutorService) {
    val socket = server.accept()
    pool.execute(new RequestHandler(socket))
    listen(server, pool)
  }
} 
开发者ID:stefan-hering,项目名称:scalaserver,代码行数:24,代码来源:Server.scala


示例3: ProblemFiftyEight

//设置package包名称以及导入依赖的类
package org.nason.euler.fifty

import scala.annotation.tailrec

import org.nason.euler.EulerUtils
import org.nason.euler.NumberUtils

object ProblemFiftyEight {
  def main(args: Array[String])
  {
    EulerUtils timing { println( solution ) }
  }
  
  def solution =
  {
    def ne( n:Long ) = n*n - 3*n + 3
    def sw( n:Long ) = n*n - n + 1
    def nw( n:Long ) = n*n - 2*n + 2
    def allThree( n:Long ) = List( ne(n), sw(n), nw(n) )
    def PrimeFrac = 0.1
    @tailrec def count( i:Long, nDiag:Long, nPrime:Long ) : Long =
    {
      if ( i>3 && nPrime.toDouble/nDiag.toDouble < PrimeFrac )
        i-2
      else
      {
        val nDiag2 = nDiag + 4
        val nPrime2 = nPrime + allThree(i).filter(NumberUtils.fastPrimeLong).length
        count( i+2, nDiag2, nPrime2 )
      }
    }
    count( 3, 1, 0 )
  }
} 
开发者ID:drkeoni,项目名称:euler-jms-scala,代码行数:35,代码来源:ProblemFiftyEight.scala


示例4: TorrentPiece

//设置package包名称以及导入依赖的类
package com.karasiq.bittorrent.format

import akka.util.ByteString

import scala.annotation.tailrec
import scala.collection.mutable.ArrayBuffer

case class TorrentPiece(index: Int, size: Int, sha1: ByteString, file: TorrentFile)
case class TorrentPieceBlock(piece: TorrentPiece, offset: Int, size: Int)

object TorrentPiece {
  def pieces(files: TorrentFiles): IndexedSeq[TorrentPiece] = {
    // Total torrent size
    val totalSize = files.files.map(_.size).sum

    @tailrec
    def pieceSequenceRec(buffer: ArrayBuffer[TorrentPiece], offset: Long, fileOffset: Long, pieceIndex: Int, fileSeq: Seq[TorrentFile]): IndexedSeq[TorrentPiece] = fileSeq match {
      case Seq(currentFile, fs @ _*) if fs.nonEmpty && fileOffset >= currentFile.size ?
        pieceSequenceRec(buffer, offset, 0L, pieceIndex, fs)

      case fs @ Seq(currentFile, _*) if offset < totalSize ?
        val length = Array(files.pieceLength.toLong, totalSize - offset).min
        require(length <= Int.MaxValue)
        val sha1 = files.pieces.slice(pieceIndex * 20, (pieceIndex * 20) + 20)
        val piece = TorrentPiece(buffer.length, length.toInt, sha1, currentFile)
        pieceSequenceRec(buffer :+ piece, offset + length, fileOffset + length, pieceIndex + 1, fs)

      case other ?
        buffer.result()
    }
    pieceSequenceRec(new ArrayBuffer[TorrentPiece](files.pieces.length / 20), 0L, 0L, 0, files.files)
  }

  def blocks(piece: TorrentPiece, sizeLimit: Int): IndexedSeq[TorrentPieceBlock] = {
    @tailrec
    def pieceBlockRec(buffer: ArrayBuffer[TorrentPieceBlock], offset: Int): IndexedSeq[TorrentPieceBlock] = {
      if (offset >= piece.size) {
        buffer.result()
      } else {
        val block = TorrentPieceBlock(piece, offset, Array(sizeLimit, piece.size - offset).min)
        pieceBlockRec(buffer :+ block, offset + block.size)
      }
    }
    pieceBlockRec(new ArrayBuffer[TorrentPieceBlock](piece.size / sizeLimit + 1), 0)
  }
} 
开发者ID:Karasiq,项目名称:torrentstream,代码行数:47,代码来源:TorrentPiece.scala


示例5: Collections

//设置package包名称以及导入依赖的类
package akka.util

import scala.collection.immutable
import scala.annotation.tailrec


private[akka] object Collections {

  case object EmptyImmutableSeq extends immutable.Seq[Nothing] {
    override final def iterator = Iterator.empty
    override final def apply(idx: Int): Nothing = throw new java.lang.IndexOutOfBoundsException(idx.toString)
    override final def length: Int = 0
  }

  abstract class PartialImmutableValuesIterable[From, To] extends immutable.Iterable[To] {
    def isDefinedAt(from: From): Boolean
    def apply(from: From): To
    def valuesIterator: Iterator[From]
    final def iterator: Iterator[To] = {
      val superIterator = valuesIterator
      new Iterator[To] {
        private[this] var _next: To = _
        private[this] var _hasNext = false

        @tailrec override final def hasNext: Boolean =
          if (!_hasNext && superIterator.hasNext) { // If we need and are able to look for the next value
            val potentiallyNext = superIterator.next()
            if (isDefinedAt(potentiallyNext)) {
              _next = apply(potentiallyNext)
              _hasNext = true
              true
            } else hasNext //Attempt to find the next
          } else _hasNext // Return if we found one

        override final def next(): To =
          if (hasNext) {
            val ret = _next
            _next = null.asInstanceOf[To] // Mark as consumed (nice to the GC, don't leak the last returned value)
            _hasNext = false // Mark as consumed (we need to look for the next value)
            ret
          } else throw new java.util.NoSuchElementException("next")
      }
    }

    override lazy val size: Int = iterator.size
    override def foreach[C](f: To ? C) = iterator foreach f
  }

} 
开发者ID:love1314sea,项目名称:akka-2.3.16,代码行数:50,代码来源:Collections.scala


示例6: apply

//设置package包名称以及导入依赖的类
package com.oradian.autofuture

import scala.annotation.tailrec

trait AutoFuture {
  def apply(source: String): AutoFuture.Result
}

object AutoFuture {
  val tasks = List[AutoFuture](
    ProcedureSyntaxConversion
  , AdaptTupleArgumentsExplicitly
  )

  sealed trait Result
  object Result {
    case class Error(error: String) extends Result
    case class Success(body: String) extends Result
    case object Noop extends Result
  }

  def apply(source: String): Result =
    process(source, tasks, Result.Noop)

  @tailrec
  private[autofuture] def process(source: String, tasks: List[AutoFuture], last: Result): Result = {
    tasks match {
      case Nil =>
        last

      case head :: tail =>
        head(source) match {
          case error: Result.Error =>
            error

          case newLast @ Result.Success(source) =>
            process(source, tail, newLast)

          case Result.Noop =>
            process(source, tail, last)
        }
    }
  }
} 
开发者ID:oradian,项目名称:sbt-auto-future,代码行数:45,代码来源:AutoFuture.scala


示例7: Newton

//设置package包名称以及导入依赖的类
package cz.letal.progfun.toying

import scala.annotation.tailrec

object Newton extends App {

  def sqrt(value: Double, precision: Double = 1E-6): Double = {

    def isPreciseEnough(x: Double): Boolean = Math.abs((x * x - value) / value) < precision

    def newtonStep(x: Double): Double = {
      val functionValue = x * x - value
      val gradient = 2 * x

      x - functionValue / gradient
    }

    @tailrec
    def sqrtIter(xPrev: Double): Double = {
      val xNext = newtonStep(xPrev)

      if (isPreciseEnough(xNext))
        xNext
      else
        sqrtIter(xNext)
    }

    sqrtIter(value)
  }

  println(s"${Math.sqrt(2)} - Math.sqrt")
  println("----------------------------------")
  for (a <- 1 to 15) {
    println(s"${sqrt(2, Math.pow(10, -a))} - precision $a significant digits")
  }
} 
开发者ID:letalvoj,项目名称:progfun_assignments,代码行数:37,代码来源:Newton.scala


示例8: RemoteMultiMap

//设置package包名称以及导入依赖的类
package com.init6.channels.utils

import scala.annotation.tailrec
import scala.collection.mutable


object RemoteMultiMap {
  def apply[A, B]() = new RemoteMultiMap[A, B]
}

sealed class RemoteMultiMap[A, B] extends mutable.HashMap[A, mutable.Set[B]] with mutable.MultiMap[A, B] {

  def +=(kv: (A, B)): this.type = addBinding(kv._1, kv._2)
  def +=(key: A): this.type = {
    if (!contains(key)) {
      +=(key -> mutable.Set[B]())
    } else {
      this
    }
  }

  def ++=(kv: (A, Iterable[B])): this.type = {
    @tailrec
    def loop(kv: (A, Iterable[B])): Unit = {
      if (nonEmpty) {
        +=(kv._1 -> kv._2.head)
        loop(kv._1 -> kv._2.tail)
      }
    }
    loop(kv)
    this
  }

  def -=(kv: (A, B)): this.type = {
    get(kv._1).foreach(_ -= kv._2)
    this
  }
} 
开发者ID:fjaros,项目名称:init6,代码行数:39,代码来源:RemoteMultiMap.scala


示例9: Collections

//设置package包名称以及导入依赖的类
package akka.util

import scala.collection.immutable
import scala.annotation.tailrec


private[akka] object Collections {

  case object EmptyImmutableSeq extends immutable.Seq[Nothing] {
    override final def iterator = Iterator.empty
    override final def apply(idx: Int): Nothing = throw new java.lang.IndexOutOfBoundsException(idx.toString)
    override final def length: Int = 0
  }

  abstract class PartialImmutableValuesIterable[From, To] extends immutable.Iterable[To] {
    def isDefinedAt(from: From): Boolean
    def apply(from: From): To
    def valuesIterator: Iterator[From]
    final def iterator: Iterator[To] = {
      val superIterator = valuesIterator
      new Iterator[To] {
        private[this] var _next: To = _
        private[this] var _hasNext = false

        @tailrec override final def hasNext: Boolean =
          if (!_hasNext && superIterator.hasNext) { // If we need and are able to look for the next value
          val potentiallyNext = superIterator.next()
            if (isDefinedAt(potentiallyNext)) {
              _next = apply(potentiallyNext)
              _hasNext = true
              true
            } else hasNext //Attempt to find the next
          } else _hasNext // Return if we found one

        override final def next(): To =
          if (hasNext) {
            val ret = _next
            _next = null.asInstanceOf[To] // Mark as consumed (nice to the GC, don't leak the last returned value)
            _hasNext = false // Mark as consumed (we need to look for the next value)
            ret
          } else throw new java.util.NoSuchElementException("next")
      }
    }

    override lazy val size: Int = iterator.size
    override def foreach[C](f: To ? C) = iterator foreach f
  }

} 
开发者ID:Starofall,项目名称:Chakka,代码行数:50,代码来源:Collections.scala


示例10: escape

//设置package包名称以及导入依赖的类
package shared

import scala.annotation.tailrec



  def escape(msg:String):String = {
    @tailrec
    def go(msg:List[Char], acc:String):String = msg match {
      case '&' :: xs => go(xs, acc + "&amp;")
      case '>' :: xs => go(xs, acc + "&gt;")
      case '<' :: xs => go(xs, acc + "&lt;")
      case   x :: xs => go(xs, acc + x)
      case       Nil => acc
    }
    go(msg.toList, "")
  }
} 
开发者ID:Starofall,项目名称:Chakka,代码行数:19,代码来源:Utils.scala


示例11: ScalaDeque

//设置package包名称以及导入依赖的类
package com.naoh.beef.internal

import java.util

import scala.annotation.tailrec

class ScalaDeque[T] private(val inner: util.ArrayDeque[T]) extends AnyVal {
  @inline
  def isEmpty: Boolean = inner.isEmpty

  @inline
  def add(a: T): Boolean = inner.add(a)

  @inline
  def <<(a: T): Boolean = inner.add(a)

  @inline
  def polls[U](f: (T) => U): Unit = pollTraverse(f)

  @inline
  def size: Int = inner.size()

  @inline
  def pollOr[U](f: T => U)(or: => Unit): Unit = {
    poll.map(f).getOrElse(or)
  }

  @inline
  def poll: Option[T] = Option(inner.poll())

  @tailrec
  private def pollTraverse[U](f: T => U): Unit = {
    poll match {
      case Some(x) =>
        f(x)
        pollTraverse(f)
      case _ =>
    }
  }
}

object ScalaDeque {
  def empty[T] = new ScalaDeque[T](new util.ArrayDeque[T])
} 
开发者ID:naoh87,项目名称:grpc-scala-experiment,代码行数:45,代码来源:ScalaDeque.scala


示例12: ByteStringToDeltaStage

//设置package包名称以及导入依赖的类
package com.martinseeler.dtf.stages

import akka.stream.stage.{GraphStage, GraphStageLogic, InHandler, OutHandler}
import akka.stream.{Attributes, FlowShape, Inlet, Outlet}
import akka.util.ByteString
import com.martinseeler.dtf.{FactorizedDeltaTick, NonNegativeFactorizedDeltaTick}
import scodec.Attempt.{Failure, Successful}
import scodec.DecodeResult
import scodec.bits.BitVector

import scala.annotation.tailrec

class ByteStringToDeltaStage extends GraphStage[FlowShape[ByteString, FactorizedDeltaTick]] {

  val in = Inlet[ByteString]("ByteStringToDeltaStage.in")
  val out = Outlet[FactorizedDeltaTick]("ByteStringToDeltaStage.out")

  def shape: FlowShape[ByteString, FactorizedDeltaTick] = FlowShape(in, out)

  def createLogic(inheritedAttributes: Attributes): GraphStageLogic =
    new GraphStageLogic(shape) with OutHandler {

      def onPull(): Unit = if (!hasBeenPulled(in)) tryPull(in)
      setHandler(out, this)

      val inHandler = new InHandler {

        def decodeAllFromBits(bits: BitVector): (Vector[NonNegativeFactorizedDeltaTick], BitVector) = {

          @tailrec
          def compute(results: Vector[NonNegativeFactorizedDeltaTick], remainingBits: BitVector): (Vector[NonNegativeFactorizedDeltaTick], BitVector) = {
            NonNegativeFactorizedDeltaTick.nonNegFactorizedDeltaTickCodecV.decode(remainingBits) match {
              case Successful(DecodeResult(value, BitVector.empty)) =>
                (results :+ value, BitVector.empty)
              case Successful(DecodeResult(value, remainder)) if remainder.sizeGreaterThan(25) =>
                compute(results :+ value, remainder)
              case Successful(DecodeResult(value, remainder)) =>
                (results :+ value, remainder)
              case Failure(e) =>
                println("e = " + e)
                (results, BitVector.empty)
            }
          }

          compute(Vector.empty, bits)
        }

        private[this] var remainingBits = BitVector.empty

        def onPush(): Unit = {
          val bits = BitVector.view(grab(in).asByteBuffer)
          val (results, rest) = decodeAllFromBits(remainingBits ++ bits)
          emitMultiple(out, results.map(_.withNegatives))
          remainingBits = rest
        }
      }

      setHandler(in, inHandler)
    }

} 
开发者ID:MartinSeeler,项目名称:dense-tick-file-format,代码行数:62,代码来源:ByteStringToDeltaStage.scala


示例13: Mult

//设置package包名称以及导入依赖的类
package newts

import cats.kernel.Eq
import cats.syntax.functor._
import cats.{Applicative, Eval, Monad, Monoid, Traverse}

import scala.annotation.tailrec

final case class Mult[A](getMult: A) extends AnyVal

object Mult extends MultInstances0 {
  implicit def newtypeInstance[A]: Newtype.Aux[Mult[A], A] = Newtype.from[Mult[A], A](Mult.apply)(_.getMult)

  implicit val monadInstance: Monad[Mult] = new Monad[Mult] {
    def pure[A](x: A): Mult[A] = Mult(x)
    def flatMap[A, B](fa: Mult[A])(f: A => Mult[B]): Mult[B] = f(fa.getMult)
    @tailrec
    def tailRecM[A, B](a: A)(f: A => Mult[Either[A, B]]): Mult[B] = f(a) match {
      case Mult(Left(a1)) => tailRecM(a1)(f)
      case Mult(Right(b)) => Mult(b)
    }
  }

  implicit def monoidInstance[A](implicit num: Numeric[A]): Monoid[Mult[A]] = new Monoid[Mult[A]] {
    val empty: Mult[A] = Mult(num.one)
    def combine(x: Mult[A], y: Mult[A]): Mult[A] = Mult(num.times(x.getMult, y.getMult))
  }

  implicit def eqInstance[A: Eq]: Eq[Mult[A]] = Eq.by(_.getMult)
}

trait MultInstances0 {
  implicit val traverseInstance: Traverse[Mult] = new Traverse[Mult] {
    def traverse[G[_], A, B](fa: Mult[A])(f: A => G[B])(implicit ev: Applicative[G]): G[Mult[B]] =
      f(fa.getMult).map(Mult(_))

    def foldLeft[A, B](fa: Mult[A], b: B)(f: (B, A) => B): B =
      f(b, fa.getMult)

    def foldRight[A, B](fa: Mult[A], lb: Eval[B])(f: (A, Eval[B]) => Eval[B]): Eval[B] =
      f(fa.getMult, lb)
  }
} 
开发者ID:julien-truffaut,项目名称:newts,代码行数:44,代码来源:Mult.scala


示例14: Max

//设置package包名称以及导入依赖的类
package newts

import cats.kernel.Order
import cats.syntax.functor._
import cats.syntax.order._
import cats.{Applicative, Eval, Monad, Monoid, Semigroup, Show, Traverse}
import newts.internal.MinBounded

import scala.annotation.tailrec

final case class Max[A](getMax: A) extends AnyVal

object Max extends MaxInstances0{
  implicit def newtypeInstance[A]: Newtype.Aux[Max[A], A] = Newtype.from[Max[A], A](Max.apply)(_.getMax)

  implicit val monadInstance: Monad[Max] = new Monad[Max] {
    def pure[A](x: A): Max[A] = Max(x)
    def flatMap[A, B](fa: Max[A])(f: A => Max[B]): Max[B] = f(fa.getMax)
    @tailrec
    def tailRecM[A, B](a: A)(f: A => Max[Either[A, B]]): Max[B] = f(a) match {
      case Max(Left(a1)) => tailRecM(a1)(f)
      case Max(Right(b)) => Max(b)
    }
  }
  
  implicit def instances[A: Order]: Order[Max[A]] with Semigroup[Max[A]] = new Order[Max[A]] with Semigroup[Max[A]] {
    def combine(x: Max[A], y: Max[A]): Max[A] = Max(x.getMax max y.getMax)
    def compare(x: Max[A], y: Max[A]): Int = x.getMax compare y.getMax
  }

  implicit def showInstance[A](implicit ev: Show[A]): Show[Max[A]] = new Show[Max[A]] {
    override def show(f: Max[A]): String = s"Max(${ev.show(f.getMax)})"
  }
}

trait MaxInstances0{
  implicit def maxMonoid[A](implicit A: MinBounded[A]): Monoid[Max[A]] = new Monoid[Max[A]]{
    def empty: Max[A] = Max(A.minValue)
    def combine(x: Max[A], y: Max[A]): Max[A] = Max(x.getMax max y.getMax)
  }

  implicit val traverseInstance: Traverse[Max] = new Traverse[Max] {
    def traverse[G[_], A, B](fa: Max[A])(f: A => G[B])(implicit ev: Applicative[G]): G[Max[B]] =
      f(fa.getMax).map(Max(_))

    def foldLeft[A, B](fa: Max[A], b: B)(f: (B, A) => B): B =
      f(b, fa.getMax)

    def foldRight[A, B](fa: Max[A], lb: Eval[B])(f: (A, Eval[B]) => Eval[B]): Eval[B] =
      f(fa.getMax, lb)
  }
} 
开发者ID:julien-truffaut,项目名称:newts,代码行数:53,代码来源:Max.scala


示例15: FirstOption

//设置package包名称以及导入依赖的类
package newts

import cats.{Applicative, Eval, MonadCombine, Monoid, MonoidK, Show, Traverse}
import cats.instances.option._
import cats.syntax.functor._
import cats.syntax.traverse._
import cats.kernel.Eq

import scala.annotation.tailrec

final case class FirstOption[A](getFirstOption: Option[A]) extends AnyVal

object FirstOption extends FirstOptionInstances0 {
  implicit def newtypeInstance[A]: Newtype.Aux[FirstOption[A], Option[A]] =
    Newtype.from[FirstOption[A], Option[A]](FirstOption.apply)(_.getFirstOption)

  implicit val monadCombineInstance: MonadCombine[FirstOption] = new MonadCombine[FirstOption] {
    def empty[A]: FirstOption[A] = FirstOption(None)
    def combineK[A](x: FirstOption[A], y: FirstOption[A]): FirstOption[A] = FirstOption(x.getFirstOption.orElse(y.getFirstOption))
    def pure[A](x: A): FirstOption[A] = FirstOption(Some(x))
    def flatMap[A, B](fa: FirstOption[A])(f: A => FirstOption[B]): FirstOption[B] =
      fa.getFirstOption.fold(empty[B])(f)

    @tailrec
    def tailRecM[A, B](a: A)(f: A => FirstOption[Either[A, B]]): FirstOption[B] =
      f(a).getFirstOption match {
        case None           => empty
        case Some(Left(a1)) => tailRecM(a1)(f)
        case Some(Right(b)) => pure(b)
      }
  }

  implicit def monoidInstance[A]: Monoid[FirstOption[A]] = MonoidK[FirstOption].algebra

  implicit def eqInstance[A: Eq]: Eq[FirstOption[A]] = Eq.by(_.getFirstOption)

  implicit def showInstance[A: Show]: Show[FirstOption[A]] = new Show[FirstOption[A]] {
    override def show(f: FirstOption[A]): String = s"FirstOption(${Show[Option[A]].show(f.getFirstOption)})"
  }
}

trait FirstOptionInstances0 {
  implicit val traverseInstance: Traverse[FirstOption] = new Traverse[FirstOption] {
    def traverse[G[_], A, B](fa: FirstOption[A])(f: A => G[B])(implicit ev: Applicative[G]): G[FirstOption[B]] =
      fa.getFirstOption.traverse(f).map(FirstOption(_))

    def foldLeft[A, B](fa: FirstOption[A], b: B)(f: (B, A) => B): B =
      fa.getFirstOption.fold(b)(f(b, _))

    def foldRight[A, B](fa: FirstOption[A], lb: Eval[B])(f: (A, Eval[B]) => Eval[B]): Eval[B] =
      fa.getFirstOption.fold(lb)(f(_, lb))
  }
} 
开发者ID:julien-truffaut,项目名称:newts,代码行数:54,代码来源:FirstOption.scala


示例16: PeerId

//设置package包名称以及导入依赖的类
package com.spooky.bittorrent.model

import com.spooky.bittorrent.metainfo.Torrent
import java.nio.ByteBuffer
import java.nio.file.Path
import java.util.BitSet
import com.spooky.bittorrent.Binary
import java.nio.charset.Charset
import scala.annotation.tailrec
import com.spooky.bittorrent.Checksum
import com.spooky.bittorrent.InfoHash

case class PeerId(id: String)
object PeerId {
  val UTF8 = Charset.forName("UTF8")
  def parse(buffer: ByteBuffer): PeerId = {
    val buff = Array.ofDim[Byte](20)
    buffer.get(buff)
    //    val charset = Charset.forName("ASCII")
    PeerId(new String(buff, UTF8).intern())
  }
  def create = PeerId("SPOOKY6-c2b4f6c4h4d9")
}
case class TorrentFileState(have: BitSet) {
  //This is bugged since last piece is generaly not fully utalized
  def getDownloaded(torrent: Torrent): Long = {
      @tailrec
      def rec(bitset: BitSet, index: Int, accumulated: Long, length: Long): Long = {
        if (bitset.size == index) {
          accumulated
        } else rec(bitset, index + 1, if (bitset.get(index)) accumulated + length else accumulated, length)
      }
    rec(have, 0, 0l, torrent.info.pieceLength)
  }
  override def toString: String = {
    "|" + Binary.toBinary(have) + "|"
  }
}
case class TorrentSetup(torrent: Torrent, root: Path)
case class EnrichedTorrentSetup(torrent: Torrent, root: Path, state: TorrentFileState)
case class TorrentStatistics(infoHash: InfoHash, uploaded: Long, downloaded: Long, left: Long, corrupt: Long)
case class TorrentConfiguration(port: Short, numwant: Int)
abstract class AbstractPeer(ip: String, port: Short)
case class Peer(ip: String, port: Short) extends AbstractPeer(ip, port)

//rename to something maybe **Context
case class TorrentRef(info: InfoHash, peerId: PeerId)
object TorrentRef {
  def apply(torrent: Torrent, peerId: PeerId): TorrentRef = TorrentRef(torrent.infoHash, peerId)
} 
开发者ID:zpooky,项目名称:bittorrent,代码行数:51,代码来源:model.scala


示例17: Checksum

//设置package包名称以及导入依赖的类
package com.spooky.bittorrent

import scala.annotation.tailrec
import java.security.MessageDigest
import java.util.Arrays
import org.apache.commons.codec.binary.Hex
import java.nio.ByteBuffer

sealed case class Checksum(sum: Array[Byte], algorithm: Algorithm) {
  def check(other: Array[Byte]): Boolean = {
    check(other.length, other)
  }
  def check(length: Int, other: Array[Byte]*): Boolean = {
      @tailrec
      def rec(length: Int, other: Seq[Array[Byte]], index: Int, digest: MessageDigest): Array[Byte] = {
        if (other.length - 1 == index) {
          digest.update(other(index), 0, length)
          digest.digest
        } else {
          digest.update(other(index))
          rec(length, other, index + 1, digest)
        }
      }
    val digester = MessageDigest.getInstance(algorithm.toString)
    MessageDigest.isEqual(sum, rec(length, other, 0, digester))
  }
  def compare(other: Array[Byte]): Boolean = MessageDigest.isEqual(sum, other)
  override def toString: String = Hex.encodeHexString(sum)
  override def hashCode: Int = Arrays.hashCode(sum)
  override def equals(o: Any): Boolean = o match {
    case Checksum(otherHash, Sha1) => MessageDigest.isEqual(otherHash, sum)
    case _                         => false
  }
}
object Checksum {
  def parse(raw: ByteBuffer, algorithm: Algorithm): Checksum = {
    val checksum = Array.ofDim[Byte](algorithm.bytes)
    for (n <- 0 until algorithm.bytes) {
      checksum(n) = raw.get
    }
    Checksum(checksum, algorithm)
  }
} 
开发者ID:zpooky,项目名称:bittorrent,代码行数:44,代码来源:Checksum.scala


示例18: Scale

//设置package包名称以及导入依赖的类
package com.spooky.bittorrent.u

import scala.annotation.tailrec

sealed abstract class Scale extends Ordered[Scale] {
  def apply(capacity: Double): Size = Size(capacity, this)
  def bytes: Int
  def convert(size: Size): Size = size match {
    case o if o.scale == this => o
    case o => {
      val v = o.scale.bytes.asInstanceOf[Double] / bytes.asInstanceOf[Double]
      Size(v * size.capacity, this)
    }
  }
  override def compare(o: Scale): Int = bytes.compareTo(o.bytes)
  override def toString: String = getClass.getSimpleName.replace("$", "")
}
sealed case class Size(capacity: Double, scale: Scale) extends Ordered[Size] {
  def toBytes: Size = Byte.convert(this)
  def toKiloByte: Size = KiloByte.convert(this)
  def toMegaByte: Size = MegaByte.convert(this)
  def toGigaByte: Size = GigaByte.convert(this)

  def percentageFor(other: Size): Double = {
    val i = toBytes.capacity / 100
    other.toBytes.capacity / i
  }
  @tailrec
  override def compare(o: Size): Int = scale.compare(o.scale) match {
    case 0 => capacity.compare(o.capacity)
    case 1 => o.scale.convert(this).compare(o)
    case -1 => compare(scale.convert(o))
  }
  override def equals(o: Any): Boolean = o match {
    case e: Size => e.capacity == capacity && e.scale == scale
    case _       => false
  }

}
object Byte extends Scale {
  override val bytes = 1
}
object KiloByte extends Scale {
  override val bytes = 1024 * Byte.bytes
}
object MegaByte extends Scale {
  override val bytes = 1024 * KiloByte.bytes
}
object GigaByte extends Scale {
  override val bytes = 1024 * MegaByte.bytes
} 
开发者ID:zpooky,项目名称:bittorrent,代码行数:52,代码来源:Size.scala


示例19: Deck

//设置package包名称以及导入依赖的类
package hanabi

import scala.util.Random
import scala.annotation.tailrec

object Deck {
  def shuffle(allCards: Seq[Card]) = Deck(allCards).shuffle
}

case class Deck(cards: Seq[Card]) {
  def empty = cards.isEmpty
  def draw: (Option[Card], Deck) = (cards.headOption, Deck(cards.drop(1)))

  def drop(cs: Seq[Card]) = Deck(cards diff cs)

  def deal(hands: Int, cardsPerHand: Int): (Seq[Hand], Deck) = {

    @tailrec
    def dealRec(currentHands: Seq[Hand] = Seq.fill(hands)(Hand()), currentDeck: Deck = this, nbCards: Int = hands * cardsPerHand): (Seq[Hand], Deck) =
      if (nbCards == 0)
        (currentHands, currentDeck)
      else {
        val (card, deck) = currentDeck.draw
        val hand = currentHands.head + card
        dealRec(currentHands.tail :+ hand, deck, nbCards - 1)
      }

    dealRec()

  }

  def shuffle = Deck(Random.shuffle(cards))
} 
开发者ID:wl-seclin-hashcode,项目名称:hanabi,代码行数:34,代码来源:Deck.scala


示例20: IteratorSource

//设置package包名称以及导入依赖的类
package knot.core.sources

import knot.core.Source

import scala.annotation.tailrec
import scala.collection.immutable

case class IteratorSource[O](iterable: immutable.Iterable[O]) extends Source[O] {
  require(iterable ne null, "iterable may not be null")

  private[this] val it = iterable.iterator

  override def onRequest(): Unit = {
    @tailrec
    def go(total: Long, n: Int): Long = {
      var c = 0
      while (it.hasNext && c < n) {
        val e = it.next()
        if (e != null) push(e)
        c += 1
      }
      val r = total + c
      if (!it.hasNext) {
        context.complete()
        r
      } else if (demandOrMax > 0)
        go(r, demandOrMax)
      else
        r
    }

    log.debug(s"iterate by request...")
    val total = go(0, demandOrMax)
    log.debug(s"iterate by request. count=$total ")
  }

  private def demandOrMax: Int = Math.min(context.demand, Int.MaxValue).toInt
} 
开发者ID:defvar,项目名称:knot,代码行数:39,代码来源:IteratorSource.scala



注:本文中的scala.annotation.tailrec类示例整理自Github/MSDocs等源码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。


鲜花

握手

雷人

路过

鸡蛋
该文章已有0人参与评论

请发表评论

全部评论

专题导读
上一篇:
Scala Duration类代码示例发布时间:2022-05-23
下一篇:
Scala SparkConf类代码示例发布时间:2022-05-23
热门推荐
热门话题
阅读排行榜

扫描微信二维码

查看手机版网站

随时了解更新最新资讯

139-2527-9053

在线客服(服务时间 9:00~18:00)

在线QQ客服
地址:深圳市南山区西丽大学城创智工业园
电邮:jeky_zhao#qq.com
移动电话:139-2527-9053

Powered by 互联科技 X3.4© 2001-2213 极客世界.|Sitemap