HPCToolkit
NonUniformDegreeTree.hpp
Go to the documentation of this file.
1 // -*-Mode: C++;-*-
2 
3 // * BeginRiceCopyright *****************************************************
4 //
5 // $HeadURL$
6 // $Id$
7 //
8 // --------------------------------------------------------------------------
9 // Part of HPCToolkit (hpctoolkit.org)
10 //
11 // Information about sources of support for research and development of
12 // HPCToolkit is at 'hpctoolkit.org' and in 'README.Acknowledgments'.
13 // --------------------------------------------------------------------------
14 //
15 // Copyright ((c)) 2002-2019, Rice University
16 // All rights reserved.
17 //
18 // Redistribution and use in source and binary forms, with or without
19 // modification, are permitted provided that the following conditions are
20 // met:
21 //
22 // * Redistributions of source code must retain the above copyright
23 // notice, this list of conditions and the following disclaimer.
24 //
25 // * Redistributions in binary form must reproduce the above copyright
26 // notice, this list of conditions and the following disclaimer in the
27 // documentation and/or other materials provided with the distribution.
28 //
29 // * Neither the name of Rice University (RICE) nor the names of its
30 // contributors may be used to endorse or promote products derived from
31 // this software without specific prior written permission.
32 //
33 // This software is provided by RICE and contributors "as is" and any
34 // express or implied warranties, including, but not limited to, the
35 // implied warranties of merchantability and fitness for a particular
36 // purpose are disclaimed. In no event shall RICE or contributors be
37 // liable for any direct, indirect, incidental, special, exemplary, or
38 // consequential damages (including, but not limited to, procurement of
39 // substitute goods or services; loss of use, data, or profits; or
40 // business interruption) however caused and on any theory of liability,
41 // whether in contract, strict liability, or tort (including negligence
42 // or otherwise) arising in any way out of the use of this software, even
43 // if advised of the possibility of such damage.
44 //
45 // ******************************************************* EndRiceCopyright *
46 
47 //***************************************************************************
48 //
49 // NonUniformDegreeTree.h
50 //
51 // a general purpose abstraction for non-uniform degree trees.
52 // children of a node are represented by a circularly linked-list
53 // of siblings.
54 //
55 // two iterators are included here. one for enumerating the children
56 // of a node, and a second for enumerating a tree rooted at a node.
57 //
58 // these abstractions are useless in their own right since the tree
59 // contains only structural information. to make use of the abstraction,
60 // derive a tree node class that contains some useful data. all of the
61 // structural manipulation can be performed using the functions provided
62 // in the base classes defined here.
63 //
64 // Author: John Mellor-Crummey
65 //
66 // Creation Date: September 1991
67 //
68 // Modification History:
69 // November 1994 -- John Mellor-Crummey
70 // -- reimplemented iterators in terms of StackableIterator and
71 // IteratorStack, increasing functionality while reducing code
72 // volume.
73 //
74 //***************************************************************************
75 
76 #ifndef support_NonUniformDegreeTree_hpp
77 #define support_NonUniformDegreeTree_hpp
78 
79 //************************* System Include Files ****************************
80 
81 #include <iostream>
82 #include <string>
83 
84 //*************************** User Include Files ****************************
85 
86 #include <include/uint.h>
87 
88 #include "IteratorStack.hpp"
89 
90 //*************************** Forward Declarations **************************
91 
92 //***************************************************************************
93 // class NonUniformDegreeTreeNode
94 //***************************************************************************
95 
97 public:
98 
99  //-----------------------------------------------
100  // constructor initializes empty node then links
101  // it to its parent and siblings (if any)
102  //-----------------------------------------------
104  {
105  zeroLinks();
106  link(parent); // link to parent and siblings if any
107  }
108 
110  {
111  *this = other;
112  }
113 
116  {
117  // shallow copy
118  if (&other != this) {
119  m_parent = other.m_parent;
120  m_children = other.m_children;
124  }
125  return *this;
126  }
127 
128  //-----------------------------------------------
129  // virtual destructor that frees all of its
130  // children before freeing itself
131  //-----------------------------------------------
133  {
134  if (m_child_count > 0) {
135  NonUniformDegreeTreeNode *next, *start = m_children;
136  for (int i = m_child_count; i-- > 0; ) {
137  next = start->m_next_sibling;
138  delete start;
139  start = next;
140  }
141  }
142  }
143 
144  // link/unlink a node to a parent and siblings
145  void
147 
148  void
150 
151  void
153 
154  void
155  unlink();
156 
157  // returns the number of ancestors walking up the tree
158  uint
159  ancestorCount() const;
160 
161  // functions for inspecting links to other nodes
162  uint
163  childCount() const
164  { return m_child_count; };
165 
166  bool
167  isLeaf() const
168  { return (m_child_count == 0); }
169 
170 
171  uint
173  { return maxDepth(0); }
174 
175  uint
176  maxDepth(uint parentDepth);
177 
178 public:
179  virtual std::string
180  toString(uint oFlags = 0, const char* pfx = "") const;
181 
182 public:
183  // N.B.: For derived classes, these may get in the way...
185  Parent() const
186  { return m_parent; };
187 
189  NextSibling() const
190  { return m_next_sibling; };
191 
193  PrevSibling() const
194  { return m_prev_sibling; };
195 
197  FirstChild() const
198  { return m_children; };
199 
201  LastChild() const
202  { return m_children ? m_children->m_prev_sibling : 0; };
203 
204 protected:
205  // useful for resetting parent/child/etc links after cloning
206  void
208  {
209  // no parent
210  m_parent = NULL;
211 
212  // no children
213  m_children = NULL;
214  m_child_count = 0;
215 
216  // initial circular list of siblings includes only self
218  }
219 
220 protected:
226 
229 };
230 
231 
232 
233 //***************************************************************************
234 // class NonUniformDegreeTreeNodeChildIterator
235 //***************************************************************************
236 
238 public:
240  bool _forward = true)
241  : m_parent(parent), currentChild(0), forward(_forward)
242  {
243  Reset();
244  }
245 
247  {
248  }
249 
250  void Reset(void)
251  {
252  currentChild = forward ? m_parent->FirstChild() : m_parent->LastChild();
253  }
254 
255  // prefix increment
256  void
258  {
259  if (currentChild) {
260  currentChild = (forward ? currentChild->NextSibling()
261  : currentChild->PrevSibling());
262  const NonUniformDegreeTreeNode* pastEnd =
263  forward ? m_parent->FirstChild() : m_parent->LastChild();
264  if (currentChild == pastEnd) {
265  currentChild = NULL;
266  }
267  }
268  }
269 
270  // postfix increment
271  void
273  {
274  operator++();
275  }
276 
277  virtual NonUniformDegreeTreeNode*
278  Current() const
279  {
280  return currentChild;
281  }
282 
283  virtual void
284  DumpAndReset(std::ostream &os = std::cerr);
285 
286 private:
287  // interface for StackableIterator
288  void*
290  {
291  return Current();
292  }
293 
296  bool forward;
297 };
298 
299 
300 //***************************************************************************
301 // class NonUniformDegreeTreeIterator
302 //***************************************************************************
303 
308 };
309 
310 // Note: Reverse traversal orders are OK.
311 
313 public:
315  (const NonUniformDegreeTreeNode *root, TraversalOrder torder = PreOrder,
317 
319  {
320  }
321 
322 
323  virtual NonUniformDegreeTreeNode*
324  Current() const
325  {
327  }
328 
329  virtual void DumpAndReset(std::ostream &os = std::cerr);
330 
331 private:
332  // upcall interface for StackableIterator
333  void*
335  {
336  return Current();
337  }
338 
339  // upcall for IteratorStack
341  IteratorToPushIfAny(void *current);
342 };
343 
344 #endif /* NonUniformDegreeTree_hpp */
const NonUniformDegreeTreeNode * m_parent
virtual NonUniformDegreeTreeNode * Current() const
NonUniformDegreeTreeNode & operator=(const NonUniformDegreeTreeNode &other)
NonUniformDegreeTreeNode * NextSibling() const
virtual NonUniformDegreeTreeNode * Current() const
NonUniformDegreeTreeNode * FirstChild() const
void linkBefore(NonUniformDegreeTreeNode *sibling)
NonUniformDegreeTreeNode * m_prev_sibling
NonUniformDegreeTreeNode * LastChild() const
NonUniformDegreeTreeNode * PrevSibling() const
NonUniformDegreeTreeNode * m_children
NonUniformDegreeTreeNode(const NonUniformDegreeTreeNode &other)
unsigned int uint
Definition: uint.h:124
NonUniformDegreeTreeNode * m_next_sibling
void link(NonUniformDegreeTreeNode *parent)
void linkAfter(NonUniformDegreeTreeNode *sibling)
NonUniformDegreeTreeNodeChildIterator(const NonUniformDegreeTreeNode *parent, bool _forward=true)
virtual std::string toString(uint oFlags=0, const char *pfx="") const
NonUniformDegreeTreeEnumType
#define NULL
Definition: ElfHelper.cpp:85
NonUniformDegreeTreeNode * Parent() const
NonUniformDegreeTreeNode(NonUniformDegreeTreeNode *parent=0)
void * CurrentUpCall() const
NonUniformDegreeTreeNode * m_parent