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;
import java.util.Comparator;
/** A type-specific linked 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.
*
*
Iterators generated by this set will enumerate elements in the same order in which they
* have been added to the set (addition of elements already present
* in the set does not change the iteration order). Note that this order has nothing in common with the natural
* order of the keys. The order is kept by means of a doubly linked list, represented
* via an array of longs parallel to the table.
*
*
This class implements the interface of a sorted set, so to allow easy
* access of the iteration order: for instance, you can get the first element
* in iteration order with {@code first()} without having to create an
* iterator; however, this class partially violates the {@link java.util.SortedSet}
* contract because all subset methods throw an exception and {@link
* #comparator()} returns always {@code null}.
*
*
Additional methods, such as {@code addAndMoveToFirst()}, make it easy
* to use instances of this class as a cache (e.g., with LRU policy).
*
*
The iterators provided by this class are type-specific {@linkplain
* java.util.ListIterator list iterators}, and can be started at any
* element which is in the set (if the provided element
* is not in the set, a {@link NoSuchElementException} exception will be thrown).
* If, however, the provided element is not the first or last element in the
* set, the first access to the list index will require linear time, as in the worst case
* the entire set must be scanned in iteration order to retrieve the positional
* index of the starting element. If you use just the methods of a type-specific {@link it.unimi.dsi.fastutil.BidirectionalIterator},
* however, all operations will be performed in constant time.
*
* @see Hash
* @see HashCommon
*/
public class ObjectLinkedOpenHashSet extends AbstractObjectSortedSet 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 index of the first entry in iteration order. It is valid iff {@link #size} is nonzero; otherwise, it contains -1. */
protected transient int first = -1;
/** The index of the last entry in iteration order. It is valid iff {@link #size} is nonzero; otherwise, it contains -1. */
protected transient int last = -1;
/** For each entry, the next and the previous entry in iteration order,
* stored as {@code ((prev & 0xFFFFFFFFL) << 32) | (next & 0xFFFFFFFFL)}.
* The first entry contains predecessor -1, and the last entry
* contains successor -1. */
protected transient long[] link;
/** 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 ObjectLinkedOpenHashSet(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];
link = new long[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 ObjectLinkedOpenHashSet(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 ObjectLinkedOpenHashSet() {
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 ObjectLinkedOpenHashSet(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 ObjectLinkedOpenHashSet(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 ObjectLinkedOpenHashSet(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 ObjectLinkedOpenHashSet(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 ObjectLinkedOpenHashSet(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 ObjectLinkedOpenHashSet(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 ObjectLinkedOpenHashSet(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 ObjectLinkedOpenHashSet(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 ObjectLinkedOpenHashSet(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 ObjectLinkedOpenHashSet(final K[] a) {
this(a, DEFAULT_LOAD_FACTOR);
}
/** Creates a new empty hash set.
*
* @return a new empty hash set.
*/
public static ObjectLinkedOpenHashSet of() {
return new ObjectLinkedOpenHashSet <>();
}
/** 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 ObjectLinkedOpenHashSet of(final K e) {
ObjectLinkedOpenHashSet result = new ObjectLinkedOpenHashSet <>(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 ObjectLinkedOpenHashSet of(final K e0, final K e1) {
ObjectLinkedOpenHashSet result = new ObjectLinkedOpenHashSet <>(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 ObjectLinkedOpenHashSet of(final K e0, final K e1, final K e2) {
ObjectLinkedOpenHashSet result = new ObjectLinkedOpenHashSet <>(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 ObjectLinkedOpenHashSet of(final K... a) {
ObjectLinkedOpenHashSet result = new ObjectLinkedOpenHashSet <>(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 ObjectLinkedOpenHashSet combine(ObjectLinkedOpenHashSet extends K> toAddFrom) {
addAll(toAddFrom);
return this;
}
private static final Collector