Guest User

Untitled

a guest
Mar 29th, 2023
46
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 1.79 KB | None | 0 0
  1. /**
  2.  * @param array is a simple (all nested arrays at the same depth have the same size) array.
  3.  * @return the dimensions of the array.
  4.  * @deprecated there might be a better way to calculate.
  5.  */
  6. public static int[] calculateLengthsEvenArray(Object array) {
  7.     int[] lengths = new int[calculateDeepArray(array)];
  8.     Object tempLink = array;
  9.     lengths[0] = Array.getLength(array);
  10.     for (int i = 1; i < lengths.length; ++i) {
  11.         lengths[i] = Array.getLength((tempLink = Array.get(tempLink, 0)));
  12.     }
  13.     return lengths;
  14. }
  15.  
  16. /**
  17.  * @param array the array to be checked for simplicity (all nested arrays at the same depth have the same size)
  18.  * @return whether the array is simple
  19.  */
  20. public static boolean isEvenArray(Object array) {
  21.     int[] lengths = new int[calculateDeepArray(array)], indexed = new int[lengths.length - 1];
  22.     Arrays.fill(lengths, -1);
  23.     Object[] tempLinks = new Object[lengths.length];
  24.     tempLinks[0] = array;
  25.     int arrow = 1;
  26.  
  27.     mainLoop:
  28.     while (true) {
  29.         tempLinks[arrow] = Array.get(tempLinks[arrow - 1], indexed[arrow - 1]);
  30.         if (tempLinks[arrow] == null) {
  31.             return false;
  32.         }
  33.         if (lengths[arrow] == -1) {
  34.             lengths[arrow] = Array.getLength(tempLinks[arrow]);
  35.         } else if (lengths[arrow] != Array.getLength(tempLinks[arrow])) {
  36.             return false;
  37.         }
  38.  
  39.         if (arrow < lengths.length - 1) {
  40.             ++arrow;
  41.         } else {
  42.             ++indexed[arrow - 1];
  43.         }
  44.  
  45.         while (Array.getLength(tempLinks[arrow - 1]) <= indexed[arrow - 1]) {
  46.             if (--arrow != 0) {
  47.                 indexed[arrow] = 0;
  48.                 ++indexed[arrow - 1];
  49.             } else {
  50.                 break mainLoop;
  51.             }
  52.         }
  53.     }
  54.     return true;
  55. }
Advertisement
Add Comment
Please, Sign In to add comment