summaryrefslogtreecommitdiff
path: root/Runtime/Utilities/fixed_bitset.h
diff options
context:
space:
mode:
authorchai <chaifix@163.com>2019-08-14 22:50:43 +0800
committerchai <chaifix@163.com>2019-08-14 22:50:43 +0800
commit15740faf9fe9fe4be08965098bbf2947e096aeeb (patch)
treea730ec236656cc8cab5b13f088adfaed6bb218fb /Runtime/Utilities/fixed_bitset.h
+Unity Runtime codeHEADmaster
Diffstat (limited to 'Runtime/Utilities/fixed_bitset.h')
-rw-r--r--Runtime/Utilities/fixed_bitset.h53
1 files changed, 53 insertions, 0 deletions
diff --git a/Runtime/Utilities/fixed_bitset.h b/Runtime/Utilities/fixed_bitset.h
new file mode 100644
index 0000000..b805ae7
--- /dev/null
+++ b/Runtime/Utilities/fixed_bitset.h
@@ -0,0 +1,53 @@
+#pragma once
+
+#include "Runtime/Utilities/StaticAssert.h"
+
+
+// Fixed size bitset; size (N) must be a multiple of 32.
+// Similar to dynamic_bitset, but does not do dynamic allocations and stuff.
+template<int N>
+class fixed_bitset {
+public:
+ enum { kBlockSize = 32, kBlockCount = N/kBlockSize };
+public:
+ fixed_bitset()
+ {
+ CompileTimeAssert(N % kBlockSize == 0, "size should be multiple fo 32");
+ CompileTimeAssert(sizeof(m_Bits[0])*8 == kBlockSize, "size of internal array type should be 4" );
+ for( int i = 0; i < kBlockCount; ++i )
+ m_Bits[i] = 0;
+ }
+ // note: default copy constructor and assignment operator are ok
+
+ void set( int index ) {
+ AssertIf( index < 0 || index >= N );
+ m_Bits[index/kBlockSize] |= 1 << (index & (kBlockSize-1));
+ }
+ void reset( int index ) {
+ AssertIf( index < 0 || index >= N );
+ m_Bits[index/kBlockSize] &= ~( 1 << (index & (kBlockSize-1)) );
+ }
+ bool test( int index ) const {
+ AssertIf( index < 0 || index >= N );
+ return m_Bits[index/kBlockSize] & ( 1 << (index & (kBlockSize-1)) );
+ }
+ void reset_all() {
+ memset( m_Bits, 0, sizeof(m_Bits) );
+ }
+
+ bool operator==( const fixed_bitset<N>& o ) const {
+ for( int i = 0; i < kBlockCount; ++i )
+ if( m_Bits[i] != o.m_Bits[i] )
+ return false;
+ return true;
+ }
+ bool operator!=( const fixed_bitset<N>& o ) const {
+ for( int i = 0; i < kBlockCount; ++i )
+ if( m_Bits[i] == o.m_Bits[i] )
+ return false;
+ return true;
+ }
+
+private:
+ UInt32 m_Bits[kBlockCount];
+};