diff options
3 files changed, 186 insertions, 21 deletions
diff --git a/src/java/com/jogamp/common/util/IntIntHashMap.java b/src/java/com/jogamp/common/util/IntIntHashMap.java index 1288ad4..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; @@ -88,19 +90,6 @@ public class /*name*/IntIntHashMap/*name*/ { this.mask = capacity - 1; } - /** - * Sets the new key not found value. - * For primitive types (int, long) the default is -1, - * for Object types, the default is null. - * - * @return the previous key not found value - */ - public /*value*/int/*value*/ setKeyNotFoundValue(/*value*/int/*value*/ newKeyNotFoundValue) { - /*value*/int/*value*/ t = keyNotFoundValue; - keyNotFoundValue = newKeyNotFoundValue; - return t; - } - public boolean containsValue(/*value*/int/*value*/ value) { Entry[] table = this.table; for (int i = table.length; i-- > 0;) { @@ -124,6 +113,10 @@ public class /*name*/IntIntHashMap/*name*/ { return false; } + /** + * Returns the value to which the specified key is mapped, + * or {@link #getKeyNotFoundValue} if this map contains no mapping for the key. + */ // @SuppressWarnings(value="cast") public /*value*/int/*value*/ get(/*key*/int/*key*/ key) { int index = (int) (key & mask); @@ -135,6 +128,10 @@ public class /*name*/IntIntHashMap/*name*/ { return keyNotFoundValue; } + /** + * Maps the key to the specified value. If a mapping to this key already exists, + * the previous value will be returned (otherwise {@link #getKeyNotFoundValue}). + */ // @SuppressWarnings(value="cast") public /*value*/int/*value*/ put(/*key*/int/*key*/ key, /*value*/int/*value*/ value) { int index = (int) (key & mask); @@ -175,6 +172,10 @@ public class /*name*/IntIntHashMap/*name*/ { return keyNotFoundValue; } + /** + * Removes the key-value mapping from this map. + * 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) { int index = (int) (key & mask); @@ -209,10 +210,103 @@ public class /*name*/IntIntHashMap/*name*/ { size = 0; } - private final static class Entry { +// @Override + public Iterator/*<Entry>*/ iterator() { + return new EntryIterator(this.table); + } - private final /*key*/int/*key*/ key; - private /*value*/int/*value*/ value; + /** + * Sets the new key not found value. + * For primitive types (int, long) the default is -1, + * for Object types, the default is null. + * + * @return the previous key not found value + * @see #get + * @see #put + */ + public /*value*/int/*value*/ setKeyNotFoundValue(/*value*/int/*value*/ newKeyNotFoundValue) { + /*value*/int/*value*/ t = keyNotFoundValue; + keyNotFoundValue = newKeyNotFoundValue; + return t; + } + + /** + * Returns the value which is returned if no value has been found for the specified key. + * @see #get + * @see #put + */ + public /*value*/int/*value*/ getKeyNotFoundValue() { + return keyNotFoundValue; + } + +// @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 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) { @@ -220,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]); } |