QP/C 6.9.1
qpset.h
Go to the documentation of this file.
1 
40 #ifndef QPSET_H
41 #define QPSET_H
42 
43 #ifndef QF_MAX_ACTIVE
44  /* default value when NOT defined */
45  #define QF_MAX_ACTIVE 32U
46 #endif
47 
48 #if (QF_MAX_ACTIVE < 1U) || (64U < QF_MAX_ACTIVE)
49  #error "QF_MAX_ACTIVE out of range. Valid range is 1U..64U"
50 #elif (QF_MAX_ACTIVE <= 8U)
51  typedef uint8_t QPSetBits;
52 #elif (QF_MAX_ACTIVE <= 16U)
53  typedef uint16_t QPSetBits;
54 #else
55 
56  typedef uint32_t QPSetBits;
57 #endif
58 
59 #if (QF_MAX_ACTIVE <= 32U)
60 
61 /****************************************************************************/
68 typedef struct {
69  QPSetBits volatile bits;
70 } QPSet;
71 
73 #define QPSet_setEmpty(me_) ((me_)->bits = 0U)
74 
76 #define QPSet_isEmpty(me_) ((me_)->bits == 0U)
77 
79 #define QPSet_notEmpty(me_) ((me_)->bits != 0U)
80 
82 #define QPSet_hasElement(me_, n_) \
83  (((me_)->bits & ((QPSetBits)1 << ((n_) - 1U))) != 0U)
84 
86 #define QPSet_insert(me_, n_) \
87  ((me_)->bits |= ((QPSetBits)1 << ((n_) - 1U)))
88 
90 #define QPSet_remove(me_, n_) \
91  ((me_)->bits &= (QPSetBits)(~((QPSetBits)1 << ((n_) - 1U))))
92 
96 #define QPSet_findMax(me_, n_) \
97  ((n_) = QF_LOG2((me_)->bits))
98 
99 #else /* QF_MAX_ACTIVE > 32U */
100 
101 /****************************************************************************/
108 typedef struct {
109  uint32_t volatile bits[2];
110 } QPSet;
111 
113 #define QPSet_setEmpty(me_) do { \
114  (me_)->bits[0] = 0U; \
115  (me_)->bits[1] = 0U; \
116 } while (false)
117 
119 /* the following logic avoids UB in volatile access for MISRA compliantce */
120 #define QPSet_isEmpty(me_) \
121  (((me_)->bits[0] == 0U) \
122  ? ((me_)->bits[1] == 0U) \
123  : false)
124 
126 /* the following logic avoids UB in volatile access for MISRA compliantce */
127 #define QPSet_notEmpty(me_) \
128  (((me_)->bits[0] != 0U) \
129  ? true \
130  : ((me_)->bits[1] != 0U))
131 
133 #define QPSet_hasElement(me_, n_) \
134  (((n_) <= 32U) \
135  ? (((me_)->bits[0] & ((uint32_t)1 << ((n_) - 1U ))) != 0U) \
136  : (((me_)->bits[1] & ((uint32_t)1 << ((n_) - 33U))) != 0U))
137 
139 #define QPSet_insert(me_, n_) do { \
140  if ((n_) <= 32U) { \
141  ((me_)->bits[0] |= ((uint32_t)1 << ((n_) - 1U ))); \
142  } \
143  else { \
144  ((me_)->bits[1] |= ((uint32_t)1 << ((n_) - 33U))); \
145  } \
146 } while (false)
147 
149 #define QPSet_remove(me_, n_) do { \
150  if ((n_) <= 32U) { \
151  ((me_)->bits[0] &= (uint32_t)(~((uint32_t)1 << ((n_) - 1U )))); \
152  } \
153  else { \
154  ((me_)->bits[1] &= (uint32_t)(~((uint32_t)1 << ((n_) - 33U)))); \
155  } \
156 } while (false)
157 
161 #define QPSet_findMax(me_, n_) \
162  ((n_) = ((me_)->bits[1] != 0U) \
163  ? (QF_LOG2((me_)->bits[1]) + 32U) \
164  : (QF_LOG2((me_)->bits[0])))
165 
166 #endif /* QF_MAX_ACTIVE */
167 
168 
169 /****************************************************************************/
170 /* Log-base-2 calculations ...*/
171 #ifndef QF_LOG2
173 #endif /* QF_LOG2 */
174 
175 #endif /* QPSET_H */
uint8_t
unsigned char uint8_t
exact-width 8-bit unsigned int
Definition: 16bit/stdint.h:29
QF_LOG2
uint_fast8_t QF_LOG2(QPSetBits x)
Definition: qf_act.c:156
uint16_t
unsigned int uint16_t
exact-width 16-bit unsigned int
Definition: 16bit/stdint.h:30
uint_fast8_t
unsigned int uint_fast8_t
fast at-least 8-bit unsigned int
Definition: 16bit/stdint.h:36
QPSetBits
uint8_t QPSetBits
Definition: qpset.h:51
QPSet
Priority Set of up to 32 elements.
Definition: qpset.h:68
QPSet::bits
QPSetBits volatile bits
bitmask with a bit for each element
Definition: qpset.h:69
uint32_t
unsigned long int uint32_t
exact-width 32-bit unsigned int
Definition: 16bit/stdint.h:31