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 access and insertion; 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. Note that if you have both this jar and fastutil-core.jar in your dependencies, fastutil-core.jar should be excluded.
/*
* Copyright (C) 2002-2023 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 java.util.Arrays;
import java.util.Collection;
import java.util.Iterator;
import java.util.RandomAccess;
import java.util.NoSuchElementException;
import java.lang.reflect.Array;
import java.util.Comparator;
import java.util.stream.Collector;
import java.util.function.Consumer;
/**
* A type-specific array-based list; provides some additional methods that use polymorphism to avoid
* (un)boxing.
*
*
* This class implements a lightweight, fast, open, optimized, reuse-oriented version of array-based
* lists. Instances of this class represent a list with an array that is enlarged as needed when new
* entries are created (by increasing its current length by 50%), but is never made smaller
* (even on a {@link #clear()}). A family of {@linkplain #trim() trimming methods} lets you control
* the size of the backing array; this is particularly useful if you reuse instances of this class.
* Range checks are equivalent to those of {@link java.util}'s classes, but they are delayed as much
* as possible.
*
*
* The backing array is exposed by the {@link #elements()} method. If an instance of this class was
* created {@linkplain #wrap(Object[],int) by wrapping}, backing-array reallocations will be
* performed using reflection, so that {@link #elements()} can return an array of the same type of
* the original array: the comments about efficiency made in
* {@link it.unimi.dsi.fastutil.objects.ObjectArrays} apply here. Moreover, you must take into
* consideration that assignment to an array not of type {@code Object[]} is slower due to type
* checking.
*
*
* This class implements the bulk methods {@code removeElements()}, {@code addElements()} and
* {@code getElements()} using high-performance system calls (e.g.,
* {@link System#arraycopy(Object,int,Object,int,int) System.arraycopy()}) instead of expensive
* loops.
*
* @see java.util.ArrayList
*/
public class ReferenceArrayList extends AbstractReferenceList implements RandomAccess, Cloneable, java.io.Serializable {
private static final long serialVersionUID = -7046029254386353131L;
/** The initial default capacity of an array list. */
public static final int DEFAULT_INITIAL_CAPACITY = 10;
/**
* Whether the backing array was passed to {@code wrap()}. In this case, we must reallocate with the
* same type of array.
*/
protected final boolean wrapped;
/** The backing array. */
protected transient K a[];
/** The current actual size of the list (never greater than the backing-array length). */
protected int size;
/**
* Ensures that the component type of the given array is the proper type. This is irrelevant for
* primitive types, so it will just do a trivial copy. But for Reference types, you can have a
* {@code String[]} masquerading as an {@code Object[]}, which is a case we need to prepare for
* because we let the user give an array to use directly with {@link #wrap}.
*/
@SuppressWarnings("unchecked")
private static final K[] copyArraySafe(K[] a, int length) {
if (length == 0) return (K[])ObjectArrays.EMPTY_ARRAY;
return (K[])java.util.Arrays.copyOf(a, length, Object[].class);
}
private static final K[] copyArrayFromSafe(ReferenceArrayList l) {
return copyArraySafe(l.a, l.size);
}
/**
* Creates a new array list using a given array.
*
*
* This constructor is only meant to be used by the wrapping methods.
*
* @param a the array that will be used to back this array list.
*/
protected ReferenceArrayList(final K a[], @SuppressWarnings("unused") boolean wrapped) {
this.a = a;
this.wrapped = wrapped;
}
@SuppressWarnings("unchecked")
private void initArrayFromCapacity(final int capacity) {
if (capacity < 0) throw new IllegalArgumentException("Initial capacity (" + capacity + ") is negative");
if (capacity == 0) a = (K[])ObjectArrays.EMPTY_ARRAY;
else a = (K[])new Object[capacity];
}
/**
* Creates a new array list with given capacity.
*
* @param capacity the initial capacity of the array list (may be 0).
*/
public ReferenceArrayList(final int capacity) {
initArrayFromCapacity(capacity);
this.wrapped = false;
}
/** Creates a new array list with {@link #DEFAULT_INITIAL_CAPACITY} capacity. */
@SuppressWarnings("unchecked")
public ReferenceArrayList() {
a = (K[])ObjectArrays.DEFAULT_EMPTY_ARRAY; // We delay allocation
wrapped = false;
}
/**
* Creates a new array list and fills it with a given collection.
*
* @param c a collection that will be used to fill the array list.
*/
public ReferenceArrayList(final Collection c) {
if (c instanceof ReferenceArrayList) {
a = copyArrayFromSafe((ReferenceArrayList)c);
size = a.length;
} else {
initArrayFromCapacity(c.size());
if (c instanceof ReferenceList) {
((ReferenceList)c).getElements(0, a, 0, size = c.size());
} else {
size = ObjectIterators.unwrap(c.iterator(), a);
}
}
this.wrapped = false;
}
/**
* Creates a new array list and fills it with a given type-specific collection.
*
* @param c a type-specific collection that will be used to fill the array list.
*/
public ReferenceArrayList(final ReferenceCollection c) {
if (c instanceof ReferenceArrayList) {
a = copyArrayFromSafe((ReferenceArrayList)c);
size = a.length;
} else {
initArrayFromCapacity(c.size());
if (c instanceof ReferenceList) {
((ReferenceList)c).getElements(0, a, 0, size = c.size());
} else {
size = ObjectIterators.unwrap(c.iterator(), a);
}
}
this.wrapped = false;
}
/**
* Creates a new array list and fills it with a given type-specific list.
*
* @param l a type-specific list that will be used to fill the array list.
*/
public ReferenceArrayList(final ReferenceList l) {
if (l instanceof ReferenceArrayList) {
a = copyArrayFromSafe((ReferenceArrayList)l);
size = a.length;
} else {
initArrayFromCapacity(l.size());
l.getElements(0, a, 0, size = l.size());
}
this.wrapped = false;
}
/**
* Creates a new array list and fills it with the elements of a given array.
*
* @param a an array whose elements will be used to fill the array list.
*/
public ReferenceArrayList(final K a[]) {
this(a, 0, a.length);
}
/**
* Creates a new array list and fills it with the elements of a given array.
*
* @param a an array whose elements will be used to fill the array list.
* @param offset the first element to use.
* @param length the number of elements to use.
*/
public ReferenceArrayList(final K a[], final int offset, final int length) {
this(length);
System.arraycopy(a, offset, this.a, 0, length);
size = length;
}
/**
* Creates a new array list and fills it with the elements returned by an iterator..
*
* @param i an iterator whose returned elements will fill the array list.
*/
public ReferenceArrayList(final Iterator i) {
this();
while (i.hasNext()) this.add((i.next()));
}
/**
* Creates a new array list and fills it with the elements returned by a type-specific iterator..
*
* @param i a type-specific iterator whose returned elements will fill the array list.
*/
public ReferenceArrayList(final ObjectIterator i) {
this();
while (i.hasNext()) this.add(i.next());
}
/**
* Returns the backing array of this list.
*
*
* If this array list was created by wrapping a given array, it is guaranteed that the type of the
* returned array will be the same. Otherwise, the returned array will be of type {@link Object
* Object[]} (in spite of the declared return type).
*
*
* Warning: This behaviour may cause (unfathomable) run-time errors if a method
* expects an array actually of type {@code K[]}, but this methods returns an array of type
* {@link Object Object[]}.
*
* @return the backing array.
*/
public K[] elements() {
return a;
}
/**
* Wraps a given array into an array list of given size.
*
*
* Note it is guaranteed that the type of the array returned by {@link #elements()} will be the same
* (see the comments in the class documentation).
*
* @param a an array to wrap.
* @param length the length of the resulting array list.
* @return a new array list of the given size, wrapping the given array.
*/
public static ReferenceArrayList wrap(final K a[], final int length) {
if (length > a.length) throw new IllegalArgumentException("The specified length (" + length + ") is greater than the array size (" + a.length + ")");
final ReferenceArrayList l = new ReferenceArrayList<>(a, true);
l.size = length;
return l;
}
/**
* Wraps a given array into an array list.
*
*
* Note it is guaranteed that the type of the array returned by {@link #elements()} will be the same
* (see the comments in the class documentation).
*
* @param a an array to wrap.
* @return a new array list wrapping the given array.
*/
public static ReferenceArrayList wrap(final K a[]) {
return wrap(a, a.length);
}
/**
* Creates a new empty array list.
*
* @return a new empty array list.
*/
public static ReferenceArrayList of() {
return new ReferenceArrayList<>();
}
/**
* Creates an array list using an array of elements.
*
* @param init a the array the will become the new backing array of the array list.
* @return a new array list backed by the given array.
* @see #wrap
*/
@SafeVarargs
public static ReferenceArrayList of(final K... init) {
return wrap(init);
}
// Collector wants a function that returns the collection being added to.
ReferenceArrayList combine(ReferenceArrayList toAddFrom) {
addAll(toAddFrom);
return this;
}
private static final Collector