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
36template<xwsz_t TNum>
37class Bmp
38{
39 public:
40 class Bit
41 {
42 private:
45
46 public:
47 explicit Bit(Bmp & bmp, xwsq_t index)
48 : mBmp(&bmp)
49 , mBit(index)
50 {
51 }
52 explicit Bit(Bmp * bmp, xwsq_t index)
53 : mBmp(bmp)
54 , mBit(index)
55 {
56 }
57 explicit Bit(const Bit & other)
58 : mBit(other.mBmp, other.mBit)
59 {
60 }
61 explicit Bit(const Bit && other)
62 : mBit(other.mBmp, other.mBit)
63 {
64 }
66 {
67 }
68 Bit & operator=(bool x)
69 {
70 if (x) {
71 mBmp->set(mBit);
72 } else {
73 mBmp->clear(mBit);
74 }
75 return *this;
76 }
77 Bit & operator=(const Bit & other)
78 {
79 if (this != &other) {
80 mBmp = other.mBmp;
81 mBit = other.mBit;
82 }
83 return *this;
84 }
85 Bit & operator=(const Bit && other)
86 {
87 if (this != &other) {
88 mBmp = other.mBmp;
89 mBit = other.mBit;
90 }
91 return *this;
92 }
94 {
95 mBmp->flip(mBit);
96 return *this;
97 }
98 explicit operator bool()
99 {
100 bool ret;
101 if ((xwsq_t)mBit < TNum) {
102 ret = xwbmpop_t1i(mBmp->mData, mBit);
103 } else {
104 ret = false;
105 }
106 return ret;
107 }
108 void flip() { mBmp->flip(mBit); }
109 };
110
111 public:
113
114 public:
115 Bmp(const Bmp & other)
116 {
117 xwbmpop_assign(mData, other.mData, TNum);
118 }
119 Bmp(const Bmp && other)
120 {
121 xwbmpop_assign(mData, other.mData, TNum);
122 }
124 {
125 xwbmpop_c0all(mData, TNum);
126 }
128 {
129 xwbmpop_c0all(mData, TNum);
130 if (sz < sizeof(mData)) {
131 memcpy(mData, x, sz);
132 } else {
133 memcpy(mData, x, sizeof(mData));
134 }
135 }
136 Bmp(unsigned long long x)
137 {
138 xwbmpop_c0all(mData, TNum);
139 if (sizeof(x) < sizeof(mData)) {
140 memcpy(mData, &x, sizeof(x));
141 } else {
142 memcpy(mData, &x, sizeof(mData));
143 }
144 }
145 Bmp(unsigned long x)
146 {
147 xwbmpop_c0all(mData, TNum);
148 if (sizeof(x) < sizeof(mData)) {
149 memcpy(mData, &x, sizeof(x));
150 } else {
151 memcpy(mData, &x, sizeof(mData));
152 }
153 }
154 Bmp(unsigned int x)
155 {
156 xwbmpop_c0all(mData, TNum);
157 if (sizeof(x) < sizeof(mData)) {
158 memcpy(mData, &x, sizeof(x));
159 } else {
160 memcpy(mData, &x, sizeof(mData));
161 }
162 }
163 Bmp(long long x)
164 {
165 xwbmpop_c0all(mData, TNum);
166 if (sizeof(x) < sizeof(mData)) {
167 memcpy(mData, &x, sizeof(x));
168 } else {
169 memcpy(mData, &x, sizeof(mData));
170 }
171 }
172 Bmp(long x)
173 {
174 xwbmpop_c0all(mData, TNum);
175 if (sizeof(x) < sizeof(mData)) {
176 memcpy(mData, &x, sizeof(x));
177 } else {
178 memcpy(mData, &x, sizeof(mData));
179 }
180 }
181 Bmp(int x)
182 {
183 xwbmpop_c0all(mData, TNum);
184 if (sizeof(x) < sizeof(mData)) {
185 memcpy(mData, &x, sizeof(x));
186 } else {
187 memcpy(mData, &x, sizeof(mData));
188 }
189 }
191 {
192 }
193 void set()
194 {
195 xwbmpop_s1all(mData, TNum);
196 }
197 void set(int pos)
198 {
199 if ((xwsq_t)pos < TNum) {
200 xwbmpop_s1i(mData, (xwsq_t)pos);
201 }
202 }
203 void set(unsigned int pos)
204 {
205 if ((xwsq_t)pos < TNum) {
206 xwbmpop_s1i(mData, (xwsq_t)pos);
207 }
208 }
209 void set(long pos)
210 {
211 if ((xwsq_t)pos < TNum) {
212 xwbmpop_s1i(mData, (xwsq_t)pos);
213 }
214 }
215 void set(unsigned long pos)
216 {
217 if ((xwsq_t)pos < TNum) {
218 xwbmpop_s1i(mData, (xwsq_t)pos);
219 }
220 }
221 void set(Bmp<TNum> * msk)
222 {
223 xwbmpop_s1m(mData, msk->mData, TNum);
224 }
225 void set(Bmp<TNum> & msk)
226 {
227 xwbmpop_s1m(mData, msk.mData, TNum);
228 }
229 void clear()
230 {
231 xwbmpop_c0all(mData, TNum);
232 }
233 void clear(int pos)
234 {
235 if ((xwsq_t)pos < TNum) {
236 xwbmpop_c0i(mData, (xwsq_t)pos);
237 }
238 }
239 void clear(unsigned int pos)
240 {
241 if ((xwsq_t)pos < TNum) {
242 xwbmpop_c0i(mData, (xwsq_t)pos);
243 }
244 }
245 void clear(long pos)
246 {
247 if ((xwsq_t)pos < TNum) {
248 xwbmpop_c0i(mData, (xwsq_t)pos);
249 }
250 }
251 void clear(unsigned long pos)
252 {
253 if ((xwsq_t)pos < TNum) {
254 xwbmpop_c0i(mData, (xwsq_t)pos);
255 }
256 }
257 void clear(Bmp<TNum> * msk)
258 {
259 xwbmpop_c0m(mData, msk->mData, TNum);
260 }
261 void clear(Bmp<TNum> & msk)
262 {
263 xwbmpop_c0m(mData, msk.mData, TNum);
264 }
265 void flip()
266 {
267 xwbmpop_not(mData, TNum);
268 }
269 void flip(int pos)
270 {
271 if ((xwsq_t)pos < TNum) {
272 xwbmpop_x1i(mData, (xwsq_t)pos);
273 }
274 }
275 void flip(unsigned int pos)
276 {
277 if ((xwsq_t)pos < TNum) {
278 xwbmpop_x1i(mData, (xwsq_t)pos);
279 }
280 }
281 void flip(long pos)
282 {
283 if ((xwsq_t)pos < TNum) {
284 xwbmpop_x1i(mData, (xwsq_t)pos);
285 }
286 }
287 void flip(unsigned long pos)
288 {
289 if ((xwsq_t)pos < TNum) {
290 xwbmpop_x1i(mData, (xwsq_t)pos);
291 }
292 }
293 void flip(Bmp<TNum> * msk)
294 {
295 xwbmpop_x1m(mData, msk->mData, TNum);
296 }
297 void flip(Bmp<TNum> & msk)
298 {
299 xwbmpop_x1m(mData, msk.mData, TNum);
300 }
302 {
303 return xwbmpop_ffs(mData, TNum);
304 }
306 {
307 return xwbmpop_fls(mData, TNum);
308 }
310 {
311 return xwbmpop_ffz(mData, TNum);
312 }
314 {
315 return xwbmpop_flz(mData, TNum);
316 }
318 {
319 return xwbmpop_weight(mData, TNum);
320 }
321
322 bool all()
323 {
324 return xwbmpop_t1ma(mData);
325 }
326 bool any()
327 {
328 return xwbmpop_t1mo(mData);
329 }
330 bool none()
331 {
332 return xwbmpop_t0ma(mData);
333 }
334
336 {
337 if (this != &other) {
338 xwbmpop_assign(mData, other.mData, TNum);
339 }
340 return *this;
341 }
342 Bmp<TNum> & operator=(const Bmp<TNum> && other)
343 {
344 if (this != &other) {
345 xwbmpop_assign(mData, other.mData, TNum);
346 }
347 return *this;
348 }
350 {
351 Bmp tmp(*this);
352 xwbmpop_and(tmp.mData, other.mData, TNum);
353 return tmp;
354 }
356 {
357 Bmp tmp(*this);
358 xwbmpop_and(tmp.mData, other.mData, TNum);
359 return tmp;
360 }
362 {
363 Bmp tmp(*this);
364 xwbmpop_or(tmp.mData, other.mData, TNum);
365 return tmp;
366 }
368 {
369 Bmp tmp(*this);
370 xwbmpop_or(tmp.mData, other.mData, TNum);
371 return tmp;
372 }
374 {
375 Bmp tmp(*this);
376 xwbmpop_xor(tmp.mData, other.mData, TNum);
377 return tmp;
378 }
380 {
381 Bmp tmp(*this);
382 xwbmpop_xor(tmp.mData, other.mData, TNum);
383 return tmp;
384 }
386 {
387 xwbmpop_and(mData, other.mData, TNum);
388 return *this;
389 }
391 {
392 xwbmpop_and(mData, other.mData, TNum);
393 return *this;
394 }
396 {
397 xwbmpop_or(mData, other.mData, TNum);
398 return *this;
399 }
401 {
402 xwbmpop_or(mData, other.mData, TNum);
403 return *this;
404 }
406 {
407 xwbmpop_xor(mData, other.mData, TNum);
408 return *this;
409 }
411 {
412 xwbmpop_xor(mData, other.mData, TNum);
413 return *this;
414 }
416 {
417 xwbmpop_not(mData, TNum);
418 return *this;
419 }
420 bool operator==(const Bmp<TNum> & other)
421 {
422 return (0 == xwbmpop_cmp(mData, other.mData, TNum));
423 }
424 bool operator==(const Bmp<TNum> && other)
425 {
426 return (0 == xwbmpop_cmp(mData, other.mData, TNum));
427 }
428 bool operator!=(const Bmp<TNum> & other)
429 {
430 return (0 != xwbmpop_cmp(mData, other.mData, TNum));
431 }
432 bool operator!=(const Bmp<TNum> && other)
433 {
434 return (0 != xwbmpop_cmp(mData, other.mData, TNum));
435 }
436 bool operator>(const Bmp<TNum> & other)
437 {
438 return (xwbmpop_cmp(mData, other.mData, TNum) > 0);
439 }
440 bool operator>(const Bmp<TNum> && other)
441 {
442 return (xwbmpop_cmp(mData, other.mData, TNum) > 0);
443 }
444 bool operator<(const Bmp<TNum> & other)
445 {
446 return (xwbmpop_cmp(mData, other.mData, TNum) < 0);
447 }
448 bool operator<(const Bmp<TNum> && other)
449 {
450 return (xwbmpop_cmp(mData, other.mData, TNum) < 0);
451 }
452 bool operator>=(const Bmp<TNum> & other)
453 {
454 return (xwbmpop_cmp(mData, other.mData, TNum) >= 0);
455 }
456 bool operator>=(const Bmp<TNum> && other)
457 {
458 return (xwbmpop_cmp(mData, other.mData, TNum) >= 0);
459 }
460 bool operator<=(const Bmp<TNum> & other)
461 {
462 return (xwbmpop_cmp(mData, other.mData, TNum) <= 0);
463 }
464 bool operator<=(const Bmp<TNum> && other)
465 {
466 return (xwbmpop_cmp(mData, other.mData, TNum) <= 0);
467 }
468 explicit operator bool()
469 {
470 return any();
471 }
473 {
474 return Bit(this, pos);
475 }
476};
477
482} // namespace xwos
483
484#endif /* xwos/cxx/Bmp.hxx */
Bit(Bmp *bmp, xwsq_t index)
Definition Bmp.hxx:52
Bit & operator~()
Definition Bmp.hxx:93
Bit(const Bit &&other)
Definition Bmp.hxx:61
Bmp * mBmp
Definition Bmp.hxx:43
void flip()
Definition Bmp.hxx:108
Bit & operator=(bool x)
Definition Bmp.hxx:68
Bit(Bmp &bmp, xwsq_t index)
Definition Bmp.hxx:47
Bit & operator=(const Bit &&other)
Definition Bmp.hxx:85
xwsq_t mBit
Definition Bmp.hxx:44
Bit & operator=(const Bit &other)
Definition Bmp.hxx:77
Bit(const Bit &other)
Definition Bmp.hxx:57
void flip(long pos)
Definition Bmp.hxx:281
bool all()
Definition Bmp.hxx:322
Bmp< TNum > & operator^=(const Bmp< TNum > &&other)
Definition Bmp.hxx:410
xwssq_t ffs()
Definition Bmp.hxx:301
void set(unsigned int pos)
Definition Bmp.hxx:203
bool operator>(const Bmp< TNum > &other)
Definition Bmp.hxx:436
Bmp(unsigned long long x)
Definition Bmp.hxx:136
bool operator<=(const Bmp< TNum > &&other)
Definition Bmp.hxx:464
Bmp< TNum > operator|(const Bmp< TNum > &other)
Definition Bmp.hxx:361
void clear()
Definition Bmp.hxx:229
xwssq_t ffz()
Definition Bmp.hxx:309
Bmp< TNum > operator^(const Bmp< TNum > &&other)
Definition Bmp.hxx:379
void set(long pos)
Definition Bmp.hxx:209
Bmp(unsigned int x)
Definition Bmp.hxx:154
Bmp(long long x)
Definition Bmp.hxx:163
void flip(Bmp< TNum > *msk)
Definition Bmp.hxx:293
bool operator<(const Bmp< TNum > &other)
Definition Bmp.hxx:444
bool operator<(const Bmp< TNum > &&other)
Definition Bmp.hxx:448
Bmp< TNum > & operator^=(const Bmp< TNum > &other)
Definition Bmp.hxx:405
void flip(unsigned int pos)
Definition Bmp.hxx:275
bool operator==(const Bmp< TNum > &other)
Definition Bmp.hxx:420
void set(int pos)
Definition Bmp.hxx:197
Bmp< TNum > & operator=(const Bmp< TNum > &other)
Definition Bmp.hxx:335
void flip(int pos)
Definition Bmp.hxx:269
bool none()
Definition Bmp.hxx:330
Bmp< TNum > & operator&=(const Bmp< TNum > &&other)
Definition Bmp.hxx:390
xwssq_t flz()
Definition Bmp.hxx:313
void set(unsigned long pos)
Definition Bmp.hxx:215
void clear(unsigned long pos)
Definition Bmp.hxx:251
void set(Bmp< TNum > *msk)
Definition Bmp.hxx:221
xwbmp_t mData[(((TNum)+(8U *sizeof(xwbmp_t)) - 1U)/(8U *sizeof(xwbmp_t)))]
Definition Bmp.hxx:112
Bmp< TNum > operator&(const Bmp< TNum > &other)
Definition Bmp.hxx:349
Bmp< TNum > & operator&=(const Bmp< TNum > &other)
Definition Bmp.hxx:385
bool operator>(const Bmp< TNum > &&other)
Definition Bmp.hxx:440
void flip(unsigned long pos)
Definition Bmp.hxx:287
Bmp< TNum > & operator~()
Definition Bmp.hxx:415
Bmp(const Bmp &other)
Definition Bmp.hxx:115
void flip(Bmp< TNum > &msk)
Definition Bmp.hxx:297
Bmp(unsigned long x)
Definition Bmp.hxx:145
Bmp(const Bmp &&other)
Definition Bmp.hxx:119
Bmp< TNum > & operator=(const Bmp< TNum > &&other)
Definition Bmp.hxx:342
void clear(int pos)
Definition Bmp.hxx:233
bool operator>=(const Bmp< TNum > &other)
Definition Bmp.hxx:452
void flip()
Definition Bmp.hxx:265
xwssq_t fls()
Definition Bmp.hxx:305
bool operator<=(const Bmp< TNum > &other)
Definition Bmp.hxx:460
Bmp< TNum > & operator|=(const Bmp< TNum > &&other)
Definition Bmp.hxx:400
Bmp< TNum > operator|(const Bmp< TNum > &&other)
Definition Bmp.hxx:367
void clear(Bmp< TNum > &msk)
Definition Bmp.hxx:261
bool operator!=(const Bmp< TNum > &&other)
Definition Bmp.hxx:432
Bmp(int x)
Definition Bmp.hxx:181
void clear(unsigned int pos)
Definition Bmp.hxx:239
void set()
Definition Bmp.hxx:193
bool operator>=(const Bmp< TNum > &&other)
Definition Bmp.hxx:456
xwsz_t weight()
Definition Bmp.hxx:317
bool operator!=(const Bmp< TNum > &other)
Definition Bmp.hxx:428
Bmp(long x)
Definition Bmp.hxx:172
Bmp(xwu8_t x[], xwsz_t sz)
Definition Bmp.hxx:127
Bmp< TNum > & operator|=(const Bmp< TNum > &other)
Definition Bmp.hxx:395
void set(Bmp< TNum > &msk)
Definition Bmp.hxx:225
Bmp< TNum > operator^(const Bmp< TNum > &other)
Definition Bmp.hxx:373
Bit operator[](xwsq_t pos)
Definition Bmp.hxx:472
Bmp< TNum > operator&(const Bmp< TNum > &&other)
Definition Bmp.hxx:355
bool operator==(const Bmp< TNum > &&other)
Definition Bmp.hxx:424
void clear(long pos)
Definition Bmp.hxx:245
bool any()
Definition Bmp.hxx:326
void clear(Bmp< TNum > *msk)
Definition Bmp.hxx:257
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通用库:位操作