|
||||||||||
PREV CLASS NEXT CLASS | FRAMES NO FRAMES | |||||||||
SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD |
java.lang.Objectit.unimi.dsi.fastutil.bytes.ByteArrays
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.
Arrays
Field Summary | |
static byte[] |
EMPTY_ARRAY
A static, final, empty array. |
static Hash.Strategy |
HASH_STRATEGY
A type-specific content-based hash strategy for arrays. |
Method Summary | |
static byte[] |
copy(byte[] array)
Returns a copy of an array. |
static byte[] |
copy(byte[] array,
int offset,
int length)
Returns a copy of a portion of an array. |
static byte[] |
ensureCapacity(byte[] array,
int length)
Ensures that an array can contain the given number of entries. |
static byte[] |
ensureCapacity(byte[] 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(byte[] a,
int from,
int to)
Ensures that a range given by its first (inclusive) and last (exclusive) elements fits an array. |
static void |
ensureOffsetLength(byte[] a,
int offset,
int length)
Ensures that a range given by an offset and a length fits an array. |
static boolean |
equals(byte[] a1,
byte[] a2)
Returns true if the two arrays are elementwise equal. |
static void |
fill(byte[] array,
byte value)
Fills the given array with the given value. |
static void |
fill(byte[] array,
int from,
int to,
byte value)
Fills a portion of the given array with the given value. |
static byte[] |
grow(byte[] 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 byte[] |
grow(byte[] 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 byte[] |
setLength(byte[] array,
int length)
Sets the length of the given array. |
static byte[] |
trim(byte[] 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 |
public static final byte[] EMPTY_ARRAY
public static final Hash.Strategy HASH_STRATEGY
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 |
public static byte[] ensureCapacity(byte[] array, int length)
If you cannot foresee whether this array will need again to be
enlarged, you should probably use grow()
instead.
array
- an array.length
- the new minimum length for this array.
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
.public static byte[] ensureCapacity(byte[] array, int length, int preserve)
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.
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
.public static byte[] grow(byte[] array, int 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.
array
- an array.length
- the new minimum length for this array.
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
.public static byte[] grow(byte[] array, int length, int preserve)
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.
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.
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
.public static byte[] trim(byte[] array, int length)
array
- an array.length
- the new maximum length for the array.
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
.public static byte[] setLength(byte[] array, int length)
array
- an array.length
- the new length for the array.
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
.public static byte[] copy(byte[] array, int offset, int length)
array
- an array.offset
- the first element to copy.length
- the number of elements to copy.
length
elements of array
starting at offset
.public static byte[] copy(byte[] array)
array
- an array.
array
.public static void fill(byte[] array, byte value)
This method uses a backward loop. It is significantly faster than the corresponding
method in Arrays
.
array
- an array.value
- the new value for all elements of the array.public static void fill(byte[] array, int from, int to, byte 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
.
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.public static boolean equals(byte[] a1, byte[] a2)
This method uses a backward loop. It is significantly faster than the corresponding
method in Arrays
.
a1
- an array.a2
- another array.
public static void ensureFromTo(byte[] a, int from, int to)
This method may be used whenever an array range check is needed.
a
- an array.from
- a start index (inclusive).to
- an end index (inclusive).
IllegalArgumentException
- if from
is greater than to
.
ArrayIndexOutOfBoundsException
- if from
or to
are greater than the array length or negative.public static void ensureOffsetLength(byte[] a, int offset, int length)
This method may be used whenever an array range check is needed.
a
- an array.offset
- a start index.length
- a length (the number of elements in the range).
IllegalArgumentException
- if length
is negative.
ArrayIndexOutOfBoundsException
- if offset
is negative or offset
+length
is greater than the array length.
|
||||||||||
PREV CLASS NEXT CLASS | FRAMES NO FRAMES | |||||||||
SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD |