Voxel
 All Classes Namespaces Files Functions Typedefs Enumerations Enumerator Macros Pages
helpers.h
Go to the documentation of this file.
1 #pragma once
2 
3 #ifndef HELPERS_H
4 #define HELPERS_H
5 
6 
7 /**
8  \file
9  \brief Contains helper macros and functions.
10 */
11 
12 
13 
14 
15 #include <string>
16 #include <cctype>
17 
18 #include <stdint.h>
19 
20 
21 
22 
23 /*==============================================================================
24  ARRAYLENGTH macro
25 ==============================================================================*/
26 
27 
28 /// Calculates the number of elements in the array \p aa by dividing the size of \p aa by the size of each element.
29 #define ARRAYLENGTH( aa ) \
30  ( sizeof( aa ) / sizeof( ( aa )[ 0 ] ) )
31 
32 
33 
34 
35 /*==============================================================================
36  STDCALL macro
37 ==============================================================================*/
38 
39 
40 #if defined( _WIN32 )
41 #define STDCALL __stdcall
42 #else
43 #define STDCALL
44 #endif // _WIN32
45 
46 
47 
48 
49 /*==============================================================================
50  ALIGN macro
51 ==============================================================================*/
52 
53 
54 #if defined( __GNUC__ )
55 #define ALIGN( aa ) __attribute__(( aligned( aa ) ))
56 #elif defined( _WIN32 )
57 #define ALIGN( aa ) __declspec( align( aa ) )
58 #endif // __GNUC__, _WIN32
59 
60 
61 
62 
63 /*==============================================================================
64  Square helper function
65 ==============================================================================*/
66 
67 
68 /// \brief Returns the square of its argument.
69 template< typename t_Type >
70 inline t_Type Square( t_Type const& value ) {
71 
72  return( value * value );
73 }
74 
75 
76 
77 
78 /*==============================================================================
79  Cube helper function
80 ==============================================================================*/
81 
82 
83 /// \brief Returns the cube of its argument.
84 template< typename t_Type >
85 inline t_Type Cube( t_Type const& value ) {
86 
87  return( value * value * value );
88 }
89 
90 
91 
92 
93 /*==============================================================================
94  CollapseWhitespace helper function
95 ==============================================================================*/
96 
97 
98 /**
99  \brief Removes leading/trailing whitespace, and collapses internal whitespace.
100 
101  \param source String in which to collapse whitespace.
102  \param space Character with which to replace internal stretches of whitespace.
103  \return String with leading/trailing whitespace removed, and internal whitespace collapsed.
104 
105  The purpose of this function is to make strings which may contain
106  leading/trailing whitespace, and annoying stretches of internal space
107  (including newlines and tabs), somewhat "cleaner" to print or display in a
108  dialog. In addition to the removal of leading and trailing whitespace, all
109  "stretches" of consecutive whitespace characters (found using \c
110  std::isspace) in the interior of the string will be replaced with (one copy
111  of) the given \p space character.
112 */
113 inline std::string CollapseWhitespace( std::string const& source, char const space = ' ' ) {
114 
115  std::string result;
116 
117  std::string::const_iterator ii = source.begin();
118  std::string::const_iterator iiEnd = source.end();
119 
120  bool first = true;
121  for ( ; ( ii != iiEnd ) && isspace( *ii ); ++ii );
122  while ( ii != iiEnd ) {
123 
124  std::string::const_iterator lower = ii;
125  for ( ; ( ii != iiEnd ) && ( ! isspace( *ii ) ); ++ii );
126 
127  if ( ! first )
128  result.append( 1, space );
129  result.append( lower, ii );
130  first = false;
131 
132  for ( ; ( ii != iiEnd ) && isspace( *ii ); ++ii );
133  }
134 
135  return result;
136 }
137 
138 
139 
140 
141 /*==============================================================================
142  CountBits helper functions
143 ==============================================================================*/
144 
145 
146 /// \brief Returns the number of set bits in an 8-bit integer.
147 inline uint8_t CountBits( uint8_t number ) {
148 
149  static_assert( ( sizeof( number ) == 1 ), "uint8_t should be one byte long" );
150  number = ( number & 0x55 ) + ( ( number >> 1 ) & 0x55 );
151  number = ( number & 0x33 ) + ( ( number >> 2 ) & 0x33 );
152  number = ( number & 0x0f ) + ( number >> 4 );
153  return number;
154 }
155 
156 
157 /// \brief Returns the number of set bits in a 16-bit integer.
158 inline uint16_t CountBits( uint16_t number ) {
159 
160  static_assert( ( sizeof( number ) == 2 ), "uint16_t should be two bytes long" );
161  number = ( number & 0x5555 ) + ( ( number >> 1 ) & 0x5555 );
162  number = ( number & 0x3333 ) + ( ( number >> 2 ) & 0x3333 );
163  number = ( number & 0x0f0f ) + ( ( number >> 4 ) & 0x0f0f );
164  number = ( number & 0x00ff ) + ( number >> 8 );
165  return number;
166 }
167 
168 
169 /// \brief Returns the number of set bits in a 32-bit integer.
170 inline uint32_t CountBits( uint32_t number ) {
171 
172  static_assert( ( sizeof( number ) == 4 ), "uint32_t should be four bytes long" );
173  number = ( number & 0x55555555 ) + ( ( number >> 1 ) & 0x55555555 );
174  number = ( number & 0x33333333 ) + ( ( number >> 2 ) & 0x33333333 );
175  number = ( number & 0x0f0f0f0f ) + ( ( number >> 4 ) & 0x0f0f0f0f );
176  number = ( number & 0x00ff00ff ) + ( ( number >> 8 ) & 0x00ff00ff );
177  number = ( number & 0x0000ffff ) + ( number >> 16 );
178  return number;
179 }
180 
181 
182 /// \brief Returns the number of set bits in a 64-bit integer.
183 inline uint64_t CountBits( uint64_t number ) {
184 
185  static_assert( ( sizeof( number ) == 8 ), "uint64_t should be eight bytes long" );
186  number = ( number & 0x5555555555555555ull ) + ( ( number >> 1 ) & 0x5555555555555555ull );
187  number = ( number & 0x3333333333333333ull ) + ( ( number >> 2 ) & 0x3333333333333333ull );
188  number = ( number & 0x0f0f0f0f0f0f0f0full ) + ( ( number >> 4 ) & 0x0f0f0f0f0f0f0f0full );
189  number = ( number & 0x00ff00ff00ff00ffull ) + ( ( number >> 8 ) & 0x00ff00ff00ff00ffull );
190  number = ( number & 0x0000ffff0000ffffull ) + ( ( number >> 16 ) & 0x0000ffff0000ffffull );
191  number = ( number & 0x00000000ffffffffull ) + ( number >> 32 );
192  return number;
193 }
194 
195 
196 
197 
198 /*==============================================================================
199  CountLowBits helper functions
200 ==============================================================================*/
201 
202 
203 /// \brief Returns the number of bits required to represent the given 8-bit integer. The number of bits required to represent 0 is taken to be 0.
204 inline unsigned int CountLowBits( uint8_t number ) {
205 
206  static_assert( ( sizeof( number ) == 1 ), "uint8_t should be one byte long" );
207 
208  unsigned int bits = 0;
209 
210  if ( number & 0xf0 ) {
211 
212  bits += 4;
213  number >>= 4;
214  }
215 
216  if ( number & 0x0c ) {
217 
218  bits += 2;
219  number >>= 2;
220  }
221 
222  if ( number & 0x02 )
223  bits += 2;
224  else if ( number & 0x01 )
225  ++bits;
226 
227  return bits;
228 }
229 
230 
231 /// \brief Returns the number of bits required to represent the given 16-bit integer. The number of bits required to represent 0 is taken to be 0.
232 inline unsigned int CountLowBits( uint16_t number ) {
233 
234  static_assert( ( sizeof( number ) == 2 ), "uint16_t should be two bytes long" );
235 
236  unsigned int bits = 0;
237 
238  if ( number & 0xff00 ) {
239 
240  bits += 8;
241  number >>= 8;
242  }
243 
244  if ( number & 0x00f0 ) {
245 
246  bits += 4;
247  number >>= 4;
248  }
249 
250  if ( number & 0x000c ) {
251 
252  bits += 2;
253  number >>= 2;
254  }
255 
256  if ( number & 0x0002 )
257  bits += 2;
258  else if ( number & 0x0001 )
259  ++bits;
260 
261  return bits;
262 }
263 
264 
265 /// \brief Returns the number of bits required to represent the given 32-bit integer. The number of bits required to represent 0 is taken to be 0.
266 inline unsigned int CountLowBits( uint32_t number ) {
267 
268  static_assert( ( sizeof( number ) == 4 ), "uint32_t should be four bytes long" );
269 
270  unsigned int bits = 0;
271 
272  if ( number & 0xffff0000 ) {
273 
274  bits += 16;
275  number >>= 16;
276  }
277 
278  if ( number & 0x0000ff00 ) {
279 
280  bits += 8;
281  number >>= 8;
282  }
283 
284  if ( number & 0x000000f0 ) {
285 
286  bits += 4;
287  number >>= 4;
288  }
289 
290  if ( number & 0x0000000c ) {
291 
292  bits += 2;
293  number >>= 2;
294  }
295 
296  if ( number & 0x00000002 )
297  bits += 2;
298  else if ( number & 0x00000001 )
299  ++bits;
300 
301  return bits;
302 }
303 
304 
305 /// \brief Returns the number of bits required to represent the given 64-bit integer. The number of bits required to represent 0 is taken to be 0.
306 inline unsigned int CountLowBits( uint64_t number ) {
307 
308  static_assert( ( sizeof( number ) == 8 ), "uint64_t should be eight bytes long" );
309 
310  unsigned int bits = 0;
311 
312  if ( number & 0xffffffff00000000ull ) {
313 
314  bits += 32;
315  number >>= 32;
316  }
317 
318  if ( number & 0x00000000ffff0000ull ) {
319 
320  bits += 16;
321  number >>= 16;
322  }
323 
324  if ( number & 0x000000000000ff00ull ) {
325 
326  bits += 8;
327  number >>= 8;
328  }
329 
330  if ( number & 0x00000000000000f0ull ) {
331 
332  bits += 4;
333  number >>= 4;
334  }
335 
336  if ( number & 0x000000000000000cull ) {
337 
338  bits += 2;
339  number >>= 2;
340  }
341 
342  if ( number & 0x0000000000000002ull )
343  bits += 2;
344  else if ( number & 0x0000000000000001ull )
345  ++bits;
346 
347  return bits;
348 }
349 
350 
351 
352 
353 #endif // HELPERS_H