TNB Library
TnbBitSet.h
[詳解]
1#pragma once
12#include "TnbStr.h"
13#include "TnbComparable.h"
14#include "TnbException.h"
15
16
17
18//TNB Library
19namespace TNB
20{
21
22
23
24//T-TestCaseコードカバレッジDisable
25#pragma comment(user,"T-Coverage Disable")
26
27
28
43class CBitSet : public IComparableT<TNB::CBitSet>
44{
46 size_t m_bitSize;
47 BYTE* m_pBuf;
48
50 size_t m_BufSize(void) const
51 {
52 return (m_bitSize + 7) / 8;
53 }
55 INDEX m_BitNo(INDEX index) const
56 {
57 return index & 7;
58 }
60 INDEX m_ByteNo(INDEX index) const
61 {
62 return index / 8;
63 }
65 bool m_Test(INDEX index) const
66 {
67 return (m_pBuf[m_ByteNo(index)] & _BIT(m_BitNo(index))) != 0;
68 }
70 void m_Set(INDEX index)
71 {
72 m_pBuf[m_ByteNo(index)] |= _BIT(m_BitNo(index));
73 }
75 void m_Reset(INDEX index)
76 {
77 m_pBuf[m_ByteNo(index)] &= ~_BIT(m_BitNo(index));
78 }
80 void m_Fix(INDEX index, bool b)
81 {
82 (b) ? m_Set(index) : m_Reset(index);
83 }
85 void m_Flip(INDEX index)
86 {
87 m_Test(index) ? m_Reset(index) : m_Set(index);
88 }
89
90public:
91
97 explicit CBitSet(size_t bitSize = 32) : _super()
98 {
99 if ( bitSize == 0 )
100 {
101 throw CNotSupportException();
102 }
103 m_bitSize = bitSize;
104 m_pBuf = new BYTE[m_BufSize()];
105 memset(m_pBuf, 0, m_BufSize());
106 }
107
115 explicit CBitSet(LPCTSTR lpszBin) : _super()
116 {
117 m_bitSize = STRLIB::GetLen(lpszBin);
118 if ( m_bitSize == 0 )
119 {
120 throw CNotSupportException();
121 }
122 m_pBuf = new BYTE[m_BufSize()];
123 memset(m_pBuf, 0, m_BufSize());
124 loop ( i, m_bitSize )
125 {
126 if ( lpszBin[i] != '0' )
127 {
128 m_Set(m_bitSize - 1 - i);
129 }
130 }
131 }
132
138 CBitSet(const CBitSet& other) : _super()
139 {
140 m_bitSize = 0;
141 m_pBuf = NULL;
142 operator=(other);
143 }
144
146 virtual ~CBitSet(void)
147 {
148 if ( m_pBuf != NULL )
149 {
150 delete[] m_pBuf;
151 m_pBuf = NULL;
152 }
153 }
154
161 CBitSet& operator=(const CBitSet& other)
162 {
163 ASSERTLIB(other.m_bitSize > 0);
164 if ( m_pBuf != NULL )
165 {
166 delete[] m_pBuf;
167 }
168 m_bitSize = other.m_bitSize;
169 m_pBuf = new BYTE[m_BufSize()];
170 MemCopy(m_pBuf, other.m_pBuf, m_BufSize());
171 return *this;
172 }
173
182 CBitSet& SetData(size_t size, LPCVOID P)
183 {
184 if ( size == 0 || P == NULL )
185 {
186 throw CNotSupportException();
187 }
188 if ( m_pBuf != NULL )
189 {
190 delete[] m_pBuf;
191 }
192 m_bitSize = size * 8;
193 ASSERTLIB(m_BufSize() == size);
194 m_pBuf = new BYTE[size];
195 MemCopy(m_pBuf, static_cast<const BYTE*>(P), size);
196 return *this;
197 }
198
206 CBitSet& SetInteger(ULONGLONG ullValue)
207 {
208 Reset();
209 if ( m_bitSize < sizeof(ULONGLONG) * 8 )
210 {
211 ullValue &= _BIT(m_bitSize) - 1;
212 }
213 size_t s = m_BufSize();
214 if ( s >= 7 ){ m_pBuf[7] = static_cast<BYTE>(ullValue>>56); }
215 if ( s >= 6 ){ m_pBuf[6] = static_cast<BYTE>(ullValue>>48); }
216 if ( s >= 5 ){ m_pBuf[5] = static_cast<BYTE>(ullValue>>40); }
217 if ( s >= 4 ){ m_pBuf[4] = static_cast<BYTE>(ullValue>>32); }
218 if ( s >= 3 ){ m_pBuf[3] = static_cast<BYTE>(ullValue>>24); }
219 if ( s >= 2 ){ m_pBuf[2] = static_cast<BYTE>(ullValue>>16); }
220 if ( s >= 1 ){ m_pBuf[1] = static_cast<BYTE>(ullValue>> 8); }
221 m_pBuf[0] = static_cast<BYTE>(ullValue );
222 return *this;
223 }
224
230 UINT GetUnsignedInt(void) const
231 {
232 UINT r = 0;
233 for ( INDEX i = 0; i < sizeof(UINT) * 8; i++ )
234 {
235 if ( Test(i) )
236 {
237 r |= (1 << i);
238 }
239 }
240 return r;
241 }
242
248 int GetSignedInt(void) const
249 {
250 int r = GetSignedInt();
251 if ( Test(GetSize() - 1) )
252 {
253 //符号拡張の必要あり
254 for ( size_t i = GetSize(); i < sizeof(int) * 8; i++ )
255 {
256 r |= (1 << i);
257 }
258 }
259 return r;
260 }
261
267 ULONGLONG GetUnsignedInt64(void) const
268 {
269 ULONGLONG r = 0;
270 for ( INDEX i = 0; i < sizeof(ULONGLONG) * 8; i++ )
271 {
272 if ( Test(i) )
273 {
274 r |= (1ui64 << i);
275 }
276 }
277 return r;
278 }
279
285 LONGLONG GetSignedInt64(void) const
286 {
287 LONGLONG r = GetUnsignedInt64();
288 if ( Test(GetSize() - 1) )
289 {
290 //符号拡張の必要あり
291 for ( size_t i = GetSize(); i < sizeof(ULONGLONG) * 8; i++ )
292 {
293 r |= (1ui64 << i);
294 }
295 }
296 return r;
297 }
298
305 CStr ToString(void) const
306 {
307 CStr str;
308 LPTSTR P = str.GetBuffer(m_bitSize + 1);
309 for ( INDEX i = m_bitSize - 1; i >= 0; i-- )
310 {
311 *P++ = m_Test(i) ? _T('1') : _T('0');
312 }
313 *P = 0;
314 str.ReleaseBuffer();
315 return str;
316 }
317
323 size_t GetSize(void) const
324 {
325 return m_bitSize;
326 }
327
334 CBitSet& SetSize(size_t size)
335 {
336 if ( GetSize() != size )
337 {
338 CBitSet bs = GetMid(0, size);
339 *this = bs;
340 }
341 return *this;
342 }
343
350 bool Test(INDEX index) const
351 {
352 if ( ! IsInRange(index) )
353 {
354 return false;
355 }
356 return m_Test(index);
357 }
358
364 CBitSet& Set(INDEX index)
365 {
366 if ( IsInRange(index) )
367 {
368 m_Set(index);
369 }
370 return *this;
371 }
372
379 {
380 for ( INDEX i = 0; i < m_bitSize; i++ )
381 {
382 m_Set(i);
383 }
384 return *this;
385 }
386
392 CBitSet& Reset(INDEX index)
393 {
394 if ( IsInRange(index) )
395 {
396 m_Reset(index);
397 }
398 return *this;
399 }
400
407 {
408 memset(m_pBuf, 0, m_BufSize());
409 return *this;
410 }
411
417 CBitSet& Flip(INDEX index)
418 {
419 if ( IsInRange(index) )
420 {
421 m_Flip(index);
422 }
423 return *this;
424 }
425
432 {
433 for ( INDEX i = 0; i<m_bitSize; i++ )
434 {
435 m_Flip(i);
436 }
437 return *this;
438 }
439
447 bool IsInRange(INDEX index) const
448 {
449 return TNB::IsInRange(index, m_bitSize);
450 }
451
459 CBitSet GetMid(INDEX index, size_t bitSize) const
460 {
461 CBitSet bs(bitSize);
462 for ( INDEX i = 0; i < bitSize; i++ )
463 {
464 if ( Test(i + index) )
465 {
466 bs.m_Set(i);
467 }
468 }
469 return bs;
470 }
471
478 {
479 CBitSet bs = *this + t;
480 return *this = bs;
481 }
482
489 {
490 for ( INDEX i = 0; i < m_bitSize; i++ )
491 {
492 if ( ! t.Test(i) )
493 {
494 m_Reset(i);
495 }
496 }
497 return *this;
498 }
499
506 {
507 for ( INDEX i = 0; i < m_bitSize; i++ )
508 {
509 if ( t.Test(i) )
510 {
511 m_Set(i);
512 }
513 }
514 return *this;
515 }
516
523 {
524 for ( INDEX i = 0; i < m_bitSize; i++ )
525 {
526 m_Fix(i, t.Test(i)^m_Test(i));
527 }
528 return *this;
529 }
530
537 CBitSet& operator>>=(DWORD dwPos)
538 {
539 ASSERT0(false, "CBitSet::operator>>=()", "未実装");
540 return *this;
541 }
542
549 CBitSet& operator<<=(DWORD dwPos)
550 {
551 ASSERT0(false, "CBitSet::operator<<=()", "未実装");
552 return *this;
553 }
554
561 {
562 size_t l1 = GetSize();
563 size_t l2 = t.GetSize();
564 CBitSet bs(l1 + l2);
565 MemCopy(bs.m_pBuf, m_pBuf, m_BufSize());
566 for ( INDEX i = 0; i < l2; i++ )
567 {
568 if ( t.Test(i) )
569 {
570 bs.m_Set(l1 + i);
571 }
572 }
573 return bs;
574 }
575
581 CBitSet operator&(const CBitSet& t) const
582 {
583 CBitSet bs(*this);
584 return bs &= t;
585 }
586
592 CBitSet operator|(const CBitSet& t) const
593 {
594 CBitSet bs(*this);
595 return bs |= t;
596 }
597
603 CBitSet operator^(const CBitSet& t) const
604 {
605 CBitSet bs(*this);
606 return bs ^= t;
607 }
608
614 CBitSet operator<<(DWORD dwPos) const
615 {
616 CBitSet bs(*this);
617 return bs <<= dwPos;
618 }
619
625 CBitSet operator>>(DWORD dwPos) const
626 {
627 CBitSet bs(*this);
628 return bs >>= dwPos;
629 }
630
635 CBitSet operator~(void) const
636 {
637 CBitSet bs(*this);
638 return bs.Flip();
639 }
640
648 virtual INT_PTR Compare(const CBitSet& t) const
649 {
650 size_t size = (m_bitSize < t.m_bitSize) ? t.m_bitSize : m_bitSize;
651 size_t l = size - 1;
652 for ( INDEX i = 0; i < size; i++ )
653 {
654 int r = t.Test(l) - Test(l);
655 if ( r != 0 )
656 {
657 return r;
658 }
659 l--;
660 }
661 return 0;
662 }
663
664private:
665 friend class CBitSetTest;
666};
667
668
669
678{
679 _sr << t.ToString();
680 return _sr;
681}
682
690inline const IDeserializer& operator>>(const IDeserializer& ds, CBitSet& _t)
691{
692 CStr s;
693 ds >> s;
694 _t = CBitSet(s);
695 return ds;
696}
697
698
699
700}; // TNB
701
702
703
704//T-TestCaseコードカバレッジEnable
705#pragma comment(user,"T-Coverage Enable")
比較機能関係のヘッダ
#define _BIT(X)
BIT演算
Definition: TnbDef.h:307
#define loop(VAR, CNT)
loop構文.
Definition: TnbDef.h:343
例外状態管理関係のヘッダ
文字列管理関係のヘッダ
Bit管理クラス
Definition: TnbBitSet.h:44
CBitSet operator<<(DWORD dwPos) const
[取得] 右シフト計算結果取得
Definition: TnbBitSet.h:614
CBitSet & SetData(size_t size, LPCVOID P)
[代入] データ代入.
Definition: TnbBitSet.h:182
int GetSignedInt(void) const
[取得] 数値取り出し.
Definition: TnbBitSet.h:248
CBitSet operator~(void) const
[取得] BIT反転情報取得
Definition: TnbBitSet.h:635
CBitSet & Flip(INDEX index)
[設定] BIT反転.
Definition: TnbBitSet.h:417
CBitSet & SetInteger(ULONGLONG ullValue)
[代入] 数値代入.
Definition: TnbBitSet.h:206
CBitSet & Set(void)
[設定] BITセット.
Definition: TnbBitSet.h:378
size_t GetSize(void) const
[取得] BIT数サイズ取得.
Definition: TnbBitSet.h:323
CBitSet GetMid(INDEX index, size_t bitSize) const
[取得] 一部取り出し.
Definition: TnbBitSet.h:459
CBitSet & operator>>=(DWORD dwPos)
[計算] 右シフト計算
Definition: TnbBitSet.h:537
ULONGLONG GetUnsignedInt64(void) const
[取得] 数値取り出し.
Definition: TnbBitSet.h:267
CBitSet(LPCTSTR lpszBin)
コンストラクタ.
Definition: TnbBitSet.h:115
CBitSet operator&(const CBitSet &t) const
[取得] AND計算結果取得
Definition: TnbBitSet.h:581
virtual INT_PTR Compare(const CBitSet &t) const
[確認] 比較
Definition: TnbBitSet.h:648
CBitSet operator+(const CBitSet &t)
[計算] 足し算結果取得
Definition: TnbBitSet.h:560
CBitSet & operator^=(const CBitSet &t)
[計算] XOR計算
Definition: TnbBitSet.h:522
CBitSet operator|(const CBitSet &t) const
[取得] OR計算結果取得
Definition: TnbBitSet.h:592
bool IsInRange(INDEX index) const
[確認] INDEX有効チェック.
Definition: TnbBitSet.h:447
CBitSet & SetSize(size_t size)
[設定] BIT数サイズ設定.
Definition: TnbBitSet.h:334
CBitSet(const CBitSet &other)
コピーコンストラクタ.
Definition: TnbBitSet.h:138
LONGLONG GetSignedInt64(void) const
[取得] 数値取り出し.
Definition: TnbBitSet.h:285
UINT GetUnsignedInt(void) const
[取得] 数値取り出し.
Definition: TnbBitSet.h:230
CBitSet operator^(const CBitSet &t) const
[取得] XOR計算結果取得
Definition: TnbBitSet.h:603
CBitSet & Flip(void)
[設定] BIT反転.
Definition: TnbBitSet.h:431
CBitSet operator>>(DWORD dwPos) const
[取得] 左シフト計算結果取得
Definition: TnbBitSet.h:625
CBitSet & operator+=(const CBitSet &t)
[計算] 足し算
Definition: TnbBitSet.h:477
virtual ~CBitSet(void)
デストラクタ
Definition: TnbBitSet.h:146
CStr ToString(void) const
[取得] 文字列取り出し.
Definition: TnbBitSet.h:305
bool Test(INDEX index) const
[確認] BITの状態.
Definition: TnbBitSet.h:350
CBitSet & Reset(void)
[設定] BITリセット.
Definition: TnbBitSet.h:406
CBitSet & Set(INDEX index)
[設定] BITセット.
Definition: TnbBitSet.h:364
CBitSet & operator|=(const CBitSet &t)
[計算] OR計算
Definition: TnbBitSet.h:505
CBitSet & operator&=(const CBitSet &t)
[計算] AND計算
Definition: TnbBitSet.h:488
CBitSet & operator<<=(DWORD dwPos)
[計算] 左シフト計算
Definition: TnbBitSet.h:549
CBitSet(size_t bitSize=32)
コンストラクタ
Definition: TnbBitSet.h:97
CBitSet & Reset(INDEX index)
[設定] BITリセット.
Definition: TnbBitSet.h:392
CBitSet & operator=(const CBitSet &other)
[代入] コピーオペレータ.
Definition: TnbBitSet.h:161
サポート外例外
Definition: TnbException.h:185
void ReleaseBuffer(void)
[操作] 割り当てたバッファを開放.
Definition: TnbStr.h:954
TYP * GetBuffer(size_t iLength=0)
[操作] 書き込みバッファ要求.
Definition: TnbStr.h:914
size_t GetLen(LPCSTR lpsz)
[計算] 文字列長計算(ASCII/SJIS用)
Definition: TnbStrLib.h:44
bool IsInRange(INDEX value, size_t size)
[確認] 範囲チェック.
Definition: TnbDef.h:421
ISerializer & operator<<(ISerializer &_sr, const CBitSet &t)
[追加] シリアライズ
Definition: TnbBitSet.h:677
const IDeserializer & operator>>(const IDeserializer &ds, CBitSet &_t)
[取得] デシリアライズ
Definition: TnbBitSet.h:690
TNB Library
Definition: TnbDoxyTitle.txt:2
void MemCopy(T *_pDst, const void *pSrc, size_t len)
[複製] メモリコピー
Definition: TnbDef.h:376
比較機能インターフェース.
Definition: TnbComparable.h:54
デシリアライザーインターフェースクラス.
シリアライザーインターフェースクラス.