ae2f_docs
Loading...
Searching...
No Matches
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 "./c90/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 uint_least8_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#endif // !defined(ae2f_Macro_BitVector_h)
uint_least8_t ae2f_BitVecI_t
The pre-defined index type for Bit vector.
Definition BitVec.h:25
#define ae2f_BitVecFilled(len)
Generates the vector filled in 1.
Definition BitVec.h:40
#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_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
#define ae2f_static_cast(t, v)
static_cast for C++
Definition Cast.h:28
#define ae2f_CmpGetGt(a, b)
Definition Cmp.h:63
#define ae2f_CmpGetLs(a, b)
Definition Cmp.h:71
#define ae2f_CmpDiff(a, b)
Definition Cmp.h:77