595269c327e570fc6d8b5e765a7b8b193c103488
[oota-llvm.git] / lib / CodeGen / PBQP / SimpleGraph.h
1 #ifndef LLVM_CODEGEN_PBQP_SIMPLEGRAPH_H
2 #define LLVM_CODEGEN_PBQP_SIMPLEGRAPH_H
3
4 #include "GraphBase.h"
5
6 namespace PBQP {
7
8 class SimpleEdge;
9
10 class SimpleNode : public NodeBase<SimpleNode, SimpleEdge> {
11 public:
12   SimpleNode(const Vector &costs) :
13     NodeBase<SimpleNode, SimpleEdge>(costs) {}
14 };
15
16 class SimpleEdge : public EdgeBase<SimpleNode, SimpleEdge> {
17 public:
18   SimpleEdge(const NodeIterator &node1Itr, const NodeIterator &node2Itr,
19              const Matrix &costs) :
20     EdgeBase<SimpleNode, SimpleEdge>(node1Itr, node2Itr, costs) {}
21 };
22
23 class SimpleGraph : public GraphBase<SimpleNode, SimpleEdge> {
24 private:
25
26   typedef GraphBase<SimpleNode, SimpleEdge> PGraph;
27
28   void copyFrom(const SimpleGraph &other) {
29     assert(other.areNodeIDsValid() &&
30            "Cannot copy from another graph unless IDs have been assigned.");
31    
32     std::vector<NodeIterator> newNodeItrs(other.getNumNodes());
33
34     for (ConstNodeIterator nItr = other.nodesBegin(), nEnd = other.nodesEnd();
35          nItr != nEnd; ++nItr) {
36       newNodeItrs[other.getNodeID(nItr)] = addNode(other.getNodeCosts(nItr));
37     }
38
39     for (ConstEdgeIterator eItr = other.edgesBegin(), eEnd = other.edgesEnd();
40          eItr != eEnd; ++eItr) {
41
42       unsigned node1ID = other.getNodeID(other.getEdgeNode1Itr(eItr)),
43                node2ID = other.getNodeID(other.getEdgeNode2Itr(eItr));
44
45       addEdge(newNodeItrs[node1ID], newNodeItrs[node2ID],
46               other.getEdgeCosts(eItr));
47     }
48   }
49
50   void copyFrom(SimpleGraph &other) {
51     if (!other.areNodeIDsValid()) {
52       other.assignNodeIDs();
53     }
54     copyFrom(const_cast<const SimpleGraph&>(other));
55   }
56
57 public:
58
59   SimpleGraph() {}
60
61
62   SimpleGraph(const SimpleGraph &other) : PGraph() {
63     copyFrom(other);
64   }
65
66   SimpleGraph& operator=(const SimpleGraph &other) {
67     clear();
68     copyFrom(other);
69     return *this;
70   }
71
72   NodeIterator addNode(const Vector &costs) {
73     return PGraph::addConstructedNode(SimpleNode(costs));
74   }
75
76   EdgeIterator addEdge(const NodeIterator &node1Itr,
77                        const NodeIterator &node2Itr,
78                        const Matrix &costs) {
79     return PGraph::addConstructedEdge(SimpleEdge(node1Itr, node2Itr, costs));
80   }
81
82 };
83
84 }
85
86 #endif // LLVM_CODEGEN_PBQP_SIMPLEGRAPH_H