it.unimi.dsi.fastutil.ints
Class IntArrays

java.lang.Object
  extended byit.unimi.dsi.fastutil.ints.IntArrays

public class IntArrays
extends 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.

Note that BinIO and TextIO contain several methods make it possible to load and save arrays of primitive types as sequences of elements in DataInput format (i.e., not as objects) or as sequences of lines of text.

See Also:
Arrays

Field Summary
static int[] EMPTY_ARRAY
          A static, final, empty array.
static Hash.Strategy HASH_STRATEGY
          A type-specific content-based hash strategy for arrays.
 
Method Summary
static int[] copy(int[] array)
          Returns a copy of an array.
static int[] copy(int[] array, int offset, int length)
          Returns a copy of a portion of an array.
static int[] ensureCapacity(int[] array, int length)
          Ensures that an array can contain the given number of entries.
static int[] ensureCapacity(int[] array, int length, int preserve)
          Ensures that an array can contain the given number of entries, preserving just a part of the array.
static void ensureFromTo(int[] a, int from, int to)
          Ensures that a range given by its first (inclusive) and last (exclusive) elements fits an array.
static void ensureOffsetLength(int[] a, int offset, int length)
          Ensures that a range given by an offset and a length fits an array.
static boolean equals(int[] a1, int[] a2)
          Returns true if the two arrays are elementwise equal.
static void fill(int[] array, int value)
          Fills the given array with the given value.
static void fill(int[] array, int from, int to, int value)
          Fills a portion of the given array with the given value.
static int[] grow(int[] 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 int[] grow(int[] 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 int[] setLength(int[] array, int length)
          Sets the length of the given array.
static int[] trim(int[] 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

EMPTY_ARRAY

public static final int[] EMPTY_ARRAY
A static, final, empty array.


HASH_STRATEGY

public static final Hash.Strategy 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 int[] ensureCapacity(int[] 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 int[] ensureCapacity(int[] 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 int[] grow(int[] 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 int[] grow(int[] 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 int[] trim(int[] 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 int[] setLength(int[] 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 int[] copy(int[] 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 int[] copy(int[] array)
Returns a copy of an array.

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

fill

public static void fill(int[] array,
                        int 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 void fill(int[] array,
                        int from,
                        int to,
                        int 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 boolean equals(int[] a1,
                             int[] 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 void ensureFromTo(int[] 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:
IllegalArgumentException - if from is greater than to.
ArrayIndexOutOfBoundsException - if from or to are greater than the array length or negative.

ensureOffsetLength

public static void ensureOffsetLength(int[] 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:
IllegalArgumentException - if length is negative.
ArrayIndexOutOfBoundsException - if offset is negative or offset+length is greater than the array length.