Jlm
AnnotationMap.hpp
Go to the documentation of this file.
1 /*
2  * Copyright 2024 Nico Reißmann <nico.reissmann@gmail.com>
3  * See COPYING for terms of redistribution.
4  */
5 
6 #ifndef JLM_UTIL_ANNOTATION_MAP_HPP
7 #define JLM_UTIL_ANNOTATION_MAP_HPP
8 
9 #include <jlm/util/common.hpp>
11 
12 #include <cstdint>
13 #include <string_view>
14 #include <unordered_map>
15 #include <variant>
16 #include <vector>
17 
18 namespace jlm::util
19 {
20 
25 class Annotation final
26 {
27  using AnnotationValue = std::variant<std::string, int64_t, uint64_t, double>;
28 
29 public:
30  Annotation(std::string_view label, std::string value)
31  : Label_(std::move(label)),
32  Value_(std::move(value))
33  {}
34 
35  Annotation(std::string_view label, int64_t value)
36  : Label_(std::move(label)),
37  Value_(std::move(value))
38  {}
39 
40  Annotation(std::string_view label, uint64_t value)
41  : Label_(std::move(label)),
42  Value_(std::move(value))
43  {}
44 
45  Annotation(std::string_view label, double value)
46  : Label_(std::move(label)),
47  Value_(std::move(value))
48  {}
49 
53  [[nodiscard]] const std::string_view &
54  Label() const noexcept
55  {
56  return Label_;
57  }
58 
62  template<typename TValue>
63  [[nodiscard]] const TValue &
64  Value() const
65  {
66  return std::get<TValue>(Value_);
67  }
68 
74  template<typename TValue>
75  [[nodiscard]] bool
76  HasValueType() const noexcept
77  {
78  return std::holds_alternative<TValue>(Value_);
79  }
80 
81  bool
82  operator==(const Annotation & other) const noexcept
83  {
84  return Label_ == other.Label_ && Value_ == other.Value_;
85  }
86 
87  bool
88  operator!=(const Annotation & other) const noexcept
89  {
90  return !(*this == other);
91  }
92 
93 private:
94  std::string_view Label_ = {};
96 };
97 
101 class AnnotationMap final
102 {
103  using AnnotationMapType = std::unordered_map<const void *, std::vector<Annotation>>;
104 
105  class ConstIterator final
106  {
107  public:
108  using iterator_category = std::forward_iterator_tag;
110  using difference_type = std::ptrdiff_t;
111  using pointer = Annotation *;
113 
114  private:
116 
117  explicit ConstIterator(const typename AnnotationMapType::const_iterator & it)
118  : It_(it)
119  {}
120 
121  public:
122  [[nodiscard]] const std::vector<Annotation> &
123  Annotations() const noexcept
124  {
125  return It_.operator->()->second;
126  }
127 
128  const std::vector<Annotation> &
129  operator*() const
130  {
131  return Annotations();
132  }
133 
134  const std::vector<Annotation> *
135  operator->() const
136  {
137  return &Annotations();
138  }
139 
140  ConstIterator &
142  {
143  ++It_;
144  return *this;
145  }
146 
149  {
150  ConstIterator tmp = *this;
151  ++*this;
152  return tmp;
153  }
154 
155  bool
156  operator==(const ConstIterator & other) const
157  {
158  return It_ == other.It_;
159  }
160 
161  bool
162  operator!=(const ConstIterator & other) const
163  {
164  return !operator==(other);
165  }
166 
167  private:
168  typename AnnotationMapType::const_iterator It_ = {};
169  };
170 
172 
173 public:
179  [[nodiscard]] AnnotationRange
180  Annotations() const
181  {
182  return { ConstIterator(Map_.begin()), ConstIterator(Map_.end()) };
183  }
184 
190  [[nodiscard]] bool
191  HasAnnotations(const void * key) const noexcept
192  {
193  return Map_.find(key) != Map_.end();
194  }
195 
201  [[nodiscard]] const std::vector<Annotation> &
202  GetAnnotations(const void * key) const noexcept
203  {
205  return Map_.at(key);
206  }
207 
211  void
212  AddAnnotation(const void * key, Annotation annotation)
213  {
214  Map_[key].emplace_back(std::move(annotation));
215  }
216 
217 private:
219 };
220 
221 }
222 
223 #endif
std::forward_iterator_tag iterator_category
const std::vector< Annotation > & Annotations() const noexcept
bool operator==(const ConstIterator &other) const
ConstIterator(const typename AnnotationMapType::const_iterator &it)
bool operator!=(const ConstIterator &other) const
const std::vector< Annotation > & operator*() const
const std::vector< Annotation > * operator->() const
AnnotationMapType::const_iterator It_
void AddAnnotation(const void *key, Annotation annotation)
const std::vector< Annotation > & GetAnnotations(const void *key) const noexcept
AnnotationRange Annotations() const
std::unordered_map< const void *, std::vector< Annotation > > AnnotationMapType
bool HasAnnotations(const void *key) const noexcept
AnnotationMapType Map_
bool operator!=(const Annotation &other) const noexcept
bool HasValueType() const noexcept
std::variant< std::string, int64_t, uint64_t, double > AnnotationValue
AnnotationValue Value_
const TValue & Value() const
std::string_view Label_
Annotation(std::string_view label, std::string value)
Annotation(std::string_view label, double value)
Annotation(std::string_view label, int64_t value)
const std::string_view & Label() const noexcept
bool operator==(const Annotation &other) const noexcept
Annotation(std::string_view label, uint64_t value)
#define JLM_ASSERT(x)
Definition: common.hpp:16