WorkListPQueue.hpp

Go to the documentation of this file.
00001 
00017 #ifndef WorkListPQueue_h
00018 #define WorkListPQueue_h
00019 
00020 //#ifndef DirectedGraph_h
00021 //#include <libs/support/graphs/directedGraph/DirectedGraph.h>
00022 //#endif
00023 // Going to attempt to use DGraph instead
00024 #include <OpenAnalysis/Utils/OA_ptr.hpp>
00025 #include <OpenAnalysis/Utils/DGraph/DGraphInterface.hpp>
00026 #include <OpenAnalysis/Utils/DGraph/DGraphImplement.hpp>
00027 #include <OpenAnalysis/CFG/CFGInterface.hpp>
00028 #include <OpenAnalysis/CFG/CFG.hpp>
00029 
00030 #include <iostream>
00031 
00032 #include <sys/times.h>
00033 
00034 #include <vector>
00035 
00036 #include <set>
00037 
00038 #include <queue>
00039 
00040 
00041 
00042 #include <OpenAnalysis/DataFlow/WorkList.hpp>
00043 
00044 namespace OA {
00045   namespace DataFlow {
00046 
00047       /*
00048 class node_compare {
00049 
00050     public:
00051     
00052       node_compare(std::map<OA_ptr<DGraph::NodeInterface>, int> NodeToPriorityMap) 
00053           : mNodeToPriorityMap(NodeToPriorityMap)
00054       {
00055           
00056       }
00057 
00058         int operator()( const OA_ptr<DGraph::NodeInterface> x,
00059                         const OA_ptr<DGraph::NodeInterface> y )
00060         {
00061             return ( mNodeToPriorityMap[x] > mNodeToPriorityMap[y] ) ;
00062 
00063         }
00064 
00065     private:
00066         std::map<OA_ptr<DGraph::NodeInterface>, int> mNodeToPriorityMap;
00067 };
00068 */
00069 
00070 static std::map<OA_ptr<DGraph::NodeInterface>, int> NodeToPriorityMap;
00071 
00072 class node_compare {
00073 
00074     public:
00075 
00076         int operator()( const OA_ptr<DGraph::NodeInterface> x,
00077                         const OA_ptr<DGraph::NodeInterface> y)
00078         {
00079             return ( NodeToPriorityMap[x] > NodeToPriorityMap[y] ) ;
00080         }
00081 
00082 };
00083 
00084       
00085 //*********************************************************************
00086 // class Worklist
00087 //*********************************************************************
00088  
00089 class Worklist_PQueue : public WorkList {
00090 
00091     public:
00092 
00093 
00094         Worklist_PQueue(OA_ptr<DGraph::DGraphInterface> dg,
00095                         DGraph::DGraphEdgeDirection alongFlow) { 
00096 
00097             OA_ptr<DGraph::NodesIteratorInterface> nodeIterPtr
00098                = dg->getReversePostDFSIterator(alongFlow);
00099 
00100             int priority=1;
00101             for (; nodeIterPtr->isValid(); ++(*nodeIterPtr)) {
00102                 NodeToPriorityMap[nodeIterPtr->current()] = priority++;
00103                 worklist.push(nodeIterPtr->current());
00104                 worklistSet.insert(nodeIterPtr->current());
00105             }
00106 
00107         }
00108 
00109         virtual ~Worklist_PQueue() { }
00110         
00111         OA_ptr<DGraph::NodeInterface> getNext() 
00112         {
00113                OA_ptr<DGraph::NodeInterface> node;
00114                node = worklist.top();
00115                worklist.pop();
00116                worklistSet.erase(node);
00117                return node;
00118         }
00119 
00120         void add(OA_ptr<DGraph::NodeInterface> node)
00121         {
00122              if (worklistSet.find(node)==worklistSet.end()) {
00123                  worklist.push(node);
00124                  worklistSet.insert(node);
00125              }
00126         }
00127 
00128         bool isEmpty() 
00129         {
00130         
00131              if(worklist.empty()) 
00132              { 
00133                 return true; 
00134              } else {
00135                 return false;
00136              }
00137              
00138         }
00139 
00140         int getPriority( OA_ptr<DGraph::NodeInterface> node) {
00141             return NodeToPriorityMap[node];
00142         }
00143 
00144     private:
00145 
00146         // Member Functions
00147          
00148         virtual bool atDGraphNode(OA_ptr<DGraph::NodeInterface>,
00149                                   DGraph::DGraphEdgeDirection)
00150         {
00151             return false;
00152         }
00153 
00154         virtual bool atDGraphEdge(OA_ptr<DGraph::EdgeInterface>,
00155                                   DGraph::DGraphEdgeDirection)
00156         {
00157             return false;
00158         }
00159 
00160         virtual void finalizeNode(OA_ptr<DGraph::NodeInterface> node)
00161         {
00162         }
00163      
00164         virtual void finalizeEdge(OA_ptr<DGraph::EdgeInterface> edge)
00165         {
00166         }
00167 
00168         // Member Variables
00169        
00170         std::priority_queue<OA_ptr<DGraph::NodeInterface>,
00171                       std::vector<OA_ptr<DGraph::NodeInterface> >,
00172                       node_compare> worklist;
00173         
00174         std::set<OA_ptr<DGraph::NodeInterface> > worklistSet;
00175         
00176         
00177 };
00178 
00179 
00180   } // end of DataFlow
00181 }  // end of OA namespace
00182 
00183 #endif