diff options
3 files changed, 151 insertions, 9 deletions
diff --git a/src/java/com/jogamp/common/util/IntIntHashMap.java b/src/java/com/jogamp/common/util/IntIntHashMap.java index 1f99944..2dff20b 100644 --- a/src/java/com/jogamp/common/util/IntIntHashMap.java +++ b/src/java/com/jogamp/common/util/IntIntHashMap.java @@ -30,6 +30,8 @@ */ package com.jogamp.common.util; +import java.util.Iterator; + /* * Note: this map is used as template for other maps. */ @@ -48,7 +50,7 @@ package com.jogamp.common.util; * @see LongLongHashMap * @see LongIntHashMap */ -public class /*name*/IntIntHashMap/*name*/ { +public class /*name*/IntIntHashMap/*name*/ implements Iterable { private final float loadFactor; @@ -172,7 +174,7 @@ public class /*name*/IntIntHashMap/*name*/ { /** * Removes the key-value mapping from this map. - * Returns the priviously mapped value or {@link #getKeyNotFoundValue} if no such mapping exists. + * Returns the previously mapped value or {@link #getKeyNotFoundValue} if no such mapping exists. */ // @SuppressWarnings(value="cast") public /*value*/int/*value*/ remove(/*key*/int/*key*/ key) { @@ -208,6 +210,11 @@ public class /*name*/IntIntHashMap/*name*/ { size = 0; } +// @Override + public Iterator/*<Entry>*/ iterator() { + return new EntryIterator(this.table); + } + /** * Sets the new key not found value. * For primitive types (int, long) the default is -1, @@ -232,10 +239,74 @@ public class /*name*/IntIntHashMap/*name*/ { return keyNotFoundValue; } - private final static class Entry { +// @Override + public String toString() { + // TODO use StringBuilder as soon we are at language level 5 + String str = "{"; + Iterator itr = iterator(); + while(itr.hasNext()) { + str += itr.next(); + if(itr.hasNext()) { + str += ", "; + } + } + str += "}"; + return str; + } + + private final static class EntryIterator implements Iterator/*<Entry>*/ { - private final /*key*/int/*key*/ key; - private /*value*/int/*value*/ value; + private final Entry[] entries; + + private int index; + private Entry next; + + private EntryIterator(Entry[] entries){ + this.entries = entries; + // load next + next(); + } + +// @Override + public boolean hasNext() { + return next != null; + } + +// @Override + public Object next() { + Entry current = next; + + if(current != null && current.next != null) { + next = current.next; + }else{ + while(index < entries.length) { + Entry e = entries[index++]; + if(e != null) { + next = e; + return current; + } + } + next = null; + } + + return current; + } + +// @Override + public void remove() { + throw new UnsupportedOperationException("Not supported yet."); + } + + } + + /** + * An entry mapping a key to a value. + */ + public final static class Entry { + + public final /*key*/int/*key*/ key; + public /*value*/int/*value*/ value; + private Entry next; private Entry(/*key*/int/*key*/ k, /*value*/int/*value*/ v, Entry n) { @@ -243,5 +314,19 @@ public class /*name*/IntIntHashMap/*name*/ { value = v; next = n; } + + public /*key*/int/*key*/ getKey() { + return key; + } + + public /*value*/int/*value*/ getValue() { + return value; + } + +// @Override + public String toString() { + return "["+key+":"+value+"]"; + } + } } diff --git a/test/junit/com/jogamp/common/util/IntIntHashMapTest.java b/test/junit/com/jogamp/common/util/IntIntHashMapTest.java index 18eeef6..f041123 100644 --- a/test/junit/com/jogamp/common/util/IntIntHashMapTest.java +++ b/test/junit/com/jogamp/common/util/IntIntHashMapTest.java @@ -4,6 +4,7 @@ package com.jogamp.common.util; import java.util.HashMap; +import java.util.Iterator; import java.util.Map.Entry; import java.util.Random; import org.junit.BeforeClass; @@ -39,6 +40,7 @@ public class IntIntHashMapTest { } } + /** * Test of put method, of class IntIntHashMap. */ @@ -75,6 +77,31 @@ public class IntIntHashMapTest { } } + + @Test + public void iteratorTest() { + + final IntIntHashMap intmap = new IntIntHashMap(iterations); + + for (int i = 0; i < iterations; i++) { + intmap.put(rndKeys[i], rndValues[i]); + } + + Iterator iterator = intmap.iterator(); + assertNotNull(iterator); + assertTrue(iterator.hasNext()); + + int n = 0; + while (iterator.hasNext()) { + IntIntHashMap.Entry entry = (IntIntHashMap.Entry)iterator.next(); + assertNotNull(entry); + n++; + } + assertEquals(intmap.size(), n); + +// out.println(intmap); + + } @Test public void benchmark() { @@ -83,6 +110,8 @@ public class IntIntHashMapTest { final IntIntHashMap intmap = new IntIntHashMap(1024); final HashMap<Integer, Integer> map = new HashMap<Integer, Integer>(1024); + out.println(intmap.getClass().getName()+" vs "+map.getClass().getName()); + out.println("put"); long time = currentTimeMillis(); for (int i = 0; i < iterations; i++) { diff --git a/test/junit/com/jogamp/common/util/LongIntHashMapTest.java b/test/junit/com/jogamp/common/util/LongIntHashMapTest.java index 67a610f..53aa252 100644 --- a/test/junit/com/jogamp/common/util/LongIntHashMapTest.java +++ b/test/junit/com/jogamp/common/util/LongIntHashMapTest.java @@ -3,6 +3,7 @@ */ package com.jogamp.common.util; +import java.util.Iterator; import java.util.HashMap; import java.util.Map.Entry; import java.util.Random; @@ -78,19 +79,46 @@ public class LongIntHashMapTest { } @Test + public void iteratorTest() { + + final LongIntHashMap map = new LongIntHashMap(iterations); + + for (int i = 0; i < iterations; i++) { + map.put(rndKeys[i], rndValues[i]); + } + + Iterator iterator = map.iterator(); + assertNotNull(iterator); + assertTrue(iterator.hasNext()); + + int n = 0; + while (iterator.hasNext()) { + LongIntHashMap.Entry entry = (LongIntHashMap.Entry)iterator.next(); + assertNotNull(entry); + n++; + } + assertEquals(map.size(), n); + +// out.println(intmap); + + } + + @Test public void benchmark() { // simple benchmark - final LongIntHashMap intmap = new LongIntHashMap(1024); + final LongIntHashMap intmap = new LongIntHashMap(1024); final HashMap<Long, Integer> map = new HashMap<Long, Integer>(1024); + out.println(intmap.getClass().getName()+" vs "+map.getClass().getName()); + out.println("put"); long time = currentTimeMillis(); for (int i = 0; i < iterations; i++) { intmap.put(rndKeys[i], rndValues[i]); } long intmapTime = (currentTimeMillis() - time); - out.println(" iimap: " + intmapTime+"ms"); + out.println(" limap: " + intmapTime+"ms"); time = currentTimeMillis(); @@ -106,7 +134,7 @@ public class LongIntHashMapTest { System.out.println(); System.out.println("get"); intmapTime = (currentTimeMillis() - time); - out.println(" iimap: " + intmapTime+"ms"); + out.println(" limap: " + intmapTime+"ms"); for (int i = 0; i < iterations; i++) { intmap.get(rndValues[i]); } @@ -122,7 +150,7 @@ public class LongIntHashMapTest { out.println(); out.println("remove"); intmapTime = (currentTimeMillis() - time); - out.println(" iimap: " + intmapTime+"ms"); + out.println(" limap: " + intmapTime+"ms"); for (int i = 0; i < iterations; i++) { intmap.remove(rndValues[i]); } |