author | wenzelm |
Sat, 08 Oct 2016 11:21:29 +0200 | |
changeset 64100 | 9b1573213ebe |
parent 63579 | 73939a9b70a3 |
child 64370 | 865b39487b5d |
permissions | -rw-r--r-- |
52975 | 1 |
/* Title: Pure/General/multi_map.scala |
2 |
Module: PIDE |
|
56744 | 3 |
Author: Makarius |
52975 | 4 |
|
5 |
Maps with multiple entries per key. |
|
6 |
*/ |
|
7 |
||
8 |
package isabelle |
|
9 |
||
55618 | 10 |
|
63579 | 11 |
import scala.collection.GenTraversableOnce |
52975 | 12 |
import scala.collection.generic.{ImmutableMapFactory, CanBuildFrom} |
13 |
||
14 |
||
15 |
object Multi_Map extends ImmutableMapFactory[Multi_Map] |
|
16 |
{ |
|
17 |
private val empty_val: Multi_Map[Any, Nothing] = new Multi_Map[Any, Nothing](Map.empty) |
|
18 |
override def empty[A, B] = empty_val.asInstanceOf[Multi_Map[A, B]] |
|
19 |
||
20 |
implicit def canBuildFrom[A, B]: CanBuildFrom[Coll, (A, B), Multi_Map[A, B]] = |
|
21 |
new MapCanBuildFrom[A, B] |
|
22 |
} |
|
23 |
||
24 |
||
59073 | 25 |
final class Multi_Map[A, +B] private(protected val rep: Map[A, List[B]]) |
52975 | 26 |
extends scala.collection.immutable.Map[A, B] |
27 |
with scala.collection.immutable.MapLike[A, B, Multi_Map[A, B]] |
|
28 |
{ |
|
29 |
/* Multi_Map operations */ |
|
30 |
||
55488
60c159d490a2
more integrity checks of theory names vs. full node names;
wenzelm
parents:
52975
diff
changeset
|
31 |
def iterator_list: Iterator[(A, List[B])] = rep.iterator |
60c159d490a2
more integrity checks of theory names vs. full node names;
wenzelm
parents:
52975
diff
changeset
|
32 |
|
52975 | 33 |
def get_list(a: A): List[B] = rep.getOrElse(a, Nil) |
34 |
||
35 |
def insert[B1 >: B](a: A, b: B1): Multi_Map[A, B1] = |
|
36 |
{ |
|
37 |
val bs = get_list(a) |
|
38 |
if (bs.contains(b)) this |
|
39 |
else new Multi_Map(rep + (a -> (b :: bs))) |
|
40 |
} |
|
41 |
||
42 |
def remove[B1 >: B](a: A, b: B1): Multi_Map[A, B1] = |
|
43 |
{ |
|
44 |
val bs = get_list(a) |
|
45 |
if (bs.contains(b)) { |
|
46 |
bs.filterNot(_ == b) match { |
|
47 |
case Nil => new Multi_Map(rep - a) |
|
48 |
case bs1 => new Multi_Map(rep + (a -> bs1)) |
|
49 |
} |
|
50 |
} |
|
51 |
else this |
|
52 |
} |
|
53 |
||
59073 | 54 |
def ++[B1 >: B] (other: Multi_Map[A, B1]): Multi_Map[A, B1] = |
55 |
if (this eq other) this |
|
56 |
else if (isEmpty) other |
|
57 |
else |
|
58 |
(this.asInstanceOf[Multi_Map[A, B1]] /: other.rep.iterator) { |
|
59 |
case (m1, (a, bs)) => (bs :\ m1) { case (b, m2) => m2.insert(a, b) } |
|
60 |
} |
|
61 |
||
52975 | 62 |
|
63 |
/* Map operations */ |
|
64 |
||
65 |
override def stringPrefix = "Multi_Map" |
|
66 |
||
67 |
override def empty = Multi_Map.empty |
|
68 |
override def isEmpty: Boolean = rep.isEmpty |
|
69 |
||
70 |
override def keySet: Set[A] = rep.keySet |
|
71 |
||
72 |
override def iterator: Iterator[(A, B)] = |
|
73 |
for ((a, bs) <- rep.iterator; b <- bs.iterator) yield (a, b) |
|
74 |
||
75 |
def get(a: A): Option[B] = get_list(a).headOption |
|
76 |
||
77 |
def + [B1 >: B](p: (A, B1)): Multi_Map[A, B1] = insert(p._1, p._2) |
|
78 |
||
63579 | 79 |
override def ++ [B1 >: B](entries: GenTraversableOnce[(A, B1)]): Multi_Map[A, B1] = |
80 |
(this.asInstanceOf[Multi_Map[A, B1]] /: entries)(_ + _) |
|
81 |
||
52975 | 82 |
def - (a: A): Multi_Map[A, B] = |
83 |
if (rep.isDefinedAt(a)) new Multi_Map(rep - a) else this |
|
84 |
} |