de.ummels

prioritymap

package prioritymap

Provides an implementation of immutable priority maps, i.e. immutable maps that return their entries sorted by value.

Overview

Priority maps are similar to sorted maps, but while for sorted maps iterator returns an iterator that produces entries sorted by their keys, calling iterator on a priority map returns an iterator that produces entries sorted by their values. Priority maps also offer several range methods, which return a submap with values inside a given range.

Since calling head on a priority map returns a key-value pair with minimal value, priority maps can also be thought of as a more versatile variant of priority queues.

Usage

The easiest way to instantiate a new priority map is to use the apply method in the de.ummels.prioritymap.PriorityMap companion object.

scala> val m = PriorityMap('a' -> 1, 'b' -> 2, 'c' -> 0)
m: de.ummels.prioritymap.PriorityMap[Char,Int] = PriorityMap(c -> 0, a -> 1, b -> 2)

Since priority maps are immutable, updating a key/value pair returns a new map and does not modify the old map.

scala> m + ('b' -> -1)
res1: de.ummels.prioritymap.PriorityMap[Char,Int] = PriorityMap(b -> -1, c -> 0, a -> 1)

scala> m
res2: de.ummels.prioritymap.PriorityMap[Char,Int] = PriorityMap(c -> 0, a -> 1, b -> 2)

In addition to the methods available for maps, priority maps offer methods for obtaining a submap whose values lie inside a given range.

scala> m.from(1)
res3: de.ummels.prioritymap.PriorityMap[Char,Int] = PriorityMap(a -> 1, b -> 2)

scala> m.until(2)
res4: de.ummels.prioritymap.PriorityMap[Char,Int] = PriorityMap(c -> 0, a -> 1)

scala> m.range(1, 2)
res5: de.ummels.prioritymap.PriorityMap[Char,Int] = PriorityMap(a -> 1)
Linear Supertypes
AnyRef, Any
Ordering
  1. Alphabetic
  2. By inheritance
Inherited
  1. prioritymap
  2. AnyRef
  3. Any
  1. Hide All
  2. Show all
Learn more about member selection
Visibility
  1. Public
  2. All

Type Members

  1. final class DefaultPriorityMap[A, B] extends PriorityMap[A, B] with PriorityMapLike[A, B, DefaultPriorityMap[A, B]] with Serializable

    Default implementation of immutable priority maps using two maps: a regular map from keys to values and a sorted map from values to sets of keys.

  2. trait PriorityMap[A, B] extends Map[A, B] with PriorityMapLike[A, B, PriorityMap[A, B]]

    A generic trait for immutable priority maps.

    A generic trait for immutable priority maps. Concrete classes have to provide functionality for the abstract methods in PriorityMap:

    def get(key: A): Option[B]
    def iterator: Iterator[(A, B)]
    def +(kv: (A, B): PriorityMap[A, B]
    def +[B1 >: B](kv: (A, B1)): Map[A, B1]
    def -(key: A): PriorityMap[A, B]
    def rangeImpl(from: Option[B], until: Option[B]): PriorityMap[A, B]
    implicit def ordering: Ordering[B]

    The iterator returned by iterator should generate key/value pairs in the order specified by the implicit ordering on values.

    Concrete classes may also override valueSet, whose default implementation builds up a new SortedSet from the map's values.

  3. trait PriorityMapLike[A, B, +This <: PriorityMapLike[A, B, This] with PriorityMap[A, B]] extends MapLike[A, B, This]

    A template trait for immutable priority maps.

    A template trait for immutable priority maps. To create a concrete priority map, you need to implement the following methods in addition to those of MapLike:

    def +(kv: (A, B): This
    def rangeImpl(from: Option[B], until: Option[B]): This

    The iterator returned by iterator should generate key/value pairs in the order specified by the implicit ordering on values.

    Concrete classes may also override valueSet, whose default implementation builds up a new SortedSet from the map's values.

Value Members

  1. object DefaultPriorityMap extends Serializable

    This object provides a set of operations needed to create priority maps.

  2. object PriorityMap

    This object provides a set of operations needed to create priority maps.

Inherited from AnyRef

Inherited from Any

Ungrouped