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

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

Description

Compute the cardinality of the intersection

License

Apache License

Parameter

Parameter Description
set1 first set
length1 how many values to consider in the first set
set2 second set
length2 how many values to consider in the second set

Return

cardinality of the intersection

Declaration

public static int unsignedLocalIntersect2by2Cardinality(final short[] set1, final int length1,
        final short[] set2, final int length2) 

Method Source Code

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

public class Main {
    /**
     * Compute the cardinality of the intersection
     * @param set1 first set
     * @param length1 how many values to consider in the first set
     * @param set2 second set
     * @param length2 how many values to consider in the second set
     * @return cardinality of the intersection
     */
    public static int unsignedLocalIntersect2by2Cardinality(final short[] set1, final int length1,
            final short[] set2, final int length2) {
        if ((0 == length1) || (0 == length2)) {
            return 0;
        }
        int k1 = 0;
        int k2 = 0;
        int pos = 0;
        short s1 = set1[k1];
        short s2 = set2[k2];

        mainwhile: while (true) {
            int v1 = toIntUnsigned(s1);
            int v2 = toIntUnsigned(s2);
            if (v2 < v1) {
                do {
                    ++k2;
                    if (k2 == length2) {
                        break mainwhile;
                    }
                    s2 = set2[k2];
                    v2 = toIntUnsigned(s2);
                } while (v2 < v1);
            }
            if (v1 < v2) {
                do {
                    ++k1;
                    if (k1 == length1) {
                        break mainwhile;
                    }
                    s1 = set1[k1];
                    v1 = toIntUnsigned(s1);
                } while (v1 < v2);
            } else {
                // (set2[k2] == set1[k1])
                pos++;
                ++k1;
                if (k1 == length1) {
                    break;
                }
                ++k2;
                if (k2 == length2) {
                    break;
                }
                s1 = set1[k1];
                s2 = set2[k2];
            }
        }
        return pos;
    }

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

Related

  1. unsignedIntToString(int x, int radix)
  2. unsignedIntValue(byte[] data, int offset)
  3. unsignedLeb128Size(int value)
  4. unsignedLessThan(long a, long b)
  5. unsignedLocalIntersect2by2(final short[] set1, final int length1, final short[] set2, final int length2, final short[] buffer)
  6. unsignedLong(byte b)
  7. unsignedLongToByteArray(final long value)
  8. unsignedLongToString(long value)
  9. unsignedLongToString(long x)