Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /**
- * @param array is a simple (all nested arrays at the same depth have the same size) array.
- * @return the dimensions of the array.
- * @deprecated there might be a better way to calculate.
- */
- public static int[] calculateLengthsEvenArray(Object array) {
- int[] lengths = new int[calculateDeepArray(array)];
- Object tempLink = array;
- lengths[0] = Array.getLength(array);
- for (int i = 1; i < lengths.length; ++i) {
- lengths[i] = Array.getLength((tempLink = Array.get(tempLink, 0)));
- }
- return lengths;
- }
- /**
- * @param array the array to be checked for simplicity (all nested arrays at the same depth have the same size)
- * @return whether the array is simple
- */
- public static boolean isEvenArray(Object array) {
- int[] lengths = new int[calculateDeepArray(array)], indexed = new int[lengths.length - 1];
- Arrays.fill(lengths, -1);
- Object[] tempLinks = new Object[lengths.length];
- tempLinks[0] = array;
- int arrow = 1;
- mainLoop:
- while (true) {
- tempLinks[arrow] = Array.get(tempLinks[arrow - 1], indexed[arrow - 1]);
- if (tempLinks[arrow] == null) {
- return false;
- }
- if (lengths[arrow] == -1) {
- lengths[arrow] = Array.getLength(tempLinks[arrow]);
- } else if (lengths[arrow] != Array.getLength(tempLinks[arrow])) {
- return false;
- }
- if (arrow < lengths.length - 1) {
- ++arrow;
- } else {
- ++indexed[arrow - 1];
- }
- while (Array.getLength(tempLinks[arrow - 1]) <= indexed[arrow - 1]) {
- if (--arrow != 0) {
- indexed[arrow] = 0;
- ++indexed[arrow - 1];
- } else {
- break mainLoop;
- }
- }
- }
- return true;
- }
Advertisement
Add Comment
Please, Sign In to add comment