本文整理汇总了Java中java.util.PriorityQueue.offer方法的典型用法代码示例。如果您正苦于以下问题:Java PriorityQueue.offer方法的具体用法?Java PriorityQueue.offer怎么用?Java PriorityQueue.offer使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类java.util.PriorityQueue
的用法示例。
在下文中一共展示了PriorityQueue.offer方法的12个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。
示例1: computeRelaxation
import java.util.PriorityQueue; //导入方法依赖的package包/类
/**
*
* @param arc
* @param paths
* @param distances
* @param queue
*/
private void computeRelaxation(Arc arc, Map<Node, ResidualArc> paths,
Map<Node, Integer> distances, PriorityQueue<Node> queue) {
Node srcNode = arc.getSourceNode();
Node destNode = arc.getDestinationNode();
int sd = distances.get(srcNode);
int dd = distances.get(destNode);
int cost = arc.getCost();
if (dd > sd + cost) {
distances.put(destNode, sd + cost);
paths.put(destNode, (ResidualArc) arc);
// There can be multiple instance of the same node in the queue.
// This is the only way to update the queue.
queue.offer(destNode);
}
}
示例2: main
import java.util.PriorityQueue; //导入方法依赖的package包/类
public static void main(String[] args)
{
// queue of capacity 11
PriorityQueue<Double> queue = new PriorityQueue<>();
// insert elements to queue
queue.offer(3.2);
queue.offer(9.8);
queue.offer(5.4);
System.out.print("Polling from queue: ");
// display elements in queue
while (queue.size() > 0)
{
System.out.printf("%.1f ", queue.peek()); // view top element
queue.poll(); // remove top element
}
}
示例3: testInQueue
import java.util.PriorityQueue; //导入方法依赖的package包/类
@Test
public void testInQueue() {
PriorityQueue<ExponentialBackoff> queue = new PriorityQueue<>();
ExponentialBackoff backoff1 = new ExponentialBackoff(params);
backoff.trackFailure();
backoff.trackFailure();
backoff1.trackFailure();
backoff1.trackFailure();
backoff1.trackFailure();
queue.offer(backoff);
queue.offer(backoff1);
assertEquals(queue.poll(), backoff); // The one with soonest retry time
assertEquals(queue.peek(), backoff1);
queue.offer(backoff);
assertEquals(queue.poll(), backoff); // Still the same one
}
示例4: main
import java.util.PriorityQueue; //导入方法依赖的package包/类
public static void main(String[] args) {
PriorityQueue<Integer> pq = new PriorityQueue<>();
// offer (add) some element
pq.offer(3);
pq.offer(2);
pq.offer(4);
System.out.println("Offer Some Element, autosort. Content: " + pq.toString());
// add
pq.add(1);
pq.add(5);
pq.add(2);
System.out.println("Add some element, autosort. Content: "+pq.toString());
// peek head element
System.out.println("Peek is " + pq.peek());
// poll head element
System.out.println("Poll is " + pq.poll()+", content: " + pq.toString());
}
示例5: improve
import java.util.PriorityQueue; //导入方法依赖的package包/类
/**
* Replace a given path with that of this candidate move.
*
* @param openMap The list of available nodes.
* @param openMapQueue The queue of available nodes.
* @param f The heuristic values for A*.
* @param sh An optional {@code SearchHeuristic} to apply.
*/
public void improve(HashMap<String, PathNode> openMap,
PriorityQueue<PathNode> openMapQueue,
HashMap<String, Integer> f,
SearchHeuristic sh) {
PathNode best = openMap.get(dst.getId());
if (best != null) {
openMap.remove(dst.getId());
openMapQueue.remove(best);
}
int fcost = cost;
if (sh != null && dst.getTile() != null) {
fcost += sh.getValue(dst.getTile());
}
f.put(dst.getId(), fcost);
openMap.put(dst.getId(), path);
openMapQueue.offer(path);
}
示例6: updateProxyPool
import java.util.PriorityQueue; //导入方法依赖的package包/类
public void updateProxyPool() {
PriorityQueue<HttpProxy> temp = new PriorityQueue<>(poolSize);
addressSet.clear();
while (proxies.size() > updateThreshold) {
addressSet.add(proxies.peek().address);
temp.offer(proxies.poll());
}
proxies.clear();
proxies = temp;
this.fill(HttpProxyCollector.getProxyIps(strategy, (poolSize - temp.size()) * 2));
}
示例7: getSortedTimestamps
import java.util.PriorityQueue; //导入方法依赖的package包/类
/**
* Gets the sorted timestamps of any buffered events.
*
* @return a sorted list of timestamps that have at least one buffered event.
*/
private PriorityQueue<Long> getSortedTimestamps() throws Exception {
PriorityQueue<Long> sortedTimestamps = new PriorityQueue<>();
for (Long timestamp : elementQueueState.keys()) {
sortedTimestamps.offer(timestamp);
}
return sortedTimestamps;
}
示例8: add
import java.util.PriorityQueue; //导入方法依赖的package包/类
/**
* Add this candidate to the open map+queue.
*
* @param openMap The list of available nodes.
* @param openMapQueue The queue of available nodes.
* @param cost The provisional cost for the path.
* @param sh A {@code SearchHeuristic} to apply.
*/
public void add(HashMap<String, PathNode> openMap,
PriorityQueue<PathNode> openMapQueue,
HashMap<String, Integer> f,
SearchHeuristic sh) {
int fcost = this.cost;
if (this.dst.getTile() != null) {
fcost += sh.getValue(this.dst.getTile());
}
f.put(this.dst.getId(), fcost);
PathNode path = new PathNode(this.dst, this.movesLeft, this.turns,
this.onCarrier, this.current, null);
openMap.put(this.dst.getId(), path);
openMapQueue.offer(path);
}
示例9: nextGeneration
import java.util.PriorityQueue; //导入方法依赖的package包/类
/**
* Advances the current population to the next generation. All objects are cloned during
* the process.
*/
@SuppressWarnings("unchecked")
public void nextGeneration() {
PriorityQueue<T> newPopulation = new PriorityQueue<T>(getPopulationSize(), new FitnessComparator());
//Take best n and copy into new population
newPopulation.addAll(getTopN(config.getNumElite()));
int fitnessSum = getFitnessSum();
while (newPopulation.size() < config.getPopulationSize()) {
T mommy = getWeightedOrganism(fitnessSum);
T daddy = getWeightedOrganism(fitnessSum);
T offspring = mommy;
if (shouldCrossover()) {
offspring = (T) mommy.breed(daddy);
}
if (shouldMutate()) {
offspring = (T) offspring.mutate();
}
newPopulation.offer(offspring);
}
generation++;
population = newPopulation;
}
示例10: testOfferNull
import java.util.PriorityQueue; //导入方法依赖的package包/类
/**
* offer(null) throws NPE
*/
public void testOfferNull() {
PriorityQueue q = new PriorityQueue(1);
try {
q.offer(null);
shouldThrow();
} catch (NullPointerException success) {}
}
示例11: testOfferNonComparable
import java.util.PriorityQueue; //导入方法依赖的package包/类
/**
* Offer of non-Comparable throws CCE
*/
public void testOfferNonComparable() {
PriorityQueue q = new PriorityQueue(1);
try {
q.offer(new Object());
shouldThrow();
} catch (ClassCastException success) {
assertTrue(q.isEmpty());
assertEquals(0, q.size());
assertNull(q.poll());
}
}
示例12: computeCOF
import java.util.PriorityQueue; //导入方法依赖的package包/类
public void computeCOF(ArrayList<COFObject> cofobjectList, int k, DistanceMeasure measure) {
// define a list of knn for each cof object
PriorityQueue<COFKnn> knnList = new PriorityQueue<COFKnn>();
// reset pcl, kDist, and deviation
double pcl = 0.0;
double kDist = 0.0;
double deviation = 0.0;
for (COFObject cofobject : cofobjectList) {// for all objects in the dataset
double distance = Double.POSITIVE_INFINITY;
// compute the distance to current object
distance = measure.calculateDistance(this.getValues(), cofobject.getValues());
COFKnn cOFKnn = new COFKnn(cofobject, distance);
// determine if cofobject is on of the nearest neighbors to current object
if (knnList.size() < k) {
knnList.offer(cOFKnn);
} else if (distance < knnList.peek().getDistance()) {
knnList.remove();
knnList.offer(cOFKnn);
}
// if the cofobject has the same class label, add its distance to deviation
if (this.getLabel() == cofobject.getLabel()) {
deviation += distance;
}
}
this.setDeviation(deviation); // save deviation
// compute pcl to current object
for (COFKnn cofKnn : knnList) {
kDist += measure.calculateDistance(getValues(), cofKnn.getCofobject().getValues());
if (this.getLabel() == cofKnn.getCofobject().getLabel()) {
pcl++;
}
}
this.setPcl(pcl); // save pcl
this.setCOF(pcl); // save the initial cof based on pcl
this.setKDist(kDist); // save kDist
}