1 //===-- SimpleGraph.h - Simple PBQP Graph ----------------------*- C++ --*-===//
3 // The LLVM Compiler Infrastructure
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
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.
13 //===----------------------------------------------------------------------===//
15 #ifndef LLVM_CODEGEN_PBQP_SIMPLEGRAPH_H
16 #define LLVM_CODEGEN_PBQP_SIMPLEGRAPH_H
18 #include "GraphBase.h"
24 class SimpleNode
: public NodeBase
<SimpleNode
, SimpleEdge
> {
26 SimpleNode(const Vector
&costs
) :
27 NodeBase
<SimpleNode
, SimpleEdge
>(costs
) {}
30 class SimpleEdge
: public EdgeBase
<SimpleNode
, SimpleEdge
> {
32 SimpleEdge(const NodeIterator
&node1Itr
, const NodeIterator
&node2Itr
,
33 const Matrix
&costs
) :
34 EdgeBase
<SimpleNode
, SimpleEdge
>(node1Itr
, node2Itr
, costs
) {}
37 class SimpleGraph
: public GraphBase
<SimpleNode
, SimpleEdge
> {
40 typedef GraphBase
<SimpleNode
, SimpleEdge
> PGraph
;
42 void copyFrom(const SimpleGraph
&other
) {
43 assert(other
.areNodeIDsValid() &&
44 "Cannot copy from another graph unless IDs have been assigned.");
46 std::vector
<NodeIterator
> newNodeItrs(other
.getNumNodes());
48 for (ConstNodeIterator nItr
= other
.nodesBegin(), nEnd
= other
.nodesEnd();
49 nItr
!= nEnd
; ++nItr
) {
50 newNodeItrs
[other
.getNodeID(nItr
)] = addNode(other
.getNodeCosts(nItr
));
53 for (ConstEdgeIterator eItr
= other
.edgesBegin(), eEnd
= other
.edgesEnd();
54 eItr
!= eEnd
; ++eItr
) {
56 unsigned node1ID
= other
.getNodeID(other
.getEdgeNode1Itr(eItr
)),
57 node2ID
= other
.getNodeID(other
.getEdgeNode2Itr(eItr
));
59 addEdge(newNodeItrs
[node1ID
], newNodeItrs
[node2ID
],
60 other
.getEdgeCosts(eItr
));
64 void copyFrom(SimpleGraph
&other
) {
65 if (!other
.areNodeIDsValid()) {
66 other
.assignNodeIDs();
68 copyFrom(const_cast<const SimpleGraph
&>(other
));
76 SimpleGraph(const SimpleGraph
&other
) : PGraph() {
80 SimpleGraph
& operator=(const SimpleGraph
&other
) {
86 NodeIterator
addNode(const Vector
&costs
) {
87 return PGraph::addConstructedNode(SimpleNode(costs
));
90 EdgeIterator
addEdge(const NodeIterator
&node1Itr
,
91 const NodeIterator
&node2Itr
,
92 const Matrix
&costs
) {
93 return PGraph::addConstructedEdge(SimpleEdge(node1Itr
, node2Itr
, costs
));
100 #endif // LLVM_CODEGEN_PBQP_SIMPLEGRAPH_H