From 4cc46f1cca8a7c97ed5fc0effa511e6705802a3f Mon Sep 17 00:00:00 2001 From: Mark Wielaard Date: Fri, 6 Apr 2007 15:56:25 +0000 Subject: 2007-04-06 Mark Wielaard * lib/mkcollections.pl.in: Add externalclasses. * java/util/Collections.java: Unroll enhanced for loops. * java/util/HashMap.java: Likewise. * java/util/Hashtable.java: Likewise. * java/util/TreeMap.java: Likewise. --- java/util/Collections.java | 32 +++++++++++++++++++++----------- java/util/HashMap.java | 16 ++++++++-------- java/util/Hashtable.java | 18 ++++++++---------- java/util/TreeMap.java | 8 ++++++-- 4 files changed, 43 insertions(+), 31 deletions(-) (limited to 'java') diff --git a/java/util/Collections.java b/java/util/Collections.java index 1c3735eab..fd802fe9d 100644 --- a/java/util/Collections.java +++ b/java/util/Collections.java @@ -1462,8 +1462,10 @@ public class Collections public static int frequency (Collection c, Object o) { int result = 0; - for (Object v : c) + final Iterator it = c.iterator(); + while (it.hasNext()) { + Object v = it.next(); if (AbstractCollection.equals(o, v)) ++result; } @@ -1524,8 +1526,9 @@ public class Collections public static boolean disjoint(Collection c1, Collection c2) { Collection oc1 = (Collection) c1; - for (Object o : oc1) - if (c2.contains(o)) + final Iterator it = oc1.iterator(); + while (it.hasNext()) + if (c2.contains(it.next())) return false; return true; } @@ -5827,8 +5830,10 @@ public class Collections public boolean addAll(Collection coll) { Collection typedColl = (Collection) c; - for (E element : typedColl) + final Iterator it = typedColl.iterator(); + while (it.hasNext()) { + final E element = it.next(); if (!type.isInstance(element)) throw new ClassCastException("A member of the collection is not of the correct type."); } @@ -6169,9 +6174,10 @@ public class Collections public boolean addAll(int index, Collection coll) { Collection typedColl = (Collection) coll; - for (E element : typedColl) + final Iterator it = typedColl.iterator(); + while (it.hasNext()) { - if (!type.isInstance(element)) + if (!type.isInstance(it.next())) throw new ClassCastException("A member of the collection is not of the correct type."); } return list.addAll(index, coll); @@ -6872,8 +6878,10 @@ public class Collections public void putAll(Map map) { Map typedMap = (Map) map; - for (Map.Entry entry : typedMap.entrySet()) + final Iterator> it = typedMap.entrySet().iterator(); + while (it.hasNext()) { + final Map.Entry entry = it.next(); if (!keyType.isInstance(entry.getKey())) throw new ClassCastException("A key is of the wrong type."); if (!valueType.isInstance(entry.getValue())) @@ -7494,8 +7502,9 @@ public class Collections public boolean addAll(Collection c) { boolean result = false; - for (T e : c) - result |= deque.offerFirst(e); + final Iterator it = c.iterator(); + while (it.hasNext()) + result |= deque.offerFirst(it.next()); return result; } @@ -7572,8 +7581,9 @@ public class Collections public boolean addAll(Collection c) { boolean result = false; - for (E e : c) - result |= (map.put(e, true) == null); + final Iterator it = c.iterator(); + while (it.hasNext()) + result |= (map.put(it.next(), true) == null); return result; } diff --git a/java/util/HashMap.java b/java/util/HashMap.java index 92022a7d5..eca3ad6aa 100644 --- a/java/util/HashMap.java +++ b/java/util/HashMap.java @@ -380,11 +380,11 @@ public class HashMap extends AbstractMap */ public void putAll(Map m) { - Map addMap; - - addMap = (Map) m; - for (Map.Entry e : addMap.entrySet()) + final Map addMap = (Map) m; + final Iterator> it = addMap.entrySet().iterator(); + while (it.hasNext()) { + final Map.Entry e = it.next(); // Optimize in case the Entry is one of our own. if (e instanceof AbstractMap.SimpleEntry) { @@ -710,12 +710,12 @@ public class HashMap extends AbstractMap */ void putAllInternal(Map m) { - Map addMap; - - addMap = (Map) m; + final Map addMap = (Map) m; + final Iterator> it = addMap.entrySet().iterator(); size = 0; - for (Map.Entry e : addMap.entrySet()) + while (it.hasNext()) { + final Map.Entry e = it.next(); size++; K key = e.getKey(); int idx = hash(key); diff --git a/java/util/Hashtable.java b/java/util/Hashtable.java index 2e265a473..20fb002b9 100644 --- a/java/util/Hashtable.java +++ b/java/util/Hashtable.java @@ -505,12 +505,11 @@ public class Hashtable extends Dictionary */ public synchronized void putAll(Map m) { - Map addMap; - - addMap = (Map) m; - - for (Map.Entry e : addMap.entrySet()) + final Map addMap = (Map) m; + final Iterator> it = addMap.entrySet().iterator(); + while (it.hasNext()) { + final Map.Entry e = it.next(); // Optimize in case the Entry is one of our own. if (e instanceof AbstractMap.SimpleEntry) { @@ -857,13 +856,12 @@ public class Hashtable extends Dictionary */ void putAllInternal(Map m) { - Map addMap; - - addMap = (Map) m; + final Map addMap = (Map) m; + final Iterator> it = addMap.entrySet().iterator(); size = 0; - - for (Map.Entry e : addMap.entrySet()) + while (it.hasNext()); { + final Map.Entry e = it.next(); size++; K key = e.getKey(); int idx = hash(key); diff --git a/java/util/TreeMap.java b/java/util/TreeMap.java index 71047cfc9..f54cbc336 100644 --- a/java/util/TreeMap.java +++ b/java/util/TreeMap.java @@ -2721,8 +2721,10 @@ public class TreeMap extends AbstractMap public String toString() { StringBuilder r = new StringBuilder("{"); - for (Entry e : entrySet()) + final Iterator> it = entrySet().iterator(); + while (it.hasNext()) { + final Entry e = it.next(); r.append(e.getKey()); r.append('='); r.append(e.getValue()); @@ -3157,8 +3159,10 @@ public class TreeMap extends AbstractMap public String toString() { StringBuilder r = new StringBuilder("["); - for (D o : this) + final Iterator it = iterator(); + while (it.hasNext()) { + final D o = it.next(); if (o == this) r.append(""); else -- cgit v1.2.1