AGX Dynamics 2.38.0.2
Loading...
Searching...
No Matches
BitArray.h
Go to the documentation of this file.
1/*
2Copyright 2007-2024. Algoryx Simulation AB.
3
4All AGX source code, intellectual property, documentation, sample code,
5tutorials, scene files and technical white papers, are copyrighted, proprietary
6and confidential material of Algoryx Simulation AB. You may not download, read,
7store, distribute, publish, copy or otherwise disseminate, use or expose this
8material unless having a written signed agreement with Algoryx Simulation AB, or having been
9advised so by Algoryx Simulation AB for a time limited evaluation, or having purchased a
10valid commercial license from Algoryx Simulation AB.
11
12Algoryx Simulation AB disclaims all responsibilities for loss or damage caused
13from using this software, unless otherwise stated in written agreements with
14Algoryx Simulation AB.
15*/
16#ifndef AGX_BITARRAY_H
17#define AGX_BITARRAY_H
18
19#include <agx/macros.h>
20#include <stdlib.h>
21#include <memory.h>
22
23#ifdef _MSC_VER
24# pragma warning(push)
25# pragma warning(disable: 6011) // Disable warningC6011: dereferencing nullptr pointer
26#endif
27
28
29#ifndef CHAR_BIT
30#define CHAR_BIT 8
31#endif
32
33// Calculate index into array for the specified bit
34#define BITARRAY_BIT_INDEX(bit) ((bit) / CHAR_BIT)
35
36/* Calculates a bit mask to mask out in a byte the bit:th bit modulus 8*/
37#define BITARRAY_BIT_MASK(bit) (1 << (bit % CHAR_BIT))
38
39namespace agx
40{
42
43 namespace {
44#ifdef _MSC_VER
45 // MS CL and gcc does not agree upon what is allowed.
46 // Using non literals in const expression is just fone according to MS.
47 template <size_t val>
48 struct BitSize {
49 static const unsigned short result = ( val / 8.0 - ( int )( val / 8.0 ) ) == 0 ? ( int )( val / 8.0 ) : ( int )( val / 8.0 ) + 1;
50 };
51
52#else
53
54#if defined(__INTEL_COMPILER) || defined(__clang__)
55 template <size_t val>
56 struct BitSize {
57 static const unsigned short result = ( val / 8.0 - ( int )( val / 8.0 ) ) == 0 ? ( int )( val / 8.0 ) : ( int )( val / 8.0 ) + 1;
58 };
59
60#else
61
62 // On the other hand, gcc requires that the non-literal in an expression
63 // must be outside of the template. So here we put it into a namespace.
64 // This code generates zero-bytes on a MS compiler...
65 double const BITS=8.0;
66
67 template <size_t val>
68 struct BitSize {
69 static const unsigned short result;
70 };
71
72 template <size_t val>
73 const unsigned short BitSize<val>::result = (unsigned short)(( val / BITS - ( int )( val / BITS ) ) == 0 ? ( int )( val / BITS ) : ( int )( val / BITS ) + 1);
74#endif
75#endif
76 }
77
79
80
84 template <size_t NUM_BITS>
86 {
87 public:
90 memset( m_data, 0, NUM_BYTES );
91 }
92
95
101 AGX_FORCE_INLINE void set( size_t n, bool flag ) {
102 if ( flag )
103 m_data[n / CHAR_BIT] |= (char)(BITARRAY_BIT_MASK( n ));
104 else
105 m_data[n / CHAR_BIT] &= (char)(~BITARRAY_BIT_MASK( n ));
106 }
107
111 AGX_FORCE_INLINE bool get( size_t n ) const {
112 return( ( m_data[BITARRAY_BIT_INDEX( n )] & ( 1 << (n % CHAR_BIT) ) ) != 0 );
113 }
114
117 {
118 public:
119 AGX_FORCE_INLINE reference( BitArray *array, size_t index ) {
120 m_bitArray = array;
121 m_index = index;
122 }
123
128 AGX_FORCE_INLINE operator bool() const {
129 agxAssert1( m_bitArray, "Null reference to bitarray" );
130 agxAssert1( m_bitArray->size() > m_index, "Out of bounds indexing of bitarray" );
131
132 return m_bitArray->get( m_index );
133 }
134
139 agxAssert1( m_bitArray, "Null reference to bitarray" );
140 agxAssert1( m_bitArray->size() > m_index, "Out of bounds indexing of bitarray" );
141
142 m_bitArray->set( m_index, val );
143 return *this;
144 }
145
147 inline reference( const reference& other ) {
148 m_index = other.m_index;
149 m_bitArray = other.m_bitArray;
150 }
151
153 inline reference& operator=( const reference& other ) {
154 if ( this == &other )
155 return *this;
156
157 m_index = other.m_index;
158 m_bitArray = other.m_bitArray;
159
160 return *this;
161 }
162
163 private:
164
165 reference() {}
166
167 BitArray *m_bitArray; /* array index applies to */
168 size_t m_index; /* index of bit in array */
169 };
170
173 {
174 public:
175 AGX_FORCE_INLINE const_reference( const BitArray *array, size_t index ) {
176 m_bitArray = array;
177 m_index = index;
178 }
179
184 AGX_FORCE_INLINE operator bool() const {
185 agxAssert1( m_bitArray, "Null reference to bitarray" );
186 agxAssert1( m_bitArray->size() > m_index, "Out of bounds indexing of bitarray" );
187
188 return m_bitArray->get( m_index );
189 }
190
192 inline const_reference( const const_reference& other ) {
193 m_index = other.m_index;
194 m_bitArray = other.m_bitArray;
195 }
196
198 inline const_reference& operator=( const const_reference& other ) {
199 if ( this == &other )
200 return *this;
201
202 m_index = other.m_index;
203 m_bitArray = other.m_bitArray;
204
205 return *this;
206 }
207
208 private:
209
210 const_reference() {}
211
212 const BitArray *m_bitArray; /* array index applies to */
213 size_t m_index; /* index of bit in array */
214 };
215
216 AGX_FORCE_INLINE size_t size() const {
217 return NUM_BITS;
218 }
219 AGX_FORCE_INLINE size_t sizeOf() const {
220 return NUM_BYTES;
221 }
222
224 reference result( this, bit );
225 return result;
226 }
227
229 const_reference result( this, bit );
230 return result;
231 }
232
233 protected:
234 BitArray( const BitArray& ) {}
236 return *this;
237 }
238
239 private:
240
242
243 enum Size
244 {
245 NUM_BYTES = int(BitSize<NUM_BITS>::result)
246 };
247
249
250 char m_data[NUM_BYTES];
251 };
252
253#ifdef BITARRAY_BIT_MASK
254#undef BITARRAY_BIT_MASK
255#endif
256} // namespace agx
257
258#ifdef _MSC_VER
259# pragma warning(pop)
260#endif
261
262#endif
#define CHAR_BIT
Definition BitArray.h:30
#define BITARRAY_BIT_INDEX(bit)
Definition BitArray.h:34
#define BITARRAY_BIT_MASK(bit)
Definition BitArray.h:37
Class which is a reference into an existing bitarray.
Definition BitArray.h:173
const_reference & operator=(const const_reference &other)
assignment operator
Definition BitArray.h:198
const_reference(const BitArray *array, size_t index)
Definition BitArray.h:175
const_reference(const const_reference &other)
Copy constructor.
Definition BitArray.h:192
Class which is a reference into an existing bitarray.
Definition BitArray.h:117
reference & operator=(const bool val)
Assignment of a bool value into the bitarray index.
Definition BitArray.h:138
reference(BitArray *array, size_t index)
Definition BitArray.h:119
reference(const reference &other)
Copy constructor.
Definition BitArray.h:147
reference & operator=(const reference &other)
assignment operator
Definition BitArray.h:153
Class for stack-storage of a (at compile time) specified number of bits with a minimal size of storag...
Definition BitArray.h:86
BitArray(const BitArray &)
Definition BitArray.h:234
void set(size_t n, bool flag)
Set the n:th bit in the bitarray to flag.
Definition BitArray.h:101
BitArray()
Constructor, initializes all bits to zero.
Definition BitArray.h:89
size_t size() const
Definition BitArray.h:216
bool get(size_t n) const
Definition BitArray.h:111
~BitArray()
Destructor.
Definition BitArray.h:94
reference operator[](size_t bit)
Definition BitArray.h:223
BitArray & operator=(const BitArray &)
Definition BitArray.h:235
size_t sizeOf() const
Definition BitArray.h:219
const_reference operator[](size_t bit) const
Definition BitArray.h:228
#define agxAssert1(expr, msg)
Definition debug.h:144
#define DOXYGEN_END_INTERNAL_BLOCK()
Definition macros.h:97
#define DOXYGEN_START_INTERNAL_BLOCK()
Definition macros.h:96
#define AGX_FORCE_INLINE
Definition macros.h:58
The agx namespace contains the dynamics/math part of the AGX Dynamics API.
size_t Size
Definition Integer.h:42