int main() { // print my name and this assignment's title cout << "LAB 11a: Write And Test A Priority Queue Template\n"; cout << "Programmer: Jacky Chow\n"; cout << "Editor(s) used: Notepad++\n"; cout << "Compiler(s) used: Visual C++\n"; cout << "File: " << __FILE__ << endl; cout << "Complied: " << __DATE__ << " at " << __TIME__ << endl << endl; PriorityQueue<int> pq; int temp; cout << "Created a PriorityQueue<int> named pq\n"; cout << "Its size should be 0. It is: " << pq.size() << endl; assert(0 == pq.size()); if(pq.empty()) cout << "pq.empty() returned true that it was empty\n"; else cout << "Error: pq.empty() returned false for empty PQ\n"; assert(pq.empty()); cout << "\nEnqueuing the ints 13, 8, 4, 20, 10 into pq\n"; pq.enqueue(13); pq.enqueue(8); pq.enqueue(4); pq.enqueue(20); pq.enqueue(10); if(!pq.empty()) cout << "pq.empty() returned false that it was empty\n"; else cout << "Error: pq.empty() returned true for a non-empty PQ\n"; assert(!pq.empty()); cout << "\nIts size should now be 5. It is: " << pq.size() << endl; assert(5 == pq.size()); cout << "The front should be 20. It is: " << pq.front() << endl; assert(20 == pq.front()); cout << "The back should be 10. It is: " << pq.back() << endl; assert(10 == pq.back()); //const copy constructor test { cout << "\nCreating const copy with copy constructor\n"; const PriorityQueue<int> copy = pq; if(!copy.empty()) cout << "copy.empty() returned false that it was empty\n"; else cout << "Error: copy.empty() returned true for a non-empty PQ\n"; assert(!copy.empty()); cout << "Copy size should now be 5. It is: " << copy.size() << endl; assert(5 == copy.size()); } //operator= copy test { cout << "\nCreating copy with with operator=\n"; PriorityQueue<int> copy; cout << "Should initially have size 0. It is: " << copy.size() << endl; assert(copy.empty()); cout << "Assigning copy to = pq\n"; copy = pq; if(!copy.empty()) cout << "copy.empty() returned false that it was empty\n"; else cout << "Error: copy.empty() returned true for a non-empty copy\n"; assert(!copy.empty()); cout << "Copy 2's size should now be 5. It is: " << copy.size() << endl; assert(5 == copy.size()); cout << "The front should be 20. It is: " << copy.front() << endl; assert(20 == copy.front()); cout << "The back should be 10. It is: " << copy.back() << endl; assert(10 == copy.back()); cout << "Dequeuing the entire copy of pq: \n"; for(; copy.size();) cout << copy.dequeue() << ' '; cout << "\nCopy should now be size 0. It is: " << copy.size() << endl; assert(copy.empty()); if(copy.empty()) cout << "copy.empty() returned true that it was empty\n"; else cout << "Error: copy.empty() returned false for an empty copy\n"; assert(copy.empty()); } temp = pq.dequeue(); cout << "\nDequeuing root of pq. It should return 20. It is: " << temp << endl; assert(20 == temp); cout << "\nIts size should now be 4. It is: " << pq.size() << endl; assert(4 == pq.size()); cout << "The front should be 13. It is: " << pq.front() << endl; assert(13 == pq.front()); cout << "The back should be 8. It is: " << pq.back() << endl; assert(8 == pq.back()); cout << "\nNow using clear to clear pq\n"; pq.clear(); cout << "Size should now be 0. It is: " << pq.size() << endl; assert(0 == pq.size()); if(pq.empty()) cout << "pq.empty() returned true that it was empty\n"; else cout << "Error: pq.empty() returned false for empty PQ\n"; assert(pq.empty()); }
void EstimatePropagator::propagate(OptimizableGraph::VertexSet& vset, const EstimatePropagator::PropagateCost& cost, const EstimatePropagator::PropagateAction& action, double maxDistance, double maxEdgeCost) { reset(); PriorityQueue frontier; for (OptimizableGraph::VertexSet::iterator vit=vset.begin(); vit!=vset.end(); ++vit){ OptimizableGraph::Vertex* v = static_cast<OptimizableGraph::Vertex*>(*vit); AdjacencyMap::iterator it = _adjacencyMap.find(v); assert(it != _adjacencyMap.end()); it->second._distance = 0.; it->second._parent.clear(); it->second._frontierLevel = 0; frontier.push(&it->second); } while(! frontier.empty()){ AdjacencyMapEntry* entry = frontier.pop(); OptimizableGraph::Vertex* u = entry->child(); double uDistance = entry->distance(); //cerr << "uDistance " << uDistance << endl; // initialize the vertex if (entry->_frontierLevel > 0) { action(entry->edge(), entry->parent(), u); } /* std::pair< OptimizableGraph::VertexSet::iterator, bool> insertResult = */ _visited.insert(u); OptimizableGraph::EdgeSet::iterator et = u->edges().begin(); while (et != u->edges().end()){ OptimizableGraph::Edge* edge = static_cast<OptimizableGraph::Edge*>(*et); ++et; int maxFrontier = -1; OptimizableGraph::VertexSet initializedVertices; for (size_t i = 0; i < edge->vertices().size(); ++i) { OptimizableGraph::Vertex* z = static_cast<OptimizableGraph::Vertex*>(edge->vertex(i)); AdjacencyMap::iterator ot = _adjacencyMap.find(z); if (ot->second._distance != numeric_limits<double>::max()) { initializedVertices.insert(z); maxFrontier = (max)(maxFrontier, ot->second._frontierLevel); } } assert(maxFrontier >= 0); for (size_t i = 0; i < edge->vertices().size(); ++i) { OptimizableGraph::Vertex* z = static_cast<OptimizableGraph::Vertex*>(edge->vertex(i)); if (z == u) continue; size_t wasInitialized = initializedVertices.erase(z); double edgeDistance = cost(edge, initializedVertices, z); if (edgeDistance > 0. && edgeDistance != std::numeric_limits<double>::max() && edgeDistance < maxEdgeCost) { double zDistance = uDistance + edgeDistance; //cerr << z->id() << " " << zDistance << endl; AdjacencyMap::iterator ot = _adjacencyMap.find(z); assert(ot!=_adjacencyMap.end()); if (zDistance < ot->second.distance() && zDistance < maxDistance){ //if (ot->second.inQueue) //cerr << "Updating" << endl; ot->second._distance = zDistance; ot->second._parent = initializedVertices; ot->second._edge = edge; ot->second._frontierLevel = maxFrontier + 1; frontier.push(&ot->second); } } if (wasInitialized > 0) initializedVertices.insert(z); } } } // writing debug information like cost for reaching each vertex and the parent used to initialize #ifdef DEBUG_ESTIMATE_PROPAGATOR cerr << "Writing cost.dat" << endl; ofstream costStream("cost.dat"); for (AdjacencyMap::const_iterator it = _adjacencyMap.begin(); it != _adjacencyMap.end(); ++it) { HyperGraph::Vertex* u = it->second.child(); costStream << "vertex " << u->id() << " cost " << it->second._distance << endl; } cerr << "Writing init.dat" << endl; ofstream initStream("init.dat"); vector<AdjacencyMapEntry*> frontierLevels; for (AdjacencyMap::iterator it = _adjacencyMap.begin(); it != _adjacencyMap.end(); ++it) { if (it->second._frontierLevel > 0) frontierLevels.push_back(&it->second); } sort(frontierLevels.begin(), frontierLevels.end(), FrontierLevelCmp()); for (vector<AdjacencyMapEntry*>::const_iterator it = frontierLevels.begin(); it != frontierLevels.end(); ++it) { AdjacencyMapEntry* entry = *it; OptimizableGraph::Vertex* to = entry->child(); initStream << "calling init level = " << entry->_frontierLevel << "\t ("; for (OptimizableGraph::VertexSet::iterator pit = entry->parent().begin(); pit != entry->parent().end(); ++pit) { initStream << " " << (*pit)->id(); } initStream << " ) -> " << to->id() << endl; } #endif }
void emptyFrameQueue(){ while(!frameQueue.empty()){ frameQueue.pop(); } }