Package hlcd.testing.arrayTester
Class ByteArray
java.lang.Object
hlcd.testing.arrayTester.ByteArray
A tester class that uses
bytes instead of longs to store
vectors of 8-bits. It is created to test the structure implemented in
LongArray without the need to have significant amount of RAM. It
uses a 2-D array for storage. The size can be chosen arbitrary without
constraints or as \(base^k\). Each 1-D array also referred to as a
segment. This class uses
Integer.MAX_VALUE - JVM_OVERHEAD as the
maximum segment size (except possibly for the last segment). The last segment
will contain the remaining cells which could be less than or equal to the
maximum segment size. A 2-D array is used because Java's maximum 1-D array
is Integer.MAX_VALUE - 8 or
\(2^{31} - 1 - 8 = 2,147,483,647 - 8 = 2,147,483,639\), which is not
sufficient for large values of \(k\) of quaternary codes. The value of
JVM_OVERHEAD is 16 and is needed because VMs
reserve some header words in an array which reduces the true
maximum value. Java 8's implementation of Hashtable in
line 397 has MAX_ARRAY_SIZE as
Integer.MAX_VALUE - 8. We are using - 16 for extra
precaution.
//minimal example to execute this class:
public static void main(String[] args) {
System.out.println("Running ByteArray...");
byte n = 20;
byte k = 16;
byte base = 4;
long size = Functions.power(base, k);//or some number like 1L, 2L, etc
//Use the following if testing needs to be performed:
/*
//Make MAX_SEGMENT_LENGTH hold a smaller number, like 10000
long full = (long) (Math.random() * 6);//number of full segments
long extra = (long) (Math.random() * 10000);//last segment size
if (Math.random() < 0.5 && full != 0) {
extra *= -1;
}
size = MAX_SEGMENT_LENGTH * full + extra;
*/
double sizeInGb = (8 * size) / 8000000000.0;
System.out.println("Total GBs of RAM used: " + sizeInGb);
ByteArray array = new ByteArray(size);
System.out.println("Size: " + array.SIZE);
//print the lengths of each 1-D array
System.out.print("[");
for (int i = 0; i < array.ARRAY.length; i++) {
System.out.print(array.ARRAY[i].length);
if (i != array.ARRAY.length - 1) {
System.out.print(", ");
}
}
System.out.println("]");
//stores the sequence 0, 1, ..., 126, 127, 0, 1, ...
for (long i = 0; i < size; i++) {
array.set(i, (byte) (i % 128L));
}
//check each cell to ensure the above sequence is present
for (long i = 0; i < size; i++) {
if (array.get(i) != i % 128L) {
System.out.println(
"i = " + i + ", " +
"value stored = " + array.get(i) + ", " +
"correct value = " + (byte) (i % 128)
);
}
}
//Should the elements of the array be printed
//array.print();// or use the next line for formatting
//array.print(" ", Style.DECIMAL);
System.out.println("ByteArray completed.");
}- Since:
- April 13th, 2022
- Version:
- 1.0
- Author:
- Maysara Al Jumaily
-
Field Summary
FieldsModifier and TypeFieldDescriptionbyte[][]The structure used to store the codewords of the code.static intThe amount of cells that are excluded from an array because VMs reserve some header words in an array which reduces the true maximum value.static intThe maximum size of each 1-D array. -
Constructor Summary
Constructors -
Method Summary
Modifier and TypeMethodDescriptionbooleancontains(long value)Checks if the value specified is exists in the combination array.booleanCompares whether the specified combination array is the same as the current one.longget(long index)Returns a specific element in the combination array.longlength()Returns the number of elements.static voidExecutes the program.voidprint()Prints the elements in the combination array on screen.voidPrints the elements in the combination array on screen as binary values or decimal values.voidset(long index, byte value)Sets the value specified at the appropriate index.
-
Field Details
-
ARRAY
public final byte[][] ARRAYThe structure used to store the codewords of the code. -
JVM_OVERHEAD
public static final int JVM_OVERHEADThe amount of cells that are excluded from an array because VMs reserve some header words in an array which reduces the true maximum value. It could be at least 8 but 16 is used for extra precaution.- See Also:
- Constant Field Values
-
MAX_SEGMENT_LENGTH
public static int MAX_SEGMENT_LENGTHThe maximum size of each 1-D array.
-
-
Constructor Details
-
ByteArray
public ByteArray(byte n, byte k, byte base)Creates a combination array filled with zeros based on the parameters specified.- Parameters:
n- the length of the codek- the dimension of the codebase- the base of the code which could either be \(2\) or \(4\)
-
ByteArray
public ByteArray(long size)Creates a combination array filled with zeros based on the size specified. This should only be used for testing purposes to ensure there doesn't exist a bug in dividing the arrays and/or accessing/setting specific indices.- Parameters:
size- the number of cells to create
-
-
Method Details
-
get
public final long get(long index)Returns a specific element in the combination array.- Parameters:
index- the index of the element- Returns:
- the element in the index specified
-
set
public final void set(long index, byte value)Sets the value specified at the appropriate index.- Parameters:
index- the index of the element to setvalue- the value to be assigned to
-
length
public long length()Returns the number of elements.- Returns:
- the number of elements
-
contains
public boolean contains(long value)Checks if the value specified is exists in the combination array.- Parameters:
value- the value to look for- Returns:
trueif the value exists,falseotherwise
-
print
public void print()Prints the elements in the combination array on screen. The values will be printed in base 10. -
print
Prints the elements in the combination array on screen as binary values or decimal values.- Parameters:
delimiter- the delimiter between each digitstyle- the style format which could either be binary, quaternary, decimal or \(\LaTeX\)- See Also:
print()
-
equalsTo
Compares whether the specified combination array is the same as the current one. By same, it means both arrays have the same values stored in the same order. Having the same values but in shuffled order will returnfalse.- Parameters:
passed- the combination array to be compared to- Returns:
trueif both combination arrays are the same,falseotherwise
-
main
Executes the program.- Parameters:
args- the arguments specified but will be ignored
-