diff options
author | Martin Odersky <odersky@gmail.com> | 2010-05-31 17:23:38 +0000 |
---|---|---|
committer | Martin Odersky <odersky@gmail.com> | 2010-05-31 17:23:38 +0000 |
commit | 6e3814fe9e5c61601db81f6a46f3f5147c6c0e5f (patch) | |
tree | 45344e4aa2a7edcc2f81a68aaac886f6c2bf52f9 /src | |
parent | 0b006e7762a8c3ec2f5d02c8c7c34b09511e6a47 (diff) | |
download | scala-6e3814fe9e5c61601db81f6a46f3f5147c6c0e5f.tar.gz scala-6e3814fe9e5c61601db81f6a46f3f5147c6c0e5f.tar.bz2 scala-6e3814fe9e5c61601db81f6a46f3f5147c6c0e5f.zip |
made hashset more robust for concurrent access ...
made hashset more robust for concurrent access to reduce eclipse race
conditions.
Diffstat (limited to 'src')
-rw-r--r-- | src/compiler/scala/tools/nsc/util/HashSet.scala | 25 |
1 files changed, 11 insertions, 14 deletions
diff --git a/src/compiler/scala/tools/nsc/util/HashSet.scala b/src/compiler/scala/tools/nsc/util/HashSet.scala index aa6e19538c..8e0c2e2e59 100644 --- a/src/compiler/scala/tools/nsc/util/HashSet.scala +++ b/src/compiler/scala/tools/nsc/util/HashSet.scala @@ -11,19 +11,17 @@ class HashSet[T >: Null <: AnyRef](val label: String, initialCapacity: Int) exte def this(label: String) = this(label, 16) def this() = this(16) - private var capacity = initialCapacity private var used = 0 - private var table = new Array[AnyRef](capacity) + private var table = new Array[AnyRef](initialCapacity) // System.err.println("Created: " + this) def size: Int = used def clear() { - capacity = initialCapacity used = 0 - table = new Array[AnyRef](capacity) + table = new Array[AnyRef](initialCapacity) } - private def index(x: Int): Int = math.abs(x % capacity) + private def index(x: Int): Int = math.abs(x % table.length) def findEntryOrUpdate(x: T): T = { var h = index(x.##) @@ -37,7 +35,7 @@ class HashSet[T >: Null <: AnyRef](val label: String, initialCapacity: Int) exte } table(h) = x used += 1 - if (used > (capacity >> 2)) growTable() + if (used > (table.length >> 2)) growTable() x } @@ -61,14 +59,14 @@ class HashSet[T >: Null <: AnyRef](val label: String, initialCapacity: Int) exte } table(h) = x used += 1 - if (used > (capacity >> 2)) growTable() + if (used > (table.length >> 2)) growTable() } def iterator = new Iterator[T] { private var i = 0 def hasNext: Boolean = { - while (i < capacity && (table(i) eq null)) i += 1 - i < capacity + while (i < table.length && (table(i) eq null)) i += 1 + i < table.length } def next: T = if (hasNext) { i += 1; table(i - 1).asInstanceOf[T] } @@ -88,12 +86,11 @@ class HashSet[T >: Null <: AnyRef](val label: String, initialCapacity: Int) exte private def growTable() { val oldtable = table val growthFactor = - if (capacity <= initialCapacity) 8 - else if (capacity <= (initialCapacity * 8)) 4 + if (table.length <= initialCapacity) 8 + else if (table.length <= (initialCapacity * 8)) 4 else 2 - capacity *= growthFactor - table = new Array[AnyRef](capacity) + table = new Array[AnyRef](table.length * growthFactor) var i = 0 while (i < oldtable.length) { val entry = oldtable(i) @@ -101,5 +98,5 @@ class HashSet[T >: Null <: AnyRef](val label: String, initialCapacity: Int) exte i += 1 } } - override def toString() = "HashSet %s(%d / %d)".format(label, used, capacity) + override def toString() = "HashSet %s(%d / %d)".format(label, used, table.length) } |