Java Unsigned Number Create unsignedOneSidedGallopingIntersect2by2(final short[] smallSet, final int smallLength, final short[] largeSet, final int largeLength, final short[] buffer)

Here you can find the source of unsignedOneSidedGallopingIntersect2by2(final short[] smallSet, final int smallLength, final short[] largeSet, final int largeLength, final short[] buffer)

Description

unsigned One Sided Galloping Intersectby

License

Apache License

Declaration

protected static int unsignedOneSidedGallopingIntersect2by2(final short[] smallSet, final int smallLength,
            final short[] largeSet, final int largeLength, final short[] buffer) 

Method Source Code

//package com.java2s;
/*/*from  w  ww.  ja  v a  2s  .c  o  m*/
 * (c) the authors Licensed under the Apache License, Version 2.0.
 */

public class Main {
    protected static int unsignedOneSidedGallopingIntersect2by2(final short[] smallSet, final int smallLength,
            final short[] largeSet, final int largeLength, final short[] buffer) {
        if (0 == smallLength) {
            return 0;
        }
        int k1 = 0;
        int k2 = 0;
        int pos = 0;
        short s1 = largeSet[k1];
        short s2 = smallSet[k2];
        while (true) {
            if (toIntUnsigned(s1) < toIntUnsigned(s2)) {
                k1 = advanceUntil(largeSet, k1, largeLength, s2);
                if (k1 == largeLength) {
                    break;
                }
                s1 = largeSet[k1];
            }
            if (toIntUnsigned(s2) < toIntUnsigned(s1)) {
                ++k2;
                if (k2 == smallLength) {
                    break;
                }
                s2 = smallSet[k2];
            } else {
                // (set2[k2] == set1[k1])
                buffer[pos++] = s2;
                ++k2;
                if (k2 == smallLength) {
                    break;
                }
                s2 = smallSet[k2];
                k1 = advanceUntil(largeSet, k1, largeLength, s2);
                if (k1 == largeLength) {
                    break;
                }
                s1 = largeSet[k1];
            }

        }
        return pos;

    }

    protected static int toIntUnsigned(short x) {
        return x & 0xFFFF;
    }

    /**
     * Find the smallest integer larger than pos such that array[pos]&gt;= min. If none can be found,
     * return length. Based on code by O. Kaser.
     *
     * @param array array to search within
     * @param pos starting position of the search
     * @param length length of the array to search
     * @param min minimum value
     * @return x greater than pos such that array[pos] is at least as large as min, pos is is equal to
     *         length if it is not possible.
     */
    public static int advanceUntil(short[] array, int pos, int length, short min) {
        int lower = pos + 1;

        // special handling for a possibly common sequential case
        if (lower >= length || toIntUnsigned(array[lower]) >= toIntUnsigned(min)) {
            return lower;
        }

        int spansize = 1; // could set larger
        // bootstrap an upper limit

        while (lower + spansize < length && toIntUnsigned(array[lower + spansize]) < toIntUnsigned(min)) {
            spansize *= 2; // hoping for compiler will reduce to
        }
        // shift
        int upper = (lower + spansize < length) ? lower + spansize : length - 1;

        // maybe we are lucky (could be common case when the seek ahead
        // expected
        // to be small and sequential will otherwise make us look bad)
        if (array[upper] == min) {
            return upper;
        }

        if (toIntUnsigned(array[upper]) < toIntUnsigned(min)) {// means
            // array
            // has no
            // item
            // >= min
            // pos = array.length;
            return length;
        }

        // we know that the next-smallest span was too small
        lower += (spansize / 2);

        // else begin binary search
        // invariant: array[lower]<min && array[upper]>min
        while (lower + 1 != upper) {
            int mid = (lower + upper) / 2;
            short arraymid = array[mid];
            if (arraymid == min) {
                return mid;
            } else if (toIntUnsigned(arraymid) < toIntUnsigned(min)) {
                lower = mid;
            } else {
                upper = mid;
            }
        }
        return upper;

    }
}

Related

  1. unsignedLongToByteArray(final long value)
  2. unsignedLongToString(long value)
  3. unsignedLongToString(long x)
  4. unsignedMediumToBytes(final Long unsignedInt)
  5. unsignedNumericToByteArray(long src, int length)
  6. unsignedShort(byte b)
  7. unsignedShort(short s)
  8. unsignedShort2Arr(int var, byte[] arrayBytes, int startIndex)
  9. unsignedShortAt(byte[] data, int pos)