From 88112252043c41b5bb3e232d2a3c65dba2188c31 Mon Sep 17 00:00:00 2001 From: Michael Bien Date: Wed, 21 Apr 2010 18:48:04 +0200 Subject: added getKeyNotFoundValue() and some javadoc to IntIntHashMap. --- src/java/com/jogamp/common/util/IntIntHashMap.java | 49 ++++++++++++++++------ 1 file changed, 36 insertions(+), 13 deletions(-) (limited to 'src/java/com/jogamp') diff --git a/src/java/com/jogamp/common/util/IntIntHashMap.java b/src/java/com/jogamp/common/util/IntIntHashMap.java index 1288ad4..1f99944 100644 --- a/src/java/com/jogamp/common/util/IntIntHashMap.java +++ b/src/java/com/jogamp/common/util/IntIntHashMap.java @@ -88,19 +88,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 +111,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 +126,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 +170,10 @@ public class /*name*/IntIntHashMap/*name*/ { return keyNotFoundValue; } + /** + * Removes the key-value mapping from this map. + * Returns the priviously 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,6 +208,30 @@ public class /*name*/IntIntHashMap/*name*/ { size = 0; } + /** + * 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; + } + private final static class Entry { private final /*key*/int/*key*/ key; -- cgit v1.2.3 From 1babc3207da7210d7d70c47307d91db496109f0d Mon Sep 17 00:00:00 2001 From: Michael Bien Date: Wed, 21 Apr 2010 21:38:37 +0200 Subject: IntIntHashMap (and friends) is now Iterable. Added iterator testcase. --- src/java/com/jogamp/common/util/IntIntHashMap.java | 95 ++++++++++++++++++++-- .../com/jogamp/common/util/IntIntHashMapTest.java | 29 +++++++ .../com/jogamp/common/util/LongIntHashMapTest.java | 36 +++++++- 3 files changed, 151 insertions(+), 9 deletions(-) (limited to 'src/java/com/jogamp') 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/**/ 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/**/ { - 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 map = new HashMap(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; @@ -77,20 +78,47 @@ 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 map = new HashMap(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]); } -- cgit v1.2.3