org.greenrobot.greendao.internal.LongHashMap Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of greendao Show documentation
Show all versions of greendao Show documentation
greenDAO is a light and fast ORM for Android
/*
* Copyright (C) 2011-2016 Markus Junginger, greenrobot (http://greenrobot.org)
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.greenrobot.greendao.internal;
import java.util.Arrays;
import org.greenrobot.greendao.DaoLog;
/**
* An minimalistic hash map optimized for long keys.
*
* @author Markus
*
* @param
* The class to store.
*/
public final class LongHashMap {
final static class Entry {
final long key;
T value;
Entry next;
Entry(long key, T value, Entry next) {
this.key = key;
this.value = value;
this.next = next;
}
}
private Entry[] table;
private int capacity;
private int threshold;
private int size;
public LongHashMap() {
this(16);
}
@SuppressWarnings("unchecked")
public LongHashMap(int capacity) {
this.capacity = capacity;
this.threshold = capacity * 4 / 3;
this.table = new Entry[capacity];
}
public boolean containsKey(long key) {
final int index = ((((int) (key >>> 32)) ^ ((int) (key))) & 0x7fffffff) % capacity;
for (Entry entry = table[index]; entry != null; entry = entry.next) {
if (entry.key == key) {
return true;
}
}
return false;
}
public T get(long key) {
final int index = ((((int) (key >>> 32)) ^ ((int) (key))) & 0x7fffffff) % capacity;
for (Entry entry = table[index]; entry != null; entry = entry.next) {
if (entry.key == key) {
return entry.value;
}
}
return null;
}
public T put(long key, T value) {
final int index = ((((int) (key >>> 32)) ^ ((int) (key))) & 0x7fffffff) % capacity;
final Entry entryOriginal = table[index];
for (Entry entry = entryOriginal; entry != null; entry = entry.next) {
if (entry.key == key) {
T oldValue = entry.value;
entry.value = value;
return oldValue;
}
}
table[index] = new Entry(key, value, entryOriginal);
size++;
if (size > threshold) {
setCapacity(2 * capacity);
}
return null;
}
public T remove(long key) {
int index = ((((int) (key >>> 32)) ^ ((int) (key))) & 0x7fffffff) % capacity;
Entry previous = null;
Entry entry = table[index];
while (entry != null) {
Entry next = entry.next;
if (entry.key == key) {
if (previous == null) {
table[index] = next;
} else {
previous.next = next;
}
size--;
return entry.value;
}
previous = entry;
entry = next;
}
return null;
}
public void clear() {
size = 0;
Arrays.fill(table, null);
}
public int size() {
return size;
}
public void setCapacity(int newCapacity) {
@SuppressWarnings("unchecked")
Entry[] newTable = new Entry[newCapacity];
int length = table.length;
for (int i = 0; i < length; i++) {
Entry entry = table[i];
while (entry != null) {
long key = entry.key;
int index = ((((int) (key >>> 32)) ^ ((int) (key))) & 0x7fffffff) % newCapacity;
Entry originalNext = entry.next;
entry.next = newTable[index];
newTable[index] = entry;
entry = originalNext;
}
}
table = newTable;
capacity = newCapacity;
threshold = newCapacity * 4 / 3;
}
/** Target load: 0,6 */
public void reserveRoom(int entryCount) {
setCapacity(entryCount * 5 / 3);
}
public void logStats() {
int collisions = 0;
for (Entry entry : table) {
while (entry != null && entry.next != null) {
collisions++;
entry = entry.next;
}
}
DaoLog.d("load: " + ((float) size) / capacity + ", size: " + size + ", capa: " + capacity + ", collisions: "
+ collisions + ", collision ratio: " + ((float) collisions) / size);
}
}
© 2015 - 2025 Weber Informatics LLC | Privacy Policy