HPCToolkit
Struct-TreeIterator.cpp
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 // File:
50 // $HeadURL$
51 //
52 // Purpose:
53 // [The purpose of this file]
54 //
55 // Description:
56 // [The set of functions, macros, etc. defined in the file]
57 //
58 //***************************************************************************
59 
60 //************************* System Include Files ****************************
61 
62 #include <iostream>
63 using std::ostream;
64 using std::endl;
65 
66 
67 //*************************** User Include Files ****************************
68 
69 #include "Struct-Tree.hpp"
70 
72 
73 //*************************** Forward Declarations **************************
74 
75 //***************************************************************************
76 
77 namespace Prof {
78 
79 namespace Struct {
80 
81 
82 //***************************************************************************
83 // ANodeFilter support
84 //***************************************************************************
85 
86 bool
87 HasANodeTy(const ANode& node, long type)
88 {
89  return (type == ANode::TyANY || node.type() == ANode::IntToANodeTy(type));
90 }
91 
92 
116 };
117 
118 
119 //***************************************************************************
120 // ANodeChildIterator
121 //***************************************************************************
122 
123 
124 //***************************************************************************
125 // ANodeIterator
126 //***************************************************************************
127 
128 
129 //***************************************************************************
130 // ANodeSortedIterator
131 //***************************************************************************
132 
136  const ANodeFilter* filterFunc,
137  bool leavesOnly)
138 {
139  ANodeIterator it(node, filterFunc, leavesOnly);
140  for (ANode* cur = NULL; (cur = it.current()); it++) {
141  scopes.Add((unsigned long) cur);
142  }
143  ptrSetIt = new WordSetSortedIterator(&scopes, compare_fn);
144 }
145 
146 
147 void
149 {
150  os << "ANodeSortedIterator: " << endl;
151  while (current()) {
152  os << current()->toString() << endl;
153  (*this)++;
154  }
155  reset();
156 }
157 
158 
159 int
160 ANodeSortedIterator::cmpByName(const void* a, const void* b)
161 {
162  ANode* x = (*(ANode**)a);
163  ANode* y = (*(ANode**)b);
164  return x->name().compare(y->name()); // strcmp(x, y)
165 }
166 
167 
168 int
169 ANodeSortedIterator::cmpByLine(const void* a, const void* b)
170 {
171  // WARNING: this assumes it will only see ACodeNodes!
172  ACodeNode* x = (*(ACodeNode**)a);
173  ACodeNode* y = (*(ACodeNode**)b);
174  return ACodeNode::compare(x, y);
175 }
176 
177 
178 int
179 ANodeSortedIterator::cmpById(const void* a, const void* b)
180 {
181  ANode* x = (*(ANode**)a);
182  ANode* y = (*(ANode**)b);
183  return (x->id() - y->id());
184 }
185 
186 
188 
189 int
190 ANodeSortedIterator::cmpByMetric_fn(const void* a, const void *b)
191 {
192  ANode* x = *(ANode**) a;
193  ANode* y = *(ANode**) b;
194 
195  double vx = x->hasMetric(cmpByMetric_mId) ? x->metric(cmpByMetric_mId) : 0.0;
196  double vy = y->hasMetric(cmpByMetric_mId) ? y->metric(cmpByMetric_mId) : 0.0;
197  double difference = vy - vx;
198 
199  if (difference < 0) return -1;
200  else if (difference > 0) return 1;
201  return 0;
202 }
203 
204 
205 //***************************************************************************
206 // ANodeSortedChildIterator
207 //***************************************************************************
208 
212  const ANodeFilter* f)
213 {
214  ANodeChildIterator it(node, f);
215  for (ANode* cur = NULL; (cur = it.current()); it++) {
216  scopes.Add((unsigned long) cur);
217  }
218  ptrSetIt = new WordSetSortedIterator(&scopes, compare_fn);
219 }
220 
221 
222 void
224 {
225  os << "ANodeSortedChildIterator: " << endl;
226  while (current()) {
227  os << current()->toString() << endl;
228  (*this)++;
229  }
230  reset();
231 }
232 
233 //***************************************************************************
234 
235 } // namespace Struct
236 
237 } // namespace Prof
static const std::string & ANodeTyToName(ANodeTy tp)
static int cmpByMetric_fn(const void *x, const void *y)
static int cmpById(const void *x, const void *y)
static ANodeTy IntToANodeTy(long i)
double metric(size_t mId) const
cct_node_t * node
Definition: cct.c:128
int(* cmp_fptr_t)(const void *x, const void *y)
const ANodeFilter ANodeTyFilter[ANode::TyNUMBER]
static int compare(const ACodeNode *x, const ACodeNode *y)
ANodeSortedChildIterator(const ANode *root, ANodeSortedIterator::cmp_fptr_t compare_fn, const ANodeFilter *filterFunc=NULL)
virtual const std::string & name() const
bool hasMetric(size_t mId) const
static int cmpByLine(const void *x, const void *y)
void Add(unsigned long entry)
Definition: WordSet.cpp:107
ANodeSortedIterator(const ANode *node, cmp_fptr_t compare_fn, const ANodeFilter *filterFunc=NULL, bool leavesOnly=true)
#define NULL
Definition: ElfHelper.cpp:85
static int cmpByName(const void *x, const void *y)
void dumpAndReset(std::ostream &os=std::cerr)
virtual std::string toString(uint oFlags=0, const char *pre="") const
void dumpAndReset(std::ostream &os=std::cerr)
ANodeTy type() const
bool HasANodeTy(const ANode &node, long type)