本文整理汇总了Java中com.google.common.math.IntMath类的典型用法代码示例。如果您正苦于以下问题:Java IntMath类的具体用法?Java IntMath怎么用?Java IntMath使用的例子?那么, 这里精选的类代码示例或许可以为您提供帮助。
IntMath类属于com.google.common.math包,在下文中一共展示了IntMath类的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。
示例1: testWorstCase
import com.google.common.math.IntMath; //导入依赖的package包/类
public void testWorstCase() {
int n = 2000000;
int k = 200000;
final long[] compareCalls = {0};
Comparator<Integer> cmp =
new Comparator<Integer>() {
@Override
public int compare(Integer o1, Integer o2) {
compareCalls[0]++;
return o1.compareTo(o2);
}
};
TopKSelector<Integer> top = TopKSelector.least(k, cmp);
top.offer(1);
for (int i = 1; i < n; i++) {
top.offer(0);
}
assertThat(top.topK()).containsExactlyElementsIn(Collections.nCopies(k, 0));
assertThat(compareCalls[0]).isAtMost(10L * n * IntMath.log2(k, RoundingMode.CEILING));
}
示例2: generateCode
import com.google.common.math.IntMath; //导入依赖的package包/类
/**
* <b>NOTE</b>: The generated code is not guaranteed to be unique. You should validate it with other generated
* codes where uniqueness is required before using it.
* @param requiredCodeLength
* @return A random sequence of letters and/or numbers of the specified length
* @throws IllegalArgumentException If the specified length is less than or equal to 0
*/
public String generateCode(int requiredCodeLength) throws IllegalArgumentException {
if(requiredCodeLength <= 0)
throw new IllegalArgumentException("Required code length must be positive");
int randomBitLength = IntMath.checkedPow(requiredCodeLength, 2);
String newReferralCode = new BigInteger(randomBitLength, random).toString(TO_STRING_RADIX);
if(newReferralCode.length() > requiredCodeLength)
return newReferralCode.substring(0, requiredCodeLength);
else if(newReferralCode.length() < requiredCodeLength)
return generateCode(requiredCodeLength);
return newReferralCode;
}
示例3: addToWorldChunks
import com.google.common.math.IntMath; //导入依赖的package包/类
public static void addToWorldChunks(Nation nation)
{
for (Rect r : nation.getRegion().getRects())
{
if (!worldChunks.containsKey(r.getWorld()))
{
worldChunks.put(r.getWorld(), new Hashtable<Vector2i, ArrayList<Nation>>());
}
Hashtable<Vector2i, ArrayList<Nation>> chunks = worldChunks.get(r.getWorld());
for (int i = IntMath.divide(r.getMinX(), 16, RoundingMode.FLOOR); i < IntMath.divide(r.getMaxX(), 16, RoundingMode.FLOOR) + 1; i++)
{
for (int j = IntMath.divide(r.getMinY(), 16, RoundingMode.FLOOR); j < IntMath.divide(r.getMaxY(), 16, RoundingMode.FLOOR) + 1; j++)
{
Vector2i vect = new Vector2i(i, j);
if (!chunks.containsKey(vect))
{
chunks.put(vect, new ArrayList<Nation>());
}
if (!chunks.get(vect).contains(nation))
{
chunks.get(vect).add(nation);
}
}
}
}
}
示例4: createSlidingUnifier
import com.google.common.math.IntMath; //导入依赖的package包/类
public static PTOperator createSlidingUnifier(StreamMeta streamMeta, PhysicalPlan plan, int
operatorApplicationWindowCount, int slidingWindowCount)
{
int gcd = IntMath.gcd(operatorApplicationWindowCount, slidingWindowCount);
OperatorMeta um = streamMeta.getSource()
.getSlidingUnifier(operatorApplicationWindowCount / gcd, gcd, slidingWindowCount / gcd);
PTOperator pu = plan.newOperator(um, um.getName());
Operator unifier = um.getOperator();
PortMappingDescriptor mergeDesc = new PortMappingDescriptor();
Operators.describe(unifier, mergeDesc);
if (mergeDesc.outputPorts.size() != 1) {
throw new AssertionError("Unifier must have a single output port, instead found : " + mergeDesc.outputPorts);
}
pu.unifiedOperatorMeta = streamMeta.getSource().getOperatorMeta();
pu.outputs.add(new PTOutput(mergeDesc.outputPorts.keySet().iterator().next(), streamMeta, pu));
plan.newOpers.put(pu, unifier);
return pu;
}
示例5: OoaBFilter
import com.google.common.math.IntMath; //导入依赖的package包/类
/**
* Constructs a OoaBFilter with an underlying array of the given size, rounded up to the next
* power of two.
*
* This rounding occurs because the hashing is much faster on an array the size of a power of two.
*
* @param size The size of the underlying array.
* @param bufSize The size of the buffers occupying each slot in the array.
*/
public OoaBFilter(int size, int bufSize) {
if (size <= 0) {
throw new IllegalArgumentException("array size must be greater than zero, was " + size);
}
if (size > MAX_SIZE) {
throw new IllegalArgumentException(
"array size may not be larger than 2**31-1, but will be rounded to larger. was " + size);
}
// round to the next largest power of two
int poweredSize = IntMath.pow(2, IntMath.log2(size, RoundingMode.CEILING));
this.sizeMask = poweredSize - 1;
this.array = new ByteBuffer[poweredSize];
// pre-allocate a ByteBuffer for each slot in the array
int i = 0;
while (i < poweredSize) {
array[i] = ByteBuffer.allocate(bufSize);
i++;
}
}
示例6: allGetterFlagsAreAccepted
import com.google.common.math.IntMath; //导入依赖的package包/类
@Test
public void allGetterFlagsAreAccepted() {
final RequestLogAvailability[] values = RequestLogAvailability.values();
final int end = IntMath.pow(2, values.length);
for (int i = 0; i < end; i++) {
int flags = 0;
for (RequestLogAvailability v : values) {
if ((i & 1 << v.ordinal()) != 0) {
flags |= v.getterFlags();
}
}
if (flags != 0) {
assertThat(RequestLogAvailabilitySet.of(flags)).isNotEmpty();
}
}
}
示例7: run
import com.google.common.math.IntMath; //导入依赖的package包/类
@Override
public void run() {
// TODO #93: Disable keep-alives for nodes that have a consensus server, as then the consensus will do the
// keep-alives!
int sleepTime = Math.max(IntMath.gcd(keepAliveMs, connectionIdleTimeMs), 1000);
while (true) {
try {
synchronized (wait) {
wait.wait(sleepTime);
}
} catch (InterruptedException e) {
// interrupted, quielty say goodbye.
return;
}
long curTime = System.nanoTime();
executeTimeouts(curTime);
if (curTime / keepAliveMs != previousKeepAliveDiv) {
previousKeepAliveDiv = curTime / keepAliveMs;
executeKeepAlives();
}
}
}
示例8: backOffWait
import com.google.common.math.IntMath; //导入依赖的package包/类
public void backOffWait(int retryCount) {
// No wait if wait is disabled or this is first re-try
if(retryCount <= 0 || backOff <= 0) {
return;
}
// Wait time period
int waitTime;
// Current exponential back off
try {
waitTime = IntMath.checkedPow(backOff, retryCount);
} catch (ArithmeticException e) {
waitTime = MAX_BACKOFF_WAIT;
}
// Apply upper limit for the wait and finally wait
waitTime = Math.min(waitTime, MAX_BACKOFF_WAIT);
if (!ThreadUtil.sleep(waitTime)) {
LOG.info("Backoff waiting was interrupted");
}
}
示例9: splitToNChunks
import com.google.common.math.IntMath; //导入依赖的package包/类
/**
* If there are fewer files than chunks, fewer than numChunks will be returned.
*/
private static Iterable<List<Map.Entry<Symbol, File>>> splitToNChunks(
final ImmutableMap<Symbol, File> inputMap, int numChunks) {
checkArgument(numChunks > 0);
final List<Map.Entry<Symbol, File>> emptyChunk = ImmutableList.of();
if (inputMap.isEmpty()) {
return Collections.nCopies(numChunks, emptyChunk);
}
final int chunkSize = IntMath.divide(inputMap.size(), numChunks, RoundingMode.UP);
final ImmutableList<List<Map.Entry<Symbol, File>>> chunks =
ImmutableList.copyOf(splitToChunksOfFixedSize(inputMap, chunkSize));
if (chunks.size() == numChunks) {
return chunks;
} else {
// there weren't enough elements to make the desired number of chunks, so we need to
// pad with empty chunks
final int shortage = numChunks - chunks.size();
final List<List<Map.Entry<Symbol, File>>> padding = Collections.nCopies(shortage, emptyChunk);
return Iterables.concat(chunks, padding);
}
}
示例10: calculateSize
import com.google.common.math.IntMath; //导入依赖的package包/类
/**
* The number of permutations with repeated elements is calculated as follows:
*
* <ul>
* <li>For an empty list, it is 1 (base case).
* <li>When r numbers are added to a list of n-r elements, the number of permutations is
* increased by a factor of (n choose r).
* </ul>
*/
private static <E> int calculateSize(
List<E> sortedInputList, Comparator<? super E> comparator) {
int permutations = 1;
int n = 1;
int r = 1;
while (n < sortedInputList.size()) {
int comparison = comparator.compare(sortedInputList.get(n - 1), sortedInputList.get(n));
if (comparison < 0) {
// We move to the next non-repeated element.
permutations = IntMath.saturatedMultiply(permutations, IntMath.binomial(n, r));
r = 0;
if (permutations == Integer.MAX_VALUE) {
return Integer.MAX_VALUE;
}
}
n++;
r++;
}
return IntMath.saturatedMultiply(permutations, IntMath.binomial(n, r));
}
示例11: testCounts
import com.google.common.math.IntMath; //导入依赖的package包/类
@Test
public void testCounts() {
String ascii = "abcdefghijklmnopqrstuvwxyz";
String range = ascii.toLowerCase() + ascii.toUpperCase();
int rangeA = IntMath.pow(range.length(), 1);
assertEquals(rangeA, Iterables.size(new Mutator(range, 1, 1).mutate()));
int rangeB = IntMath.pow(range.length(), 2) + rangeA;
assertEquals(rangeB, Iterables.size(new Mutator(range, 1, 2).mutate()));
int rangeC = IntMath.pow(range.length(), 3) + rangeB;
assertEquals(rangeC, Iterables.size(new Mutator(range, 1, 3).mutate()));
int rangeD = IntMath.pow(range.length(), 4) + rangeC;
assertEquals(rangeD, Iterables.size(new Mutator(range, 1, 4).mutate()));
}
示例12: paintComponent
import com.google.common.math.IntMath; //导入依赖的package包/类
@Override
protected void paintComponent(Graphics g1)
{
super.paintComponent(g1);
Graphics2D g = (Graphics2D)g1;
int cellsX = IntMath.divide(getWidth(), tileModel.getAvgTileWidth(), RoundingMode.UP);
int cellsY = IntMath.divide(getHeight(), tileModel.getTileHeight(), RoundingMode.UP);
tileModel.setMapWidth(cellsX);
tileModel.setMapHeight(cellsY);
// cut of partly draw hexagons
g.clipRect(0, 0, tileModel.getWorldWidth(), tileModel.getWorldHeight());
drawTiles(g);
Object oldAAhint = g.getRenderingHint(RenderingHints.KEY_ANTIALIASING);
g.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
drawVectorField(g);
drawSelection(g);
g.setRenderingHint(RenderingHints.KEY_ANTIALIASING, oldAAhint);
g1.setClip(null);
}
示例13: getTilesInRect
import com.google.common.math.IntMath; //导入依赖的package包/类
@Override
public List<Tile> getTilesInRect(int worldX0, int worldY0, int worldX1, int worldY1)
{
int x0 = IntMath.divide(worldX0, tileWidth, RoundingMode.FLOOR);
int y0 = IntMath.divide(worldY0, tileHeight, RoundingMode.FLOOR);
int x1 = IntMath.divide(worldX1, tileWidth, RoundingMode.FLOOR);
int y1 = IntMath.divide(worldY1, tileHeight, RoundingMode.FLOOR);
// Restrict to map bounds
int minX = Math.max(x0, 0);
int maxX = Math.min(x1, mapWidth - 1);
int minY = Math.max(y0, 0);
int maxY = Math.min(y1, mapHeight - 1);
List<Tile> result = new ArrayList<Tile>();
for (int y = minY; y <= maxY; y++)
{
for (int x = minX; y <= maxX; x++)
{
result.add(getTile(x, y));
}
}
return result;
}
示例14: getInteger
import com.google.common.math.IntMath; //导入依赖的package包/类
protected static int getInteger(byte[] b) {
int sum = 0;
for (int i = 0; i < b.length; i++) {
final int reverseIndex = b.length - 1 - i;
final int factor = IntMath.pow(256, reverseIndex);
final int base = UnsignedBytes.toInt(b[i]);
sum += base * factor;
}
return sum;
}
示例15: CartesianList
import com.google.common.math.IntMath; //导入依赖的package包/类
CartesianList(ImmutableList<List<E>> axes) {
this.axes = axes;
int[] axesSizeProduct = new int[axes.size() + 1];
axesSizeProduct[axes.size()] = 1;
try {
for (int i = axes.size() - 1; i >= 0; i--) {
axesSizeProduct[i] = IntMath.checkedMultiply(axesSizeProduct[i + 1], axes.get(i).size());
}
} catch (ArithmeticException e) {
throw new IllegalArgumentException(
"Cartesian product too large; must have size at most Integer.MAX_VALUE");
}
this.axesSizeProduct = axesSizeProduct;
}