collection.mutable.ListBuffer Scala Examples
The following examples show how to use collection.mutable.ListBuffer.
You can vote up the ones you like or vote down the ones you don't like,
and go to the original project or source file by following the links above each example.
Example 1
Source File: ExpressionRenderer.scala From nanotest-strawman with Apache License 2.0 | 5 votes |
package verify package asserts import collection.mutable.ListBuffer import collection.immutable.TreeMap class ExpressionRenderer(showTypes: Boolean, shortString: Boolean) { def render(recordedExpr: RecordedExpression[_]): String = { val offset = recordedExpr.text.segmentLength(_.isWhitespace, 0) val intro = new StringBuilder().append(recordedExpr.text.trim()) val lines = ListBuffer(new StringBuilder) val rightToLeft = filterAndSortByAnchor(recordedExpr.recordedValues) for (recordedValue <- rightToLeft) { placeValue(lines, recordedValue.value, math.max(recordedValue.anchor - offset, 0)) } lines.prepend(intro) lines.append(new StringBuilder) // debug // recordedExpr.recordedValues foreach { v => // val line = new StringBuilder() // line.append(v.toString) // lines.append(line) // } lines.mkString("\n") } private[this] def filterAndSortByAnchor(recordedValues: List[RecordedValue]): Iterable[RecordedValue] = { var map = TreeMap[Int, RecordedValue]()(Ordering.by(-_)) // values stemming from compiler generated code often have the same anchor as regular values // and get recorded before them; let's filter them out for { value <- recordedValues } { if (!map.contains(value.anchor)) map += (value.anchor -> value) } map.values } private[this] def placeValue(lines: ListBuffer[StringBuilder], value: Any, col: Int): Unit = { val str = renderValue(value) placeString(lines(0), "|", col) import util.control.Breaks._ breakable { for (line <- lines.drop(1)) { if (fits(line, str, col)) { placeString(line, str, col) break() } placeString(line, "|", col) } val newLine = new StringBuilder() placeString(newLine, str, col) lines.append(newLine) } } private[this] def renderValue(value: Any): String = { val str0 = if (value == null) "null" else value.toString val str = if (!shortString) str0 else if (str0.contains("\n")) str0.linesIterator.toList.headOption.getOrElse("") + "..." else str0 if (showTypes) str + " (" + value.getClass.getName + ")" // TODO: get type name the Scala way else str } private[this] def placeString(line: StringBuilder, str: String, anchor: Int): Unit = { val diff = anchor - line.length for (i <- 1 to diff) line.append(' ') line.replace(anchor, anchor + str.length(), str) } private[this] def fits(line: StringBuilder, str: String, anchor: Int): Boolean = { line.slice(anchor, anchor + str.length() + 1).forall(_.isWhitespace) } }
Example 2
Source File: CompanionSorter.scala From mango with Apache License 2.0 | 5 votes |
package com.kakao.shaded.jackson.module.scala.util import collection.mutable.{ArrayBuffer, ListBuffer} import collection.GenTraversable import collection.generic.GenericCompanion import scala.reflect.ClassTag import scala.language.higherKinds class CompanionSorter[CC[X] <: GenTraversable[X]] { type HKClassManifest[CC2[_]] = ClassTag[CC2[_]] private[this] val companions = new ArrayBuffer[(Class[_], GenericCompanion[CC])]() def add[T[X] <: CC[X] : HKClassManifest](companion: GenericCompanion[T]): CompanionSorter[CC] = { companions += implicitly[HKClassManifest[T]].runtimeClass -> companion this } def toList: List[(Class[_], GenericCompanion[CC])] = { val cs = companions.toArray val output = new ListBuffer[(Class[_], GenericCompanion[CC])]() val remaining = cs.map(_ => 1) val adjMatrix = Array.ofDim[Int](cs.length, cs.length) // Build the adjacency matrix. Only mark the in-edges. for (i <- 0 until cs.length; j <- 0 until cs.length) { val (ic, _) = cs(i) val (jc, _) = cs(j) if (i != j && ic.isAssignableFrom(jc)) { adjMatrix(i)(j) = 1 } } // While we haven't removed every node, remove all nodes with 0 degree in-edges. while (output.length < cs.length) { val startLength = output.length for (i <- 0 until cs.length) { if (remaining(i) == 1 && dotProduct(adjMatrix(i), remaining) == 0) { output += companions(i) remaining(i) = 0 } } // If we couldn't remove any nodes, it means we've found a cycle. Realistically this should never happen. if (output.length == startLength) { throw new IllegalStateException("Companions contain a cycle.") } } output.toList } private[this] def dotProduct(a: Array[Int], b: Array[Int]): Int = { if (a.length != b.length) throw new IllegalArgumentException() (0 until a.length).map(i => a(i) * b(i)).sum } }
Example 3
Source File: RecordingLogger.scala From sbt-coursera with BSD 3-Clause "New" or "Revised" License | 5 votes |
package ch.epfl.lamp 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) = () }
Example 4
Source File: StringFlatSpecWithBeforeAndAfter.scala From scala-tutorials with MIT License | 5 votes |
package com.baeldung.scala.scalatest import org.scalatest._ import collection.mutable.ListBuffer class StringFlatSpecWithBeforeAndAfter extends FlatSpec with BeforeAndAfter { val builder = new StringBuilder before { builder.append("Baeldung ") } after { builder.clear() } "Baeldung" should "be interesting" in { assert(builder.toString === "Baeldung ") builder.append("is very interesting!") assert(builder.toString === "Baeldung is very interesting!") } it should "have great tutorials" in { assert(builder.toString === "Baeldung ") builder.append("has great tutorials!") assert(builder.toString === "Baeldung has great tutorials!") } }