首页

关于flink-core源码包中MathUtils数据计算工具类大字段属性问题追加等操作

标签:MathUtils,数字公式,计算工具类     发布时间:2018-03-28   

一、前言

关于flink-core源码包中的org.apache.flink.util.MathUtils数组计算转换处理工具类,对常见的数据长度转换等,具体参考下发源码说明。

二、源码说明

package org.apache.flink.util;@b@@b@/**@b@ * Collection of simple mathematical routines.@b@ */@b@public final class MathUtils {@b@	@b@	/**@b@	 * Computes the logarithm of the given value to the base of 2, rounded down. It corresponds to the@b@	 * position of the highest non-zero bit. The position is counted, starting with 0 from the least@b@	 * significant bit to the most significant bit. For example, <code>log2floor(16) = 4</code>, and@b@	 * <code>log2floor(10) = 3</code>.@b@	 * @b@	 * @param value The value to compute the logarithm for.@b@	 * @return The logarithm (rounded down) to the base of 2.@b@	 * @throws ArithmeticException Thrown, if the given value is zero.@b@	 */@b@	public static int log2floor(int value) throws ArithmeticException {@b@		if (value == 0) {@b@			throw new ArithmeticException("Logarithm of zero is undefined.");@b@		}@b@@b@		return 31 - Integer.numberOfLeadingZeros(value);@b@	}@b@	@b@	/**@b@	 * Computes the logarithm of the given value to the base of 2. This method throws an error,@b@	 * if the given argument is not a power of 2.@b@	 * @b@	 * @param value The value to compute the logarithm for.@b@	 * @return The logarithm to the base of 2.@b@	 * @throws ArithmeticException Thrown, if the given value is zero.@b@	 * @throws IllegalArgumentException Thrown, if the given value is not a power of two.@b@	 */@b@	public static int log2strict(int value) throws ArithmeticException, IllegalArgumentException {@b@		if (value == 0) {@b@			throw new ArithmeticException("Logarithm of zero is undefined.");@b@		}@b@		if ((value & (value - 1)) != 0) {@b@			throw new IllegalArgumentException("The given value " + value + " is not a power of two.");@b@		}@b@		return 31 - Integer.numberOfLeadingZeros(value);@b@	}@b@	@b@	/**@b@	 * Decrements the given number down to the closest power of two. If the argument is a@b@	 * power of two, it remains unchanged.@b@	 * @b@	 * @param value The value to round down.@b@	 * @return The closest value that is a power of two and less or equal than the given value.@b@	 */@b@	public static int roundDownToPowerOf2(int value) {@b@		return Integer.highestOneBit(value);@b@	}@b@	@b@	/**@b@	 * Casts the given value to a 32 bit integer, if it can be safely done. If the cast would change the numeric@b@	 * value, this method raises an exception.@b@	 * <p>@b@	 * This method is a protection in places where one expects to be able to safely case, but where unexpected@b@	 * situations could make the cast unsafe and would cause hidden problems that are hard to track down.@b@	 * @b@	 * @param value The value to be cast to an integer.@b@	 * @return The given value as an integer.@b@	 * @see Math#toIntExact(long)@b@	 */@b@	public static int checkedDownCast(long value) {@b@		int downCast = (int) value;@b@		if (downCast != value) {@b@			throw new IllegalArgumentException("Cannot downcast long value " + value + " to integer.");@b@		}@b@		return downCast;@b@	}@b@@b@	/**@b@	 * Checks whether the given value is a power of two.@b@	 *@b@	 * @param value The value to check.@b@	 * @return True, if the value is a power of two, false otherwise.@b@	 */@b@	public static boolean isPowerOf2(long value) {@b@		return (value & (value - 1)) == 0;@b@	}@b@@b@	/**@b@	 * This function hashes an integer value. It is adapted from Bob Jenkins' website@b@	 * <a href="http://www.burtleburtle.net/bob/hash/integer.html">http://www.burtleburtle.net/bob/hash/integer.html</a>.@b@	 * The hash function has the <i>full avalanche</i> property, meaning that every bit of the value to be hashed@b@	 * affects every bit of the hash value.@b@	 *@b@	 * It is crucial to use different hash functions to partition data across machines and the internal partitioning of@b@	 * data structures. This hash function is intended for partitioning internally in data structures.@b@	 *@b@	 * @param code The integer to be hashed.@b@	 * @return The non-negative hash code for the integer.@b@	 */@b@	public static int jenkinsHash(int code) {@b@		code = (code + 0x7ed55d16) + (code << 12);@b@		code = (code ^ 0xc761c23c) ^ (code >>> 19);@b@		code = (code + 0x165667b1) + (code << 5);@b@		code = (code + 0xd3a2646c) ^ (code << 9);@b@		code = (code + 0xfd7046c5) + (code << 3);@b@		code = (code ^ 0xb55a4f09) ^ (code >>> 16);@b@		return code >= 0 ? code : -(code + 1);@b@	}@b@@b@	/**@b@	 * This function hashes an integer value.@b@	 *@b@	 * It is crucial to use different hash functions to partition data across machines and the internal partitioning of@b@	 * data structures. This hash function is intended for partitioning across machines.@b@	 *@b@	 * @param code The integer to be hashed.@b@	 * @return The non-negative hash code for the integer.@b@	 */@b@	public static int murmurHash(int code) {@b@		code *= 0xcc9e2d51;@b@		code = Integer.rotateLeft(code, 15);@b@		code *= 0x1b873593;@b@@b@		code = Integer.rotateLeft(code, 13);@b@		code = code * 5 + 0xe6546b64;@b@@b@		code ^= 4;@b@		code = bitMix(code);@b@@b@		if (code >= 0) {@b@			return code;@b@		}@b@		else if (code != Integer.MIN_VALUE) {@b@			return -code;@b@		}@b@		else {@b@			return 0;@b@		}@b@	}@b@@b@	/**@b@	 * Round the given number to the next power of two@b@	 * @param x number to round@b@	 * @return x rounded up to the next power of two@b@	 */@b@	public static int roundUpToPowerOfTwo(int x) {@b@		x = x - 1;@b@		x |= x >> 1;@b@		x |= x >> 2;@b@		x |= x >> 4;@b@		x |= x >> 8;@b@		x |= x >> 16;@b@		return x + 1;@b@	}@b@@b@	/**@b@	 * Pseudo-randomly maps a long (64-bit) to an integer (32-bit) using some bit-mixing for better distribution.@b@	 *@b@	 * @param in the long (64-bit)input.@b@	 * @return the bit-mixed int (32-bit) output@b@	 */@b@	public static int longToIntWithBitMixing(long in) {@b@		in = (in ^ (in >>> 30)) * 0xbf58476d1ce4e5b9L;@b@		in = (in ^ (in >>> 27)) * 0x94d049bb133111ebL;@b@		in = in ^ (in >>> 31);@b@		return (int) in;@b@	}@b@@b@	/**@b@	 * Bit-mixing for pseudo-randomization of integers (e.g., to guard against bad hash functions). Implementation is@b@	 * from Murmur's 32 bit finalizer.@b@	 *@b@	 * @param in the input value@b@	 * @return the bit-mixed output value@b@	 */@b@	public static int bitMix(int in) {@b@		in ^= in >>> 16;@b@		in *= 0x85ebca6b;@b@		in ^= in >>> 13;@b@		in *= 0xc2b2ae35;@b@		in ^= in >>> 16;@b@		return in;@b@	}@b@@b@	// ============================================================================================@b@	@b@	/**@b@	 * Prevent Instantiation through private constructor.@b@	 */@b@	private MathUtils() {}@b@}