diff options
Diffstat (limited to 'src/java')
-rw-r--r-- | src/java/com/jogamp/common/util/IntIntHashMap.java | 142 |
1 files changed, 125 insertions, 17 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+"]"; + } + } } |