it.unimi.dsi.fastutil.shorts
Class ShortBigArrays

java.lang.Object
  extended by it.unimi.dsi.fastutil.shorts.ShortBigArrays

public class ShortBigArrays
extends Object

A class providing static methods and objects that do useful things with big arrays.

In particular, the ensureCapacity(), grow(), trim() and setLength() methods allow to handle big arrays much like array lists.

Note that BinIO and TextIO contain several methods make it possible to load and save big 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:
BigArrays

Field Summary
static short[][] EMPTY_BIG_ARRAY
          A static, final, empty big array.
static Hash.Strategy HASH_STRATEGY
          A type-specific content-based hash strategy for big arrays.
static long ONEOVERPHI
          The inverse of the golden ratio times 216.
 
Method Summary
static void add(short[][] array, long index, short incr)
          Adds the specified increment the element of the given big array of specified index.
static long binarySearch(short[][] a, long from, long to, short key)
          Searches a range of the specified big array for the specified value using the binary search algorithm.
static long binarySearch(short[][] a, long from, long to, short key, ShortComparator c)
          Searches a range of the specified big array for the specified value using the binary search algorithm and a specified comparator.
static long binarySearch(short[][] a, short key)
          Searches a big array for the specified value using the binary search algorithm.
static long binarySearch(short[][] a, short key, ShortComparator c)
          Searches a big array for the specified value using the binary search algorithm and a specified comparator.
static short[][] copy(short[][] array)
          Returns a copy of a big array.
static short[][] copy(short[][] array, long offset, long length)
          Returns a copy of a portion of a big array.
static void copy(short[][] srcArray, long srcPos, short[][] destArray, long destPos, long length)
          Copies a big array from the specified source big array, beginning at the specified position, to the specified position of the destination big array.
static void copyFromBig(short[][] srcArray, long srcPos, short[] destArray, int destPos, int length)
          Copies a big array from the specified source big array, beginning at the specified position, to the specified position of the destination array.
static void copyToBig(short[] srcArray, int srcPos, short[][] destArray, long destPos, long length)
          Copies an array from the specified source array, beginning at the specified position, to the specified position of the destination big array.
static void decr(short[][] array, long index)
          Decrements the element of the given big array of specified index.
static short[][] ensureCapacity(short[][] array, long length)
          Ensures that a big array can contain the given number of entries.
static short[][] ensureCapacity(short[][] array, long length, long preserve)
          Ensures that a big array can contain the given number of entries, preserving just a part of the big array.
static void ensureFromTo(short[][] a, long from, long to)
          Ensures that a range given by its first (inclusive) and last (exclusive) elements fits a big array.
static void ensureOffsetLength(short[][] a, long offset, long length)
          Ensures that a range given by an offset and a length fits a big array.
static boolean equals(short[][] a1, short[][] a2)
          Returns true if the two big arrays are elementwise equal.
static void fill(short[][] array, long from, long to, short value)
          Fills a portion of the given big array with the given value.
static void fill(short[][] array, short value)
          Fills the given big array with the given value.
static short get(short[][] array, long index)
          Returns the element of the given big array of specified index.
static short[][] grow(short[][] array, long length)
          Grows the given big 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 short[][] grow(short[][] array, long length, long preserve)
          Grows the given big 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 big array.
static void incr(short[][] array, long index)
          Increments the element of the given big array of specified index.
static long length(short[][] array)
          Returns the length of the given big array.
static void mul(short[][] array, long index, short factor)
          Multiplies by the specified factor the element of the given big array of specified index.
static short[][] newBigArray(long length)
          Creates a new big array.
static void quickSort(short[][] x)
          Sorts the specified big array according to the natural ascending order using quicksort.
static void quickSort(short[][] x, long from, long to)
          Sorts the specified range of elements according to the natural ascending order using quicksort.
static void quickSort(short[][] x, long from, long to, ShortComparator comp)
          Sorts the specified range of elements according to the order induced by the specified comparator using quicksort.
static void quickSort(short[][] x, ShortComparator comp)
          Sorts the specified big array according to the order induced by the specified comparator using quicksort.
static void set(short[][] array, long index, short value)
          Sets the element of the given big array of specified index.
static short[][] setLength(short[][] array, long length)
          Sets the length of the given big array.
static short[][] shuffle(short[][] a, long from, long to, Random random)
          Shuffles the specified big array fragment using the specified pseudorandom number generator.
static short[][] shuffle(short[][] a, Random random)
          Shuffles the specified big array using the specified pseudorandom number generator.
static void swap(short[][] array, long first, long second)
          Swaps the element of the given big array of specified indices.
static String toString(short[][] a)
           
static short[][] trim(short[][] array, long length)
          Trims the given big array to the given length.
static short[][] wrap(short[] array)
          Turns a standard array into a big array.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, 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_BIG_ARRAY

public static final short[][] EMPTY_BIG_ARRAY
A static, final, empty big array.


HASH_STRATEGY

public static final Hash.Strategy HASH_STRATEGY
A type-specific content-based hash strategy for big arrays.

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

Method Detail

get

public static short get(short[][] array,
                        long index)
Returns the element of the given big array of specified index.

Parameters:
array - a big array.
index - a position in the big array.
Returns:
the element of the big array at the specified position.

set

public static void set(short[][] array,
                       long index,
                       short value)
Sets the element of the given big array of specified index.

Parameters:
array - a big array.
index - a position in the big array.

swap

public static void swap(short[][] array,
                        long first,
                        long second)
Swaps the element of the given big array of specified indices.

Parameters:
array - a big array.
first - a position in the big array.
second - a position in the big array.

add

public static void add(short[][] array,
                       long index,
                       short incr)
Adds the specified increment the element of the given big array of specified index.

Parameters:
array - a big array.
index - a position in the big array.
incr - the increment

mul

public static void mul(short[][] array,
                       long index,
                       short factor)
Multiplies by the specified factor the element of the given big array of specified index.

Parameters:
array - a big array.
index - a position in the big array.
factor - the factor

incr

public static void incr(short[][] array,
                        long index)
Increments the element of the given big array of specified index.

Parameters:
array - a big array.
index - a position in the big array.

decr

public static void decr(short[][] array,
                        long index)
Decrements the element of the given big array of specified index.

Parameters:
array - a big array.
index - a position in the big array.

length

public static long length(short[][] array)
Returns the length of the given big array.

Parameters:
array - a big array.
Returns:
the length of the given big array.

copy

public static void copy(short[][] srcArray,
                        long srcPos,
                        short[][] destArray,
                        long destPos,
                        long length)
Copies a big array from the specified source big array, beginning at the specified position, to the specified position of the destination big array. Handles correctly overlapping regions of the same big array.

Parameters:
srcArray - the source big array.
srcPos - the starting position in the source big array.
destArray - the destination big array.
destPos - the starting position in the destination data.
length - the number of elements to be copied.

copyFromBig

public static void copyFromBig(short[][] srcArray,
                               long srcPos,
                               short[] destArray,
                               int destPos,
                               int length)
Copies a big array from the specified source big array, beginning at the specified position, to the specified position of the destination array.

Parameters:
srcArray - the source big array.
srcPos - the starting position in the source big array.
destArray - the destination array.
destPos - the starting position in the destination data.
length - the number of elements to be copied.

copyToBig

public static void copyToBig(short[] srcArray,
                             int srcPos,
                             short[][] destArray,
                             long destPos,
                             long length)
Copies an array from the specified source array, beginning at the specified position, to the specified position of the destination big array.

Parameters:
srcArray - the source array.
srcPos - the starting position in the source array.
destArray - the destination big array.
destPos - the starting position in the destination data.
length - the number of elements to be copied.

newBigArray

public static short[][] newBigArray(long length)
Creates a new big array.

Parameters:
length - the length of the new big array.
Returns:
a new big array of given length.

wrap

public static short[][] wrap(short[] array)
Turns a standard array into a big array.

Note that the returned big array might contain as a segment the original array.

Parameters:
array - an array.
Returns:
a new big array with the same length and content of array.

ensureCapacity

public static short[][] ensureCapacity(short[][] array,
                                       long length)
Ensures that a big array can contain the given number of entries.

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

Warning: the returned array might use part of the segments of the original array, which must be considered read-only after calling this method.

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

ensureCapacity

public static short[][] ensureCapacity(short[][] array,
                                       long length,
                                       long preserve)
Ensures that a big array can contain the given number of entries, preserving just a part of the big array.

Warning: the returned array might use part of the segments of the original array, which must be considered read-only after calling this method.

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

grow

public static short[][] grow(short[][] array,
                             long length)
Grows the given big 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 big array length by 1.618. If you want complete control on the big array growth, you should probably use ensureCapacity() instead.

Warning: the returned array might use part of the segments of the original array, which must be considered read-only after calling this method.

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

grow

public static short[][] grow(short[][] array,
                             long length,
                             long preserve)
Grows the given big 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 big array.

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

Warning: the returned array might use part of the segments of the original array, which must be considered read-only after calling this method.

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

trim

public static short[][] trim(short[][] array,
                             long length)
Trims the given big array to the given length.

Warning: the returned array might use part of the segments of the original array, which must be considered read-only after calling this method.

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

setLength

public static short[][] setLength(short[][] array,
                                  long length)
Sets the length of the given big array.

Warning: the returned array might use part of the segments of the original array, which must be considered read-only after calling this method.

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

copy

public static short[][] copy(short[][] array,
                             long offset,
                             long length)
Returns a copy of a portion of a big array.

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

copy

public static short[][] copy(short[][] array)
Returns a copy of a big array.

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

fill

public static void fill(short[][] array,
                        short value)
Fills the given big array with the given value.

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

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

fill

public static void fill(short[][] array,
                        long from,
                        long to,
                        short value)
Fills a portion of the given big 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 - a big 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 big array.

equals

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

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

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

toString

public static String toString(short[][] a)

ensureFromTo

public static void ensureFromTo(short[][] a,
                                long from,
                                long to)
Ensures that a range given by its first (inclusive) and last (exclusive) elements fits a big array.

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

Parameters:
a - a big 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 big array length or negative.

ensureOffsetLength

public static void ensureOffsetLength(short[][] a,
                                      long offset,
                                      long length)
Ensures that a range given by an offset and a length fits a big array.

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

Parameters:
a - a big 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 big array length.

quickSort

public static void quickSort(short[][] x,
                             long from,
                             long to,
                             ShortComparator comp)
Sorts the specified range of elements according to the order induced by the specified comparator using quicksort.

The sorting algorithm is a tuned quicksort adapted from Jon L. Bentley and M. Douglas McIlroy, “Engineering a Sort Function”, Software: Practice and Experience, 23(11), pages 1249−1265, 1993.

Parameters:
x - the big array to be sorted.
from - the index of the first element (inclusive) to be sorted.
to - the index of the last element (exclusive) to be sorted.
comp - the comparator to determine the sorting order.

quickSort

public static void quickSort(short[][] x,
                             ShortComparator comp)
Sorts the specified big array according to the order induced by the specified comparator using quicksort.

The sorting algorithm is a tuned quicksort adapted from Jon L. Bentley and M. Douglas McIlroy, “Engineering a Sort Function”, Software: Practice and Experience, 23(11), pages 1249−1265, 1993.

Parameters:
x - the big array to be sorted.
comp - the comparator to determine the sorting order.

quickSort

public static void quickSort(short[][] x,
                             long from,
                             long to)
Sorts the specified range of elements according to the natural ascending order using quicksort.

The sorting algorithm is a tuned quicksort adapted from Jon L. Bentley and M. Douglas McIlroy, “Engineering a Sort Function”, Software: Practice and Experience, 23(11), pages 1249−1265, 1993.

Parameters:
x - the big array to be sorted.
from - the index of the first element (inclusive) to be sorted.
to - the index of the last element (exclusive) to be sorted.

quickSort

public static void quickSort(short[][] x)
Sorts the specified big array according to the natural ascending order using quicksort.

The sorting algorithm is a tuned quicksort adapted from Jon L. Bentley and M. Douglas McIlroy, “Engineering a Sort Function”, Software: Practice and Experience, 23(11), pages 1249−1265, 1993.

Parameters:
x - the big array to be sorted.

binarySearch

public static long binarySearch(short[][] a,
                                long from,
                                long to,
                                short key)
Searches a range of the specified big array for the specified value using the binary search algorithm. The range must be sorted prior to making this call. If it is not sorted, the results are undefined. If the range contains multiple elements with the specified value, there is no guarantee which one will be found.

Parameters:
a - the big array to be searched.
from - the index of the first element (inclusive) to be searched.
to - the index of the last element (exclusive) to be searched.
key - the value to be searched for.
Returns:
index of the search key, if it is contained in the big array; otherwise, (-(insertion point) - 1). The insertion point is defined as the the point at which the value would be inserted into the big array: the index of the first element greater than the key, or the length of the big array, if all elements in the big array are less than the specified key. Note that this guarantees that the return value will be >= 0 if and only if the key is found.
See Also:
Arrays

binarySearch

public static long binarySearch(short[][] a,
                                short key)
Searches a big array for the specified value using the binary search algorithm. The range must be sorted prior to making this call. If it is not sorted, the results are undefined. If the range contains multiple elements with the specified value, there is no guarantee which one will be found.

Parameters:
a - the big array to be searched.
key - the value to be searched for.
Returns:
index of the search key, if it is contained in the big array; otherwise, (-(insertion point) - 1). The insertion point is defined as the the point at which the value would be inserted into the big array: the index of the first element greater than the key, or the length of the big array, if all elements in the big array are less than the specified key. Note that this guarantees that the return value will be >= 0 if and only if the key is found.
See Also:
Arrays

binarySearch

public static long binarySearch(short[][] a,
                                long from,
                                long to,
                                short key,
                                ShortComparator c)
Searches a range of the specified big array for the specified value using the binary search algorithm and a specified comparator. The range must be sorted following the comparator prior to making this call. If it is not sorted, the results are undefined. If the range contains multiple elements with the specified value, there is no guarantee which one will be found.

Parameters:
a - the big array to be searched.
from - the index of the first element (inclusive) to be searched.
to - the index of the last element (exclusive) to be searched.
key - the value to be searched for.
c - a comparator.
Returns:
index of the search key, if it is contained in the big array; otherwise, (-(insertion point) - 1). The insertion point is defined as the the point at which the value would be inserted into the big array: the index of the first element greater than the key, or the length of the big array, if all elements in the big array are less than the specified key. Note that this guarantees that the return value will be >= 0 if and only if the key is found.
See Also:
Arrays

binarySearch

public static long binarySearch(short[][] a,
                                short key,
                                ShortComparator c)
Searches a big array for the specified value using the binary search algorithm and a specified comparator. The range must be sorted following the comparator prior to making this call. If it is not sorted, the results are undefined. If the range contains multiple elements with the specified value, there is no guarantee which one will be found.

Parameters:
a - the big array to be searched.
key - the value to be searched for.
c - a comparator.
Returns:
index of the search key, if it is contained in the big array; otherwise, (-(insertion point) - 1). The insertion point is defined as the the point at which the value would be inserted into the big array: the index of the first element greater than the key, or the length of the big array, if all elements in the big array are less than the specified key. Note that this guarantees that the return value will be >= 0 if and only if the key is found.
See Also:
Arrays

shuffle

public static short[][] shuffle(short[][] a,
                                long from,
                                long to,
                                Random random)
Shuffles the specified big array fragment using the specified pseudorandom number generator.

Parameters:
a - the big array to be shuffled.
from - the index of the first element (inclusive) to be shuffled.
to - the index of the last element (exclusive) to be shuffled.
random - a pseudorandom number generator (please use a XorShift* generator).
Returns:
a.

shuffle

public static short[][] shuffle(short[][] a,
                                Random random)
Shuffles the specified big array using the specified pseudorandom number generator.

Parameters:
a - the big array to be shuffled.
random - a pseudorandom number generator (please use a XorShift* generator).
Returns:
a.


Copyright © 2011. All Rights Reserved.