LLVM API Documentation

SimpleGraph.h

Go to the documentation of this file.
00001 //===-- SimpleGraph.h - Simple PBQP Graph ----------------------*- C++ --*-===//
00002 //
00003 //                     The LLVM Compiler Infrastructure
00004 //
00005 // This file is distributed under the University of Illinois Open Source
00006 // License. See LICENSE.TXT for details.
00007 //
00008 //===----------------------------------------------------------------------===//
00009 //
00010 // Simple PBQP graph class representing a PBQP problem. Graphs of this type
00011 // can be passed to a PBQPSolver instance to solve the PBQP problem.
00012 //
00013 //===----------------------------------------------------------------------===//
00014 
00015 #ifndef LLVM_CODEGEN_PBQP_SIMPLEGRAPH_H
00016 #define LLVM_CODEGEN_PBQP_SIMPLEGRAPH_H
00017 
00018 #include "GraphBase.h"
00019 
00020 namespace PBQP {
00021 
00022 class SimpleEdge;
00023 
00024 class SimpleNode : public NodeBase<SimpleNode, SimpleEdge> {
00025 public:
00026   SimpleNode(const Vector &costs) :
00027     NodeBase<SimpleNode, SimpleEdge>(costs) {}
00028 };
00029 
00030 class SimpleEdge : public EdgeBase<SimpleNode, SimpleEdge> {
00031 public:
00032   SimpleEdge(const NodeIterator &node1Itr, const NodeIterator &node2Itr,
00033              const Matrix &costs) :
00034     EdgeBase<SimpleNode, SimpleEdge>(node1Itr, node2Itr, costs) {}
00035 };
00036 
00037 class SimpleGraph : public GraphBase<SimpleNode, SimpleEdge> {
00038 private:
00039 
00040   typedef GraphBase<SimpleNode, SimpleEdge> PGraph;
00041 
00042   void copyFrom(const SimpleGraph &other) {
00043     assert(other.areNodeIDsValid() &&
00044            "Cannot copy from another graph unless IDs have been assigned.");
00045    
00046     std::vector<NodeIterator> newNodeItrs(other.getNumNodes());
00047 
00048     for (ConstNodeIterator nItr = other.nodesBegin(), nEnd = other.nodesEnd();
00049          nItr != nEnd; ++nItr) {
00050       newNodeItrs[other.getNodeID(nItr)] = addNode(other.getNodeCosts(nItr));
00051     }
00052 
00053     for (ConstEdgeIterator eItr = other.edgesBegin(), eEnd = other.edgesEnd();
00054          eItr != eEnd; ++eItr) {
00055 
00056       unsigned node1ID = other.getNodeID(other.getEdgeNode1Itr(eItr)),
00057                node2ID = other.getNodeID(other.getEdgeNode2Itr(eItr));
00058 
00059       addEdge(newNodeItrs[node1ID], newNodeItrs[node2ID],
00060               other.getEdgeCosts(eItr));
00061     }
00062   }
00063 
00064   void copyFrom(SimpleGraph &other) {
00065     if (!other.areNodeIDsValid()) {
00066       other.assignNodeIDs();
00067     }
00068     copyFrom(const_cast<const SimpleGraph&>(other));
00069   }
00070 
00071 public:
00072 
00073   SimpleGraph() {}
00074 
00075 
00076   SimpleGraph(const SimpleGraph &other) : PGraph() {
00077     copyFrom(other);
00078   }
00079 
00080   SimpleGraph& operator=(const SimpleGraph &other) {
00081     clear();
00082     copyFrom(other);
00083     return *this;
00084   }
00085 
00086   NodeIterator addNode(const Vector &costs) {
00087     return PGraph::addConstructedNode(SimpleNode(costs));
00088   }
00089 
00090   EdgeIterator addEdge(const NodeIterator &node1Itr,
00091                        const NodeIterator &node2Itr,
00092                        const Matrix &costs) {
00093     return PGraph::addConstructedEdge(SimpleEdge(node1Itr, node2Itr, costs));
00094   }
00095 
00096 };
00097 
00098 }
00099 
00100 #endif // LLVM_CODEGEN_PBQP_SIMPLEGRAPH_H