muda
id_with_type.h
1 #pragma once
2 #include <cstdint>
3 #include <ostream>
4 #include <muda/muda_def.h>
5 #undef max
6 namespace muda
7 {
8 template <typename T = uint64_t>
9 class IdWithType
10 {
11  public:
12  using value_type = T;
13  static constexpr auto invalid_id = std::numeric_limits<value_type>::max();
14  MUDA_GENERIC explicit IdWithType(value_type value) noexcept
15  : m_value{value}
16  {
17  }
18  MUDA_GENERIC explicit IdWithType() noexcept
19  : m_value{invalid_id}
20  {
21  }
22  MUDA_GENERIC value_type value() const noexcept { return m_value; }
23  friend std::ostream& operator<<(std::ostream& os, const IdWithType& id)
24  {
25  os << id.m_value;
26  return os;
27  }
28  MUDA_GENERIC friend bool operator==(const IdWithType& lhs, const IdWithType& rhs) noexcept
29  {
30  return lhs.m_value == rhs.m_value;
31  }
32  MUDA_GENERIC friend bool operator!=(const IdWithType& lhs, const IdWithType& rhs) noexcept
33  {
34  return lhs.m_value != rhs.m_value;
35  }
36  MUDA_GENERIC friend bool operator<(const IdWithType& lhs, const IdWithType& rhs) noexcept
37  {
38  return lhs.m_value < rhs.m_value;
39  }
40  MUDA_GENERIC friend bool operator>(const IdWithType& lhs, const IdWithType& rhs) noexcept
41  {
42  return lhs.m_value > rhs.m_value;
43  }
44  MUDA_GENERIC bool is_valid() const noexcept
45  {
46  return m_value != invalid_id;
47  }
48 
49  protected:
50  value_type m_value{invalid_id};
51 };
52 
57 } // namespace muda
58 
59 namespace std
60 {
61 template <typename T>
62 struct hash<muda::IdWithType<T>>
63 {
64  size_t operator()(const muda::IdWithType<T>& s) const noexcept
65  {
66  return std::hash<T>{}(s.value());
67  }
68 };
69 } // namespace std
Definition: id_with_type.h:9
Definition: compute_graph_closure_id.h:11
Definition: assert.h:13