summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorPaul Phillips <paulp@improving.org>2009-12-01 18:28:37 +0000
committerPaul Phillips <paulp@improving.org>2009-12-01 18:28:37 +0000
commitc2359ccec521ed24641fb010774e7b39b4ae62b2 (patch)
tree3bebe1d11fa8448b5bd3f7ef187fe5277f8a0d9c
parenta55310838b97a1e55d6804e93e5080d8187994b1 (diff)
downloadscala-c2359ccec521ed24641fb010774e7b39b4ae62b2.tar.gz
scala-c2359ccec521ed24641fb010774e7b39b4ae62b2.tar.bz2
scala-c2359ccec521ed24641fb010774e7b39b4ae62b2.zip
Implement foreachEntry by calling iteratorEntri...
Implement foreachEntry by calling iteratorEntries.foreach due to an edge-case. The comment in the method explains the issue in detail.
-rw-r--r--src/library/scala/collection/mutable/HashTable.scala26
1 files changed, 14 insertions, 12 deletions
diff --git a/src/library/scala/collection/mutable/HashTable.scala b/src/library/scala/collection/mutable/HashTable.scala
index db4e100634..fa3865ada2 100644
--- a/src/library/scala/collection/mutable/HashTable.scala
+++ b/src/library/scala/collection/mutable/HashTable.scala
@@ -144,18 +144,20 @@ trait HashTable[A] {
}
}
- protected final def foreachEntry[C](f: Entry => C) {
- val t = table
- var index = t.length - 1
- while (index >= 0) {
- var entry = t(index)
- while (entry ne null) {
- f(entry.asInstanceOf[Entry])
- entry = entry.next
- }
- index -= 1
- }
- }
+ /*
+ * We should implement this as a primitive operation over the underlying array, but it can
+ * cause a behaviour change in edge cases where:
+ * - Someone modifies a map during iteration
+ * - The insertion point is close to the iteration point.
+ *
+ * The reason this happens is that the iterator prefetches the following element before
+ * returning from next (to simplify the implementation of hasNext) while the natural
+ * implementation of foreach does not.
+ *
+ * It should be mentioned that modifying a map during iteration leads to unpredictable
+ * results with either implementation.
+ */
+ protected final def foreachEntry[C](f: Entry => C) { entriesIterator.foreach(f) }
/** An iterator returning all entries */
@deprecated("use entriesIterator instead")