1 //===-- AnnotatedGraph.h - Annotated 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 // Annotated PBQP Graph class. This class is used internally by the PBQP solver
11 // to cache information to speed up reduction.
13 //===----------------------------------------------------------------------===//
15 #ifndef LLVM_CODEGEN_PBQP_ANNOTATEDGRAPH_H
16 #define LLVM_CODEGEN_PBQP_ANNOTATEDGRAPH_H
18 #include "GraphBase.h"
23 template <typename NodeData
, typename EdgeData
> class AnnotatedEdge
;
25 template <typename NodeData
, typename EdgeData
>
26 class AnnotatedNode
: public NodeBase
<AnnotatedNode
<NodeData
, EdgeData
>,
27 AnnotatedEdge
<NodeData
, EdgeData
> > {
34 AnnotatedNode(const Vector
&costs
, const NodeData
&nodeData
) :
35 NodeBase
<AnnotatedNode
<NodeData
, EdgeData
>,
36 AnnotatedEdge
<NodeData
, EdgeData
> >(costs
),
39 NodeData
& getNodeData() { return nodeData
; }
40 const NodeData
& getNodeData() const { return nodeData
; }
44 template <typename NodeData
, typename EdgeData
>
45 class AnnotatedEdge
: public EdgeBase
<AnnotatedNode
<NodeData
, EdgeData
>,
46 AnnotatedEdge
<NodeData
, EdgeData
> > {
49 typedef typename GraphBase
<AnnotatedNode
<NodeData
, EdgeData
>,
50 AnnotatedEdge
<NodeData
, EdgeData
> >::NodeIterator
58 AnnotatedEdge(const NodeIterator
&node1Itr
, const NodeIterator
&node2Itr
,
59 const Matrix
&costs
, const EdgeData
&edgeData
) :
60 EdgeBase
<AnnotatedNode
<NodeData
, EdgeData
>,
61 AnnotatedEdge
<NodeData
, EdgeData
> >(node1Itr
, node2Itr
, costs
),
64 EdgeData
& getEdgeData() { return edgeData
; }
65 const EdgeData
& getEdgeData() const { return edgeData
; }
69 template <typename NodeData
, typename EdgeData
>
70 class AnnotatedGraph
: public GraphBase
<AnnotatedNode
<NodeData
, EdgeData
>,
71 AnnotatedEdge
<NodeData
, EdgeData
> > {
74 typedef GraphBase
<AnnotatedNode
<NodeData
, EdgeData
>,
75 AnnotatedEdge
<NodeData
, EdgeData
> > PGraph
;
77 typedef AnnotatedNode
<NodeData
, EdgeData
> NodeEntry
;
78 typedef AnnotatedEdge
<NodeData
, EdgeData
> EdgeEntry
;
81 void copyFrom(const AnnotatedGraph
&other
) {
82 if (!other
.areNodeIDsValid()) {
83 other
.assignNodeIDs();
85 std::vector
<NodeIterator
> newNodeItrs(other
.getNumNodes());
87 for (ConstNodeIterator nItr
= other
.nodesBegin(), nEnd
= other
.nodesEnd();
88 nItr
!= nEnd
; ++nItr
) {
89 newNodeItrs
[other
.getNodeID(nItr
)] = addNode(other
.getNodeCosts(nItr
));
92 for (ConstEdgeIterator eItr
= other
.edgesBegin(), eEnd
= other
.edgesEnd();
93 eItr
!= eEnd
; ++eItr
) {
95 unsigned node1ID
= other
.getNodeID(other
.getEdgeNode1(eItr
)),
96 node2ID
= other
.getNodeID(other
.getEdgeNode2(eItr
));
98 addEdge(newNodeItrs
[node1ID
], newNodeItrs
[node2ID
],
99 other
.getEdgeCosts(eItr
), other
.getEdgeData(eItr
));
106 typedef typename
PGraph::NodeIterator NodeIterator
;
107 typedef typename
PGraph::ConstNodeIterator ConstNodeIterator
;
108 typedef typename
PGraph::EdgeIterator EdgeIterator
;
109 typedef typename
PGraph::ConstEdgeIterator ConstEdgeIterator
;
113 AnnotatedGraph(const AnnotatedGraph
&other
) {
117 AnnotatedGraph
& operator=(const AnnotatedGraph
&other
) {
123 NodeIterator
addNode(const Vector
&costs
, const NodeData
&data
) {
124 return PGraph::addConstructedNode(NodeEntry(costs
, data
));
127 EdgeIterator
addEdge(const NodeIterator
&node1Itr
,
128 const NodeIterator
&node2Itr
,
129 const Matrix
&costs
, const EdgeData
&data
) {
130 return PGraph::addConstructedEdge(EdgeEntry(node1Itr
, node2Itr
,
134 NodeData
& getNodeData(const NodeIterator
&nodeItr
) {
135 return getNodeEntry(nodeItr
).getNodeData();
138 const NodeData
& getNodeData(const NodeIterator
&nodeItr
) const {
139 return getNodeEntry(nodeItr
).getNodeData();
142 EdgeData
& getEdgeData(const EdgeIterator
&edgeItr
) {
143 return getEdgeEntry(edgeItr
).getEdgeData();
146 const EdgeEntry
& getEdgeData(const EdgeIterator
&edgeItr
) const {
147 return getEdgeEntry(edgeItr
).getEdgeData();
150 SimpleGraph
toSimpleGraph() const {
153 if (!PGraph::areNodeIDsValid()) {
154 PGraph::assignNodeIDs();
156 std::vector
<SimpleGraph::NodeIterator
> newNodeItrs(PGraph::getNumNodes());
158 for (ConstNodeIterator nItr
= PGraph::nodesBegin(),
159 nEnd
= PGraph::nodesEnd();
160 nItr
!= nEnd
; ++nItr
) {
162 newNodeItrs
[getNodeID(nItr
)] = g
.addNode(getNodeCosts(nItr
));
165 for (ConstEdgeIterator
166 eItr
= PGraph::edgesBegin(), eEnd
= PGraph::edgesEnd();
167 eItr
!= eEnd
; ++eItr
) {
169 unsigned node1ID
= getNodeID(getEdgeNode1(eItr
)),
170 node2ID
= getNodeID(getEdgeNode2(eItr
));
172 g
.addEdge(newNodeItrs
[node1ID
], newNodeItrs
[node2ID
],
184 #endif // LLVM_CODEGEN_PBQP_ANNOTATEDGRAPH_H