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

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

Description

Unite two sorted lists and write the result to the provided output array

License

Apache License

Parameter

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

Return

cardinality of the union

Declaration

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

Method Source Code

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

public class Main {
    /**
     * Unite two sorted lists and write the result to the provided output array
     *
     * @param set1 first array
     * @param length1 length of first array
     * @param set2 second array
     * @param length2 length of second array
     * @param buffer output array
     * @return cardinality of the union
     */
    public static int unsignedUnion2by2(final short[] set1, final int length1, final short[] set2,
            final int length2, final short[] buffer) {
        int pos = 0;
        int k1 = 0, k2 = 0;
        if (0 == length2) {
            System.arraycopy(set1, 0, buffer, 0, length1);
            return length1;
        }
        if (0 == length1) {
            System.arraycopy(set2, 0, buffer, 0, length2);
            return length2;
        }
        short s1 = set1[k1];
        short s2 = set2[k2];
        while (true) {
            int v1 = toIntUnsigned(s1);
            int v2 = toIntUnsigned(s2);
            if (v1 < v2) {
                buffer[pos++] = s1;
                ++k1;
                if (k1 >= length1) {
                    System.arraycopy(set2, k2, buffer, pos, length2 - k2);
                    return pos + length2 - k2;
                }
                s1 = set1[k1];
            } else if (v1 == v2) {
                buffer[pos++] = s1;
                ++k1;
                ++k2;
                if (k1 >= length1) {
                    System.arraycopy(set2, k2, buffer, pos, length2 - k2);
                    return pos + length2 - k2;
                }
                if (k2 >= length2) {
                    System.arraycopy(set1, k1, buffer, pos, length1 - k1);
                    return pos + length1 - k1;
                }
                s1 = set1[k1];
                s2 = set2[k2];
            } else {// if (set1[k1]>set2[k2])
                buffer[pos++] = s2;
                ++k2;
                if (k2 >= length2) {
                    System.arraycopy(set1, k1, buffer, pos, length1 - k1);
                    return pos + length1 - k1;
                }
                s2 = set2[k2];
            }
        }
        // return pos;
    }

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

Related

  1. unsignedToSigned(int[] ints)
  2. unsignedToSigned(int[] unsignedBytes)
  3. unsignedToSigned(long value, int size)
  4. unsignedToSigned16(char value)
  5. unsignedToSigned8(char value)
  6. unsignedUpcast(short s)
  7. unsignedValue(byte signedByte)
  8. unsignedVLQSize(int value)
  9. unsignedZerofill(Integer number, int offset)