XWOS API  4.0
XWOS C/C++ API参考手册
载入中...
搜索中...
未找到
Bmp.hxx
浏览该文件的文档.
1
13#ifndef __xwos_cxx_Bmp_hxx__
14#define __xwos_cxx_Bmp_hxx__
15
16extern "C" {
17#include <xwos/lib/xwbop.h>
18#include <string.h>
19}
20
21namespace xwos {
22
30template<xwsz_t TNum>
31class Bmp
32{
33 public:
34 class Bit
35 {
36 private:
39
40 public:
41 explicit Bit(Bmp & bmp, xwsq_t index)
42 : mBmp(&bmp)
43 , mBit(index)
44 {
45 }
46 explicit Bit(Bmp * bmp, xwsq_t index)
47 : mBmp(bmp)
48 , mBit(index)
49 {
50 }
51 explicit Bit(const Bit & other)
52 : mBit(other.mBmp, other.mBit)
53 {
54 }
55 explicit Bit(const Bit && other)
56 : mBit(other.mBmp, other.mBit)
57 {
58 }
60 {
61 }
62 Bit & operator=(bool x)
63 {
64 if (x) {
65 mBmp->set(mBit);
66 } else {
67 mBmp->clear(mBit);
68 }
69 return *this;
70 }
71 Bit & operator=(const Bit & other)
72 {
73 if (this != &other) {
74 mBmp = other.mBmp;
75 mBit = other.mBit;
76 }
77 return *this;
78 }
79 Bit & operator=(const Bit && other)
80 {
81 if (this != &other) {
82 mBmp = other.mBmp;
83 mBit = other.mBit;
84 }
85 return *this;
86 }
88 {
89 mBmp->flip(mBit);
90 return *this;
91 }
92 explicit operator bool()
93 {
94 bool ret;
95 if ((xwsq_t)mBit < TNum) {
96 ret = xwbmpop_t1i(mBmp->mData, mBit);
97 } else {
98 ret = false;
99 }
100 return ret;
101 }
102 void flip() { mBmp->flip(mBit); }
103 };
104
105 public:
107
108 public:
109 Bmp(const Bmp & other)
110 {
111 xwbmpop_assign(mData, other.mData, TNum);
112 }
113 Bmp(const Bmp && other)
114 {
115 xwbmpop_assign(mData, other.mData, TNum);
116 }
118 {
119 xwbmpop_c0all(mData, TNum);
120 }
121 Bmp(const xwu8_t x[], xwsz_t sz)
122 {
123 xwbmpop_c0all(mData, TNum);
124 if (sz < sizeof(mData)) {
125 memcpy(mData, x, sz);
126 } else {
127 memcpy(mData, x, sizeof(mData));
128 }
129 }
130 Bmp(unsigned long long x)
131 {
132 xwbmpop_c0all(mData, TNum);
133 if (sizeof(x) < sizeof(mData)) {
134 memcpy(mData, &x, sizeof(x));
135 } else {
136 memcpy(mData, &x, sizeof(mData));
137 }
138 }
139 Bmp(unsigned long x)
140 {
141 xwbmpop_c0all(mData, TNum);
142 if (sizeof(x) < sizeof(mData)) {
143 memcpy(mData, &x, sizeof(x));
144 } else {
145 memcpy(mData, &x, sizeof(mData));
146 }
147 }
148 Bmp(unsigned int x)
149 {
150 xwbmpop_c0all(mData, TNum);
151 if (sizeof(x) < sizeof(mData)) {
152 memcpy(mData, &x, sizeof(x));
153 } else {
154 memcpy(mData, &x, sizeof(mData));
155 }
156 }
157 Bmp(long long x)
158 {
159 xwbmpop_c0all(mData, TNum);
160 if (sizeof(x) < sizeof(mData)) {
161 memcpy(mData, &x, sizeof(x));
162 } else {
163 memcpy(mData, &x, sizeof(mData));
164 }
165 }
166 Bmp(long x)
167 {
168 xwbmpop_c0all(mData, TNum);
169 if (sizeof(x) < sizeof(mData)) {
170 memcpy(mData, &x, sizeof(x));
171 } else {
172 memcpy(mData, &x, sizeof(mData));
173 }
174 }
175 Bmp(int x)
176 {
177 xwbmpop_c0all(mData, TNum);
178 if (sizeof(x) < sizeof(mData)) {
179 memcpy(mData, &x, sizeof(x));
180 } else {
181 memcpy(mData, &x, sizeof(mData));
182 }
183 }
185 {
186 }
187 void set()
188 {
189 xwbmpop_s1all(mData, TNum);
190 }
191 void set(xwsq_t pos)
192 {
193 if ((xwsq_t)pos < TNum) {
194 xwbmpop_s1i(mData, pos);
195 }
196 }
197 void set(Bmp<TNum> * msk)
198 {
199 xwbmpop_s1m(mData, msk->mData, TNum);
200 }
201 void set(Bmp<TNum> & msk)
202 {
203 xwbmpop_s1m(mData, msk.mData, TNum);
204 }
205 void clear()
206 {
207 xwbmpop_c0all(mData, TNum);
208 }
209 void clear(xwsq_t pos)
210 {
211 if ((xwsq_t)pos < TNum) {
212 xwbmpop_c0i(mData, pos);
213 }
214 }
215 void clear(Bmp<TNum> * msk)
216 {
217 xwbmpop_c0m(mData, msk->mData, TNum);
218 }
219 void clear(Bmp<TNum> & msk)
220 {
221 xwbmpop_c0m(mData, msk.mData, TNum);
222 }
223 void flip()
224 {
225 xwbmpop_not(mData, TNum);
226 }
227 void flip(xwsq_t pos)
228 {
229 if ((xwsq_t)pos < TNum) {
230 xwbmpop_x1i(mData, pos);
231 }
232 }
233 void flip(Bmp<TNum> * msk)
234 {
235 xwbmpop_x1m(mData, msk->mData, TNum);
236 }
237 void flip(Bmp<TNum> & msk)
238 {
239 xwbmpop_x1m(mData, msk.mData, TNum);
240 }
242 {
243 return xwbmpop_ffs(mData, TNum);
244 }
246 {
247 return xwbmpop_fls(mData, TNum);
248 }
250 {
251 return xwbmpop_ffz(mData, TNum);
252 }
254 {
255 return xwbmpop_flz(mData, TNum);
256 }
258 {
259 return xwbmpop_weight(mData, TNum);
260 }
261
262 bool all()
263 {
264 return xwbmpop_t1ma(mData);
265 }
266 bool any()
267 {
268 return xwbmpop_t1mo(mData);
269 }
270 bool none()
271 {
272 return xwbmpop_t0ma(mData);
273 }
274
276 {
277 if (this != &other) {
278 xwbmpop_assign(mData, other.mData, TNum);
279 }
280 return *this;
281 }
282 Bmp<TNum> & operator=(const Bmp<TNum> && other)
283 {
284 if (this != &other) {
285 xwbmpop_assign(mData, other.mData, TNum);
286 }
287 return *this;
288 }
290 {
291 Bmp tmp(*this);
292 xwbmpop_and(tmp.mData, other.mData, TNum);
293 return tmp;
294 }
296 {
297 Bmp tmp(*this);
298 xwbmpop_and(tmp.mData, other.mData, TNum);
299 return tmp;
300 }
302 {
303 Bmp tmp(*this);
304 xwbmpop_or(tmp.mData, other.mData, TNum);
305 return tmp;
306 }
308 {
309 Bmp tmp(*this);
310 xwbmpop_or(tmp.mData, other.mData, TNum);
311 return tmp;
312 }
314 {
315 Bmp tmp(*this);
316 xwbmpop_xor(tmp.mData, other.mData, TNum);
317 return tmp;
318 }
320 {
321 Bmp tmp(*this);
322 xwbmpop_xor(tmp.mData, other.mData, TNum);
323 return tmp;
324 }
326 {
327 xwbmpop_and(mData, other.mData, TNum);
328 return *this;
329 }
331 {
332 xwbmpop_and(mData, other.mData, TNum);
333 return *this;
334 }
336 {
337 xwbmpop_or(mData, other.mData, TNum);
338 return *this;
339 }
341 {
342 xwbmpop_or(mData, other.mData, TNum);
343 return *this;
344 }
346 {
347 xwbmpop_xor(mData, other.mData, TNum);
348 return *this;
349 }
351 {
352 xwbmpop_xor(mData, other.mData, TNum);
353 return *this;
354 }
356 {
357 xwbmpop_not(mData, TNum);
358 return *this;
359 }
360 bool operator==(const Bmp<TNum> & other)
361 {
362 return (0 == xwbmpop_cmp(mData, other.mData, TNum));
363 }
364 bool operator==(const Bmp<TNum> && other)
365 {
366 return (0 == xwbmpop_cmp(mData, other.mData, TNum));
367 }
368 bool operator!=(const Bmp<TNum> & other)
369 {
370 return (0 != xwbmpop_cmp(mData, other.mData, TNum));
371 }
372 bool operator!=(const Bmp<TNum> && other)
373 {
374 return (0 != xwbmpop_cmp(mData, other.mData, TNum));
375 }
376 bool operator>(const Bmp<TNum> & other)
377 {
378 return (xwbmpop_cmp(mData, other.mData, TNum) > 0);
379 }
380 bool operator>(const Bmp<TNum> && other)
381 {
382 return (xwbmpop_cmp(mData, other.mData, TNum) > 0);
383 }
384 bool operator<(const Bmp<TNum> & other)
385 {
386 return (xwbmpop_cmp(mData, other.mData, TNum) < 0);
387 }
388 bool operator<(const Bmp<TNum> && other)
389 {
390 return (xwbmpop_cmp(mData, other.mData, TNum) < 0);
391 }
392 bool operator>=(const Bmp<TNum> & other)
393 {
394 return (xwbmpop_cmp(mData, other.mData, TNum) >= 0);
395 }
396 bool operator>=(const Bmp<TNum> && other)
397 {
398 return (xwbmpop_cmp(mData, other.mData, TNum) >= 0);
399 }
400 bool operator<=(const Bmp<TNum> & other)
401 {
402 return (xwbmpop_cmp(mData, other.mData, TNum) <= 0);
403 }
404 bool operator<=(const Bmp<TNum> && other)
405 {
406 return (xwbmpop_cmp(mData, other.mData, TNum) <= 0);
407 }
408 explicit operator bool()
409 {
410 return any();
411 }
413 {
414 return Bit(this, pos);
415 }
416};
417
422} // namespace xwos
423
424#endif /* xwos/cxx/Bmp.hxx */
Bit(Bmp *bmp, xwsq_t index)
Definition Bmp.hxx:46
Bit & operator~()
Definition Bmp.hxx:87
Bit(const Bit &&other)
Definition Bmp.hxx:55
Bmp * mBmp
Definition Bmp.hxx:37
void flip()
Definition Bmp.hxx:102
Bit & operator=(bool x)
Definition Bmp.hxx:62
Bit(Bmp &bmp, xwsq_t index)
Definition Bmp.hxx:41
Bit & operator=(const Bit &&other)
Definition Bmp.hxx:79
xwsq_t mBit
Definition Bmp.hxx:38
Bit & operator=(const Bit &other)
Definition Bmp.hxx:71
Bit(const Bit &other)
Definition Bmp.hxx:51
bool all()
Definition Bmp.hxx:262
Bmp< TNum > & operator^=(const Bmp< TNum > &&other)
Definition Bmp.hxx:350
xwssq_t ffs()
Definition Bmp.hxx:241
bool operator>(const Bmp< TNum > &other)
Definition Bmp.hxx:376
Bmp(unsigned long long x)
Definition Bmp.hxx:130
bool operator<=(const Bmp< TNum > &&other)
Definition Bmp.hxx:404
Bmp< TNum > operator|(const Bmp< TNum > &other)
Definition Bmp.hxx:301
void clear()
Definition Bmp.hxx:205
xwssq_t ffz()
Definition Bmp.hxx:249
Bmp< TNum > operator^(const Bmp< TNum > &&other)
Definition Bmp.hxx:319
Bmp(unsigned int x)
Definition Bmp.hxx:148
Bmp(long long x)
Definition Bmp.hxx:157
void flip(Bmp< TNum > *msk)
Definition Bmp.hxx:233
bool operator<(const Bmp< TNum > &other)
Definition Bmp.hxx:384
bool operator<(const Bmp< TNum > &&other)
Definition Bmp.hxx:388
Bmp< TNum > & operator^=(const Bmp< TNum > &other)
Definition Bmp.hxx:345
bool operator==(const Bmp< TNum > &other)
Definition Bmp.hxx:360
Bmp< TNum > & operator=(const Bmp< TNum > &other)
Definition Bmp.hxx:275
bool none()
Definition Bmp.hxx:270
Bmp< TNum > & operator&=(const Bmp< TNum > &&other)
Definition Bmp.hxx:330
xwssq_t flz()
Definition Bmp.hxx:253
void set(Bmp< TNum > *msk)
Definition Bmp.hxx:197
xwbmp_t mData[(((TNum)+(8U *sizeof(xwbmp_t)) - 1U)/(8U *sizeof(xwbmp_t)))]
Definition Bmp.hxx:106
Bmp< TNum > operator&(const Bmp< TNum > &other)
Definition Bmp.hxx:289
Bmp< TNum > & operator&=(const Bmp< TNum > &other)
Definition Bmp.hxx:325
void set(xwsq_t pos)
Definition Bmp.hxx:191
bool operator>(const Bmp< TNum > &&other)
Definition Bmp.hxx:380
Bmp< TNum > & operator~()
Definition Bmp.hxx:355
Bmp(const Bmp &other)
Definition Bmp.hxx:109
void flip(Bmp< TNum > &msk)
Definition Bmp.hxx:237
Bmp(unsigned long x)
Definition Bmp.hxx:139
Bmp(const Bmp &&other)
Definition Bmp.hxx:113
Bmp< TNum > & operator=(const Bmp< TNum > &&other)
Definition Bmp.hxx:282
bool operator>=(const Bmp< TNum > &other)
Definition Bmp.hxx:392
void flip()
Definition Bmp.hxx:223
xwssq_t fls()
Definition Bmp.hxx:245
Bmp(const xwu8_t x[], xwsz_t sz)
Definition Bmp.hxx:121
bool operator<=(const Bmp< TNum > &other)
Definition Bmp.hxx:400
Bmp< TNum > & operator|=(const Bmp< TNum > &&other)
Definition Bmp.hxx:340
Bmp< TNum > operator|(const Bmp< TNum > &&other)
Definition Bmp.hxx:307
void clear(Bmp< TNum > &msk)
Definition Bmp.hxx:219
bool operator!=(const Bmp< TNum > &&other)
Definition Bmp.hxx:372
Bmp(int x)
Definition Bmp.hxx:175
void flip(xwsq_t pos)
Definition Bmp.hxx:227
void set()
Definition Bmp.hxx:187
bool operator>=(const Bmp< TNum > &&other)
Definition Bmp.hxx:396
xwsz_t weight()
Definition Bmp.hxx:257
bool operator!=(const Bmp< TNum > &other)
Definition Bmp.hxx:368
Bmp(long x)
Definition Bmp.hxx:166
Bmp< TNum > & operator|=(const Bmp< TNum > &other)
Definition Bmp.hxx:335
void set(Bmp< TNum > &msk)
Definition Bmp.hxx:201
void clear(xwsq_t pos)
Definition Bmp.hxx:209
Bmp< TNum > operator^(const Bmp< TNum > &other)
Definition Bmp.hxx:313
Bit operator[](xwsq_t pos)
Definition Bmp.hxx:412
Bmp< TNum > operator&(const Bmp< TNum > &&other)
Definition Bmp.hxx:295
bool operator==(const Bmp< TNum > &&other)
Definition Bmp.hxx:364
bool any()
Definition Bmp.hxx:266
void clear(Bmp< TNum > *msk)
Definition Bmp.hxx:215
unsigned long xwsz_t
Definition type.h:339
uint8_t xwu8_t
Definition type.h:194
unsigned long xwsq_t
Definition type.h:445
signed long xwssq_t
Definition type.h:461
xwu32_t xwbmp_t
Definition type.h:574
xwssq_t xwbmpop_ffs(xwbmp_t *bmp, xwsz_t num)
在位图中从最低位起查找第一个被置1的位
xwssq_t xwbmpop_fls(xwbmp_t *bmp, xwsz_t num)
在位图中从最高位起查找第一个被置1的位
bool xwbmpop_t0ma(xwbmp_t *bmp, const xwbmp_t msk[], xwsz_t num)
测试位图中掩码部分是否全部为0
Definition xwbop.c:828
bool xwbmpop_t1mo(xwbmp_t *bmp, const xwbmp_t msk[], xwsz_t num)
测试位图中掩码部分是否至少有一位为1
Definition xwbop.c:787
void xwbmpop_xor(xwbmp_t *bmp, const xwbmp_t opd[], xwsz_t num)
将位图与操作数进行逐位“异或”运算
Definition xwbop.c:946
bool xwbmpop_t1i(xwbmp_t *bmp, xwsq_t n)
测试位图中的单个位是否为1
Definition xwbop.c:732
#define BITS_TO_XWBMP_T(n)
Definition xwbop.h:35
void xwbmpop_or(xwbmp_t *bmp, const xwbmp_t opd[], xwsz_t num)
将位图与操作数进行逐位“或”运算
Definition xwbop.c:934
xwssq_t xwbmpop_flz(xwbmp_t *bmp, xwsz_t num)
在位图中从最高位起查找第一个被清0的位
void xwbmpop_c0all(xwbmp_t *bmp, xwsq_t num)
将位图中所有位清0
Definition xwbop.c:658
bool xwbmpop_t1ma(xwbmp_t *bmp, const xwbmp_t msk[], xwsz_t num)
测试位图中掩码部分是否全部为1
Definition xwbop.c:746
void xwbmpop_c0m(xwbmp_t *bmp, const xwbmp_t msk[], xwsz_t num)
将位图中掩码部分清0
Definition xwbop.c:699
void xwbmpop_s1i(xwbmp_t *bmp, xwsq_t n)
将位图中单个位置1
void xwbmpop_x1m(xwbmp_t *bmp, const xwbmp_t msk[], xwsz_t num)
将位图中掩码部分翻转
Definition xwbop.c:720
void xwbmpop_s1all(xwbmp_t *bmp, xwsq_t num)
将位图中所有位置1
Definition xwbop.c:647
xwssq_t xwbmpop_cmp(xwbmp_t *bmp, const xwbmp_t opd[], xwsz_t num)
从数组最高元素开始比较两个位图的数值大小
Definition xwbop.c:625
void xwbmpop_assign(xwbmp_t *bmp, const xwbmp_t opd[], xwsz_t num)
赋值操作数到位图
Definition xwbop.c:614
void xwbmpop_x1i(xwbmp_t *bmp, xwsq_t n)
将位图中单个位翻转
void xwbmpop_not(xwbmp_t *bmp, xwsz_t num)
将位图按位取反
Definition xwbop.c:910
void xwbmpop_c0i(xwbmp_t *bmp, xwsq_t n)
将位图中单个位清0
xwssq_t xwbmpop_ffz(xwbmp_t *bmp, xwsz_t num)
在位图中从最低位起查找第一个被清0的位
xwsz_t xwbmpop_weight(xwbmp_t *bmp, xwsz_t num)
在位图中统计被置1的位的个数
Definition xwbop.c:1068
void xwbmpop_s1m(xwbmp_t *bmp, const xwbmp_t msk[], xwsz_t num)
将位图中掩码部分置1
Definition xwbop.c:678
void xwbmpop_and(xwbmp_t *bmp, const xwbmp_t opd[], xwsz_t num)
将位图与操作数进行逐位“与”运算
Definition xwbop.c:922
Definition Bmp.hxx:21
XWOS通用库:位操作