diff options
author | michelou <michelou@epfl.ch> | 2006-09-12 13:08:27 +0000 |
---|---|---|
committer | michelou <michelou@epfl.ch> | 2006-09-12 13:08:27 +0000 |
commit | 3f3634c6d0e21d358bef355e6a4f28d949e6f2c8 (patch) | |
tree | 92e9df749f5ac1b131dfd6b5de691cd1a24456c8 /src/library/scala/collection/mutable/HashTable.scala | |
parent | 1874b9eba43766d3e601d8b8a5fb7ef6e58cef54 (diff) | |
download | scala-3f3634c6d0e21d358bef355e6a4f28d949e6f2c8.tar.gz scala-3f3634c6d0e21d358bef355e6a4f28d949e6f2c8.tar.bz2 scala-3f3634c6d0e21d358bef355e6a4f28d949e6f2c8.zip |
removed leading/trailing tabs/blanks in scala/L...
removed leading/trailing tabs/blanks in scala/List.scala
Diffstat (limited to 'src/library/scala/collection/mutable/HashTable.scala')
-rw-r--r-- | src/library/scala/collection/mutable/HashTable.scala | 209 |
1 files changed, 102 insertions, 107 deletions
diff --git a/src/library/scala/collection/mutable/HashTable.scala b/src/library/scala/collection/mutable/HashTable.scala index 81eb283fe0..f778ccf28b 100644 --- a/src/library/scala/collection/mutable/HashTable.scala +++ b/src/library/scala/collection/mutable/HashTable.scala @@ -9,8 +9,7 @@ // $Id$ -package scala.collection.mutable; - +package scala.collection.mutable /** This class can be used to construct data structures that are based * on hashtables. Class <code>HashTable[A]</code> implements a hashtable @@ -31,119 +30,115 @@ package scala.collection.mutable; */ trait HashTable[A] extends AnyRef { - /** The load factor for the hash table. - */ - protected val loadFactor: Float = 0.75f; - - /** The initial size of the hash table. - */ - protected val initialSize: Int = 16; - - /** The initial threshold - */ - protected val initialThreshold: Int = newThreshold(initialSize); - - /** The actual hash table. - */ - protected var table: Array[List[Entry]] = new Array(initialSize); - initTable(table); - - /** The number of mappings contained in this hash table. - */ - protected var tableSize: Int = 0; - - /** The next size value at which to resize (capacity * load factor). - */ - protected var threshold: Int = initialThreshold; - - /** Returns the size of this hash map. - */ - def size = tableSize; - - protected def findEntry(key: A): Option[Entry] = - table(index(elemHashCode(key))).find(entryFor(key)); - - protected def addEntry(e: Entry): Unit = { - val h = index(elemHashCode(entryKey(e))); - table(h) = e :: table(h); - tableSize = tableSize + 1; - if (tableSize > threshold) - resize(2 * table.length); + /** The load factor for the hash table. + */ + protected val loadFactor: Float = 0.75f + + /** The initial size of the hash table. + */ + protected val initialSize: Int = 16 + + /** The initial threshold + */ + protected val initialThreshold: Int = newThreshold(initialSize) + + /** The actual hash table. + */ + protected var table: Array[List[Entry]] = new Array(initialSize) + initTable(table) + + /** The number of mappings contained in this hash table. + */ + protected var tableSize: Int = 0 + + /** The next size value at which to resize (capacity * load factor). + */ + protected var threshold: Int = initialThreshold + + /** Returns the size of this hash map. + */ + def size = tableSize + + protected def findEntry(key: A): Option[Entry] = + table(index(elemHashCode(key))).find(entryFor(key)) + + protected def addEntry(e: Entry): Unit = { + val h = index(elemHashCode(entryKey(e))) + table(h) = e :: table(h) + tableSize = tableSize + 1 + if (tableSize > threshold) + resize(2 * table.length) + } + + protected def removeEntry(key: A): Unit = findEntry(key) match { + case None => + case Some(e) => + val idx = index(elemHashCode(key)) + table(idx) = table(idx).filter(e => !elemEquals(entryKey(e), key)) + tableSize = tableSize - 1 + } + + protected type Entry + + protected def entryKey(e: Entry): A + + protected def entries: Iterator[Entry] = new Iterator[Entry] { + val iterTable = table + var idx = table.length - 1 + var xs = iterTable(idx) + scan() + def hasNext = !xs.isEmpty + def next = { + val res = xs.head + xs = xs.tail + scan() + res } - - protected def removeEntry(key: A): Unit = { - val old = findEntry(key); - old match { - case None => - case Some(e) => { - val idx = index(elemHashCode(key)); - table(idx) = table(idx).filter(e => !elemEquals(entryKey(e), key)); - tableSize = tableSize - 1; - } - } + def scan(): Unit = if (xs.isEmpty && (idx > 0)) { + idx = idx - 1 + xs = iterTable(idx) + scan() } + } - protected type Entry; - - protected def entryKey(e: Entry): A; - - protected def entries: Iterator[Entry] = new Iterator[Entry] { - val iterTable = table; - var idx = table.length - 1; - var xs = iterTable(idx); - scan(); - def hasNext = !xs.isEmpty; - def next = { - val res = xs.head; - xs = xs.tail; - scan(); - res; - } - def scan(): Unit = if (xs.isEmpty && (idx > 0)) { - idx = idx - 1; - xs = iterTable(idx); - scan(); - } - } + private def entryFor(key: A) = { e: Entry => elemEquals(entryKey(e), key) } - private def entryFor(key: A) = { e: Entry => elemEquals(entryKey(e), key) } - - protected def initTable(tb: Array[List[Entry]]): Unit = { - var i = tb.length - 1; - while (i >= 0) { - tb(i) = Nil; - i = i - 1; - } + protected def initTable(tb: Array[List[Entry]]): Unit = { + var i = tb.length - 1 + while (i >= 0) { + tb(i) = Nil + i = i - 1 } - - private def newThreshold(size: Int) = - (size * loadFactor).asInstanceOf[Int]; - - private def resize(newSize: Int) = { - val newTable: Array[List[Entry]] = new Array(newSize); - initTable(newTable); - var i = table.length - 1; - while (i >= 0) { - table(i).foreach { e => { - val idx = improve(elemHashCode(entryKey(e))) & (newSize - 1); - newTable(idx) = e :: newTable(idx); - }}; - i = i - 1; - } - table = newTable; - threshold = newThreshold(newSize); + } + + private def newThreshold(size: Int) = + (size * loadFactor).asInstanceOf[Int] + + private def resize(newSize: Int) = { + val newTable: Array[List[Entry]] = new Array(newSize) + initTable(newTable) + var i = table.length - 1 + while (i >= 0) { + table(i).foreach { e => { + val idx = improve(elemHashCode(entryKey(e))) & (newSize - 1) + newTable(idx) = e :: newTable(idx) + }} + i = i - 1 } + table = newTable + threshold = newThreshold(newSize) + } - protected def elemEquals(key1: A, key2: A): Boolean = (key1 == key2); + protected def elemEquals(key1: A, key2: A): Boolean = (key1 == key2) - protected def elemHashCode(key: A) = key.hashCode(); + protected def elemHashCode(key: A) = key.hashCode() - protected final def improve(hcode: Int) = { - var h: Int = hcode + ~(hcode << 9); - h = h ^ (h >>> 14); - h = h + (h << 4); - h ^ (h >>> 10); - } + protected final def improve(hcode: Int) = { + var h: Int = hcode + ~(hcode << 9) + h = h ^ (h >>> 14) + h = h + (h << 4) + h ^ (h >>> 10) + } - protected final def index(hcode: Int) = improve(hcode) & (table.length - 1); + protected final def index(hcode: Int) = improve(hcode) & (table.length - 1) } |