package org.davidmoten.hilbert;

final class Util {

    private Util() {
        // prevent instantiation
    }

    // visible for testing
    static void reverse(byte[] array) {
        if (array == null) {
            return;
        }
        int i = 0;
        int j = array.length - 1;
        byte tmp;
        while (j > i) {
            tmp = array[j];
            array[j] = array[i];
            array[i] = tmp;
            j--;
            i++;
        }
    }

    static boolean allZero(int[] a) {
        for (int i = 0; i < a.length; i++) {
            if (a[i] != 0)
                return false;
        }
        return true;
    }

    /**
     * Returns the least value x where min(a,b) <= x < max(a,b) and x has the
     * maximum number of trailing zeroes in its binary representation.
     * 
     * @param a
     *            first value
     * @param b
     *            second value
     * @return most significant (in terms of divisors of powers of 2) between a and
     *         b
     */
    static long mostSignificantBetween(long a, long b) {
        if (a > b) {
            return mostSignificantBetween(b, a);
        } else if (a == b) {
            return a;
        } else {
            long x = a == 0 ? 1 : a;
            int bit = 0;
            while (x < b) {
                if ((x & (1 << bit)) == 0) {
                    bit++;
                } else {
                    long y = x + (1 << bit);
                    if (y < b) {
                        bit++;
                        x = y;
                    } else {
                        break;
                    }
                }
            }
            return x;
        }
    }

    public static void zero(long[] x) {
        for (int i = 0; i < x.length; i++) {
            x[i] = 0;
        }
    }

}

@@ -26,16 +26,42 @@
 lbert;%0A%0A
+import java.util.Arrays;%0A%0A
 final cl
@@ -498,186 +498,8 @@
  %7D%0A%0A
-    static boolean allZero(int%5B%5D a) %7B%0A        for (int i = 0; i %3C a.length; i++) %7B%0A            if (a%5Bi%5D != 0)%0A                return false;%0A        %7D%0A        return true;%0A    %7D%0A%0A
     
@@ -1588,76 +1588,26 @@
     
-for (int i = 0; i %3C x.length; i++) %7B%0A            x%5Bi%5D = 0;%0A        %7D
+Arrays.fill(x, 0);
 %0A   

use Arrays.fill