com.carrotsearch.hppcrt.ShortArrays Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of hppcrt Show documentation
Show all versions of hppcrt Show documentation
High Performance Primitive Collections Realtime
(fork of HPPC from Carrotsearch)
Fundamental data structures (maps, sets, lists, queues, heaps, sorts) generated for
combinations of object and primitive types to conserve JVM memory and speed
up execution. The Realtime fork intends to extend the existing collections, by tweaking to remove any dynamic allocations at runtime,
and to obtain low variance execution times whatever the input nature.
The newest version!
package com.carrotsearch.hppcrt;
import java.util.Arrays;
/**
* Utility class gathering array or ShortIndexedContainer handling algorithms for short
s.
* This is a kind of complement for {@link java.util.Arrays}.
*/
@javax.annotation.Generated(
date = "2017-07-11T19:16:23+0200",
value = "KTypeArrays.java")
public final class ShortArrays
{
/**
* public static instance of an empty array of short
s.
*/
public final static short[] EMPTY = (new short[(0)]);
final private static int BLANK_ARRAY_SIZE_IN_BIT_SHIFT = 10;
/**
* Batch blanking array size
*/
final private static int BLANK_ARRAY_SIZE = 1 << ShortArrays.BLANK_ARRAY_SIZE_IN_BIT_SHIFT;
/**
* Batch blanking array with short nulls
*/
final private static short[] BLANKING_OBJECT_ARRAY = (new short[(ShortArrays.BLANK_ARRAY_SIZE)]);
private ShortArrays() {
//nothing
}
/**
* Rotate utility :
* Transforms the range [[slice_1: from; mid - 1][slice_2: mid, to - 1]] of table, into
* [[slice_2][slice_1]]in place, i.e swap the two slices while keeping their own internal order.
* @param table
* @param from the start range to consider
* @param mid start index of the second slice
* @param to the array end range, exclusive
*/
public static void rotate(final short[] table, final int from, final int mid, final int to) {
ShortArrays.reverse(table, from, mid);
ShortArrays.reverse(table, mid, to);
ShortArrays.reverse(table, from, to);
}
/**
* Rotate utility :
* Transforms the range [[slice_1: from; mid - 1][slice_2: mid, to - 1]] of ShortIndexedContainer, into
* [[slice_2][slice_1]] in place, i.e swap the two slices while keeping their own internal order.
* @param table
* @param from the start range to consider
* @param mid start index of the second slice
* @param to the array end range, exclusive
*/
public static void rotate(final ShortIndexedContainer table, final int from, final int mid, final int to) {
ShortArrays.reverse(table, from, mid);
ShortArrays.reverse(table, mid, to);
ShortArrays.reverse(table, from, to);
}
/**
* Reverse the elements positions of the specified range of array table :
* @param table
* @param from the start range to consider
* @param to the array end range, exclusive
*/
public static void reverse(final short[] table, final int from, final int to) {
final int halfSize = (to - from) >>> 1;
short tmpValue;
for (int i = 0; i < halfSize; i++)
{
tmpValue = table[i + from];
table[i + from] = table[to - i - 1];
table[to - i - 1] = tmpValue;
}
}
/**
* Reverse the elements positions of the specified range of ShortIndexedContainer table :
* @param table
* @param from the start range to consider
* @param to the array end range, exclusive
*/
public static void reverse(final ShortIndexedContainer table, final int from, final int to) {
final int halfSize = (to - from) >>> 1;
short tmpValue;
for (int i = 0; i < halfSize; i++)
{
tmpValue = table.get(i + from);
table.set(i + from, table.get(to - i - 1));
table.set(to - i - 1, tmpValue);
}
}
/**
* Method to blank any short[] array elements to its default value
* from [startIndex; endIndex[, equivalent to {@link Arrays}.fill(objectArray, startIndex, endIndex, 0 or null)
*/
public static void blankArray(final short[] objectArray, final int startIndex, final int endIndex) {
assert startIndex <= endIndex;
final int size = endIndex - startIndex;
final int nbChunks = size >> ShortArrays.BLANK_ARRAY_SIZE_IN_BIT_SHIFT;
//compute remainder
final int rem = size & (ShortArrays.BLANK_ARRAY_SIZE - 1);
for (int i = 0; i < nbChunks; i++) {
System.arraycopy(ShortArrays.BLANKING_OBJECT_ARRAY, 0,
objectArray, startIndex + (i << ShortArrays.BLANK_ARRAY_SIZE_IN_BIT_SHIFT),
ShortArrays.BLANK_ARRAY_SIZE);
} //end for
//fill the reminder
if (rem > 0) {
Arrays.fill(objectArray, startIndex + (nbChunks << ShortArrays.BLANK_ARRAY_SIZE_IN_BIT_SHIFT),
startIndex + (nbChunks << ShortArrays.BLANK_ARRAY_SIZE_IN_BIT_SHIFT) + rem, ((short)0));
}
}
}