Class ByteArray

java.lang.Object
hlcd.testing.arrayTester.ByteArray

public class ByteArray extends Object
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

    Fields
    Modifier and Type
    Field
    Description
    byte[][]
    The structure used to store the codewords of the code.
    static int
    The 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 int
    The maximum size of each 1-D array.
  • Constructor Summary

    Constructors
    Constructor
    Description
    ByteArray​(byte n, byte k, byte base)
    Creates a combination array filled with zeros based on the parameters specified.
    ByteArray​(long size)
    Creates a combination array filled with zeros based on the size specified.
  • Method Summary

    Modifier and Type
    Method
    Description
    boolean
    contains​(long value)
    Checks if the value specified is exists in the combination array.
    boolean
    equalsTo​(ByteArray passed)
    Compares whether the specified combination array is the same as the current one.
    long
    get​(long index)
    Returns a specific element in the combination array.
    long
    Returns the number of elements.
    static void
    main​(String[] args)
    Executes the program.
    void
    Prints the elements in the combination array on screen.
    void
    print​(String delimiter, Style style)
    Prints the elements in the combination array on screen as binary values or decimal values.
    void
    set​(long index, byte value)
    Sets the value specified at the appropriate index.

    Methods inherited from class java.lang.Object

    clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
  • Field Details

    • ARRAY

      public final byte[][] ARRAY
      The structure used to store the codewords of the code.
    • JVM_OVERHEAD

      public static final int JVM_OVERHEAD
      The 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_LENGTH
      The 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 code
      k - the dimension of the code
      base - 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 set
      value - 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:
      true if the value exists, false otherwise
    • print

      public void print()
      Prints the elements in the combination array on screen. The values will be printed in base 10.
    • print

      public void print(String delimiter, Style style)
      Prints the elements in the combination array on screen as binary values or decimal values.
      Parameters:
      delimiter - the delimiter between each digit
      style - the style format which could either be binary, quaternary, decimal or \(\LaTeX\)
      See Also:
      print()
    • equalsTo

      public boolean equalsTo(ByteArray passed)
      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 return false.
      Parameters:
      passed - the combination array to be compared to
      Returns:
      true if both combination arrays are the same, false otherwise
    • main

      public static void main(String[] args)
      Executes the program.
      Parameters:
      args - the arguments specified but will be ignored