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

The following examples show how to use java.util.TreeMap#lastEntry() . These examples are extracted from open source projects. 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
private boolean[] teatWaClear(TeamRanking ranking, int capacity) {
    List<PbStatus> list = ranking.getPbStatus();
    boolean[] ans = new boolean[list.size()];
    TreeMap<PbStatus, Integer> ac = new TreeMap<>();
    int waCnt = 0;
    for (int i = 0; i < list.size(); ++i) {
        if(list.get(i).isSolved()) {
            waCnt += list.get(i).getWaCount();
            if(list.get(i).getWaCount() == 0) //1A奖励
                waCnt -= 1;
            ac.put(list.get(i), i);
        }
    }
    while(ac.size() >= 2 && waCnt > ac.size() * capacity) {
        Map.Entry<PbStatus, Integer> entry = ac.lastEntry();
        ans[entry.getValue()] = true;
        waCnt -= entry.getKey().getWaCount();
        ac.remove(entry.getKey());
    }
    return ans;
}
 
Example 2
/**
 * Note: This method has nothing to do with a regular getValue() implementation.
 * Its more designed as a remote debugger
 *
 * @throws WrongKeyPartitionException
 */
@Override
public String getValue(Long timestamp, String key) throws WrongKeyPartitionException {
  LOG.info("Query for timestamp {} and key {}", timestamp, key);
  if (Math.abs(key.hashCode() % getRuntimeContext().getNumberOfParallelSubtasks()) != getRuntimeContext().getIndexOfThisSubtask()) {
    throw new WrongKeyPartitionException("Key " + key + " is not part of the partition " +
      "of subtask " + getRuntimeContext().getIndexOfThisSubtask());
  }

  if (windows == null) {
    return "No windows created yet";
  }

  synchronized (windows) {
    Map<Long, CountAndAccessTime> window = windows.get(key);
    if (window == null) {
      return "Key is not known. Available campaign IDs " + windows.keySet().toString();
    }
    if (timestamp == null) {
      // return the latency of the last window:
      TreeMap<Long, CountAndAccessTime> orderedMap = new TreeMap<>(window);
      Map.Entry<Long, CountAndAccessTime> first = orderedMap.lastEntry();
      return Long.toString(first.getValue().lastAccessTime - first.getValue().lastEventTime);
    } else {
      // query with timestamp:
      long windowStart = timestamp - (timestamp % windowSize);
      long windowEnd = windowStart + windowSize;
      CountAndAccessTime cat = window.get(windowEnd);
      if (cat == null) {
        return "Timestamp not available";
      }
      return Long.toString(cat.lastAccessTime - cat.lastEventTime);
    }
  }
}
 
Example 3
private void packEventPointsIntoFingerprints(){
	int minTimeDifference = 7;//time steps
	//Pack the event points into fingerprints
	for(int i = 0; i < eventPoints.size();i++){
		int t1 = eventPoints.get(i).t;
		int f1 = eventPoints.get(i).f;
		int maxtFirstLevel = t1 + maxEventPointDeltaTInSteps;
		int maxfFirstLevel = f1 + maxEventPointDeltaFInBins;
		int minfFirstLevel = f1 - maxEventPointDeltaFInBins;
		
		//A list of fingerprints Per Event Point, ordered by energy of the combined event points
		TreeMap<Float,NCteQFingerprint> fingerprintsPerEventPoint = new TreeMap<Float,NCteQFingerprint>();
		
		for(int j = i + 1; j < eventPoints.size()  && eventPoints.get(j).t < maxtFirstLevel;j++){
			int t2 = eventPoints.get(j).t;
			int f2 = eventPoints.get(j).f;
			if(t1 != t2 && t2 > t1 + minTimeDifference && f2 > minfFirstLevel && f2 < maxfFirstLevel){
				int maxtScndLevel = t2 + maxEventPointDeltaTInSteps;
				int maxfScndLevel = f2 + maxEventPointDeltaFInBins;
				int minfScndLevel = f2 - maxEventPointDeltaFInBins;
				for(int k = j + 1; k < eventPoints.size() && eventPoints.get(k).t < maxtScndLevel ;k++){
					int f3 = eventPoints.get(k).f;
					int t3 = eventPoints.get(k).t;
					if(t2 != t3 && t3 > t2 + minTimeDifference && f3 > minfScndLevel && f3 < maxfScndLevel){
						float energy = eventPoints.get(k).contrast + eventPoints.get(j).contrast + eventPoints.get(i).contrast ;
						fingerprintsPerEventPoint.put(energy,new NCteQFingerprint(t1, f1, t2, f2, t3, f3));
					}
				}
			}
		}
		
		if(fingerprintsPerEventPoint.size() >= maxFingerprintsPerEventPoint ){
			for(int s = 0 ; s < maxFingerprintsPerEventPoint ; s++){
				Entry<Float, NCteQFingerprint> e = fingerprintsPerEventPoint.lastEntry();
				fingerprints.add(e.getValue());
				fingerprintsPerEventPoint.remove(e.getKey());
			}
		}else{
			fingerprints.addAll(fingerprintsPerEventPoint.values());	
		}
	}
}
 
Example 4
Source Project: JavaTutorial   File: TreeMapExample.java    License: MIT License 4 votes vote down vote up
public void getLargestEntry(TreeMap<String,String> maps){
    Map.Entry<String,String> entry =  maps.lastEntry();
    System.out.println("最大的Entry如下");
    System.out.print("key = " + entry.getKey());
    System.out.println(" value = " + entry.getValue());
}
 
Example 5
/**
 * Select a data pool given the requested replication factor.
 */
private String selectDataPool(Path path, int repl_wanted) throws IOException {
  /* map pool size -> pool name */
  TreeMap<Integer, String> pools = new TreeMap<Integer, String>();

  /*
   * Start with a mapping for the default pool. An error here would indicate
   * something bad, so we throw any exceptions. For configured pools we
   * ignore some errors.
   */
  int fd = ceph.__open(new Path("/"), CephMount.O_RDONLY, 0);
  String pool_name = ceph.get_file_pool_name(fd);
  ceph.close(fd);
  int replication = getPoolReplication(pool_name);
  pools.put(new Integer(replication), pool_name);

  /*
   * Insert extra data pools from configuration. Errors are logged (most
   * likely a non-existant pool), and a configured pool will override the
   * default pool.
   */
  String[] conf_pools = getConfiguredDataPools();
  for (String name : conf_pools) {
    try {
      replication = getPoolReplication(name);
      pools.put(new Integer(replication), name);
    } catch (IOException e) {
      LOG.warn("Error looking up replication of pool: " + name + ", " + e);
    }
  }

  /* Choose smallest entry >= target, or largest in map. */
  Map.Entry<Integer, String> entry = pools.ceilingEntry(new Integer(repl_wanted));
  if (entry == null)
    entry = pools.lastEntry();

  /* should always contain default pool */
  assert(entry != null);

  replication = entry.getKey().intValue();
  pool_name = entry.getValue();

  /* log non-exact match cases */
  if (replication != repl_wanted) {
    LOG.info("selectDataPool path=" + path + " pool:repl=" +
        pool_name + ":" + replication + " wanted=" + repl_wanted);
  }

  return pool_name;
}
 
Example 6
@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(), new RandomVariableFromDoubleArray(1.0));

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

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

		// Get arguments of this node and propagate derivative to arguments
		final List<OperatorTreeNode> arguments = independent.arguments;
		if(arguments != null && arguments.size() > 0) {
			independent.propagateDerivativesFromResultToArgument(derivatives);

			// Add all non constant arguments to the list of independents
			for(final OperatorTreeNode argument : arguments) {
				if(argument != null) {
					final Long argumentId = argument.id;
					independents.put(argumentId, argument);
				}
			}

			// Remove id from derivatives - keep only leaf nodes.
			derivatives.remove(id);
		}

		// Done with processing. Remove from map.
		independents.remove(id);
	}

	return derivatives;
}
 
Example 7
@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(), new RandomVariableFromDoubleArray(1.0));

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

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

		// Get arguments of this node and propagate derivative to arguments
		final List<OperatorTreeNode> arguments = independent.arguments;
		if(arguments != null && arguments.size() > 0) {
			independent.propagateDerivativesFromResultToArgument(derivatives);

			// Add all non constant arguments to the list of independents
			for(final OperatorTreeNode argument : arguments) {
				if(argument != null) {
					final Long argumentId = argument.id;
					independents.put(argumentId, argument);
				}
			}

			// Remove id from derivatives - keep only leaf nodes.
			derivatives.remove(id);
		}

		// Done with processing. Remove from map.
		independents.remove(id);
	}

	return derivatives;
}
 
Example 8
/**
 * 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(), new RandomVariableFromDoubleArray(1.0));

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

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

		// Get arguments of this node and propagate derivative to arguments
		final List<OperatorTreeNode> arguments = independent.arguments;
		if(arguments != null && arguments.size() > 0) {
			independent.propagateDerivativesFromResultToArgument(derivatives);

			// Add all non constant arguments to the list of independents
			for(final OperatorTreeNode argument : arguments) {
				if(argument != null) {
					final Long argumentId = argument.id;
					independents.put(argumentId, argument);
				}
			}

			// Remove id from derivatives - keep only leaf nodes.
			derivatives.remove(id);
		}

		// Done with processing. Remove from map.
		independents.remove(id);
	}

	return derivatives;
}
 
Example 9
@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(), new RandomVariableFromDoubleArray(1.0));

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

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

		// Get arguments of this node and propagate derivative to arguments
		final List<OperatorTreeNode> arguments = independent.arguments;
		if(arguments != null && arguments.size() > 0) {
			independent.propagateDerivativesFromResultToArgument(derivatives);

			// Add all non constant arguments to the list of independents
			for(final OperatorTreeNode argument : arguments) {
				if(argument != null) {
					final Long argumentId = argument.id;
					independents.put(argumentId, argument);
				}
			}

			// Remove id from derivatives - keep only leaf nodes.
			derivatives.remove(id);
		}

		// Done with processing. Remove from map.
		independents.remove(id);
	}

	return derivatives;
}
 
Example 10
@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(), new RandomVariableFromDoubleArray(1.0));

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

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

		// Get arguments of this node and propagate derivative to arguments
		final List<OperatorTreeNode> arguments = independent.arguments;
		if(arguments != null && arguments.size() > 0) {
			independent.propagateDerivativesFromResultToArgument(derivatives);

			// Add all non constant arguments to the list of independents
			for(final OperatorTreeNode argument : arguments) {
				if(argument != null) {
					final Long argumentId = argument.id;
					independents.put(argumentId, argument);
				}
			}

			// Remove id from derivatives - keep only leaf nodes.
			derivatives.remove(id);
		}

		// Done with processing. Remove from map.
		independents.remove(id);
	}

	return derivatives;
}
 
Example 11
/**
 * 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(), new RandomVariableFromDoubleArray(1.0));

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

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

		// Get arguments of this node and propagate derivative to arguments
		final List<OperatorTreeNode> arguments = independent.arguments;
		if(arguments != null && arguments.size() > 0) {
			independent.propagateDerivativesFromResultToArgument(derivatives);

			// Add all non constant arguments to the list of independents
			for(final OperatorTreeNode argument : arguments) {
				if(argument != null) {
					final Long argumentId = argument.id;
					independents.put(argumentId, argument);
				}
			}

			// Remove id from derivatives - keep only leaf nodes.
			derivatives.remove(id);
		}

		// Done with processing. Remove from map.
		independents.remove(id);
	}

	return derivatives;
}