libGimbal 0.1.0
C17-Based Extended Standard Library and Cross-Language Runtime Framework
Loading...
Searching...
No Matches
gimbal_bit_view.h
Go to the documentation of this file.
1/*! \file
2 * \brief GblBitView bitvector view container and related functions
3 * \ingroup utils
4 * \copydoc GblBitView
5 *
6 * \author Falco Girgis
7 */
8
9#ifndef GIMBAL_BIT_VIEW_H
10#define GIMBAL_BIT_VIEW_H
11
13
14#define GBL_BIT_VIEW_BPW (8*sizeof(uint32_t))
15#define GBL_BIT_VIEW_BYTES(bits) ((((bits) + 8 - 1)&(~(8-1)))/8)
16#define GBL_BIT_VIEW_WORDS(bits) ((((bits) + GBL_BIT_VIEW_BPW - 1) & (~(GBL_BIT_VIEW_BPW-1)))/GBL_BIT_VIEW_BPW)
17
18#define GBL_SELF_TYPE GblBitView
19
21
22GBL_FORWARD_DECLARE_STRUCT(GblStringBuffer);
23
24/*! \brief Mutable view to a field of bits within some
25 * block of memory.
26 * \ingroup utils
27 *
28 * GblBitView is a view-based container, which does not
29 * own, but simply references, an existing field of bits
30 * within another buffer. It provides a comparable API
31 * to that of a "BitArray," "BitVector," "BitMap,"
32 * or "BitSet," as they're often known.
33 *
34 * GblBitView allows you to conveniently refer a range
35 * of bits as though they were a regular array, allowing
36 * you to perform operations on them individually.
37 * Certain operations such as count, set, reset, and flip,
38 * are implemented with optimized algorithms that
39 * operate either per-word or per-byte,
40 * rather than requiring iteration over every bit.
41 *
42 * \note Even though GblBitView is a view, which does
43 * not "own" the memory it is backed by, certain
44 * function overloads are able to create alloca()-backed
45 * stack memory on-the-fly to associate with the view,
46 * so you don't have to provide a backing buffer for it.
47 * \sa GblByteArray
48 *
49 * \todo
50 * - finish implementation
51 * - decide on API for bitwise ops against others
52 */
53typedef struct GblBitView {
54 union {
55 void* pData;
56 uint8_t* pBytes;
57 uint32_t* pWords;
58 };
59 size_t length;
60 uint8_t offset;
61} GblBitView;
62
63GBL_EXPORT GblBitView* GblBitView_fromBuffer (GBL_SELF, void* pBuffer, size_t bitCount, size_t offset) GBL_NOEXCEPT;
64GBL_EXPORT GblBitView* GblBitView_fromView (GBL_SELF, const GblBitView* pView, size_t count, size_t offset) GBL_NOEXCEPT;
65
66GBL_EXPORT const char* GblBitView_string (GBL_CSELF, GblStringBuffer* pBuffer, size_t index, size_t count) GBL_NOEXCEPT;
67GBL_EXPORT uint64_t GblBitView_uint64 (GBL_CSELF, size_t index, size_t count) GBL_NOEXCEPT;
68
69GBL_EXPORT GblBitView* GblBitView_setString (GBL_SELF, const char* pString, size_t index, size_t count) GBL_NOEXCEPT;
70GBL_EXPORT GblBitView* GblBitView_setUint64 (GBL_SELF, uint64_t value, size_t index, size_t count) GBL_NOEXCEPT;
71
72GBL_EXPORT size_t GblBitView_bytes (GBL_CSELF) GBL_NOEXCEPT;
73GBL_EXPORT size_t GblBitView_words (GBL_CSELF) GBL_NOEXCEPT;
74
75GBL_EXPORT GblBool GblBitView_at (GBL_CSELF, size_t index) GBL_NOEXCEPT;
76GBL_EXPORT size_t GblBitView_count (GBL_CSELF, size_t index, size_t count) GBL_NOEXCEPT;
77GBL_EXPORT GblBool GblBitView_any (GBL_CSELF, size_t index, size_t count) GBL_NOEXCEPT;
78GBL_EXPORT GblBool GblBitView_all (GBL_CSELF, size_t index, size_t count) GBL_NOEXCEPT;
79GBL_EXPORT GblBool GblBitView_none (GBL_CSELF, size_t index, size_t count) GBL_NOEXCEPT;
80
81GBL_EXPORT GblBitView* GblBitView_assign (GBL_SELF, GblBool value, size_t index, size_t count) GBL_NOEXCEPT;
82GBL_EXPORT GblBitView* GblBitView_set (GBL_SELF, size_t index, size_t count) GBL_NOEXCEPT;
83GBL_EXPORT GblBitView* GblBitView_reset (GBL_SELF, size_t index, size_t count) GBL_NOEXCEPT;
84GBL_EXPORT GblBitView* GblBitView_flip (GBL_SELF, size_t index, size_t count) GBL_NOEXCEPT;
85GBL_EXPORT GblBitView* GblBitView_rotate (GBL_SELF, ptrdiff_t n, size_t offset, size_t count) GBL_NOEXCEPT;
86GBL_EXPORT GblBitView* GblBitView_shiftLeft (GBL_SELF, const GblBitView* pSrc, size_t amount) GBL_NOEXCEPT;
87GBL_EXPORT GblBitView* GblBitView_shiftRight (GBL_SELF, const GblBitView* pSrc, size_t amount) GBL_NOEXCEPT;
88
89/* todo
90GBL_EXPORT GBL_RESULT GblBitView_and (GBL_SELF, const GblBitView* pValue1, const GblBitView* pValue2) GBL_NOEXCEPT;
91GBL_EXPORT GBL_RESULT GblBitView_or (GBL_SELF, const GblBitView* pValue1, const GblBitView* pValue2) GBL_NOEXCEPT;
92GBL_EXPORT GBL_RESULT GblBitView_xor (GBL_SELF, const GblBitView* pValue1, const GblBitView* pValue2) GBL_NOEXCEPT;
93GBL_EXPORT GBL_RESULT GblBitView_andNot (GBL_SELF, const GblBitView* pValue1, const GblBitView* pValue2) GBL_NOEXCEPT;
94
95GBL_EXPORT GblBool GblBitView_eq (GBL_CSELF, const GblBitView* pOther) GBL_NOEXCEPT;
96GBL_EXPORT GblBool GblBitView_lt (GBL_CSELF, const GblBitView* pOther) GBL_NOEXCEPT;
97GBL_EXPORT GblBool GblBitView_leq (GBL_CSELF, const GblBitView* pOther) GBL_NOEXCEPT;
98*/
99
100// ===== Implementation =====
101/// \cond
102#define GblBitView_fromBuffer(...) GblBitView_fromBufferDefault_(__VA_ARGS__)
103#define GblBitView_fromView(...) GblBitView_fromViewDefault_(__VA_ARGS__)
104#define GblBitView_string(...) GblBitView_stringDefault_(__VA_ARGS__)
105#define GblBitView_uint64(...) GblBitView_uint64Default_(__VA_ARGS__)
106#define GblBitView_setString(...) GblBitView_setStringDefault_(__VA_ARGS__)
107#define GblBitView_setUint64(...) GblBitView_setUUint64Default_(__VA_ARGS__)
108#define GblBitView_count(...) GblBitView_countDefault_(__VA_ARGS__)
109#define GblBitView_any(...) GblBitView_anyDefault_(__VA_ARGS__)
110#define GblBitView_all(...) GblBitView_allDefault_(__VA_ARGS__)
111#define GblBitView_none(...) GblBitView_noneDefault_(__VA_ARGS__)
112#define GblBitView_assign(...) GblBitView_assignDefault_(__VA_ARGS__)
113#define GblBitView_set(...) GblBitView_setDefault_(__VA_ARGS__)
114#define GblBitView_reset(...) GblBitView_resetDefault_(__VA_ARGS__)
115#define GblBitView_flip(...) GblBitView_flipDefault_(__VA_ARGS__)
116#define GblBitView_rotate(...) GblBitView_rotateDefault_(__VA_ARGS__)
117
118#define GblBitView_fromBufferDefault_(...)
119 GblBitView_fromBufferDefault__(__VA_ARGS__, 0)
120#define GblBitView_fromBufferDefault__(self, buffer, count, offset, ...)
121 (GblBitView_fromBuffer)(self, buffer, count, offset)
122
123#define GblBitView_fromViewDefault_(...)
124 GblBitView_fromViewDefault__(__VA_ARGS__, GBL_TUPLE_FIRST(__VA_ARGS__)->length, 0)
125#define GblBitView_fromViewDefault__(self, view, count, offset, ...)
126 (GblBitView_fromView)(self, view, count, offset)
127
128#define GblBitView_stringDefault_(...)
129 GblBitView_stringDefault__(__VA_ARGS__, 0, (GBL_TUPLE_FIRST(__VA_ARGS__))->length)
130#define GblBitView_stringDefault__(self, buffer, index, count, ...)
131 (GblBitView_string)(self, buffer, index, count)
132
133#define GblBitView_uint64Default_(...)
134 GblBitView_uint64Default__(__VA_ARGS__, 0, (GBL_TUPLE_FIRST(__VA_ARGS__))->length)
135#define GblBitView_uint64Default__(self, index, count, ...)
136 (GblBitView_uint64)(self, index, count)
137
138#define GblBitView_setStringDefault_(...)
139 GblBitView_setStringDefault__(__VA_ARGS__, 0, 0)
140#define GblBitView_setStringDefault__(self, string, index, count, ...)
141 (GblBitView_fromString)(self, string, index, count)
142
143#define GblBitView_countDefault_(...)
144 GblBitView_countDefault__(__VA_ARGS__, 0, 0)
145#define GblBitView_countDefault__(self, index, count, ...)
146 (GblBitView_count)(self, index, count)
147
148#define GblBitView_anyDefault_(...)
149 GblBitView_anyDefault__(__VA_ARGS__, 0, 0)
150#define GblBitView_anyDefault__(self, index, count, ...)
151 (GblBitView_any)(self, index, count)
152
153#define GblBitView_allDefault_(...)
154 GblBitView_allDefault__(__VA_ARGS__, 0, 0)
155#define GblBitView_allDefault__(self, index, count, ...)
156 (GblBitView_all)(self, index, count)
157
158#define GblBitView_noneDefault_(...)
159 GblBitView_noneDefault__(__VA_ARGS__, 0, 0)
160#define GblBitView_noneDefault__(self, index, count, ...)
161 (GblBitView_none)(self, index, count)
162
163#define GblBitView_assignDefault_(...)
164 GblBitView_assignDefault__(__VA_ARGS__, 0, 0)
165#define GblBitView_assignDefault__(self, value, index, count, ...)
166 (GblBitView_assign)(self, value, index, count)
167
168#define GblBitView_setDefault_(...)
169 GblBitView_setDefault__(__VA_ARGS__, 0, 0)
170#define GblBitView_setDefault__(self, index, count, ...)
171 (GblBitView_set)(self, index, count)
172
173#define GblBitView_resetDefault_(...)
174 GblBitView_resetDefault__(__VA_ARGS__, 0, 0)
175#define GblBitView_resetDefault__(self, index, count, ...)
176 (GblBitView_reset)(self, index, count)
177
178#define GblBitView_flipDefault_(...)
179 GblBitView_filpDefault__(__VA_ARGS__, 0, 0)
180#define GblBitView_flipDefault__(self, index, count, ...)
181 (GblBitView_flip)(self, index, count)
182
183#define GblBitView_rotateDefault_(...)
184 GblBitView_rotateDefault__(__VA_ARGS__, 0, 0)
185#define GblBitView_rotateDefault__(self, offset, index, count, ...)
186 (GblBitView_rotate)(self, offset, index, count)
187//! \endcond
188
190
191#undef GBL_SELF_TYPE
192
193#endif // GIMBAL_BIT_VIEW_H
#define GBL_BIT_VIEW_BPW
#define GBL_NOEXCEPT
#define GBL_DECLS_BEGIN
#define GBL_FORWARD_DECLARE_STRUCT(S)
#define GBL_EXPORT
#define GBL_TUPLE_FIRST(...)
uint8_t GblBool
Basic boolean type, standardized to sizeof(char)
Mutable view to a field of bits within some block of memory.