OpenFPM_pdata  4.1.0
Project that contain the implementation of distributed structures
util.hpp
1 /*
2  * util.hpp
3  *
4  * Created on: May 7, 2015
5  * Author: Pietro Incardona
6  */
7 
8 #include "config.h"
9 
10 #include "util/SimpleRNG.hpp"
11 
12 #ifndef UTIL_HPP_
13 #define UTIL_HPP_
14 
15 #include <boost/iostreams/device/mapped_file.hpp>
16 
17 
26 static inline bool compare(std::string file1, std::string file2)
27 {
28  boost::iostreams::mapped_file_source f1(file1);
29  boost::iostreams::mapped_file_source f2(file2);
30 
31  if( f1.size() == f2.size() && std::equal(f1.data(), f1.data() + f1.size(), f2.data()) )
32  return true;
33  else
34  return false;
35 }
36 
38 struct RGB
39 {
41  float R;
42 
44  float G;
45 
47  float B;
48 
50  std::string toString()
51  {
52  return std::to_string(R) + " " + std::to_string(G) + " " + std::to_string(B);
53  }
54 };
55 
77 static inline struct RGB getColor(int group, SimpleRNG & d)
78 {
79  struct RGB col;
80 
81  float s = (float)d.GetUniform();
82 
83  group = group % 12;
84 
85 #ifdef ON_IO_UNIT_TESTS
86  s = 0.5;
87 #endif
88 
89  if (group == 0)
90  {
91  col.R = s/2 + 0.5;
92  col.G = 0.0;
93  col.B = 0.0;
94  }
95  else if (group == 1)
96  {
97  col.R = 0.0;
98  col.G = s/2 + 0.5;
99  col.B = 0.0;
100  }
101  else if (group == 2)
102  {
103  col.R = 0.0;
104  col.G = 0.0;
105  col.B = s;
106  }
107  else if (group == 3)
108  {
109  col.R = s/2 + 0.5;
110  col.G = s/2 + 0.5;
111  col.B = 0.0;
112  }
113  else if (group == 4)
114  {
115  col.R = s/2 + 0.5;
116  col.G = 0.0;
117  col.B = s/2 + 0.5;
118  }
119  else if (group == 5)
120  {
121  col.R = 0.0;
122  col.G = s/2 + 0.5;
123  col.B = s/2 + 0.5;
124  }
125  else if (group == 6)
126  {
127  col.R = s/2 + 0.5;
128  col.G = s/4 + 0.5;
129  col.B = 0.0;
130  }
131  else if (group == 7)
132  {
133  col.R = s/4 + 0.5;
134  col.G = s/2 + 0.5;
135  col.B = 0.0;
136  }
137  else if (group == 8)
138  {
139  col.R = 0.0;
140  col.G = s/2 + 0.5;
141  col.B = s/4 + 0.5;
142  }
143  else if (group == 9)
144  {
145  col.R = 0.0;
146  col.G = s/4 + 0.5;
147  col.B = s/2 + 0.5;
148  }
149  else if (group == 10)
150  {
151  col.R = s/4 + 0.5;
152  col.G = 0.0;
153  col.B = s/2 + 0.5;
154  }
155  else
156  {
157  col.R = s/2 + 0.5;
158  col.G = 0.0;
159  col.B = s/4 + 0.5;
160  }
161 
162  return col;
163 }
164 
171 static inline bool hasEnding (std::string const &fullString, std::string const &ending)
172 {
173  if (fullString.length() >= ending.length())
174  {return (0 == fullString.compare (fullString.length() - ending.length(), ending.length(), ending));}
175  else
176  {return false;}
177 }
178 
179 static const std::string base64_chars =
180  "ABCDEFGHIJKLMNOPQRSTUVWXYZ"
181  "abcdefghijklmnopqrstuvwxyz"
182  "0123456789+/";
183 
184 
185 static inline bool is_base64(unsigned char c) {
186  return (isalnum(c) || (c == '+') || (c == '/'));
187 }
188 
195 /*std::string base64_encode(unsigned char const* bytes_to_encode, unsigned int in_len) {
196  std::string ret;
197  int i = 0;
198  int j = 0;
199  unsigned char char_array_3[3];
200  unsigned char char_array_4[4];
201 
202  while (in_len--) {
203  char_array_3[i++] = *(bytes_to_encode++);
204  if (i == 3) {
205  char_array_4[0] = (char_array_3[0] & 0xfc) >> 2;
206  char_array_4[1] = ((char_array_3[0] & 0x03) << 4) + ((char_array_3[1] & 0xf0) >> 4);
207  char_array_4[2] = ((char_array_3[1] & 0x0f) << 2) + ((char_array_3[2] & 0xc0) >> 6);
208  char_array_4[3] = char_array_3[2] & 0x3f;
209 
210  for(i = 0; (i <4) ; i++)
211  ret += base64_chars[char_array_4[i]];
212  i = 0;
213  }
214  }
215 
216  if (i)
217  {
218  for(j = i; j < 3; j++)
219  char_array_3[j] = '\0';
220 
221  char_array_4[0] = (char_array_3[0] & 0xfc) >> 2;
222  char_array_4[1] = ((char_array_3[0] & 0x03) << 4) + ((char_array_3[1] & 0xf0) >> 4);
223  char_array_4[2] = ((char_array_3[1] & 0x0f) << 2) + ((char_array_3[2] & 0xc0) >> 6);
224  char_array_4[3] = char_array_3[2] & 0x3f;
225 
226  for (j = 0; (j < i + 1); j++)
227  ret += base64_chars[char_array_4[j]];
228 
229  while((i++ < 3))
230  ret += '=';
231 
232  }
233 
234  return ret;
235 
236 }
237 
238 *//*
243 std::string base64_decode(std::string const& encoded_string) {
244  int in_len = encoded_string.size();
245  int i = 0;
246  int j = 0;
247  int in_ = 0;
248  unsigned char char_array_4[4], char_array_3[3];
249  std::string ret;
250 
251  while (in_len-- && ( encoded_string[in_] != '=') && is_base64(encoded_string[in_])) {
252  char_array_4[i++] = encoded_string[in_]; in_++;
253  if (i ==4) {
254  for (i = 0; i <4; i++)
255  char_array_4[i] = base64_chars.find(char_array_4[i]);
256 
257  char_array_3[0] = (char_array_4[0] << 2) + ((char_array_4[1] & 0x30) >> 4);
258  char_array_3[1] = ((char_array_4[1] & 0xf) << 4) + ((char_array_4[2] & 0x3c) >> 2);
259  char_array_3[2] = ((char_array_4[2] & 0x3) << 6) + char_array_4[3];
260 
261  for (i = 0; (i < 3); i++)
262  ret += char_array_3[i];
263  i = 0;
264  }
265  }
266 
267  if (i) {
268  for (j = i; j <4; j++)
269  char_array_4[j] = 0;
270 
271  for (j = 0; j <4; j++)
272  char_array_4[j] = base64_chars.find(char_array_4[j]);
273 
274  char_array_3[0] = (char_array_4[0] << 2) + ((char_array_4[1] & 0x30) >> 4);
275  char_array_3[1] = ((char_array_4[1] & 0xf) << 4) + ((char_array_4[2] & 0x3c) >> 2);
276  char_array_3[2] = ((char_array_4[2] & 0x3) << 6) + char_array_4[3];
277 
278  for (j = 0; (j < i - 1); j++) ret += char_array_3[j];
279  }
280 
281  return ret;
282 }*/
283 
284 
285 static const unsigned char vtkBase64UtilitiesEncodeTable[65] =
286  "ABCDEFGHIJKLMNOPQRSTUVWXYZ"
287  "abcdefghijklmnopqrstuvwxyz"
288  "0123456789+/";
289 
290 //----------------------------------------------------------------------------
291 inline static unsigned char vtkBase64UtilitiesEncodeChar(unsigned char c)
292 {
293  assert( c < 65 );
294  return vtkBase64UtilitiesEncodeTable[c];
295 }
296 
297 //----------------------------------------------------------------------------
298 static void EncodeTriplet(unsigned char i0,
299  unsigned char i1,
300  unsigned char i2,
301  unsigned char *o0,
302  unsigned char *o1,
303  unsigned char *o2,
304  unsigned char *o3)
305 {
306  *o0 = vtkBase64UtilitiesEncodeChar((i0 >> 2) & 0x3F);
307  *o1 = vtkBase64UtilitiesEncodeChar(((i0 << 4) & 0x30)|((i1 >> 4) & 0x0F));
308  *o2 = vtkBase64UtilitiesEncodeChar(((i1 << 2) & 0x3C)|((i2 >> 6) & 0x03));
309  *o3 = vtkBase64UtilitiesEncodeChar(i2 & 0x3F);
310 }
311 
312 //----------------------------------------------------------------------------
313 static void EncodePair(unsigned char i0,
314  unsigned char i1,
315  unsigned char *o0,
316  unsigned char *o1,
317  unsigned char *o2,
318  unsigned char *o3)
319 {
320  *o0 = vtkBase64UtilitiesEncodeChar((i0 >> 2) & 0x3F);
321  *o1 = vtkBase64UtilitiesEncodeChar(((i0 << 4) & 0x30)|((i1 >> 4) & 0x0F));
322  *o2 = vtkBase64UtilitiesEncodeChar(((i1 << 2) & 0x3C));
323  *o3 = '=';
324 }
325 
326 //----------------------------------------------------------------------------
327 static void EncodeSingle(unsigned char i0,
328  unsigned char *o0,
329  unsigned char *o1,
330  unsigned char *o2,
331  unsigned char *o3)
332 {
333  *o0 = vtkBase64UtilitiesEncodeChar((i0 >> 2) & 0x3F);
334  *o1 = vtkBase64UtilitiesEncodeChar(((i0 << 4) & 0x30));
335  *o2 = '=';
336  *o3 = '=';
337 }
338 
339 //----------------------------------------------------------------------------
340 static unsigned long EncodeToBase64(const unsigned char *input,
341  unsigned long length,
342  unsigned char *output,
343  int mark_end)
344 {
345 
346  const unsigned char *ptr = input;
347  const unsigned char *end = input + length;
348  unsigned char *optr = output;
349 
350  // Encode complete triplet
351 
352  while ((end - ptr) >= 3)
353  {
354  EncodeTriplet(ptr[0], ptr[1], ptr[2],
355  &optr[0], &optr[1], &optr[2], &optr[3]);
356  ptr += 3;
357  optr += 4;
358  }
359 
360  // Encodes a 2-byte ending into 3 bytes and 1 pad byte and writes.
361 
362  if (end - ptr == 2)
363  {
364  EncodePair(ptr[0], ptr[1],
365  &optr[0], &optr[1], &optr[2], &optr[3]);
366  optr += 4;
367  }
368 
369  // Encodes a 1-byte ending into 2 bytes and 2 pad bytes
370 
371  else if (end - ptr == 1)
372  {
373  EncodeSingle(ptr[0],
374  &optr[0], &optr[1], &optr[2], &optr[3]);
375  optr += 4;
376  }
377 
378  // Do we need to mark the end
379 
380  else if (mark_end)
381  {
382  optr[0] = optr[1] = optr[2] = optr[3] = '=';
383  optr += 4;
384  }
385 
386  return optr - output;
387 }
388 
389 
390 #endif /* UTIL_HPP_ */
float B
Blue.
Definition: util.hpp:47
std::string toString()
Return the color as string.
Definition: util.hpp:50
SimpleRNG is a simple random number generator based on George Marsaglia's MWC (multiply with carry) g...
Definition: SimpleRNG.hpp:18
float R
Red.
Definition: util.hpp:41
KeyT const ValueT ValueT OffsetIteratorT OffsetIteratorT int
[in] The number of segments that comprise the sorting data
RGB color struct.
Definition: util.hpp:38
float G
Green.
Definition: util.hpp:44