| author | wenzelm |
| Sat, 15 Feb 2014 16:55:48 +0100 | |
| changeset 55502 | 72238ea2201c |
| parent 55488 | 60c159d490a2 |
| child 55618 | 995162143ef4 |
| permissions | -rw-r--r-- |
| 52975 | 1 |
/* Title: Pure/General/multi_map.scala |
2 |
Module: PIDE |
|
3 |
||
4 |
Maps with multiple entries per key. |
|
5 |
*/ |
|
6 |
||
7 |
package isabelle |
|
8 |
||
9 |
import scala.collection.generic.{ImmutableMapFactory, CanBuildFrom}
|
|
10 |
||
11 |
||
12 |
object Multi_Map extends ImmutableMapFactory[Multi_Map] |
|
13 |
{
|
|
14 |
private val empty_val: Multi_Map[Any, Nothing] = new Multi_Map[Any, Nothing](Map.empty) |
|
15 |
override def empty[A, B] = empty_val.asInstanceOf[Multi_Map[A, B]] |
|
16 |
||
17 |
implicit def canBuildFrom[A, B]: CanBuildFrom[Coll, (A, B), Multi_Map[A, B]] = |
|
18 |
new MapCanBuildFrom[A, B] |
|
19 |
} |
|
20 |
||
21 |
||
22 |
final class Multi_Map[A, +B] private(rep: Map[A, List[B]]) |
|
23 |
extends scala.collection.immutable.Map[A, B] |
|
24 |
with scala.collection.immutable.MapLike[A, B, Multi_Map[A, B]] |
|
25 |
{
|
|
26 |
/* Multi_Map operations */ |
|
27 |
||
|
55488
60c159d490a2
more integrity checks of theory names vs. full node names;
wenzelm
parents:
52975
diff
changeset
|
28 |
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
|
29 |
|
| 52975 | 30 |
def get_list(a: A): List[B] = rep.getOrElse(a, Nil) |
31 |
||
32 |
def insert[B1 >: B](a: A, b: B1): Multi_Map[A, B1] = |
|
33 |
{
|
|
34 |
val bs = get_list(a) |
|
35 |
if (bs.contains(b)) this |
|
36 |
else new Multi_Map(rep + (a -> (b :: bs))) |
|
37 |
} |
|
38 |
||
39 |
def remove[B1 >: B](a: A, b: B1): Multi_Map[A, B1] = |
|
40 |
{
|
|
41 |
val bs = get_list(a) |
|
42 |
if (bs.contains(b)) {
|
|
43 |
bs.filterNot(_ == b) match {
|
|
44 |
case Nil => new Multi_Map(rep - a) |
|
45 |
case bs1 => new Multi_Map(rep + (a -> bs1)) |
|
46 |
} |
|
47 |
} |
|
48 |
else this |
|
49 |
} |
|
50 |
||
51 |
||
52 |
/* Map operations */ |
|
53 |
||
54 |
override def stringPrefix = "Multi_Map" |
|
55 |
||
56 |
override def empty = Multi_Map.empty |
|
57 |
override def isEmpty: Boolean = rep.isEmpty |
|
58 |
||
59 |
override def keySet: Set[A] = rep.keySet |
|
60 |
||
61 |
override def iterator: Iterator[(A, B)] = |
|
62 |
for ((a, bs) <- rep.iterator; b <- bs.iterator) yield (a, b) |
|
63 |
||
64 |
def get(a: A): Option[B] = get_list(a).headOption |
|
65 |
||
66 |
def + [B1 >: B](p: (A, B1)): Multi_Map[A, B1] = insert(p._1, p._2) |
|
67 |
||
68 |
def - (a: A): Multi_Map[A, B] = |
|
69 |
if (rep.isDefinedAt(a)) new Multi_Map(rep - a) else this |
|
70 |
} |