本文整理汇总了C++中PriorityQueue::isEmpty方法的典型用法代码示例。如果您正苦于以下问题:C++ PriorityQueue::isEmpty方法的具体用法?C++ PriorityQueue::isEmpty怎么用?C++ PriorityQueue::isEmpty使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类PriorityQueue
的用法示例。
在下文中一共展示了PriorityQueue::isEmpty方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C++代码示例。
示例1: main
int main() {
PriorityQueue<int> *testQueue = new PriorityQueue<int>(3);
// push a few random int's
testQueue->enqueue(4);
testQueue->enqueue(7);
testQueue->enqueue(1);
// queue shouldn't be empty; check this
if(testQueue->isEmpty()) {
cout << "The queue is empty!" << endl;
}
// pop items (more items than were pushed)
cout << testQueue->dequeue() << endl;
cout << testQueue->dequeue() << endl;
cout << testQueue->dequeue() << endl;
cout << testQueue->dequeue() << endl;
// should be empty now; check this
if(testQueue->isEmpty()) {
cout << "The queue is empty!" << endl;
}
return 0;
}
示例2: main
int main(){
PriorityQueue* pr = new PriorityQueue();
pr->insertElem(4,13);
pr->insertElem(3,5);
pr->insertElem(2,9);
pr->insertElem(1,2);
for(int i=0 ; i<pr->size() ;i++){
printf("%d\n", pr->minQueue[i]->priority );
}
if( pr->contains(4) ) {
pr->changePriority(4,1);
}
while(!pr->isEmpty()){
Element * elem = pr->minPriority();
printf("(%d,%d)\n",elem->vertex,elem->priority);
}
}
示例3: pathBuilderToStart
vector<Node *> dijkstrasAlgorithm(BasicGraph& graph, Vertex* start, Vertex* end) {
graph.resetData();
vector<Vertex*> path;
for (Vertex* node : graph.getNodeSet()){ //initiate all nodes with an inf. cost
node->cost = INFINITY;
}
PriorityQueue<Vertex*> pQueue;
start->cost = 0;
pQueue.enqueue(start, start->cost); //start of search
while (!pQueue.isEmpty()){
Vertex* v = pQueue.dequeue();
v->visited = true;
v->setColor(GREEN);
if (v == end){
return pathBuilderToStart(start, v); //same as the one bfs uses
}else{
for(Arc* edge : graph.getEdgeSet(v)){ //Go through each edge from the Vertex v, counting the cost for each newly visited vertex
Vertex* next = edge->finish;
if (!next->visited){
double cost = v->cost + edge->cost;
if (cost < next->cost){ //found a lesser cost, what dijkstra is all about
next->setColor(YELLOW);
next->cost = cost;
next->previous = v;
pQueue.enqueue(next, cost);
//pQueue.changePriority(next, cost); Only do this if next already is in the queue, but this should not ever occur?
}
}
}
}
}
return path;
}
示例4: aStar
/*
* Method: aStar
* Parameters: BasicGraph graph by reference
* Vertex pointer variable for path start
* Vertex pointer variable for path end
* - - - - - - - - - - - - - - - - - - - - - - - - - -
* Returns a Vector of Vertex pointer variables for which
* each index corresponds to a step in the path between
* the path's start and end points, if any exists.
* This function uses A*'s algorithm, which evaluates
* the estimated total cost of all neighbors' paths to the end
* from the starting vertex before continuing. It differs from Dijkstra's
* in its prioritization of location based on their estimated total cost
* or distance from the starting point, as opposed to the current cost
* up to that point. Because A* also orients subsequent vertices
* to their earlier parent, this function uses a helper function to
* rewind the shortest route from end to start, if applicable,
* returning an empty vector if not.
*/
Vector<Vertex*> aStar(BasicGraph& graph, Vertex* start, Vertex* end) {
graph.resetData();
Vector<Vertex*> path;
PriorityQueue<Vertex*> pqueue;
pqueue.enqueue(start, heuristicFunction(start, end));
while(!pqueue.isEmpty()) {
Vertex* current = pqueue.dequeue();
visitVertex(current);
if(BasicGraph::compare(current, end) == 0) break;
for(Edge* edge : current->edges) {
double cost = current->cost + edge->cost;
if(!edge->finish->visited &&
edge->finish->getColor() != YELLOW) {
enqueueVertex(edge->finish, current, cost);
pqueue.enqueue(edge->finish, cost +
heuristicFunction(edge->finish, end));
}
if(edge->finish->getColor() == YELLOW &&
cost < edge->finish->cost) {
enqueueVertex(edge->finish, current, cost);
pqueue.changePriority(edge->finish, cost +
heuristicFunction(edge->finish, end));
}
}
}
determinePath(start, end, path);
return path;
}
示例5: buildTree
Node* buildTree(Vector <int> & weightOfChars, char & delimiter){
PriorityQueue<Node*> weightsOfNodes; // declare a PriorityQueue for simple sorting weights of nodes
bool flag = 1; // set flag to prevent overwriting a delimiter
for(int i = 0; i < weightOfChars.size(); ++i){
if(weightOfChars[i] > 0){
Node* node = new Node((i - 128), weightOfChars[i]);
weightsOfNodes.enqueue(node, weightOfChars[i]);
}
else if((flag && (i < 128)) || (flag && (i > 159))){ // looking for a character that is not a control code and has a number of occurences which is equal zero
delimiter = i;
flag = 0;
}
}
// if file is empty the pointer of root of the tree is a NULL
if(weightsOfNodes.isEmpty()){
return NULL;
}
while (weightsOfNodes.size() > 1) {
Node* lNode = weightsOfNodes.dequeue();
Node* rNode = weightsOfNodes.dequeue();
Node* newNode = new Node(lNode, rNode);
weightsOfNodes.enqueue(newNode, newNode->weight);
}
Node* root = weightsOfNodes.dequeue();
return root;
}
示例6: while
vector<T> print_queue(PriorityQueue<T> &q){
vector<T> ret;
while(!q.isEmpty()) {
ret.push_back(q.getTop());
q.pop();
}
return ret;
}
示例7: dijkstra
float* Dijkstra::dijkstra(Graph *&graph, int s)
{
int n = graph->getVerticesNum();
if ((s < 0)||(s >= n))
return 0;
int m = graph->getRealSize();
Data** dist = new Data*[n];
int* up = new int[n];
for (int i = 0; i < n; i++){
up[i] = 0;
dist[i] = new DataFloat(i, FLT_MAX);
}
dist[s]->priority = 0;
PriorityQueue *queue = new PriorityQueue(dist, n, 4);
Edge** edges = graph->getEdgeSet();
int edgeCount = m;
while ((edgeCount != 0) && (!queue->isEmpty()))
{
Data* tmp = queue->pop();
int v = ((DataFloat*)tmp)->v;
int v0 = -1;
float delta;
for (int i = 0; i < m; i++)
{
v0 = -1;
if (edges[i]->K == v)
v0 = edges[i]->N;
if (edges[i]->N == v)
v0 = edges[i]->K;
if (v0 == -1) continue;
//edgeCount--;
delta = dist[v0]->priorities - (dist[v]->priorities + graph->getWeight(v, v0));
if (delta > 0){
dist[v0]->priorities = graph->getWeight(v, v0) + dist[v]->priorities;
up[v0] = v;
}
}
}
float *result = new float[n];
for (int i = 0; i < n; i++)
result[i] = dist[i]->priorities;
for (int i = 0; i < n; i++)
delete dist[i];
delete []dist;
delete queue;
delete []up;
return result;
}
示例8: main
int main()
{
PriorityQueue<TaskData> taskPQ; // Priority queue of tasks
TaskData task; // Task
int simLength, // Length of simulation (minutes)
minute, // Current minute
numPtyLevels = 2, // Number of priority levels
numArrivals = 0, // Number of new tasks arriving
j; // Loop counter
// Seed the random number generator
srand((unsigned int)time(NULL));
// cout << endl << "Enter the number of priority levels : ";
// cin >> numPtyLevels;
cout << "Enter the length of time to run the simulator : ";
cin >> simLength;
time_t timer;
for (minute = 0; minute < simLength; minute++)
{
// Dequeue the first task in the queue (if any).
if (taskPQ.isEmpty() == false)
{
task=taskPQ.dequeue();
cout << "Priority " << task.priority << " task arrived at " << task.arrived << " & completed at " << minute << endl;
}
numArrivals = rand() % 4;
switch (numArrivals)
{
case 2:
task.arrived = minute;
task.priority = rand() % numPtyLevels;
//task.priority = rand() % numPtyLevels - (task.arrived / simLength);
taskPQ.enqueue(task);
case 1:
task.arrived = minute;
task.priority = rand() % numPtyLevels;
taskPQ.enqueue(task);
default:
break;
}
}
return 0;
}
示例9: if
Vector<Vertex*> dijkstrasAlgorithm(BasicGraph& graph, Vertex* start, Vertex* end) {
graph.resetData();
PriorityQueue<Vertex*> pqueue;
Vector<Vertex*> path;
pqueue.enqueue(start, 0);
start->cost = 0.0;
start->setColor(YELLOW);
while(!pqueue.isEmpty())
{
Vertex* v = pqueue.dequeue();
v->setColor(GREEN);
if (v == end)
break;
for (auto edge : v->edges)
{
Vertex* next_v = edge->finish;
if (next_v->getColor() == 0)
{
next_v->setColor(YELLOW);
double next_cost = v->cost + edge->cost;
next_v->previous = v;
next_v->cost = next_cost;
pqueue.enqueue(next_v, next_cost);
}
else if (next_v->getColor() == YELLOW)
{
double next_cost = v->cost + edge->cost;
if (next_cost < next_v->cost)
{
next_v->cost = next_cost;
next_v->previous = v;
pqueue.changePriority(next_v, next_v->cost);
}
}
}
}
if (end->getColor() == GREEN)
{
Vertex* path_v = end;
while (path_v->previous)
{
path.insert(0, path_v);
path_v = path_v->previous;
}
path.insert(0, path_v);
}
return path;
}
示例10: aStar
/*
* A* search. Will explore from 'end' until it finds 'start' or can determine that no valid path exists.
* It explores in reverse to make it easier to build path array.
*/
vector<Node *> aStar(BasicGraph& graph, Vertex* start, Vertex* end) {
graph.resetData();
vector<Vertex*> path;
PriorityQueue<Vertex*> openQueue;
//Setup starting state
end->cost = 0;
end->visited = true;
openQueue.enqueue(end, end->cost);
Vertex* current = nullptr;
//While there are still reachable nodes to explore
while(!openQueue.isEmpty()){
//Set current node to the next node in the queue
current = openQueue.dequeue();
current->setColor(GREEN);
//If current node is target, build path and return
if(current == start){
rewind(end, start, path);
return path;
}
//Add any unvisited neighbor to queue, adjust cost for already visited neighbor nodes
for(Vertex* neighbor : graph.getNeighbors(current)){
if(!neighbor->visited){
neighbor->previous = current;
neighbor->visited = true;
neighbor->cost = current->cost + graph.getArc(current, neighbor)->cost;
openQueue.enqueue(neighbor, neighbor->cost + neighbor->heuristic(start));
neighbor->setColor(YELLOW);
}else{
double newCost = current->cost + graph.getArc(current, neighbor)->cost;
if(neighbor->cost > newCost){
neighbor->previous = current;
neighbor->cost = newCost;
openQueue.changePriority(neighbor, neighbor->cost + neighbor->heuristic(start));
neighbor->setColor(YELLOW);
}
}
}
}
return path;
}
示例11: kruskal
/*
* Method: kruskal
* Parameters: BasicGraph graph by reference
* - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
* Returns a set of Edges that correspond to the minimum
* spanning tree of the graph passed by reference. This Kruskal
* algorithm implementation leverages a Priority Queue to
* connect each Vertex with its minimum cost Edge without
* creating cycles, ensuring its a minimum spanning tree.
*/
Set<Edge*> kruskal(BasicGraph& graph) {
Set<Edge*> mst;
Map<Vertex*, Set<Vertex*> > clusters;
PriorityQueue<Edge*> pqueue;
initializeClusters(graph, clusters);
enqueueEdges(graph, pqueue);
while(!pqueue.isEmpty()) {
Edge* edge = pqueue.dequeue();
if(!clusters[edge->start].contains(edge->finish)) {
mergeClusters(clusters, edge->start, edge->finish);
mst.add(edge);
}
}
return mst;
}
示例12: getOptimalPath
int* getOptimalPath(Vertex Graph[], int size, int start, int goal)
{
PriorityQueue<float> PQ;
LinkedList<edge>* Edges;
int current, next, i;
float NewCost;
float *CostSoFar = new float[size];
for (i = 1; i < size; i++)
CostSoFar[i] = FLT_MAX;
CostSoFar[0] = 0.0;
int* parent = new int[size];
//PQ.insert(start, getEuclideanDistance(Coordinates[start], Coordinates[goal]));
PQ.insert(start, 0);
while (!PQ.isEmpty())
{
//PQ.Print();
current = PQ.remove();
if (current == goal)
{
delete [] CostSoFar;
return parent;
}
else
{
Edges = &Graph[current].getEdges();
for (Edges->begin(); !Edges->end(); Edges->next())
{
next = Edges->getCurrent().to;
NewCost = CostSoFar[current] + Edges->getCurrent().cost /*+ getEuclideanDistance(Coordinates[next], Coordinates[goal])*/;
if (NewCost < CostSoFar[next])
{
CostSoFar[next] = NewCost;
parent[next] = current;
PQ.insert(next, NewCost);
}
}
}
}
delete [] CostSoFar;
return NULL;
}
示例13: dijkstra
void Graph::dijkstra(Vertex *src)
{
comparisons = 0;//tallies number of comparisons made
src->d = 0;
src->p = NULL;
PriorityQueue<Vertex*> pq;
for(int i = 0;i<vertList.size();++i){//Fill priority queue
if(vertList[i]->getID() != src->getID()){
vertList[i]->d = INT_MAX;
vertList[i]->p = NULL;
}
pq.insertItem(vertList[i]->d, vertList[i], vertList[i]->getID());
}
while(!pq.isEmpty()){//Start sifting through the vertices
Vertex* u = pq.minElement();
pq.removeMin();
int sz = u->outList.size();
for(int i =0; i < sz;++i){
int alt = u->d + u->outList[i]->getWeight();
Vertex* evalVert = u->outList[i]->geteVertP();
comparisons++;
if(u->outList[i]->geteVertP()->d == INT_MAX){//relax function
Locator repkey = pq.getLoc(evalVert->getID());
pq.replaceKey(repkey,alt);
evalVert->setD(alt);
evalVert->setP(u);
}
else if(alt < u->outList[i]->geteVertP()->d){//relax function
Locator repkey = pq.getLoc(evalVert->getID());
pq.replaceKey(repkey,alt);
evalVert->setD(alt);
evalVert->setP(u);
}
}
}
comparisons += pq.getComps();//grab comparisons from priority queue
return;
}
示例14: main
int main()
{
PriorityQueue<DataType> testPQA;
PriorityQueue<DataType> testPQB;
PriorityQueue<DataType> testPQC;
char cmdChar;
DataType dataItem;
int qPriority;
char qProcess[ SMALL_STR_LEN ];
bool dataChanged;
ShowMenu();
do
{
dataChanged = false;
cout << endl << "Command: "; // Read command
cmdChar = GetCommandInput( qProcess, qPriority );
switch ( cmdChar )
{
case 'c': case 'C': // clear priority queue
while( !testPQA.isEmpty() )
{
testPQA.dequeue( dataItem );
}
if( VERBOSE )
{
cout << " Priority Queue has been cleared " << endl;
}
dataChanged = true;
break;
case 'd': case 'D': // dequeue one data item
testPQA.dequeue( dataItem );
if( VERBOSE )
{
cout << " Process: " << dataItem.process
<< " has been dequeued with a priority of "
<< dataItem.priority << PERIOD << endl;
}
dataChanged = true;
break;
case 'e': case 'E': // enqueue
testPQA.enqueue( qPriority, qProcess );
if( VERBOSE )
{
cout << " Process: " << qProcess
<< " has been enqueued with a priority of "
<< qPriority << PERIOD << endl;
}
dataChanged = true;
break;
case 'h': case 'H': // help request
ShowMenu();
break;
case 'p': case 'P': // peek at next item
testPQA.peekAtFront( dataItem );
if( VERBOSE )
{
cout << " Process: " << dataItem.process
<< " with priority: " << dataItem.priority
<< " found at front of queue." << endl;
}
break;
case 'q': case 'Q': // quit the test program
if( VERBOSE )
{
cout << " End Program Requested" << endl;
}
cout << endl;
break;
case 'x': case 'X': // create copy constructor PQ
//.........这里部分代码省略.........
示例15: testPriorityQueue
void testPriorityQueue(){
PriorityQueue<int> mycontainer;
cout << "\n\n Begin test function for the PriorityQueue<T> class\n";
// Testing the enqueue function
cout << "Testing size of new empty container: " << mycontainer.length() << endl;
cout << "Testing enqueue(1), length(), and isEmpty() functions. mycontainer is empty? "
<< (mycontainer.isEmpty() ? " true\n" : "false\n");
mycontainer.enqueue(1);
cout << "Size is " << mycontainer.length() << endl;
cout << "Testing enqueue(2), length(), and isEmpty() functions. mycontainer is empty? "
<< (mycontainer.isEmpty() ? " true\n" : "false\n");
mycontainer.enqueue(2);
cout << "Size is " << mycontainer.length() << endl;
cout << "Testing enqueue(2), length(), and isEmpty() functions. mycontainer is empty? "
<< (mycontainer.isEmpty() ? " true\n" : "false\n");
mycontainer.enqueue(2);
cout << "Size is " << mycontainer.length() << endl;
cout << "Testing enqueue(2), length(), and isEmpty() functions. mycontainer is empty? "
<< (mycontainer.isEmpty() ? " true\n" : "false\n");
mycontainer.enqueue(2);
cout << "Size is " << mycontainer.length() << endl;
cout << "Testing enqueue(2), length(), and isEmpty() functions. mycontainer is empty? "
<< (mycontainer.isEmpty() ? " true\n" : "false\n");
mycontainer.enqueue(2);
cout << "Size is " << mycontainer.length() << endl;
cout << "Testing enqueue(2), length(), and isEmpty() functions. mycontainer is empty? "
<< (mycontainer.isEmpty() ? " true\n" : "false\n");
cout << "Size is " << mycontainer.length() << endl << endl;
int size = mycontainer.length();
cout << "Testing pop_back(), front(), back(), length() and isEmpty() functions \n"
<< "in a for loop with iterations greater than container size.";
for (int i = 0; i < size + 1; i++) {
cout << "\nIteration: " << i + 1 << "\n";
if (!mycontainer.isEmpty()) {
cout << " mycontainer is empty? " << (mycontainer.isEmpty() ? " true\n" : "false\n");
cout << "PriorityQueue size before pop is " << mycontainer.length() << endl;
//cout<<"Front of container is " << mycontainer.front()<<endl;
//cout<<"Back of container is " << mycontainer.back()<<endl;
//cout << "Popping: " << mycontainer.front() << endl << endl;
mycontainer.pop_back();
} else {
cout << "The PriorityQueue is empty.\n";
}
cout << "PriorityQueue size is now: " << mycontainer.length() << endl;
}
cout << "\nFinished for loop\n";
cout << "\nTesting the reference for front() and back() functions.\n";
cout << "Start with int test = 7. mycontainer.enqueue(test)\n";
int test = 7;
mycontainer.enqueue(test);
cout << "Testing with test = 8. test=mycontainer.front(). mycontainer.front() = 13 \n";
test = 8;
test = mycontainer.front();
mycontainer.front() = 13;
cout << "Test is now " << test << " front of container is " << mycontainer.front()
<< " back of container is " << mycontainer.back() << endl;
test = 11;
mycontainer.enqueue(test);
cout << "Back of container is: " << mycontainer.back() << endl;
cout << "Test is now " << test << " front of container is " << mycontainer.front()
<< " back of container is " << mycontainer.back() << endl;
mycontainer.back() = test;
cout << "Test is now " << test << " front of container is " << mycontainer.front()
<< " back of container is " << mycontainer.back() << endl;
cout << "mycontainer size is " << mycontainer.length() << endl;
cout << "\nTesting the clear() function: \n";
mycontainer.clear();
cout << "mycontainer size now is " << mycontainer.length()
<< " mycontainer is empty: "
<< (mycontainer.isEmpty() ? " true\n" : "false\n");
cout << "\nTesting assignment operator: container2 = mycontainer\n";
cout << "Filling mycontainer with ints starting at 42\n";
size = 5;
// Fill mycontainer with ints to test copy constructor
for (int i = 0; i < size; i++) {
mycontainer.enqueue(i + 41);
}
cout << "mycontainer size now is " << mycontainer.length()
<< " mycontainer is empty: "
<< (mycontainer.isEmpty() ? " true\n" : "false\n");
PriorityQueue<int> container2;
container2 = mycontainer;
cout << "mycontainer size is: " << mycontainer.length() << endl;
cout << "container2 size is: " << container2.length() << endl;
cout << "Testing the contents of container2 and mycontainer using back() and pop_back() functions:\n";
size = container2.length();
for (int i = 0; i < size; i++) {
cout << "Attempting front and pop functions. Iteration: " << i + 1 << "\n";
//.........这里部分代码省略.........