diff options
author | Martin Odersky <odersky@gmail.com> | 2008-04-21 10:14:06 +0000 |
---|---|---|
committer | Martin Odersky <odersky@gmail.com> | 2008-04-21 10:14:06 +0000 |
commit | ae74ec2e109a9a77d5c864a50441529d9cd5c115 (patch) | |
tree | cf185fff68b164f978cabaff53a7938406cef1f2 | |
parent | bb56c59b4c00c1b5cd9d501b63af3f18d4741670 (diff) | |
download | scala-ae74ec2e109a9a77d5c864a50441529d9cd5c115.tar.gz scala-ae74ec2e109a9a77d5c864a50441529d9cd5c115.tar.bz2 scala-ae74ec2e109a9a77d5c864a50441529d9cd5c115.zip |
added Array.fromFunction. Fixed #768 in HashSet
-rw-r--r-- | src/library/scala/Array.scala | 37 | ||||
-rw-r--r-- | src/library/scala/collection/immutable/HashSet.scala | 8 |
2 files changed, 43 insertions, 2 deletions
diff --git a/src/library/scala/Array.scala b/src/library/scala/Array.scala index edd508e6ef..05335ddfd2 100644 --- a/src/library/scala/Array.scala +++ b/src/library/scala/Array.scala @@ -184,6 +184,43 @@ object Array { a } + /** Create an array containing the values of a given function <code>f</code> + * over given range <code>[0..n)</code> + */ + def fromFunction[A](f: Int => A)(n: Int): Array[A] = { + val a = new Array[A](n) + var i = 0 + while (i < n) { + a(i) = f(i) + i += 1 + } + a + } + + /** Create an array containing the values of a given function <code>f</code> + * over given range <code>[0..n1, 0..n2)</code> + */ + def fromFunction[A](f: (Int, Int) => A)(n1: Int, n2: Int): Array[Array[A]] = + fromFunction(i => fromFunction(f(i, _))(n2))(n1) + + /** Create an array containing the values of a given function <code>f</code> + * over given range <code>[0..n1, 0..n2, 0..n3)</code> + */ + def fromFunction[A](f: (Int, Int, Int) => A)(n1: Int, n2: Int, n3: Int): Array[Array[Array[A]]] = + fromFunction(i => fromFunction(f(i, _, _))(n2, n3))(n1) + + /** Create an array containing the values of a given function <code>f</code> + * over given range <code>[0..n1, 0..n2, 0..n3, 0..n4)</code> + */ + def fromFunction[A](f: (Int, Int, Int, Int) => A)(n1: Int, n2: Int, n3: Int, n4: Int): Array[Array[Array[Array[A]]]] = + fromFunction(i => fromFunction(f(i, _, _, _))(n2, n3, n4))(n1) + + /** Create an array containing the values of a given function <code>f</code> + * over given range <code>[0..n1, 0..n2, 0..n3, 0..n4, 0..n5)</code> + */ + def fromFunction[A](f: (Int, Int, Int, Int, Int) => A)(n1: Int, n2: Int, n3: Int, n4: Int, n5: Int): Array[Array[Array[Array[Array[A]]]]] = + fromFunction(i => fromFunction(f(i, _, _, _, _))(n2, n3, n4, n5))(n1) + /** This method is called as a result of a pattern match { case Array(...) => } or val Array(...) = .... * * @param x the selector value diff --git a/src/library/scala/collection/immutable/HashSet.scala b/src/library/scala/collection/immutable/HashSet.scala index 22488ec79f..0bcc58693d 100644 --- a/src/library/scala/collection/immutable/HashSet.scala +++ b/src/library/scala/collection/immutable/HashSet.scala @@ -26,7 +26,11 @@ object HashSet { def apply[A](elems: A*) = empty[A] ++ elems } -/** This class implements immutable sets using a hash table. +/** This class implements immutable maps/sets using a hash table. + * It is optimized for sequential accesses where the last updated table is accessed most often. + * It supports with reasonable efficiency accesses to previous versions of the table by keeping + * a change log that's regularly compacted. + * It needs to synchronize most methods, so it is less suitable for highly concurrent accesses. * * @author Martin Odersky * @version 2.0, 19/01/2007 @@ -80,7 +84,7 @@ class HashSet[A] extends Set[A] with mutable.FlatHashTable[A] { cnt += 1 m = m.later } - s += tableSize + s += m.tableSize if (cnt > logLimit) makeCopy(m) s } |