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
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
|
/* __ *\
** ________ ___ / / ___ Scala API **
** / __/ __// _ | / / / _ | (c) 2003-2010, LAMP/EPFL **
** __\ \/ /__/ __ |/ /__/ __ | http://scala-lang.org/ **
** /____/\___/_/ |_/____/_/ | | **
** |/ **
\* */
// $Id$
package scala.collection
package mutable
import generic._
/**
* @since 1
*/
@serializable @SerialVersionUID(1L)
class HashMap[A, B] extends Map[A, B]
with MapLike[A, B, HashMap[A, B]]
with HashTable[A] {
override def empty: HashMap[A, B] = HashMap.empty[A, B]
override def clear() = clearTable()
override def size: Int = tableSize
type Entry = DefaultEntry[A, B]
def get(key: A): Option[B] = {
val e = findEntry(key)
if (e == null) None
else Some(e.value)
}
override def put(key: A, value: B): Option[B] = {
val e = findEntry(key)
if (e == null) { addEntry(new Entry(key, value)); None }
else { val v = e.value; e.value = value; Some(v) }
}
override def update(key: A, value: B): Unit = put(key, value)
override def remove(key: A): Option[B] = {
val e = removeEntry(key)
if (e ne null) Some(e.value)
else None
}
def += (kv: (A, B)): this.type = {
val e = findEntry(kv._1)
if (e == null) addEntry(new Entry(kv._1, kv._2))
else e.value = kv._2
this
}
def -=(key: A): this.type = { removeEntry(key); this }
def iterator = entriesIterator map {e => (e.key, e.value)}
override def foreach[C](f: ((A, B)) => C): Unit = foreachEntry(e => f(e.key, e.value))
/* Override to avoid tuple allocation in foreach */
override def keySet: collection.Set[A] = new DefaultKeySet {
override def foreach[C](f: A => C) = foreachEntry(e => f(e.key))
}
/* Override to avoid tuple allocation in foreach */
override def valuesIterable: collection.Iterable[B] = new DefaultValuesIterable {
override def foreach[C](f: B => C) = foreachEntry(e => f(e.value))
}
/* Override to avoid tuple allocation */
override def keysIterator: Iterator[A] = new Iterator[A] {
val iter = entriesIterator
def hasNext = iter.hasNext
def next = iter.next.key
}
/* Override to avoid tuple allocation */
override def valuesIterator: Iterator[B] = new Iterator[B] {
val iter = entriesIterator
def hasNext = iter.hasNext
def next = iter.next.value
}
private def writeObject(out: java.io.ObjectOutputStream) {
serializeTo(out, _.value)
}
private def readObject(in: java.io.ObjectInputStream) {
init[B](in, new Entry(_, _))
}
}
/** This class implements mutable maps using a hashtable.
*
* @author Matthias Zenger
* @author Martin Odersky
* @version 2.8
*/
object HashMap extends MutableMapFactory[HashMap] {
implicit def canBuildFrom[A, B]: CanBuildFrom[Coll, (A, B), HashMap[A, B]] = new MapCanBuildFrom[A, B]
def empty[A, B]: HashMap[A, B] = new HashMap[A, B]
}
|