Java Unsigned Number Create unsignedIntersects(short[] set1, int length1, short[] set2, int length2)

Here you can find the source of unsignedIntersects(short[] set1, int length1, short[] set2, int length2)

Description

Checks if two arrays intersect

License

Apache License

Parameter

Parameter Description
set1 first array
length1 length of first array
set2 second array
length2 length of second array

Return

true if they intersect

Declaration

public static boolean unsignedIntersects(short[] set1, int length1, short[] set2, int length2) 

Method Source Code

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

public class Main {
    /**
     * Checks if two arrays intersect
     *
     * @param set1 first array
     * @param length1 length of first array
     * @param set2 second array
     * @param length2 length of second array
     * @return true if they intersect
     */
    public static boolean unsignedIntersects(short[] set1, int length1, short[] set2, int length2) {
        // galloping might be faster, but we do not expect this function to be slow
        if ((0 == length1) || (0 == length2)) {
            return false;
        }
        int k1 = 0;
        int k2 = 0;
        short s1 = set1[k1];
        short s2 = set2[k2];
        mainwhile: while (true) {
            if (toIntUnsigned(s2) < toIntUnsigned(s1)) {
                do {
                    ++k2;
                    if (k2 == length2) {
                        break mainwhile;
                    }
                    s2 = set2[k2];
                } while (toIntUnsigned(s2) < toIntUnsigned(s1));
            }
            if (toIntUnsigned(s1) < toIntUnsigned(s2)) {
                do {
                    ++k1;
                    if (k1 == length1) {
                        break mainwhile;
                    }
                    s1 = set1[k1];
                } while (toIntUnsigned(s1) < toIntUnsigned(s2));
            } else {
                return true;
            }
        }
        return false;
    }

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

Related

  1. unsignedInt2ByteLE(byte[] bytes, long value, int offset)
  2. unsignedInt2Long(int x)
  3. unsignedInt32ToBytes(long v)
  4. unsignedIntArray2signedIntArray(int[] myIntArray, int numberOfBytes)
  5. unsignedIntersect2by2(final short[] set1, final int length1, final short[] set2, final int length2, final short[] buffer)
  6. unsignedIntToByteArray(long value)
  7. unsignedIntToHex(int i)
  8. unsignedIntToInt(byte[] b)
  9. unsignedIntToLong(byte[] b)