Java Code Examples for java.util.TreeMap#pollLastEntry()

The following examples show how to use java.util.TreeMap#pollLastEntry() . 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: TreeMapTest.java    From openjdk-jdk9 with GNU General Public License v2.0 6 votes vote down vote up
/**
 * 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);
}
 
Example 2
Source File: TreeMapTest.java    From j2objc with Apache License 2.0 6 votes vote down vote up
/**
 * 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);
}
 
Example 3
Source File: EllipticCurveMethod.java    From symja_android_library with GNU General Public License v3.0 4 votes vote down vote up
/**
 * 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;
}
 
Example 4
Source File: RandomVariableDifferentiableAAD.java    From finmath-lib with Apache License 2.0 4 votes vote down vote up
/**
 * 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;
}
 
Example 5
Source File: RandomVariableDifferentiableAAD.java    From finmath-lib with Apache License 2.0 4 votes vote down vote up
/**
 * 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;
}