it.unimi.dsi.fastutil.objects
Class ObjectArrays

java.lang.Object
  extended by it.unimi.dsi.fastutil.objects.ObjectArrays

public class ObjectArrays
extends java.lang.Object

A class providing static methods and objects that do useful things with type-specific arrays. In particular, the ensureCapacity(), grow(), trim() and setLength() methods allow to handle arrays much like array lists. This can be very useful when efficiency (or syntactic simplicity) reasons make array lists unsuitable.

Warning: creating arrays using reflection, as it happens in ensureCapacity(Object[],int,int) and grow(Object[],int,int), is significantly slower than using new. This phenomenon is particularly evident in the first growth phases of an array reallocated with doubling (or similar) logic.

See Also:
Arrays

Field Summary
static java.lang.Object[] EMPTY_ARRAY
          A static, final, empty array.
static Hash.Strategy<java.lang.Object[]> HASH_STRATEGY
          A type-specific content-based hash strategy for arrays.
static long ONEOVERPHI
          The inverse of the golden ratio times 216.
 
Method Summary
static
<K> K[]
copy(K[] array)
          Returns a copy of an array.
static
<K> K[]
copy(K[] array, int offset, int length)
          Returns a copy of a portion of an array.
static
<K> K[]
ensureCapacity(K[] array, int length)
          Ensures that an array can contain the given number of entries.
static
<K> K[]
ensureCapacity(K[] array, int length, int preserve)
          Ensures that an array can contain the given number of entries, preserving just a part of the array.
static
<K> void
ensureFromTo(K[] a, int from, int to)
          Ensures that a range given by its first (inclusive) and last (exclusive) elements fits an array.
static
<K> void
ensureOffsetLength(K[] a, int offset, int length)
          Ensures that a range given by an offset and a length fits an array.
static
<K> boolean
equals(K[] a1, K[] a2)
          Returns true if the two arrays are elementwise equal.
static
<K> void
fill(K[] array, int from, int to, K value)
          Fills a portion of the given array with the given value.
static
<K> void
fill(K[] array, K value)
          Fills the given array with the given value.
static
<K> K[]
grow(K[] array, int length)
          Grows the given array to the maximum between the given length and the current length divided by the golden ratio, provided that the given length is larger than the current length.
static
<K> K[]
grow(K[] array, int length, int preserve)
          Grows the given array to the maximum between the given length and the current length divided by the golden ratio, provided that the given length is larger than the current length, preserving just a part of the array.
static
<K> K[]
setLength(K[] array, int length)
          Sets the length of the given array.
static
<K> K[]
trim(K[] array, int length)
          Trims the given array to the given length.
 
Methods inherited from class java.lang.Object
equals, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

ONEOVERPHI

public static final long ONEOVERPHI
The inverse of the golden ratio times 216.

See Also:
Constant Field Values

EMPTY_ARRAY

public static final java.lang.Object[] EMPTY_ARRAY
A static, final, empty array.


HASH_STRATEGY

public static final Hash.Strategy<java.lang.Object[]> HASH_STRATEGY
A type-specific content-based hash strategy for arrays.

This hash strategy may be used in custom hash collections whenever keys are arrays, and they must be considered equal by content. This strategy will handle null correctly, and it is serializable.

Method Detail

ensureCapacity

public static <K> K[] ensureCapacity(K[] array,
                                     int length)
Ensures that an array can contain the given number of entries.

If you cannot foresee whether this array will need again to be enlarged, you should probably use grow() instead.

Parameters:
array - an array.
length - the new minimum length for this array.
Returns:
array, if it contains length entries or more; otherwise, an array with length entries whose first array.length entries are the same as those of array.

ensureCapacity

public static <K> K[] ensureCapacity(K[] array,
                                     int length,
                                     int preserve)
Ensures that an array can contain the given number of entries, preserving just a part of the array.

Parameters:
array - an array.
length - the new minimum length for this array.
preserve - the number of elements of the array that must be preserved in case a new allocation is necessary.
Returns:
array, if it can contain length entries or more; otherwise, an array with length entries whose first preserve entries are the same as those of array.

grow

public static <K> K[] grow(K[] array,
                           int length)
Grows the given array to the maximum between the given length and the current length divided by the golden ratio, provided that the given length is larger than the current length.

Dividing by the golden ratio (φ) approximately increases the array length by 1.618. If you want complete control on the array growth, you should probably use ensureCapacity() instead.

Parameters:
array - an array.
length - the new minimum length for this array.
Returns:
array, if it can contain length entries; otherwise, an array with max(length,array.length/φ) entries whose first array.length entries are the same as those of array.

grow

public static <K> K[] grow(K[] array,
                           int length,
                           int preserve)
Grows the given array to the maximum between the given length and the current length divided by the golden ratio, provided that the given length is larger than the current length, preserving just a part of the array.

Dividing by the golden ratio (φ) approximately increases the array length by 1.618. If you want complete control on the array growth, you should probably use ensureCapacity() instead.

Parameters:
array - an array.
length - the new minimum length for this array.
preserve - the number of elements of the array that must be preserved in case a new allocation is necessary.
Returns:
array, if it can contain length entries; otherwise, an array with max(length,array.length/φ) entries whose first preserve entries are the same as those of array.

trim

public static <K> K[] trim(K[] array,
                           int length)
Trims the given array to the given length.

Parameters:
array - an array.
length - the new maximum length for the array.
Returns:
array, if it contains length entries or less; otherwise, an array with length entries whose entries are the same as the first length entries of array.

setLength

public static <K> K[] setLength(K[] array,
                                int length)
Sets the length of the given array.

Parameters:
array - an array.
length - the new length for the array.
Returns:
array, if it contains exactly length entries; otherwise, if it contains more than length entries, an array with length entries whose entries are the same as the first length entries of array; otherwise, an array with length entries whose first array.length entries are the same as those of array.

copy

public static <K> K[] copy(K[] array,
                           int offset,
                           int length)
Returns a copy of a portion of an array.

Parameters:
array - an array.
offset - the first element to copy.
length - the number of elements to copy.
Returns:
a new array containing length elements of array starting at offset.

copy

public static <K> K[] copy(K[] array)
Returns a copy of an array.

Parameters:
array - an array.
Returns:
a copy of array.

fill

public static <K> void fill(K[] array,
                            K value)
Fills the given array with the given value.

This method uses a backward loop. It is significantly faster than the corresponding method in Arrays.

Parameters:
array - an array.
value - the new value for all elements of the array.

fill

public static <K> void fill(K[] array,
                            int from,
                            int to,
                            K value)
Fills a portion of the given array with the given value.

If possible (i.e., from is 0) this method uses a backward loop. In this case, it is significantly faster than the corresponding method in Arrays.

Parameters:
array - an array.
from - the starting index of the portion to fill.
to - the end index of the portion to fill.
value - the new value for all elements of the specified portion of the array.

equals

public static <K> boolean equals(K[] a1,
                                 K[] a2)
Returns true if the two arrays are elementwise equal.

This method uses a backward loop. It is significantly faster than the corresponding method in Arrays.

Parameters:
a1 - an array.
a2 - another array.
Returns:
true if the two arrays are of the same length, and their elements are equal.

ensureFromTo

public static <K> void ensureFromTo(K[] a,
                                    int from,
                                    int to)
Ensures that a range given by its first (inclusive) and last (exclusive) elements fits an array.

This method may be used whenever an array range check is needed.

Parameters:
a - an array.
from - a start index (inclusive).
to - an end index (inclusive).
Throws:
java.lang.IllegalArgumentException - if from is greater than to.
java.lang.ArrayIndexOutOfBoundsException - if from or to are greater than the array length or negative.

ensureOffsetLength

public static <K> void ensureOffsetLength(K[] a,
                                          int offset,
                                          int length)
Ensures that a range given by an offset and a length fits an array.

This method may be used whenever an array range check is needed.

Parameters:
a - an array.
offset - a start index.
length - a length (the number of elements in the range).
Throws:
java.lang.IllegalArgumentException - if length is negative.
java.lang.ArrayIndexOutOfBoundsException - if offset is negative or offset+length is greater than the array length.