scala.collection.immutable.TreeSet Scala Examples

The following examples show how to use scala.collection.immutable.TreeSet. 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: CollectionConvertersSuite.scala    From pureconfig   with Mozilla Public License 2.0 5 votes vote down vote up
package pureconfig

import scala.collection.JavaConverters._
import scala.collection.immutable.{ HashSet, ListSet, Queue, TreeSet }

import com.typesafe.config.{ ConfigFactory, ConfigValueFactory, ConfigValueType }
import pureconfig.error.{ ConfigReaderFailures, ConvertFailure, WrongType }

class CollectionConvertersSuite extends BaseSuite {
  implicit override val generatorDrivenConfig = PropertyCheckConfiguration(minSuccessful = 100)

  behavior of "ConfigConvert"

  checkArbitrary[HashSet[String]]

  checkArbitrary[List[Float]]
  checkRead[List[Int]](
    // order of keys maintained
    ConfigValueFactory.fromMap(Map("2" -> 1, "0" -> 2, "1" -> 3).asJava) -> List(2, 3, 1),
    ConfigValueFactory.fromMap(Map("3" -> 2, "1" -> 4).asJava) -> List(4, 2),
    ConfigValueFactory.fromMap(Map("1" -> 1, "a" -> 2).asJava) -> List(1))

  checkFailures[List[Int]](
    ConfigValueFactory.fromMap(Map("b" -> 1, "a" -> 2).asJava) -> ConfigReaderFailures(
      ConvertFailure(WrongType(ConfigValueType.OBJECT, Set(ConfigValueType.LIST)), emptyConfigOrigin, "")),
    ConfigValueFactory.fromMap(Map().asJava) -> ConfigReaderFailures(
      ConvertFailure(WrongType(ConfigValueType.OBJECT, Set(ConfigValueType.LIST)), emptyConfigOrigin, "")))

  checkArbitrary[ListSet[Int]]

  checkArbitrary[Map[String, Int]]
  checkFailures[Map[String, Int]](
    // nested map should fail
    ConfigFactory.parseString("conf.a=1").root() -> ConfigReaderFailures(
      ConvertFailure(WrongType(ConfigValueType.OBJECT, Set(ConfigValueType.NUMBER)), stringConfigOrigin(1), "conf")),
    // wrong value type should fail
    ConfigFactory.parseString("{ a=b }").root() -> ConfigReaderFailures(
      ConvertFailure(WrongType(ConfigValueType.STRING, Set(ConfigValueType.NUMBER)), stringConfigOrigin(1), "a")))

  checkArbitrary[Queue[Boolean]]

  checkArbitrary[Set[Double]]
  checkRead[Set[Int]](
    ConfigValueFactory.fromMap(Map("1" -> 4, "2" -> 5, "3" -> 6).asJava) -> Set(4, 5, 6))

  checkArbitrary[Stream[String]]

  checkArbitrary[TreeSet[Int]]

  checkArbitrary[Vector[Short]]

  checkArbitrary[Option[Int]]

  checkArbitrary[Array[Int]]
} 
Example 2
Source File: XSet.scala    From topshell   with MIT License 5 votes vote down vote up
package com.github.ahnfelt.topshell.data

import scala.collection.immutable.{SortedSet, TreeMap, TreeSet}
import scala.scalajs.js
import scala.scalajs.js.annotation.{JSExport, JSExportTopLevel}

@JSExportTopLevel("XSet")
object XSet {

    private implicit val ordering : Ordering[Any] = XOrder.ordering

    type XSet = SortedSet[Any]
    type Fun[A, B] = js.Function1[A, B]

    @JSExport
    def isInstance(any : Any) : Boolean = any.isInstanceOf[scala.collection.immutable.SortedSet[_]]

    @JSExport
    def of(array : js.Array[Any]) : XSet = {
        TreeSet(array : _*)
    }

    @JSExport
    def toList(set : XSet) : js.Array[Any] = {
        js.Array(set.toList : _*)
    }

    @JSExport
    def toMap(f : Fun[Any, Any], set : XSet) : XMap.XMap = {
        set.foldLeft(XMap.empty) ( (m, v) => m + (v -> f(v)) )
    }

    @JSExport
    def add(value : Any, set : XSet) : XSet = {
        set + value
    }

    @JSExport
    def remove(value : Any, set : XSet) : XSet = {
        set - value
    }

    @JSExport
    def union(a : XSet, b : XSet) : XSet = {
        a ++ b
    }

    @JSExport
    def intersect(a : XSet, b : XSet) : XSet = {
        a.intersect(b)
    }

    @JSExport
    def exclude(a : XSet, b : XSet) : XSet = {
        b -- a
    }

    @JSExport
    def has(value : Any, set : XSet) : Boolean = {
        set.contains(value)
    }

    @JSExport
    def from(value : Any, set : XSet) : XSet = {
        set.from(value)
    }

    @JSExport
    def until(value : Any, set : XSet) : XSet = {
        set.until(value)
    }

    @JSExport
    def foldLeft(f : Fun[Any, Fun[Any, Any]], z : Any, set : XSet) : Any = {
        set.foldLeft(z) { (v, p) => f(p)(v) }
    }

    @JSExport
    def foldRight(f : Fun[Any, Fun[Any, Any]], z : Any, set : XSet) : Any = {
        set.foldRight(z) { (p, v) => f(p)(v) }
    }

    @JSExport
    def size(set : XSet) : Int = {
        set.size
    }

    @JSExport
    def isEmpty(set : XSet) : Boolean = {
        set.isEmpty
    }

    @JSExport
    val empty : XSet = TreeSet.empty

} 
Example 3
Source File: Broadcast.scala    From zio-keeper   with Apache License 2.0 5 votes vote down vote up
package zio.keeper.swim

import zio.keeper.swim.Broadcast._
import zio.stm.{ STM, TRef }
import zio.{ Chunk, UIO }

import scala.collection.immutable.TreeSet

final class Broadcast(
  ref: TRef[TreeSet[Item]],
  sequenceId: TRef[Int],
  messageOverhead: Int,
  messageLimit: Int,
  resent: Int
) {

  def add(message: Message.Broadcast[Chunk[Byte]]): UIO[Unit] =
    sequenceId
      .getAndUpdate(_ + 1)
      .flatMap[Any, Nothing, Unit](
        seqId =>
          ref.update(
            items =>
              items ++ TreeSet(Item(seqId, resent , message.message))
          )
      )
      .commit

  def broadcast(currentMessageSize: Int): UIO[List[Chunk[Byte]]] =
    ref.modify { items =>
      val (toSend, toReschedule, _) = items.foldRight((Vector.empty[Item], Vector.empty[Item], currentMessageSize)) {
        case (item, (toSend, toReschedule, size)) if size + item.chunk.size + messageOverhead <= messageLimit =>
          (toSend :+ item, toReschedule, size + item.chunk.size + messageOverhead)
        case (item, (toSend, toReschedule, size)) =>
          (toSend, toReschedule :+ item, size)
      }

      val newValue = TreeSet.empty[Item] ++ toSend
        .map(item => item.copy(resend = item.resend - 1))
        .filter(_.resend > 0) ++
        toReschedule

      val broadcast = toSend.map(item => item.chunk).toList
      (broadcast, newValue)
    }.commit

}

object Broadcast {

  def make(mtu: Int, resent: Int): UIO[Broadcast] =
    STM.mapN(TRef.make(TreeSet.empty[Item]), TRef.make(0))(new Broadcast(_, _, 3, mtu, resent)).commit

  final case class Item(seqId: Int, resend: Int, chunk: Chunk[Byte])
  implicit val ordering: Ordering[Item] = Ordering.by[Item, Int](_.seqId)

} 
Example 4
Source File: IterableTest.scala    From reactive-programming   with Apache License 2.0 5 votes vote down vote up
package com.test.week4

import com.test.TestSpec

import scala.collection.immutable.TreeSet

class IterableTest extends TestSpec {

  "List" should "be an Iterable" in {
    List(1, 2, 3) shouldBe an[Iterable[_]]
  }

  it should "have next elements" in {
    List(1, 2, 3).iterator should have('hasNext(true))
  }

  it should "contain elements" in {
    List(Seq(0, 1), Seq(1, 2)) should contain inOrder (Seq(0, 1), Seq(1, 2))
  }

  "Vector" should "be an Iterable" in {
    Vector(1, 2, 3) shouldBe an[Iterable[_]]
  }

  it should "have next elements" in {
    Vector(1, 2, 3).iterator should have('hasNext(true))
  }

  "Set" should "be an Iterable" in {
    Set(1, 2, 3) shouldBe an[Iterable[_]]
  }

  it should "have next elements" in {
    Set(1, 2, 3).iterator should have('hasNext(true))
  }

  "Tree" should "be an Iterable" in {
    TreeSet(1, 2, 3) shouldBe an[Iterable[_]]
  }

  it should "have next elements" in {
    TreeSet(1, 2, 3).iterator should have('hasNext(true))
  }

  "Map" should "be an Iterable" in {
    Map(1 -> "1", 2 -> "2", 3 -> "3") shouldBe an[Iterable[_]]
  }

  it should "have next elements" in {
    Map(1 -> "1", 2 -> "2", 3 -> "3").iterator should have('hasNext(true))
  }
}