All Downloads are FREE. Search and download functionalities are using the official Maven repository.

com.firefly.utils.collection.IdentityHashMap Maven / Gradle / Ivy

There is a newer version: 4.0.3.2
Show newest version
package com.firefly.utils.collection;

@SuppressWarnings("unchecked")
public class IdentityHashMap {

	public static final int DEFAULT_TABLE_SIZE = 1024;

	private final Entry[] buckets;
	private final int indexMask;

	public IdentityHashMap() {
		this(DEFAULT_TABLE_SIZE);
	}

	public IdentityHashMap(int tableSize) {
		this.indexMask = tableSize - 1;
		this.buckets = new Entry[tableSize];
	}

	public final V get(K key) {
		final int hash = System.identityHashCode(key);
		final int bucket = hash & indexMask;

		for (Entry entry = buckets[bucket]; entry != null; entry = entry.next) {
			if (key == entry.key) {
				return entry.value;
			}
		}

		return null;
	}

	public boolean put(K key, V value) {
		final int hash = System.identityHashCode(key);
		final int bucket = hash & indexMask;

		for (Entry entry = buckets[bucket]; entry != null; entry = entry.next) {
			if (key == entry.key) {
				return true;
			}
		}

		Entry entry = new Entry(key, value, hash, buckets[bucket]);
		buckets[bucket] = entry; // If multi-thread visits this method, perhaps the entry will be replaced by other entries.
		return false;
	}

	public int size() {
		int size = 0;
		for (int i = 0; i < buckets.length; ++i) {
			for (Entry entry = buckets[i]; entry != null; entry = entry.next) {
				size++;
			}
		}
		return size;
	}

	protected static final class Entry {
		public final int hashCode;
		public final K key;
		public final V value;

		public final Entry next;

		public Entry(K key, V value, int hash, Entry next) {
			this.key = key;
			this.value = value;
			this.next = next;
			this.hashCode = hash;
		}
	}

}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy