scala.collection.immutable.Set Scala Examples

The following examples show how to use scala.collection.immutable.Set. 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: InsertOrdSet.scala    From daml   with Apache License 2.0 5 votes vote down vote up
// Copyright (c) 2020 Digital Asset (Switzerland) GmbH and/or its affiliates. All rights reserved.
// SPDX-License-Identifier: Apache-2.0

package com.daml.lf.data


import scala.collection.immutable.{HashSet, Set, Queue}
import scala.collection.{SetLike, AbstractSet}
import scala.collection.generic.{
  ImmutableSetFactory,
  GenericCompanion,
  CanBuildFrom,
  GenericSetTemplate
}

final class InsertOrdSet[T] private (_items: Queue[T], _hashSet: HashSet[T])
    extends AbstractSet[T]
    with Set[T]
    with SetLike[T, InsertOrdSet[T]]
    with GenericSetTemplate[T, InsertOrdSet]
    with Serializable {
  override def empty: InsertOrdSet[T] = InsertOrdSet.empty
  override def size: Int = _hashSet.size

  def iterator: Iterator[T] =
    _items.reverseIterator

  override def contains(elem: T): Boolean =
    _hashSet.contains(elem)

  override def +(elem: T): InsertOrdSet[T] =
    if (_hashSet.contains(elem))
      this
    else
      new InsertOrdSet(
        elem +: _items,
        _hashSet + elem
      )

  override def -(elem: T): InsertOrdSet[T] =
    new InsertOrdSet(
      _items.filter(elem2 => elem != elem2),
      _hashSet - elem
    )

  override def companion: GenericCompanion[InsertOrdSet] = InsertOrdSet

}

object InsertOrdSet extends ImmutableSetFactory[InsertOrdSet] {
  private val Empty = new InsertOrdSet(Queue.empty, HashSet.empty)
  override def empty[T] = Empty.asInstanceOf[InsertOrdSet[T]]
  def emptyInstance: InsertOrdSet[Any] = empty[Any]

  def fromSeq[T](s: Seq[T]): InsertOrdSet[T] =
    new InsertOrdSet(Queue(s.reverse: _*), HashSet(s: _*))

  implicit def canBuildFrom[A]: CanBuildFrom[Coll, A, InsertOrdSet[A]] =
    setCanBuildFrom[A]

} 
Example 2
Source File: ReadJournalDaoImpl.scala    From akka-persistence-dynamodb   with Apache License 2.0 5 votes vote down vote up
package com.github.j5ik2o.akka.persistence.dynamodb.query.dao

import akka.NotUsed
import akka.actor.ActorSystem
import akka.persistence.PersistentRepr
import akka.stream.ActorMaterializer
import akka.stream.scaladsl.{ Flow, Source }
import com.github.j5ik2o.akka.persistence.dynamodb.config.QueryPluginConfig
import com.github.j5ik2o.akka.persistence.dynamodb.journal.dao.{ DaoSupport, JournalRowReadDriver }
import com.github.j5ik2o.akka.persistence.dynamodb.journal.JournalRow
import com.github.j5ik2o.akka.persistence.dynamodb.metrics.MetricsReporter
import com.github.j5ik2o.akka.persistence.dynamodb.model.{ PersistenceId, SequenceNumber }
import com.github.j5ik2o.akka.persistence.dynamodb.serialization.FlowPersistentReprSerializer

import scala.collection.immutable.Set
import scala.concurrent.ExecutionContext
import scala.util.Try

class ReadJournalDaoImpl(
    queryProcessor: QueryProcessor,
    override protected val journalRowDriver: JournalRowReadDriver,
    pluginConfig: QueryPluginConfig,
    override val serializer: FlowPersistentReprSerializer[JournalRow],
    override protected val metricsReporter: Option[MetricsReporter]
)(implicit val ec: ExecutionContext, system: ActorSystem)
    extends ReadJournalDao
    with DaoSupport {

  implicit val mat = ActorMaterializer()

  override def allPersistenceIds(max: Long): Source[PersistenceId, NotUsed] = queryProcessor.allPersistenceIds(max)

  private def perfectlyMatchTag(tag: String, separator: String): Flow[JournalRow, JournalRow, NotUsed] =
    Flow[JournalRow].filter(_.tags.exists(tags => tags.split(separator).contains(tag)))

  override def eventsByTag(
      tag: String,
      offset: Long,
      maxOffset: Long,
      max: Long
  ): Source[Try[(PersistentRepr, Set[String], Long)], NotUsed] =
    eventsByTagAsJournalRow(tag, offset, maxOffset, max)
      .via(perfectlyMatchTag(tag, pluginConfig.tagSeparator))
      .via(serializer.deserializeFlowAsTry)

  override def eventsByTagAsJournalRow(
      tag: String,
      offset: Long,
      maxOffset: Long,
      max: Long
  ): Source[JournalRow, NotUsed] = queryProcessor.eventsByTagAsJournalRow(tag, offset, maxOffset, max)

  override def journalSequence(offset: Long, limit: Long): Source[Long, NotUsed] =
    queryProcessor.journalSequence(offset, limit)

  override def getMessagesAsJournalRow(
      persistenceId: PersistenceId,
      fromSequenceNr: SequenceNumber,
      toSequenceNr: SequenceNumber,
      max: Long,
      deleted: Option[Boolean]
  ): Source[JournalRow, NotUsed] =
    journalRowDriver.getJournalRows(persistenceId, fromSequenceNr, toSequenceNr, max, deleted)

  override def maxJournalSequence(): Source[Long, NotUsed] = {
    Source.single(Long.MaxValue)
  }

} 
Example 3
Source File: SetOfIntsReading.scala    From jsoniter-scala   with MIT License 5 votes vote down vote up
package com.github.plokhotnyuk.jsoniter_scala.benchmark

import java.nio.charset.StandardCharsets.UTF_8

import com.avsystem.commons.serialization.json._
import com.github.plokhotnyuk.jsoniter_scala.benchmark.DslPlatformJson._
import com.github.plokhotnyuk.jsoniter_scala.benchmark.JacksonSerDesers._
import com.github.plokhotnyuk.jsoniter_scala.benchmark.JsoniterScalaCodecs._
import com.github.plokhotnyuk.jsoniter_scala.benchmark.SprayFormats._
import com.github.plokhotnyuk.jsoniter_scala.core._
import com.rallyhealth.weejson.v1.jackson.FromJson
import com.rallyhealth.weepickle.v1.WeePickle.ToScala
import io.circe.parser._
import org.openjdk.jmh.annotations.Benchmark
import play.api.libs.json.Json
import spray.json._
import upickle.default._

import scala.collection.immutable.Set

class SetOfIntsReading extends SetOfIntsBenchmark {
  @Benchmark
  def avSystemGenCodec(): Set[Int] = JsonStringInput.read[Set[Int]](new String(jsonBytes, UTF_8))

  @Benchmark
  def borer(): Set[Int] = io.bullet.borer.Json.decode(jsonBytes).to[Set[Int]].value

  @Benchmark
  def circe(): Set[Int] = decode[Set[Int]](new String(jsonBytes, UTF_8)).fold(throw _, identity)

  @Benchmark
  def dslJsonScala(): Set[Int] = dslJsonDecode[Set[Int]](jsonBytes)

  @Benchmark
  def jacksonScala(): Set[Int] = jacksonMapper.readValue[Set[Int]](jsonBytes)

  @Benchmark
  def jsoniterScala(): Set[Int] = readFromArray[Set[Int]](jsonBytes)

  @Benchmark
  def playJson(): Set[Int] = Json.parse(jsonBytes).as[Set[Int]]

  @Benchmark
  def sprayJson(): Set[Int] = JsonParser(jsonBytes).convertTo[Set[Int]]

  @Benchmark
  def uPickle(): Set[Int] = read[Set[Int]](jsonBytes)

  @Benchmark
  def weePickle(): Set[Int] = FromJson(jsonBytes).transform(ToScala[Set[Int]])
} 
Example 4
Source File: SetOfIntsBenchmark.scala    From jsoniter-scala   with MIT License 5 votes vote down vote up
package com.github.plokhotnyuk.jsoniter_scala.benchmark

import java.nio.charset.StandardCharsets.UTF_8

import org.openjdk.jmh.annotations.{Param, Setup}

import scala.collection.immutable.Set

abstract class SetOfIntsBenchmark extends CommonParams {
  @Param(Array("1", "10", "100", "1000", "10000", "100000", "1000000"))
  var size: Int = 1000
  var obj: Set[Int] = _
  var jsonString: String = _
  var jsonBytes: Array[Byte] = _
  var preallocatedBuf: Array[Byte] = _

  @Setup
  def setup(): Unit = {
    obj = (1 to size).map(i => ((i * 1498724053) / Math.pow(10, i % 10)).toInt).toSet
    jsonString = obj.mkString("[", ",", "]")
    jsonBytes = jsonString.getBytes(UTF_8)
    preallocatedBuf = new Array[Byte](jsonBytes.length + 100)
  }
} 
Example 5
Source File: SetOfIntsWritingSpec.scala    From jsoniter-scala   with MIT License 5 votes vote down vote up
package com.github.plokhotnyuk.jsoniter_scala.benchmark

import com.github.plokhotnyuk.jsoniter_scala.benchmark.JsoniterScalaCodecs.setOfIntsCodec
import com.github.plokhotnyuk.jsoniter_scala.core.readFromArray

import scala.collection.immutable.Set

class SetOfIntsWritingSpec extends BenchmarkSpecBase {
  def benchmark: SetOfIntsWriting = new SetOfIntsWriting {
    setup()
  }
  
  "SetOfIntsWriting" should {
    "serialize properly" in {
      val b = benchmark
      toString(b.avSystemGenCodec()) shouldBe b.jsonString
      toString(b.borer()) shouldBe b.jsonString
      toString(b.circe()) shouldBe b.jsonString
      toString(b.dslJsonScala()) shouldBe b.jsonString
      toString(b.jacksonScala()) shouldBe b.jsonString
      toString(b.jsoniterScala()) shouldBe b.jsonString
      toString(b.preallocatedBuf, 0, b.jsoniterScalaPrealloc()) shouldBe b.jsonString
      toString(b.playJson()) shouldBe b.jsonString
      readFromArray[Set[Int]](b.sprayJson()) shouldBe b.obj
      toString(b.uPickle()) shouldBe b.jsonString
      toString(b.weePickle()) shouldBe b.jsonString
    }
  }
} 
Example 6
Source File: AlphaTLApTools.scala    From apalache   with Apache License 2.0 5 votes vote down vote up
package at.forsyte.apalache.tla.assignments

import at.forsyte.apalache.tla.lir.{LetInEx, NameEx, OperEx, TlaEx}
import at.forsyte.apalache.tla.lir.oper.{TlaActionOper, TlaOper}

import scala.collection.immutable.Set


  def findPrimes( ex : TlaEx ) : Set[String] = {
    ex match {
      case OperEx( TlaActionOper.prime, NameEx( name ) ) =>
        Set( name )
      case OperEx( _, args@_* ) =>
        args.map( findPrimes ).fold( Set[String]() ) {
          _ ++ _
        }
      case LetInEx( body, defs@_* ) =>
        val primesInDefs = defs map { d => findPrimes(d.body) }
        primesInDefs.foldLeft( findPrimes(body) ){ _ ++ _ }
      case _ =>
        Set[String]()
    }
  }

} 
Example 7
Source File: StubbedConsulClient.scala    From nelson   with Apache License 2.0 5 votes vote down vote up
package nelson

import cats.~>
import cats.effect.IO

import helm.ConsulOp

import scala.collection.immutable.Set

object StubbedConsulClient extends (ConsulOp ~> IO) {
  def apply[A](fa: ConsulOp[A]): IO[A] = fa match {
    case ConsulOp.KVGet(_) => IO.pure(None)
    case ConsulOp.KVSet(_, _) => IO.unit
    case ConsulOp.KVListKeys(_) => IO.pure(Set.empty)
    case ConsulOp.KVDelete(_) => IO.unit
    case ConsulOp.HealthListChecksForService(_, _, _, _) => IO.pure(List.empty)
    case ConsulOp.HealthListChecksForNode(_, _) => IO.pure(List.empty)
    case ConsulOp.HealthListChecksInState(_, _, _, _) => IO.pure(List.empty)
    case ConsulOp.HealthListNodesForService(_, _, _, _, _, _) => IO.pure(List.empty)
    case ConsulOp.AgentRegisterService(_, _, _, _, _, _, _, _) => IO.unit
    case ConsulOp.AgentDeregisterService(_) => IO.unit
    case ConsulOp.AgentListServices => IO.pure(Map.empty)
    case ConsulOp.AgentEnableMaintenanceMode(_, _, _) => IO.unit
  }
}