java.util.TreeMap#pollLastEntry ( )源码实例Demo

下面列出了java.util.TreeMap#pollLastEntry ( ) 实例代码,或者点击链接到github查看源代码,也可以在右侧发表评论。

源代码1 项目: openjdk-jdk9   文件: TreeMapTest.java
/**
 * pollLastEntry returns entries in order
 */
public void testPollLastEntry() {
    TreeMap map = map5();
    Map.Entry e = map.pollLastEntry();
    assertEquals(five, e.getKey());
    assertEquals("E", e.getValue());
    e = map.pollLastEntry();
    assertEquals(four, e.getKey());
    map.put(five, "E");
    e = map.pollLastEntry();
    assertEquals(five, e.getKey());
    assertEquals("E", e.getValue());
    e = map.pollLastEntry();
    assertEquals(three, e.getKey());
    map.remove(two);
    e = map.pollLastEntry();
    assertEquals(one, e.getKey());
    try {
        e.setValue("E");
        shouldThrow();
    } catch (UnsupportedOperationException success) {}
    e = map.pollLastEntry();
    assertNull(e);
}
 
源代码2 项目: j2objc   文件: TreeMapTest.java
/**
 * pollLastEntry returns entries in order
 */
public void testPollLastEntry() {
    TreeMap map = map5();
    Map.Entry e = map.pollLastEntry();
    assertEquals(five, e.getKey());
    assertEquals("E", e.getValue());
    e = map.pollLastEntry();
    assertEquals(four, e.getKey());
    map.put(five, "E");
    e = map.pollLastEntry();
    assertEquals(five, e.getKey());
    assertEquals("E", e.getValue());
    e = map.pollLastEntry();
    assertEquals(three, e.getKey());
    map.remove(two);
    e = map.pollLastEntry();
    assertEquals(one, e.getKey());
    try {
        e.setValue("E");
        shouldThrow();
    } catch (UnsupportedOperationException success) {}
    e = map.pollLastEntry();
    assertNull(e);
}
 
/**
 * Find small factors of some N. Returns found factors in <code>primeFactors</code> and eventually some
 * unfactored composites as return value.
 * 
 * @param N the number to factor
 * @param primeFactors the found prime factors.
 * @return unfactored composites left after stopping ECM, empty map if N has been factored completely
 */
public SortedMultiset<BigInteger> factorize(BigInteger N, SortedMap<BigInteger, Integer> primeFactors) {
	// set up new N
	EC = 1;
	
	// Do trial division by all primes < 131072.
	SortedMultiset<BigInteger> unresolvedComposites = new SortedMultiset_BottomUp<>();
	N = tdiv.findSmallFactors(N, 131072, primeFactors); // TODO do outside ECM?
	if (N.equals(I_1)) {
		return unresolvedComposites;
	}
	
	// There are factors greater than 131071, and they may be prime or composite.
	if (isProbablePrime(N)) {
		addToMap(N, 1, primeFactors);
		return unresolvedComposites;
	}
	
	// N is composite -> do ECM
	TreeMap<BigInteger, Integer> compositesToTest = new TreeMap<BigInteger, Integer>();
	compositesToTest.put(N, 1);
	while (!compositesToTest.isEmpty()) {
		// get next composite to test
		Entry<BigInteger, Integer> compositeEntry = compositesToTest.pollLastEntry();
		N = compositeEntry.getKey();
		int exp = compositeEntry.getValue();
		
		// pure power?
		PurePowerTest.Result r = powerTest.test(N);
		if (r != null) {
			// N is a pure power!
			addToMapDependingOnPrimeTest(r.base, exp*r.exponent, primeFactors, compositesToTest);
			continue; // test next composite
		}

		// ECM
		final BigInteger NN = fnECM(N);
		if (NN.equals(I_1)) {
			// N is composite but could not be resolved
			addToMap(N, exp, unresolvedComposites);
			continue;
		}
		// NN is a factor of N
		addToMapDependingOnPrimeTest(NN, exp, primeFactors, compositesToTest);
		addToMapDependingOnPrimeTest(N.divide(NN), exp, primeFactors, compositesToTest);
	}
	return unresolvedComposites;
}
 
/**
 * Returns the gradient of this random variable with respect to all its leaf nodes.
 * The method calculated the map \( v \mapsto \frac{d u}{d v} \) where \( u \) denotes <code>this</code>.
 *
 * Performs a backward automatic differentiation.
 *
 * @return The gradient map.
 */
@Override
public Map<Long, RandomVariable> getGradient(final Set<Long> independentIDs) {

	// The map maintaining the derivatives id -> derivative
	final Map<Long, RandomVariable> derivatives = new HashMap<>();
	// Put derivative of this node w.r.t. itself
	derivatives.put(getID(), one);

	// The set maintaining the independents. Note: TreeMap is maintaining a sorting on the keys.
	final TreeMap<Long, OperatorTreeNode> independents = new TreeMap<>();
	// Initialize with root node
	independents.put(getID(), getOperatorTreeNode());

	while(independents.size() > 0) {
		// Get and remove node with the highest id in independents
		final Map.Entry<Long, OperatorTreeNode> independentEntry = independents.pollLastEntry();
		final Long id = independentEntry.getKey();
		final OperatorTreeNode independent = independentEntry.getValue();

		// Process this node (node with highest id in independents)
		final List<OperatorTreeNode> arguments = independent.arguments;
		if(arguments != null && arguments.size() > 0) {
			// Node has arguments: Propagate derivative to arguments.
			independent.propagateDerivativesFromResultToArgument(derivatives);

			// Remove id of this node from derivatives - keep only leaf nodes.
			if(isGradientRetainsLeafNodesOnly()) {
				derivatives.remove(id);
			}

			// Add all non leaf node arguments to the list of independents
			for(final OperatorTreeNode argument : arguments) {
				// If an argument is null, it is a (non-differentiable) constant.
				if(argument != null) {
					independents.put(argument.id, argument);
				}
			}
		}

		if(independentIDs != null && independentIDs.contains(id)) {
			derivatives.remove(id);
		}
	}

	return derivatives;
}
 
/**
 * Returns the gradient of this random variable with respect to all its leaf nodes.
 * The method calculated the map \( v \mapsto \frac{d u}{d v} \) where \( u \) denotes <code>this</code>.
 *
 * Performs a backward automatic differentiation.
 *
 * @return The gradient map.
 */
@Override
public Map<Long, RandomVariable> getGradient(final Set<Long> independentIDs) {

	// The map maintaining the derivatives id -> derivative
	final Map<Long, RandomVariable> derivatives = new HashMap<>();
	// Put derivative of this node w.r.t. itself
	derivatives.put(getID(), one);

	// The set maintaining the independents. Note: TreeMap is maintaining a sorting on the keys.
	final TreeMap<Long, OperatorTreeNode> independents = new TreeMap<>();
	// Initialize with root node
	independents.put(getID(), getOperatorTreeNode());

	while(independents.size() > 0) {
		// Get and remove node with the highest id in independents
		final Map.Entry<Long, OperatorTreeNode> independentEntry = independents.pollLastEntry();
		final Long id = independentEntry.getKey();
		final OperatorTreeNode independent = independentEntry.getValue();

		// Process this node (node with highest id in independents)
		final List<OperatorTreeNode> arguments = independent.arguments;
		if(arguments != null && arguments.size() > 0) {
			// Node has arguments: Propagate derivative to arguments.
			independent.propagateDerivativesFromResultToArgument(derivatives);

			// Remove id of this node from derivatives - keep only leaf nodes.
			if(isGradientRetainsLeafNodesOnly()) {
				derivatives.remove(id);
			}

			// Add all non leaf node arguments to the list of independents
			for(final OperatorTreeNode argument : arguments) {
				// If an argument is null, it is a (non-differentiable) constant.
				if(argument != null) {
					independents.put(argument.id, argument);
				}
			}
		}

		if(independentIDs != null && independentIDs.contains(id)) {
			derivatives.remove(id);
		}
	}

	return derivatives;
}