ae2f_docs
BitVec.h
Go to the documentation of this file.
1/**
2 * @file BitVec.h
3 * @author ae2f
4 * @brief
5 * @date 2025-02-01
6 *
7 * @copyright Copyright (c) 2025
8 *
9 */
10
11#if !defined(ae2f_BitVec_h)
12#define ae2f_BitVec_h
13
14#include "./Cast.h"
15#include "./Cmp.h"
16#include <stdint.h>
17
18/** @brief
19 * Calculate proper byte size with bit count
20 * */
21#define ae2f_BitToByteCount(bitc) (((bitc) >> 3) + !!((bitc) & 7))
22
23/// @brief
24/// The pre-defined index type for Bit vector.
25typedef uint8_t ae2f_BitVecI_t;
26
27/// @brief Generates the vector filled in `1`.
28/// @param len The length of the filled vector.
29/// @tparam vec_t The integer data type.
30#define _ae2f_BitVecFilled(len, vec_t)
32 vec_t,
33 (sizeof(vec_t) << 3) == (len)
34 ? ae2f_static_cast(vec_t, -1)
35 : (ae2f_static_cast(vec_t, ae2f_static_cast(vec_t, 1) << (len)) -
36 1))
37
38/// @brief Generates the vector filled in `1`.
39/// @param len {ae2f_Macro_BitVecI_t} The length of the filled vector.
40#define ae2f_BitVecFilled(len) _ae2f_BitVecFilled(len, size_t)
41
42/// @brief
43/// Gets the bits of [vector] between index of [start] and [end].
44/// @param vector {vec_t} The target for operation.
45/// @param start {ae2f_Macro_BitVecI_t} The starting index.
46/// @param end {ae2f_Macro_BitVecI_t} The ending index.
47/// @tparam vec_t The integer data type.
48/// @warning
49/// [start] greater than [end] may cause undefined behaviour.
50#define _ae2f_BitVecGetRanged(vector, start, end, vec_t)
51 (((vector) >> (start)) & _ae2f_BitVecFilled((end) - (start), vec_t))
52
53/// @brief
54/// Gets the bits of [vector] between index of [start] and [end]. \n
55/// It will normalise the values of [start] and [end] by switching them.
56/// @param vector {size_t} The target for operation.
57/// @param start {ae2f_Macro_BitVecI_t} The starting index.
58/// @param end {ae2f_Macro_BitVecI_t} The ending index.
59/// @see _ae2f_Macro_BitVec_GetRanged
60#define ae2f_BitVecGetRanged(vector, start, end)
61 _ae2f_BitVecGetRanged(vector, ae2f_CmpGetLs(start, end),
62 ae2f_CmpGetGt(start, end), size_t)
63
64/// @brief
65/// Gets a bit of [vector] from index of [idx].
66/// @param vector {size_t} The target for operation.
67/// @param idx {ae2f_Macro_BitVecI_t} The wanted index for searching.
68/// @see ae2f_Macro_BitVec_GetRanged
69#define ae2f_BitVecGet(vector, idx) ae2f_BitVecGetRanged(vector, idx, (idx) + 1)
70
71/// @brief
72/// Sets the bits of [vector] from index of [start] and [end] by [val].
73/// @param vector {vec_t} The target for operation.
74/// @param start {ae2f_Macro_BitVecI_t} The starting index.
75/// @param end {ae2f_Macro_BitVecI_t} The ending index.
76/// @param val {vec_t} The value to set.
77/// @tparam vec_t The integer data type.
78/// @warning
79/// [start] greater than [end] may cause undefined behaviour.
80#define _ae2f_BitVecSetRanged(vector, start, end, val, vec_t)
81 ((vector) & (~((_ae2f_BitVecFilled((end) - (start), vec_t)) << start)) |
82 ((val) << start))
83
84/// @brief
85/// Sets the bits of [vector] from index of [start] and [end] by [val]. \n
86/// It will normalise the values of [start] and [end] by switching them.
87/// @param vector {vec_t} The target for operation.
88/// @param start {ae2f_Macro_BitVecI_t} The starting index.
89/// @param end {ae2f_Macro_BitVecI_t} The ending index.
90/// @param val {size_t} The value to set.
91#define ae2f_BitVecSetRanged(vector, start, end, val)
93 vector, ae2f_CmpGetLs(start, end), ae2f_CmpGetGt(start, end),
94 (val) & ae2f_BitVecFilled(ae2f_CmpDiff(start, end)), size_t)
95
96/// @brief
97/// Sets a bit of [vector] from index of [idx] by [val].
98/// @param vector {size_t} The target for operation.
99/// @param idx {bool} The wanted index for searching.
100/// @see ae2f_Macro_BitVec_GetRanged
101#define ae2f_BitVecSet(vector, idx, val)
102 ae2f_BitVecSetRanged(vector, idx, (idx) + 1, val)
103
104/// @brief
105/// Gets the `vec`'s last index where the flag set to 1.
106/// @tparam t Must be an integer type
107/// @param vec {t}
108#define ae2f_BitVecSizeDefName(t) ae2f_BitVecSize_##t##_func
109
110/// @brief
111/// Gets the `vec`'s last index where the flag set to 1.
112/// @tparam t Must be an integer type
113/// @param vec {t}
114#define ae2f_BitVecSizeDef(t)
115 constextendedfun uint8_t ae2f_BitVecSizeDefName(t)(t vec) noexcept {
116 uint8_t i;
117 for (i = (sizeof(t) << 3); i; i--)
118 if (ae2f_BitVecGet(vec, i - 1))
119 break;
120 return i == (sizeof(t) << 3) ? 0 : i;
121 }
122
123/// @brief
124/// Gets the vector that `vec`'s first flag set to 1.
125/// @tparam t Must be an integer type
126/// @param vec {t}
127#define ae2f_BitVecFndOneDefName(t) ae2f_BitVecFndOne_##t##_func
128
129/// @brief
130/// Gets the vector that `vec`'s first flag set to 1.
131/// @tparam t Must be an integer type
132/// @param vec {t}
133#define ae2f_BitVecFndOneDef(t)
134 constextendedfun t ae2f_BitVecFndOneDefName(t)(const t vec) noexcept {
135 t i = 1;
136 for (; i; i <<= 1) {
137 if (vec & i)
138 break;
139 }
140 return i;
141 }
142
143#endif // !defined(ae2f_Macro_BitVector_h)
#define ae2f_BitVecGet(vector, idx)
Gets a bit of [vector] from index of [idx].
Definition BitVec.h:69
#define ae2f_BitVecSizeDef(t)
Gets the vec's last index where the flag set to 1.
Definition BitVec.h:114
#define ae2f_BitVecFilled(len)
Generates the vector filled in 1.
Definition BitVec.h:40
#define ae2f_BitVecFndOneDefName(t)
Gets the vector that vec's first flag set to 1.
Definition BitVec.h:127
#define ae2f_BitVecSet(vector, idx, val)
Sets a bit of [vector] from index of [idx] by [val].
Definition BitVec.h:101
#define ae2f_BitVecGetRanged(vector, start, end)
Gets the bits of [vector] between index of [start] and [end]. It will normalise the values of [start...
Definition BitVec.h:60
#define _ae2f_BitVecFilled(len, vec_t)
Generates the vector filled in 1.
Definition BitVec.h:30
#define ae2f_BitVecFndOneDef(t)
Gets the vector that vec's first flag set to 1.
Definition BitVec.h:133
#define ae2f_BitVecSizeDefName(t)
Gets the vec's last index where the flag set to 1.
Definition BitVec.h:108
#define _ae2f_BitVecSetRanged(vector, start, end, val, vec_t)
Sets the bits of [vector] from index of [start] and [end] by [val].
Definition BitVec.h:80
#define _ae2f_BitVecGetRanged(vector, start, end, vec_t)
Gets the bits of [vector] between index of [start] and [end].
Definition BitVec.h:50
#define ae2f_BitVecSetRanged(vector, start, end, val)
Sets the bits of [vector] from index of [start] and [end] by [val]. It will normalise the values of ...
Definition BitVec.h:91
uint8_t ae2f_BitVecI_t
The pre-defined index type for Bit vector.
Definition BitVec.h:25
#define ae2f_static_cast(t, v)
Definition Cast.h:42
#define ae2f_CmpGetGt(a, b)
Definition Cmp.h:20
#define ae2f_CmpGetLs(a, b)
Definition Cmp.h:26
#define ae2f_CmpDiff(a, b)
Definition Cmp.h:30
#define constexprmethod
Definition Constexpr.hpp:50
#define constextendedmethod
Definition Constexpr.hpp:43
#define constextendedfun
Definition Constexpr.hpp:42
#define __ae2f_stdcheck_CC(v)
Definition LangVer.h:72
int main()
constexprmethod rBitVec< t > SetRangedConst(idx_t start, idx_t end, rBitVec< t > val) const noexcept
Definition BitVec.hpp:80
constexprmethod rBitVec(const t &&obj) noexcept
Definition BitVec.hpp:45
t obj
The actual integer.
Definition BitVec.hpp:39
constextendedmethod const rBitVec< t > FndOne() const noexcept
Definition BitVec.hpp:92
constexprmethod rBitVec< t > GetRangedConst(idx_t start, idx_t end) const noexcept
Definition BitVec.hpp:69
constexprmethod rBitVec< t > SetConst(idx_t i, bool val) const noexcept
Definition BitVec.hpp:77
constexprmethod rBitVec(const rBitVec< T > &&vec) noexcept
Definition BitVec.hpp:56
constexprmethod bool Get(idx_t i) const noexcept
Definition BitVec.hpp:65
constexprmethod rBitVec(const rBitVec< T > &vec) noexcept
Definition BitVec.hpp:50
constexprmethod rBitVec(const t &obj) noexcept
Definition BitVec.hpp:41
constexprmethod rBitVec< t > & Set(idx_t i, bool val) noexcept
Definition BitVec.hpp:72
constexprmethod rBitVec< t > & SetRanged(idx_t start, idx_t end, rBitVec< t > val) noexcept
Definition BitVec.hpp:83
static constexprmethod rBitVec< t > Filled(idx_t length) noexcept
Definition BitVec.hpp:61
constexprmethod const idx_t Size() const noexcept
Definition BitVec.hpp:88
ae2f_BitVecI_t idx_t
Definition BitVec.hpp:35
This namespace contains the class from this library.
Definition BitVec.hpp:23