This is the mail archive of the
java-patches@gcc.gnu.org
mailing list for the Java project.
Re: libgcj/2357: HashMap does not handle initialCapacity=0 correctly
- To: mark at klomp dot org
- Subject: Re: libgcj/2357: HashMap does not handle initialCapacity=0 correctly
- From: Bryce McKinlay <bryce at albatross dot co dot nz>
- Date: Sat, 24 Mar 2001 20:10:23 +1200
- CC: gcc-gnats at gcc dot gnu dot org, java-patches at gcc dot gnu dot org
- References: <20010323121731.25071.qmail@sourceware.cygnus.com>
mark@klomp.org wrote:
> >Description:
> When the initialCapacity of a HashMap is zero then bucket.length will be zero and then you will get a ArithmetichException in the hash() method.
I've checked in this fix. It seems better to simply bump the initialCapacity to 1 if 0 is given, than to add a special case in hash(), making it slower.
regards
[ bryce ]
2001-03-24 Bryce McKinlay <bryce@albatross.co.nz>
* java/util/HashMap.java (HashMap): If 0 is given for initialCapacity
paramater, bump it to 1.
* java/util/Hashtable.java (Hashtable): Likewise.
Index: Hashtable.java
===================================================================
RCS file: /cvs/gcc/egcs/libjava/java/util/Hashtable.java,v
retrieving revision 1.10.4.1
diff -u -r1.10.4.1 Hashtable.java
--- Hashtable.java 2001/02/22 04:26:06 1.10.4.1
+++ Hashtable.java 2001/03/24 08:00:18
@@ -179,7 +179,9 @@
+ initialCapacity);
if (loadFactor <= 0)
throw new IllegalArgumentException("Illegal Load Factor: " + loadFactor);
-
+
+ if (initialCapacity == 0)
+ initialCapacity = 1;
buckets = new Entry[initialCapacity];
this.loadFactor = loadFactor;
this.threshold = (int) (initialCapacity * loadFactor);
Index: HashMap.java
===================================================================
RCS file: /cvs/gcc/egcs/libjava/java/util/HashMap.java,v
retrieving revision 1.4.4.2
diff -u -r1.4.4.2 HashMap.java
--- HashMap.java 2001/02/22 04:26:06 1.4.4.2
+++ HashMap.java 2001/03/24 08:00:18
@@ -157,7 +157,7 @@
* @param loadFactor the load factor
*
* @throws IllegalArgumentException if (initialCapacity < 0) ||
- * (initialLoadFactor > 1.0) ||
+ * (loadFactor <= 0)
*/
public HashMap(int initialCapacity, float loadFactor)
throws IllegalArgumentException
@@ -167,7 +167,9 @@
+ initialCapacity);
if (loadFactor <= 0)
throw new IllegalArgumentException("Illegal Load Factor: " + loadFactor);
-
+
+ if (initialCapacity == 0)
+ initialCapacity = 1;
buckets = new Entry[initialCapacity];
this.loadFactor = loadFactor;
this.threshold = (int) (initialCapacity * loadFactor);