Many resources are needed to download a project. Please understand that we have to compensate our server costs. Thank you in advance. Project price only 1 $
You can buy this project and download/modify it how often you want.
fastutil extends the Java Collections Framework by providing type-specific maps, sets, lists, and queues with a small memory footprint and fast operations; it provides also big (64-bit) arrays, sets, and lists, sorting algorithms, fast, practical I/O classes for binary and text files, and facilities for memory mapping large files. This jar (fastutil-core.jar) contains data structures based on integers, longs, doubles, and objects, only; fastutil.jar contains all classes. If you have both jars in your dependencies, this jar should be excluded.
/*
* Copyright (C) 2002-2022 Sebastiano Vigna
*
* 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 it.unimi.dsi.fastutil.objects;
import it.unimi.dsi.fastutil.Hash;
import it.unimi.dsi.fastutil.HashCommon;
import static it.unimi.dsi.fastutil.HashCommon.arraySize;
import static it.unimi.dsi.fastutil.HashCommon.maxFill;
import java.util.Arrays;
import java.util.Collection;
import java.util.Iterator;
import java.util.NoSuchElementException;
import java.util.function.Consumer;
import java.util.stream.Collector;
/** A type-specific hash set with with a fast, small-footprint implementation.
*
*
Instances of this class use a hash table to represent a set. The table is
* filled up to a specified load factor, and then doubled in size to
* accommodate new entries. If the table is emptied below one fourth
* of the load factor, it is halved in size; however, the table is never reduced to a
* size smaller than that at creation time: this approach makes it
* possible to create sets with a large capacity in which insertions and
* deletions do not cause immediately rehashing. Moreover, halving is
* not performed when deleting entries from an iterator, as it would interfere
* with the iteration process.
*
*
Note that {@link #clear()} does not modify the hash table size.
* Rather, a family of {@linkplain #trim() trimming
* methods} lets you control the size of the table; this is particularly useful
* if you reuse instances of this class.
*
* @see Hash
* @see HashCommon
*/
public class ObjectOpenHashSet extends AbstractObjectSet implements java.io.Serializable, Cloneable, Hash {
private static final long serialVersionUID = 0L;
private static final boolean ASSERTS = false;
/** The array of keys. */
protected transient K[] key;
/** The mask for wrapping a position counter. */
protected transient int mask;
/** Whether this set contains the null key. */
protected transient boolean containsNull;
/** The current table size. Note that an additional element is allocated for storing the null key. */
protected transient int n;
/** Threshold after which we rehash. It must be the table size times {@link #f}. */
protected transient int maxFill;
/** We never resize below this threshold, which is the construction-time {#n}. */
protected final transient int minN;
/** Number of entries in the set (including the null key, if present). */
protected int size;
/** The acceptable load factor. */
protected final float f;
/** Creates a new hash set.
*
*
The actual table size will be the least power of two greater than {@code expected}/{@code f}.
*
* @param expected the expected number of elements in the hash set.
* @param f the load factor.
*/
@SuppressWarnings("unchecked")
public ObjectOpenHashSet(final int expected, final float f) {
if (f <= 0 || f >= 1) throw new IllegalArgumentException("Load factor must be greater than 0 and smaller than 1");
if (expected < 0) throw new IllegalArgumentException("The expected number of elements must be nonnegative");
this.f = f;
minN = n = arraySize(expected, f);
mask = n - 1;
maxFill = maxFill(n, f);
key = (K[]) new Object[n + 1];
}
/** Creates a new hash set with {@link Hash#DEFAULT_LOAD_FACTOR} as load factor.
*
* @param expected the expected number of elements in the hash set.
*/
public ObjectOpenHashSet(final int expected) {
this(expected, DEFAULT_LOAD_FACTOR);
}
/** Creates a new hash set with initial expected {@link Hash#DEFAULT_INITIAL_SIZE} elements
* and {@link Hash#DEFAULT_LOAD_FACTOR} as load factor.
*/
public ObjectOpenHashSet() {
this(DEFAULT_INITIAL_SIZE, DEFAULT_LOAD_FACTOR);
}
/** Creates a new hash set copying a given collection.
*
* @param c a {@link Collection} to be copied into the new hash set.
* @param f the load factor.
*/
public ObjectOpenHashSet(final Collection extends K> c, final float f) {
this(c.size(), f);
addAll(c);
}
/** Creates a new hash set with {@link Hash#DEFAULT_LOAD_FACTOR} as load factor
* copying a given collection.
*
* @param c a {@link Collection} to be copied into the new hash set.
*/
public ObjectOpenHashSet(final Collection extends K> c) {
this(c, DEFAULT_LOAD_FACTOR);
}
/** Creates a new hash set copying a given type-specific collection.
*
* @param c a type-specific collection to be copied into the new hash set.
* @param f the load factor.
*/
public ObjectOpenHashSet(final ObjectCollection extends K> c, final float f) {
this(c.size(), f);
addAll(c);
}
/** Creates a new hash set with {@link Hash#DEFAULT_LOAD_FACTOR} as load factor
* copying a given type-specific collection.
*
* @param c a type-specific collection to be copied into the new hash set.
*/
public ObjectOpenHashSet(final ObjectCollection extends K> c) {
this(c, DEFAULT_LOAD_FACTOR);
}
/** Creates a new hash set using elements provided by a type-specific iterator.
*
* @param i a type-specific iterator whose elements will fill the set.
* @param f the load factor.
*/
public ObjectOpenHashSet(final Iterator extends K> i, final float f) {
this(DEFAULT_INITIAL_SIZE, f);
while(i.hasNext()) add(i.next());
}
/** Creates a new hash set with {@link Hash#DEFAULT_LOAD_FACTOR} as load factor using elements provided by a type-specific iterator.
*
* @param i a type-specific iterator whose elements will fill the set.
*/
public ObjectOpenHashSet(final Iterator extends K> i) {
this(i, DEFAULT_LOAD_FACTOR);
}
/** Creates a new hash set and fills it with the elements of a given array.
*
* @param a an array whose elements will be used to fill the set.
* @param offset the first element to use.
* @param length the number of elements to use.
* @param f the load factor.
*/
public ObjectOpenHashSet(final K[] a, final int offset, final int length, final float f) {
this(length < 0 ? 0 : length, f);
ObjectArrays.ensureOffsetLength(a, offset, length);
for(int i = 0; i < length; i++) add(a[offset + i]);
}
/** Creates a new hash set with {@link Hash#DEFAULT_LOAD_FACTOR} as load factor and fills it with the elements of a given array.
*
* @param a an array whose elements will be used to fill the set.
* @param offset the first element to use.
* @param length the number of elements to use.
*/
public ObjectOpenHashSet(final K[] a, final int offset, final int length) {
this(a, offset, length, DEFAULT_LOAD_FACTOR);
}
/** Creates a new hash set copying the elements of an array.
*
* @param a an array to be copied into the new hash set.
* @param f the load factor.
*/
public ObjectOpenHashSet(final K[] a, final float f) {
this(a, 0, a.length, f);
}
/** Creates a new hash set with {@link Hash#DEFAULT_LOAD_FACTOR} as load factor
* copying the elements of an array.
*
* @param a an array to be copied into the new hash set.
*/
public ObjectOpenHashSet(final K[] a) {
this(a, DEFAULT_LOAD_FACTOR);
}
/** Creates a new empty hash set.
*
* @return a new empty hash set.
*/
public static ObjectOpenHashSet of() {
return new ObjectOpenHashSet <>();
}
/** Creates a new hash set with {@link Hash#DEFAULT_LOAD_FACTOR} as load factor
* using the given element.
*
* @param e the element that the returned set will contain.
* @return a new hash set with {@link Hash#DEFAULT_LOAD_FACTOR} as load factor containing {@code e}.
*/
public static ObjectOpenHashSet of(final K e) {
ObjectOpenHashSet result = new ObjectOpenHashSet <>(1, DEFAULT_LOAD_FACTOR);
result.add(e);
return result;
}
/** Creates a new hash set with {@link Hash#DEFAULT_LOAD_FACTOR} as load factor
* using the elements given.
*
* @param e0 the first element.
* @param e1 the second element.
* @return a new hash set with {@link Hash#DEFAULT_LOAD_FACTOR} as load factor containing {@code e0} and {@code e1}.
* @throws IllegalArgumentException if there were duplicate entries.
*/
public static ObjectOpenHashSet of(final K e0, final K e1) {
ObjectOpenHashSet result = new ObjectOpenHashSet <>(2, DEFAULT_LOAD_FACTOR);
result.add(e0);
if (!result.add(e1)) {
throw new IllegalArgumentException("Duplicate element: " + e1);
}
return result;
}
/** Creates a new hash set with {@link Hash#DEFAULT_LOAD_FACTOR} as load factor
* using the elements given.
*
* @param e0 the first element.
* @param e1 the second element.
* @param e2 the third element.
* @return a new hash set with {@link Hash#DEFAULT_LOAD_FACTOR} as load factor containing {@code e0}, {@code e1}, and {@code e2}.
* @throws IllegalArgumentException if there were duplicate entries.
*/
public static ObjectOpenHashSet of(final K e0, final K e1, final K e2) {
ObjectOpenHashSet result = new ObjectOpenHashSet <>(3, DEFAULT_LOAD_FACTOR);
result.add(e0);
if (!result.add(e1)) {
throw new IllegalArgumentException("Duplicate element: " + e1);
}
if (!result.add(e2)) {
throw new IllegalArgumentException("Duplicate element: " + e2);
}
return result;
}
/** Creates a new hash set with {@link Hash#DEFAULT_LOAD_FACTOR} as load factor
* using a list of elements.
*
* @param a a list of elements that will be used to initialize the new hash set.
* @return a new hash set with {@link Hash#DEFAULT_LOAD_FACTOR} as load factor containing the elements of {@code a}.
* @throws IllegalArgumentException if a duplicate entry was encountered.
*/
@SafeVarargs
public static ObjectOpenHashSet of(final K... a) {
ObjectOpenHashSet result = new ObjectOpenHashSet <>(a.length, DEFAULT_LOAD_FACTOR);
for (K element : a) {
if (!result.add(element)) {
throw new IllegalArgumentException("Duplicate element " + element);
}
}
return result;
}
// Collector wants a function that returns the collection being added to.
private ObjectOpenHashSet combine(ObjectOpenHashSet extends K> toAddFrom) {
addAll(toAddFrom);
return this;
}
private static final Collector