Thanks for your replies! It made me rethink how I should do this. I have some questions to the code Dx4 and counterp provided.
public class TileHashtable {
public class Entry {
Entry next;
int x;
int y;
int hash;
UserLayer.Tile value;
public Entry(Entry next, int x, int y, int hash, Tile value) {
this.next = next;
this.x = x;
this.y = y;
this.hash = hash;
this.value = value;
}
int hash() {
return (x << 5) + y;
}
public int getHash() {
return hash;
}
public Entry getNext() {
return next;
}
public Tile getValue() {
return value;
}
public int getX() {
return x;
}
public int getY() {
return y;
}
}
int size;
Entry [] table;
public TileHashtable() {
table = new Entry[64];
}
static int hash(int h) {
// This function ensures that hashCodes that differ only by
// constant multiples at each bit position have a bounded
// number of collisions (approximately 8 at default load factor).
h ^= (h >>> 20) ^ (h >>> 12);
return h ^ (h >>> 7) ^ (h >>> 4);
}
static int getPointHash(int x, int y) {
return hash((x & 0xFFFF) << 16) | (y & 0xFFFF);
}
static int indexFor(int h, int length) {
return h & (length-1);
}
public void clear() {
for (int i = 0; i < table.length; i++) {
table[i] = null;
}
size = 0;
}
public UserLayer.Tile lookup(int x, int y, boolean createIfNotExist) {
int hash = getPointHash(x, y);
Entry e;
int index = indexFor(hash, table.length);
for (e = table[index]; e != null; e = e.next) {
if (e.hash == hash && e.x == x && e.y == y) {
return e.value;
}
}
if (createIfNotExist) {
UserLayer.Tile val = new UserLayer.Tile();
table[index] = new Entry(table[index], x, y, hash, val);
size++;
return val;
}
return null;
}
public int size() {
return size;
}
public boolean isEmpty() {
return size == 0;
}
public Iterator<Entry> entryIterator() {
return new EntryIterator();
}
public Iterator<UserLayer.Tile> valueIterator() {
return new ValueIterator();
}
public Iterator<Void> keyIterator() {
return new KeyIterator();
}
private abstract class HashIterator<E> implements Iterator<E> {
Entry next; // next entry to return
int index; // current slot
Entry current; // current entry
HashIterator() {
if (size > 0) { // advance to first entry
Entry[] t = table;
while (index < t.length && (next = t[index++]) == null)
;
}
}
public final boolean hasNext() {
return next != null;
}
final Entry nextEntry() {
Entry e = next;
if (e == null)
throw new NoSuchElementException();
if ((next = e.next) == null) {
Entry[] t = table;
while (index < t.length && (next = t[index++]) == null)
;
}
current = e;
return e;
}
public void remove() {
throw new UnsupportedOperationException("My Dick");
}
}
private final class ValueIterator extends HashIterator<UserLayer.Tile> {
public UserLayer.Tile next() {
return nextEntry().value;
}
}
private final class KeyIterator extends HashIterator<Void> {
public void next(Point p) {
}
public Void next() {
throw new UnsupportedOperationException("My Dick");
}
}
private final class EntryIterator extends HashIterator<Entry> {
public Entry next() {
return nextEntry();
}
}
}
static int getPointHash(int x, int y) {
return hash((x & 0xFFFF) << 16) | (y & 0xFFFF);
}
Why is it that we have to do that AND operation on 0xFFF? To me, it looks like it will return the same, if we exclude that. Also, I don’t see a change if I exchange the OR operation with a “+”. Can you please explain this?
static int hash(int h) {
// This function ensures that hashCodes that differ only by
// constant multiples at each bit position have a bounded
// number of collisions (approximately 8 at default load factor).
h ^= (h >>> 20) ^ (h >>> 12);
return h ^ (h >>> 7) ^ (h >>> 4);
}
This also I can’t seem to get much meaning out of. I’m unsure what this ensures, and I’d very much like a more in-depth explanation that the comment provided :clue:
int hash() {
return (x << 5) + y;
}
This again makes me feel like I don’t really know what the hashes do. I though we’d use the last 16 bits to define y, and the first 16 ones to define x. Also, what happended to the AND-operation with the 0xFFFF byte?
Thank you very much for your replies guys, I will be sure to Appriciate after I just hit post 