OpenASIP  2.0
GraphUtilities.hh
Go to the documentation of this file.
1 /*
2  Copyright (c) 2002-2009 Tampere University.
3 
4  This file is part of TTA-Based Codesign Environment (TCE).
5 
6  Permission is hereby granted, free of charge, to any person obtaining a
7  copy of this software and associated documentation files (the "Software"),
8  to deal in the Software without restriction, including without limitation
9  the rights to use, copy, modify, merge, publish, distribute, sublicense,
10  and/or sell copies of the Software, and to permit persons to whom the
11  Software is furnished to do so, subject to the following conditions:
12 
13  The above copyright notice and this permission notice shall be included in
14  all copies or substantial portions of the Software.
15 
16  THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17  IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18  FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
19  THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20  LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
21  FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
22  DEALINGS IN THE SOFTWARE.
23  */
24 /**
25  * @file GraphUtilities.hh
26  *
27  * Declaration of miscellaneous generic graph utility functions for the
28  * prototype graph class. Probably these functions won't be needed once the
29  * boost and TCE framework applib-specific functions are fully utilised.
30  *
31  * @author Andrea Cilio 2005 (cilio-no.spam-cs.tut.fi)
32  * @author Vladimir Guzma 2006 (vladimir.guzma-no.spam-tut.fi)
33  * @note rating: red
34  */
35 
36 #ifndef TTA_GRAPH_UTILITIES_HH
37 #define TTA_GRAPH_UTILITIES_HH
38 
39 
40 /**
41  * Base template class for functors that apply to graph nodes.
42  *
43  * The node type is an independent template parameter. This enables using a
44  * subclass instead of the actual graph node class, or even an unrelated
45  * class, as long as it provides an appropriate conversion operator.
46  *
47  * When the graph contains several specialised types of nodes, the template
48  * parameter should be convertible to the base node type.
49  */
50 template<typename Graph, typename GraphNode>
52 public:
53  /// Constructor. Keep record of the parent graph of the nodes to work on.
54  GraphNodeFunctor(const Graph& pGraph) :
55  graph_(&pGraph) {}
56  /// Destructor. Subclasses should clean up any state created during
57  /// operation in this method.
58  virtual ~GraphNodeFunctor() { };
59  /// Operation to apply to a given node of the graph.
60  virtual void operator()(GraphNode&) const = 0;
61 
62 protected:
63  /// Expected parent graph.
64  const Graph* graph_;
65 };
66 
67 
68 /**
69  * Base template class for functors that apply to graph edges.
70  *
71  * The edge type is an independent template parameter. This enables using a
72  * subclass instead of the actual graph edge class, or even an unrelated
73  * class, as long as it provides an appropriate conversion operator.
74  *
75  * When the graph contains several specialised types of edges, the template
76  * parameter should be convertible to the base edge type.
77  */
78 template<typename Graph, typename GraphEdge>
80 public:
81  /// Constructor. Keep record of the parent graph of the nodes to work on.
82  GraphEdgeFunctor(const Graph& pGraph) :
83  graph_(&pGraph) {}
84  /// Destructor. Subclasses should clean up any state created during
85  /// operation in this method.
86  virtual ~GraphEdgeFunctor() { };
87  /// Operation to apply to a given edge of the graph.
88  virtual void operator()(GraphEdge&) const = 0;
89 
90 protected:
91  /// Expected parent graph.
92  const Graph* graph_;
93 };
94 
95 
96 ////////////////////////////////////////////////////////////////////////////
97 // Algorithms based on edge and node functors
98 ////////////////////////////////////////////////////////////////////////////
99 
100 /**
101  * Utility class. Its instances are function objects that delete the given
102  * pointer.
103  *
104  * Use it inside std::for_each and std::transform to deallocate storage
105  * pointer to by elements in a container.
106  */
107 template<class T>
108 class Destroyer {
109 public:
111  T* operator()(const T* element) {
112  delete element;
113  return NULL;
114  }
115 };
116 
117 
118 /**
119  * Algorithm: apply given functor to all nodes of a given graph.
120  *
121  * @param pGraph The input graph.
122  * @param functor The function object to apply to each node.
123  */
124 template<typename Graph, typename GraphNode>
125 void
127  Graph& pGraph,
128  const GraphNodeFunctor<Graph, GraphNode>& functor);
129 
130 
131 /**
132  * Algorithm: apply given functor to all edges of a given graph.
133  *
134  * @param pGraph The input graph.
135  * @param functor The function object to apply to each edge.
136  */
137 template<typename Graph, typename GraphEdge>
138 void
140  Graph& pGraph,
141  const GraphEdgeFunctor<Graph, GraphEdge>& functor);
142 
143 
144 #include "GraphUtilities.icc"
145 
146 #endif
GraphNodeFunctor::GraphNodeFunctor
GraphNodeFunctor(const Graph &pGraph)
Constructor. Keep record of the parent graph of the nodes to work on.
Definition: GraphUtilities.hh:54
GraphEdgeFunctor::operator()
virtual void operator()(GraphEdge &) const =0
Operation to apply to a given edge of the graph.
GraphEdgeFunctor
Definition: GraphUtilities.hh:79
GraphNodeFunctor::operator()
virtual void operator()(GraphNode &) const =0
Operation to apply to a given node of the graph.
GraphNodeFunctor::graph_
const Graph * graph_
Expected parent graph.
Definition: GraphUtilities.hh:64
doOnAllNodes
void doOnAllNodes(Graph &pGraph, const GraphNodeFunctor< Graph, GraphNode > &functor)
doOnAllEdges
void doOnAllEdges(Graph &pGraph, const GraphEdgeFunctor< Graph, GraphEdge > &functor)
Destroyer::operator()
T * operator()(const T *element)
Definition: GraphUtilities.hh:111
Destroyer
Definition: GraphUtilities.hh:108
GraphEdgeFunctor::GraphEdgeFunctor
GraphEdgeFunctor(const Graph &pGraph)
Constructor. Keep record of the parent graph of the nodes to work on.
Definition: GraphUtilities.hh:82
GraphEdgeFunctor::~GraphEdgeFunctor
virtual ~GraphEdgeFunctor()
Destructor. Subclasses should clean up any state created during operation in this method.
Definition: GraphUtilities.hh:86
GraphNodeFunctor
Definition: GraphUtilities.hh:51
GraphEdgeFunctor::graph_
const Graph * graph_
Expected parent graph.
Definition: GraphUtilities.hh:92
Destroyer::Destroyer
Destroyer()
Definition: GraphUtilities.hh:110
GraphEdge
Definition: GraphEdge.hh:52
GraphUtilities.icc
GraphNode
Definition: GraphNode.hh:42
GraphNodeFunctor::~GraphNodeFunctor
virtual ~GraphNodeFunctor()
Destructor. Subclasses should clean up any state created during operation in this method.
Definition: GraphUtilities.hh:58