Package hlcd.operations
Class LongArray
java.lang.Object
hlcd.operations.LongArray
A structure for storing the linear combinations of the code. It uses a 2-D
array for storage. The size is determined based on the fact that a code
will have \(base^k\) linear combinations. 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.
NOTE: the code has been tested for small values of \(k\) (i.e., for \(k \lt 16\)) when the base is \(4\). In general, \(64 \times base^k\) bits are required to be stored in memory.
TODO: further test this class when the base is \(4\) and \(k\) is \(16\) or larger.
//minimal example to execute this class:
public static void main(String[] args) {
System.out.println("Running LongArray...");
byte n = 20;
byte k = 14;
byte base = 4;
long size = Functions.power(base, k);//or some number like 1L, 2L, etc
//Use the following if testing with limit RAM space is required:
/*
//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 = (64 * size) / 8000000000.0;
System.out.println("Total GBs of RAM used: " + sizeInGb);
LongArray array = new LongArray(size);//or the next line:
//array = new LongArray(n, k, base);
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, ..., 9223372036854775806,
//9223372036854775807, 0, 1, ...
for (long i = 0; i < size; i++) {
array.set(i, i % Long.MAX_VALUE);
}
//check each cell to ensure the above sequence is present
for (long i = 0; i < size; i++) {
if (array.get(i) != i % Long.MAX_VALUE) {
System.out.println(
"i = " + i + ", " +
"value stored = " + array.get(i) + ", " +
"correct value = " + i % Long.MAX_VALUE
);
}
}
//Should the elements of the array be printed
//array.print();// or use the next line for formatting
//array.print(" ", Style.DECIMAL);
System.out.println("LongArray completed.");
}- Since:
- April 13th, 2022
- Version:
- 1.0
- Author:
- Maysara Al Jumaily
-
Field Summary
FieldsModifier and TypeFieldDescriptionlong[][]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, long value)Sets the value specified at the appropriate index.
-
Field Details
-
ARRAY
public final long[][] 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
-
LongArray
public LongArray(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\)
-
LongArray
public LongArray(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, long 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
-