Java Code Examples for com.google.common.math.IntMath#gcd()

The following examples show how to use com.google.common.math.IntMath#gcd() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: StreamMapping.java    From Bats with Apache License 2.0 6 votes vote down vote up
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;
}
 
Example 2
Source File: StreamMapping.java    From attic-apex-core with Apache License 2.0 6 votes vote down vote up
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;
}
 
Example 3
Source File: MathUtils.java    From turbo-rpc with Apache License 2.0 5 votes vote down vote up
/**
 * 最大公约数
 * 
 * @param a
 * @param b
 * @return
 */
public static int gcd(int a, int b) {
	if (a == b) {
		return a;
	}

	return IntMath.gcd(a, b);
}
 
Example 4
Source File: RatLitExpr.java    From theta with Apache License 2.0 5 votes vote down vote up
private RatLitExpr(final int num, final int denom) {
	checkArgument(denom != 0);

	final int gcd = IntMath.gcd(Math.abs(num), Math.abs(denom));
	if (denom >= 0) {
		this.num = num / gcd;
		this.denom = denom / gcd;
	} else {
		this.num = -num / gcd;
		this.denom = -denom / gcd;
	}
}
 
Example 5
Source File: Node.java    From Bats with Apache License 2.0 4 votes vote down vote up
@SuppressWarnings("unchecked")
public void activate()
{
  alive = true;
  APPLICATION_WINDOW_COUNT = context.getValue(OperatorContext.APPLICATION_WINDOW_COUNT);
  if (context.getValue(OperatorContext.SLIDE_BY_WINDOW_COUNT) != null) {
    int slidingWindowCount = context.getValue(OperatorContext.SLIDE_BY_WINDOW_COUNT);
    APPLICATION_WINDOW_COUNT = IntMath.gcd(APPLICATION_WINDOW_COUNT, slidingWindowCount);
  }
  DAG_CHECKPOINT_WINDOW_COUNT = context.getValue(Context.DAGContext.CHECKPOINT_WINDOW_COUNT);
  CHECKPOINT_WINDOW_COUNT = context.getValue(OperatorContext.CHECKPOINT_WINDOW_COUNT);
  Collection<StatsListener> statsListeners = context.getValue(OperatorContext.STATS_LISTENERS);

  if (CHECKPOINT_WINDOW_COUNT % APPLICATION_WINDOW_COUNT != 0) {
    logger.warn("{} is not exact multiple of {} for operator {}. This may cause side effects such as processing to begin without beginWindow preceding it in the first window after activation.",
        OperatorContext.CHECKPOINT_WINDOW_COUNT,
        OperatorContext.APPLICATION_WINDOW_COUNT,
        operator);
  }

  PROCESSING_MODE = context.getValue(OperatorContext.PROCESSING_MODE);
  if (PROCESSING_MODE == ProcessingMode.EXACTLY_ONCE && CHECKPOINT_WINDOW_COUNT != 1) {
    logger.warn("Ignoring {} attribute in favor of {} processing mode", OperatorContext.CHECKPOINT_WINDOW_COUNT.getSimpleName(), ProcessingMode.EXACTLY_ONCE.name());
    CHECKPOINT_WINDOW_COUNT = 1;
  }

  activateSinks();
  if (operator instanceof Operator.ActivationListener) {
    ((Operator.ActivationListener<OperatorContext>)operator).activate(context);
  }

  if (statsListeners != null) {
    Iterator<StatsListener> iterator = statsListeners.iterator();
    while (iterator.hasNext()) {
      DATA_TUPLE_AWARE = iterator.next().getClass().isAnnotationPresent(StatsListener.DataQueueSize.class);
      if (DATA_TUPLE_AWARE) {
        break;
      }
    }
  }

  if (!DATA_TUPLE_AWARE && (operator instanceof StatsListener)) {
    DATA_TUPLE_AWARE = operator.getClass().isAnnotationPresent(StatsListener.DataQueueSize.class);
  }
  /*
   * If there were any requests which needed to be executed before the operator started
   * its normal execution, execute those requests now - e.g. Restarting the operator
   * recording for the operators which failed while recording and being replaced.
   */
  handleRequests(currentWindowId);
}
 
Example 6
Source File: Node.java    From attic-apex-core with Apache License 2.0 4 votes vote down vote up
@SuppressWarnings("unchecked")
public void activate()
{
  alive = true;
  APPLICATION_WINDOW_COUNT = context.getValue(OperatorContext.APPLICATION_WINDOW_COUNT);
  if (context.getValue(OperatorContext.SLIDE_BY_WINDOW_COUNT) != null) {
    int slidingWindowCount = context.getValue(OperatorContext.SLIDE_BY_WINDOW_COUNT);
    APPLICATION_WINDOW_COUNT = IntMath.gcd(APPLICATION_WINDOW_COUNT, slidingWindowCount);
  }
  DAG_CHECKPOINT_WINDOW_COUNT = context.getValue(Context.DAGContext.CHECKPOINT_WINDOW_COUNT);
  CHECKPOINT_WINDOW_COUNT = context.getValue(OperatorContext.CHECKPOINT_WINDOW_COUNT);
  Collection<StatsListener> statsListeners = context.getValue(OperatorContext.STATS_LISTENERS);

  if (CHECKPOINT_WINDOW_COUNT % APPLICATION_WINDOW_COUNT != 0) {
    logger.warn("{} is not exact multiple of {} for operator {}. This may cause side effects such as processing to begin without beginWindow preceding it in the first window after activation.",
        OperatorContext.CHECKPOINT_WINDOW_COUNT,
        OperatorContext.APPLICATION_WINDOW_COUNT,
        operator);
  }

  PROCESSING_MODE = context.getValue(OperatorContext.PROCESSING_MODE);
  if (PROCESSING_MODE == ProcessingMode.EXACTLY_ONCE && CHECKPOINT_WINDOW_COUNT != 1) {
    logger.warn("Ignoring {} attribute in favor of {} processing mode", OperatorContext.CHECKPOINT_WINDOW_COUNT.getSimpleName(), ProcessingMode.EXACTLY_ONCE.name());
    CHECKPOINT_WINDOW_COUNT = 1;
  }

  activateSinks();
  if (operator instanceof Operator.ActivationListener) {
    ((Operator.ActivationListener<OperatorContext>)operator).activate(context);
  }

  if (statsListeners != null) {
    Iterator<StatsListener> iterator = statsListeners.iterator();
    while (iterator.hasNext()) {
      DATA_TUPLE_AWARE = iterator.next().getClass().isAnnotationPresent(StatsListener.DataQueueSize.class);
      if (DATA_TUPLE_AWARE) {
        break;
      }
    }
  }

  if (!DATA_TUPLE_AWARE && (operator instanceof StatsListener)) {
    DATA_TUPLE_AWARE = operator.getClass().isAnnotationPresent(StatsListener.DataQueueSize.class);
  }
  /*
   * If there were any requests which needed to be executed before the operator started
   * its normal execution, execute those requests now - e.g. Restarting the operator
   * recording for the operators which failed while recording and being replaced.
   */
  handleRequests(currentWindowId);
}
 
Example 7
Source File: GuavaMathUnitTest.java    From tutorials with MIT License 4 votes vote down vote up
@Test
public void should_calculate_gcd() {
    int result = IntMath.gcd(15, 20);
    assertThat(result, equalTo(5));
}
 
Example 8
Source File: GuavaIntMathUnitTest.java    From tutorials with MIT License 4 votes vote down vote up
@Test
public void whenGcdOfTwoIntegers_shouldReturnValue() {
    int result = IntMath.gcd(30, 40);
    assertEquals(10, result);
}
 
Example 9
Source File: MathUtil.java    From j360-dubbo-app-all with Apache License 2.0 2 votes vote down vote up
/**
 * 两个数的最大公约数,必须均为非负数.
 * 
 * 是公约数,别想太多
 */
public static int gcd(int a, int b) {
	return IntMath.gcd(a, b);
}