src/Pure/General/multi_map.scala
changeset 75393 87ebf5a50283
parent 73362 dde25151c3c1
equal deleted inserted replaced
75388:b3ca4a6ed74b 75393:87ebf5a50283
     9 import scala.collection.mutable
     9 import scala.collection.mutable
    10 import scala.collection.{IterableFactory, MapFactory, MapFactoryDefaults}
    10 import scala.collection.{IterableFactory, MapFactory, MapFactoryDefaults}
    11 import scala.collection.immutable.{Iterable, MapOps}
    11 import scala.collection.immutable.{Iterable, MapOps}
    12 
    12 
    13 
    13 
    14 object Multi_Map extends MapFactory[Multi_Map]
    14 object Multi_Map extends MapFactory[Multi_Map] {
    15 {
       
    16   private val empty_val: Multi_Map[Any, Nothing] = new Multi_Map[Any, Nothing](Map.empty)
    15   private val empty_val: Multi_Map[Any, Nothing] = new Multi_Map[Any, Nothing](Map.empty)
    17   def empty[A, B]: Multi_Map[A, B] = empty_val.asInstanceOf[Multi_Map[A, B]]
    16   def empty[A, B]: Multi_Map[A, B] = empty_val.asInstanceOf[Multi_Map[A, B]]
    18 
    17 
    19   def from[A, B](entries: IterableOnce[(A, B)]): Multi_Map[A, B] =
    18   def from[A, B](entries: IterableOnce[(A, B)]): Multi_Map[A, B] =
    20     entries.iterator.foldLeft(empty[A, B]) { case (m, (a, b)) => m.insert(a, b) }
    19     entries.iterator.foldLeft(empty[A, B]) { case (m, (a, b)) => m.insert(a, b) }
    21 
    20 
    22   override def newBuilder[A, B]: mutable.Builder[(A, B), Multi_Map[A, B]] = new Builder[A, B]
    21   override def newBuilder[A, B]: mutable.Builder[(A, B), Multi_Map[A, B]] = new Builder[A, B]
    23   private class Builder[A, B] extends mutable.Builder[(A, B), Multi_Map[A, B]]
    22   private class Builder[A, B] extends mutable.Builder[(A, B), Multi_Map[A, B]] {
    24   {
       
    25     private var res = empty[A, B]
    23     private var res = empty[A, B]
    26     override def clear(): Unit = { res = empty[A, B] }
    24     override def clear(): Unit = { res = empty[A, B] }
    27     override def addOne(p: (A, B)): this.type = { res = res.insert(p._1, p._2); this }
    25     override def addOne(p: (A, B)): this.type = { res = res.insert(p._1, p._2); this }
    28     override def result(): Multi_Map[A, B] = res
    26     override def result(): Multi_Map[A, B] = res
    29   }
    27   }
    30 }
    28 }
    31 
    29 
    32 final class Multi_Map[A, +B] private(protected val rep: Map[A, List[B]])
    30 final class Multi_Map[A, +B] private(protected val rep: Map[A, List[B]])
    33   extends Iterable[(A, B)]
    31   extends Iterable[(A, B)]
    34     with MapOps[A, B, Multi_Map, Multi_Map[A, B]]
    32     with MapOps[A, B, Multi_Map, Multi_Map[A, B]]
    35     with MapFactoryDefaults[A, B, Multi_Map, Iterable]
    33     with MapFactoryDefaults[A, B, Multi_Map, Iterable] {
    36 {
       
    37   /* Multi_Map operations */
    34   /* Multi_Map operations */
    38 
    35 
    39   def iterator_list: Iterator[(A, List[B])] = rep.iterator
    36   def iterator_list: Iterator[(A, List[B])] = rep.iterator
    40 
    37 
    41   def get_list(a: A): List[B] = rep.getOrElse(a, Nil)
    38   def get_list(a: A): List[B] = rep.getOrElse(a, Nil)
    42 
    39 
    43   def insert[B1 >: B](a: A, b: B1): Multi_Map[A, B1] =
    40   def insert[B1 >: B](a: A, b: B1): Multi_Map[A, B1] = {
    44   {
       
    45     val bs = get_list(a)
    41     val bs = get_list(a)
    46     if (bs.contains(b)) this
    42     if (bs.contains(b)) this
    47     else new Multi_Map(rep + (a -> (b :: bs)))
    43     else new Multi_Map(rep + (a -> (b :: bs)))
    48   }
    44   }
    49 
    45 
    50   def remove[B1 >: B](a: A, b: B1): Multi_Map[A, B1] =
    46   def remove[B1 >: B](a: A, b: B1): Multi_Map[A, B1] = {
    51   {
       
    52     val bs = get_list(a)
    47     val bs = get_list(a)
    53     if (bs.contains(b)) {
    48     if (bs.contains(b)) {
    54       bs.filterNot(_ == b) match {
    49       bs.filterNot(_ == b) match {
    55         case Nil => new Multi_Map(rep - a)
    50         case Nil => new Multi_Map(rep - a)
    56         case bs1 => new Multi_Map(rep + (a -> bs1))
    51         case bs1 => new Multi_Map(rep + (a -> bs1))