com.questdb.std.ObjHashSet Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of questdb-core Show documentation
Show all versions of questdb-core Show documentation
QuestDB is High Performance Time Series Database
/*******************************************************************************
* ___ _ ____ ____
* / _ \ _ _ ___ ___| |_| _ \| __ )
* | | | | | | |/ _ \/ __| __| | | | _ \
* | |_| | |_| | __/\__ \ |_| |_| | |_) |
* \__\_\\__,_|\___||___/\__|____/|____/
*
* Copyright (C) 2014-2016 Appsicle
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Affero General Public License, version 3,
* as published by the Free Software Foundation.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Affero General Public License for more details.
*
* You should have received a copy of the GNU Affero General Public License
* along with this program. If not, see .
*
******************************************************************************/
package com.questdb.std;
import com.questdb.misc.Numbers;
import com.questdb.misc.Unsafe;
import org.jetbrains.annotations.NotNull;
import java.util.AbstractSet;
import java.util.Arrays;
import java.util.Iterator;
public class ObjHashSet extends AbstractSet implements Mutable {
private static final int MIN_INITIAL_CAPACITY = 16;
private static final Object noEntryValue = new Object();
private final double loadFactor;
private final ObjList list;
private T[] keys;
private int free;
private int capacity;
private int mask;
public ObjHashSet() {
this(MIN_INITIAL_CAPACITY);
}
public ObjHashSet(int initialCapacity) {
this(initialCapacity, 0.4f, 0.3f);
}
@SuppressWarnings("unchecked")
private ObjHashSet(int initialCapacity, double loadFactor, double hashFactor) {
if (loadFactor <= 0d || loadFactor >= 1d) {
throw new IllegalArgumentException("0 < loadFactor < 1");
}
if (hashFactor <= 0d || hashFactor >= 1d) {
throw new IllegalArgumentException("0 < hashFactor < 1");
}
initialCapacity = (int) (initialCapacity * (1 + hashFactor));
int capacity = Math.max(initialCapacity, (int) (initialCapacity / loadFactor));
this.loadFactor = loadFactor;
keys = (T[]) new Object[capacity < MIN_INITIAL_CAPACITY ? MIN_INITIAL_CAPACITY : Numbers.ceilPow2(capacity)];
mask = keys.length - 1;
free = this.capacity = initialCapacity;
this.list = new ObjList<>(free);
clear();
}
public void addAll(ObjHashSet that) {
for (int i = 0, n = that.size(); i < n; i++) {
this.add(that.get(i));
}
}
public T get(int index) {
return list.getQuick(index);
}
@Override
@NotNull
public Iterator iterator() {
return list.iterator();
}
public int size() {
return capacity - free;
}
public boolean add(T key) {
boolean r = insertKey(key);
if (r) {
list.add(key);
if (free == 0) {
rehash();
}
}
return r;
}
@Override
@SuppressWarnings("unchecked")
public boolean remove(Object key) {
if (list.remove(key) > -1) {
int index = idx((T) key);
if (key.equals(Unsafe.arrayGet(keys, index))) {
Unsafe.arrayPut(keys, index, noEntryValue);
free++;
return true;
}
return probeRemove(key, index);
}
return false;
}
public final void clear() {
free = capacity;
Arrays.fill(keys, noEntryValue);
list.clear();
}
@Override
public String toString() {
return list.toString();
}
private int idx(T key) {
return key == null ? 0 : (key.hashCode() & mask);
}
private boolean insertKey(T key) {
int index = idx(key);
if (Unsafe.arrayGet(keys, index) == noEntryValue) {
Unsafe.arrayPut(keys, index, key);
free--;
return true;
} else {
return !(key == Unsafe.arrayGet(keys, index) || key.equals(Unsafe.arrayGet(keys, index))) && probeInsert(key, index);
}
}
private boolean probeInsert(T key, int index) {
do {
index = (index + 1) & mask;
if (Unsafe.arrayGet(keys, index) == noEntryValue) {
Unsafe.arrayPut(keys, index, key);
free--;
return true;
}
if (key == Unsafe.arrayGet(keys, index) || key.equals(Unsafe.arrayGet(keys, index))) {
return false;
}
} while (true);
}
private boolean probeRemove(Object key, int index) {
int i = index;
do {
index = (index + 1) & mask;
if (key.equals(Unsafe.arrayGet(keys, index))) {
Unsafe.arrayPut(keys, index, noEntryValue);
free++;
return true;
}
} while (i != index);
return false;
}
@SuppressWarnings({"unchecked"})
private void rehash() {
int newCapacity = keys.length << 1;
mask = newCapacity - 1;
free = capacity = (int) (newCapacity * loadFactor);
T[] oldKeys = keys;
this.keys = (T[]) new Object[newCapacity];
Arrays.fill(keys, noEntryValue);
for (int i = oldKeys.length; i-- > 0; ) {
if (Unsafe.arrayGet(oldKeys, i) != noEntryValue) {
insertKey(Unsafe.arrayGet(oldKeys, i));
}
}
}
}