32package com.jogamp.common.util;
34import com.jogamp.common.JogampRuntimeException;
35import java.lang.reflect.Constructor;
36import java.lang.reflect.Method;
37import java.security.PrivilegedAction;
38import java.util.ArrayList;
39import java.util.Arrays;
40import java.util.Iterator;
62 Iterable< IntObjectHashMap.Entry > {
63 private final float loadFactor;
65 private Entry[] table;
70 private int threshold;
71 private Object keyNotFoundValue =
null;
73 private static final boolean isPrimitive;
75 private static final Method equalsMethod;
77 static class EntryCM { EntryCM() { c =
null; m1 =
null; } Constructor<Entry> c; Method m1; }
80 final Class<?> valueClazz = Object.class;
81 final Class<?> keyClazz =
int.class;
83 isPrimitive = valueClazz.isPrimitive();
88 @SuppressWarnings(
"unchecked")
89 public EntryCM run() {
90 final EntryCM r =
new EntryCM();
91 r.c = (Constructor<Entry>)
93 new Class[] { keyClazz, valueClazz,
Entry.class } );
95 r.m1 = valueClazz.getDeclaredMethod(
"equals", Object.class);
96 }
catch (
final NoSuchMethodException ex) {
101 entryConstructor = cm.c;
102 equalsMethod = cm.m1;
104 entryConstructor =
null;
114 this(initialCapacity, 0.75f);
118 if (initialCapacity > 1 << 30) {
119 throw new IllegalArgumentException(
"initialCapacity is too large.");
121 if (initialCapacity < 0) {
122 throw new IllegalArgumentException(
"initialCapacity must be greater than zero.");
124 if (loadFactor <= 0) {
125 throw new IllegalArgumentException(
"loadFactor must be greater than zero.");
128 while (capacity < initialCapacity) {
131 this.loadFactor = loadFactor;
132 this.threshold = (int) (capacity * loadFactor);
133 this.table =
new Entry[capacity];
134 this.mask = capacity - 1;
137 private IntObjectHashMap(
final float loadFactor,
final int table_size,
final int size,
138 final int mask,
final int capacity,
final int threshold,
139 final Object keyNotFoundValue) {
140 this.loadFactor = loadFactor;
141 this.table =
new Entry[table_size];
145 this.capacity = capacity;
146 this.threshold = threshold;
148 this.keyNotFoundValue = keyNotFoundValue;
161 mask, capacity, threshold,
164 final ArrayList<Entry> entries =
new ArrayList<Entry>();
165 for(
int i=table.length-1; i>=0; i--) {
173 final int count = entries.size();
174 Entry de_next =
null;
175 for(
int j=count-1; j>=0; j--) {
176 se = entries.remove(j);
185 n.table[i] = de_next;
191 final Entry[] t = this.table;
192 for (
int i = t.length; i-- > 0;) {
193 for (
Entry e = t[i]; e !=
null; e = e.next) {
195 if (e.value == value) {
200 if(b.booleanValue()) {
211 final Entry[] t = this.table;
212 final int index = key & mask;
213 for (
Entry e = t[index]; e !=
null; e = e.next) {
226 public Object
get(
final int key) {
227 final Entry[] t = this.table;
228 final int index = key & mask;
229 for (
Entry e = t[index]; e !=
null; e = e.next) {
234 return keyNotFoundValue;
242 public Object
put(
final int key,
final Object value) {
243 final Entry[] t = this.table;
244 final int index = key & mask;
246 for (
Entry e = t[index]; e !=
null; e = e.next) {
250 final Object oldValue = e.
value;
254 t[index] =
new Entry(key, value, t[index]);
256 if (size++ >= threshold) {
258 final int newCapacity = 2 * capacity;
259 final Entry[] newTable =
new Entry[newCapacity];
260 final int newMask = newCapacity - 1;
261 for (
int j = 0; j < t.length; j++) {
266 final Entry next = e.next;
267 final int index2 = e.key & newMask;
268 e.next = newTable[index2];
269 newTable[index2] = e;
275 capacity = newCapacity;
276 threshold = (int) (newCapacity * loadFactor);
279 return keyNotFoundValue;
286 final Iterator<Entry> itr = source.
iterator();
287 while(itr.hasNext()) {
288 final Entry e = itr.next();
298 public Object
remove(
final int key) {
299 final Entry[] t = this.table;
300 final int index = key & mask;
301 Entry prev = t[index];
305 final Entry next = e.next;
318 return keyNotFoundValue;
339 Arrays.fill(table,
null);
349 return new EntryIterator(table);
362 final Object t = keyNotFoundValue;
363 keyNotFoundValue = newKeyNotFoundValue;
373 return keyNotFoundValue;
382 sb =
new StringBuilder();
385 final Iterator<Entry> itr = iterator();
386 while(itr.hasNext()) {
387 itr.next().toString(sb);
398 return toString(
null).toString();
401 private final static class EntryIterator
implements Iterator<Entry> {
403 private final Entry[] entries;
408 private EntryIterator(
final Entry[] entries){
409 this.entries = entries;
415 public boolean hasNext() {
420 public Entry next() {
421 final Entry current = next;
423 if(current !=
null && current.next !=
null) {
426 while(index < entries.length) {
427 final Entry e = entries[index++];
440 public void remove() {
441 throw new UnsupportedOperationException(
"Not supported yet.");
456 Entry(
final int k,
final Object v,
final Entry n) {
489 sb =
new StringBuilder();
491 sb.append(
"[").append(key).append(
":").append(value).append(
"]");
497 return toString(
null).toString();
502 private static Method getCloneMethod(
final Object obj) {
503 final Class<?> clazz = obj.getClass();
506 public Method run() {
508 return clazz.getDeclaredMethod(
"clone");
509 }
catch (
final NoSuchMethodException ex) {
A generic unchecked exception for Jogamp errors used throughout the binding as a substitute for Runti...
An entry mapping a key to a value.
Object getValue()
Returns the value of this entry.
StringBuilder toString(StringBuilder sb)
void setValue(final Object value)
Sets the value for this entry.
int getKey()
Returns the key of this entry.
Fast HashMap for primitive data.
Iterator< Entry > iterator()
Returns a new Iterator.
void putAll(final IntObjectHashMap source)
Copies all of the mappings from the specified map to this map.
Object put(final int key, final Object value)
Maps the key to the specified value.
boolean containsKey(final int key)
Object getKeyNotFoundValue()
Returns the value which is returned if no value has been found for the specified key.
StringBuilder toString(StringBuilder sb)
boolean containsValue(final Object value)
void clear()
Clears the entire map.
Object clone()
Disclaimer: If the value type doesn't implement clone(), only the reference is copied.
int size()
Returns the current number of key-value mappings in this map.
Object setKeyNotFoundValue(final Object newKeyNotFoundValue)
Sets the new key not found value.
int capacity()
Returns the current capacity (buckets) in this map.
static final Object createInstance(final Constructor<?> cstr, final Object ... cstrArgs)
static final Constructor<?> getConstructor(final String clazzName, final Class<?>[] cstrArgTypes, final boolean initializeClazz, final ClassLoader cl)
static final Object callMethod(final Object instance, final Method method, final Object ... args)
static< T > T doPrivileged(final PrivilegedAction< T > o)
Call wrapper for java.security.AccessController#doPrivileged(PrivilegedAction).