Returns the result of adding this and {@code val}. If the result would have more than 32 bits,
returns the low 32 bits of the result.

@since 14.0
176517
@CheckReturnValue
  public UnsignedInteger plus(UnsignedInteger val) {
    return fromIntBits(this.value + checkNotNull(val).value);
  }
213728
public <V extends Val> V returning(V val) {
    if (val instanceof ValFrame)
      _ses.addRefCnt(val.getFrame(), 1);
    return val;
  }
1280957
public Rational add(BigInteger val) {
        Rational val2 = new Rational(val, BigInteger.ONE);
        return (add(val2));
    }
883035
public static byte[] intToBytesNoLeadZeroes(int val) {

        if (val == 0)
            return EMPTY_BYTE_ARRAY;

        int lenght = 0;

        int tmpVal = val;
        while (tmpVal != 0) {
            tmpVal = tmpVal >>> 8;
            ++lenght;
        }

        byte[] result = new byte[lenght];

        int index = result.length - 1;
        while (val != 0) {

            result[index] = (byte) (val & 0xFF);
            val = val >>> 8;
            index -= 1;
        }

        return result;
    }
975295
public int add(long v, long[] out) {
        //System.out.println("S64.add v=" + v + " " + (1 + inputCount - inputCompressable) + " waiting");
        pending[inputCount++] = v;
        while (inputCompressable <= inputCount) {
            final long nextData = pending[(inputCompressable - 1)];
            //System.out.println("  cycle: data=" + nextData);
            while ((nextData >= maxFitPlus1) && (minBits < NUM_DATA_BITS)) {
                //System.out.println("  cycle maxFitPlus1=" + maxFitPlus1 + " minBits=" + minBits);
                if ((minBits == 7) && (inputCompressable == 8) && (nextData < (maxFitPlus1 << 4))) {
                    break;
                } else if ((minBits == 8) && (inputCompressable == 7) && (nextData < (maxFitPlus1 << 4))) {
                    break;
                } else {
                    //System.out.println("  advance");
                    minBits++;
                    maxFitPlus1 <<= 1;
                    if ((inputCompressable * minBits) > NUM_DATA_BITS) {
                        inputCompressable--;
                        //System.out.println("  hard break");
                        break;
                    }
                }
            }
            inputCompressable++;

            //System.out.println("  minBits=" + minBits + " count=" + (inputCompressable-1) + " inputCount=" + inputCount);

            if ((inputCompressable * minBits) > NUM_DATA_BITS) {
                // Time to compress!
                inputCompressable--;
                //System.out.println("  FLUSH count=" + inputCompressable);

                // nocommit -- it should always be > 0... right??
                assert inputCompressable > 0;

                // Check whether a bigger number of bits can be used:
                while ((inputCompressable * (minBits + 1)) <= NUM_DATA_BITS) {
                    minBits++;
                    //System.out.println("  incr minBits=" + minBits);
                }

        /*
          if (((inputCompressable+1) * minBits) <= NUM_DATA_BITS) {
          // not enough input available for minBits
          minBits++;
          // do not compress all available input
          inputCompressable = NUM_DATA_BITS / minBits;
          }
        */

                // Put compression method in status bits and encode input data
                long s9;
                final int consumed;
                switch (minBits) { // add status bits and later input values
                    case 60:
                        s9 = STATUS_1NUM_60BITS;
                        s9 |= pending[0] << 4;
                        consumed = 1;
                        break;
                    case 30:
                        s9 = STATUS_2NUM_30BITS;
                        // nocommit -- make a single expr instead of |'ing ?
                        s9 |= pending[0] << 4;
                        s9 |= pending[1] << 34;
                        consumed = 2;
                        break;
                    case 20:
                        s9 = STATUS_3NUM_20BITS;
                        s9 |= pending[0] << 4;
                        s9 |= pending[1] << 24;
                        s9 |= pending[2] << 44;
                        consumed = 3;
                        break;
                    case 15:
                        s9 = STATUS_4NUM_15BITS;
                        s9 |= pending[0] << 4;
                        s9 |= pending[1] << 19;
                        s9 |= pending[2] << 34;
                        s9 |= pending[3] << 49;
                        consumed = 4;
                        break;
                    case 12:
                        s9 = STATUS_5NUM_12BITS;
                        s9 |= pending[0] << 4;
                        s9 |= pending[1] << 16;
                        s9 |= pending[2] << 28;
                        s9 |= pending[3] << 40;
                        s9 |= pending[4] << 52;
                        consumed = 5;
                        break;
                    case 10:
                        s9 = STATUS_6NUM_10BITS;
                        s9 |= pending[0] << 4;
                        s9 |= pending[1] << 14;
                        s9 |= pending[2] << 24;
                        s9 |= pending[3] << 34;
                        s9 |= pending[4] << 44;
                        s9 |= pending[5] << 54;
                        consumed = 6;
                        break;
                    case 8:
                        s9 = STATUS_7NUM_8BITS;
                        s9 |= pending[0] << 4;
                        s9 |= pending[1] << 12;
                        s9 |= pending[2] << 20;
                        s9 |= pending[3] << 28;
                        s9 |= pending[4] << 36;
                        s9 |= pending[5] << 44;
                        s9 |= pending[6] << 52; // 4 more bits
                        consumed = 7;
                        break;
                    case 7:
                        s9 = STATUS_8NUM_7BITS;
                        s9 |= pending[0] << 4;
                        s9 |= pending[1] << 11;
                        s9 |= pending[2] << 18;
                        s9 |= pending[3] << 25;
                        s9 |= pending[4] << 32;
                        s9 |= pending[5] << 39;
                        s9 |= pending[6] << 46;
                        s9 |= pending[7] << 53; // 4 more bits
                        consumed = 8;
                        break;
                    case 6:
                        s9 = STATUS_10NUM_6BITS;
                        s9 |= pending[0] << 4;
                        s9 |= pending[1] << 10;
                        s9 |= pending[2] << 16;
                        s9 |= pending[3] << 22;
                        s9 |= pending[4] << 28;
                        s9 |= pending[5] << 34;
                        s9 |= pending[6] << 40;
                        s9 |= pending[7] << 46;
                        s9 |= pending[8] << 52;
                        s9 |= pending[9] << 58;
                        consumed = 10;
                        break;
                    case 5:
                        s9 = STATUS_12NUM_5BITS;
                        s9 |= pending[0] << 4;
                        s9 |= pending[1] << 9;
                        s9 |= pending[2] << 14;
                        s9 |= pending[3] << 19;
                        s9 |= pending[4] << 24;
                        s9 |= pending[5] << 29;
                        s9 |= pending[6] << 34;
                        s9 |= pending[7] << 39;
                        s9 |= pending[8] << 44;
                        s9 |= pending[9] << 49;
                        s9 |= pending[10] << 54;
                        s9 |= pending[11] << 59;
                        consumed = 12;
                        break;
                    case 4:
                        s9 = STATUS_15NUM_4BITS;
                        s9 |= pending[0] << 4;
                        s9 |= pending[1] << 8;
                        s9 |= pending[2] << 12;
                        s9 |= pending[3] << 16;
                        s9 |= pending[4] << 20;
                        s9 |= pending[5] << 24;
                        s9 |= pending[6] << 28;
                        s9 |= pending[7] << 32;
                        s9 |= pending[8] << 36;
                        s9 |= pending[9] << 40;
                        s9 |= pending[10] << 44;
                        s9 |= pending[11] << 48;
                        s9 |= pending[12] << 52;
                        s9 |= pending[13] << 56;
                        s9 |= pending[14] << 60;
                        consumed = 15;
                        break;
                    case 3:
                        s9 = STATUS_20NUM_3BITS;
                        s9 |= pending[0] << 4;
                        s9 |= pending[1] << 7;
                        s9 |= pending[2] << 10;
                        s9 |= pending[3] << 13;
                        s9 |= pending[4] << 16;
                        s9 |= pending[5] << 19;
                        s9 |= pending[6] << 22;
                        s9 |= pending[7] << 25;
                        s9 |= pending[8] << 28;
                        s9 |= pending[9] << 31;
                        s9 |= pending[10] << 34;
                        s9 |= pending[11] << 37;
                        s9 |= pending[12] << 40;
                        s9 |= pending[13] << 43;
                        s9 |= pending[14] << 46;
                        s9 |= pending[15] << 49;
                        s9 |= pending[16] << 52;
                        s9 |= pending[17] << 55;
                        s9 |= pending[18] << 58;
                        s9 |= pending[19] << 61;
                        consumed = 20;
                        break;
                    case 2:
                        s9 = STATUS_30NUM_2BITS;
                        s9 |= pending[0] << 4;
                        s9 |= pending[1] << 6;
                        s9 |= pending[2] << 8;
                        s9 |= pending[3] << 10;
                        s9 |= pending[4] << 12;
                        s9 |= pending[5] << 14;
                        s9 |= pending[6] << 16;
                        s9 |= pending[7] << 18;
                        s9 |= pending[8] << 20;
                        s9 |= pending[9] << 22;
                        s9 |= pending[10] << 24;
                        s9 |= pending[11] << 26;
                        s9 |= pending[12] << 28;
                        s9 |= pending[13] << 30;
                        s9 |= pending[14] << 32;
                        s9 |= pending[15] << 34;
                        s9 |= pending[16] << 36;
                        s9 |= pending[17] << 38;
                        s9 |= pending[18] << 40;
                        s9 |= pending[19] << 42;
                        s9 |= pending[20] << 44;
                        s9 |= pending[21] << 46;
                        s9 |= pending[22] << 48;
                        s9 |= pending[23] << 50;
                        s9 |= pending[24] << 52;
                        s9 |= pending[25] << 54;
                        s9 |= pending[26] << 56;
                        s9 |= pending[27] << 58;
                        s9 |= pending[28] << 60;
                        s9 |= pending[29] << 62;
                        consumed = 30;
                        break;
                    case 1:
                        s9 = STATUS_60NUM_1BITS;
                        s9 |= pending[0] << 4;
                        s9 |= pending[1] << 5;
                        s9 |= pending[2] << 6;
                        s9 |= pending[3] << 7;
                        s9 |= pending[4] << 8;
                        s9 |= pending[5] << 9;
                        s9 |= pending[6] << 10;
                        s9 |= pending[7] << 11;
                        s9 |= pending[8] << 12;
                        s9 |= pending[9] << 13;
                        s9 |= pending[10] << 14;
                        s9 |= pending[11] << 15;
                        s9 |= pending[12] << 16;
                        s9 |= pending[13] << 17;
                        s9 |= pending[14] << 18;
                        s9 |= pending[15] << 19;
                        s9 |= pending[16] << 20;
                        s9 |= pending[17] << 21;
                        s9 |= pending[18] << 22;
                        s9 |= pending[19] << 23;
                        s9 |= pending[20] << 24;
                        s9 |= pending[21] << 25;
                        s9 |= pending[22] << 26;
                        s9 |= pending[23] << 27;
                        s9 |= pending[24] << 28;
                        s9 |= pending[25] << 29;
                        s9 |= pending[26] << 30;
                        s9 |= pending[27] << 31;
                        s9 |= pending[28] << 32;
                        s9 |= pending[29] << 33;
                        s9 |= pending[30] << 34;
                        s9 |= pending[31] << 35;
                        s9 |= pending[32] << 36;
                        s9 |= pending[33] << 37;
                        s9 |= pending[34] << 38;
                        s9 |= pending[35] << 39;
                        s9 |= pending[36] << 40;
                        s9 |= pending[37] << 41;
                        s9 |= pending[38] << 42;
                        s9 |= pending[39] << 43;
                        s9 |= pending[40] << 44;
                        s9 |= pending[41] << 45;
                        s9 |= pending[42] << 46;
                        s9 |= pending[43] << 47;
                        s9 |= pending[44] << 48;
                        s9 |= pending[45] << 49;
                        s9 |= pending[46] << 50;
                        s9 |= pending[47] << 51;
                        s9 |= pending[48] << 52;
                        s9 |= pending[49] << 53;
                        s9 |= pending[50] << 54;
                        s9 |= pending[51] << 55;
                        s9 |= pending[52] << 56;
                        s9 |= pending[53] << 57;
                        s9 |= pending[54] << 58;
                        s9 |= pending[55] << 59;
                        s9 |= pending[56] << 60;
                        s9 |= pending[57] << 61;
                        s9 |= pending[58] << 62;
                        s9 |= pending[59] << 63;
                        consumed = 60;
                        break;
                    default:
                        assert false;
                        s9 = 0;
                        consumed = 60;
                        //throw new Error("S98b.compressSingle internal error: unknown minBits: " + minBits);
                }

                final int leftover = inputCount - consumed;
                assert leftover >= 0 : "consumed=" + consumed + " vs " + inputCompressable;

        /*
        for(int x=0;x<consumed;x++) {
          System.out.println(""+pending[x]);
        }
        */
        /*
        // like cd burning!  ;)
        {
        int[] test = new int[consumed];
        int ct = decompressSingle(s9, test, 0);
        assert ct == consumed;
        for(int x=0;x<ct;x++) {
        assert test[x] == pending[x];
        }
        }
        */

                //System.out.println("  return consumed=" + consumed);
                reset();

                // save leftovers:
                System.arraycopy(pending, consumed, pending, 0, leftover);
                inputCount = leftover;

                out[0] = s9;
                return consumed;
            }
        }

        return 0;
    }
809179
public static int lenVarInt(long longVal) {
        if (longVal == 0) {
            return 0;
        }
        if (longVal <  0) longVal = -longVal;
        if (longVal < (1L << (7 * 1 - 1))) return 1;  // 6  bits
        if (longVal < (1L << (7 * 2 - 1))) return 2;  // 13 bits
        if (longVal < (1L << (7 * 3 - 1))) return 3;  // 20 bits
        if (longVal < (1L << (7 * 4 - 1))) return 4;  // 27 bits
        if (longVal < (1L << (7 * 5 - 1))) return 5;  // 34 bits
        if (longVal < (1L << (7 * 6 - 1))) return 6;  // 41 bits
        if (longVal < (1L << (7 * 7 - 1))) return 7;  // 48 bits
        if (longVal < (1L << (7 * 8 - 1))) return 8;  // 55 bits
        if (longVal < (1L << (7 * 9 - 1))) return 9;  // 62 bits
        return 10;
    }
145743
public boolean greaterThan(long val) {
        if (isInfinity && posOrNeg) {
            return true;
        } else if (isInfinity && !posOrNeg) {
            return false;
        } else {
            return this.val > val;
        }
    }
1024772
public int readVarInt() throws IOException {
    boolean more;
    int result = 0;
    do {
      int b = in.readUnsignedByte();
      more = (b & 0x80) == 0x80;
      result = 128 * result + (b & 0x7F);

      // TODO Check for int overflow
    } while (more);

    return result;
  }
601543
private int getByteLength(final Object val) {
		if (val == null) {
			return 4;
		}
		String str = val.toString();
		try {
			int len = str.getBytes(ENCODING_SHIFT_JIS).length;
			return len <= 200 ? len : 200;
		} catch (UnsupportedEncodingException ex) {
			return 1;
		}
	}
117536
public int count(double val) {
        int count = 0;
        for (int i=0; i<this.size(); i++) {
            if (this.getValue(i) == val) {
                count++;
            }
        }
        return count;
    }
797172
@Override
    public final byte[] toBytes(Object val)
    {
        if (val != null)
        {
            Integer value = (Integer) (val);
            return new byte[] { (byte) (value >>> 24), (byte) (value >>> 16), (byte) (value >>> 8),
                    (byte) value.intValue() };
        }
        return null;
    }
212967
public byte minValue() {
        if (data.length == 0)
            return 0;

        byte min = Byte.MAX_VALUE;
        for (byte b : data)
            if (b < min)
                min = b;
        return min;
    }
678900
public static byte[] toBytes(BigDecimal val) {
    byte[] valueBytes = val.unscaledValue().toByteArray();
    byte[] result = new byte[valueBytes.length + SIZEOF_INT];
    int offset = putInt(result, 0, val.scale());
    putBytes(result, offset, valueBytes, 0, valueBytes.length);
    return result;
  }
599449
public static int mod(int val, int mod) {
        val = val % mod;
        if (val < 0) {
            val += mod;
        }
        return val;
    }
1225134
static public byte[] asBytes(int value) {

        if (0 > value) {
            throw new IllegalArgumentException("value cannot be less than zero");
        }

        byte[] result = new byte[4];
        result[0] = (byte) ((value >>> 24) & 0xFF);
        result[1] = (byte) ((value >>> 16) & 0xFF);
        result[2] = (byte) ((value >>> 8) & 0xFF);
        result[3] = (byte) ((value) & 0xFF);
        return result;
    }
1092377
public void addValue(double val) {
        numItems++;
        double valMinusMean = val - mean;
        sumsq += valMinusMean * valMinusMean * (numItems - 1) / numItems;
        mean += valMinusMean / numItems;
    }
922216
public void putDouble(Double val) throws IOException {
    long v = java.lang.Double.doubleToLongBits(val.getValue());
    if (byteOrder == ByteOrder.BIG_ENDIAN) {
      writeIntCurrentPosition((int) (v >>> 56) & 0xFF);
      writeIntCurrentPosition((int) (v >>> 48) & 0xFF);
      writeIntCurrentPosition((int) (v >>> 40) & 0xFF);
      writeIntCurrentPosition((int) (v >>> 32) & 0xFF);
      writeIntCurrentPosition((int) (v >>> 24) & 0xFF);
      writeIntCurrentPosition((int) (v >>> 16) & 0xFF);
      writeIntCurrentPosition((int) (v >>> 8) & 0xFF);
      writeIntCurrentPosition((int) (v >>> 0) & 0xFF);
    } else {
      writeIntCurrentPosition((int) (v >>> 0) & 0xFF);
      writeIntCurrentPosition((int) (v >>> 8) & 0xFF);
      writeIntCurrentPosition((int) (v >>> 16) & 0xFF);
      writeIntCurrentPosition((int) (v >>> 24) & 0xFF);
      writeIntCurrentPosition((int) (v >>> 32) & 0xFF);
      writeIntCurrentPosition((int) (v >>> 40) & 0xFF);
      writeIntCurrentPosition((int) (v >>> 48) & 0xFF);
      writeIntCurrentPosition((int) (v >>> 56) & 0xFF);
    }
  }