AGX Dynamics  2.37.3.2
BitArray.h
Go to the documentation of this file.
1 /*
2 Copyright 2007-2024. Algoryx Simulation AB.
3 
4 All AGX source code, intellectual property, documentation, sample code,
5 tutorials, scene files and technical white papers, are copyrighted, proprietary
6 and confidential material of Algoryx Simulation AB. You may not download, read,
7 store, distribute, publish, copy or otherwise disseminate, use or expose this
8 material unless having a written signed agreement with Algoryx Simulation AB, or having been
9 advised so by Algoryx Simulation AB for a time limited evaluation, or having purchased a
10 valid commercial license from Algoryx Simulation AB.
11 
12 Algoryx Simulation AB disclaims all responsibilities for loss or damage caused
13 from using this software, unless otherwise stated in written agreements with
14 Algoryx 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 
39 namespace 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>
85  class BitArray
86  {
87  public:
90  memset( m_data, 0, NUM_BYTES );
91  }
92 
94  ~BitArray() {}
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 
116  class reference
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 
138  AGX_FORCE_INLINE reference& operator=( const bool val ) {
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 
223  AGX_FORCE_INLINE reference operator[]( size_t bit ) {
224  reference result( this, bit );
225  return result;
226  }
227 
228  AGX_FORCE_INLINE const_reference operator[]( size_t bit ) const {
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
size_t size() const
Definition: BitArray.h:216
BitArray(const BitArray &)
Definition: BitArray.h:234
reference & operator=(const reference &other)
assignment operator
Definition: BitArray.h:153
reference(const reference &other)
Copy constructor.
Definition: BitArray.h:147
Class for stack-storage of a (at compile time) specified number of bits with a minimal size of storag...
Definition: BitArray.h:85
const_reference(const const_reference &other)
Copy constructor.
Definition: BitArray.h:192
const_reference(const BitArray *array, size_t index)
Definition: BitArray.h:175
#define AGX_FORCE_INLINE
Definition: Object.h:59
reference operator[](size_t bit)
Definition: BitArray.h:223
Class which is a reference into an existing bitarray.
Definition: BitArray.h:172
BitArray()
Constructor, initializes all bits to zero.
Definition: BitArray.h:89
size_t sizeOf() const
Definition: BitArray.h:219
The agx namespace contains the dynamics/math part of the AGX Dynamics API.
Class which is a reference into an existing bitarray.
Definition: BitArray.h:116
BitArray & operator=(const BitArray &)
Definition: BitArray.h:235
size_t Size
Definition: Integer.h:50
#define CHAR_BIT
Definition: BitArray.h:30
const_reference & operator=(const const_reference &other)
assignment operator
Definition: BitArray.h:198
reference & operator=(const bool val)
Assignment of a bool value into the bitarray index.
Definition: BitArray.h:138
~BitArray()
Destructor.
Definition: BitArray.h:94
reference(BitArray *array, size_t index)
Definition: BitArray.h:119
#define BITARRAY_BIT_INDEX(bit)
Definition: BitArray.h:34
#define agxAssert1(expr, msg)
Definition: debug.h:144
#define BITARRAY_BIT_MASK(bit)
Definition: BitArray.h:37
#define DOXYGEN_END_INTERNAL_BLOCK()
Definition: Object.h:98
const_reference operator[](size_t bit) const
Definition: BitArray.h:228
#define DOXYGEN_START_INTERNAL_BLOCK()
Definition: Object.h:97