Lang Hames | 25e9651 | 2009-08-07 00:25:12 +0000 | [diff] [blame] | 1 | //===-- SimpleGraph.h - Simple PBQP Graph ----------------------*- 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 | // Simple PBQP graph class representing a PBQP problem. Graphs of this type |
| 11 | // can be passed to a PBQPSolver instance to solve the PBQP problem. |
| 12 | // |
| 13 | //===----------------------------------------------------------------------===// |
| 14 | |
Lang Hames | 3ca9a5b | 2009-08-06 23:32:48 +0000 | [diff] [blame] | 15 | #ifndef LLVM_CODEGEN_PBQP_SIMPLEGRAPH_H |
| 16 | #define LLVM_CODEGEN_PBQP_SIMPLEGRAPH_H |
| 17 | |
| 18 | #include "GraphBase.h" |
| 19 | |
| 20 | namespace PBQP { |
| 21 | |
| 22 | class SimpleEdge; |
| 23 | |
| 24 | class SimpleNode : public NodeBase<SimpleNode, SimpleEdge> { |
| 25 | public: |
| 26 | SimpleNode(const Vector &costs) : |
| 27 | NodeBase<SimpleNode, SimpleEdge>(costs) {} |
| 28 | }; |
| 29 | |
| 30 | class SimpleEdge : public EdgeBase<SimpleNode, SimpleEdge> { |
| 31 | public: |
| 32 | SimpleEdge(const NodeIterator &node1Itr, const NodeIterator &node2Itr, |
| 33 | const Matrix &costs) : |
| 34 | EdgeBase<SimpleNode, SimpleEdge>(node1Itr, node2Itr, costs) {} |
| 35 | }; |
| 36 | |
| 37 | class SimpleGraph : public GraphBase<SimpleNode, SimpleEdge> { |
| 38 | private: |
| 39 | |
| 40 | typedef GraphBase<SimpleNode, SimpleEdge> PGraph; |
| 41 | |
| 42 | void copyFrom(const SimpleGraph &other) { |
| 43 | assert(other.areNodeIDsValid() && |
| 44 | "Cannot copy from another graph unless IDs have been assigned."); |
| 45 | |
| 46 | std::vector<NodeIterator> newNodeItrs(other.getNumNodes()); |
| 47 | |
| 48 | for (ConstNodeIterator nItr = other.nodesBegin(), nEnd = other.nodesEnd(); |
| 49 | nItr != nEnd; ++nItr) { |
| 50 | newNodeItrs[other.getNodeID(nItr)] = addNode(other.getNodeCosts(nItr)); |
| 51 | } |
| 52 | |
| 53 | for (ConstEdgeIterator eItr = other.edgesBegin(), eEnd = other.edgesEnd(); |
| 54 | eItr != eEnd; ++eItr) { |
| 55 | |
| 56 | unsigned node1ID = other.getNodeID(other.getEdgeNode1Itr(eItr)), |
| 57 | node2ID = other.getNodeID(other.getEdgeNode2Itr(eItr)); |
| 58 | |
| 59 | addEdge(newNodeItrs[node1ID], newNodeItrs[node2ID], |
| 60 | other.getEdgeCosts(eItr)); |
| 61 | } |
| 62 | } |
| 63 | |
| 64 | void copyFrom(SimpleGraph &other) { |
| 65 | if (!other.areNodeIDsValid()) { |
| 66 | other.assignNodeIDs(); |
| 67 | } |
| 68 | copyFrom(const_cast<const SimpleGraph&>(other)); |
| 69 | } |
| 70 | |
| 71 | public: |
| 72 | |
| 73 | SimpleGraph() {} |
| 74 | |
| 75 | |
| 76 | SimpleGraph(const SimpleGraph &other) : PGraph() { |
| 77 | copyFrom(other); |
| 78 | } |
| 79 | |
| 80 | SimpleGraph& operator=(const SimpleGraph &other) { |
| 81 | clear(); |
| 82 | copyFrom(other); |
| 83 | return *this; |
| 84 | } |
| 85 | |
| 86 | NodeIterator addNode(const Vector &costs) { |
| 87 | return PGraph::addConstructedNode(SimpleNode(costs)); |
| 88 | } |
| 89 | |
| 90 | EdgeIterator addEdge(const NodeIterator &node1Itr, |
| 91 | const NodeIterator &node2Itr, |
| 92 | const Matrix &costs) { |
| 93 | return PGraph::addConstructedEdge(SimpleEdge(node1Itr, node2Itr, costs)); |
| 94 | } |
| 95 | |
| 96 | }; |
| 97 | |
| 98 | } |
| 99 | |
| 100 | #endif // LLVM_CODEGEN_PBQP_SIMPLEGRAPH_H |