summaryrefslogtreecommitdiff
path: root/src/library/scala/collection/mutable/OpenHashMap.scala
diff options
context:
space:
mode:
authorPhilipp Haller <hallerp@gmail.com>2009-11-24 17:11:45 +0000
committerPhilipp Haller <hallerp@gmail.com>2009-11-24 17:11:45 +0000
commit2e0c9a6ba45b7b394c92e88d85932b7e8f6aafb6 (patch)
tree3ad955cd2abeba6412f0500b47a70a9e77f383ae /src/library/scala/collection/mutable/OpenHashMap.scala
parent4ad672b0b279ee5b28d166483928a5acc6d136d7 (diff)
downloadscala-2e0c9a6ba45b7b394c92e88d85932b7e8f6aafb6.tar.gz
scala-2e0c9a6ba45b7b394c92e88d85932b7e8f6aafb6.tar.bz2
scala-2e0c9a6ba45b7b394c92e88d85932b7e8f6aafb6.zip
Made mutable.OpenHashMap a MapLike. Closes #2681.
Diffstat (limited to 'src/library/scala/collection/mutable/OpenHashMap.scala')
-rw-r--r--src/library/scala/collection/mutable/OpenHashMap.scala30
1 files changed, 17 insertions, 13 deletions
diff --git a/src/library/scala/collection/mutable/OpenHashMap.scala b/src/library/scala/collection/mutable/OpenHashMap.scala
index 5582d48355..42625092e4 100644
--- a/src/library/scala/collection/mutable/OpenHashMap.scala
+++ b/src/library/scala/collection/mutable/OpenHashMap.scala
@@ -24,9 +24,10 @@ object OpenHashMap{
def empty[K, V] = new OpenHashMap[K, V];
- private[mutable] class Entry[Key, Value](val key : Key,
- val hash : Int,
- var value : Option[Value])
+ final private class OpenEntry[Key, Value](val key: Key,
+ val hash: Int,
+ var value: Option[Value])
+ extends HashEntry[Key, OpenEntry[Key, Value]]
private[mutable] def highestOneBit(j : Int) = { // This should really go somewhere central as we're now code sharing by cut and paste. :(
var i = j;
@@ -41,8 +42,6 @@ object OpenHashMap{
private[mutable] def nextPowerOfTwo(i : Int) = highestOneBit(i) << 1;
}
-import OpenHashMap.Entry;
-
/**
* A mutable hash map based on an open hashing scheme. The precise scheme is undefined,
* but it should make a reasonable effort to ensure that an insert with consecutive hash
@@ -52,15 +51,20 @@ import OpenHashMap.Entry;
* @author David MacIver
* @since 2.7
*/
-class OpenHashMap[Key, Value](initialSize : Int) extends scala.collection.mutable.Map[Key, Value]{
+class OpenHashMap[Key, Value](initialSize : Int) extends Map[Key, Value]
+ with MapLike[Key, Value, OpenHashMap[Key, Value]] {
+
+ import OpenHashMap.OpenEntry
+ type Entry = OpenEntry[Key, Value]
+
def this() = this(8);
- override def empty = OpenHashMap.empty
+ override def empty: OpenHashMap[Key, Value] = OpenHashMap.empty[Key, Value]
private[this] val actualInitialSize = OpenHashMap.nextPowerOfTwo(initialSize);
private var mask = actualInitialSize - 1;;
- private var table : Array[Entry[Key, Value]] = new Array[Entry[Key, Value]](actualInitialSize);
+ private var table : Array[Entry] = new Array[Entry](actualInitialSize);
private var _size = 0;
private var deleted = 0;
@@ -80,7 +84,7 @@ class OpenHashMap[Key, Value](initialSize : Int) extends scala.collection.mutabl
val oldSize = mask + 1;
val newSize = 4 * oldSize;
val oldTable = table;
- table = new Array[Entry[Key, Value]](newSize);
+ table = new Array[Entry](newSize);
mask = newSize - 1;
oldTable.foreach( entry =>
if (entry != null && entry.value != None) addEntry(entry));
@@ -104,7 +108,7 @@ class OpenHashMap[Key, Value](initialSize : Int) extends scala.collection.mutabl
index;
}
- private[this] def addEntry(entry : Entry[Key, Value]) =
+ private[this] def addEntry(entry : Entry) =
if (entry != null) table(findIndex(entry.key, entry.hash)) = entry;
override def update(key : Key, value : Value) {
@@ -122,7 +126,7 @@ class OpenHashMap[Key, Value](initialSize : Int) extends scala.collection.mutabl
val index = findIndex(key, hash);
val entry = table(index);
if (entry == null) {
- table(index) = new Entry(key, hash, Some(value));
+ table(index) = new OpenEntry(key, hash, Some(value));
modCount += 1;
size += 1;
None
@@ -189,7 +193,7 @@ class OpenHashMap[Key, Value](initialSize : Int) extends scala.collection.mutabl
}
}
- override def clone : OpenHashMap[Key, Value] = {
+ override def clone = {
val it = new OpenHashMap[Key, Value]
foreachUndeletedEntry(entry => it.put(entry.key, entry.hash, entry.value.get));
it
@@ -216,7 +220,7 @@ class OpenHashMap[Key, Value](initialSize : Int) extends scala.collection.mutabl
);
}
- private[this] def foreachUndeletedEntry(f : Entry[Key, Value] => Unit){
+ private[this] def foreachUndeletedEntry(f : Entry => Unit){
table.foreach(entry => if (entry != null && entry.value != None) f(entry));
}
override def transform(f : (Key, Value) => Value) = {