HPCToolkit
VMAInterval.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 // 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 #ifndef VMAInterval_hpp
61 #define VMAInterval_hpp
62 
63 //************************* System Include Files ****************************
64 
65 #include <iostream>
66 #include <sstream>
67 
68 #include <set>
69 #include <map>
70 
71 //*************************** User Include Files ****************************
72 
73 #include <include/gcc-attr.h>
74 #include <include/uint.h>
75 
76 #include <lib/isa/ISATypes.hpp>
77 
78 //*************************** Forward Declarations **************************
79 
80 
81 //***************************************************************************
82 // VMAInterval
83 //***************************************************************************
84 
85 // --------------------------------------------------------------------------
86 // VMAInterval: Represents the VMA interval [beg, end)
87 // --------------------------------------------------------------------------
89 {
90 public:
91  // -------------------------------------------------------
92  // constructor/destructor
93  // -------------------------------------------------------
95  : m_beg(beg), m_end(end)
96  { }
97 
98  VMAInterval(const char* formattedstr)
99  { fromString(formattedstr); }
100 
102  : m_beg(x.m_beg), m_end(x.m_end)
103  { }
104 
105  VMAInterval&
107  {
108  if (this != &x) {
109  m_beg = x.m_beg;
110  m_end = x.m_end;
111  }
112  return *this;
113  }
114 
116  { }
117 
118  // -------------------------------------------------------
119  // data
120  // -------------------------------------------------------
121  VMA
122  beg() const
123  { return m_beg; }
124 
125  void
126  beg(VMA x)
127  { m_beg = x; }
128 
129  VMA
130  end() const
131  { return m_end; }
132 
133  void
134  end(VMA x)
135  { m_end = x; }
136 
137  bool
138  empty() const
139  { return m_beg >= m_end; }
140 
141  static bool
143  { return beg >= end; }
144 
145  // -------------------------------------------------------
146  // interval comparison
147  // -------------------------------------------------------
148 
149  // overlaps: does this interval overlap x:
150  // {interval}
151  // a. <x>
152  // b. <x>
153  // c. <x>
154  bool
156  {
157  return (contains(x)
158  || (x.beg() <= beg() && beg() < x.end())
159  || (x.beg() < end() && end() <= x.end()));
160  }
161 
162  // contains: does this interval contain x
163  bool
165  { return ((beg() <= x.beg()) && (end() >= x.end())); }
166 
167  // -------------------------------------------------------
168  // print/read/write
169  // -------------------------------------------------------
170 
171  // Format: "[lb1-ub1)"
172  std::string
173  toString() const;
174 
175  void
176  fromString(const char* formattedstr);
177 
178  void
179  fromString(std::string& formattedstr)
180  { fromString(formattedstr.c_str()); }
181 
182  std::ostream&
183  dump(std::ostream& os) const;
184 
185  std::istream&
186  slurp(std::istream& is);
187 
188  void
189  ddump() const;
190 
191 private:
194 };
195 
196 
197 // --------------------------------------------------------------------------
198 // operator <, lt_VMAInterval: for ordering VMAInterval: Should work
199 // for any kind of interval: [ ], ( ), [ ), ( ]. For example:
200 //
201 // [1,1] < [1,2] --> true
202 // [1,1] < [1,1] --> false
203 // [1,10] < [4,6] --> true
204 // --------------------------------------------------------------------------
205 
206 inline bool
207 operator<(const VMAInterval& x, const VMAInterval& y)
208 {
209  return ((x.beg() < y.beg()) ||
210  ((x.beg() == y.beg()) && (x.end() < y.end())));
211 }
212 
214 public:
215  bool
216  operator() (const VMAInterval& x, const VMAInterval& y) const
217  { return (x < y); }
218 };
219 
220 
221 // --------------------------------------------------------------------------
222 // operators
223 // --------------------------------------------------------------------------
224 
225 inline bool
226 operator==(const VMAInterval& x, const VMAInterval& y)
227 {
228  return ((x.beg() == y.beg()) && (x.end() == y.end()));
229 }
230 
231 inline bool
232 operator!=(const VMAInterval& x, const VMAInterval& y)
233 {
234  return ( !(x == y) );
235 }
236 
237 
238 //***************************************************************************
239 // VMAIntervalSet
240 //***************************************************************************
241 
242 // --------------------------------------------------------------------------
243 // VMAIntervalSet: A set of *non-overlapping* VMAIntervals
244 // --------------------------------------------------------------------------
246  : public std::set<VMAInterval>
247 {
248 public:
250 
251  typedef std::set<key_type> My_t;
252  typedef key_type value_type;
253  typedef My_t::key_compare key_compare;
254  typedef My_t::allocator_type allocator_type;
255  typedef My_t::reference reference;
256  typedef My_t::const_reference const_reference;
257  typedef My_t::iterator iterator;
258  typedef My_t::const_iterator const_iterator;
259  typedef My_t::size_type size_type;
260 
261 public:
262  // -------------------------------------------------------
263  // constructor/destructor
264  // -------------------------------------------------------
266  { }
267 
268  VMAIntervalSet(const char* formattedstr)
269  { fromString(formattedstr); }
270 
271  virtual ~VMAIntervalSet()
272  { }
273 
274  // -------------------------------------------------------
275  // cloning (proscribe by hiding copy constructor and operator=)
276  // -------------------------------------------------------
277 
278  // -------------------------------------------------------
279  // iterator, find/insert, etc
280  // -------------------------------------------------------
281  // use inherited std::set routines
282 
283  // insert: Insert a VMA or VMAInterval and maintain the
284  // non-overlapping interval invariant. Merges intervals that overlap
285  // or intersect only at a boundary.
286  std::pair<iterator, bool>
287  insert(const VMA beg, const VMA end)
288  { return insert(value_type(beg, end)); }
289 
290  std::pair<iterator, bool>
291  insert(const value_type& x);
292 
293  // erase: Erase a VMA or VMAInterval and maintain the
294  // non-overlapping interval invariant
295  void
296  erase(const VMA beg, const VMA end)
297  { return erase(value_type(beg, end)); }
298 
299  void
300  erase(const key_type& x);
301 
302  // find: []
303 
304  // merge: Merge 'x' with this set
305  void
306  merge(const VMAIntervalSet& x);
307 
308  // -------------------------------------------------------
309  // print/read/write
310  // -------------------------------------------------------
311 
312  // Format: space-separated list of intervals: "{[lb1-ub1) [lb2-ub2) ...}"
313  std::string
314  toString() const;
315 
316  void
317  fromString(const char* formattedstr);
318 
319  void
320  fromString(std::string& formattedstr)
321  { fromString(formattedstr.c_str()); }
322 
323  std::ostream&
324  dump(std::ostream& os) const;
325 
326  std::istream&
327  slurp(std::istream& is);
328 
329  void
330  ddump() const;
331 
332 
333 private:
334  VMAIntervalSet(const VMAIntervalSet& x);
335 
338  { return *this; }
339 
340 private:
341 
342 };
343 
344 
345 // --------------------------------------------------------------------------
346 // operator <, lt_VMAIntervalSet: for ordering VMAIntervalSets based
347 // on first entry. For example:
348 //
349 // {} < {} --> false
350 // {} < {[1,2]} --> true
351 // {[1,1]} < {} --> false
352 // {[1,1]} < {[1,2]} --> true
353 // {[1,1]} < {[1,1]} --> false
354 // {[1,10]} < {[4,6]} --> true
355 // --------------------------------------------------------------------------
356 
357 inline bool
359 {
360  if (x.size() == 0 && y.size() >= 1) { // x < y
361  return true;
362  }
363  else if (x.size() >= 1 && y.size() >= 1) {
364  const VMAInterval& xfirst = *(x.begin());
365  const VMAInterval& yfirst = *(y.begin());
366  return (xfirst < yfirst);
367  }
368  else {
369  // (x.size() == 0 && y.size() == 0) || (x.size() >= 1 && y.size() == 0)
370  return false;
371  }
372 }
373 
375 public:
376  bool operator() (const VMAIntervalSet& x, const VMAIntervalSet& y) const {
377  return (x < y);
378  }
379 };
380 
381 
382 
383 //***************************************************************************
384 // VMAIntervalMap
385 //***************************************************************************
386 
387 // --------------------------------------------------------------------------
388 // VMAIntervalMap: maps *non-overlapping* intervals to some type T.
389 // Provides an interface for finding entries based contained intervals.
390 // --------------------------------------------------------------------------
391 
392 template <typename T>
394  : public std::map<VMAInterval, T>
395 {
396 public:
398  typedef T mapped_type;
399 
400  typedef std::map<key_type, T> My_t;
401  typedef std::pair<const key_type, T> value_type;
402  typedef typename My_t::key_compare key_compare;
403  typedef typename My_t::allocator_type allocator_type;
404  typedef typename My_t::reference reference;
405  typedef typename My_t::const_reference const_reference;
406  typedef typename My_t::iterator iterator;
407  typedef typename My_t::const_iterator const_iterator;
408  typedef typename My_t::reverse_iterator reverse_iterator;
409  typedef typename My_t::const_reverse_iterator const_reverse_iterator;
410  typedef typename My_t::size_type size_type;
411 
412 public:
413  // -------------------------------------------------------
414  // constructor/destructor
415  // -------------------------------------------------------
417  { }
418 
419  virtual ~VMAIntervalMap()
420  { }
421 
422  // -------------------------------------------------------
423  // cloning (proscribe by hiding copy constructor and operator=)
424  // -------------------------------------------------------
425 
426  // -------------------------------------------------------
427  // iterator, find/insert, etc
428  // -------------------------------------------------------
429 
430  // element access:
431  //mapped_type& operator[](const key_type& x);
432 
433  // mMap operations:
434 
435  // find: Given a VMAInterval x, find the element mapped to the
436  // interval that equals or contains x.
437  iterator
438  find(const key_type& toFind)
439  {
440  // find lb where lb is the first element !< x;
441  reverse_iterator lb_r(this->lower_bound(toFind));
442  if (lb_r.base() == this->end() && !this->empty()) {
443  lb_r = this->rbegin();
444  }
445  else if (!this->empty()) {
446  lb_r--; // adjust for reverse iterators
447  }
448 
449  // Reverse-search for match
450  for ( ; lb_r != this->rend(); ++lb_r) {
451  const VMAInterval& vmaint = lb_r->first;
452  if (vmaint.contains(toFind)) {
453  return --(lb_r.base()); // adjust for reverse iterators
454  }
455  else if (vmaint < toFind) {
456  break; // !vmaint.contains(toFind) AND (toFind > vmaint)
457  }
458  }
459 
460  return this->end();
461  }
462 
463  const_iterator
464  find(const key_type& x) const
465  { return const_cast<VMAIntervalMap*>(this)->find(x); }
466 
467 
468  // use inherited std::map routines
469 
470  // -------------------------------------------------------
471  // debugging
472  // -------------------------------------------------------
473  std::string
474  toString() const
475  {
476  std::ostringstream os;
477  dump(os);
478  return os.str();
479  }
480 
481  std::ostream&
482  dump(std::ostream& os) const
483  {
484  for (const_iterator it = this->begin(); it != this->end(); ++it) {
485  os << it->first.toString() << " --> " << it->second << std::endl;
486  }
487  return os;
488  }
489 
490  std::ostream&
491  ddump() const
492  {
493  return dump(std::cerr);
494  }
495 
496 private:
497  VMAIntervalMap(const VMAIntervalMap& x);
498 
501  { return *this; }
502 
503 private:
504 
505 };
506 
507 
508 //***************************************************************************
509 
510 #endif
VMAInterval(VMA beg, VMA end)
Definition: VMAInterval.hpp:94
key_type value_type
My_t::size_type size_type
My_t::iterator iterator
My_t::reverse_iterator reverse_iterator
bool overlaps(VMAInterval x) const
std::ostream & dump(std::ostream &os) const
VMAIntervalMap & operator=(const VMAIntervalMap &x)
bfd_vma VMA
Definition: ISATypes.hpp:79
My_t::iterator iterator
My_t::const_iterator const_iterator
VMAInterval(const VMAInterval &x)
int find(char s1[], char s2[])
Definition: CStrUtil.cpp:177
void fromString(std::string &formattedstr)
My_t::allocator_type allocator_type
bool operator==(const VMAInterval &x, const VMAInterval &y)
VMAInterval & operator=(const VMAInterval &x)
My_t::reference reference
VMAInterval(const char *formattedstr)
Definition: VMAInterval.hpp:98
My_t::const_reverse_iterator const_reverse_iterator
void ddump() const
VMA beg() const
bool operator!=(const VMAInterval &x, const VMAInterval &y)
My_t::size_type size_type
void erase(const VMA beg, const VMA end)
VMAInterval key_type
My_t::key_compare key_compare
virtual ~VMAIntervalSet()
My_t::key_compare key_compare
void(* T)(int code, va_list_box *box, int put(int c, void *cl), void *cl, unsigned char flags[256], int width, int precision)
Definition: fmt.h:62
void beg(VMA x)
std::ostream & dump(std::ostream &os) const
bool empty() const
VMAIntervalSet(const char *formattedstr)
std::istream & slurp(std::istream &is)
bool contains(VMAInterval x) const
My_t::const_reference const_reference
VMAIntervalSet & operator=(const VMAIntervalSet &GCC_ATTR_UNUSED x)
std::string toString() const
VMA end() const
std::pair< iterator, bool > insert(const VMA beg, const VMA end)
My_t::const_iterator const_iterator
std::pair< const key_type, T > value_type
std::map< key_type, T > My_t
std::string toString() const
Definition: VMAInterval.cpp:93
void fromString(const char *formattedstr)
iterator find(const key_type &toFind)
std::set< key_type > My_t
My_t::reference reference
void end(VMA x)
My_t::const_reference const_reference
#define GCC_ATTR_UNUSED
Definition: gcc-attr.h:80
bool operator<(const VMAInterval &x, const VMAInterval &y)
std::ostream & ddump() const
static bool empty(VMA beg, VMA end)
void fromString(std::string &formattedstr)
My_t::allocator_type allocator_type
VMAInterval key_type
virtual ~VMAIntervalMap()
const_iterator find(const key_type &x) const