summaryrefslogtreecommitdiff
path: root/src/library/scalax/collection/mutable/Map.scala
blob: 82fb6a56809dd68bd2d7e02bc534c5daa33717ae (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
/*                     __                                               *\
**     ________ ___   / /  ___     Scala API                            **
**    / __/ __// _ | / /  / _ |    (c) 2003-2009, LAMP/EPFL             **
**  __\ \/ /__/ __ |/ /__/ __ |    http://scala-lang.org/               **
** /____/\___/_/ |_/____/_/ | |                                         **
**                          |/                                          **
\*                                                                      */

// $Id: Map.scala 16884 2009-01-09 16:52:09Z cunei $


package scalax.collection.mutable

import generic._

/* Factory object for `Map` class */
object Map extends MapFactory[Map] {
  def empty[A, B]: Map[A, B] = new HashMap[A, B]
}

trait Map[A, B]
  extends collection.Map[A, B]
     with MapTemplate[A, B, Map]
     with Growable[(A, B)]
     with Shrinkable[A]
     with Cloneable[Map[A, B]] {
self =>

  /** This method allows one to add a new mapping from <code>key</code>
   *  to <code>value</code> to the map. If the map already contains a
   *  mapping for <code>key</code>, it will be overridden by this
   *  function.
   *
   * @param key    The key to update
   * @param value  The new value
   */
  def update(key: A, value: B): this.type

  /** Add a key/value pair to this map.
   *  @param    kv the key/value pair.
   */
  def += (kv: (A, B)) { update(kv._1, kv._2) }

  /** Remove a key from this map, noop if key is not present.
   *  @param    key the key to be removed
   */
  def -= (key: A)

  def -(key: A): this.type = { -=(key); this }

  /** Removes all elements from the set for
   *  which the predicate <code>p</code> yields the value <code>false</code>.
   */
  def retain(p: A => Boolean): Unit = for ((k, v) <- this) if (!p(k)) -=(k)

  /** Removes all elements from the set. After this operation is completed,
   *  the set will be empty.
   */
  def clear() { for ((k, v) <- elements) -=(k) }

  override def clone(): Map[A, B] = empty[B] ++ this

  /** Return a read-only projection of this set !!! just us an (immutable) setProxy? */
  def readOnly : collection.Map[A, B] = new collection.Map[A, B] {
    override def size = self.size
    override def update(key: A, value: B) = self.update(key, value)
    override def - (elem: A) = self - elem
    override def elements = self.elements
    override def foreach(f: ((A, B)) => Unit) = self.foreach(f)
    override def empty[C] = self.empty[C]
    def get(key: A) = self.get(key)
  }
}