blob: 6ae089bc3eef73caff1f1666f26335ec11f700ac (
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
|
/* __ *\
** ________ ___ / / ___ Scala API **
** / __/ __// _ | / / / _ | (c) 2003-2004, LAMP/EPFL **
** __\ \/ /__/ __ |/ /__/ __ | **
** /____/\___/_/ |_/____/_/ | | **
** |/ **
** $Id$
\* */
package scala.collection.mutable;
/** This trait is used internally. It implements the mutable <code>Map</code>
* trait in terms of three functions: <code>findEntry</code>, <code>addEntry</code>,
* and <code>entries</code>.
*
* @author Matthias Zenger
* @version 1.0, 08/07/2003
*/
trait DefaultMapModel[A, B] extends AnyRef with scala.collection.mutable.Map[A, B] {
protected type Entry = DefaultEntry[A,B];
protected def findEntry(key: A): Option[Entry];
protected def addEntry(e: Entry): Unit;
protected def entries: Iterator[Entry];
def get(key: A) = findEntry(key) match {
case None => None
case Some(e) => Some(e.value);
}
def update(key: A, value: B) = findEntry(key) match {
case None => addEntry(new Entry(key, value));
case Some(e) => e.value = value;
}
def elements = new Iterator[Pair[A, B]] {
val iter = entries;
def hasNext = iter.hasNext;
def next = iter.next.toPair;
}
}
[serializable]
protected class DefaultEntry[A,B](k: A, v: B) extends AnyRef {
def key = k;
var value = v;
def toPair = Pair(k, value);
override def toString() = k.toString() + " -> " + value;
}
|