Home | History | Annotate | Download | only in PBQP
      1 //===-- Solution.h ------- PBQP Solution ------------------------*- C++ -*-===//
      2 //
      3 //                     The LLVM Compiler Infrastructure
      4 //
      5 // This file is distributed under the University of Illinois Open Source
      6 // License. See LICENSE.TXT for details.
      7 //
      8 //===----------------------------------------------------------------------===//
      9 //
     10 // PBQP Solution class.
     11 //
     12 //===----------------------------------------------------------------------===//
     13 
     14 #ifndef LLVM_CODEGEN_PBQP_SOLUTION_H
     15 #define LLVM_CODEGEN_PBQP_SOLUTION_H
     16 
     17 #include "Math.h"
     18 #include "Graph.h"
     19 
     20 #include <map>
     21 
     22 namespace PBQP {
     23 
     24   /// \brief Represents a solution to a PBQP problem.
     25   ///
     26   /// To get the selection for each node in the problem use the getSelection method.
     27   class Solution {
     28   private:
     29 
     30     typedef std::map<Graph::ConstNodeItr, unsigned,
     31                      NodeItrComparator> SelectionsMap;
     32     SelectionsMap selections;
     33 
     34     unsigned r0Reductions, r1Reductions, r2Reductions, rNReductions;
     35 
     36   public:
     37 
     38     /// \brief Initialise an empty solution.
     39     Solution()
     40       : r0Reductions(0), r1Reductions(0), r2Reductions(0), rNReductions(0) {}
     41 
     42     /// \brief Number of nodes for which selections have been made.
     43     /// @return Number of nodes for which selections have been made.
     44     unsigned numNodes() const { return selections.size(); }
     45 
     46     /// \brief Records a reduction via the R0 rule. Should be called from the
     47     ///        solver only.
     48     void recordR0() { ++r0Reductions; }
     49 
     50     /// \brief Returns the number of R0 reductions applied to solve the problem.
     51     unsigned numR0Reductions() const { return r0Reductions; }
     52 
     53     /// \brief Records a reduction via the R1 rule. Should be called from the
     54     ///        solver only.
     55     void recordR1() { ++r1Reductions; }
     56 
     57     /// \brief Returns the number of R1 reductions applied to solve the problem.
     58     unsigned numR1Reductions() const { return r1Reductions; }
     59 
     60     /// \brief Records a reduction via the R2 rule. Should be called from the
     61     ///        solver only.
     62     void recordR2() { ++r2Reductions; }
     63 
     64     /// \brief Returns the number of R2 reductions applied to solve the problem.
     65     unsigned numR2Reductions() const { return r2Reductions; }
     66 
     67     /// \brief Records a reduction via the RN rule. Should be called from the
     68     ///        solver only.
     69     void recordRN() { ++ rNReductions; }
     70 
     71     /// \brief Returns the number of RN reductions applied to solve the problem.
     72     unsigned numRNReductions() const { return rNReductions; }
     73 
     74     /// \brief Set the selection for a given node.
     75     /// @param nItr Node iterator.
     76     /// @param selection Selection for nItr.
     77     void setSelection(Graph::NodeItr nItr, unsigned selection) {
     78       selections[nItr] = selection;
     79     }
     80 
     81     /// \brief Get a node's selection.
     82     /// @param nItr Node iterator.
     83     /// @return The selection for nItr;
     84     unsigned getSelection(Graph::ConstNodeItr nItr) const {
     85       SelectionsMap::const_iterator sItr = selections.find(nItr);
     86       assert(sItr != selections.end() && "No selection for node.");
     87       return sItr->second;
     88     }
     89 
     90   };
     91 
     92 }
     93 
     94 #endif // LLVM_CODEGEN_PBQP_SOLUTION_H
     95