qxLib
priority.h
Go to the documentation of this file.
1 /**
2 
3  @file priority.h
4  @author Khrapov
5  @date 3.12.2023
6  @copyright (c) Nick Khrapov, 2023. All right reserved.
7 
8 **/
9 #pragma once
10 
11 #include <qx/containers/flags.h>
12 #include <qx/hash.h>
13 #include <qx/typedefs.h>
14 
15 #include <atomic>
16 #include <functional>
17 
18 namespace qx
19 {
20 
21 /**
22  @enum priority
23  @brief User may use the predefined values or the custom ones, for ex. "normal - 1",
24  this type is supposed to be compared relatively and not for equality
25  @note The base type of this enum is u8,
26  and lowest will always be the lowest value and highest will be the highest.
27  You should use std::greater<qx::priority> as a predicate in ordered containers to iterate them with decreasing priority.
28 **/
29 enum class priority : u8
30 {
31  lowest = 0,
32  very_low = 32,
33  low = 64,
34  normal = 128,
35  high = 160,
36  very_high = 224,
37  highest = 255,
38 };
39 
40 /**
41 
42  @class time_ordered_priority_key
43  @brief A class that can be used as a key in ordered containers
44  so that items are ordered in descending order of priority but ascending order of creation time.
45  @author Khrapov
46  @date 10.08.2025
47 
48 **/
50 {
51  friend std::hash<time_ordered_priority_key>;
52 
53 public:
54  constexpr time_ordered_priority_key() noexcept = default;
55 
56  /**
57  @brief time_ordered_priority_key object constructor
58  @param ePriority - key priority
59  **/
60  time_ordered_priority_key(priority ePriority) noexcept;
61 
62  /**
63  @brief Get key priority
64  @return - key priority
65  **/
66  priority get_priority() const noexcept;
67 
68  /**
69  @brief Set key priority
70  @param ePriority - key priority
71  **/
72  constexpr void set_priority(priority ePriority) noexcept;
73 
74  constexpr bool operator==(const time_ordered_priority_key&) const noexcept = default;
75  constexpr auto operator<(const time_ordered_priority_key& other) const noexcept;
76 
77 private:
78  static inline std::atomic_size_t s_nIdCounter { 0 };
79  priority m_ePriority = priority::normal;
80  size_t m_nId = 0;
81 };
82 
83 inline time_ordered_priority_key::time_ordered_priority_key(priority ePriority) noexcept
84  : m_ePriority(ePriority)
85  , m_nId(++s_nIdCounter)
86 {
87 }
88 
90 {
91  return m_ePriority;
92 }
93 
94 constexpr void time_ordered_priority_key::set_priority(priority ePriority) noexcept
95 {
96  m_ePriority = ePriority;
97 }
98 
99 constexpr auto time_ordered_priority_key::operator<(const time_ordered_priority_key& other) const noexcept
100 {
101  if (m_ePriority != other.m_ePriority)
102  return m_ePriority > other.m_ePriority;
103  else
104  return m_nId < other.m_nId;
105 }
106 
107 } // namespace qx
108 
109 constexpr auto operator<=>(qx::priority eLeft, qx::priority eRight)
110 {
111  return static_cast<u8>(eLeft) <=> static_cast<u8>(eRight);
112 }
113 
114 template<>
115 struct std::hash<qx::time_ordered_priority_key>
116 {
117  constexpr size_t operator()(const qx::time_ordered_priority_key& timeOrderedPriorityKey) const noexcept
118  {
119  size_t nHash = 0;
120  qx::hash_combine(nHash, timeOrderedPriorityKey.m_nId);
121  qx::hash_combine(nHash, timeOrderedPriorityKey.m_ePriority);
122  return nHash;
123  }
124 };
A class that can be used as a key in ordered containers so that items are ordered in descending order...
Definition: priority.h:50
priority get_priority() const noexcept
Get key priority.
Definition: priority.h:89
constexpr void set_priority(priority ePriority) noexcept
Set key priority.
Definition: priority.h:94
priority
User may use the predefined values or the custom ones, for ex. "normal - 1", this type is supposed to...
Definition: priority.h:30
std::uint8_t u8
0 .. 65 535
Definition: typedefs.h:19