summaryrefslogtreecommitdiff
path: root/sources/scala/HashMap.scala
blob: 4aa4bc7ab5de13d45b9785fb353fddf373322558 (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
/*                     __                                               *\
**     ________ ___   / /  ___     Scala API                            **
**    / __/ __// _ | / /  / _ |    (c) 2003, LAMP/EPFL                  **
**  __\ \/ /__/ __ |/ /__/ __ |                                         **
** /____/\___/_/ |_/____/_/ | |                                         **
**                          |/                                          **
** $Id$
\*                                                                      */

package scala;

/** I promise, there will be some documentation soon! :-) Matthias
 */
class HashMap[A, B] extends MutableMap[A, B] with HashTable[A] {

    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 remove(key: A) = {
        val old = apply(key);
        removeEntry(key);
        old;
    }

    def elements = new Iterator[Pair[A, B]] {
        val iter = entries;
        def hasNext = iter.hasNext;
        def next = iter.next.toPair;
    }

    protected class Entry(k: A, v: B) {
        def key = k;
        var value = v;
        def toPair = Pair(k, value);
        override def toString() = k.toString() + " -> " + value;
    }

    protected def entryKey(e: Entry) = e.key;
}