scala.collection.SortedMap Scala Examples
The following examples show how to use scala.collection.SortedMap.
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: SortedMapDeserializerModule.scala From mango with Apache License 2.0 | 5 votes |
package com.kakao.shaded.jackson.module.scala.deser import java.util.AbstractMap import java.util.Map.Entry import scala.collection.{mutable, SortedMap} import scala.collection.immutable.TreeMap import com.kakao.shaded.jackson.core.JsonParser import com.kakao.shaded.jackson.databind._ import com.kakao.shaded.jackson.databind.deser.std.{MapDeserializer, ContainerDeserializerBase} import com.kakao.shaded.jackson.databind.jsontype.TypeDeserializer import com.kakao.shaded.jackson.databind.`type`.MapLikeType import com.kakao.shaded.jackson.module.scala.modifiers.MapTypeModifierModule import deser.{ContextualDeserializer, Deserializers, ValueInstantiator} import com.kakao.shaded.jackson.module.scala.introspect.OrderingLocator import scala.language.existentials private class SortedMapBuilderWrapper[K,V](val builder: mutable.Builder[(K,V), SortedMap[K,V]]) extends AbstractMap[K,V] { override def put(k: K, v: V) = { builder += ((k,v)); v } // Isn't used by the deserializer def entrySet(): java.util.Set[Entry[K, V]] = throw new UnsupportedOperationException } private object SortedMapDeserializer { def orderingFor = OrderingLocator.locate _ def builderFor(cls: Class[_], keyCls: JavaType): mutable.Builder[(AnyRef,AnyRef), SortedMap[AnyRef,AnyRef]] = if (classOf[TreeMap[_,_]].isAssignableFrom(cls)) TreeMap.newBuilder[AnyRef,AnyRef](orderingFor(keyCls)) else SortedMap.newBuilder[AnyRef,AnyRef](orderingFor(keyCls)) } private class SortedMapDeserializer( collectionType: MapLikeType, config: DeserializationConfig, keyDeser: KeyDeserializer, valueDeser: JsonDeserializer[_], valueTypeDeser: TypeDeserializer) extends ContainerDeserializerBase[SortedMap[_,_]](collectionType) with ContextualDeserializer { private val javaContainerType = config.getTypeFactory.constructMapLikeType(classOf[MapBuilderWrapper[_,_]], collectionType.getKeyType, collectionType.getContentType) private val instantiator = new ValueInstantiator { def getValueTypeDesc = collectionType.getRawClass.getCanonicalName override def canCreateUsingDefault = true override def createUsingDefault(ctx: DeserializationContext) = new SortedMapBuilderWrapper[AnyRef,AnyRef](SortedMapDeserializer.builderFor(collectionType.getRawClass, collectionType.getKeyType)) } private val containerDeserializer = new MapDeserializer(javaContainerType,instantiator,keyDeser,valueDeser.asInstanceOf[JsonDeserializer[AnyRef]],valueTypeDeser) override def getContentType = containerDeserializer.getContentType override def getContentDeserializer = containerDeserializer.getContentDeserializer override def createContextual(ctxt: DeserializationContext, property: BeanProperty) = if (keyDeser != null && valueDeser != null) this else { val newKeyDeser = Option(keyDeser).getOrElse(ctxt.findKeyDeserializer(collectionType.getKeyType, property)) val newValDeser = Option(valueDeser).getOrElse(ctxt.findContextualValueDeserializer(collectionType.getContentType, property)) new SortedMapDeserializer(collectionType, config, newKeyDeser, newValDeser, valueTypeDeser) } override def deserialize(jp: JsonParser, ctxt: DeserializationContext): SortedMap[_,_] = { containerDeserializer.deserialize(jp,ctxt) match { case wrapper: SortedMapBuilderWrapper[_,_] => wrapper.builder.result() } } } private object SortedMapDeserializerResolver extends Deserializers.Base { private val SORTED_MAP = classOf[collection.SortedMap[_,_]] override def findMapLikeDeserializer(theType: MapLikeType, config: DeserializationConfig, beanDesc: BeanDescription, keyDeserializer: KeyDeserializer, elementTypeDeserializer: TypeDeserializer, elementDeserializer: JsonDeserializer[_]): JsonDeserializer[_] = if (!SORTED_MAP.isAssignableFrom(theType.getRawClass)) null else new SortedMapDeserializer(theType,config,keyDeserializer,elementDeserializer,elementTypeDeserializer) } trait SortedMapDeserializerModule extends MapTypeModifierModule { this += (_ addDeserializers SortedMapDeserializerResolver) }
Example 2
Source File: AnnotationData.scala From ScalaClean with Apache License 2.0 | 5 votes |
package org.scalaclean.analysis object AnnotationData extends StandardExtensionDescriptor[AnnotationData] { override protected def buildImpl(posOffsetStart: Int, posOffsetEnd: Int, otherParams: String*): AnnotationData = { val map: Map[String, String] = otherParams.toList.drop(1).grouped(2).collect { case k :: v :: Nil => k -> v case x :: Nil => println(s"***** $x") x.toString -> "UNKNOWN" }.toMap new AnnotationData(posOffsetStart, posOffsetEnd, otherParams(0), map) } } case class AnnotationData(posOffsetStart: Int, posOffsetEnd: Int, fqName: String, values: Map[String, String]) extends StandardExtensionData { import scala.collection.SortedMap override def restToCSV: String = { val csv = if (values.isEmpty) "" else values.toList.sortBy(_._1).map { e => s"${e._1},${e._2}" }.mkString(",", ",", "") s",$fqName$csv" } override def toString: String = s"AnnotationData[${maskToString(posOffsetStart)},${maskToString(posOffsetEnd)},$fqName,${SortedMap.empty[String, String] ++ values}" require(!fqName.contains(","), fqName) values.foreach { case (k, v) => require(!k.contains(","), s"key of $k") require(!v.contains(","), s"value of $v") } }
Example 3
Source File: QueryString.scala From graphcool-framework with Apache License 2.0 | 5 votes |
package cool.graph.stub import com.netaporter.uri.Uri import scala.collection.SortedMap object QueryString { def queryStringToMap(asNullableString: String): Map[String, String] = { Option(asNullableString) match { case Some(string) => Map(Uri.parse(s"?$string").query.params: _*).filterKeys(_.trim != "").mapValues(_.getOrElse("")) case None => Map.empty } } def queryMapToString(queryMap: Map[String, Any]): String = { queryMap.isEmpty match { case false => "?" + queryMap.map { case (k, v) => s"$k=$v" }.mkString("&") case true => "" } } def mapToSortedMap(map: Map[String, Any]): SortedMap[String, Any] = { SortedMap(map.toSeq: _*) } }
Example 4
Source File: NGrams.scala From featran with Apache License 2.0 | 5 votes |
package com.spotify.featran.transformers import com.spotify.featran.FeatureBuilder import scala.collection.{mutable, SortedMap} def fromSettings( setting: Settings ): Transformer[Seq[String], Set[String], SortedMap[String, Int]] = NGrams(setting.name) } private[featran] class NGrams(name: String, val low: Int, val high: Int, val sep: String) extends NHotEncoder(name, false) { override def prepare(a: Seq[String]): Set[String] = ngrams(a).toSet override def buildFeatures( a: Option[Seq[String]], c: SortedMap[String, Int], fb: FeatureBuilder[_] ): Unit = super.buildFeatures(a.map(ngrams), c, fb) private[transformers] def ngrams(a: Seq[String]): Seq[String] = { val max = if (high == -1) a.length else high val b = Seq.newBuilder[String] var i = low while (i <= max) { if (i == 1) { b ++= a } else if (i <= a.size) { val q = mutable.Queue[String]() var j = 0 val it = a.iterator while (j < i) { q.enqueue(it.next()) j += 1 } b += mkNGram(q, sep) while (it.hasNext) { q.dequeue() q.enqueue(it.next()) b += mkNGram(q, sep) } } i += 1 } b.result() } private def mkNGram(xs: mutable.Queue[String], sep: String): String = { val sb = StringBuilder.newBuilder val i = xs.iterator sb.append(i.next()) while (i.hasNext) { sb.append(sep).append(i.next()) } sb.mkString } }
Example 5
Source File: NHotWeightedEncoder.scala From featran with Apache License 2.0 | 5 votes |
package com.spotify.featran.transformers import com.spotify.featran.{FeatureBuilder, FeatureRejection, FlatReader, FlatWriter} import scala.collection.SortedMap import scala.collection.mutable.{Map => MMap, Set => MSet} def fromSettings( setting: Settings ): Transformer[Seq[WeightedLabel], Set[String], SortedMap[String, Int]] = { val encodeMissingValue = setting.params("encodeMissingValue").toBoolean NHotWeightedEncoder(setting.name, encodeMissingValue) } } private[featran] class NHotWeightedEncoder(name: String, encodeMissingValue: Boolean) extends BaseHotEncoder[Seq[WeightedLabel]](name, encodeMissingValue) { import MissingValue.MissingValueToken def addMissingValue( fb: FeatureBuilder[_], unseen: MSet[String], keys: Seq[String], unseenWeight: Double ): Unit = if (keys.isEmpty) { fb.add(name + '_' + MissingValueToken, 1.0) } else if (unseen.isEmpty) { fb.skip() } else { fb.add(name + '_' + MissingValueToken, unseenWeight) } override def prepare(a: Seq[WeightedLabel]): Set[String] = Set(a.map(_.name): _*) override def buildFeatures( a: Option[Seq[WeightedLabel]], c: SortedMap[String, Int], fb: FeatureBuilder[_] ): Unit = a match { case Some(xs) => val weights = MMap.empty[String, Double].withDefaultValue(0.0) xs.foreach(x => weights(x.name) += x.value) var unseenWeight = 0.0 val keys = weights.keySet.toList.sorted var prev = -1 val unseen = MSet[String]() keys.foreach { key => c.get(key) match { case Some(curr) => val gap = curr - prev - 1 if (gap > 0) fb.skip(gap) fb.add(name + '_' + key, weights(key)) prev = curr case None => unseen += key unseenWeight += weights(key) } } val gap = c.size - prev - 1 if (gap > 0) fb.skip(gap) if (encodeMissingValue) { addMissingValue(fb, unseen, keys, unseenWeight) } if (unseen.nonEmpty) { fb.reject(this, FeatureRejection.Unseen(unseen.toSet)) } case None => addMissingItem(c, fb) } override def flatRead[T: FlatReader]: T => Option[Any] = FlatReader[T].readWeightedLabel(name) override def flatWriter[T](implicit fw: FlatWriter[T]): Option[Seq[WeightedLabel]] => fw.IF = fw.writeWeightedLabel(name) }
Example 6
Source File: PositionEncoder.scala From featran with Apache License 2.0 | 5 votes |
package com.spotify.featran.transformers import com.spotify.featran.{FeatureBuilder, FeatureRejection, FlatReader, FlatWriter} import scala.collection.SortedMap def fromSettings(setting: Settings): Transformer[String, Set[String], SortedMap[String, Int]] = PositionEncoder(setting.name) } private[featran] class PositionEncoder(name: String) extends BaseHotEncoder[String](name, false) { override def prepare(a: String): Set[String] = Set(a) override def featureDimension(c: SortedMap[String, Int]): Int = 1 override def featureNames(c: SortedMap[String, Int]): Seq[String] = Seq(name) override def buildFeatures( a: Option[String], c: SortedMap[String, Int], fb: FeatureBuilder[_] ): Unit = a match { case Some(k) => c.get(k) match { case Some(v) => fb.add(name, v.toDouble) case None => fb.skip(1) fb.reject(this, FeatureRejection.Unseen(Set(k))) } case None => fb.skip(1) fb.reject(this, FeatureRejection.Collision) } override def flatRead[T: FlatReader]: T => Option[Any] = FlatReader[T].readString(name) override def flatWriter[T](implicit fw: FlatWriter[T]): Option[String] => fw.IF = fw.writeString(name) }
Example 7
Source File: NHotEncoder.scala From featran with Apache License 2.0 | 5 votes |
package com.spotify.featran.transformers import com.spotify.featran.{FeatureBuilder, FeatureRejection, FlatReader, FlatWriter} import scala.collection.SortedMap import scala.collection.mutable.{Set => MSet} def fromSettings( setting: Settings ): Transformer[Seq[String], Set[String], SortedMap[String, Int]] = { val encodeMissingValue = setting.params("encodeMissingValue").toBoolean NHotEncoder(setting.name, encodeMissingValue) } } private[featran] class NHotEncoder(name: String, encodeMissingValue: Boolean) extends BaseHotEncoder[Seq[String]](name, encodeMissingValue) { import MissingValue.MissingValueToken def addMissingValue(fb: FeatureBuilder[_], unseen: MSet[String], keys: Seq[String]): Unit = if ( unseen.isEmpty && keys.nonEmpty ) { fb.skip() } else { fb.add(name + '_' + MissingValueToken, 1.0) } override def prepare(a: Seq[String]): Set[String] = Set(a: _*) override def buildFeatures( a: Option[Seq[String]], c: SortedMap[String, Int], fb: FeatureBuilder[_] ): Unit = a match { case Some(xs) => val keys = xs.distinct.sorted var prev = -1 val unseen = MSet[String]() keys.foreach { key => c.get(key) match { case Some(curr) => val gap = curr - prev - 1 if (gap > 0) fb.skip(gap) fb.add(name + '_' + key, 1.0) prev = curr case None => unseen += key } } val gap = c.size - prev - 1 if (gap > 0) fb.skip(gap) if (encodeMissingValue) { addMissingValue(fb, unseen, keys) } if (unseen.nonEmpty) { fb.reject(this, FeatureRejection.Unseen(unseen.toSet)) } case None => addMissingItem(c, fb) } override def flatRead[T: FlatReader]: T => Option[Any] = FlatReader[T].readStrings(name) override def flatWriter[T](implicit fw: FlatWriter[T]): Option[Seq[String]] => fw.IF = fw.writeStrings(name) }
Example 8
Source File: Bencharts.scala From rtree2d with Apache License 2.0 | 5 votes |
import java.awt.{Color, Paint} import java.text.NumberFormat import javax.imageio.ImageIO import org.jfree.chart.JFreeChart import org.jfree.chart.axis.LogarithmicAxis import org.jfree.chart.plot.{DefaultDrawingSupplier, XYPlot} import org.jfree.chart.renderer.xy.XYErrorRenderer import org.jfree.data.xy.{YIntervalSeries, YIntervalSeriesCollection} import sbt._ import com.github.plokhotnyuk.jsoniter_scala.macros._ import com.github.plokhotnyuk.jsoniter_scala.core._ import com.github.plokhotnyuk.jsoniter_scala.macros.JsonCodecMaker._ import scala.collection.SortedMap def apply(jmhReport: File, yAxisTitle: String, targetDir: File): Unit = { val allResults = readFromArray(IO.readBytes(jmhReport))(make[Seq[BenchmarkResult]](CodecMakerConfig)) val constParams = allResults.flatMap(_.params.toSeq).groupBy(_._1).collect { case (_, kvs) if kvs.distinct.size == 1 => kvs.head }.toSeq allResults.groupBy(benchmarkName(constParams)).foreach { case (benchmark, results) => val dataset = new YIntervalSeriesCollection { SortedMap(results.groupBy(otherParams(constParams)).toSeq:_*).foreach { case (params, iterations) => addSeries(new YIntervalSeries(params) { iterations.foreach { iteration => val x = iteration.params.get("size").fold(0.0)(_.toDouble) val y = Math.max(iteration.primaryMetric.score, 1.0) val yLow = Math.max(iteration.primaryMetric.scoreConfidence._1, 1.0) val yHigh = Math.max(iteration.primaryMetric.scoreConfidence._2, 1.0) add(x, y, yLow, yHigh) } }) } } val renderer = new XYErrorRenderer { (0 to dataset.getSeriesCount).foreach(i => setSeriesLinesVisible(i, true)) } val plot = new XYPlot(dataset, axis("Size"), axis(yAxisTitle), renderer) { setDrawingSupplier(new DefaultDrawingSupplier { override def getNextPaint: Paint = super.getNextPaint match { case x: Color if x.getRed > 200 && x.getGreen > 200 => new Color(x.getRed, (x.getGreen * 0.8).toInt, x.getBlue, x.getAlpha) case x => x } }) } val chart = new JFreeChart(benchmark, JFreeChart.DEFAULT_TITLE_FONT, plot, true) ImageIO.write(chart.createBufferedImage(1200, 900), "png", targetDir / s"$benchmark.png") } } private def axis(title: String): LogarithmicAxis = new LogarithmicAxis(title) { setAllowNegativesFlag(true) setNumberFormatOverride(NumberFormat.getInstance()) } private def benchmarkName(constParams: Seq[(String, String)])(result: BenchmarkResult): String = { val benchName = result.benchmark.split("""\.""").last constParams.map { case (k, v) => s"$k=$v" }.sorted.mkString(s"$benchName[", ",", "]") } private def otherParams(constParams: Seq[(String, String)])(result: BenchmarkResult): String = { val constParamNames = constParams.map(_._1).toSet val benchSuitName = result.benchmark.split("""\.""").reverse.tail.head result.params.filterKeys(k => k != "size" && !constParamNames(k)).map { case (k, v) => s"$k=$v" }.toSeq.sorted.mkString(s"$benchSuitName[", ",", "]") } } case class BenchmarkMetric(score: Double, scoreConfidence: (Double, Double)) case class BenchmarkResult(benchmark: String, params: Map[String, String], primaryMetric: BenchmarkMetric)