torc-master
 All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
RouteTreeNode.hpp
Go to the documentation of this file.
1 // Torc - Copyright 2011-2013 University of Southern California. All Rights Reserved.
2 // $HeadURL$
3 // $Id$
4 
5 // This program is free software: you can redistribute it and/or modify it under the terms of the
6 // GNU General Public License as published by the Free Software Foundation, either version 3 of the
7 // License, or (at your option) any later version.
8 //
9 // This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY;
10 // without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See
11 // the GNU General Public License for more details.
12 //
13 // You should have received a copy of the GNU General Public License along with this program. If
14 // not, see <http://www.gnu.org/licenses/>.
15 
16 /// \file
17 /// \brief Header for the RouteTreeNode class.
18 
19 #ifndef TORC_ROUTER_ROUTETREENODE_HPP
20 #define TORC_ROUTER_ROUTETREENODE_HPP
21 
23 
24 namespace torc {
25 namespace router {
26 
27  // Pack RouteTreeNode objects tightly.
28  /// \todo Have to justify the packing decision, and its impact on memory footprint versus
29  /// performance.
30  #ifdef __GNUC__
31  #pragma pack(push, 2)
32  #endif
33 
34  /// \brief An object that holds more complete path information for routing and tracing.
35  /// \details A RouteTreeNode contains children pointers to allow tracing of arcs through a
36  /// device to recover complete nets from the device usage information.
37  class RouteTreeNode : public RouteNode {
38  // types
39  typedef std::vector<RouteTreeNode*>::iterator RouteTreeNodeIterator;
40  protected:
41  // members
42  /// \brief Pointer to a child node that is the only one.
44  /// \brief Pointer to a vector of child nodes that is dynamically allocated.
45  std::vector<RouteTreeNode*>* mChildren;
46  public:
47  // constructors
48  /// \brief Null Constructor.
49  //RouteTreeNode() : RouteNode(), mDepth(0), mOnlyChild(0), mChildren(0) {}
51  /// \brief Public Constructor.
52  RouteTreeNode(Tilewire inSource, Tilewire inSink, boost::int32_t inCost,
53  RouteTreeNode* inParent)
54  : RouteNode(inSource, inSink, inCost, inCost, 0, inParent), mOnlyChild(0),
55  mChildren(0) {
56  if (inParent != 0) {
57  mDepth = inParent->getDepth() + 1;
58  } else {
59  mDepth = 0;
60  }
61  }
62  /// \brief Public Constructor.
63  RouteTreeNode(Arc inArc, boost::int32_t inCost, RouteTreeNode* inParent)
64  : RouteNode(inArc, inCost, inCost, 0, inParent), mOnlyChild(0), mChildren(0) {
65  if (inParent != 0) {
66  mDepth = inParent->getDepth() + 1;
67  } else {
68  mDepth = 0;
69  }
70  }
71  /// \brief Destructor.
73  if (mOnlyChild != 0) { delete mOnlyChild; mOnlyChild = 0; }
74  if (mChildren != 0) {
75  RouteTreeNodeIterator p = mChildren->begin();
77  while (p < e) { delete *p, p++; }
78  delete mChildren; mChildren = 0;
79  }
80  }
81  // accessors
82 
83  // functions
84  /// \brief Add children to the node.
85  void addChildren(const std::vector<RouteTreeNode*>& newChildren) {
86  boost::uint32_t size = newChildren.size();
87  if (size == 0) { return; }
88 
89  // no child or 1 child
90  if (mChildren == 0) {
91  // adding 1 child to a node with 0
92  if (mOnlyChild == 0 && size == 1) {
93  mOnlyChild = newChildren[0];
94  return;
95  }
96  // node will have more than 1 child, create vector
97  mChildren = new std::vector<RouteTreeNode*>();
98  // if the node had 1 child, move it to the vector
99  if (mOnlyChild != 0) {
100  mChildren->reserve(size+1);
101  mChildren->push_back(mOnlyChild);
102  mOnlyChild = 0;
103  // node had 0 children, reserve space for the new children
104  } else {
105  mChildren->reserve(size);
106  }
107  // already 2 or more children
108  } else {
109  mChildren->reserve(mChildren->size() + size);
110  }
111  // insert children into the node
112  mChildren->insert(mChildren->end(), newChildren.begin(), newChildren.end());
113  }
114  /// \brief Allocate a new node and make it the parent of this node.
115  void makeParent(const Tilewire& inSource, const Tilewire& inSink) {
116  //mArc = Arc(inSource, inSink); // DON'T WANT TO DO THIS
117  //mParent = new RouteTreeNode(mArc.getSourceTilewire(), Tilewire::sInvalid, 0, 0);
118  mParent = new RouteTreeNode(inSource, inSink, 0, 0);
119  ((RouteTreeNode*)mParent)->mOnlyChild = this;
120  ((RouteTreeNode*)mParent)->mDepth = mDepth - 1;
121  }
122  /// \brief Get the number of children.
123  boost::uint16_t getNumChildren() {
124  if (mOnlyChild != 0) return 1;
125  else if (mChildren == 0) return 0;
126  return mChildren->size();
127  }
128  /// \brief Get a child by index, returns 0 for invalid index.
129  RouteTreeNode* getChild(unsigned int index) {
130  if (mOnlyChild != 0 && index == 0) return mOnlyChild;
131  if (mChildren != 0 && index < mChildren->size()) return (*mChildren)[index];
132  return 0;
133  }
134  /// \brief Normalize depth of nodes.
135  void normalizeDepth() {
136  RouteTreeNode* top = (RouteTreeNode*) getTop();
137  int topDepth = top->mDepth;
138  if (topDepth == 0) return;
139  top->adjustDepth(-topDepth);
140  }
141  protected:
142  /// \brief Recursively adjust node depths.
143  void adjustDepth(int adjustment) {
144  mDepth += adjustment;
145  if (mOnlyChild != 0) mOnlyChild->adjustDepth(adjustment);
146  else if (mChildren != 0) {
147  RouteTreeNodeIterator p = mChildren->begin();
148  RouteTreeNodeIterator e = mChildren->end();
149  while (p < e) {
150  (*p)->adjustDepth(adjustment);
151  p++;
152  }
153  }
154  }
155  };
156 
157  #ifdef __GNUC__
158  #pragma pack(pop)
159  #endif
160 
161 } // namespace router
162 } // namespace torc
163 
164 #endif // TORC_ROUTER_ROUTETREENODE_HPP
Encapsulation of an arc between two tilewires.
Definition: Arc.hpp:28
void addChildren(const std::vector< RouteTreeNode * > &newChildren)
Add children to the node.
std::vector< RouteTreeNode * > * mChildren
Pointer to a vector of child nodes that is dynamically allocated.
RouteTreeNode * mOnlyChild
Pointer to a child node that is the only one.
std::vector< RouteTreeNode * >::iterator RouteTreeNodeIterator
Header for the RouteNode class.
const boost::int32_t getDepth() const
Get the node depth.
Definition: RouteNode.hpp:94
RouteTreeNode(Tilewire inSource, Tilewire inSink, boost::int32_t inCost, RouteTreeNode *inParent)
Public Constructor.
Encapsulation of a device tile and wire pair.
Definition: Tilewire.hpp:39
boost::uint16_t getNumChildren()
Get the number of children.
void adjustDepth(int adjustment)
Recursively adjust node depths.
boost::int32_t mDepth
Depth of node from the source.
Definition: RouteNode.hpp:56
An object that holds more complete path information for routing and tracing.
RouteNode * getTop()
Return the top node by tracing parent pointers.
Definition: RouteNode.hpp:100
An object that holds an arc and path information for routing.
Definition: RouteNode.hpp:40
void makeParent(const Tilewire &inSource, const Tilewire &inSink)
Allocate a new node and make it the parent of this node.
RouteTreeNode * getChild(unsigned int index)
Get a child by index, returns 0 for invalid index.
RouteTreeNode(Arc inArc, boost::int32_t inCost, RouteTreeNode *inParent)
Public Constructor.
RouteNode * mParent
Pointer to parent node.
Definition: RouteNode.hpp:58
void normalizeDepth()
Normalize depth of nodes.
RouteTreeNode()
Null Constructor.