15 { |
15 { |
16 private val empty_val: Multi_Map[Any, Nothing] = new Multi_Map[Any, Nothing](Map.empty) |
16 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]] |
17 def empty[A, B]: Multi_Map[A, B] = empty_val.asInstanceOf[Multi_Map[A, B]] |
18 |
18 |
19 def from[A, B](entries: IterableOnce[(A, B)]): Multi_Map[A, B] = |
19 def from[A, B](entries: IterableOnce[(A, B)]): Multi_Map[A, B] = |
20 (empty[A, B] /: entries)({ case (m, (a, b)) => m.insert(a, b) }) |
20 entries.foldLeft(empty[A, B]) { case (m, (a, b)) => m.insert(a, b) } |
21 |
21 |
22 override def newBuilder[A, B]: mutable.Builder[(A, B), Multi_Map[A, B]] = new Builder[A, B] |
22 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]] |
23 private class Builder[A, B] extends mutable.Builder[(A, B), Multi_Map[A, B]] |
24 { |
24 { |
25 private var res = empty[A, B] |
25 private var res = empty[A, B] |
61 |
61 |
62 def ++[B1 >: B] (other: Multi_Map[A, B1]): Multi_Map[A, B1] = |
62 def ++[B1 >: B] (other: Multi_Map[A, B1]): Multi_Map[A, B1] = |
63 if (this eq other) this |
63 if (this eq other) this |
64 else if (isEmpty) other |
64 else if (isEmpty) other |
65 else |
65 else |
66 (this.asInstanceOf[Multi_Map[A, B1]] /: other.rep.iterator) { |
66 other.rep.iterator.foldLeft(this.asInstanceOf[Multi_Map[A, B1]]) { |
67 case (m1, (a, bs)) => (bs :\ m1) { case (b, m2) => m2.insert(a, b) } |
67 case (m1, (a, bs)) => (bs :\ m1) { case (b, m2) => m2.insert(a, b) } |
68 } |
68 } |
69 |
69 |
70 |
70 |
71 /* Map operations */ |
71 /* Map operations */ |