本文整理汇总了Scala中collection.mutable.ListBuffer类的典型用法代码示例。如果您正苦于以下问题:Scala ListBuffer类的具体用法?Scala ListBuffer怎么用?Scala ListBuffer使用的例子?那么恭喜您, 这里精选的类代码示例或许可以为您提供帮助。
在下文中一共展示了ListBuffer类的12个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Scala代码示例。
示例1: RecordingLogger
//设置package包名称以及导入依赖的类
import sbt._
import collection.mutable.ListBuffer
object RecordingLogger extends Logger {
private val buffer = ListBuffer[String]()
def hasErrors = buffer.nonEmpty
def readAndClear() = {
val res = buffer.mkString("\n")
buffer.clear()
res
}
def clear() {
buffer.clear()
}
def log(level: Level.Value, message: => String) =
if (level == Level.Error) {
buffer += message
}
// we don't log success here
def success(message: => String) = ()
// invoked when a task throws an exception. invoked late, when the exception is logged, i.e.
// just before returning to the prompt. therefore we do nothing: storing the exception in the
// buffer would happen *after* the `handleFailure` reads the buffer.
def trace(t: => Throwable) = ()
}
示例2: init
//设置package包名称以及导入依赖的类
def init[A](l: List[A]): List[A] =
l match {
case Nil => sys.error("init of empty list")
case Cons(_,Nil) => Nil
case Cons(h,t) => Cons(h,init(t))
}
def init2[A](l: List[A]): List[A] = {
import collection.mutable.ListBuffer
val buf = new ListBuffer[A]
@annotation.tailrec
def go(cur: List[A]): List[A] = cur match {
case Nil => sys.error("init of empty list")
case Cons(_,Nil) => List(buf.toList: _*)
case Cons(h,t) => buf += h; go(t)
}
go(l)
}
示例3: Relation
//设置package包名称以及导入依赖的类
package xyz.hyperreal.energize
import java.sql._
import collection.mutable.ListBuffer
class Relation( env: Environment, rs: ResultSet ) extends Iterable[IndexedSeq[AnyRef]] {
private val md = rs.getMetaData
private var cur: List[IndexedSeq[AnyRef]] = _
val columnCount = md.getColumnCount
val columns =
for (i <- 1 to columnCount)
yield
Col( md.getTableName(i), nameOut(md.getColumnName(i)) )
val columnMap = Map( (for (i <- 0 until columnCount) yield (columns(i), i)): _* )
val rows = {
val buf = new ListBuffer[IndexedSeq[AnyRef]]
while (rs.next)
buf += (for (i <- 1 to columnCount)
yield
rs.getObject( i ))
buf.toList
}
def iterator = rows.iterator
def next =
if (cur eq null)
if (rows == Nil)
false
else {
cur = rows
true
}
else if (cur.tail == Nil)
false
else {
cur = cur.tail
true
}
def get( index: Int ) = cur.head( index )
def get( table: String, column: String ): Option[AnyRef] = {
columnMap get Col(env.db.desensitize(table), env.db.desensitize(column)) map get
}
def getLong( table: String, column: String ) = get( table, column ).get.asInstanceOf[Long]
override def toString =
columns.toString + rows
}
case class Col( table: String, name: String )
示例4: SnowAnimation
//设置package包名称以及导入依赖的类
package gui.animations
import java.awt.Graphics2D
import java.awt.AlphaComposite
import collection.mutable.ListBuffer
import util.Random
import game_mechanics.path.{Waypoint,CellPos}
import gui._
object SnowAnimation
{
val rng = new Random()
}
class SnowAnimation( pos : CellPos, radius : Double ) extends Animatable
{
import SnowAnimation._
val size = MapPanel.cellsize.toDouble
val origin = (pos.toDouble + new Waypoint(0.5,0.5)) * size
val duration = 7.0
timer = duration
val particle_amount = 30
val particles = new ListBuffer[Waypoint]()
val fall_distance = 20
for( i <- 0 until particle_amount )
{
// Uniform disk distribution
val r = rng.nextDouble
val theta = rng.nextDouble * 2 * Math.PI
val x = Math.sqrt( r ) * Math.cos( theta ) * radius * size
val y = Math.sqrt( r ) * Math.sin( theta ) * radius * size
particles += new Waypoint( x + origin.x, y + origin.y - fall_distance )
}
override def draw(g: Graphics2D): Unit = {
g.setColor( Colors.white )
val alpha = (timer / duration).toFloat
g.setComposite(
AlphaComposite.getInstance( AlphaComposite.SRC_OVER, alpha ) )
val movement = (1.0 - timer / duration) * fall_distance
for( particle <- particles ) {
g.drawRect( particle.x.toInt, particle.y.toInt + movement.toInt,
1, 1 )
}
}
}
示例5: SmokeAnimation
//设置package包名称以及导入依赖的类
package gui.animations
import collection.mutable.ListBuffer
import util.Random
import java.awt.Graphics2D
import game_mechanics.path.Waypoint
import gui._
class SmokeAnimation(origin: Waypoint) extends Animatable
{
val duration = 2.0
val rng = new Random
timer = duration
val particles = new ListBuffer[Waypoint]
for( i <- 0 until 30 )
{
val theta = rng.nextDouble * 2 * Math.PI
particles += new Waypoint( Math.cos( theta ), Math.sin( theta ) )
}
override def draw(g: Graphics2D) : Unit = {
val interp = 1 - timer / duration
g.setColor( Colors.lightGrey )
for( particle <- particles )
{
val pos = (origin + particle * interp * 1.5) * MapPanel.cellsize
g.fillOval(
pos.x.toInt, pos.y.toInt,
(MapPanel.cellsize * (1-interp)).toInt,
(MapPanel.cellsize * (1-interp)).toInt )
}
}
}
示例6: BuffAnimation
//设置package包名称以及导入依赖的类
package gui.animations
import java.awt.Graphics2D
import java.awt.AlphaComposite
import collection.mutable.ListBuffer
import gui._
import util.Random
import game_mechanics.path.{Waypoint,CellPos}
object BuffAnimation
{
val rng = new Random()
}
class BuffAnimation( pos : CellPos, radius : Double ) extends Animatable
{
import BuffAnimation._
val size = MapPanel.cellsize.toDouble
val origin = (pos.toDouble + new Waypoint(0.5,0.5)) * size
val duration = 7.0
timer = duration
val particle_amount = 30
val particles = new ListBuffer[Waypoint]()
val fall_distance = 20
for( i <- 0 until particle_amount )
{
// Uniform disk distribution
val r = rng.nextDouble
val theta = rng.nextDouble * 2 * Math.PI
val x = Math.sqrt( r ) * Math.cos( theta ) * radius * size
val y = Math.sqrt( r ) * Math.sin( theta ) * radius * size
particles += new Waypoint( x + origin.x, y + origin.y - fall_distance )
}
override def draw(g: Graphics2D): Unit = {
g.setColor( Colors.blue )
val alpha = (timer / duration).toFloat
g.setComposite(
AlphaComposite.getInstance( AlphaComposite.SRC_OVER, alpha ) )
val movement = timer / duration * fall_distance
for( particle <- particles ) {
g.drawString( "+",
particle.x.toInt, particle.y.toInt + movement.toInt )
}
}
}
示例7: UsingFixture
//设置package包名称以及导入依赖的类
import org.scalatest.FlatSpec
import collection.mutable.ListBuffer
class UsingFixture extends FlatSpec {
def fixture =
new {
val builder = new StringBuilder("ScalaTest is ")
val buffer = new ListBuffer[String]
}
"Testing" should "be easy" in {
val f = fixture
f.builder.append("easy!")
assert(f.builder.toString === "ScalaTest is easy!")
assert(f.buffer.isEmpty)
f.buffer += "sweet"
}
it should "be fun" in {
val f = fixture
f.builder.append("fun!")
assert(f.builder.toString === "ScalaTest is fun!")
assert(f.buffer.isEmpty)
}
}
示例8: UsingBeforeAfter
//设置package包名称以及导入依赖的类
import org.scalatest.FlatSpec
import org.scalatest.BeforeAndAfter
import collection.mutable.ListBuffer
class UsingBeforeAfter extends FlatSpec with BeforeAndAfter {
val builder = new StringBuilder
val buffer = new ListBuffer[String]
before {
builder.append("ScalaTest is ")
}
after {
builder.clear()
buffer.clear()
}
"Testing" should "be easy" in {
builder.append("easy!")
assert(builder.toString === "ScalaTest is easy!")
assert(buffer.isEmpty)
buffer += "sweet"
}
it should "be fun" in {
builder.append("fun!")
assert(builder.toString === "ScalaTest is fun!")
assert(buffer.isEmpty)
}
}
示例9: SwaggerContext
//设置package包名称以及导入依赖的类
package play.modules.swagger.util
import collection.mutable.ListBuffer
import org.slf4j.{LoggerFactory, Logger}
object SwaggerContext {
private val LOGGER = LoggerFactory.getLogger("play.modules.swagger.util.SwaggerContext")
var suffixResponseFormat = true
private val classLoaders = ListBuffer.empty[ClassLoader]
registerClassLoader(this.getClass.getClassLoader)
def registerClassLoader(cl: ClassLoader) = this.classLoaders += cl
def loadClass(name: String) = {
var clazz: Class[_] = null
for (classLoader <- classLoaders.reverse) {
if(clazz == null) {
try {
clazz = Class.forName(name, true, classLoader)
} catch {
case e: ClassNotFoundException => LOGGER.debug("Class not found in classLoader " + classLoader)
}
}
}
if(clazz == null)
throw new ClassNotFoundException("class " + name + " not found")
clazz
}
}
示例10: Node
//设置package包名称以及导入依赖的类
package h3
import h3.Point4d
import collection.mutable.ListBuffer
import Config.NULL_PARENT
class Node(val node_idc: Long, val parent_idc: Long = NULL_PARENT,
val depthc: Int = 0, val tree_sizec: Int = 1, val radiusc: Double = 0,
val areac: Double = 0) {
var node_id: Long = node_idc
var parent: Long = parent_idc
//var children: ListBuffer[Node] = ListBuffer.empty[Node]
var children: List[Node] = List[Node]()
var depth: Int = depthc
var tree_size: Int = tree_sizec
var radius: Double = radiusc
var area: Double = areac
var band: Int = -1
var theta: Double = 0
var phi: Double = 0
var coord: Point4d = new Point4d()
def to_string():String = {
return s"$node_id, " +
s"parent: $parent, " +
s"children: " + children.map { n => n.node_id } + ", " +
s"depth: $depth, " +
s"tree_size: $tree_size, " +
f"radius: $radius, " +
s"area: $area, " +
s"band: $band, " +
s"theta: $theta, " +
s"phi: $phi, " +
"coord: " + coord.to_string()
}
}
示例11: Ribosome
//设置package包名称以及导入依赖的类
package emc.rna
import collection.mutable.ListBuffer
object Ribosome {
type PeptideChain = Seq[AminoAcid]
def decode(rna: RNA, rf: Int = 0): Seq[PeptideChain] = {
// input parameter rna ia an indexed seq, i.e. immutable
var started: Boolean = false
var sequence: ListBuffer[PeptideChain] = ListBuffer.empty
var chain: ListBuffer[AminoAcid] = ListBuffer.empty
for (codon <- rna.codons(rf)) codon match {
case Codon.Start =>
chain += codon.aminoAcid
started = true
case Codon.Stop(_, _, _) =>
if (started) {
sequence += chain
chain = ListBuffer.empty
}
case codon: Codon =>
if (started) {
chain += codon.aminoAcid
}
}
if (chain.nonEmpty) sequence += chain
sequence
}
}
示例12: SignalDetector
//设置package包名称以及导入依赖的类
package pl.fermich.analysis.signal
import pl.fermich.analysis.data.DailyPrice
import collection.mutable.ListBuffer
import pl.fermich.analysis.indicator.IndicatorConverters._
case class SignalDetector(val priceData: List[DailyPrice]) {
def CD_HISTOGRAM(secLong: List[DailyPrice]): List[TradingSignal] = {
val signals = ListBuffer[TradingSignal]()
val convs = priceData CONV_DIV secLong
convs.reduceLeft((c1, c2) => {
if (c1.value < 0 && c2.value >= 0) signals += TradingSignal(c1.date, SignalType.SELL)
if (c1.value > 0 && c2.value <= 0) signals += TradingSignal(c1.date, SignalType.BUY)
c2
})
signals.toList
}
def TRAILING_STOP(threshold: Double): Option[TradingSignal] = {
val maxDailyPrices = priceData.scanRight(DailyPrice(0L, 0.0)){ case(currentPrice, lastMaxPrice) =>
if(currentPrice.value < lastMaxPrice.value) DailyPrice(currentPrice.date, lastMaxPrice.value)
else currentPrice
}.init
val rocs = priceData CONV_DIV_ROC maxDailyPrices
val trailingStop = rocs.reverse.find(_.value.abs >= threshold)
trailingStop match {
case Some(trailingStop) => Some(TradingSignal(trailingStop.date, SignalType.SELL))
case _ => None
}
}
def BREAKOUT(upperPrices: List[DailyPrice]): List[TradingSignal] = {
val signals = ListBuffer[TradingSignal]()
val joined = priceData zip upperPrices
joined.foreach{ case(price, upPrice) => {
require(price.date == upPrice.date, "Inconsistent data! Wrong Dates during BREAKOUT search!")
if (price.value < upPrice.value) signals += TradingSignal(upPrice.date, SignalType.BUY)
}}
signals.toList
}
def BREAKDOWN(lowerPrices: List[DailyPrice]): List[TradingSignal] = {
val signals = ListBuffer[TradingSignal]()
val joined = priceData zip lowerPrices
joined.foreach{ case(price, lowPrice) => {
require(price.date == lowPrice.date, "Inconsistent data! Wrong Dates during BREAKDOWN search!")
if (price.value > lowPrice.value) signals += TradingSignal(lowPrice.date, SignalType.SELL)
}}
signals.toList
}
}